StoreRefundOrder.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\store\order;
  12. use app\common\repositories\store\order\StoreOrderProductRepository;
  13. use app\common\repositories\store\order\StoreOrderRepository;
  14. use app\common\repositories\store\order\StoreOrderStatusRepository;
  15. use app\common\repositories\store\order\StoreRefundOrderRepository;
  16. use app\common\repositories\store\shipping\ExpressRepository;
  17. use app\validate\api\BackGoodsValidate;
  18. use app\validate\api\StoreRefundOrderValidate;
  19. use crmeb\basic\BaseController;
  20. use think\App;
  21. /**
  22. * Class StoreRefundOrder
  23. * @package app\controller\api\store\order
  24. * @author xaboy
  25. * @day 2020/6/12
  26. */
  27. class StoreRefundOrder extends BaseController
  28. {
  29. /**
  30. * @var StoreRefundOrderRepository
  31. */
  32. protected $repository;
  33. /**
  34. * StoreRefundOrder constructor.
  35. * @param App $app
  36. * @param StoreRefundOrderRepository $repository
  37. */
  38. public function __construct(App $app, StoreRefundOrderRepository $repository)
  39. {
  40. parent::__construct($app);
  41. $this->repository = $repository;
  42. }
  43. /**
  44. * @param $id
  45. * @param StoreOrderRepository $orderRepository
  46. * @return mixed
  47. * @author xaboy
  48. * @day 2020/6/12
  49. */
  50. public function batchProduct($id, StoreOrderRepository $orderRepository)
  51. {
  52. return app('json')->success($orderRepository->refundProduct($id, $this->request->uid()));
  53. }
  54. /**
  55. * @param $id
  56. * @param StoreOrderProductRepository $orderProductRepository
  57. * @param StoreOrderRepository $storeOrderRepository
  58. * @return \think\response\Json
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. * @author xaboy
  63. * @day 2020/9/2
  64. */
  65. public function product($id, StoreOrderProductRepository $orderProductRepository, StoreOrderRepository $storeOrderRepository)
  66. {
  67. $_id = (string)$this->request->param('ids', '');
  68. $type = (string)$this->request->param('type', '');
  69. $ids = explode(',', $_id);
  70. if (!$_id || !count($ids))
  71. return app('json')->fail('请选择退款商品');
  72. $uid = $this->request->uid();
  73. $order = $storeOrderRepository->userOrder(intval($id), $uid);
  74. if (!$order)
  75. return app('json')->fail('订单状态有误');
  76. if (!$order->refund_status)
  77. return app('json')->fail('订单已过退款/退货期限');
  78. if ($order->status < 0) return app('json')->fail('订单已退款');
  79. if ($order->status == 10) return app('json')->fail('订单不支持退款');
  80. $product = $orderProductRepository->userRefundProducts($ids, $uid, intval($id));
  81. if (!$product)
  82. return app('json')->fail('商品不存在或已退款');
  83. if (count($product) != count($ids))
  84. return app('json')->fail('请选择正确的退款商品');
  85. if ($type == 2) {
  86. $total_refund_price = $this->repository->getRefundsTotalPrice($order, $product);
  87. $postage_price = 0;
  88. } else {
  89. $data = $this->repository->getRefundTotalPrice($order, $product);
  90. $total_refund_price = (float)$data['total_refund_price'];
  91. $postage_price = (float)$data['postage_price'];
  92. }
  93. if ($order->status == 11) {
  94. $total_refund_price = $order->pay_price;
  95. }
  96. $status = (!$order->status || $order->status == 9) ? 0 : $order->status;
  97. $activity_type = $order->activity_type;
  98. return app('json')->success(compact('activity_type', 'total_refund_price', 'product', 'postage_price', 'status'));
  99. }
  100. /**
  101. * @param $id
  102. * @param StoreRefundOrderValidate $validate
  103. * @param StoreOrderRepository $orderRepository
  104. * @return mixed
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\DbException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. * @author xaboy
  109. * @day 2020/6/12
  110. */
  111. public function refund($id, StoreRefundOrderValidate $validate, StoreOrderRepository $orderRepository)
  112. {
  113. $data = $this->request->params(['type', 'refund_type', 'refund_price', 'num', 'ids', 'refund_message', 'mark', 'pics']);
  114. $validate->check($data);
  115. $ids = explode(',', $data['ids']);
  116. $type = $data['type'];
  117. $num = $data['num'];
  118. unset($data['num'], $data['ids'], $data['type']);
  119. if ($type == 1 && count($ids) > 1)
  120. return app('json')->fail('请选择正确的退款商品');
  121. $uid = $this->request->uid();
  122. $order = $orderRepository->userOrder($id, $uid);
  123. if (!$order) return app('json')->fail('订单状态错误');
  124. if (!$order->refund_status)
  125. return app('json')->fail('订单已过退款/退货期限');
  126. if ($order->status < 0) return app('json')->fail('订单已退款');
  127. if ($order->status == 10) return app('json')->fail('订单不支持退款');
  128. if ($order->is_virtual && $data['refund_type'] == 2) return app('json')->fail('订单不支持退款退货');
  129. if ($type == 1) {
  130. $refund = $this->repository->refund($order, (int)$ids[0], $num, $uid, $data);
  131. } else {
  132. $refund = $this->repository->refunds($order, $ids, $uid, $data);
  133. }
  134. return app('json')->success('申请退款成功', ['refund_order_id' => $refund->refund_order_id]);
  135. }
  136. /**
  137. * @return mixed
  138. * @throws \think\db\exception\DataNotFoundException
  139. * @throws \think\db\exception\DbException
  140. * @throws \think\db\exception\ModelNotFoundException
  141. * @author xaboy
  142. * @day 2020/6/12
  143. */
  144. public function lst()
  145. {
  146. $type = $this->request->param('type');
  147. [$page, $limit] = $this->getPage();
  148. return app('json')->success($this->repository->userList([
  149. 'type' => $type,
  150. 'uid' => $this->request->uid(),
  151. 'is_del' => 0,
  152. ], $page, $limit));
  153. }
  154. /**
  155. * @param $id
  156. * @return mixed
  157. * @throws \think\db\exception\DataNotFoundException
  158. * @throws \think\db\exception\DbException
  159. * @throws \think\db\exception\ModelNotFoundException
  160. * @author xaboy
  161. * @day 2020/6/12
  162. */
  163. public function detail($id, StoreOrderStatusRepository $orderStatusRepository)
  164. {
  165. $refund = $this->repository->userDetail(intval($id), $this->request->uid());
  166. if (!$refund)
  167. return app('json')->fail('退款单不存在');
  168. $daysToAdd = (int)systemConfig('platform_intervention_days');
  169. $intervention = $orderStatusRepository->existsWhere(['order_id' => $id, 'change_type' => [$orderStatusRepository::CHANGE_REFUND_PLATFORM_AGREE, $orderStatusRepository::CHANGE_REFUND_PLATFORM_REFUSE]]);
  170. $refund->platform_msg = isset($refund->platform) ? $refund->platform['change_message'] : '';
  171. $refund->platform = systemConfig('platform_intervention') && $refund->status == -1 && time() <= strtotime("{$refund->status_time} +{$daysToAdd} days") && !$intervention;
  172. return app('json')->success($refund->toArray());
  173. }
  174. /**
  175. * @param $id
  176. * @return mixed
  177. * @throws \think\db\exception\DbException
  178. * @author xaboy
  179. * @day 2020/6/12
  180. */
  181. public function del($id)
  182. {
  183. $this->repository->userDel(intval($id), $this->request->uid());
  184. return app('json')->success('删除成功');
  185. }
  186. public function back_goods($id, BackGoodsValidate $validate, ExpressRepository $expressRepository)
  187. {
  188. $data = $this->request->params(['delivery_type', 'delivery_id', 'delivery_phone', 'delivery_mark', 'delivery_pics']);
  189. $validate->check($data);
  190. if (!$expressRepository->merFieldExists('name', $data['delivery_type'], null, null, true))
  191. return app('json')->fail('不支持该快递公司');
  192. $this->repository->backGoods($this->request->uid(), $id, $data);
  193. return app('json')->success('提交成功');
  194. }
  195. public function express($id)
  196. {
  197. if (!$refund = $this->repository->getWhere(['status' => 2, 'refund_order_id' => $id]))
  198. return app('json')->fail('退款单不存在');
  199. $express = $this->repository->express($id);
  200. return app('json')->success(compact('refund', 'express'));
  201. }
  202. public function cancel($id)
  203. {
  204. $this->repository->cancel($id, $this->request->userInfo());
  205. return app('json')->success('取消成功');
  206. }
  207. /**
  208. * 用户申请平台介入
  209. * @param $refund_order_id
  210. * @return \think\response\Json
  211. * @throws \think\db\exception\DataNotFoundException
  212. * @throws \think\db\exception\DbException
  213. * @throws \think\db\exception\ModelNotFoundException
  214. */
  215. public function platformIntervene($id)
  216. {
  217. $this->repository->applyPlatformIntervene($id, $this->request->uid());
  218. return app('json')->success('申请成功');
  219. }
  220. }