RefundOrder.php 7.0 KB

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