RefundOrder.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace app\controller\admin\v1\supplier;
  3. use app\controller\admin\AuthController;
  4. use app\Request;
  5. use app\services\order\StoreOrderRefundServices;
  6. use app\services\order\StoreOrderServices;
  7. use app\services\user\UserServices;
  8. use think\facade\App;
  9. class RefundOrder extends AuthController
  10. {
  11. /**
  12. * RefundOrder constructor.
  13. * @param App $app
  14. * @param StoreOrderRefundServices $service
  15. */
  16. public function __construct(App $app, StoreOrderRefundServices $service)
  17. {
  18. parent::__construct($app);
  19. $this->services = $service;
  20. }
  21. /**
  22. * 退款订单列表
  23. * @return mixed
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\DbException
  26. * @throws \think\db\exception\ModelNotFoundException
  27. */
  28. public function getRefundList()
  29. {
  30. $where = $this->request->getMore([
  31. ['order_id', ''],
  32. ['time', ''],
  33. ['refund_type', 0],
  34. ['supplier_id', '']
  35. ]);
  36. $where['supplier'] = 1;
  37. return $this->success($this->services->refundList($where, ['user', 'supplier']));
  38. }
  39. /**
  40. * 退款表单生成
  41. * @param $id
  42. * @return mixed
  43. * @throws \FormBuilder\Exception\FormBuilderException
  44. */
  45. public function refund($id)
  46. {
  47. if (!$id) {
  48. return app('json')->fail('Data does not exist!');
  49. }
  50. return app('json')->success($this->services->refundOrderForm((int)$id));
  51. }
  52. /**
  53. * 订单退款
  54. * @param Request $request
  55. * @param StoreOrderServices $services
  56. * @param $id
  57. * @return mixed
  58. */
  59. public function update_refund(Request $request, StoreOrderServices $services, $id)
  60. {
  61. $data = $request->postMore([
  62. ['refund_price', 0],
  63. ['type', 1],
  64. ['refuse_reason', '']
  65. ]);
  66. if (!$id) {
  67. return app('json')->fail('Data does not exist!');
  68. }
  69. $orderRefund = $this->services->get($id);
  70. if (!$orderRefund) {
  71. return app('json')->fail('Data does not exist!');
  72. }
  73. if ($orderRefund['is_cancel'] == 1) {
  74. return app('json')->fail('用户已取消申请');
  75. }
  76. $order = $services->get((int)$orderRefund['store_order_id']);
  77. if (!$order) {
  78. return app('json')->fail('Data does not exist!');
  79. }
  80. if (!in_array($orderRefund['refund_type'], [1, 2, 5])) {
  81. return app('json')->fail('售后订单状态不支持该操作');
  82. }
  83. if ($data['type'] == 1) {
  84. $data['refund_type'] = 6;
  85. } else if ($data['type'] == 2) {
  86. $data['refund_type'] = 3;
  87. }
  88. $data['refunded_time'] = time();
  89. $type = $data['type'];
  90. //拒绝退款
  91. if ($type == 2) {
  92. $this->services->refuseRefund((int)$id, $data, $orderRefund);
  93. return app('json')->successful('修改退款状态成功!');
  94. } else {
  95. //0元退款
  96. if ($orderRefund['refund_price'] == 0) {
  97. $refund_price = 0;
  98. } else {
  99. if (!$data['refund_price']) {
  100. return app('json')->fail('请输入退款金额');
  101. }
  102. if ($orderRefund['refund_price'] == $orderRefund['refunded_price']) {
  103. return app('json')->fail('已退完支付金额!不能再退款了');
  104. }
  105. $refund_price = $data['refund_price'];
  106. $data['refunded_price'] = bcadd($data['refund_price'], $orderRefund['refunded_price'], 2);
  107. $bj = bccomp((string)$orderRefund['refund_price'], (string)$data['refunded_price'], 2);
  108. if ($bj < 0) {
  109. return app('json')->fail('退款金额大于支付金额,请修改退款金额');
  110. }
  111. }
  112. unset($data['type']);
  113. $refund_data['pay_price'] = $order['pay_price'];
  114. $refund_data['refund_price'] = $refund_price;
  115. if ($order['refund_price'] > 0) {
  116. mt_srand();
  117. $refund_data['refund_id'] = $order['order_id'] . rand(100, 999);
  118. }
  119. //修改订单退款状态
  120. unset($data['refund_price']);
  121. if ($this->services->agreeRefund($id, $refund_data)) {
  122. $this->services->update($id, $data);
  123. return app('json')->success('退款成功');
  124. } else {
  125. $this->services->storeProductOrderRefundYFasle((int)$id, $refund_price);
  126. return app('json')->fail('退款失败');
  127. }
  128. }
  129. }
  130. /**
  131. * 商家同意退货退款
  132. * @return mixed
  133. */
  134. public function agreeRefund()
  135. {
  136. [$order_id] = $this->request->getMore([
  137. ['order_id', '']
  138. ], true);
  139. $this->services->agreeRefundProdcut((int)$order_id);
  140. return app('json')->success('操作成功');
  141. }
  142. /**
  143. * 修改备注
  144. * @param $id
  145. * @return mixed
  146. */
  147. public function remark($id)
  148. {
  149. $data = $this->request->postMore([['remark', '']]);
  150. if (!$data['remark'])
  151. return app('json')->fail('请输入要备注的内容');
  152. if (!$id)
  153. return app('json')->fail('缺少参数');
  154. if (!$order = $this->services->get($id)) {
  155. return app('json')->fail('修改的订单不存在!');
  156. }
  157. $order->remark = $data['remark'];
  158. if ($order->save()) {
  159. return app('json')->success('备注成功');
  160. } else
  161. return app('json')->fail('备注失败');
  162. }
  163. /**
  164. * 售后详情
  165. * @param $id
  166. * @return \think\Response
  167. */
  168. public function refundDetail($id)
  169. {
  170. if (!$id) return app('json')->fail('缺少参数');
  171. $orderInfo = $this->services->refundDetail($id, ['*'], ['invoice', 'virtual', 'supplierInfo']);
  172. $userInfo = ['spread_uid' => '', 'spread_name' => '无'];
  173. if ($orderInfo['uid']) {
  174. /** @var UserServices $services */
  175. $services = app()->make(UserServices::class);
  176. $userInfo = $services->getUserWithTrashedInfo($orderInfo['uid']);
  177. if (!$userInfo) return app('json')->fail('用户信息不存在');
  178. $userInfo = $userInfo->hidden(['pwd', 'add_ip', 'last_ip', 'login_type']);
  179. $userInfo = $userInfo->toArray();
  180. $userInfo['spread_name'] = '无';
  181. if ($orderInfo['spread_uid']) {
  182. $spreadName = $services->value(['uid' => $orderInfo['spread_uid']], 'nickname');
  183. if ($spreadName) {
  184. $userInfo['spread_name'] = $orderInfo['uid'] == $orderInfo['spread_uid'] ? $spreadName . '(自购)' : $spreadName;
  185. $userInfo['spread_uid'] = $orderInfo['spread_uid'];
  186. } else {
  187. $userInfo['spread_uid'] = '';
  188. }
  189. } else {
  190. $userInfo['spread_uid'] = '';
  191. }
  192. }
  193. $orderInfo['total_price'] = floatval(bcadd((string)$orderInfo['total_price'], (string)$orderInfo['vip_true_price'], 2));
  194. return $this->success(compact('orderInfo', 'userInfo'));
  195. }
  196. }