Skip to content

Commit b8168b3

Browse files
Merge pull request #32 from khalidmaquilang/feature/widgets
Feature | additional widget
2 parents 8daaa85 + 5d7674b commit b8168b3

File tree

2 files changed

+206
-0
lines changed

2 files changed

+206
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 TotalOverview extends BaseWidget
11+
{
12+
protected function getStats(): array
13+
{
14+
$range = range(1, 5);
15+
shuffle($range);
16+
17+
return [
18+
Stat::make('Total Purchases Due', $this->getTotalPurchasesDue())
19+
->color('warning')
20+
->chart($range),
21+
Stat::make('Total Purchases Amount', $this->getTotalPurchasesAmount())
22+
->color('success')
23+
->chart($range),
24+
Stat::make('Total Sales Due', $this->getTotalSalesDue())
25+
->color('warning')
26+
->chart($range),
27+
Stat::make('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+
return cache()->remember('widget-total-due-'.$tableName, 60 * 3, function () use ($tableName) {
72+
return DB::table($tableName)
73+
->selectRaw('SUM(total_amount - paid_amount) as total_due')
74+
->value('total_due');
75+
});
76+
}
77+
78+
/**
79+
* @param $tableName
80+
* @return string
81+
*/
82+
protected function getTotalAmount($tableName): string
83+
{
84+
return cache()->remember('widget-total-amount-'.$tableName, 60 * 3, function () use ($tableName) {
85+
return DB::table($tableName)
86+
->selectRaw('SUM(total_amount) as total_amount')
87+
->value('total_amount');
88+
});
89+
}
90+
91+
/**
92+
* @param float $amount
93+
* @return string
94+
*/
95+
protected function formatCurrency(float $amount): string
96+
{
97+
return number_format($amount, 2).' '.Setting::getCurrency();
98+
}
99+
}

0 commit comments

Comments
 (0)