6
0
Fork 0

订单明细统计

base
Jing Li 2023-11-06 21:24:52 +08:00
parent 586a17fe0f
commit eb6bf66970
6 changed files with 148 additions and 0 deletions

View File

@ -0,0 +1,112 @@
<?php
namespace App\Admin\Controllers;
use App\Enums\OfflineOrderStatus;
use App\Models\OfflineOrder;
use App\Models\OfflineOrderItem;
use App\Models\OfflineProductCategory;
use App\Models\Store\Store;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;
class OfflineOrderItemStatisticController extends AdminController
{
protected function grid()
{
$datetime = request('datetime');
// 开始时间
$start = $datetime['start'] ?? '';
// 结束时间
$end = $datetime['end'] ?? '';
$ordersTable = (new OfflineOrder())->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 <<<HTML
<table class="table table-bordered">
<tbody>
<tr>
<td>单统计</td>
<td>订单总额: $totalAmount</td>
<td>积分抵扣: $pointsDeductionAmount</td>
<td>实付总额: $paymentAmount</td>
<tr>
</tbody>
</table>
HTML;
});
return $grid;
}
}

View File

@ -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');

View File

@ -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');

View File

@ -373,6 +373,11 @@ class AdminMenuSeeder extends Seeder
'icon' => '',
'uri' => 'offline-orders',
],
[
'title' => '订单明细统计',
'icon' => '',
'uri' => 'offline-order-item-statistics',
],
],
],
];

View File

@ -388,6 +388,11 @@ class AdminPermissionSeeder extends Seeder
'revoke' => ['name' => '取消订单'],
],
],
'offline_order_item_statistic' => [
'name' => '线下订单 - 订单明细统计',
'curd' => ['index'],
'children' => [],
],
];
// try {
// DB::begintransaction();

View File

@ -0,0 +1,19 @@
<?php
return [
'labels' => [
'OfflineOrderItemStatistic' => '订单明细统计',
'offline-order-item-statistics' => '订单明细统计',
],
'fields' => [
'product_category_id' => '商品分类',
'store_id' => '门店',
'products_total_amount' => '订单总额',
'discount_reduction_amount' => '折扣优惠',
'points_deduction_amount' => '积分抵扣',
'payment_amount' => '实付金额',
'datetime' => '日期',
],
'options' => [
],
];