AutoConfirmOrderJob.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace app\jobs\order;
  3. use app\modules\admin\models\MerchantForm;
  4. use yii\base\BaseObject;
  5. use yii\queue\JobInterface;
  6. use app\models\Order;
  7. use app\models\VideoShopOrderExt;
  8. use app\models\OrderDetail;
  9. use app\models\AgentGoodsInstallLog;
  10. use app\models\DeliveryInfo;
  11. use app\constants\OptionSetting;
  12. use app\models\Option;
  13. use app\models\LocalDeliveryCourier;
  14. use app\models\LocalDeliveryLog;
  15. use app\utils\OrderUtil;
  16. // 订单自动确认收货
  17. class AutoConfirmOrderJob extends BaseObject implements JobInterface
  18. {
  19. public $order_id;
  20. public $store_id;
  21. public function execute($queue)
  22. {
  23. $videoOrder = VideoShopOrderExt::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'order_id' => $this->order_id])->one();
  24. if ($videoOrder) {
  25. // 不处理视频号小店订单
  26. return;
  27. }
  28. $order = Order::findOne(['id' => $this->order_id, 'is_delete' => 0]);
  29. if (!$order) {
  30. return;
  31. }
  32. if (OrderUtil::isCancel($order)) {
  33. // 已取消,不处理
  34. return;
  35. }
  36. if (OrderUtil::isConfirm($order)) {
  37. // 已确认收货,不处理
  38. return;
  39. }
  40. //如果是代理配送 且上门安装,用户没有对商品确认收货则不可确认收货
  41. $order_detail = OrderDetail::findAll(['order_id' => $this->order_id]);
  42. $is_can_confirm = true;
  43. foreach ($order_detail as $detail) {
  44. $is_can_confirm_sub = ((bool)AgentGoodsInstallLog::findOne(['order_detail_id' => $detail->id, 'status' => [0, 1], 'is_need_install' => 1, 'order_type' => AgentGoodsInstallLog::ORDER_TYPE_NORMAL]));
  45. if ($is_can_confirm_sub) {
  46. $is_can_confirm = false;
  47. }
  48. }
  49. //如果是同城配送平台自配方式,判断骑手是否送达,如果未送达则不可以确认收货
  50. $delivery_info = DeliveryInfo::findOne(['order_no' => $order->order_no]);
  51. if ($delivery_info) {
  52. if ($delivery_info->local_status != DeliveryInfo::LOCAL_STATUS_CONFIRM) {
  53. $is_can_confirm = false;
  54. }
  55. }
  56. //如果存在可转单
  57. // $orderTransitInfo = \app\models\OrderTransit::find()->where(['order_id' => $this->order_id, "is_delete" => 0, 'status' => 1])->asArray()->all();
  58. // if ($orderTransitInfo && $is_can_confirm) {
  59. // $cloud_store_token = get_merchant_token(0, $this->store_id);
  60. // if ($cloud_store_token) {
  61. // try {
  62. // foreach ($orderTransitInfo as $val) {
  63. // $form = new MerchantForm();
  64. // $data = $form->mchPurchaseOrderConfirm(0, $this->store_id, $val['cloud_order_id']);
  65. // debug_log([
  66. // 'msg' => '自动收货返回信息',
  67. // 'data' => json_encode($data)
  68. // ], 'order_transit.log');
  69. // if ((int)$data['code'] !== 0) {
  70. // $is_can_confirm = false;
  71. // }
  72. // $order_t = \app\models\OrderTransit::findOne($val['id']);
  73. // $order_t->status = 2;
  74. // $order_t->confirm_time = time();
  75. // if (!$order_t->save()) {
  76. // $is_can_confirm = false;
  77. // debug_log([
  78. // 'msg' => '自动收货订单状态保存',
  79. // 'data' => $val->errors
  80. // ], 'order_transit.log');
  81. // }
  82. // }
  83. // } catch (\Exception $e) {
  84. // $is_can_confirm = false;
  85. // debug_log([
  86. // 'msg' => '自动收货订单状态错误',
  87. // 'data' => $e->getMessage()
  88. // ], 'order_transit.log');
  89. // }
  90. // } else {
  91. // $is_can_confirm = false;
  92. // debug_log([
  93. // 'msg' => '自动收货订单状态保存',
  94. // 'data' => 'token错误'
  95. // ], 'order_transit.log');
  96. // }
  97. // }
  98. if ($is_can_confirm) {
  99. $order->trade_status = Order::ORDER_FLOW_CONFIRM;
  100. $order->confirm_time = time();
  101. if ($order->pay_type == 2) {
  102. $order->is_pay = 1;
  103. }
  104. $order->save();
  105. $t = \Yii::$app->db->beginTransaction();
  106. try {
  107. if ($delivery_info->local_status == DeliveryInfo::LOCAL_STATUS_CONFIRM) {
  108. /* 骑手完成订单后发放收入 */
  109. $courier = LocalDeliveryCourier::findOne(['id' => $delivery_info->rider_id, 'is_delete' => 0, 'state' => 2]);
  110. //同城配送设置相关
  111. $store_id = 0;
  112. if (intval($delivery_info->is_store_delivery_type)) {
  113. $store_id = $delivery_info->store_id;
  114. }
  115. $values = Option::find()->where([
  116. 'store_id' => $store_id,
  117. 'group' => OptionSetting::LOCAL_DELIVERY_GROUP_NAME, 'name' => OptionSetting::LOCAL_DELIVERY_SETTING
  118. ])->select('value')->one();
  119. $local_setting = json_decode($values->value, true);
  120. if ($store_id > 0) {
  121. $local_setting['default_cost']['value'] = -1;
  122. }
  123. $amount = (float)$local_setting['default_cost']['value'] == -1 ? $delivery_info->fee : (float)$local_setting['default_cost']['value'];
  124. $log = LocalDeliveryLog::saveLog($courier->saas_user_id, $amount, 1, 1, $order->id, "骑手配送收入:" . $amount . "元");
  125. if (!$log) { //发放骑手佣金报错
  126. throw new \Exception('---- ORDER CONFIRM 订单完成发放骑手收入失败,order_id=' . $order->id . ' ----');
  127. }
  128. }
  129. $t->commit();
  130. } catch (\Exception $e) {
  131. \Yii::warning($e->getMessage());
  132. $t->rollBack(); //事务回滚
  133. }
  134. }
  135. }
  136. }