-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Payables.php
79 lines (58 loc) · 2.33 KB
/
Payables.php
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
<?php
namespace App\Widgets;
use App\Abstracts\Widget;
use App\Models\Document\Document;
use App\Utilities\Date;
class Payables extends Widget
{
public $default_name = 'widgets.payables';
public $description = 'widgets.description.payables';
public $report_class = 'Modules\AgedReceivablesPayables\Reports\AgedPayables';
public function show()
{
$open = $overdue = 0;
$periods = [
'overdue_1_30' => 0,
'overdue_30_60' => 0,
'overdue_60_90' => 0,
'overdue_90_un' => 0,
];
$query = Document::bill()->with('transactions')->accrued()->notPaid();
$this->applyFilters($query, ['date_field' => 'issued_at'])->each(function ($bill) use (&$open, &$overdue, &$periods) {
list($open_tmp, $overdue_tmp) = $this->calculateDocumentTotals($bill);
$open += $open_tmp;
$overdue += $overdue_tmp;
foreach ($periods as $period_name => $period_amount) {
$arr = explode('_', $period_name);
if ($arr[2] == 'un') {
$arr[2] = '9999';
}
$start = Date::today()->subDays($arr[2])->startOfDay()->toDateString();
$end = Date::today()->subDays($arr[1])->endOfDay()->toDateString();
if (! Date::parse($bill->due_at)->isBetween($start, $end)) {
continue;
}
$periods[$period_name] += $overdue_tmp;
}
});
foreach ($periods as $period_name => $period_amount) {
$periods[$period_name] = money($period_amount);
}
$has_progress = !empty($open) || !empty($overdue);
$progress = !empty($open) ? (int) ($open * 100) / ($open + $overdue) : 0;
$grand = $open + $overdue;
$totals = [
'grand' => money($grand),
'open' => money($open),
'overdue' => money($overdue),
];
$grand_total_text = trans('widgets.total_unpaid_bills');
return $this->view('widgets.receivables_payables', [
'totals' => $totals,
'has_progress' => $has_progress,
'progress' => $progress,
'periods' => $periods,
'grand_total_text' => $grand_total_text,
]);
}
}