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(); //事务回滚 } } } }