TaxSummary.php
4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?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;
use Date;
class TaxSummary extends Controller
{
use Currencies;
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$dates = $incomes = $expenses = $totals = [];
$status = request('status');
$t = Tax::enabled()->where('rate', '<>', '0')->pluck('name')->toArray();
$taxes = array_combine($t, $t);
// Get year
$year = request('year');
if (empty($year)) {
$year = Date::now()->year;
}
// Dates
for ($j = 1; $j <= 12; $j++) {
$dates[$j] = Date::parse($year . '-' . $j)->format('M');
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
$invoices = InvoicePayment::with(['invoice', 'invoice.totals'])->monthsOfYear('paid_at')->get();
$this->setAmount($incomes, $totals, $invoices, 'invoice', 'paid_at');
// Bills
$bills = BillPayment::with(['bill', 'bill.totals'])->monthsOfYear('paid_at')->get();
$this->setAmount($expenses, $totals, $bills, 'bill', 'paid_at');
break;
case 'upcoming':
// Invoices
$invoices = Invoice::with(['totals'])->accrued()->monthsOfYear('due_at')->get();
$this->setAmount($incomes, $totals, $invoices, 'invoice', 'due_at');
// Bills
$bills = Bill::with(['totals'])->accrued()->monthsOfYear('due_at')->get();
$this->setAmount($expenses, $totals, $bills, 'bill', 'due_at');
break;
default:
// Invoices
$invoices = Invoice::with(['totals'])->accrued()->monthsOfYear('invoiced_at')->get();
$this->setAmount($incomes, $totals, $invoices, 'invoice', 'invoiced_at');
// Bills
$bills = Bill::with(['totals'])->accrued()->monthsOfYear('billed_at')->get();
$this->setAmount($expenses, $totals, $bills, 'bill', 'billed_at');
break;
}
// 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';
}
return view($view_template, compact('dates', 'taxes', 'incomes', 'expenses', 'totals'));
}
private function setAmount(&$items, &$totals, $rows, $type, $date_field)
{
foreach ($rows as $row) {
if ($row['table'] == 'bill_payments' || $row['table'] == 'invoice_payments') {
$type_row = $row->$type;
$row->category_id = $type_row->category_id;
}
$date = Date::parse($row->$date_field)->format('M');
if ($date_field == 'paid_at') {
$row_totals = $row->$type->totals;
} else {
$row_totals = $row->totals;
}
foreach ($row_totals as $row_total) {
if ($row_total->code != 'tax') {
continue;
}
if (!isset($items[$row_total->name])) {
continue;
}
$amount = $this->convert($row_total->amount, $row->currency_code, $row->currency_rate);
$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;
}
}
}
}
}