OrderController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace console\controllers;
  3. use bizGhs\order\classes\OrderClass;
  4. use bizGhs\order\models\Order;
  5. use bizGhs\order\services\OrderService;
  6. use common\components\dict;
  7. use common\components\noticeUtil;
  8. use Yii;
  9. use yii\console\Controller;
  10. use bizHd\purchase\classes\PurchaseClass;
  11. use bizHd\purchase\services\PurchaseService;
  12. /**
  13. * 订单相关
  14. */
  15. class OrderController extends Controller
  16. {
  17. //待配送订单,超过24小时自动完成 ./yii order/auto-finish
  18. public function actionAutoFinish()
  19. {
  20. $remain = time() - 86400;
  21. $date = date("Y-m-d H:i:s", $remain);
  22. $query = new \yii\db\Query();
  23. $query->from(Order::tableName());
  24. $query->where(['status' => OrderClass::ORDER_STATUS_UN_SEND])->andWhere(['<=', 'addTime', $date]);
  25. $query->orderBy('deadline ASC');
  26. //注意哪个平台的行为,低层很多方法会使用到
  27. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  28. //如果有报错,抛出异常
  29. Yii::$app->params['errorReport'] = 1;
  30. //24小时后的自动确认,不需要微信通知
  31. Yii::$app->params['noNeedWxNotice'] = 1;
  32. foreach ($query->batch() as $orderInfoList) {
  33. foreach ($orderInfoList as $orderInfo) {
  34. $connection = Yii::$app->db;//事务处理
  35. $transaction = $connection->beginTransaction();
  36. $id = $orderInfo['id'] ?? 0;
  37. $orderSn = $orderInfo['orderSn'] ?? '';
  38. $status = $orderInfo['status'] ?? OrderClass::ORDER_STATUS_UN_PAY;
  39. $addTime = $orderInfo['addTime'] ?? '';
  40. $prefix = "订单{$orderSn},ID{$id},创建于{$addTime},状态是{$status}";
  41. if ($status != OrderClass::ORDER_STATUS_UN_SEND) {
  42. noticeUtil::push($prefix . " 自动完成操作时,状态不对", '15280215347');
  43. continue;
  44. }
  45. if (empty($addTime) && $addTime > $date) {
  46. noticeUtil::push($prefix . " 自动完成操作时,时间不对", '15280215347');
  47. continue;
  48. }
  49. try {
  50. $unSendOrder = OrderClass::getById($id, true);
  51. OrderClass::selfSend($unSendOrder);
  52. $hasSendOrder = OrderClass::getById($id, true);
  53. OrderService::reach($hasSendOrder);
  54. $transaction->commit();
  55. //noticeUtil::push($prefix . " 【已自动完成】 SUCCESS", '15280215347');
  56. } catch (\Exception $e) {
  57. $transaction->rollBack();
  58. $msg = $e->getMessage();
  59. noticeUtil::push("销售单自动完成报错了!订单号:{$orderSn},错误信息:{$msg}", '15280215347');
  60. }
  61. }
  62. }
  63. }
  64. //商家自己开的订单,3小时后没确认自动确认为欠款 ./yii order/auto-confirm
  65. public function actionAutoConfirm()
  66. {
  67. $remain = time() - 10800;
  68. $date = date("Y-m-d H:i:s", $remain);
  69. $query = new \yii\db\Query();
  70. $query->from(Order::tableName());
  71. $query->where(['status' => OrderClass::ORDER_STATUS_UN_PAY])->andWhere(['<=', 'addTime', $date]);
  72. $query->orderBy('addTime ASC');
  73. //注意哪个平台的行为,低层很多方法会使用到
  74. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  75. //如果有报错,抛出异常
  76. Yii::$app->params['errorReport'] = 1;
  77. //24小时后的自动确认,不需要微信通知
  78. Yii::$app->params['noNeedWxNotice'] = 1;
  79. foreach ($query->batch() as $orderInfoList) {
  80. foreach ($orderInfoList as $orderInfo) {
  81. $connection = Yii::$app->db;//事务处理
  82. $transaction = $connection->beginTransaction();
  83. $id = $orderInfo['id'] ?? 0;
  84. try {
  85. $order = OrderClass::getLockById($id);
  86. $orderId = $order->id ?? '';
  87. $shopAdminId = $order->shopAdminId ?? 0;
  88. if(empty($shopAdminId)){
  89. noticeUtil::push($prefix . " 自动确认失败,不是员工开单 orderId:{$orderId}", '15280215347');
  90. continue;
  91. }
  92. $purchaseId = $order->purchaseId ?? 0;
  93. $purchase = PurchaseClass::getById($purchaseId, true);
  94. if (empty($purchase)) {
  95. noticeUtil::push($prefix . " 自动确认失败,没有找到采购单 orderId:{$orderId}", '15280215347');
  96. continue;
  97. }
  98. $payWay = dict::getDict('payWay', 'debtPay');
  99. //不需要打印
  100. $order->needPrint = dict::getDict('needPrint', 'noNeed');
  101. $order->save();
  102. //支付
  103. PurchaseService::payAfter($purchase, $payWay);
  104. OrderService::payAfter($order, $payWay);
  105. //自己送
  106. $info = OrderClass::getById($id, true);
  107. OrderClass::selfSend($info);
  108. //确认送达,并且不需要微信通知
  109. Yii::$app->params['noNeedWxNotice'] = 1;
  110. $info = OrderClass::getById($id, true);
  111. OrderService::reach($info);
  112. $transaction->commit();
  113. noticeUtil::push($prefix . " 【已自动确认】 SUCCESS", '15280215347');
  114. } catch (\Exception $e) {
  115. $transaction->rollBack();
  116. $msg = $e->getMessage();
  117. noticeUtil::push("自动确认订单报错!订单号:{$orderSn},错误信息:{$msg}", '15280215347');
  118. }
  119. }
  120. }
  121. }
  122. }