From eb6bf66970285d4bd42c50b82dcd3eebe5fb04d0 Mon Sep 17 00:00:00 2001 From: Jing Li Date: Mon, 6 Nov 2023 21:24:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E6=98=8E=E7=BB=86=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OfflineOrderItemStatisticController.php | 112 ++++++++++++++++++ app/Admin/routes.php | 2 + app/Models/OfflineOrderItem.php | 5 + database/seeders/AdminMenuSeeder.php | 5 + database/seeders/AdminPermissionSeeder.php | 5 + .../zh_CN/offline-order-item-statistic.php | 19 +++ 6 files changed, 148 insertions(+) create mode 100644 app/Admin/Controllers/OfflineOrderItemStatisticController.php create mode 100644 resources/lang/zh_CN/offline-order-item-statistic.php diff --git a/app/Admin/Controllers/OfflineOrderItemStatisticController.php b/app/Admin/Controllers/OfflineOrderItemStatisticController.php new file mode 100644 index 00000000..9eb55147 --- /dev/null +++ b/app/Admin/Controllers/OfflineOrderItemStatisticController.php @@ -0,0 +1,112 @@ +getTable(); + $orderItemsTable = (new OfflineOrderItem())->getTable(); + + $stores = Store::pluck('title', 'id'); + $productCategories = OfflineProductCategory::pluck('name', 'id'); + + $builder = OfflineOrderItem::select([ + "{$orderItemsTable}.product_category_id", + "{$ordersTable}.store_id", + DB::raw("SUM({$orderItemsTable}.points_deduction_amount) AS points_deduction_amount"), + DB::raw("SUM({$orderItemsTable}.payment_amount) AS payment_amount"), + ]) + ->join("{$ordersTable}", "{$orderItemsTable}.order_id", '=', "{$ordersTable}.id") + ->where("{$ordersTable}.status", OfflineOrderStatus::Paid) + ->when($start || $end, function ($builder) use ($start, $end, $ordersTable) { + if ($start && $end) { + return $builder->whereBetween("{$ordersTable}.payment_time", [$start, $end]); + } + return $builder->when($start, fn ($builder) => $builder->where("{$ordersTable}.payment_time", '>=', $start)) + ->when($end, fn ($builder) => $builder->where("{$ordersTable}.payment_time", '<=', $end)); + }) + ->groupBy(["{$orderItemsTable}.product_category_id", "{$ordersTable}.store_id"]); + + $grid = Grid::make($builder); + + $grid->filter(function (Grid\Filter $filter) use ($stores, $productCategories) { + $filter->panel(); + $filter->equal('store_id')->select($stores)->width(4); + $filter->equal('product_category_id')->select($productCategories)->width(4); + $filter->between('datetime')->datetime()->ignore()->width(4); + }); + + $grid->column('product_category_id')->display(fn ($v) => $productCategories->get($v) ?: '其它'); + $grid->column('store_id')->display(fn ($v) => $stores->get($v) ?: '其它'); + $grid->column('products_total_amount')->display(fn ($v) => bcdiv($this->points_deduction_amount + $this->payment_amount, 100, 2)); + $grid->column('points_deduction_amount')->display(fn ($v) => bcdiv($v, 100, 2)); + $grid->column('payment_amount')->display(fn ($v) => bcdiv($v, 100, 2)); + + $grid->disablePagination(); + $grid->disableFilterButton(); + $grid->disableActions(); + + $grid->footer(function ($collection) use ($grid, $start, $end, $ordersTable, $orderItemsTable) { + $query = OfflineOrderItem::select([ + DB::raw("SUM({$orderItemsTable}.points_deduction_amount) AS points_deduction_amount"), + DB::raw("SUM({$orderItemsTable}.payment_amount) AS payment_amount"), + ]) + ->join("{$ordersTable}", "{$orderItemsTable}.order_id", '=', "{$ordersTable}.id") + ->where("{$ordersTable}.status", OfflineOrderStatus::Paid) + ->when($start || $end, function ($builder) use ($start, $end, $ordersTable) { + if ($start && $end) { + return $builder->whereBetween("{$ordersTable}.payment_time", [$start, $end]); + } + return $builder->when($start, fn ($builder) => $builder->where("{$ordersTable}.payment_time", '>=', $start)) + ->when($end, fn ($builder) => $builder->where("{$ordersTable}.payment_time", '<=', $end)); + }); + + $grid->model()->getQueries()->unique()->each(function ($value) use (&$query) { + if (in_array($value['method'], ['paginate', 'get', 'orderBy', 'orderByDesc'], true)) { + return; + } + + $query = call_user_func_array([$query, $value['method']], $value['arguments'] ?? []); + }); + + $results = $query->first(); + + // $onlineOrdersCount = $results['orders_count'] ?? 0; + $pointsDeductionAmount = bcdiv($results['points_deduction_amount'] ?? 0, 100, 2); + $paymentAmount = bcdiv($results['payment_amount'] ?? 0, 100, 2); + $totalAmount = bcadd($pointsDeductionAmount, $paymentAmount, 2); + + return << + + + 单统计 + 订单总额: $totalAmount + 积分抵扣: $pointsDeductionAmount + 实付总额: $paymentAmount + + + + HTML; + }); + + return $grid; + } +} diff --git a/app/Admin/routes.php b/app/Admin/routes.php index feafd5a0..ea4b9213 100644 --- a/app/Admin/routes.php +++ b/app/Admin/routes.php @@ -190,6 +190,8 @@ Route::group([ 'only' => ['index', 'show'], ])->names('offline_orders'); + $router->get('offline-order-item-statistics', 'OfflineOrderItemStatisticController@index')->name('offline_order_item_statistics.index'); + /** 调试接口 **/ // $router->get('test', 'HomeController@test'); diff --git a/app/Models/OfflineOrderItem.php b/app/Models/OfflineOrderItem.php index 588f9cda..2f80f805 100644 --- a/app/Models/OfflineOrderItem.php +++ b/app/Models/OfflineOrderItem.php @@ -24,6 +24,11 @@ class OfflineOrderItem extends Model 'payment_amount', ]; + public function order() + { + return $this->belongsTo(OfflineOrder::class, 'order_id'); + } + public function productCategory() { return $this->belongsTo(OfflineProductCategory::class, 'product_category_id'); diff --git a/database/seeders/AdminMenuSeeder.php b/database/seeders/AdminMenuSeeder.php index 7c246c10..a3a3a551 100644 --- a/database/seeders/AdminMenuSeeder.php +++ b/database/seeders/AdminMenuSeeder.php @@ -373,6 +373,11 @@ class AdminMenuSeeder extends Seeder 'icon' => '', 'uri' => 'offline-orders', ], + [ + 'title' => '订单明细统计', + 'icon' => '', + 'uri' => 'offline-order-item-statistics', + ], ], ], ]; diff --git a/database/seeders/AdminPermissionSeeder.php b/database/seeders/AdminPermissionSeeder.php index c05a2cd4..84b8c28c 100644 --- a/database/seeders/AdminPermissionSeeder.php +++ b/database/seeders/AdminPermissionSeeder.php @@ -388,6 +388,11 @@ class AdminPermissionSeeder extends Seeder 'revoke' => ['name' => '取消订单'], ], ], + 'offline_order_item_statistic' => [ + 'name' => '线下订单 - 订单明细统计', + 'curd' => ['index'], + 'children' => [], + ], ]; // try { // DB::begintransaction(); diff --git a/resources/lang/zh_CN/offline-order-item-statistic.php b/resources/lang/zh_CN/offline-order-item-statistic.php new file mode 100644 index 00000000..74ef639d --- /dev/null +++ b/resources/lang/zh_CN/offline-order-item-statistic.php @@ -0,0 +1,19 @@ + [ + 'OfflineOrderItemStatistic' => '订单明细统计', + 'offline-order-item-statistics' => '订单明细统计', + ], + 'fields' => [ + 'product_category_id' => '商品分类', + 'store_id' => '门店', + 'products_total_amount' => '订单总额', + 'discount_reduction_amount' => '折扣优惠', + 'points_deduction_amount' => '积分抵扣', + 'payment_amount' => '实付金额', + 'datetime' => '日期', + ], + 'options' => [ + ], +];