Browse Source

康养城订单管理

gorden 2 months ago
parent
commit
50c430878b

+ 110 - 103
app/admin/controller/order/KangyangCityController.php

@@ -33,7 +33,8 @@ use support\Request;
 use support\Response;
 use Webman\Event\Event;
 
-class KangyangCityController extends Curd{
+class KangyangCityController extends Curd
+{
     public function __construct()
     {
         $this->model = new Order();
@@ -63,9 +64,9 @@ class KangyangCityController extends Curd{
         // $where['order_id'] = '';
         $where['order_category'] = 'VIP';
         // $goodsClassify = 'VIP';
-        $goodsName = $request->get('goods_name','');
-        if (!empty($goodsName) && empty($goodsClassify)){
-            $goodsIds = Goods::where('goods_name','like','%'.$request->get('goods_name').'%')->pluck('goods_id')->toArray();
+        $goodsName = $request->get('goods_name', '');
+        if (!empty($goodsName) && empty($goodsClassify)) {
+            $goodsIds = Goods::where('goods_name', 'like', '%' . $request->get('goods_name') . '%')->pluck('goods_id')->toArray();
         }
         // else if(!empty($goodsClassify)){
         //     if($goodsClassify == 'SERVICE'){
@@ -80,10 +81,10 @@ class KangyangCityController extends Curd{
 
         //     $goodsIds = $goodsModel->pluck('goods_id')->toArray();
         // }
-        if(!empty($goodsIds)){
-            $orderIds = OrderSheet::whereIn('join_sheet_goods_id',$goodsIds)->pluck('join_sheet_order_id')->toArray();
-            if($orderIds){
-                $where['order_id'] = ['in',implode(',',$orderIds)];
+        if (!empty($goodsIds)) {
+            $orderIds = OrderSheet::whereIn('join_sheet_goods_id', $goodsIds)->pluck('join_sheet_order_id')->toArray();
+            if ($orderIds) {
+                $where['order_id'] = ['in', implode(',', $orderIds)];
             }
         }
 
@@ -95,7 +96,7 @@ class KangyangCityController extends Curd{
     {
         $model = $this->model->with([
             'sheets' => function ($query) {
-                $query->select('join_sheet_order_id', 'order_sheet_id', 'join_sheet_goods_id', 'order_sheet_num','order_sheet_price');
+                $query->select('join_sheet_order_id', 'order_sheet_id', 'join_sheet_goods_id', 'order_sheet_num', 'order_sheet_price');
             },
             'member' => function ($query) {
                 $query->select('member_id', 'member_mobile');
@@ -113,8 +114,8 @@ class KangyangCityController extends Curd{
             // 'express' => function ($query) {
             //     $query->select('join_express_order_id', 'order_express_type');
             // }
-        ])->leftJoin('order_return','order_return.join_return_order_id','=','order.order_id')
-        ->leftJoin('order_express','order_express.join_express_order_id','=','order.order_id');
+        ])->leftJoin('order_return', 'order_return.join_return_order_id', '=', 'order.order_id')
+            ->leftJoin('order_express', 'order_express.join_express_order_id', '=', 'order.order_id');
         // ->leftJoin('order_sheet','join_sheet_order_id','=','order.order_id');
         foreach ($where as $column => $value) {
             if (is_array($value)) {
@@ -148,7 +149,7 @@ class KangyangCityController extends Curd{
         if ($field) {
             $model = $model->orderBy($field, $order);
         }
-        $model = $model->select('order.*','order_express.join_express_order_id','order_express.order_express_type','order_return.orders_return_id', 'order_return.join_return_order_id', 'order_return.order_return_status','order_return.order_return_apply_json','order_return.order_return_remark');
+        $model = $model->select('order.*', 'order_express.join_express_order_id', 'order_express.order_express_type', 'order_return.orders_return_id', 'order_return.join_return_order_id', 'order_return.order_return_status', 'order_return.order_return_apply_json', 'order_return.order_return_remark');
         return $model;
     }
 
@@ -159,29 +160,29 @@ class KangyangCityController extends Curd{
             $item['sheet'] = $item['sheets'][0] ?? [];
             if (!empty($item['sheet'])) {
                 $goods = Goods::where('goods_id', $item['sheet']['join_sheet_goods_id'])->first();
-                if(count($item['sheets']) > 1 && $goods->goods_classify == 'MEALS'){
+                if (count($item['sheets']) > 1 && $goods->goods_classify == 'MEALS') {
                     $sheetDeng = ' 等餐品';
                 }
-                $item['sheet']['goods_name'] = ($goods && $goods->goods_name) ? $goods->goods_name.$sheetDeng : '';
+                $item['sheet']['goods_name'] = ($goods && $goods->goods_name) ? $goods->goods_name . $sheetDeng : '';
                 $item['sheet']['goods_classify'] = $goods->goods_classify ?? '';
                 $item['sheet']['order_sheet_num'] = intval($item['sheet']['order_sheet_num']);
             }
             unset($item['sheets']);
-            if (isset($item['orders_return_id'])){
+            if (isset($item['orders_return_id'])) {
                 $item['return'] = [
-                    'orders_return_id'=>$item['orders_return_id'],
+                    'orders_return_id' => $item['orders_return_id'],
                     'join_return_order_id' => $item['join_return_order_id'],
                     'order_return_status' => $item['order_return_status'],
                     'order_return_apply_json' => $item['order_return_apply_json'],
-                    'order_return_remark'=>$item['order_return_remark']
+                    'order_return_remark' => $item['order_return_remark']
                 ];
             }
-            if (isset($item['join_express_order_id'])){
+            if (isset($item['join_express_order_id'])) {
                 $item['express'] = [
-                    'join_express_order_id'=>$item['join_express_order_id'],
-                    'order_express_type'=>$item['order_express_type']
+                    'join_express_order_id' => $item['join_express_order_id'],
+                    'order_express_type' => $item['order_express_type']
                 ];
-                unset($item['join_express_order_id'],$item['order_express_type']);
+                unset($item['join_express_order_id'], $item['order_express_type']);
             }
             // if (!empty($item['order_extend_json'])){
             //     $orderExtendJson = json_decode($item['order_extend_json'],true);
@@ -315,27 +316,6 @@ class KangyangCityController extends Curd{
                         throw new BusinessException('付款码无效');
                     }
 
-                    // 组合支付,追加加一条支付记录 pay_detail
-                    if ($params['pay_constitute'] == 'Y' && $qrcodePayAmount > 0 && $params['order_status_payment'] == 'SUCCESS') {
-                        $insertPayDetailData = [
-                            'join_pay_member_id' => $params['join_order_member_id'],
-                            'join_pay_order_id' => $params['orderGroupId'],
-                            'pay_status' => 'SUCCESS',
-                            'pay_category' => $params['goods_classify'],
-                            'pay_amount' => $params['order_amount_pay'],
-                            'pay_paytimes' => date('Y-m-d H:i:s'),
-                            'pay_prepayid' => $params['pay_category'],
-                            'pay_json_request' => json_encode($params),
-                            'pay_json_response' => $params['pay_json_response'],
-                            'pay_addtimes' => time()
-                        ];
-
-                        PayDetail::insert($insertPayDetailData);
-
-                        // 组合支付,还原提交的支付分类
-                        $params['pay_category'] = $submitPayCategory;
-                    }
-
                     // 账户支付的金额
                     $params['order_amount_pay'] = $accountAmount;
                 }
@@ -350,31 +330,42 @@ class KangyangCityController extends Curd{
                 //     'express' => 'Y',
                 // ]);
             }
-            $params['order_amount_pay'] = $params['order_amount_pay'] + $qrcodePayAmount;
             $params['order_config_json'] = json_encode($orderConfigJson);
-            // 写入主订单
-            $this->insertMain($params);
-            // 订单详情
-            $sheetId = $this->insertSheet($params);
+            // 查询是否有未完全支付订单
+            $paidOrder = Order::where('join_order_member_id', $params['join_order_member_id'])
+                ->where('order_category', 'VIP')
+                ->where('order_status_system', 'PAYING')
+                ->first();
+
+            if (empty($paidOrder)) {
+                // 写入主订单
+                $this->insertMain($params);
+                // 订单详情
+                $this->insertSheet($params);
+            } else {
+                $params['orderGroupId'] = $paidOrder->order_groupby;
+                $params['order_amount_paid'] = $paidOrder->order_amount_paid;
+                $paidOrder->order_amount_paid = $paidOrder->order_amount_paid + $params['order_amount_pay'];
+                $paidOrder->order_amount_pay = $paidOrder->order_amount_paid;
+                if (intval($paidOrder->order_amount_pay) >= 24000) {
+                    $paidOrder->order_is_complete = 'Y';
+                    $paidOrder->order_status_system = 'DONE';
+                    $paidOrder->order_status_payment = 'SUCCESS';
+                }
+                $paidOrder->save();
+            }
             // 支付记录
-            $params['order_amount_pay'] = $params['order_amount_pay'] - $qrcodePayAmount;
             $this->insertPayDetail($params);
 
-            $params['order_express_goods'] = json_encode(['sheet' => [$sheetId]]);
+//            $params['order_express_goods'] = json_encode(['sheet' => [$sheetId]]);
 
             // 2.4W 康养城
-            if ($params['goods_classify'] == 'VIP' && $params['order_status_payment'] == 'SUCCESS') {
+            if ($params['goods_classify'] == 'VIP' && $params['order_status_payment'] == 'SUCCESS' && (!empty($paidOrder) && intval($paidOrder->order_amount_paid + $params['order_amount_pay']) >= 24000 || intval($params['order_amount_pay']) >= 24000)) {
                 $params['member_id'] = $params['join_order_member_id'];
                 Event::dispatch('order.kangyangCityVIP.grant', $params);
             }
 
             Db::commit();
-
-            // 会员升级
-            if (!empty($params['order_is_complete']) && $params['order_is_complete'] == 'Y' && $params['order_status_payment'] == 'SUCCESS') {
-                Event::dispatch('order.complete', $params);
-            }
-
             if ($params['settlement_now'] == 'Y' && $params['order_status_payment'] != 'SUCCESS') {
                 _syslog("订单", "支付异常,检查是否有轮询");
                 return json_throw(2001, '支付异常', ['order_id' => $params['orderId']]);
@@ -444,6 +435,7 @@ class KangyangCityController extends Curd{
         if ($params['goods_classify'] == 'PACKAGE' && $params['delivery'] == 'ARRIVAL') {
             $systemStatus = "WAITING";
         }
+        $paymentStatus = 'PENDING';
         Db::beginTransaction();
         try {
             // 组合支付时,付款码应收金额
@@ -451,6 +443,7 @@ class KangyangCityController extends Curd{
             if ($params['pay_category'] == 'OFFLINE' || $params['pay_category'] == 'MONEY') {
                 $order->order_status_system = $systemStatus;
                 $order->order_status_payment = 'SUCCESS';
+                $paymentStatus = 'SUCCESS';
                 if ($params['pay_category'] == 'OFFLINE' && !empty($params['pay_category_sub'])) {
                     $params['pay_category'] = $params['pay_category_sub'];
                 }
@@ -482,6 +475,7 @@ class KangyangCityController extends Curd{
                     } else {
                         $order->order_status_system = $systemStatus;
                         $order->order_status_payment = 'SUCCESS';
+                        $paymentStatus = 'SUCCESS';
                     }
                 } else if (in_array($prefix, [25, 26, 27, 28, 29, 30])) {
                     $params['pay_category'] = 'ALIPAY';
@@ -494,33 +488,12 @@ class KangyangCityController extends Curd{
                     } else {
                         $order->order_status_system = $systemStatus;
                         $order->order_status_payment = 'SUCCESS';
+                        $paymentStatus = 'SUCCESS';
                     }
                 } else {
                     throw new BusinessException('付款码无效');
                 }
 
-                // 组合支付,追加加一条支付记录 pay_detail
-                if ($params['pay_constitute'] == 'Y' && $qrcodePayAmount > 0) {
-                    $insertPayDetailData = [
-                        'join_pay_member_id' => $params['join_order_member_id'],
-                        'join_pay_order_id' => $params['orderGroupId'],
-                        'pay_status' => 'SUCCESS',
-                        'pay_category' => $params['goods_classify'],
-                        'pay_amount' => $params['order_amount_pay'],
-                        'pay_paytimes' => date('Y-m-d H:i:s'),
-                        'pay_prepayid' => $params['pay_category'],
-                        'pay_json_request' => json_encode($params),
-                        'pay_json_response' => $params['pay_json_response'],
-                        'join_pay_object_json' => !empty($params['orderId']) ? json_encode(['order_id' => $params['orderId']]) : '[]',
-                        'pay_addtimes' => time()
-                    ];
-
-                    PayDetail::insert($insertPayDetailData);
-
-                    // 组合支付,还原提交的支付分类
-                    $params['pay_category'] = $submitPayCategory;
-                }
-
                 // 账户支付的金额
                 $params['order_amount_pay'] = $accountAmount;
             }
@@ -533,11 +506,17 @@ class KangyangCityController extends Curd{
                 SysDept::where('dept_id', $orderConfigJson['tableid'])->where('dept_category', '桌台')->update(['dept_status' => 'ACTIVED']);
             }
             $order->order_config_json = json_encode($orderConfigJson);
-            $order->order_amount_pay = $params['order_amount_pay'] + $qrcodePayAmount;
             // 康养城订单,支付完就结束了
-            if ($order->order_status_payment == 'SUCCESS' && $params['goods_classify'] == 'VIP') {
+            if ($order->order_status_payment == 'SUCCESS' && $params['goods_classify'] == 'VIP' && intval($order->order_amount_paid + $params['order_amount_pay']) >= 24000) {
                 $order->order_is_complete = 'Y';
             }
+            if (intval($order->order_amount_paid + $params['order_amount_pay']) < 24000) {
+                $order->order_status_system = 'PAYING';
+                $order->order_status_payment = 'PENDING';
+                $order->order_is_complete = 'N';
+            }
+            $order->order_amount_paid = $order->order_amount_paid + $params['order_amount_pay'];
+            $order->order_amount_pay = $order->order_amount_paid;
             // 主订单
             $order->save();
 
@@ -551,7 +530,7 @@ class KangyangCityController extends Curd{
             $payData = [
                 'pay_amount' => $params['order_amount_pay']
             ];
-            if ($order->order_status_payment == 'SUCCESS') {
+            if ($paymentStatus == 'SUCCESS') {
                 $payData['pay_paytimes'] = date('Y-m-d H:i:s');
                 $payData['pay_status'] = 'SUCCESS';
             }
@@ -574,26 +553,26 @@ class KangyangCityController extends Curd{
                 $payData['pay_prepayid'] = 'MONEY';
             }
             // 如果 是APP 过来的菜订单,可能没有paydetail
-            if (!PayDetail::where('join_pay_order_id', $order->order_groupby)->where('pay_category', '<>', 'WXPAY')->where('pay_category', '<>', 'ALIPAY')->exists()) {
-                $payData['join_pay_member_id'] = $params['join_order_member_id'];
-                $payData['join_pay_order_id'] = $order->order_groupby;
-                $payData['pay_status'] = $payData['pay_status'] == 'SUCCESS' ? $payData['pay_status'] : 'WAITING';
-                $payData['pay_category'] = $params['goods_classify'] ?? '';
-                $payData['pay_json_request'] = json_encode($params);   // {"pay-result": "支付成功", "result-datetime": "2024-07-29 18:38:21"}
-                $payData['pay_json_response'] = $payData['pay_status'] == 'SUCCESS' ? json_encode([
-                    'pay-result' => '支付成功', 'result-datetime' => date('Y-m-d H:i:s')
-                ]) : '[]';
-                $payData['join_pay_object_json'] = !empty($params['orderId']) ? json_encode(['order_id' => $params['orderId']]) : '[]';
-                $payData['pay_addtimes'] = time();
-
-                PayDetail::insert($payData);
-            } else {
-                // 更新非微信支付宝的支付记录
-                PayDetail::where('join_pay_order_id', $order->order_groupby)->where('pay_category', '<>', 'WXPAY')->where('pay_category', '<>', 'ALIPAY')->update($payData);
-            }
+//            if (!PayDetail::where('join_pay_order_id', $order->order_groupby)->where('pay_category', '<>', 'WXPAY')->where('pay_category', '<>', 'ALIPAY')->exists()) {
+            $payData['join_pay_member_id'] = $params['join_order_member_id'];
+            $payData['join_pay_order_id'] = $order->order_groupby;
+            $payData['pay_status'] = $payData['pay_status'] == 'SUCCESS' ? $payData['pay_status'] : 'WAITING';
+            $payData['pay_category'] = $params['goods_classify'] ?? '';
+            $payData['pay_json_request'] = json_encode($params);   // {"pay-result": "支付成功", "result-datetime": "2024-07-29 18:38:21"}
+            $payData['pay_json_response'] = $payData['pay_status'] == 'SUCCESS' ? json_encode([
+                'pay-result' => '支付成功', 'result-datetime' => date('Y-m-d H:i:s')
+            ]) : '[]';
+            $payData['join_pay_object_json'] = !empty($params['orderId']) ? json_encode(['order_id' => $params['orderId']]) : '[]';
+            $payData['pay_addtimes'] = time();
+
+            PayDetail::insert($payData);
+//            } else {
+//                // 更新非微信支付宝的支付记录
+//                PayDetail::where('join_pay_order_id', $order->order_groupby)->where('pay_category', '<>', 'WXPAY')->where('pay_category', '<>', 'ALIPAY')->update($payData);
+//            }
 
             // 2.4W 康养城
-            if ($params['goods_classify'] == 'VIP' && $order->order_status_payment == 'SUCCESS') {
+            if ($params['goods_classify'] == 'VIP' && $order->order_status_payment == 'SUCCESS' && intval($order->order_amount_paid + $params['order_amount_pay']) >= 24000) {
                 $params['member_id'] = $params['join_order_member_id'];
                 Event::dispatch('order.kangyangCityVIP.grant', $params);
             }
@@ -604,7 +583,7 @@ class KangyangCityController extends Curd{
                 Event::dispatch('order.complete', $params);
             }
 
-            if ($order->order_status_payment != 'SUCCESS') {
+            if ($order->order_status_payment != 'SUCCESS' && $paymentStatus != 'SUCCESS') {
                 _syslog("订单", "支付异常,检查是否有轮询");
                 return json_throw(2001, '支付异常', ['order_id' => $params['orderId']]);
             }
@@ -660,10 +639,11 @@ class KangyangCityController extends Curd{
                 'order_name' => date('Y-m-d H:i:s') . '-订单',
                 'order_amount_total' => $params['order_amount_total'],
                 'order_amount_pay' => $params['order_amount_pay'],
+                'order_amount_paid' => $params['order_amount_pay'],
                 'order_category' => $orderCategory,
-                'order_is_complete' => $params['order_is_complete'] ?? 'N',
-                'order_status_system' => $params['order_status_system'],
-                'order_status_payment' => $params['order_status_payment'],
+                'order_is_complete' => intval($params['order_amount_pay']) >= 24000 ? 'Y' : 'N',
+                'order_status_system' => intval($params['order_amount_pay']) >= 24000 ? 'DONE' : 'PAYING',
+                'order_status_payment' => intval($params['order_amount_pay']) >= 24000 ? 'SUCCESS' : 'PENDING',
                 'order_status_storage' => $params['order_status_storage'],
                 'order_platform' => $params['order_platform'],
                 'order_remark' => $params['order_remark'] ?? '',
@@ -972,7 +952,9 @@ class KangyangCityController extends Curd{
                 ->where('pay_category', 'VIP')
                 ->where('pay_status', 'SUCCESS')
                 ->select('pay_id', 'pay_category', 'pay_prepayid', 'pay_paytimes', 'pay_status', 'pay_amount', 'pay_extend_json')
+                ->orderBy('pay_addtimes', 'DESC')
                 ->get();
+            $order->pay_amount_total = 0;
             foreach ($payDetails as &$payDetail) {
                 $categoryArray = explode('-', $payDetail->pay_prepayid);
                 if (isset($categoryArray[1])) {
@@ -984,6 +966,7 @@ class KangyangCityController extends Curd{
                     $payExtendJson = json_decode($payDetail->pay_extend_json, true);
                     $order->cancel_times = $payExtendJson['cancel_times'] ?? '';
                 }
+                $order->pay_amount_total += $payDetail->pay_amount;
             }
             $refund = OrderReturn::where('join_return_order_id', $orderId)
                 ->select('order_return_status', 'order_return_apply_datetime', 'order_return_apply_json', 'order_return_accept_datetime', 'order_return_refund_json', 'order_return_extend_json')
@@ -1083,4 +1066,28 @@ class KangyangCityController extends Curd{
             dump($e->getMessage());
         }
     }
+
+    /**
+     * @Desc 获取分期的订单
+     * @Author Gorden
+     * @Date 2024/9/10 15:14
+     *
+     * @param Request $request
+     * @return Response
+     */
+    public function getPaidOrder(Request $request)
+    {
+        $memberId = $request->get('member_id');
+        if (!$memberId){
+            return json_fail('参数异常');
+        }
+
+        $order = Order::where('join_order_member_id',$memberId)
+            ->where('order_category','VIP')
+            ->where('order_status_system','PAYING')
+            ->select('order_id','order_amount_total','order_amount_paid','order_amount_pay')
+            ->first();
+
+        return json_success('',$order);
+    }
 }

+ 4 - 0
app/event/order/KangyangCityEvent.php

@@ -30,6 +30,10 @@ class KangyangCityEvent
         Member::where('member_id', $params['member_id'])->update($memberUpdateData);
         // 康养城VIP账户
         $memberAccount = MemberAccount::where('join_account_member_id', $params['member_id'])->where('member_account_classify', 'VIP')->first();
+        // 分期付款
+        if (isset($params['order_amount_paid']) && $params['order_amount_paid'] > 0){
+            $params['order_amount_pay'] = $params['order_amount_paid'] + $params['order_amount_pay'];
+        }
         if (!$memberAccount) {
             $account = [
                 'join_account_member_id' => $params['member_id'],

+ 1 - 0
route/admin.php

@@ -946,6 +946,7 @@ Route::group('/admin', function () {
             Route::post('/add', [\app\admin\controller\order\KangyangCityController::class, 'insert']);
             Route::post('/pay', [\app\admin\controller\order\KangyangCityController::class, 'pay']);
             Route::get('/sheet', [\app\admin\controller\order\KangyangCityController::class, 'sheet']);
+            Route::get('/getPaidOrder', [\app\admin\controller\order\KangyangCityController::class, 'getPaidOrder']);
         })->middleware([
             \app\middleware\AdminAuthCheck::class
         ]);