| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace console\controllers;
- use bizGhs\order\classes\OrderClass;
- use bizGhs\order\models\Order;
- use bizGhs\order\services\OrderService;
- use common\components\dict;
- use common\components\noticeUtil;
- use common\components\util;
- use Yii;
- use yii\console\Controller;
- use bizHd\purchase\classes\PurchaseClass;
- use bizHd\purchase\services\PurchaseService;
- /**
- * 订单相关
- */
- class OrderController extends Controller
- {
- //待配送订单,超过24小时自动完成 ./yii order/auto-finish
- public function actionAutoFinish()
- {
- //好像没有用了
- util::stop();
- $remain = time() - 86400;
- $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_SEND])->andWhere(['<=', 'addTime', $date]);
- $query->orderBy('deadline 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;
- $orderSn = $orderInfo['orderSn'] ?? '';
- $status = $orderInfo['status'] ?? OrderClass::ORDER_STATUS_UN_PAY;
- $addTime = $orderInfo['addTime'] ?? '';
- $prefix = "订单{$orderSn},ID{$id},创建于{$addTime},状态是{$status}";
- if ($status != OrderClass::ORDER_STATUS_UN_SEND) {
- noticeUtil::push($prefix . " 自动完成操作时,状态不对", '15280215347');
- continue;
- }
- if (empty($addTime) && $addTime > $date) {
- noticeUtil::push($prefix . " 自动完成操作时,时间不对", '15280215347');
- continue;
- }
- try {
- $unSendOrder = OrderClass::getById($id, true);
- OrderClass::selfSend($unSendOrder);
- $hasSendOrder = OrderClass::getById($id, true);
- OrderService::reach($hasSendOrder);
- $transaction->commit();
- //noticeUtil::push($prefix . " 【已自动完成】 SUCCESS", '15280215347');
- } catch (\Exception $e) {
- $transaction->rollBack();
- $msg = $e->getMessage();
- noticeUtil::push("销售单自动完成报错了!订单号:{$orderSn},错误信息:{$msg}", '15280215347');
- }
- }
- }
- }
- //商家自己开的订单,3小时后没确认自动确认为欠款 ./yii order/auto-confirm
- public function actionAutoConfirm()
- {
- //好像没有用了
- util::stop();
- $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');
- }
- }
- }
- }
- }
|