OrderRefund.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. namespace addons\StarChain\common\models;
  3. use addons\JuheShop\common\models\MallOrderRefundModel;
  4. use addons\StarChain\common\enums\OrderRefundEnum;
  5. use common\enums\RefundStatusEnum;
  6. use common\logic\order\OrderRefundLogic;
  7. use common\models\order\OrderDetail;
  8. use common\models\order\OrderRefund as OrderOrderRefund;
  9. use Exception;
  10. use Yii;
  11. use yii\helpers\Json;
  12. /**
  13. * This is the model class for table "{{%addons_star_chain_order_refund}}".
  14. *
  15. * @property int $id
  16. * @property int|null $mall_id
  17. * @property int|null $user_id 用户ID
  18. * @property int|null $order_id 订单ID
  19. * @property string|null $spu_id 供应链商品ID
  20. * @property string|null $sku_id 供应链skuID
  21. * @property int|null $num 申请数量
  22. * @property int|null $order_refund_id 售后ID
  23. * @property string|null $chain_refund_sn 供应链端售后订单编号
  24. * @property string|null $data 推送数据
  25. * @property int|null $status 状态
  26. * @property int|null $refund_status 供应链售后状态
  27. * @property string|null $error_msg
  28. * @property string|null $return_data 返回数据
  29. * @property int|null $created_at
  30. * @property int|null $updated_at
  31. */
  32. class OrderRefund extends BaseModel
  33. {
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function tableName()
  38. {
  39. return '{{%addons_star_chain_order_refund}}';
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function rules()
  45. {
  46. return [
  47. [['mall_id', 'user_id', 'order_id', 'num', 'order_refund_id', 'status', 'refund_status', 'created_at', 'updated_at'], 'integer'],
  48. [['data', 'error_msg', 'return_data'], 'string'],
  49. [['spu_id', 'sku_id', 'chain_refund_sn'], 'string', 'max' => 64],
  50. ];
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function attributeLabels()
  56. {
  57. return [
  58. 'id' => 'ID',
  59. 'mall_id' => 'Mall ID',
  60. 'user_id' => 'User ID',
  61. 'order_id' => 'Order ID',
  62. 'spu_id' => 'Spu ID',
  63. 'sku_id' => 'Sku ID',
  64. 'num' => 'Num',
  65. 'order_refund_id' => 'Order Refund ID',
  66. 'chain_refund_sn' => 'Chain Refund Sn',
  67. 'data' => 'Data',
  68. 'status' => 'Status',
  69. 'refund_status' => 'Refund Status',
  70. 'error_msg' => 'Error Msg',
  71. 'return_data' => 'Return Data',
  72. 'created_at' => 'Created At',
  73. 'updated_at' => 'Updated At',
  74. ];
  75. }
  76. /**
  77. * 获取商品详情
  78. *
  79. * @return ActiveQueryInterface
  80. */
  81. public function getGoods()
  82. {
  83. return $this->hasOne(Goods::class, ['spu_id' => 'spu_id']);
  84. }
  85. /**
  86. * 获取订单详情
  87. *
  88. * @return ActiveQueryInterface
  89. */
  90. public function getDetail()
  91. {
  92. return $this->hasOne(OrderDetail::class, ['order_id' => 'order_id']);
  93. }
  94. /**
  95. * 获取订单信息
  96. *
  97. * @return ActiveQueryInterface
  98. */
  99. public function getOrder()
  100. {
  101. return $this->hasOne(MallOrder::class, ['id' => 'order_id']);
  102. }
  103. public function getRefund()
  104. {
  105. return $this->hasOne(OrderOrderRefund::class, ['id' => 'order_refund_id']);
  106. }
  107. /**
  108. * 添加售后订单
  109. *
  110. * @param integer $user_id
  111. * @param integer $mall_id
  112. * @param integer $order_id
  113. * @param integer $order_detail_id
  114. * @param integer $order_refund_id
  115. * @param array $data
  116. * @return OrderRefund
  117. */
  118. public static function add($data)
  119. {
  120. $model = new self();
  121. $data['data'] = Json::encode($data['data']);
  122. $model->load($data, '');
  123. return $model->save() ? $model : false;
  124. }
  125. /**
  126. * 推送成功
  127. *
  128. * @param array $data
  129. * @return void
  130. */
  131. public function pushOk($data)
  132. {
  133. $this->status = OrderRefundEnum::PUSHED;
  134. $this->chain_refund_sn = $data['returnSn'];
  135. $this->return_data = Json::encode($data);
  136. $this->updated_at = time();
  137. $this->save();
  138. }
  139. /**
  140. * 审核中
  141. *
  142. * @return void
  143. */
  144. public function review()
  145. {
  146. $this->refund_status = 1;
  147. $this->save();
  148. }
  149. /**
  150. * 卖家拒绝退款
  151. *
  152. * @return void
  153. */
  154. public function refuseMoney()
  155. {
  156. $logic = new OrderRefundLogic();
  157. if ($logic->execute($this->mall_id, [
  158. 'id' => $this->order_refund_id,
  159. 'step_status' => RefundStatusEnum::STEP_NEGATIVE_1
  160. ])) {
  161. $this->refund_status = 2;
  162. $this->save();
  163. return true;
  164. }
  165. throw new Exception('卖家拒绝退款');
  166. }
  167. /**
  168. * 退款成功
  169. *
  170. * @return void
  171. */
  172. public function success()
  173. {
  174. $logic = new OrderRefundLogic();
  175. if ($logic->execute($this->mall_id, [
  176. 'id' => $this->order_refund_id,
  177. 'step_status' => RefundStatusEnum::STEP_6
  178. ])) {
  179. $this->refund_status = 3;
  180. $this->save();
  181. return true;
  182. }
  183. throw new Exception('退款成功');
  184. }
  185. /**
  186. * 卖家拒绝退货退款
  187. *
  188. * @return void
  189. */
  190. public function refuseMoneyAndProduct()
  191. {
  192. $logic = new OrderRefundLogic();
  193. if ($logic->execute($this->mall_id, [
  194. 'id' => $this->order_refund_id,
  195. 'step_status' => RefundStatusEnum::STEP_NEGATIVE_1
  196. ])) {
  197. $this->refund_status = 4;
  198. $this->save();
  199. return true;
  200. }
  201. throw new Exception('卖家拒绝退货退款');
  202. }
  203. /**
  204. * 待买家退货
  205. *
  206. * @param array $refund_detail
  207. * @return void
  208. */
  209. public function waitBuyerSend($refund_detail)
  210. {
  211. // 退货地址
  212. $address = [
  213. 'consignee' => $refund_detail['userName'],
  214. 'mobile' => $refund_detail['userPhone'],
  215. 'address' => $refund_detail['userAddress']
  216. ];
  217. $logic = new OrderRefundLogic();
  218. if ($res = $logic->execute($this->mall_id, [
  219. 'id' => $this->order_refund_id,
  220. 'address' => $address,
  221. 'step_status' => RefundStatusEnum::STEP_2
  222. ])) {
  223. $this->refund_status = 5;
  224. $this->save();
  225. return true;
  226. }
  227. throw new Exception('卖家拒绝退货退款');
  228. }
  229. /**
  230. * 买家已退货,待卖家收货
  231. *
  232. * @return void
  233. */
  234. public function waitSellerReceive()
  235. {
  236. $logic = new OrderRefundLogic();
  237. if ($logic->execute($this->mall_id, [
  238. 'id' => $this->order_refund_id,
  239. 'step_status' => RefundStatusEnum::STEP_3
  240. ])) {
  241. $this->refund_status = 6;
  242. $this->save();
  243. return true;
  244. }
  245. throw new Exception('卖家拒绝退货退款');
  246. }
  247. /**
  248. * 买家已退货,卖家拒绝收货
  249. *
  250. * @return void
  251. */
  252. public function sellerRefuseReceive()
  253. {
  254. $this->refund_status = 7;
  255. $this->save();
  256. }
  257. /**
  258. * 卖家已收货,待确认退款
  259. *
  260. * @return void
  261. */
  262. public function sellerReceiveOfWait()
  263. {
  264. $this->refund_status = 8;
  265. $this->save();
  266. }
  267. /**
  268. * 退货退款成功
  269. *
  270. * @return void
  271. */
  272. public function successMoneyAndProduct()
  273. {
  274. $logic = new OrderRefundLogic();
  275. if ($logic->execute($this->mall_id, [
  276. 'id' => $this->order_refund_id,
  277. 'step_status' => RefundStatusEnum::STEP_4
  278. ])) {
  279. $this->refund_status = 9;
  280. $this->save();
  281. return true;
  282. }
  283. throw new Exception('退货退款成功');
  284. }
  285. /**
  286. * 卖家拒绝换货
  287. *
  288. * @return void
  289. */
  290. public function refuseExchange()
  291. {
  292. $logic = new OrderRefundLogic();
  293. if ($logic->execute($this->mall_id, [
  294. 'id' => $this->order_refund_id,
  295. 'step_status' => RefundStatusEnum::STEP_NEGATIVE_1, // 拒绝售后
  296. ])) {
  297. $this->refund_status = 10;
  298. $this->save();
  299. return true;
  300. }
  301. throw new Exception('卖家拒绝退货退款');
  302. }
  303. /**
  304. * 买家已退货,待卖家换货
  305. *
  306. * @return void
  307. */
  308. public function waitSellerSend()
  309. {
  310. $logic = new OrderRefundLogic();
  311. if ($logic->execute($this->mall_id, [
  312. 'id' => $this->order_refund_id,
  313. 'step_status' => RefundStatusEnum::STEP_7
  314. ])) {
  315. $this->refund_status = 11;
  316. $this->save();
  317. return true;
  318. }
  319. throw new Exception('买家已退货,待卖家换货');
  320. }
  321. /**
  322. * 卖家已换货,待买家收货
  323. *
  324. * @return void
  325. */
  326. public function waitBuyerReceive($refund_detail)
  327. {
  328. $express = [
  329. 'saler_express' => $refund_detail['sendDeliveryName'],
  330. 'saler_express_no' => $refund_detail['sendDeliverySn'],
  331. 'customer_name' => $refund_detail['receiveName'],
  332. ];
  333. $logic = new OrderRefundLogic();
  334. if ($logic->execute($this->mall_id, [
  335. 'id' => $this->order_refund_id,
  336. 'step_status' => RefundStatusEnum::STEP_10,
  337. 'express' => $express,
  338. ])) {
  339. $this->refund_status = 12;
  340. $this->save();
  341. return true;
  342. }
  343. throw new Exception('卖家已换货,待买家收货');
  344. }
  345. /**
  346. * 换货成功
  347. *
  348. * @return void
  349. */
  350. public function successExchange()
  351. {
  352. $logic = new OrderRefundLogic();
  353. if ($logic->execute($this->mall_id, [
  354. 'id' => $this->order_refund_id,
  355. 'step_status' => RefundStatusEnum::STEP_11
  356. ])) {
  357. $this->refund_status = 13;
  358. $this->save();
  359. return true;
  360. }
  361. throw new Exception('换货成功');
  362. }
  363. /**
  364. * 已关闭
  365. *
  366. * @return void
  367. */
  368. public function close()
  369. {
  370. $this->refund_status = 14;
  371. $this->save();
  372. }
  373. /**
  374. * 卖家同意退款
  375. *
  376. * @return void
  377. */
  378. public function adoptMoney()
  379. {
  380. $this->refund_status = 15;
  381. $this->save();
  382. }
  383. /**
  384. * 卖家同意仅退款
  385. *
  386. * @return void
  387. */
  388. public function adoptOnlyMoney()
  389. {
  390. $this->refund_status = 16;
  391. $this->save();
  392. }
  393. /**
  394. * 卖家拒绝仅退款
  395. *
  396. * @return void
  397. */
  398. public function refuseOnlyMoney()
  399. {
  400. $logic = new OrderRefundLogic();
  401. if ($logic->execute($this->mall_id, [
  402. 'id' => $this->order_refund_id,
  403. 'step_status' => RefundStatusEnum::STEP_NEGATIVE_1
  404. ])) {
  405. $this->refund_status = 17;
  406. $this->save();
  407. return true;
  408. }
  409. throw new Exception('换货成功');
  410. }
  411. /**
  412. * 仅退款成功
  413. *
  414. * @return void
  415. */
  416. public function successOnlyMoney()
  417. {
  418. $logic = new OrderRefundLogic();
  419. if ($logic->execute($this->mall_id, [
  420. 'id' => $this->order_refund_id,
  421. 'step_status' => RefundStatusEnum::STEP_6
  422. ])) {
  423. $this->refund_status = 18;
  424. $this->save();
  425. return true;
  426. }
  427. throw new Exception('仅退款成功');
  428. }
  429. /**
  430. * 获取售后订单
  431. *
  432. * @param integer $mall_id
  433. * @param string $return_sn
  434. * @return static|null
  435. */
  436. public static function getOrderByReturnSn(int $mall_id, string $return_sn)
  437. {
  438. return static::find()->where(
  439. ['mall_id' => $mall_id, 'chain_refund_sn' => $return_sn]
  440. )->one();
  441. }
  442. }