| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace app\jobs\order;
- use app\modules\admin\models\MerchantForm;
- use yii\base\BaseObject;
- use yii\queue\JobInterface;
- use app\models\Order;
- use app\models\VideoShopOrderExt;
- use app\models\OrderDetail;
- use app\models\AgentGoodsInstallLog;
- use app\models\DeliveryInfo;
- use app\constants\OptionSetting;
- use app\models\Option;
- use app\models\LocalDeliveryCourier;
- use app\models\LocalDeliveryLog;
- use app\utils\OrderUtil;
- // 订单自动确认收货
- class AutoConfirmOrderJob extends BaseObject implements JobInterface
- {
- public $order_id;
- public $store_id;
- public function execute($queue)
- {
- $videoOrder = VideoShopOrderExt::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'order_id' => $this->order_id])->one();
- if ($videoOrder) {
- // 不处理视频号小店订单
- return;
- }
- $order = Order::findOne(['id' => $this->order_id, 'is_delete' => 0]);
- if (!$order) {
- return;
- }
- if (OrderUtil::isCancel($order)) {
- // 已取消,不处理
- return;
- }
- if (OrderUtil::isConfirm($order)) {
- // 已确认收货,不处理
- return;
- }
- //如果是代理配送 且上门安装,用户没有对商品确认收货则不可确认收货
- $order_detail = OrderDetail::findAll(['order_id' => $this->order_id]);
- $is_can_confirm = true;
- foreach ($order_detail as $detail) {
- $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]));
- if ($is_can_confirm_sub) {
- $is_can_confirm = false;
- }
- }
- //如果是同城配送平台自配方式,判断骑手是否送达,如果未送达则不可以确认收货
- $delivery_info = DeliveryInfo::findOne(['order_no' => $order->order_no]);
- if ($delivery_info) {
- if ($delivery_info->local_status != DeliveryInfo::LOCAL_STATUS_CONFIRM) {
- $is_can_confirm = false;
- }
- }
- //如果存在可转单
- // $orderTransitInfo = \app\models\OrderTransit::find()->where(['order_id' => $this->order_id, "is_delete" => 0, 'status' => 1])->asArray()->all();
- // if ($orderTransitInfo && $is_can_confirm) {
- // $cloud_store_token = get_merchant_token(0, $this->store_id);
- // if ($cloud_store_token) {
- // try {
- // foreach ($orderTransitInfo as $val) {
- // $form = new MerchantForm();
- // $data = $form->mchPurchaseOrderConfirm(0, $this->store_id, $val['cloud_order_id']);
- // debug_log([
- // 'msg' => '自动收货返回信息',
- // 'data' => json_encode($data)
- // ], 'order_transit.log');
- // if ((int)$data['code'] !== 0) {
- // $is_can_confirm = false;
- // }
- // $order_t = \app\models\OrderTransit::findOne($val['id']);
- // $order_t->status = 2;
- // $order_t->confirm_time = time();
- // if (!$order_t->save()) {
- // $is_can_confirm = false;
- // debug_log([
- // 'msg' => '自动收货订单状态保存',
- // 'data' => $val->errors
- // ], 'order_transit.log');
- // }
- // }
- // } catch (\Exception $e) {
- // $is_can_confirm = false;
- // debug_log([
- // 'msg' => '自动收货订单状态错误',
- // 'data' => $e->getMessage()
- // ], 'order_transit.log');
- // }
- // } else {
- // $is_can_confirm = false;
- // debug_log([
- // 'msg' => '自动收货订单状态保存',
- // 'data' => 'token错误'
- // ], 'order_transit.log');
- // }
- // }
- if ($is_can_confirm) {
- $order->trade_status = Order::ORDER_FLOW_CONFIRM;
- $order->confirm_time = time();
- if ($order->pay_type == 2) {
- $order->is_pay = 1;
- }
- $order->save();
- $t = \Yii::$app->db->beginTransaction();
- try {
- if ($delivery_info->local_status == DeliveryInfo::LOCAL_STATUS_CONFIRM) {
- /* 骑手完成订单后发放收入 */
- $courier = LocalDeliveryCourier::findOne(['id' => $delivery_info->rider_id, 'is_delete' => 0, 'state' => 2]);
- //同城配送设置相关
- $store_id = 0;
- if (intval($delivery_info->is_store_delivery_type)) {
- $store_id = $delivery_info->store_id;
- }
- $values = Option::find()->where([
- 'store_id' => $store_id,
- 'group' => OptionSetting::LOCAL_DELIVERY_GROUP_NAME, 'name' => OptionSetting::LOCAL_DELIVERY_SETTING
- ])->select('value')->one();
- $local_setting = json_decode($values->value, true);
- if ($store_id > 0) {
- $local_setting['default_cost']['value'] = -1;
- }
- $amount = (float)$local_setting['default_cost']['value'] == -1 ? $delivery_info->fee : (float)$local_setting['default_cost']['value'];
- $log = LocalDeliveryLog::saveLog($courier->saas_user_id, $amount, 1, 1, $order->id, "骑手配送收入:" . $amount . "元");
- if (!$log) { //发放骑手佣金报错
- throw new \Exception('---- ORDER CONFIRM 订单完成发放骑手收入失败,order_id=' . $order->id . ' ----');
- }
- }
- $t->commit();
- } catch (\Exception $e) {
- \Yii::warning($e->getMessage());
- $t->rollBack(); //事务回滚
- }
- }
- }
- }
|