App\Http\Controllers\ReportController::generateTaxRateReport PHP Method

generateTaxRateReport() private method

private generateTaxRateReport ( $startDate, $endDate, $dateField, $isExport ) : array
$startDate
$endDate
$dateField
$isExport
return array
    private function generateTaxRateReport($startDate, $endDate, $dateField, $isExport)
    {
        $columns = ['tax_name', 'tax_rate', 'amount', 'paid'];
        $account = Auth::user()->account;
        $displayData = [];
        $reportTotals = [];
        $clients = Client::scope()->withArchived()->with('contacts')->with(['invoices' => function ($query) use($startDate, $endDate, $dateField) {
            $query->with('invoice_items')->withArchived();
            if ($dateField == FILTER_INVOICE_DATE) {
                $query->where('invoice_date', '>=', $startDate)->where('invoice_date', '<=', $endDate)->with('payments');
            } else {
                $query->whereHas('payments', function ($query) use($startDate, $endDate) {
                    $query->where('payment_date', '>=', $startDate)->where('payment_date', '<=', $endDate)->withArchived();
                })->with(['payments' => function ($query) use($startDate, $endDate) {
                    $query->where('payment_date', '>=', $startDate)->where('payment_date', '<=', $endDate)->withArchived();
                }]);
            }
        }]);
        foreach ($clients->get() as $client) {
            $currencyId = $client->currency_id ?: Auth::user()->account->getCurrencyId();
            $amount = 0;
            $paid = 0;
            $taxTotals = [];
            foreach ($client->invoices as $invoice) {
                foreach ($invoice->getTaxes(true) as $key => $tax) {
                    if (!isset($taxTotals[$currencyId])) {
                        $taxTotals[$currencyId] = [];
                    }
                    if (isset($taxTotals[$currencyId][$key])) {
                        $taxTotals[$currencyId][$key]['amount'] += $tax['amount'];
                        $taxTotals[$currencyId][$key]['paid'] += $tax['paid'];
                    } else {
                        $taxTotals[$currencyId][$key] = $tax;
                    }
                }
                $amount += $invoice->amount;
                $paid += $invoice->getAmountPaid();
            }
            foreach ($taxTotals as $currencyId => $taxes) {
                foreach ($taxes as $tax) {
                    $displayData[] = [$tax['name'], $tax['rate'] . '%', $account->formatMoney($tax['amount'], $client), $account->formatMoney($tax['paid'], $client)];
                }
                $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'amount', $tax['amount']);
                $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'paid', $tax['paid']);
            }
        }
        return ['columns' => $columns, 'displayData' => $displayData, 'reportTotals' => $reportTotals];
    }