Improve tax report #1351

This commit is contained in:
Hillel Coren 2017-02-15 11:42:47 +02:00
parent c2bf1b6a04
commit 556a90c8ef

View File

@ -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->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)
$account->formatMoney($tax['paid'], $client),
];
}
$this->addToTotals($client->currency_id, 'amount', $tax['amount']);
$this->addToTotals($client->currency_id, 'paid', $tax['paid']);
$this->addToTotals($client->currency_id, 'amount', $invoice->amount);
$this->addToTotals($client->currency_id, 'paid', $invoice->getAmountPaid());
}
}
}
}