|
@@ -21,8 +21,47 @@ class BillOverview extends EnhancedStatsOverviewWidget
|
21
|
21
|
|
22
|
22
|
protected function getStats(): array
|
23
|
23
|
{
|
|
24
|
+ $activeTab = $this->activeTab;
|
|
25
|
+
|
|
26
|
+ $averagePaymentTimeFormatted = '-';
|
|
27
|
+ $averagePaymentTimeSuffix = null;
|
|
28
|
+ $lastMonthTotal = '-';
|
|
29
|
+ $lastMonthTotalSuffix = null;
|
|
30
|
+
|
|
31
|
+ if ($activeTab !== 'unpaid') {
|
|
32
|
+ $averagePaymentTime = $this->getPageTableQuery()
|
|
33
|
+ ->whereNotNull('paid_at')
|
|
34
|
+ ->selectRaw('AVG(TIMESTAMPDIFF(DAY, date, paid_at)) as avg_days')
|
|
35
|
+ ->value('avg_days');
|
|
36
|
+
|
|
37
|
+ $averagePaymentTimeFormatted = Number::format($averagePaymentTime ?? 0, maxPrecision: 1);
|
|
38
|
+ $averagePaymentTimeSuffix = 'days';
|
|
39
|
+
|
|
40
|
+ $lastMonthPaid = $this->getPageTableQuery()
|
|
41
|
+ ->whereBetween('date', [
|
|
42
|
+ today()->subMonth()->startOfMonth(),
|
|
43
|
+ today()->subMonth()->endOfMonth(),
|
|
44
|
+ ])
|
|
45
|
+ ->get()
|
|
46
|
+ ->sumMoneyInDefaultCurrency('amount_paid');
|
|
47
|
+
|
|
48
|
+ $lastMonthTotal = CurrencyConverter::formatCentsToMoney($lastMonthPaid);
|
|
49
|
+ $lastMonthTotalSuffix = CurrencyAccessor::getDefaultCurrency();
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ if ($activeTab === 'paid') {
|
|
53
|
+ return [
|
|
54
|
+ EnhancedStatsOverviewWidget\EnhancedStat::make('Total To Pay', '-'),
|
|
55
|
+ EnhancedStatsOverviewWidget\EnhancedStat::make('Due Within 7 Days', '-'),
|
|
56
|
+ EnhancedStatsOverviewWidget\EnhancedStat::make('Average Payment Time', $averagePaymentTimeFormatted)
|
|
57
|
+ ->suffix($averagePaymentTimeSuffix),
|
|
58
|
+ EnhancedStatsOverviewWidget\EnhancedStat::make('Paid Last Month', $lastMonthTotal)
|
|
59
|
+ ->suffix($lastMonthTotalSuffix),
|
|
60
|
+ ];
|
|
61
|
+ }
|
|
62
|
+
|
24
|
63
|
$unpaidBills = $this->getPageTableQuery()
|
25
|
|
- ->whereIn('status', [BillStatus::Open, BillStatus::Partial, BillStatus::Overdue]);
|
|
64
|
+ ->unpaid();
|
26
|
65
|
|
27
|
66
|
$amountToPay = $unpaidBills->get()->sumMoneyInDefaultCurrency('amount_due');
|
28
|
67
|
|
|
@@ -38,35 +77,16 @@ class BillOverview extends EnhancedStatsOverviewWidget
|
38
|
77
|
->get()
|
39
|
78
|
->sumMoneyInDefaultCurrency('amount_due');
|
40
|
79
|
|
41
|
|
- $averagePaymentTime = $this->getPageTableQuery()
|
42
|
|
- ->whereNotNull('paid_at')
|
43
|
|
- ->selectRaw('AVG(TIMESTAMPDIFF(DAY, date, paid_at)) as avg_days')
|
44
|
|
- ->value('avg_days');
|
45
|
|
-
|
46
|
|
- $averagePaymentTimeFormatted = Number::format($averagePaymentTime ?? 0, maxPrecision: 1);
|
47
|
|
-
|
48
|
|
- $lastMonthTotal = $this->getPageTableQuery()
|
49
|
|
- ->where('status', BillStatus::Paid)
|
50
|
|
- ->whereBetween('date', [
|
51
|
|
- today()->subMonth()->startOfMonth(),
|
52
|
|
- today()->subMonth()->endOfMonth(),
|
53
|
|
- ])
|
54
|
|
- ->get()
|
55
|
|
- ->sumMoneyInDefaultCurrency('amount_paid');
|
56
|
|
-
|
57
|
80
|
return [
|
58
|
81
|
EnhancedStatsOverviewWidget\EnhancedStat::make('Total To Pay', CurrencyConverter::formatCentsToMoney($amountToPay))
|
59
|
82
|
->suffix(CurrencyAccessor::getDefaultCurrency())
|
60
|
83
|
->description('Includes ' . CurrencyConverter::formatCentsToMoney($amountOverdue) . ' overdue'),
|
61
|
|
-
|
62
|
84
|
EnhancedStatsOverviewWidget\EnhancedStat::make('Due Within 7 Days', CurrencyConverter::formatCentsToMoney($amountDueWithin7Days))
|
63
|
85
|
->suffix(CurrencyAccessor::getDefaultCurrency()),
|
64
|
|
-
|
65
|
86
|
EnhancedStatsOverviewWidget\EnhancedStat::make('Average Payment Time', $averagePaymentTimeFormatted)
|
66
|
|
- ->suffix('days'),
|
67
|
|
-
|
68
|
|
- EnhancedStatsOverviewWidget\EnhancedStat::make('Paid Last Month', CurrencyConverter::formatCentsToMoney($lastMonthTotal))
|
69
|
|
- ->suffix(CurrencyAccessor::getDefaultCurrency()),
|
|
87
|
+ ->suffix($averagePaymentTimeSuffix),
|
|
88
|
+ EnhancedStatsOverviewWidget\EnhancedStat::make('Paid Last Month', $lastMonthTotal)
|
|
89
|
+ ->suffix($lastMonthTotalSuffix),
|
70
|
90
|
];
|
71
|
91
|
}
|
72
|
92
|
}
|