diff --git a/app/Services/System/AnalysisService.php b/app/Services/System/AnalysisService.php index a4c38c4..8d8eba3 100644 --- a/app/Services/System/AnalysisService.php +++ b/app/Services/System/AnalysisService.php @@ -674,20 +674,29 @@ public function getTopProducts(?Carbon $startDate = null, ?Carbon $endDate = nul public function getMarketingSales(?Carbon $startDate = null, ?Carbon $endDate = null): array { + $qtySubquery = DB::table('order_items') + ->select('order_id', DB::raw('SUM(quantity) as total_qty')) + ->whereNull('deleted_at') + ->groupBy('order_id'); + return Order::query() ->completed() ->whereNotNull('marketing_id') ->when($startDate && $endDate, fn ($q) => $q->whereBetween('orders.created_at', [$startDate, $endDate])) ->join('users', 'orders.marketing_id', '=', 'users.id') ->leftJoin('user_profiles', 'users.id', '=', 'user_profiles.user_id') + ->leftJoinSub($qtySubquery, 'order_qtys', function ($join) { + $join->on('orders.id', '=', 'order_qtys.order_id'); + }) ->selectRaw(' users.id as marketing_id, COALESCE(user_profiles.full_name, users.username) as marketing_name, - COUNT(*) as total_orders, + COUNT(orders.id) as total_orders, SUM(orders.total_amount) as total_revenue, SUM(orders.subtotal) as total_subtotal, SUM(orders.discount) as total_discount, - AVG(orders.total_amount) as avg_order + AVG(orders.total_amount) as avg_order, + SUM(COALESCE(order_qtys.total_qty, 0)) as total_products_sold ') ->groupBy('users.id', 'user_profiles.full_name', 'users.username') ->orderByDesc('total_revenue') @@ -695,6 +704,7 @@ public function getMarketingSales(?Carbon $startDate = null, ?Carbon $endDate = ->map(fn ($item) => [ 'marketing_name' => $item->marketing_name, 'total_orders' => (int) $item->total_orders, + 'total_products_sold' => (int) $item->total_products_sold, 'total_revenue' => (int) $item->total_revenue, 'total_subtotal' => (int) $item->total_subtotal, 'total_discount' => (int) $item->total_discount, diff --git a/resources/js/pages/admin/Analysis.vue b/resources/js/pages/admin/Analysis.vue index 1f1e2f9..452cfe0 100644 --- a/resources/js/pages/admin/Analysis.vue +++ b/resources/js/pages/admin/Analysis.vue @@ -1,8 +1,19 @@