StoreRefundOrder.php 8.2 KB

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