| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Widgets; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\App\Helpers\Status; |
| 7 |
use FluentCart\App\Models\Coupon; |
| 8 |
use FluentCart\App\Models\Order; |
| 9 |
use FluentCart\App\Models\Product; |
| 10 |
use FluentCart\App\Services\DateTime\DateTime; |
| 11 |
use FluentCart\App\Services\URL; |
| 12 |
|
| 13 |
|
| 14 |
class DashboardWidget extends BaseWidget |
| 15 |
{ |
| 16 |
|
| 17 |
public function widgetName(): string |
| 18 |
{ |
| 19 |
return 'dashboard_stats'; |
| 20 |
} |
| 21 |
|
| 22 |
public function widgetData(): array |
| 23 |
{ |
| 24 |
$startDate = (new DateTime())->subMonth()->startOfDay(); |
| 25 |
$endDate = (new DateTime())->endOfDay(); |
| 26 |
|
| 27 |
$counts = Product::query()->selectRaw(" |
| 28 |
COUNT(*) as total_products |
| 29 |
") |
| 30 |
->whereNotIn('post_status', ['trash', 'auto-draft']) |
| 31 |
->first(); |
| 32 |
|
| 33 |
$sales = App::db()->table('fct_orders as o')->selectRaw(" |
| 34 |
COUNT(o.id) AS orders, |
| 35 |
|
| 36 |
SUM( |
| 37 |
o.total_paid |
| 38 |
- o.total_refund |
| 39 |
- o.tax_total |
| 40 |
- o.shipping_tax |
| 41 |
) / 100 AS net_revenue, |
| 42 |
|
| 43 |
SUM(o.total_refund) / 100 AS total_refunds |
| 44 |
") |
| 45 |
->whereBetween('o.created_at', [$startDate, $endDate]) |
| 46 |
->whereNotIn('o.status', [Status::ORDER_ON_HOLD, Status::ORDER_FAILED]) |
| 47 |
->first(); |
| 48 |
|
| 49 |
return [ |
| 50 |
[ |
| 51 |
'title' => __('Total Products', 'fluent-cart'), |
| 52 |
'current_count' => $counts->total_products, |
| 53 |
'icon' => 'Frame', |
| 54 |
'url' => URL::getDashboardUrl('products', [ |
| 55 |
'active_view' => 'all', |
| 56 |
]), |
| 57 |
], |
| 58 |
[ |
| 59 |
'title' => __('Orders', 'fluent-cart'), |
| 60 |
'current_count' => $sales->orders, |
| 61 |
'icon' => 'AllOrdersIcon', |
| 62 |
'url' => URL::getDashboardUrl('orders'), |
| 63 |
], |
| 64 |
[ |
| 65 |
'title' => __('Revenue', 'fluent-cart'), |
| 66 |
'current_count' => $sales->net_revenue, |
| 67 |
'icon' => 'Currency', |
| 68 |
'url' => URL::getDashboardUrl('reports/revenue'), |
| 69 |
'has_currency' => true, |
| 70 |
], |
| 71 |
[ |
| 72 |
'title' => __('Refund', 'fluent-cart'), |
| 73 |
'current_count' => $sales->total_refunds, |
| 74 |
'icon' => 'Failed', |
| 75 |
'url' => URL::getDashboardUrl('reports/refunds'), |
| 76 |
'has_currency' => true, |
| 77 |
], |
| 78 |
]; |
| 79 |
} |
| 80 |
} |
| 81 |
|