diff --git a/app/Admin/Controllers/Store/StaffPerformanceController.php b/app/Admin/Controllers/Store/StaffPerformanceController.php index 992b173c..0cca006a 100644 --- a/app/Admin/Controllers/Store/StaffPerformanceController.php +++ b/app/Admin/Controllers/Store/StaffPerformanceController.php @@ -144,6 +144,75 @@ class StaffPerformanceController extends AdminController $filter->between('datetime')->datetime()->ignore()->width(4); }); + $grid->footer(function ($collection) use ($grid, $infoTable, $onlineOrderPerformances, $offlineOrderPerformances) { + // 线上订单 + $onlineQuery = UserInfo::query() + ->leftJoinSub($onlineOrderPerformances, 'online_performances', fn ($join) => $join->on("{$infoTable}.user_id", '=', 'online_performances.inviter_id')) + ->select([ + DB::raw('SUM(online_performances.orders_count) AS orders_count'), + DB::raw('SUM(online_performances.points_deduction_amount) AS points_deduction_amount'), + DB::raw('SUM(online_performances.payment_amount) AS payment_amount'), + DB::raw('SUM(online_performances.profit) AS profit'), + ]) + ->where('is_company', true); + + // 线下订单 + $offlineQuery = UserInfo::query() + ->leftJoinSub($offlineOrderPerformances, 'offline_performances', fn ($join) => $join->on("{$infoTable}.user_id", '=', 'offline_performances.staff_id')) + ->select([ + // 线下订单 + DB::raw('SUM(offline_performances.orders_count) AS orders_count'), + DB::raw('SUM(offline_performances.points_deduction_amount) AS points_deduction_amount'), + DB::raw('SUM(offline_performances.payment_amount) AS payment_amount'), + ]) + ->where('is_company', true); + + $grid->model()->getQueries()->unique()->each(function ($value) use (&$onlineQuery, &$offlineQuery) { + if (in_array($value['method'], ['paginate', 'get', 'orderBy', 'orderByDesc'], true)) { + return; + } + + $onlineQuery = call_user_func_array([$onlineQuery, $value['method']], $value['arguments'] ?? []); + $offlineQuery = call_user_func_array([$offlineQuery, $value['method']], $value['arguments'] ?? []); + }); + + $onlineResults = $onlineQuery->first(); + + $onlineOrdersCount = $onlineResults['orders_count'] ?? 0; + $onlinePointsDeductionAmount = bcdiv($onlineResults['points_deduction_amount'] ?? 0, 100, 2); + $onlinePaymentAmount = bcdiv($onlineResults['payment_amount'] ?? 0, 100, 2); + $onlineTotalAmount = bcadd($onlinePointsDeductionAmount, $onlinePaymentAmount, 2); + $onlineProfit = $onlineResults['profit'] ?? 0; + + $offlineResults = $offlineQuery->first(); + $offlineOrdersCount = $offlineResults['orders_count'] ?? 0; + $offlinePointsDeductionAmount = bcdiv($offlineResults['points_deduction_amount'] ?? 0, 100, 2); + $offlinePaymentAmount = bcdiv($offlineResults['payment_amount'] ?? 0, 100, 2); + $offlineTotalAmount = bcadd($offlinePointsDeductionAmount, $offlinePaymentAmount, 2); + + return << + + + 线上订单统计 + 订单数: $onlineOrdersCount + 订单总额: $onlineTotalAmount + 积分抵扣: $onlinePointsDeductionAmount + 实付总额: $onlinePaymentAmount + 累计返利: $onlineProfit + + + 线下订单统计 + 订单数: $offlineOrdersCount + 订单总额: $offlineTotalAmount + 积分抵扣: $offlinePointsDeductionAmount + 实付总额: $offlinePaymentAmount + + + + HTML; + }); + return $grid; } }