166 lines
5.7 KiB
PHP
Raw Normal View History

2018-04-16 12:15:51 +03:00
<?php
namespace App\Http\Controllers\Reports;
use App\Http\Controllers\Controller;
use App\Models\Expense\Bill;
use App\Models\Expense\BillPayment;
use App\Models\Expense\BillTotal;
use App\Models\Income\Invoice;
use App\Models\Income\InvoicePayment;
use App\Models\Income\InvoiceTotal;
use App\Models\Setting\Tax;
use App\Traits\Currencies;
2018-12-31 14:41:17 +03:00
use App\Traits\DateTime;
2018-04-16 12:15:51 +03:00
use Date;
class TaxSummary extends Controller
{
2018-12-31 14:41:17 +03:00
use Currencies, DateTime;
2018-04-16 12:15:51 +03:00
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$dates = $incomes = $expenses = $totals = [];
$status = request('status');
2018-11-01 16:58:31 +03:00
$year = request('year', Date::now()->year);
2018-12-14 20:02:35 +05:30
// check and assign year start
2018-12-31 14:41:17 +03:00
$financial_start = $this->getFinancialStart();
2018-12-27 19:48:23 +03:00
if ($financial_start->month != 1) {
2018-12-14 20:02:35 +05:30
// check if a specific year is requested
if (!is_null(request('year'))) {
$financial_start->year = $year;
}
$year = [$financial_start->format('Y'), $financial_start->addYear()->format('Y')];
$financial_start->subYear()->subMonth();
}
2018-04-16 12:15:51 +03:00
$t = Tax::enabled()->where('rate', '<>', '0')->pluck('name')->toArray();
$taxes = array_combine($t, $t);
// Dates
for ($j = 1; $j <= 12; $j++) {
2018-12-14 20:02:35 +05:30
$ym_string = is_array($year) ? $financial_start->addMonth()->format('Y-m') : $year . '-' . $j;
$dates[$j] = Date::parse($ym_string)->format('M');
2018-04-16 12:15:51 +03:00
foreach ($taxes as $tax_name) {
$incomes[$tax_name][$dates[$j]] = [
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1,
];
$expenses[$tax_name][$dates[$j]] = [
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1,
];
$totals[$tax_name][$dates[$j]] = [
'amount' => 0,
'currency_code' => setting('general.default_currency'),
'currency_rate' => 1,
];
}
}
switch ($status) {
case 'paid':
// Invoices
2018-04-16 14:37:19 +03:00
$invoices = InvoicePayment::with(['invoice', 'invoice.totals'])->monthsOfYear('paid_at')->get();
2018-04-16 12:15:51 +03:00
$this->setAmount($incomes, $totals, $invoices, 'invoice', 'paid_at');
// Bills
2018-04-16 14:37:19 +03:00
$bills = BillPayment::with(['bill', 'bill.totals'])->monthsOfYear('paid_at')->get();
2018-04-16 12:15:51 +03:00
$this->setAmount($expenses, $totals, $bills, 'bill', 'paid_at');
break;
case 'upcoming':
// Invoices
2018-04-16 14:37:19 +03:00
$invoices = Invoice::with(['totals'])->accrued()->monthsOfYear('due_at')->get();
2018-04-16 12:15:51 +03:00
$this->setAmount($incomes, $totals, $invoices, 'invoice', 'due_at');
// Bills
2018-04-16 14:37:19 +03:00
$bills = Bill::with(['totals'])->accrued()->monthsOfYear('due_at')->get();
2018-04-16 12:15:51 +03:00
$this->setAmount($expenses, $totals, $bills, 'bill', 'due_at');
break;
default:
// Invoices
2018-04-16 14:37:19 +03:00
$invoices = Invoice::with(['totals'])->accrued()->monthsOfYear('invoiced_at')->get();
2018-04-16 12:15:51 +03:00
$this->setAmount($incomes, $totals, $invoices, 'invoice', 'invoiced_at');
// Bills
2018-04-16 14:37:19 +03:00
$bills = Bill::with(['totals'])->accrued()->monthsOfYear('billed_at')->get();
2018-04-16 12:15:51 +03:00
$this->setAmount($expenses, $totals, $bills, 'bill', 'billed_at');
break;
}
2018-11-01 16:58:31 +03:00
$statuses = collect([
'all' => trans('general.all'),
'paid' => trans('invoices.paid'),
'upcoming' => trans('general.upcoming'),
]);
2018-04-16 12:15:51 +03:00
// Check if it's a print or normal request
if (request('print')) {
$view_template = 'reports.tax_summary.print';
} else {
$view_template = 'reports.tax_summary.index';
}
2018-11-01 16:58:31 +03:00
return view($view_template, compact('dates', 'taxes', 'incomes', 'expenses', 'totals', 'statuses'));
2018-04-16 12:15:51 +03:00
}
private function setAmount(&$items, &$totals, $rows, $type, $date_field)
{
foreach ($rows as $row) {
2019-01-10 12:19:21 +03:00
if (($row->getTable() == 'bill_payments') || ($row->getTable() == 'invoice_payments')) {
$type_row = $row->$type;
$row->category_id = $type_row->category_id;
}
2018-04-16 12:15:51 +03:00
$date = Date::parse($row->$date_field)->format('M');
if ($date_field == 'paid_at') {
2018-04-16 14:37:19 +03:00
$row_totals = $row->$type->totals;
2018-04-16 12:15:51 +03:00
} else {
$row_totals = $row->totals;
}
foreach ($row_totals as $row_total) {
if ($row_total->code != 'tax') {
continue;
}
if (!isset($items[$row_total->name])) {
continue;
}
2018-11-06 17:34:53 +03:00
if ($date_field == 'paid_at') {
$rate = ($row->amount * 100) / $type_row->amount;
$row_amount = ($row_total->amount / 100) * $rate;
} else {
$row_amount = $row_total->amount;
}
$amount = $this->convert($row_amount, $row->currency_code, $row->currency_rate);
2018-04-16 12:15:51 +03:00
$items[$row_total->name][$date]['amount'] += $amount;
if ($type == 'invoice') {
$totals[$row_total->name][$date]['amount'] += $amount;
} else {
$totals[$row_total->name][$date]['amount'] -= $amount;
}
}
}
}
}