Sfoglia il codice sorgente

自动确认功能

shish 5 anni fa
parent
commit
e983429237
1 ha cambiato i file con 77 aggiunte e 0 eliminazioni
  1. 77 0
      console/controllers/OrderController.php

+ 77 - 0
console/controllers/OrderController.php

@@ -9,6 +9,8 @@ use common\components\dict;
 use common\components\noticeUtil;
 use Yii;
 use yii\console\Controller;
+use bizHd\purchase\classes\PurchaseClass;
+use bizHd\purchase\services\PurchaseService;
 
 /**
  * 订单相关
@@ -73,4 +75,79 @@ class OrderController extends Controller
 
     }
 
+    //商家自己开的订单,3小时后没确认自动确认为欠款 ./yii order/auto-confirm
+    public function actionAutoConfirm()
+    {
+        $remain = time() - 10800;
+        $date = date("Y-m-d H:i:s", $remain);
+
+        $query = new \yii\db\Query();
+        $query->from(Order::tableName());
+
+        $query->where(['status' => OrderClass::ORDER_STATUS_UN_PAY])->andWhere(['<=', 'addTime', $date]);
+        $query->orderBy('addTime ASC');
+
+        //注意哪个平台的行为,低层很多方法会使用到
+        Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
+
+        //如果有报错,抛出异常
+        Yii::$app->params['errorReport'] = 1;
+
+        //24小时后的自动确认,不需要微信通知
+        Yii::$app->params['noNeedWxNotice'] = 1;
+
+        foreach ($query->batch() as $orderInfoList) {
+
+            foreach ($orderInfoList as $orderInfo) {
+                $connection = Yii::$app->db;//事务处理
+                $transaction = $connection->beginTransaction();
+                $id = $orderInfo['id'] ?? 0;
+                try {
+
+                    $order = OrderClass::getLockById($id);
+                    $orderId = $order->id ?? '';
+                    $shopAdminId = $order->shopAdminId ?? 0;
+                    if(empty($shopAdminId)){
+                        noticeUtil::push($prefix . " 自动确认失败,不是员工开单 orderId:{$orderId}", '15280215347');
+                        continue;
+                    }
+
+                    $purchaseId = $order->purchaseId ?? 0;
+
+                    $purchase = PurchaseClass::getById($purchaseId, true);
+                    if (empty($purchase)) {
+                        noticeUtil::push($prefix . " 自动确认失败,没有找到采购单 orderId:{$orderId}", '15280215347');
+                        continue;
+                    }
+
+                    $payWay = dict::getDict('payWay', 'debtPay');
+
+                    //不需要打印
+                    $order->needPrint = dict::getDict('needPrint', 'noNeed');
+                    $order->save();
+
+                    //支付
+                    PurchaseService::payAfter($purchase, $payWay);
+                    OrderService::payAfter($order, $payWay);
+
+                    //自己送
+                    $info = OrderClass::getById($id, true);
+                    OrderClass::selfSend($info);
+
+                    //确认送达,并且不需要微信通知
+                    Yii::$app->params['noNeedWxNotice'] = 1;
+                    $info = OrderClass::getById($id, true);
+                    OrderService::reach($info);
+
+                    $transaction->commit();
+                    noticeUtil::push($prefix . " 【已自动确认】 SUCCESS", '15280215347');
+                } catch (\Exception $e) {
+                    $transaction->rollBack();
+                    $msg = $e->getMessage();
+                    noticeUtil::push("自动确认订单报错!订单号:{$orderSn},错误信息:{$msg}", '15280215347');
+                }
+            }
+        }
+    }
+
 }