| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\models\AgentGoodsInstallLog;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\WorkerOrderExt;
- use app\modules\admin\models\MerchantForm;
- use yii\base\Model;
- use app\models\OrderTransit;
- use app\models\StoreCloud;
- use app\constants\OptionSetting;
- use app\models\DeliveryInfo;
- use app\models\LocalDeliveryCourier;
- use app\models\LocalDeliveryLog;
- use app\models\Option;
- class OrderConfirmForm extends Model
- {
- public $store_id;
- public $user_id;
- public $order_id;
- public function rules()
- {
- return [
- [['order_id'], 'required'],
- ];
- }
- public function save()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $order = Order::findOne([
- 'store_id' => $this->store_id,
- 'id' => $this->order_id,
- 'is_delete' => 0,
- ]);
- $notAllow = !in_array($this->user_id, [$order->user_id, $order->giving_gifts_received_user_id]);
- if (!$order || $notAllow) {
- return [
- 'code' => 1,
- 'msg' => '订单不存在'
- ];
- }
- if (intval($order->order_type) !== 6) {
- if (intval($order->trade_status) !== Order::ORDER_FLOW_SEND) {
- return [
- 'code' => 1,
- 'msg' => '订单不存在'
- ];
- }
- //如果是代理配送 且上门安装,用户没有对商品确认收货则不可确认收货
- $order_detail = OrderDetail::findAll(['order_id' => $order->id]);
- foreach ($order_detail as $detail) {
- $is_can_confirm = ((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) {
- return [
- 'code' => 1,
- 'msg' => '当前有商品需要上门安装,不可优先确认收货'
- ];
- }
- }
- }
- $order->trade_status = Order::ORDER_FLOW_CONFIRM;
- $order->confirm_time = time();
- // if ($order->pay_type == 2) {
- // $order->is_pay = 1;
- // $order->pay_time = time();
- // }
- if ($order->pay_type == 2 && intval($order->is_pay) === 0) {
- return [
- 'code' => 1,
- 'msg' => '订单未支付,不可操作'
- ];
- }
- if ($order->save()) {
- if (intval($order->order_type) !== 6) {
- // //如果存在可转单
- // $orderTransitInfo = OrderTransit::find()->where(['order_id' => $this->order_id, "is_delete" => 0, 'status' => 1])->all();
- // if($orderTransitInfo){
- // // $cloud_store_token = $this->getCloudStoreToken($this->store_id);
- // $cloud_store_token = get_merchant_token(0, $this->store_id);
- // //$cloudInfo = StoreCloud::find()->where(['store_id'=>$this->store_id,"is_delete"=>0])->one();
- // if($cloud_store_token) {
- // foreach($orderTransitInfo as $val){
- // $form = new MerchantForm();
- // $data = $form->mchPurchaseOrderConfirm(0, $this->store_id, $val->attributes['cloud_order_id']);
- // debug_log(json_encode($data));
- // if ((int)$data['code'] !== 0) {
- // throw new \Exception($data['msg']);
- // }
- // $val->status = 2;
- // $val->confirm_time = time();
- // $val->save();
- // if ($val->errors) {
- // throw new \Exception(json_encode($val->errors));
- // }
- // }
- // } else {
- // throw new \Exception("token信息错误");
- // }
- // }
- /* 2023年7月21日15:39:30 begin 同城配送方式为平台自配的订单,相关处理 */
- $delivery_info = DeliveryInfo::findOne(['order_no' => $order->order_no]);
- $courier = LocalDeliveryCourier::findOne(['id' => $delivery_info->rider_id, 'is_delete' => 0, 'state' => 2]);
- if($delivery_info->is_local == DeliveryInfo::IS_LOCAL_YSE && !empty($courier)) {
- if($delivery_info->local_status == DeliveryInfo::LOCAL_STATUS_CONFIRM) {
- /* 骑手完成订单后发放收入 */
- //同城配送设置相关
- $store_id = 0;
- if (intval($delivery_info->is_store_delivery_type)) {
- $store_id = get_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 (!empty($courier)) {
- 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) {
- $t->rollBack(); //事务回滚
- throw new \Exception("发放骑手佣金报错");
- }
- }
- } else {
- return [
- 'code' => 1,
- 'msg' => '当前订单为平台自配,配送员未送达不可优先确认收货'
- ];
- }
- }
- /* end */
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '已确认收货',
- ];
- } else {
- throw new \Exception('确认收货失败');
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //获取平台商户token
- // private function getCloudStoreToken($store_id){
- // $cache = \Yii::$app->cache->get('cloud_mch_cache_token_' . $store_id);
- // if ($cache) {
- // $params = json_decode($cache, true);
- // //2小时刷新token
- // if (time() - $params['time'] < (2 * 3600) ) {
- // return $params['access_token'];
- // }
- // }
- // $storeCloud = StoreCloud::find()->where(['store_id' => $store_id,'is_delete'=>0])->one();
- // if (!$storeCloud) {
- // return '';
- // }
- // $response = $this->getToken($storeCloud->name, $storeCloud->password);
- // if ($response['code'] == 0 && $response['taken']) {
- // $data = [];
- // $data['name'] = $storeCloud->name;
- // $data['pwd'] = $storeCloud->password;
- // $data['access_token'] = $response['taken'];
- // $data['time'] = time();
- // \Yii::$app->cache->set('cloud_mch_cache_token_' . $store_id, json_encode($data), 3 * 24 * 3600);
- // return $response['taken'];
- // }
- // return '';
- // }
- //获取token方法
- // private function getToken($name,$password) {
- // $url = "/user/getToken";
- // $data = [];
- // $data['name'] = $name;
- // $data['pwd'] = $password;
- // $response = cloud_post($this->domain.$url,$data);
- // //\Yii::error('token', $response);
- // return json_decode($response,true);
- // }
- }
|