RefundOrder.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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\merchant\store\order;
  12. use app\common\repositories\store\ExcelRepository;
  13. use app\common\repositories\store\order\MerchantReconciliationRepository;
  14. use app\common\repositories\store\order\StoreOrderProductRepository;
  15. use app\common\repositories\store\order\StoreOrderRepository;
  16. use app\common\repositories\store\order\StoreOrderStatusRepository;
  17. use app\common\repositories\store\order\StoreRefundStatusRepository;
  18. use crmeb\services\ExcelService;
  19. use think\App;
  20. use crmeb\basic\BaseController;
  21. use app\common\repositories\store\order\StoreRefundOrderRepository as repository;
  22. use think\exception\ValidateException;
  23. class RefundOrder extends BaseController
  24. {
  25. /**
  26. * @var repository
  27. */
  28. protected $repository;
  29. /**
  30. * Order constructor.
  31. * @param App $app
  32. * @param repository $repository
  33. */
  34. public function __construct(App $app, repository $repository)
  35. {
  36. parent::__construct($app);
  37. $this->repository = $repository;
  38. }
  39. /**
  40. * 获取可退款的商品信息
  41. * @param $id
  42. * @param StoreOrderRepository $storeOrderRepository
  43. * @param StoreOrderProductRepository $orderProductRepository
  44. * @return \think\response\Json
  45. * @author Qinii
  46. * @day 2023/7/13
  47. */
  48. public function check($id, StoreOrderRepository $storeOrderRepository, StoreOrderProductRepository $orderProductRepository)
  49. {
  50. $order = $storeOrderRepository->getSearch(['mer_id' => $this->request->merId()])->where('order_id',$id)->find();
  51. if (!$order) return app('json')->fail('订单状态有误');
  52. // if (!$order->refund_status) return app('json')->fail('订单已过退款/退货期限');
  53. if ($order->status < 0) return app('json')->fail('订单已退款');
  54. if ($order->status == 10) return app('json')->fail('订单不支持退款');
  55. $product = $orderProductRepository->userRefundProducts([],0,$id,0);
  56. $total_refund_price = $this->repository->getRefundsTotalPrice($order,$product,0);
  57. $activity_type = $order->activity_type;
  58. $status = (!$order->status || $order->status == 9) ? 0 : $order->status;
  59. $postage_price = 0;
  60. return app('json')->success(compact('activity_type', 'total_refund_price','postage_price', 'product', 'status'));
  61. }
  62. /**
  63. * 计算退款金额
  64. *
  65. * @param StoreOrderRepository $storeOrderRepository 订单仓库
  66. * @return \think\response\Json
  67. */
  68. public function compute(StoreOrderRepository $storeOrderRepository)
  69. {
  70. $refund = $this->request->param('refund', []);
  71. $orderId = $this->request->param('order_id', 0);
  72. $order = $storeOrderRepository->getSearch(['mer_id' => $this->request->merId()])->where('order_id', $orderId)->find();
  73. // 判断订单是否存在
  74. if (!$order) return app('json')->fail('订单状态有误');
  75. // if (!$order->refund_status) return app('json')->fail('订单已过退款/退货期限');
  76. // 判断订单是否可退款
  77. if ($order->status < 0) return app('json')->fail('订单已退款');
  78. if ($order->status == 10) return app('json')->fail('订单不支持退款');
  79. $totalRefundPrice = $this->repository->compute($order, $refund);
  80. // 返回结果
  81. return app('json')->success(compact('totalRefundPrice'));
  82. }
  83. /**
  84. * 创建退款单,并执行退款操作
  85. * @param StoreOrderRepository $storeOrderRepository
  86. * @return \think\response\Json
  87. * @author Qinii
  88. * @day 2023/7/13
  89. */
  90. public function create(StoreOrderRepository $storeOrderRepository)
  91. {
  92. $data = $this->request->params(['refund_message','refund_price','mer_mark']);
  93. $refund = $this->request->param('refund',[]);
  94. $orderId = $this->request->param('order_id',02);
  95. $order = $storeOrderRepository->getSearch(['mer_id' => $this->request->merId()])->where('order_id',$orderId)->find();
  96. if (!$order) return app('json')->fail('订单状态有误');
  97. // if (!$order->refund_status) return app('json')->fail('订单已过退款/退货期限');
  98. if ($order->status < 0) return app('json')->fail('订单已退款');
  99. if ($order->status == 10) return app('json')->fail('订单不支持退款');
  100. $data['refund_type'] = 1;
  101. $data['admin_id'] = $this->request->adminId();
  102. $data['user_type'] = $this->request->userType();
  103. $refund = $this->repository->merRefund($order,$refund,$data);
  104. return app('json')->success('退款成功',['refund_order_id' => $refund->refund_order_id]);
  105. }
  106. /**
  107. * 获取列表
  108. *
  109. * @return \think\response\Json
  110. * @author Qinii
  111. * @day 2020-06-12
  112. */
  113. public function lst()
  114. {
  115. // 获取分页参数
  116. list($page, $limit) = $this->getPage();
  117. // 获取查询条件
  118. $where = $this->request->params(['refund_order_sn', 'status', 'refund_type', 'date', 'order_sn', 'id', 'delivery_id', 'user_type', 'username','uid','nickname','real_name','phone']);
  119. // 添加商家ID条件
  120. $where['mer_id'] = $this->request->merId();
  121. // 调用仓库获取列表并返回JSON格式数据
  122. return app('json')->success($this->repository->getList($where, $page, $limit));
  123. }
  124. /**
  125. * 获取详情
  126. *
  127. * @param int $id 订单ID
  128. * @return \think\response\Json
  129. * @author Qinii
  130. * @day 2020-06-12
  131. */
  132. public function detail($id)
  133. {
  134. // 判断订单是否存在
  135. if (!$this->repository->getExistsById($this->request->merId(), $id))
  136. return app('json')->fail('数据不存在');
  137. // 调用仓库获取订单详情并返回JSON格式数据
  138. return app('json')->success($this->repository->getOne($id));
  139. }
  140. /**
  141. * 切换审核状态
  142. *
  143. * @param int $id 审核ID
  144. * @return \think\response\Json
  145. */
  146. public function switchStatus($id)
  147. {
  148. if (!$this->repository->getStatusExists($this->request->merId(), $id))
  149. return app('json')->fail('信息或状态错误');
  150. // 获取审核状态
  151. $status = ($this->request->param('status') == 1) ? 1 : -1;
  152. event('refund.status', compact('id', 'status'));
  153. if ($status == 1) {
  154. $data = $this->request->params(['mer_delivery_user', 'mer_delivery_address', 'phone']);
  155. if ($data['phone'] && isPhone($data['phone']))
  156. return app('json')->fail('请输入正确的手机号');
  157. // 设置审核状态和拒绝原因并保存数据
  158. $data['status'] = $status;
  159. $this->repository->agree($id, $data);
  160. } else {
  161. $fail_message = $this->request->param('fail_message', '');
  162. if ($status == -1 && empty($fail_message))
  163. return app('json')->fail('未通过必须填写');
  164. // 设置审核状态和拒绝原因并保存数据
  165. $data['status'] = $status;
  166. $data['fail_message'] = $fail_message;
  167. $this->repository->refuse($id, $data);
  168. }
  169. // 返回成功信息
  170. return app('json')->success('审核成功');
  171. }
  172. /**
  173. * 收货后确定退款
  174. * @param $id
  175. * @return mixed
  176. * @author Qinii
  177. * @day 2020-06-12
  178. */
  179. public function refundPrice($id)
  180. {
  181. if(!$this->repository->getRefundPriceExists($this->request->merId(),$id)) {
  182. return app('json')->fail('信息或状态错误');
  183. }
  184. $params = $this->request->params(['status','fail_message']);
  185. if (!$params['status']) {
  186. return app('json')->fail('请选择状态');
  187. }
  188. if ($params['status'] == -1 && empty($params['fail_message'])) {
  189. return app('json')->fail('拒绝原因必须填写');
  190. }
  191. $this->repository->adminRefund($id, $params);
  192. return app('json')->success('审核成功');
  193. }
  194. /**
  195. * 切换状态表单
  196. * @param int $id 订单ID
  197. * @return \think\response\Json
  198. */
  199. public function switchStatusForm($id)
  200. {
  201. if (!$this->repository->getStatusExists($this->request->merId(), $id))
  202. return app('json')->fail('信息或状态错误');
  203. // 返回订单状态表单数据
  204. return app('json')->success(formToData($this->repository->statusForm($id)));
  205. }
  206. /**
  207. * 删除订单
  208. * @param int $id 订单ID
  209. * @return \think\response\Json
  210. */
  211. public function delete($id)
  212. {
  213. if (!$this->repository->getUserDelExists($this->request->merId(), $id))
  214. return app('json')->fail('信息或状态错误');
  215. $this->repository->update($id, ['is_system_del' => 1]);
  216. // 返回删除成功信息
  217. return app('json')->success('删除成功');
  218. }
  219. /**
  220. * 标记表单
  221. * @param int $id 订单ID
  222. * @return \think\response\Json
  223. */
  224. public function markForm($id)
  225. {
  226. if (!$this->repository->getExistsById($this->request->merId(), $id))
  227. return app('json')->fail('数据不存在');
  228. // 返回订单标记表单数据
  229. return app('json')->success(formToData($this->repository->markForm($id)));
  230. }
  231. /**
  232. * 标记订单
  233. * @param int $id 订单ID
  234. * @return \think\response\Json
  235. */
  236. public function mark($id)
  237. {
  238. if (!$this->repository->getExistsById($this->request->merId(), $id))
  239. return app('json')->fail('数据不存在');
  240. $this->repository->update($id, ['mer_mark' => $this->request->param('mer_mark', '')]);
  241. // 返回标记成功信息
  242. return app('json')->success('备注成功');
  243. }
  244. /**
  245. * 查看订单日志
  246. * @param int $id 订单ID
  247. * @return \think\response\Json
  248. */
  249. public function log($id)
  250. {
  251. list($page, $limit) = $this->getPage();
  252. $where = $this->request->params(['date', 'user_type']);
  253. $where['id'] = $id;
  254. $where['type'] = StoreOrderStatusRepository::TYPE_REFUND;
  255. $data = app()->make(StoreOrderStatusRepository::class)->search($where, $page, $limit);
  256. return app('json')->success($data);
  257. }
  258. /**
  259. * 重新获取对账单列表
  260. *
  261. * @param int $id 对账单ID
  262. * @return \think\response\Json
  263. */
  264. public function reList($id)
  265. {
  266. // 获取分页参数
  267. [$page, $limit] = $this->getPage();
  268. // 实例化商家对账单仓库
  269. $make = app()->make(MerchantReconciliationRepository::class);
  270. // 判断对账单是否存在
  271. if (!$make->getWhereCount(['mer_id' => $this->request->merId(), 'reconciliation_id' => $id]))
  272. return app('json')->fail('数据不存在');
  273. // 构造查询条件
  274. $where = ['reconciliation_id' => $id, 'type' => 1];
  275. // 调用仓库方法获取对账单列表并返回结果
  276. return app('json')->success($this->repository->reconList($where, $page, $limit));
  277. }
  278. /**
  279. * 快递查询
  280. * @param $id
  281. * @return mixed
  282. * @author Qinii
  283. * @day 2020-06-25
  284. */
  285. public function express($id)
  286. {
  287. return app('json')->success($this->repository->express($id));
  288. }
  289. /**
  290. * 创建 Excel 文件
  291. *
  292. * @return \think\response\Json
  293. */
  294. public function createExcel()
  295. {
  296. $where = $this->request->params(['refund_order_sn', 'status', 'refund_type', 'date', 'order_sn', 'id']);
  297. // 添加商家 ID 条件
  298. $where['mer_id'] = $this->request->merId();
  299. // 获取分页参数
  300. [$page, $limit] = $this->getPage();
  301. // 调用 ExcelService 类的 refundOrder 方法获取数据
  302. $data = app()->make(ExcelService::class)->refundOrder($where, $page, $limit);
  303. // 返回 JSON 格式的数据
  304. return app('json')->success($data);
  305. }
  306. }