akaunting/app/Reports/TaxSummary.php

128 lines
4.4 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Reports;
2019-11-23 21:47:20 +03:00
use App\Abstracts\Report;
2019-11-16 10:21:14 +03:00
use App\Models\Banking\Transaction;
2019-12-31 15:49:09 +03:00
use App\Models\Purchase\Bill;
use App\Models\Sale\Invoice;
2019-11-16 10:21:14 +03:00
use App\Models\Setting\Tax;
use App\Traits\Currencies;
use App\Utilities\Recurring;
use Date;
class TaxSummary extends Report
{
use Currencies;
2020-01-16 15:39:37 +03:00
public $default_name = 'reports.summary.tax';
2019-11-16 10:21:14 +03:00
2020-01-16 15:39:37 +03:00
public $category = 'general.accounting';
2019-11-16 10:21:14 +03:00
2020-01-16 15:39:37 +03:00
public $icon = 'fa fa-percent';
2019-11-16 10:21:14 +03:00
public function setViews()
{
parent::setViews();
$this->views['content.header'] = 'reports.tax_summary.content.header';
2020-01-28 15:51:36 +03:00
$this->views['content.footer'] = 'reports.tax_summary.content.footer';
2019-11-16 10:21:14 +03:00
$this->views['table.header'] = 'reports.tax_summary.table.header';
$this->views['table.footer'] = 'reports.tax_summary.table.footer';
}
public function setTables()
{
$taxes = Tax::enabled()->where('rate', '<>', '0')->orderBy('name')->pluck('name')->toArray();
$this->tables = array_combine($taxes, $taxes);
}
2020-01-29 01:06:12 +03:00
public function setData()
2019-11-16 10:21:14 +03:00
{
2020-01-16 15:39:37 +03:00
switch ($this->model->settings->basis) {
2019-11-16 10:21:14 +03:00
case 'cash':
// Invoice Payments
2020-06-06 21:47:02 +03:00
$invoices = $this->applyFilters(Transaction::with(['recurring', 'invoice', 'invoice.totals'])->income()->isDocument()->isNotTransfer(), ['date_field' => 'paid_at'])->get();
2019-11-16 10:21:14 +03:00
$this->setTotals($invoices, 'paid_at');
// Bill Payments
2020-06-06 21:47:02 +03:00
$bills = $this->applyFilters(Transaction::with(['recurring', 'bill', 'bill.totals'])->expense()->isDocument()->isNotTransfer(), ['date_field' => 'paid_at'])->get();
2019-11-16 10:21:14 +03:00
$this->setTotals($bills, 'paid_at');
break;
default:
// Invoices
2020-06-06 21:47:02 +03:00
$invoices = $this->applyFilters(Invoice::with(['recurring', 'transactions', 'totals'])->accrued(), ['date_field' => 'invoiced_at'])->get();
2020-01-18 13:11:19 +03:00
Recurring::reflect($invoices, 'invoiced_at');
2019-11-16 10:21:14 +03:00
$this->setTotals($invoices, 'invoiced_at');
// Bills
2020-06-06 21:47:02 +03:00
$bills = $this->applyFilters(Bill::with(['recurring', 'transactions', 'totals'])->accrued(), ['date_field' => 'billed_at'])->get();
2020-01-18 13:11:19 +03:00
Recurring::reflect($bills, 'billed_at');
2019-11-16 10:21:14 +03:00
$this->setTotals($bills, 'billed_at');
break;
}
}
2020-05-16 11:37:56 +03:00
public function setTotals($items, $date_field, $check_type = false, $table = 'default', $with_tax = true)
2019-11-16 10:21:14 +03:00
{
foreach ($items as $item) {
// Make groups extensible
$item = $this->applyGroups($item);
2020-01-18 13:11:19 +03:00
$type = (($item instanceof Invoice) || (($item instanceof Transaction) && ($item->type == 'income'))) ? 'income' : 'expense';
2019-11-16 10:21:14 +03:00
$date = $this->getFormattedDate(Date::parse($item->$date_field));
if ($date_field == 'paid_at') {
$document_type = ($item->type == 'income') ? 'invoice' : 'bill';
$item_totals = $item->$document_type->totals;
$type_item = $item->$document_type;
} else {
$item_totals = $item->totals;
}
foreach ($item_totals as $item_total) {
if ($item_total->code != 'tax') {
continue;
}
2020-01-27 22:41:08 +03:00
if (
!isset($this->row_values[$item_total->name][$type][$date])
2020-02-21 18:03:23 +03:00
|| !isset($this->footer_totals[$item_total->name][$date])
) {
2019-11-16 10:21:14 +03:00
continue;
}
if ($date_field == 'paid_at') {
$rate = ($item->amount * 100) / $type_item->amount;
$item_amount = ($item_total->amount / 100) * $rate;
} else {
$item_amount = $item_total->amount;
}
$amount = $this->convertToDefault($item_amount, $item->currency_code, $item->currency_rate);
if ($type == 'income') {
2020-01-27 22:41:08 +03:00
$this->row_values[$item_total->name][$type][$date] += $amount;
2019-11-16 10:21:14 +03:00
2020-01-27 22:41:08 +03:00
$this->footer_totals[$item_total->name][$date] += $amount;
2019-11-16 10:21:14 +03:00
} else {
2020-01-27 22:41:08 +03:00
$this->row_values[$item_total->name][$type][$date] -= $amount;
2019-11-16 10:21:14 +03:00
2020-01-27 22:41:08 +03:00
$this->footer_totals[$item_total->name][$date] -= $amount;
2019-11-16 10:21:14 +03:00
}
}
}
}
public function getFields()
{
return [
$this->getPeriodField(),
$this->getBasisField(),
];
}
2019-11-16 10:21:14 +03:00
}