|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Filament\Widgets; |
| 4 | + |
| 5 | +use App\Models\Setting; |
| 6 | +use Filament\Widgets\StatsOverviewWidget as BaseWidget; |
| 7 | +use Filament\Widgets\StatsOverviewWidget\Stat; |
| 8 | +use Illuminate\Support\Facades\DB; |
| 9 | + |
| 10 | +class TodayOverview extends BaseWidget |
| 11 | +{ |
| 12 | + protected function getStats(): array |
| 13 | + { |
| 14 | + $range = range(1, 5); |
| 15 | + shuffle($range); |
| 16 | + |
| 17 | + return [ |
| 18 | + Stat::make("Today's Total Purchases Due", $this->getTotalPurchasesDue()) |
| 19 | + ->color('warning') |
| 20 | + ->chart($range), |
| 21 | + Stat::make("Today's Total Purchases Amount", $this->getTotalPurchasesAmount()) |
| 22 | + ->color('success') |
| 23 | + ->chart($range), |
| 24 | + Stat::make("Today's Total Sales Due", $this->getTotalSalesDue()) |
| 25 | + ->color('warning') |
| 26 | + ->chart($range), |
| 27 | + Stat::make("Today's Total Sales Amount", $this->getTotalSalesAmount()) |
| 28 | + ->color('success') |
| 29 | + ->chart($range), |
| 30 | + ]; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @return string |
| 35 | + */ |
| 36 | + protected function getTotalPurchasesDue(): string |
| 37 | + { |
| 38 | + return $this->formatCurrency($this->getTotalDue('purchase_orders')); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @return string |
| 43 | + */ |
| 44 | + protected function getTotalPurchasesAmount(): string |
| 45 | + { |
| 46 | + return $this->formatCurrency($this->getTotalAmount('purchase_orders')); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @return string |
| 51 | + */ |
| 52 | + protected function getTotalSalesDue(): string |
| 53 | + { |
| 54 | + return $this->formatCurrency($this->getTotalDue('sales')); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return string |
| 59 | + */ |
| 60 | + protected function getTotalSalesAmount(): string |
| 61 | + { |
| 62 | + return $this->formatCurrency($this->getTotalAmount('sales')); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param $tableName |
| 67 | + * @return string |
| 68 | + */ |
| 69 | + protected function getTotalDue($tableName): string |
| 70 | + { |
| 71 | + $today = now()->toDateString(); |
| 72 | + $column = $tableName === 'sales' ? 'sale_date' : 'order_date'; |
| 73 | + |
| 74 | + return cache()->remember('widget-today-total-due-'.$tableName, 60 * 3, function () use ($tableName, $column, $today) { |
| 75 | + return DB::table($tableName) |
| 76 | + ->selectRaw('SUM(total_amount - paid_amount) as total_due') |
| 77 | + ->whereDate($column, $today) |
| 78 | + ->value('total_due'); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param $tableName |
| 84 | + * @return string |
| 85 | + */ |
| 86 | + protected function getTotalAmount($tableName): string |
| 87 | + { |
| 88 | + $today = now()->toDateString(); |
| 89 | + $column = $tableName === 'sales' ? 'sale_date' : 'order_date'; |
| 90 | + |
| 91 | + return cache()->remember('widget-today-total-amount-'.$tableName, 60 * 3, function () use ($tableName, $today, $column) { |
| 92 | + return DB::table($tableName) |
| 93 | + ->selectRaw('SUM(total_amount) as total_amount') |
| 94 | + ->whereDate($column, $today) |
| 95 | + ->value('total_amount'); |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param float $amount |
| 101 | + * @return string |
| 102 | + */ |
| 103 | + protected function formatCurrency(float $amount): string |
| 104 | + { |
| 105 | + return number_format($amount, 2).' '.Setting::getCurrency(); |
| 106 | + } |
| 107 | +} |
0 commit comments