StoreRefundOrder.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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\server;
  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 crmeb\basic\BaseController;
  16. use think\App;
  17. /**
  18. * Class StoreRefundOrder
  19. * app\controller\api\server
  20. * 移动客服 - 退款单
  21. */
  22. class StoreRefundOrder extends BaseController
  23. {
  24. protected $merId;
  25. protected $repository;
  26. protected $service_id;
  27. public function __construct(App $app, StoreRefundOrderRepository $repository)
  28. {
  29. parent::__construct($app);
  30. $this->repository = $repository;
  31. $this->merId = $this->request->route('merId');
  32. $this->service_id = $this->request->serviceInfo()->service_id;
  33. }
  34. /**
  35. * 获取可退款的商品信息
  36. * @param $id
  37. * @param StoreOrderRepository $storeOrderRepository
  38. * @param StoreOrderProductRepository $orderProductRepository
  39. * @return \think\response\Json
  40. * @author Qinii
  41. * @day 2023/7/13
  42. */
  43. public function check($id, StoreOrderRepository $storeOrderRepository, StoreOrderProductRepository $orderProductRepository)
  44. {
  45. $order = $storeOrderRepository->getSearch(['mer_id' => $this->merId])->where('order_id',$id)->find();
  46. if (!$order) return app('json')->fail('订单状态有误');
  47. if (!$order->refund_status) return app('json')->fail('订单已过退款/退货期限');
  48. if ($order->status < 0) return app('json')->fail('订单已退款');
  49. if ($order->status == 10) return app('json')->fail('订单不支持退款');
  50. $product = $orderProductRepository->userRefundProducts([],0,$id,0);
  51. $total_refund_price = $this->repository->getRefundsTotalPrice($order,$product,0);
  52. $activity_type = $order->activity_type;
  53. $status = (!$order->status || $order->status == 9) ? 0 : $order->status;
  54. $postage_price = 0;
  55. return app('json')->success(compact('activity_type', 'total_refund_price','postage_price', 'product', 'status'));
  56. }
  57. /**
  58. * 获取订单可退款金额
  59. * @param StoreOrderRepository $storeOrderRepository
  60. * @return \think\response\Json
  61. * @author Qinii
  62. */
  63. public function compute(StoreOrderRepository $storeOrderRepository)
  64. {
  65. $refund = $this->request->param('refund',[]);
  66. $orderId = $this->request->param('order_id',02);
  67. $order = $storeOrderRepository->getSearch(['mer_id' => $this->merId])->where('order_id',$orderId)->find();
  68. if (!$order) return app('json')->fail('订单状态有误');
  69. if (!$order->refund_status) return app('json')->fail('订单已过退款/退货期限');
  70. if ($order->status < 0) return app('json')->fail('订单已退款');
  71. if ($order->status == 10) return app('json')->fail('订单不支持退款');
  72. $totalRefundPrice = $this->repository->compute($order,$refund);
  73. return app('json')->success(compact('totalRefundPrice'));
  74. }
  75. /**
  76. * 创建退款单,并执行退款操作
  77. * @param StoreOrderRepository $storeOrderRepository
  78. * @return \think\response\Json
  79. * @author Qinii
  80. * @day 2023/7/13
  81. */
  82. public function create(StoreOrderRepository $storeOrderRepository)
  83. {
  84. $data = $this->request->params(['refund_message','refund_price','mer_mark']);
  85. $refund = $this->request->param('refund',[]);
  86. $orderId = $this->request->param('order_id',02);
  87. $order = $storeOrderRepository->getSearch(['mer_id' => $this->merId])->where('order_id',$orderId)->find();
  88. if (!$order) return app('json')->fail('订单状态有误');
  89. if (!$order->refund_status) return app('json')->fail('订单已过退款/退货期限');
  90. if ($order->status < 0) return app('json')->fail('订单已退款');
  91. if ($order->status == 10) return app('json')->fail('订单不支持退款');
  92. $data['refund_type'] = 1;
  93. $data['admin_id'] = $this->service_id;
  94. $data['user_type'] = 4;
  95. $refund = $this->repository->merRefund($order,$refund,$data);
  96. return app('json')->success('退款成功',['refund_order_id' => $refund->refund_order_id]);
  97. }
  98. /**
  99. * 列表
  100. * @return \think\response\Json
  101. * @author Qinii
  102. */
  103. public function lst()
  104. {
  105. [$page, $limit] = $this->getPage();
  106. $where = $this->request->params(['order_type','delivery_id']);
  107. $where['mer_id'] = $this->merId;
  108. return app('json')->success($this->repository->getListByService($where,$page,$limit));
  109. }
  110. /**
  111. * 详情
  112. * @param $id
  113. * @return \think\response\Json
  114. * @author Qinii
  115. */
  116. public function detail($id)
  117. {
  118. $data = $this->repository->getWhere([$this->repository->getPk() => $id,'mer_id' => $this->merId],'*',['order','refundProduct.product','user']);
  119. return app('json')->success($data);
  120. }
  121. /**
  122. * 退款详情
  123. * @param $id
  124. * @return \think\response\Json
  125. * @author Qinii
  126. */
  127. public function getRefundPrice($id)
  128. {
  129. return app('json')->success($this->repository->serverRefundDetail($id,$this->merId));
  130. }
  131. /**
  132. * 快递信息查询
  133. * @param $id
  134. * @return \think\response\Json
  135. * @author Qinii
  136. */
  137. public function express($id)
  138. {
  139. $data['refund'] = $this->repository->getWhere(['refund_order_id' => $id,'mer_id'=> $this->merId,'status' =>2],'*', ['refundProduct.product']);
  140. if(!$data['refund'])
  141. return app('json')->fail('订单信息或状态错误');
  142. $data['express'] = $this->repository->express($id);
  143. return app('json')->success($data);
  144. }
  145. /**
  146. * 审核
  147. * @param $id
  148. * @return \think\response\Json
  149. * @author Qinii
  150. */
  151. public function switchStatus($id)
  152. {
  153. if(!$this->repository->getStatusExists($this->merId,$id))
  154. return app('json')->fail('信息或状态错误');
  155. $status = ($this->request->param('status') == 1) ? 1 : -1;
  156. event('refund.status',compact('id','status'));
  157. if($status == 1){
  158. $data = $this->request->params(['mer_delivery_user','mer_delivery_address','phone']);
  159. if ($data['phone'] && isPhone($data['phone']))
  160. return app('json')->fail('请输入正确的手机号');
  161. $data['status'] = $status;
  162. $this->repository->agree($id,$data,$this->service_id);
  163. }else{
  164. $fail_message = $this->request->param('fail_message','');
  165. if($status == -1 && empty($fail_message))
  166. return app('json')->fail('未通过必须填写');
  167. $data['status'] = $status;
  168. $data['fail_message'] = $fail_message;
  169. $this->repository->refuse($id,$data, $this->service_id);
  170. }
  171. return app('json')->success('审核成功');
  172. }
  173. /**
  174. * - 用户退款退货
  175. * - 确认收货并退款
  176. * @param $id
  177. * @return \think\response\Json
  178. * @author Qinii
  179. */
  180. public function refundPrice($id)
  181. {
  182. if(!$this->repository->getRefundPriceExists($this->merId,$id)){
  183. return app('json')->fail('信息或状态错误');
  184. }
  185. $params = $this->request->params(['status','fail_message']);
  186. if (!$params['status']) {
  187. return app('json')->fail('请选择状态');
  188. }
  189. if ($params['status'] == -1 && empty($params['fail_message'])) {
  190. return app('json')->fail('拒绝原因必须填写');
  191. }
  192. $this->repository->adminRefund($id, $params, $this->service_id);
  193. return app('json')->success('审核成功');
  194. }
  195. /**
  196. * 备注
  197. * @param $id
  198. * @return \think\response\Json
  199. * @author Qinii
  200. */
  201. public function mark($id){
  202. if(!$this->repository->getExistsById($this->merId,$id))
  203. return app('json')->fail('数据不存在');
  204. $this->repository->update($id,['mer_mark' => $this->request->param('mer_mark','')]);
  205. return app('json')->success('备注成功');
  206. }
  207. }