2017-01-22 11:09:29 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Ninja\Reports;
|
|
|
|
|
|
|
|
use App\Models\Client;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Auth;
|
2017-01-22 11:09:29 +01:00
|
|
|
|
|
|
|
class TaxRateReport extends AbstractReport
|
|
|
|
{
|
2018-01-21 14:53:43 +01:00
|
|
|
public function getColumns()
|
|
|
|
{
|
|
|
|
return [
|
2018-01-23 19:32:11 +01:00
|
|
|
'client' => [],
|
|
|
|
'invoice' => [],
|
|
|
|
'tax_name' => [],
|
|
|
|
'tax_rate' => [],
|
|
|
|
'tax_amount' => [],
|
|
|
|
'tax_paid' => [],
|
2018-01-21 20:12:49 +01:00
|
|
|
'invoice_amount' => ['columnSelector-false'],
|
|
|
|
'payment_amount' => ['columnSelector-false'],
|
2018-01-21 14:53:43 +01:00
|
|
|
];
|
|
|
|
}
|
2017-01-22 11:09:29 +01:00
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$account = Auth::user()->account;
|
2018-02-28 15:57:10 +01:00
|
|
|
$subgroup = $this->options['subgroup'];
|
2017-01-22 11:09:29 +01:00
|
|
|
|
|
|
|
$clients = Client::scope()
|
2017-03-31 08:01:07 +02:00
|
|
|
->orderBy('name')
|
2017-01-22 11:09:29 +01:00
|
|
|
->withArchived()
|
2018-02-28 15:57:10 +01:00
|
|
|
->with('contacts', 'user')
|
2017-01-30 17:05:31 +01:00
|
|
|
->with(['invoices' => function ($query) {
|
2017-02-15 10:42:25 +01:00
|
|
|
$query->with('invoice_items')
|
|
|
|
->withArchived()
|
|
|
|
->invoices()
|
|
|
|
->where('is_public', '=', true);
|
2017-01-22 11:09:29 +01:00
|
|
|
if ($this->options['date_field'] == FILTER_INVOICE_DATE) {
|
|
|
|
$query->where('invoice_date', '>=', $this->startDate)
|
|
|
|
->where('invoice_date', '<=', $this->endDate)
|
|
|
|
->with('payments');
|
|
|
|
} else {
|
2017-01-30 17:05:31 +01:00
|
|
|
$query->whereHas('payments', function ($query) {
|
|
|
|
$query->where('payment_date', '>=', $this->startDate)
|
2017-01-22 11:09:29 +01:00
|
|
|
->where('payment_date', '<=', $this->endDate)
|
|
|
|
->withArchived();
|
2017-01-30 17:05:31 +01:00
|
|
|
})
|
|
|
|
->with(['payments' => function ($query) {
|
2017-01-22 11:09:29 +01:00
|
|
|
$query->where('payment_date', '>=', $this->startDate)
|
|
|
|
->where('payment_date', '<=', $this->endDate)
|
|
|
|
->withArchived();
|
|
|
|
}]);
|
|
|
|
}
|
|
|
|
}]);
|
|
|
|
|
|
|
|
foreach ($clients->get() as $client) {
|
|
|
|
$currencyId = $client->currency_id ?: Auth::user()->account->getCurrencyId();
|
|
|
|
|
|
|
|
foreach ($client->invoices as $invoice) {
|
2017-02-15 10:42:25 +01:00
|
|
|
$taxTotals = [];
|
|
|
|
|
2017-01-22 11:09:29 +01:00
|
|
|
foreach ($invoice->getTaxes(true) as $key => $tax) {
|
2017-01-30 17:05:31 +01:00
|
|
|
if (! isset($taxTotals[$currencyId])) {
|
2017-01-22 11:09:29 +01:00
|
|
|
$taxTotals[$currencyId] = [];
|
|
|
|
}
|
|
|
|
if (isset($taxTotals[$currencyId][$key])) {
|
|
|
|
$taxTotals[$currencyId][$key]['amount'] += $tax['amount'];
|
|
|
|
$taxTotals[$currencyId][$key]['paid'] += $tax['paid'];
|
|
|
|
} else {
|
|
|
|
$taxTotals[$currencyId][$key] = $tax;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 10:42:25 +01:00
|
|
|
foreach ($taxTotals as $currencyId => $taxes) {
|
|
|
|
foreach ($taxes as $tax) {
|
|
|
|
$this->data[] = [
|
2017-10-06 15:44:48 +02:00
|
|
|
$this->isExport ? $client->getDisplayName() : $client->present()->link,
|
|
|
|
$this->isExport ? $invoice->invoice_number : $invoice->present()->link,
|
2017-02-15 10:42:25 +01:00
|
|
|
$tax['name'],
|
|
|
|
$tax['rate'] . '%',
|
|
|
|
$account->formatMoney($tax['amount'], $client),
|
|
|
|
$account->formatMoney($tax['paid'], $client),
|
2018-01-21 20:12:49 +01:00
|
|
|
$invoice->present()->amount,
|
|
|
|
$invoice->present()->paid,
|
2017-02-15 10:42:25 +01:00
|
|
|
];
|
2017-01-22 11:09:29 +01:00
|
|
|
|
2017-02-15 10:46:09 +01:00
|
|
|
$this->addToTotals($client->currency_id, 'amount', $tax['amount']);
|
|
|
|
$this->addToTotals($client->currency_id, 'paid', $tax['paid']);
|
2018-02-28 15:57:10 +01:00
|
|
|
|
|
|
|
$dimension = $this->getDimension($client);
|
|
|
|
$this->addChartData($dimension, $invoice->invoice_date, $tax['amount']);
|
2017-02-15 10:46:09 +01:00
|
|
|
}
|
2017-01-22 11:09:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|