Kaynağa Gözat

自动取消订单恢复优惠券

gorden 1 ay önce
ebeveyn
işleme
0310628152

+ 15 - 13
app/admin/controller/order/WholeController.php

@@ -1312,8 +1312,8 @@ class WholeController extends Curd
 
             if ($order->order_status_payment != 'SUCCESS') {
                 _syslog("订单", "支付异常,检查是否有轮询");
-                // 恢复优惠券
-                $this->changeOrderCouponStatus(json_decode($couponUseJson, true), 'ACTIVED');
+                // 恢复优惠券到已占用
+                $this->changeOrderCouponStatus(json_decode($couponUseJson, true), 'WAITING');
 
                 return json_throw(2001, '支付异常', ['order_id' => $params['orderId']]);
             }
@@ -1762,8 +1762,8 @@ class WholeController extends Curd
             }
             if ($order->order_status_payment != 'SUCCESS') {
                 _syslog("订单", "支付异常,检查是否有轮询");
-                // 恢复优惠券
-                $this->changeOrderCouponStatus(json_decode($couponUseJson, true), 'ACTIVED');
+                // 恢复优惠券到已占用
+                $this->changeOrderCouponStatus(json_decode($couponUseJson, true), 'WAITING');
                 return json_throw(2001, '支付异常', ['order_id' => $params['orderId']]);
             }
             _syslog("订单", "订单支付成功");
@@ -2502,8 +2502,8 @@ class WholeController extends Curd
 
             if ($params['settlement_now'] == 'Y' && $params['order_status_payment'] != 'SUCCESS') {
                 _syslog("订单", "支付异常,检查是否有轮询");
-                // 恢复优惠券
-                $this->changeOrderCouponStatus(json_decode($couponUseJson, true), 'ACTIVED');
+                // 恢复优惠券到已占用
+                $this->changeOrderCouponStatus(json_decode($couponUseJson, true), 'WAITING');
 
                 return json_throw(2001, '支付异常', ['order_id' => $params['orderId']]);
             }
@@ -2542,10 +2542,12 @@ class WholeController extends Curd
             foreach ($coupon as $key => $item) {
                 if (!empty($item['coupon_id']) && !empty($item['coupon_detail_id'])) {
                     foreach ($item['coupon_detail_id'] as $detailId) {
-                        CouponDetail::where('join_detail_coupon_id', $item['coupon_id'])
-                            ->where('coupon_detail_id', $detailId)
-                            ->where('coupon_detail_status','<>','USED')
-                            ->update($updateData);
+                        if (substr($detailId, 0, 4) == 'CUDT') {
+                            CouponDetail::where('join_detail_coupon_id', $item['coupon_id'])
+                                ->where('coupon_detail_id', $detailId)
+                                ->where('coupon_detail_status', 'WAITING')
+                                ->update($updateData);
+                        }
                     }
                     unset($coupon[$key]);
                 }
@@ -2568,7 +2570,7 @@ class WholeController extends Curd
     {
         if (!empty($coupon) && is_array($coupon)) {
             $updateData['coupon_detail_status'] = $status;
-            if ($status == 'ACTIVED') {
+            if ($status == 'ACTIVED' || $status == 'WAITING') {
                 $updateData['coupon_detail_used_datetime'] = '';
             } elseif ($status == 'USED') {
                 $updateData['coupon_detail_used_datetime'] = date('Y-m-d H:i:s');
@@ -3049,8 +3051,8 @@ class WholeController extends Curd
 
             if ($params['settlement_now'] == 'Y' && $params['order_status_payment'] != 'SUCCESS') {
                 _syslog("订单", "支付异常,检查是否有轮询");
-                // 恢复优惠券
-                $this->changeOrderCouponStatus(json_decode($couponUseJson, true), 'ACTIVED');
+                // 恢复优惠券到已占用
+                $this->changeOrderCouponStatus(json_decode($couponUseJson, true), 'WAITING');
 
                 return json_throw(2001, '支付异常', ['order_id' => $params['orderId']]);
             }

+ 14 - 1
app/admin/service/order/OrderService.php

@@ -111,7 +111,6 @@ class OrderService
             $orders = Order::where('order_status_system', 'PAYING')
                 ->where('order_category', '<>', 'DISHES')     // 点餐不自动取消
                 ->where('order_category', '<>', 'VIP')      // 康养城订单不自动取消,有分次付款
-                // ->where('order_platform','<>','SYSTEM')     // 后台产生的订单不自动取消
                 ->where(function ($query) {
                     $query->where('order_platform', '<>', 'SYSTEM')->orWhereNull('order_platform');
                 })
@@ -145,6 +144,20 @@ class OrderService
                         $goodsRunning->save();
                     }
                 }
+                // 释放优惠券
+                if (!empty($order->order_discount_json)) {
+                    $orderDiscountJson = json_decode($order->order_discount_json, true);
+                    foreach ($orderDiscountJson as $discount) {
+                        if (!empty($discount['coupon_id']) && !empty($discount['coupon_detail_id'])) {
+                            foreach ($discount['coupon_detail_id'] as $detailId) {
+                                if (substr($detailId, 0, 4) == 'CUDT') {
+                                    // 恢复优惠券
+                                    CouponDetail::where('coupon_detail_id', $detailId)->update(['coupon_detail_status' => 'ACTIVED']);
+                                }
+                            }
+                        }
+                    }
+                }
 
                 // 订单详情表
                 OrderSheet::where('join_sheet_order_id', $order->order_id)->update(['order_sheet_status' => 'CANCEL']);