OrderRefundSendForm.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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\AgentFrontBind;
  9. use app\models\AgentFrontCentralizeGoods;
  10. use app\models\AgentFrontCentralizeGoodsRefundLog;
  11. use app\models\Order;
  12. use app\models\OrderDetail;
  13. use app\models\OrderRefund;
  14. use app\models\OrderTransit;
  15. use app\modules\admin\models\MerchantForm;
  16. use yii\base\Model;
  17. use yii\helpers\Json;
  18. class OrderRefundSendForm extends Model
  19. {
  20. public $order_refund_id;
  21. public $user_id;
  22. public $express;
  23. public $express_no;
  24. public $orderType;
  25. public $form_id;
  26. public $send_type;//发货方式:0快递 1上门取货 2退回门店
  27. public $latitude;
  28. public $longitude;
  29. public $name;
  30. public $mobile;
  31. public $province_id;
  32. public $city_id;
  33. public $district_id;
  34. public $address;
  35. public $main_goods_pic;
  36. public $other_goods_pic;
  37. public $back_md_pic;
  38. public function rules()
  39. {
  40. return [
  41. [['express', 'express_no', 'orderType', 'form_id'], 'trim'],
  42. [['order_refund_id'], 'required'],
  43. [['express', 'express_no'], 'string'],
  44. [['send_type', 'name', 'mobile', 'main_goods_pic', 'other_goods_pic', 'back_md_pic', 'province_id', 'city_id', 'district_id', 'address'], 'safe'],
  45. [['latitude', 'longitude'], 'number']
  46. ];
  47. }
  48. public function attributeLabels()
  49. {
  50. return [
  51. 'express' => '快递公司',
  52. 'express_no' => '快递单号',
  53. ];
  54. }
  55. public function save()
  56. {
  57. if (!$this->validate()) {
  58. return [
  59. 'code' => 1,
  60. 'msg' => $this->getErrorSummary(false)[0]
  61. ];
  62. }
  63. $send_type = intval($this->send_type);
  64. $query = OrderRefund::find();
  65. $order_refund = $query->where([
  66. 'id' => $this->order_refund_id,
  67. 'user_id' => $this->user_id,
  68. ])->one();
  69. if (!$order_refund) {
  70. return [
  71. 'code' => 1,
  72. 'msg' => '售后订单不存在。',
  73. ];
  74. }
  75. $order = Order::findOne($order_refund->order_id);
  76. $md_id = 0;
  77. if ($order) {
  78. $md_id = $order->md_id;
  79. }
  80. $agentFrontBind = AgentFrontBind::findOne(['md_id' => $md_id, 'is_delete' => 0]);
  81. $goods_list = OrderDetail::find()->where(['order_id' => $order_refund->order_id])->andWhere(['in', 'id', Json::decode($order_refund->order_detail_id)])
  82. ->select('attr, goods_info, num')->asArray()->all();
  83. $is_front_centralize = 0;
  84. $centralize_goods_id_arr = [];
  85. $goods_num_info = [];
  86. foreach ($goods_list as &$goods) {
  87. $goods_info = json_decode($goods['goods_info'], true);
  88. // if (intval($goods_info['is_front_centralize']) && $send_type) {
  89. //
  90. // }
  91. if (intval($goods_info['is_front_centralize'])) {
  92. $is_front_centralize = 1;
  93. if (!$send_type) {
  94. return [
  95. 'code' => 1,
  96. 'msg' => '部分商品售后发货仅支持退回门店以及上门取送。',
  97. ];
  98. }
  99. $detail_attr = json_decode($goods['attr'], true) ?? [];
  100. $detail_attr_id = array_column($detail_attr, 'attr_id');
  101. // var_dump($goods['attr']);die;
  102. sort($detail_attr_id);
  103. $goods_attr = json_decode($goods_info['attr'], true);
  104. $goods_no = '';
  105. foreach ($goods_attr as $goods_attr_item) {
  106. $goods_attr_id = array_column($goods_attr_item['attr_list'], 'attr_id');
  107. sort($goods_attr_id);
  108. if (!array_diff($goods_attr_id, $detail_attr_id)) {
  109. $goods_no = $goods_attr_item['no'];
  110. }
  111. }
  112. $agentFrontCentralizeGoods = AgentFrontCentralizeGoods::findOne([
  113. 'front_admin_id' => $agentFrontBind->front_agent_admin_id,
  114. 'goods_no' => $goods_no,
  115. 'is_delete' => 0
  116. ]);
  117. if ($agentFrontCentralizeGoods) {
  118. $goods_num_info[] = [
  119. 'num' => $goods['num'],
  120. 'centralize_goods_id' => $agentFrontCentralizeGoods->id,
  121. ];
  122. array_push($centralize_goods_id_arr, $agentFrontCentralizeGoods->id);
  123. }
  124. }
  125. if (in_array($send_type, [1, 2]) && !intval($goods_info['is_front_centralize'])) {
  126. return [
  127. 'code' => 1,
  128. 'msg' => '部分商品售后发货仅支持快递。',
  129. ];
  130. }
  131. }
  132. if ($is_front_centralize) {
  133. $goodsRefundLog = AgentFrontCentralizeGoodsRefundLog::findOne(['refund_id' => $order_refund->id]) ?: new AgentFrontCentralizeGoodsRefundLog();
  134. $goodsRefundLog->refund_type = $send_type;
  135. $goodsRefundLog->centralize_goods_id = implode(',', $centralize_goods_id_arr ?? []);
  136. $goodsRefundLog->md_id = $md_id;
  137. $goodsRefundLog->store_id = $order->store_id;
  138. $goodsRefundLog->refund_id = $order_refund->id;
  139. $goodsRefundLog->goods_num_info = json_encode($goods_num_info);
  140. if ($send_type == AgentFrontCentralizeGoodsRefundLog::REFUND_TYPE_VISIT) {
  141. if (empty($this->name) ||
  142. empty($this->mobile) || empty($this->province_id) || empty($this->city_id) || empty($this->district_id)
  143. || empty($this->address)
  144. // || empty($this->latitude) || empty($this->longitude)
  145. ) {
  146. return [
  147. 'code' => 1,
  148. 'msg' => '上门取货请填写寄件人信息。',
  149. ];
  150. }
  151. $goodsRefundLog->latitude = $this->latitude ?? 0;
  152. $goodsRefundLog->longitude = $this->longitude ?? 0;
  153. $goodsRefundLog->name = $this->name;
  154. $goodsRefundLog->mobile = $this->mobile;
  155. $goodsRefundLog->province_id = $this->province_id;
  156. $goodsRefundLog->city_id = $this->city_id;
  157. $goodsRefundLog->district_id = $this->district_id;
  158. $goodsRefundLog->address = $this->address;
  159. $this->express = "仓库集采-上门取货";
  160. $this->express_no = "-";
  161. }
  162. if ($send_type == AgentFrontCentralizeGoodsRefundLog::REFUND_TYPE_BACK_MD) {
  163. // if (empty($this->back_md_pic)) {
  164. // return [
  165. // 'code' => 1,
  166. // 'msg' => '退回门店请上传 送回门店照片。',
  167. // ];
  168. // }
  169. $goodsRefundLog->back_md_pic = $this->back_md_pic;
  170. $this->express = "仓库集采-退回门店";
  171. $this->express_no = "-";
  172. }
  173. if (!$goodsRefundLog->save()) {
  174. return [
  175. 'code' => 1,
  176. 'msg' => implode(';', array_values($order->firstErrors))
  177. ];
  178. };
  179. }
  180. $order_refund->is_user_send = 1;
  181. $order_refund->user_send_express = $this->express;
  182. $order_refund->user_send_express_no = $this->express_no;
  183. $orderTransit = OrderTransit::findOne(['order_id' => $order_refund->order_id, 'is_delete' => 0, 'cancel_examine' => 1]);
  184. if ($orderTransit) {
  185. $cloud_order_id = $orderTransit->cloud_order_id;
  186. $merchant = new MerchantForm();
  187. $merchant->order_id = $cloud_order_id;
  188. $merchant->user_send_express = $this->express;
  189. $merchant->user_send_express_no = $this->express_no;
  190. $merchant->order_type = 0;
  191. $merchant->pic_list = [];
  192. $r = $merchant->mchOrderRefundSend($order_refund->order_detail_id);
  193. if ($r['code']) {
  194. return $r;
  195. }
  196. }
  197. if ($order_refund->save()) {
  198. return [
  199. 'code' => 0,
  200. 'msg' => '发货成功。',
  201. ];
  202. } else {
  203. return [
  204. 'code' => 1,
  205. 'msg' => $order_refund->errors[0]
  206. ];
  207. }
  208. }
  209. }