xieruidong 2 years ago
parent
commit
e5c4629d8f
2 changed files with 32 additions and 26 deletions
  1. 12 0
      application/common.php
  2. 20 26
      application/common/service/OrderService.php

+ 12 - 0
application/common.php

@@ -632,4 +632,16 @@ function es(){
         $es= ClientBuilder::create()->build();
     }
     return $es;
+}
+
+/**
+ * 累加全部数字
+ * @param mixed ...$args
+ */
+function bcAddAll(...$args){
+    $init=0;
+    foreach (func_get_args() as $num){
+        $init=bcadd($init,$num);
+    }
+    return $init;
 }

+ 20 - 26
application/common/service/OrderService.php

@@ -174,47 +174,41 @@ class OrderService extends BaseService {
         $info['num']=array_sum(array_column($info['goods'],'num'));
         $info['num_install']=array_sum(array_column($info['goods'],'num_install'));
 
+        $injectCoupon=false;
         foreach ($info['goods'] as &$goods){
             list($amountReal,$amountKill,$amountOrigin)=$this->getAmount($goods['sku'],$goods['goods']);
-            $goods['amount_pay']=bcmul($amountReal,$goods['num']);
+            $goods['amount_pay']=0;
+            $goods['amount_coupon']=0;
+
+            $goods['amount_goods']=bcmul($amountReal,$goods['num']);
             $goods['amount_total']=bcmul($amountOrigin,$goods['num']);
             $goods['amount_coupon_kill']=bcmul($amountKill,$goods['num']);
             $goods['amount_install']=InstallService::getFee([$goods['sku']['id']=>$goods]);
 
-            $info['amount_pay']=bcadd($info['amount_pay'],$goods['amount_pay']);
-            $info['amount_total']=bcadd($info['amount_total'],$goods['amount_total']);
-            $info['amount_coupon_kill']=bcadd($info['amount_coupon_kill'],$goods['amount_coupon_kill']);
-            $info['amount_install']=bcadd($info['amount_install'],$goods['amount_install']);
-        }
-
-        #优惠券
-        $couponAmount=0;
-        if(!empty($this->coupon_use)){
-            $use=false;
-            $coupon=$user->coupon()->lock($this->isSubmit())->findOrFail($this->coupon_id);
-            foreach ($info['goods'] as &$goods){
-                $amount=$goods['amount_pay'];
-                if($coupon->canUse($amount,$goods['goods_id'])){
-                    $use=true;
+            if(!empty($this->coupon_use) && !$injectCoupon){
+                $coupon=$user->coupon()->lock($this->isSubmit())->findOrFail($this->coupon_id);
+                if($coupon->canUse($goods['amount_goods'],$goods['goods_id'])){
                     $couponAmount=$coupon['amount'];
                     $goods['use_coupon_id']=$coupon['id'];
-                    $goods['use_coupon_amount']=$couponAmount;
+                    $goods['amount_coupon']=$couponAmount;
                     if($this->isSubmit()){
                         $coupon->setUse();
                     }
-                    break;
+                    $injectCoupon=true;
                 }
             }
 
-            if(!$use){
-                $this->error('优惠券不符合使用规则');
-            }
-            $info['amount_coupon']=$couponAmount;
-        }
+            $goods['amount_total']=bcadd($goods['amount_total'],$goods['amount_install']);
+            $goods['amount_pay']=bcAddAll($goods['amount_goods'],$goods['amount_install'],-$goods['amount_coupon']);
 
-        $info['amount_pay']=bcsub($info['amount_pay'],$couponAmount);
-        $info['amount_pay']=bcadd($info['amount_pay'],$info['amount_install']);
-        $info['amount_total']=bcadd($info['amount_total'],$info['amount_install']);
+            $info['amount_pay']=bcadd($info['amount_pay'],$goods['amount_pay']);
+            $info['amount_total']=bcadd($info['amount_total'],$goods['amount_total']);
+            $info['amount_coupon_kill']=bcadd($info['amount_coupon_kill'],$goods['amount_coupon_kill']);
+            $info['amount_install']=bcadd($info['amount_install'],$goods['amount_install']);
+        }
+        if($this->coupon_use && !$injectCoupon){
+            $this->error('优惠券不符合使用规则');
+        }
 
         #删除购物车
         if($this->isClearCart()){