Ver Fonte

后台订单取消

wanghaijun há 4 anos atrás
pai
commit
aff41dd060

+ 1 - 2
app/Community/Actions/Clinc/OrderCancel.php

xqd xqd
@@ -27,7 +27,7 @@ class OrderCancel extends RowAction
         DB::beginTransaction();
         try {
             //退还余额
-            $res = Order::orderCancel($id,'社区取消');
+            $res = Order::appiontOrderCancel($id,'社区取消');
             $msg = [
                 $openid,$sn,'门诊预约',$price,$time,$pantient,$organization,'社区取消',$paystatus
             ];
@@ -38,7 +38,6 @@ class OrderCancel extends RowAction
             $ret = admin_send_wechat_message(1,$msg,$miniMsg);
             DB::commit();
         } catch ( \Exception $e){
-            dd($e->getMessage());
             DB::rollBack();
             return $this->response()->error('操作失败!');
         }

+ 1 - 2
app/Community/Actions/Nurse/OrderCance.php

xqd xqd
@@ -30,7 +30,7 @@ class OrderCance extends RowAction
         DB::beginTransaction();
         try {
             //退还余额
-            $res = Order::orderCancel($id,'社区取消');
+            $res = Order::appiontOrderCancel($id,'社区取消');
             if(empty($order->orderUser) || empty($order->orderUser->openid) ) {
                 DB::commit();
                 return $this->response()->success('操作成功!')->refresh();
@@ -46,7 +46,6 @@ class OrderCance extends RowAction
 //            $ret = admin_send_wechat_message(1,$msg,$miniMsg);
             DB::commit();
         } catch ( \Exception $e){
-            dump($e->getMessage());
             DB::rollBack();
             return $this->response()->error('操作失败!');
         }

+ 1 - 2
app/Community/Actions/Vaccine/OrderCance.php

xqd xqd
@@ -28,7 +28,7 @@ class OrderCance extends RowAction
         DB::beginTransaction();
         try {
             //退还余额
-            $res = Order::orderCancel($id,'社区取消');
+            $res = Order::appiontOrderCancel($id,'社区取消');
             $msg = [
                 $openid,$sn,'疫苗接种预约',$price,$time,$pantient,$organization,'社区取消',$paystatus
             ];
@@ -40,7 +40,6 @@ class OrderCance extends RowAction
 
             DB::commit();
         } catch ( \Exception $e){
-            dd($e->getMessage());
             DB::rollBack();
             return $this->response()->error('操作失败!');
         }

+ 67 - 0
app/Models/Order.php

xqd
@@ -414,4 +414,71 @@ class Order extends BaseModel
 
         return true;
     }
+
+    //后台预约订单取消
+    public static function appiontOrderCancel($order_id, $order_notes = '')
+    {
+        $order = Order::with(['orderPatient'])->where('id', $order_id)->first()->toArray();
+        //判断预约时间是否还剩1小时内
+        $can_refund = true;
+        if (in_array($order['product_type'], [3,4,5])) {
+            if ($order['order_patient']['appoint_start_time'] - 3600 < time()) {
+                $can_refund = false;
+            }
+        }
+        //改变订单状态
+        $updateOrder = ['order_status' => 5, 'order_notes' => $order_notes];
+        if ($order['payment_status'] > 1) {
+            //判断是余额支付还是服务包支付
+            if ($order['payment_type'] == 3) {
+                //退服务包的次数
+                $map = [1 => 'phone_minutes', 2 => 'chat_num', 3 => 'appoint_num', 4 => 'vaccine_limit_amount', 5 => 'nurses_limit_amount'];
+                OrderPack::where('id', $order['pay_order_pack_id'])->increment($map[$order['product_type']]);
+                $updateOrder['cancel_time'] = time();
+                $updateOrder['payment_status'] = 4;
+                if ($order['product_type'] == 3 && !$can_refund) {
+                    $updateOrder['payment_status'] = 2;
+                }
+            }
+            else {
+                //申请退款
+                $updateOrder['cancel_time'] = time();
+                //实际付款大于0元才发起退款
+                if ($can_refund && $order['payment_amount'] > 0) {
+                    $updateOrder['payment_status'] = 5;
+                    $arr = [
+                        'order_id' =>$order_id,
+                        'status' => 1,
+                        'refund_applicant' => \Admin::user()->id,
+                    ];
+                    RefundApplication::create($arr);
+                }
+
+                if ($order['product_type'] == 3 && !$can_refund) {
+                    $updateOrder['payment_status'] = 2;
+                }
+            }
+        }
+        Order::where('id', $order_id)->update($updateOrder);
+
+        //如果是门诊预约且距离预约时间还有1个小时以上,那么预约时间段的订单数减1
+        if ($order['product_type'] == 3 && $can_refund) {
+            $schedule_date = date('Y-m-d', $order['order_patient']['appoint_start_time']);
+            SchedulePeriod::where('docter_id', $order['docter_id'])->where('organization_id', $order['organization_id'])->where('time_period_id', $order['order_patient']['time_period_id'])->where('schedule_date', $schedule_date)->where('order_num', '>', 0)->decrement('order_num');
+        }
+
+        //如果是疫苗儿保预约且距离预约时间还有1个小时以上,那么预约时间段的订单数减1
+        if (in_array($order['product_type'], [4,5]) && $can_refund) {
+            $schedule_type_map = [4 => 2, 5 => 3];
+            $schedule_date = date('Y-m-d', $order['order_patient']['appoint_start_time']);
+            SchedulePeriod::where('organization_id', $order['organization_id'])->where('time_period_id', $order['order_patient']['time_period_id'])->where('schedule_date', $schedule_date)->where('schedule_type', $schedule_type_map[$order['product_type']])->where('order_num', '>', 0)->decrement('order_num');
+        }
+        //如果是疫苗预约那么取消订单就增加疫苗库存
+        if ($order['product_type'] == 4) {
+            $orderVaccine = OrderVaccine::where('order_id', $order_id)->first();
+            Vaccine::where('id', $orderVaccine['vaccine_id'])->increment('stock');
+        }
+
+        return true;
+    }
 }