Jelajahi Sumber

优惠券处理

fangx 1 tahun lalu
induk
melakukan
3e08583db3
1 mengubah file dengan 47 tambahan dan 60 penghapusan
  1. 47 60
      app/api/logic/ServiceOrderLogic.php

+ 47 - 60
app/api/logic/ServiceOrderLogic.php

@@ -64,7 +64,7 @@ class ServiceOrderLogic extends BaseLogic
                 }
             }
         }
-        throw new Exception('已超出服务区域!');
+        throw new Exception('已超出服务区域!-1001');
     }
 
     /**
@@ -271,18 +271,6 @@ class ServiceOrderLogic extends BaseLogic
             $order_goods = OrderGoods::where('sn',$params['sn'])->findOrEmpty();
             $goods = Goods::findOrEmpty($order_goods['goods_id']);
             //判断是否存在优惠券
-            //优惠券验证
-            if(!empty($order['coupon_id'])){
-                $order->coupon_id = 0;
-                $order->coupon_price = 0;
-                $order->order_amount = $order->order_total;
-                $order->save();
-                $user_coupon_ed = UserCoupon::findOrEmpty($order['coupon_id']);
-                $user_coupon_ed->voucher_count = $user_coupon_ed->voucher_count+1;
-                $user_coupon_ed->voucher_status = 0;
-                $user_coupon_ed->save();
-                $order = \app\common\model\recharge\RechargeOrder::where('sn',$order['sn'])->findOrEmpty();
-            }
             if(!empty($params['coupon_id']) && empty($order['coupon_id'])){
                 $user_coupon = UserCoupon::where(['id'=>$params['coupon_id'],'user_id'=>$params['user_id'],'voucher_status'=>0])
                     ->where('voucher_count','>',0)
@@ -313,12 +301,13 @@ class ServiceOrderLogic extends BaseLogic
                 $user_coupon->voucher_status = 1;
                 $user_coupon->voucher_count = $user_coupon->voucher_count-1;
                 $user_coupon->save();
+
+                $order->coupon_id = !empty($params['coupon_id'])?$params['coupon_id']:0;
+                $order->coupon_price = !empty($order_coupon_amount)?$order_coupon_amount:0;
+                $order->order_amount = !empty($order_amount)?$order_amount:$order->order_amount;
+                $order->save();
             }
 
-            $order->coupon_id = !empty($params['coupon_id'])?$params['coupon_id']:0;
-            $order->coupon_price = !empty($order_coupon_amount)?$order_coupon_amount:0;
-            $order->order_amount = !empty($order_amount)?$order_amount:$order->order_amount;
-            $order->save();
             Db::commit();
         } catch (\Exception $e) {
             Db::rollback();
@@ -592,33 +581,14 @@ class ServiceOrderLogic extends BaseLogic
      */
     public static function orderPayInfo($params)
     {
+        Db::startTrans();
         try {
-
-            //判断是否存在优惠券
-            $is_coupon_used_work_id = \app\common\model\recharge\RechargeOrder::where([
-                'order_type' => 0,
-                'user_id' => $params['user_id'],
-                'sn'=>$params['sn']
-            ])->value('work_id');
-            $is_coupon_used = \app\common\model\recharge\RechargeOrder::where('work_id',$is_coupon_used_work_id)->where('pay_status',0)->findOrEmpty();
-            if(!empty($is_coupon_used['coupon_id'])){
-                $user_coupon = UserCoupon::where(['user_id'=>$is_coupon_used->user_id,'id'=>$is_coupon_used->coupon_id])->findOrEmpty();
-                $user_coupon->voucher_status = 0;
-                $user_coupon->voucher_count = $user_coupon->voucher_count+1;
-                $user_coupon->save();
-                $is_coupon_used->order_amount = $is_coupon_used->order_amount+$is_coupon_used->coupon_price;
-                $is_coupon_used->coupon_price = 0;
-                $is_coupon_used->coupon_id = 0;
-                $is_coupon_used->save();
-            }
-
-
             $order_info =  \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
                 $query->visible(['goods_id','goods_payment_type','goods_category_id']);
             },'service_work'=>function ($query) {
                 $query->visible(['service_fee','spare_total','service_work_spare_id','goods_category_id']);
             }])
-                ->visible(['id','sn','work_id'])
+                ->visible(['id','pay_status','sn','work_id','coupon_id','coupon_price'])
                 ->where([
                     'order_type' => 0,
                     'user_id' => $params['user_id'],
@@ -634,7 +604,7 @@ class ServiceOrderLogic extends BaseLogic
             $order_info['paid_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('paid_amount');
 
             //搜索当前工单下的所有订单记录
-            $order_info['pay_orders'] = \app\common\model\recharge\RechargeOrder::where(['work_id'=>$order_info['work_id']])->field('id as order_id, pay_status,refund_status,payment_type,pay_way,pay_time,order_total,order_amount,coupon_price')->order('id asc')->select()->toArray();
+            $order_info['pay_orders'] = \app\common\model\recharge\RechargeOrder::where(['work_id'=>$order_info['work_id']])->field('id as order_id, pay_status,refund_status,payment_type,pay_way,pay_time,order_total,order_amount,coupon_id,coupon_price')->order('id asc')->select()->toArray();
 
             $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
             $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
@@ -655,9 +625,13 @@ class ServiceOrderLogic extends BaseLogic
                     $v['order_total'] = $v['order_total'] - $order_info['service_work']['spare_total'];
                     $v['order_amount'] = $v['order_amount'] - $order_info['service_work']['spare_total'] + $v['coupon_price'];
                 }
-
-                $coupon_price += $v['coupon_price'];
-
+                if($v['pay_status']!=0){
+                    $coupon_price += $v['coupon_price'];
+                }
+                if($v['pay_status'] == 0){
+                    $order_info['coupon_id'] = $v['coupon_id'];
+                    $order_info['coupon_price'] = $v['coupon_price'];
+                }
                 $payment_type = $v['payment_type'];
             }
             $order_info['payment_type'] = $payment_type;
@@ -697,25 +671,38 @@ class ServiceOrderLogic extends BaseLogic
             }
 
             //获取是否存在可使用优惠券
-            $coupon_ids = CouponCategory::where('goods_category_id',$order_info['service_work']['goods_category_id'])
-                ->whereIn('coupon_id',UserCoupon::where('user_id',$params['user_id'])
-                    ->where('voucher_count','>',0)
-                    ->where('voucher_status',0)
-                    ->where('expire_time','>',time())
-                    ->where('amount_require','<=',$order_info['order_amount'])
-                    ->column('coupon_id')
-                )->column('coupon_id');
-            $usable_coupon = UserCoupon::where('user_id',$params['user_id'])
-                ->whereIn('coupon_id',$coupon_ids)
-                ->append(['discount_ratio_text'])
-                ->where('expire_time','>',time())
-                ->count();
-
-            $order_info['usable_coupon'] = $usable_coupon>0?true:false;
-
+            if(!empty($order_info['coupon_id'])){
+                $order_info['order_amount'] = $order_info['order_amount'] + $order_info['coupon_price'];
+                $order_info['coupon'] = UserCoupon::where('id',$order_info['coupon_id'])->findOrEmpty()->toArray();
+                $usable_coupon = true;
+            }else{
+                if(!empty($order_info['order_goods'][0])){
+                    $usable_coupon = UserCouponLogic::categoryWithAmountLists(['user_id'=>$params['user_id'],'amount'=>$order_info['order_amount'],'goods_category_id'=>$order_info['order_goods'][0]['goods_category_id'],'goods_id'=>$order_info['order_goods'][0]['goods_id']]);
+                    if(!empty($usable_coupon[0]['id'])){
+                        //绑定最新可使用优惠券
+                        $final_order = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id'],'pay_status'=>0])->findOrEmpty();
+                        if(!empty($final_order)){
+                            $user_coupon = UserCoupon::where('id',$usable_coupon[0]['id'])->findOrEmpty();
+                            $user_coupon->voucher_status = 1;
+                            $user_coupon->voucher_count = $user_coupon->voucher_count-1;
+                            $user_coupon->save();
+
+
+                            $final_order->coupon_id = $usable_coupon[0]['id'];
+                            $final_order->coupon_price = $usable_coupon[0]['amount'];
+                            $final_order->order_amount = $final_order->order_amount - $usable_coupon[0]['amount'];
+                            $final_order->save();
+
+                            $order_info['coupon'] = $usable_coupon[0];
+                        }
+                    }
+                }
+            }
+            $order_info['usable_coupon'] = !empty($usable_coupon)?true:false;
+            Db::commit();
             return $order_info;
-        }
-        catch (\Exception $e) {
+        } catch (\Exception $e) {
+            Db::rollback();
             self::setError($e->getMessage());
             return false;
         }