private function rawChartData($entityType, $account, $groupBy, $startDate, $endDate, $currencyId)
{
if (!in_array($groupBy, ['DAYOFYEAR', 'WEEK', 'MONTH'])) {
return [];
}
$accountId = $account->id;
$currencyId = intval($currencyId);
$timeframe = 'concat(YEAR(' . $entityType . '_date), ' . $groupBy . '(' . $entityType . '_date))';
$records = DB::table($entityType . 's')->leftJoin('clients', 'clients.id', '=', $entityType . 's.client_id')->whereRaw('(clients.id IS NULL OR clients.is_deleted = 0)')->where($entityType . 's.account_id', '=', $accountId)->where($entityType . 's.is_deleted', '=', false)->where($entityType . 's.' . $entityType . '_date', '>=', $startDate->format('Y-m-d'))->where($entityType . 's.' . $entityType . '_date', '<=', $endDate->format('Y-m-d'))->groupBy($groupBy);
if ($entityType == ENTITY_EXPENSE) {
$records->where('expenses.expense_currency_id', '=', $currencyId);
} elseif ($currencyId == $account->getCurrencyId()) {
$records->whereRaw("(clients.currency_id = {$currencyId} or coalesce(clients.currency_id, 0) = 0)");
} else {
$records->where('clients.currency_id', '=', $currencyId);
}
if ($entityType == ENTITY_INVOICE) {
$records->select(DB::raw('sum(invoices.amount) as total, sum(invoices.balance) as balance, count(invoices.id) as count, ' . $timeframe . ' as ' . $groupBy))->where('invoice_type_id', '=', INVOICE_TYPE_STANDARD)->where('is_recurring', '=', false);
} elseif ($entityType == ENTITY_PAYMENT) {
$records->select(DB::raw('sum(payments.amount - payments.refunded) as total, count(payments.id) as count, ' . $timeframe . ' as ' . $groupBy))->join('invoices', 'invoices.id', '=', 'payments.invoice_id')->where('invoices.is_deleted', '=', false)->whereNotIn('payment_status_id', [PAYMENT_STATUS_VOIDED, PAYMENT_STATUS_FAILED]);
} elseif ($entityType == ENTITY_EXPENSE) {
$records->select(DB::raw('sum(expenses.amount + (expenses.amount * expenses.tax_rate1 / 100) + (expenses.amount * expenses.tax_rate2 / 100)) as total, count(expenses.id) as count, ' . $timeframe . ' as ' . $groupBy));
}
return $records->get();
}