OrderConfirmForm.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\models\AgentGoodsInstallLog;
  9. use app\models\Order;
  10. use app\models\OrderDetail;
  11. use app\models\WorkerOrderExt;
  12. use app\modules\admin\models\MerchantForm;
  13. use yii\base\Model;
  14. use app\models\OrderTransit;
  15. use app\models\StoreCloud;
  16. use app\constants\OptionSetting;
  17. use app\models\DeliveryInfo;
  18. use app\models\LocalDeliveryCourier;
  19. use app\models\LocalDeliveryLog;
  20. use app\models\Option;
  21. class OrderConfirmForm extends Model
  22. {
  23. public $store_id;
  24. public $user_id;
  25. public $order_id;
  26. public function rules()
  27. {
  28. return [
  29. [['order_id'], 'required'],
  30. ];
  31. }
  32. public function save()
  33. {
  34. $t = \Yii::$app->db->beginTransaction();
  35. try {
  36. if (!$this->validate()) {
  37. return $this->getErrorSummary(false)[0];
  38. }
  39. $order = Order::findOne([
  40. 'store_id' => $this->store_id,
  41. 'id' => $this->order_id,
  42. 'is_delete' => 0,
  43. ]);
  44. $notAllow = !in_array($this->user_id, [$order->user_id, $order->giving_gifts_received_user_id]);
  45. if (!$order || $notAllow) {
  46. return [
  47. 'code' => 1,
  48. 'msg' => '订单不存在'
  49. ];
  50. }
  51. if (intval($order->order_type) !== 6) {
  52. if (intval($order->trade_status) !== Order::ORDER_FLOW_SEND) {
  53. return [
  54. 'code' => 1,
  55. 'msg' => '订单不存在'
  56. ];
  57. }
  58. //如果是代理配送 且上门安装,用户没有对商品确认收货则不可确认收货
  59. $order_detail = OrderDetail::findAll(['order_id' => $order->id]);
  60. foreach ($order_detail as $detail) {
  61. $is_can_confirm = ((bool)AgentGoodsInstallLog::findOne(['order_detail_id' => $detail->id, 'status' => [0, 1], 'is_need_install' => 1, 'order_type' => AgentGoodsInstallLog::ORDER_TYPE_NORMAL]));
  62. if ($is_can_confirm) {
  63. return [
  64. 'code' => 1,
  65. 'msg' => '当前有商品需要上门安装,不可优先确认收货'
  66. ];
  67. }
  68. }
  69. }
  70. $order->trade_status = Order::ORDER_FLOW_CONFIRM;
  71. $order->confirm_time = time();
  72. // if ($order->pay_type == 2) {
  73. // $order->is_pay = 1;
  74. // $order->pay_time = time();
  75. // }
  76. if ($order->pay_type == 2 && intval($order->is_pay) === 0) {
  77. return [
  78. 'code' => 1,
  79. 'msg' => '订单未支付,不可操作'
  80. ];
  81. }
  82. if ($order->save()) {
  83. if (intval($order->order_type) !== 6) {
  84. // //如果存在可转单
  85. // $orderTransitInfo = OrderTransit::find()->where(['order_id' => $this->order_id, "is_delete" => 0, 'status' => 1])->all();
  86. // if($orderTransitInfo){
  87. // // $cloud_store_token = $this->getCloudStoreToken($this->store_id);
  88. // $cloud_store_token = get_merchant_token(0, $this->store_id);
  89. // //$cloudInfo = StoreCloud::find()->where(['store_id'=>$this->store_id,"is_delete"=>0])->one();
  90. // if($cloud_store_token) {
  91. // foreach($orderTransitInfo as $val){
  92. // $form = new MerchantForm();
  93. // $data = $form->mchPurchaseOrderConfirm(0, $this->store_id, $val->attributes['cloud_order_id']);
  94. // debug_log(json_encode($data));
  95. // if ((int)$data['code'] !== 0) {
  96. // throw new \Exception($data['msg']);
  97. // }
  98. // $val->status = 2;
  99. // $val->confirm_time = time();
  100. // $val->save();
  101. // if ($val->errors) {
  102. // throw new \Exception(json_encode($val->errors));
  103. // }
  104. // }
  105. // } else {
  106. // throw new \Exception("token信息错误");
  107. // }
  108. // }
  109. /* 2023年7月21日15:39:30 begin 同城配送方式为平台自配的订单,相关处理 */
  110. $delivery_info = DeliveryInfo::findOne(['order_no' => $order->order_no]);
  111. $courier = LocalDeliveryCourier::findOne(['id' => $delivery_info->rider_id, 'is_delete' => 0, 'state' => 2]);
  112. if($delivery_info->is_local == DeliveryInfo::IS_LOCAL_YSE && !empty($courier)) {
  113. if($delivery_info->local_status == DeliveryInfo::LOCAL_STATUS_CONFIRM) {
  114. /* 骑手完成订单后发放收入 */
  115. //同城配送设置相关
  116. $store_id = 0;
  117. if (intval($delivery_info->is_store_delivery_type)) {
  118. $store_id = get_store_id();
  119. }
  120. $values = Option::find()->where([
  121. 'store_id' => $store_id,
  122. 'group' => OptionSetting::LOCAL_DELIVERY_GROUP_NAME, 'name' => OptionSetting::LOCAL_DELIVERY_SETTING
  123. ])->select('value')->one();
  124. $local_setting = json_decode($values->value, true);
  125. if (!empty($courier)) {
  126. if ($store_id > 0) {
  127. $local_setting['default_cost']['value'] = -1;
  128. }
  129. $amount = (float)$local_setting['default_cost']['value'] == -1 ? $delivery_info->fee : (float)$local_setting['default_cost']['value'];
  130. $log = LocalDeliveryLog::saveLog($courier->saas_user_id, $amount, 1, 1, $order->id, "骑手配送收入:" . $amount . "元");
  131. if (!$log) {
  132. $t->rollBack(); //事务回滚
  133. throw new \Exception("发放骑手佣金报错");
  134. }
  135. }
  136. } else {
  137. return [
  138. 'code' => 1,
  139. 'msg' => '当前订单为平台自配,配送员未送达不可优先确认收货'
  140. ];
  141. }
  142. }
  143. /* end */
  144. }
  145. $t->commit();
  146. return [
  147. 'code' => 0,
  148. 'msg' => '已确认收货',
  149. ];
  150. } else {
  151. throw new \Exception('确认收货失败');
  152. }
  153. } catch (\Exception $e) {
  154. $t->rollBack();
  155. return [
  156. 'code' => 1,
  157. 'msg' => $e->getMessage()
  158. ];
  159. }
  160. }
  161. //获取平台商户token
  162. // private function getCloudStoreToken($store_id){
  163. // $cache = \Yii::$app->cache->get('cloud_mch_cache_token_' . $store_id);
  164. // if ($cache) {
  165. // $params = json_decode($cache, true);
  166. // //2小时刷新token
  167. // if (time() - $params['time'] < (2 * 3600) ) {
  168. // return $params['access_token'];
  169. // }
  170. // }
  171. // $storeCloud = StoreCloud::find()->where(['store_id' => $store_id,'is_delete'=>0])->one();
  172. // if (!$storeCloud) {
  173. // return '';
  174. // }
  175. // $response = $this->getToken($storeCloud->name, $storeCloud->password);
  176. // if ($response['code'] == 0 && $response['taken']) {
  177. // $data = [];
  178. // $data['name'] = $storeCloud->name;
  179. // $data['pwd'] = $storeCloud->password;
  180. // $data['access_token'] = $response['taken'];
  181. // $data['time'] = time();
  182. // \Yii::$app->cache->set('cloud_mch_cache_token_' . $store_id, json_encode($data), 3 * 24 * 3600);
  183. // return $response['taken'];
  184. // }
  185. // return '';
  186. // }
  187. //获取token方法
  188. // private function getToken($name,$password) {
  189. // $url = "/user/getToken";
  190. // $data = [];
  191. // $data['name'] = $name;
  192. // $data['pwd'] = $password;
  193. // $response = cloud_post($this->domain.$url,$data);
  194. // //\Yii::error('token', $response);
  195. // return json_decode($response,true);
  196. // }
  197. }