Procházet zdrojové kódy

订单未支付取消时候才会退优惠券

evoxwht před 2 roky
rodič
revize
01ca78ac7e

+ 1 - 0
crmeb/app/api/controller/v1/order/StoreOrderController.php

@@ -430,6 +430,7 @@ class StoreOrderController
      * 订单删除
      * @param Request $request
      * @return mixed
+     * @throws \Psr\SimpleCache\InvalidArgumentException
      */
     public function del(Request $request)
     {

+ 4 - 4
crmeb/app/jobs/UnpaidOrderCancelJob.php

@@ -33,7 +33,7 @@ class UnpaidOrderCancelJob extends BaseJobs
     public function doJob($orderId)
     {
         /** @var StoreOrderServices $services */
-        $services  = app()->make(StoreOrderServices::class);
+        $services = app()->make(StoreOrderServices::class);
         $orderInfo = $services->get($orderId);
         if (!$orderInfo) {
             return true;
@@ -49,21 +49,21 @@ class UnpaidOrderCancelJob extends BaseJobs
         }
         /** @var StoreOrderCartInfoServices $cartServices */
         $cartServices = app()->make(StoreOrderCartInfoServices::class);
-        $cartInfo     = $cartServices->getOrderCartInfo($orderId);
+        $cartInfo = $cartServices->getOrderCartInfo($orderId);
         /** @var StoreOrderRefundServices $refundServices */
         $refundServices = app()->make(StoreOrderRefundServices::class);
 
         try {
             $res = $refundServices->transaction(function () use ($orderInfo, $refundServices) {
                 //回退积分和优惠卷
-                $refundServices->integralAndCouponBack($orderInfo);
+                $refundServices->integralAndCouponBack($orderInfo, 'cancel');
                 //回退库存和销量
                 $refundServices->regressionStock($orderInfo);
                 return true;
             });
             if ($res) {
                 $orderInfo->is_del = 1;
-                $orderInfo->mark   = '订单未支付已超过系统预设时间';
+                $orderInfo->mark = '订单未支付已超过系统预设时间';
                 $orderInfo->save();
             }
             return $res;

+ 14 - 13
crmeb/app/services/order/StoreOrderRefundServices.php

@@ -426,25 +426,26 @@ class StoreOrderRefundServices extends BaseServices
     /**
      * 回退积分和优惠卷
      * @param $order
+     * @param string $type
      * @return bool
      */
-    public function integralAndCouponBack($order)
+    public function integralAndCouponBack($order, $type = 'refund')
     {
         /** @var StoreOrderStatusServices $statusService */
         $statusService = app()->make(StoreOrderStatusServices::class);
         $res = true;
-//        //回退优惠卷 拆分子订单不退优惠券
-//        if (!$order['pid'] && $order['coupon_id'] && $order['coupon_price']) {
-//            /** @var StoreCouponUserServices $couponUserServices */
-//            $couponUserServices = app()->make(StoreCouponUserServices::class);
-//            $res = $couponUserServices->recoverCoupon((int)$order['coupon_id']);
-//            $statusService->save([
-//                'oid' => $order['id'],
-//                'change_type' => 'coupon_back',
-//                'change_message' => '商品退优惠券',
-//                'change_time' => time()
-//            ]);
-//        }
+        //取消的订单退回优惠券
+        if ($type == 'cancel' && $order['coupon_id'] && $order['coupon_price']) {
+            /** @var StoreCouponUserServices $couponUserServices */
+            $couponUserServices = app()->make(StoreCouponUserServices::class);
+            $res = $couponUserServices->recoverCoupon((int)$order['coupon_id']);
+            $statusService->save([
+                'oid' => $order['id'],
+                'change_type' => 'coupon_back',
+                'change_message' => '商品退优惠券',
+                'change_time' => time()
+            ]);
+        }
         //回退积分
         $order = $this->regressionIntegral($order);
         $statusService->save([

+ 5 - 19
crmeb/app/services/order/StoreOrderServices.php

@@ -1855,9 +1855,10 @@ HTML;
 
     /**
      * 删除订单
-     * @param $uni
-     * @param $uid
+     * @param string $uni
+     * @param int $uid
      * @return bool
+     * @throws \Psr\SimpleCache\InvalidArgumentException
      */
     public function removeOrder(string $uni, int $uid)
     {
@@ -1879,21 +1880,6 @@ HTML;
             'change_time' => time()
         ]);
         if ($order->save() && $res) {
-            //未支付和已退款的状态下才可以退积分退库存退优惠券
-            if ($order['_status']['_type'] == 0 || $order['_status']['_type'] == -2) {
-                /** @var StoreOrderRefundServices $refundServices */
-                $refundServices = app()->make(StoreOrderRefundServices::class);
-                $this->transaction(function () use ($order, $refundServices) {
-                    //回退积分和优惠卷
-                    $res = $refundServices->integralAndCouponBack($order);
-                    //回退库存
-                    $res = $res && $refundServices->regressionStock($order);
-                    if (!$res) {
-                        throw new ApiException(100020);
-                    }
-                });
-
-            }
             return true;
         } else
             throw new ApiException(100020);
@@ -1924,7 +1910,7 @@ HTML;
         $refundServices = app()->make(StoreOrderRefundServices::class);
 
         $this->transaction(function () use ($refundServices, $order) {
-            $res = $refundServices->integralAndCouponBack($order) && $refundServices->regressionStock($order);
+            $res = $refundServices->integralAndCouponBack($order, 'cancel') && $refundServices->regressionStock($order);
             $order->is_del = 1;
             if (!($res && $order->save())) {
                 throw new ApiException(100020);
@@ -2136,7 +2122,7 @@ HTML;
                 try {
                     $this->transaction(function () use ($order, $refundServices) {
                         //回退积分和优惠卷
-                        $res = $refundServices->integralAndCouponBack($order);
+                        $res = $refundServices->integralAndCouponBack($order, 'cancel');
                         //回退库存和销量
                         $res = $res && $refundServices->regressionStock($order);
                         //修改订单状态