OfflineOrderItem.coupon_discount_amount
parent
b185b74f6b
commit
c4c098c385
|
|
@ -214,6 +214,8 @@ class OfflineOrderController extends AdminController
|
|||
->display(fn ($v) => bcdiv($v, 100, 2))->prepend('¥');
|
||||
$grid->column('points_deduction_amount', '积分抵扣')
|
||||
->display(fn ($v) => bcdiv($v, 100, 2))->prepend('¥');
|
||||
$grid->column('coupon_discount_amount', '优惠券')
|
||||
->display(fn ($v) => bcdiv($v, 100, 2))->prepend('¥');
|
||||
$grid->column('payment_amount', '实付金额')
|
||||
->display(fn ($v) => bcdiv($v, 100, 2))->prepend('¥');
|
||||
$grid->disableActions();
|
||||
|
|
|
|||
|
|
@ -34,6 +34,9 @@ class OfflineOrderItemStatisticController extends AdminController
|
|||
"{$ordersTable}.store_id",
|
||||
DB::raw("SUM({$orderItemsTable}.points_deduction_amount) AS points_deduction_amount"),
|
||||
DB::raw("SUM({$orderItemsTable}.payment_amount) AS payment_amount"),
|
||||
DB::raw("SUM({$orderItemsTable}.products_total_amount) AS products_total_amount"),
|
||||
DB::raw("SUM({$orderItemsTable}.discount_reduction_amount) AS discount_reduction_amount"),
|
||||
DB::raw("SUM({$orderItemsTable}.coupon_discount_amount) AS coupon_discount_amount"),
|
||||
])
|
||||
->join("{$ordersTable}", "{$orderItemsTable}.order_id", '=', "{$ordersTable}.id")
|
||||
->where("{$ordersTable}.status", OfflineOrderStatus::Paid)
|
||||
|
|
@ -57,7 +60,9 @@ class OfflineOrderItemStatisticController extends AdminController
|
|||
|
||||
$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('products_total_amount')->display(fn ($v) => bcdiv($v, 100, 2));
|
||||
$grid->column('discount_reduction_amount')->display(fn ($v) => bcdiv($v, 100, 2));
|
||||
$grid->column('coupon_discount_amount')->display(fn ($v) => bcdiv($v, 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));
|
||||
|
||||
|
|
@ -65,21 +70,10 @@ class OfflineOrderItemStatisticController extends AdminController
|
|||
$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->footer(function ($collection) use ($grid, $builder) {
|
||||
$query = $builder;
|
||||
|
||||
// 拿到表格筛选 where 条件数组进行遍历
|
||||
$grid->model()->getQueries()->unique()->each(function ($value) use (&$query) {
|
||||
if (in_array($value['method'], ['paginate', 'get', 'orderBy', 'orderByDesc'], true)) {
|
||||
return;
|
||||
|
|
@ -88,12 +82,15 @@ class OfflineOrderItemStatisticController extends AdminController
|
|||
$query = call_user_func_array([$query, $value['method']], $value['arguments'] ?? []);
|
||||
});
|
||||
|
||||
$results = $query->first();
|
||||
// 查出统计数据
|
||||
$data = $builder->get();
|
||||
|
||||
// $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);
|
||||
$totalAmount = bcdiv($data->sum("products_total_amount"), 100, 2);
|
||||
$discountAmount = bcdiv($data->sum("discount_reduction_amount"), 100, 2);
|
||||
$couponAmount = bcdiv($data->sum("coupon_discount_amount"), 100, 2);
|
||||
$pointsDeductionAmount = bcdiv($data->sum("points_deduction_amount"), 100, 2);
|
||||
$paymentAmount = bcdiv($data->sum("payment_amount"), 100, 2);
|
||||
|
||||
return <<<HTML
|
||||
<table class="table table-bordered">
|
||||
|
|
@ -101,6 +98,8 @@ class OfflineOrderItemStatisticController extends AdminController
|
|||
<tr>
|
||||
<td>单统计</td>
|
||||
<td>订单总额: $totalAmount</td>
|
||||
<td>折扣优惠: $discountAmount</td>
|
||||
<td>优惠券抵扣: $couponAmount</td>
|
||||
<td>积分抵扣: $pointsDeductionAmount</td>
|
||||
<td>实付总额: $paymentAmount</td>
|
||||
<tr>
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ class OfflineOrderItem extends Model
|
|||
'discount_reduction_amount',
|
||||
'points_deduction_amount',
|
||||
'payment_amount',
|
||||
'coupon_discount_amount'
|
||||
];
|
||||
|
||||
public function order()
|
||||
|
|
|
|||
|
|
@ -245,14 +245,26 @@ class OfflineOrderService
|
|||
protected function insertOrderItems(OfflineOrder $order, array $items)
|
||||
{
|
||||
$remainingPointDiscountAmount = $order->points_deduction_amount;
|
||||
$remainCoupon = $order->coupon_discount_amount;
|
||||
|
||||
OfflineOrderItem::insert(
|
||||
collect($items)->map(function ($item) use ($order, &$remainingPointDiscountAmount) {
|
||||
$pointsDeductionAmount = $item['payment_amount'];
|
||||
collect($items)->map(function ($item) use ($order, &$remainingPointDiscountAmount, &$remainCoupon) {
|
||||
$paymentAmount = $item['payment_amount'];
|
||||
|
||||
// 优惠券
|
||||
$couponDiscount = $paymentAmount;
|
||||
if ($paymentAmount > $remainCoupon) {
|
||||
$couponDiscount = $remainCoupon;
|
||||
}
|
||||
$remainCoupon -= $couponDiscount;
|
||||
$paymentAmount -= $couponDiscount;
|
||||
|
||||
$pointsDeductionAmount = $paymentAmount;
|
||||
if ($item['payment_amount'] > $remainingPointDiscountAmount) {
|
||||
$pointsDeductionAmount = $remainingPointDiscountAmount;
|
||||
}
|
||||
$remainingPointDiscountAmount -= $pointsDeductionAmount;
|
||||
$paymentAmount -= $pointsDeductionAmount;
|
||||
|
||||
return [
|
||||
'order_id' => $order->id,
|
||||
|
|
@ -260,7 +272,8 @@ class OfflineOrderService
|
|||
'products_total_amount' => $item['products_total_amount'],
|
||||
'discount_reduction_amount' => $item['discount_reduction_amount'],
|
||||
'points_deduction_amount' => $pointsDeductionAmount,
|
||||
'payment_amount' => $item['payment_amount'] - $pointsDeductionAmount,
|
||||
'coupon_discount_amount' => $couponDiscount,
|
||||
'payment_amount' => $paymentAmount,
|
||||
'created_at' => $order->created_at,
|
||||
'updated_at' => $order->updated_at,
|
||||
];
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ return [
|
|||
'points_deduction_amount' => '积分抵扣',
|
||||
'payment_amount' => '实付金额',
|
||||
'datetime' => '日期',
|
||||
'coupon_discount_amount' => '优惠券抵扣',
|
||||
],
|
||||
'options' => [
|
||||
],
|
||||
|
|
|
|||
Loading…
Reference in New Issue