diff --git a/app/Admin/Controllers/Store/StaffPerformanceController.php b/app/Admin/Controllers/Store/StaffPerformanceController.php index 51b7245a..992b173c 100644 --- a/app/Admin/Controllers/Store/StaffPerformanceController.php +++ b/app/Admin/Controllers/Store/StaffPerformanceController.php @@ -4,6 +4,7 @@ namespace App\Admin\Controllers\Store; use App\Enums\OfflineOrderStatus; use App\Models\OfflineOrder; +use App\Models\Order; use App\Models\UserInfo; use Dcat\Admin\Grid; use Dcat\Admin\Http\Controllers\AdminController; @@ -37,12 +38,34 @@ class StaffPerformanceController extends AdminController }) ->groupBy('inviter_id'); + // 线上订单业绩 + $onlineOrderPerformances = Order::select([ + 'inviter_id', + DB::raw('COUNT(1) AS orders_count'), + DB::raw('SUM(point_discount_amount) AS points_deduction_amount'), + DB::raw('SUM(total_amount) AS payment_amount'), + DB::raw('SUM(profit) AS profit'), + ]) + ->whereIn('status', [ + Order::STATUS_PAID, + Order::STATUS_COMPLETED, + ]) + ->when($start || $end, function ($builder) use ($start, $end) { + if ($start && $end) { + return $builder->whereBetween('created_at', [$start, $end]); + } + + return $builder->when($start, fn ($builder) => $builder->where('created_at', '>=', $start)) + ->when($end, fn ($builder) => $builder->where('created_at', '<=', $end)); + }) + ->groupBy('inviter_id'); + + // 线下订单业绩 $offlineOrderPerformances = OfflineOrder::select([ 'staff_id', - DB::raw('SUM(products_total_amount) as offline_products_total_amount'), - DB::raw('SUM(discount_reduction_amount) as offline_discount_reduction_amount'), - DB::raw('SUM(points_deduction_amount) offline_points_deduction_amount'), - DB::raw('SUM(payment_amount) as offline_payment_amount'), + DB::raw('COUNT(1) AS orders_count'), + DB::raw('SUM(points_deduction_amount) AS points_deduction_amount'), + DB::raw('SUM(payment_amount) AS payment_amount'), ]) ->where('status', OfflineOrderStatus::Paid) ->when($start || $end, function ($builder) use ($start, $end) { @@ -57,24 +80,40 @@ class StaffPerformanceController extends AdminController $builder = UserInfo::with(['user']) ->leftJoinSub($inviteUsersCounts, 'invite_users_counts', fn ($join) => $join->on("{$infoTable}.user_id", '=', 'invite_users_counts.inviter_id')) - ->leftJoinSub($offlineOrderPerformances, 'offline_order_performances', fn ($join) => $join->on("{$infoTable}.user_id", '=', 'offline_order_performances.staff_id')) + ->leftJoinSub($onlineOrderPerformances, 'online_performances', fn ($join) => $join->on("{$infoTable}.user_id", '=', 'online_performances.inviter_id')) + ->leftJoinSub($offlineOrderPerformances, 'offline_performances', fn ($join) => $join->on("{$infoTable}.user_id", '=', 'offline_performances.staff_id')) ->select([ 'user_id', 'nickname', 'avatar', 'invite_users_count', - 'offline_products_total_amount', - 'offline_discount_reduction_amount', - 'offline_points_deduction_amount', - 'offline_payment_amount', + // 线上订单 + DB::raw('online_performances.orders_count AS online_orders_count'), + DB::raw('online_performances.points_deduction_amount AS online_points_deduction_amount'), + DB::raw('online_performances.payment_amount AS online_payment_amount'), + DB::raw('online_performances.profit AS online_profit'), + // 线下订单 + DB::raw('offline_performances.orders_count AS offline_orders_count'), + DB::raw('offline_performances.points_deduction_amount AS offline_points_deduction_amount'), + DB::raw('offline_performances.payment_amount AS offline_payment_amount'), ]) ->where('is_company', true); $grid = new Grid($builder); + $grid->model()->orderBy('user_id', 'desc'); + + $grid->combine('online_order', [ + 'online_orders_count', + 'online_total_amount', + 'online_points_deduction_amount', + 'online_payment_amount', + 'online_profit', + ]); + $grid->combine('offline_order', [ - 'offline_products_total_amount', - 'offline_discount_reduction_amount', + 'offline_orders_count', + 'offline_total_amount', 'offline_points_deduction_amount', 'offline_payment_amount', ]); @@ -83,8 +122,15 @@ class StaffPerformanceController extends AdminController $grid->column('phone')->display(fn () => $this->user?->phone)->copyable(); $grid->column('nickname'); $grid->column('invite_users_count')->display(fn ($v) => (int) $v); - $grid->column('offline_products_total_amount')->display(fn ($v) => bcdiv($v, 100, 2)); - $grid->column('offline_discount_reduction_amount')->display(fn ($v) => bcdiv($v, 100, 2)); + // 线上订单业绩 + $grid->column('online_orders_count')->display(fn ($v) => (int) $v); + $grid->column('online_total_amount')->display(fn () => bcdiv($this->online_points_deduction_amount + $this->online_payment_amount, 100, 2)); + $grid->column('online_points_deduction_amount')->display(fn ($v) => bcdiv($v, 100, 2)); + $grid->column('online_payment_amount')->display(fn ($v) => bcdiv($v, 100, 2)); + $grid->column('online_profit')->display(fn ($v) => $v ?? '0.00'); + // 线下订单业绩 + $grid->column('offline_orders_count')->display(fn ($v) => (int) $v); + $grid->column('offline_total_amount')->display(fn () => bcdiv($this->offline_points_deduction_amount + $this->offline_payment_amount, 100, 2)); $grid->column('offline_points_deduction_amount')->display(fn ($v) => bcdiv($v, 100, 2)); $grid->column('offline_payment_amount')->display(fn ($v) => bcdiv($v, 100, 2)); diff --git a/resources/lang/zh_CN/store-staff-performance.php b/resources/lang/zh_CN/store-staff-performance.php index d6c620ef..f9b5c481 100644 --- a/resources/lang/zh_CN/store-staff-performance.php +++ b/resources/lang/zh_CN/store-staff-performance.php @@ -10,9 +10,15 @@ return [ 'phone' => '手机号', 'nickname' => '昵称', 'invite_users_count' => '邀请人数', + 'online_order' => '线上订单', + 'online_orders_count' => '订单数量', + 'online_total_amount' => '订单总额', + 'online_points_deduction_amount' => '积分抵扣', + 'online_payment_amount' => '实付金额', + 'online_profit' => '累计返利', 'offline_order' => '线下订单', - 'offline_products_total_amount' => '订单总额', - 'offline_discount_reduction_amount' => '折扣优惠', + 'offline_orders_count' => '订单数量', + 'offline_total_amount' => '订单总额', 'offline_points_deduction_amount' => '积分抵扣', 'offline_payment_amount' => '实付金额', 'datetime' => '日期',