From 556a90c8eff2c3f157ccb4d19a3f0547b4c076da Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Wed, 15 Feb 2017 11:42:47 +0200 Subject: [PATCH] Improve tax report #1351 --- app/Ninja/Reports/TaxRateReport.php | 51 +++++++++++++++-------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/app/Ninja/Reports/TaxRateReport.php b/app/Ninja/Reports/TaxRateReport.php index 800914c841da..90e1dd8309e0 100644 --- a/app/Ninja/Reports/TaxRateReport.php +++ b/app/Ninja/Reports/TaxRateReport.php @@ -2,12 +2,13 @@ namespace App\Ninja\Reports; -use Auth; use App\Models\Client; +use Auth; class TaxRateReport extends AbstractReport { public $columns = [ + 'invoice', 'tax_name', 'tax_rate', 'amount', @@ -21,19 +22,22 @@ class TaxRateReport extends AbstractReport $clients = Client::scope() ->withArchived() ->with('contacts') - ->with(['invoices' => function($query) { - $query->with('invoice_items')->withArchived(); + ->with(['invoices' => function ($query) { + $query->with('invoice_items') + ->withArchived() + ->invoices() + ->where('is_public', '=', true); if ($this->options['date_field'] == FILTER_INVOICE_DATE) { $query->where('invoice_date', '>=', $this->startDate) ->where('invoice_date', '<=', $this->endDate) ->with('payments'); } else { - $query->whereHas('payments', function($query) { - $query->where('payment_date', '>=', $this->startDate) + $query->whereHas('payments', function ($query) { + $query->where('payment_date', '>=', $this->startDate) ->where('payment_date', '<=', $this->endDate) ->withArchived(); - }) - ->with(['payments' => function($query) { + }) + ->with(['payments' => function ($query) { $query->where('payment_date', '>=', $this->startDate) ->where('payment_date', '<=', $this->endDate) ->withArchived(); @@ -43,13 +47,12 @@ class TaxRateReport extends AbstractReport foreach ($clients->get() as $client) { $currencyId = $client->currency_id ?: Auth::user()->account->getCurrencyId(); - $amount = 0; - $paid = 0; - $taxTotals = []; foreach ($client->invoices as $invoice) { + $taxTotals = []; + foreach ($invoice->getTaxes(true) as $key => $tax) { - if ( ! isset($taxTotals[$currencyId])) { + if (! isset($taxTotals[$currencyId])) { $taxTotals[$currencyId] = []; } if (isset($taxTotals[$currencyId][$key])) { @@ -60,22 +63,20 @@ class TaxRateReport extends AbstractReport } } - $amount += $invoice->amount; - $paid += $invoice->getAmountPaid(); - } + foreach ($taxTotals as $currencyId => $taxes) { + foreach ($taxes as $tax) { + $this->data[] = [ + $invoice->present()->link, + $tax['name'], + $tax['rate'] . '%', + $account->formatMoney($tax['amount'], $client), + $account->formatMoney($tax['paid'], $client), + ]; + } - foreach ($taxTotals as $currencyId => $taxes) { - foreach ($taxes as $tax) { - $this->data[] = [ - $tax['name'], - $tax['rate'] . '%', - $account->formatMoney($tax['amount'], $client), - $account->formatMoney($tax['paid'], $client) - ]; + $this->addToTotals($client->currency_id, 'amount', $invoice->amount); + $this->addToTotals($client->currency_id, 'paid', $invoice->getAmountPaid()); } - - $this->addToTotals($client->currency_id, 'amount', $tax['amount']); - $this->addToTotals($client->currency_id, 'paid', $tax['paid']); } } }