RefundOrder.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\controller\kefu;
  3. use app\Request;
  4. use app\services\order\StoreOrderRefundServices;
  5. use app\services\order\StoreOrderServices;
  6. use think\facade\App;
  7. class RefundOrder extends AuthController
  8. {
  9. /**
  10. * RefundOrder constructor.
  11. * @param App $app
  12. * @param StoreOrderRefundServices $service
  13. */
  14. public function __construct(App $app, StoreOrderRefundServices $service)
  15. {
  16. parent::__construct($app);
  17. $this->services = $service;
  18. }
  19. /**
  20. * 退款订单列表
  21. * @return mixed
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\DbException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. */
  26. public function getRefundList()
  27. {
  28. $where = $this->request->getMore([
  29. ['order_id', ''],
  30. ['time', ''],
  31. ['refund_type', 0]
  32. ]);
  33. return $this->success($this->services->refundList($where));
  34. }
  35. /**
  36. * 退款表单生成
  37. * @param $id
  38. * @return mixed
  39. * @throws \FormBuilder\Exception\FormBuilderException
  40. */
  41. public function refund($id)
  42. {
  43. if (!$id) {
  44. return app('json')->fail('Data does not exist!');
  45. }
  46. return app('json')->success($this->services->refundOrderForm((int)$id));
  47. }
  48. /**
  49. * 订单退款
  50. * @param Request $request
  51. * @param StoreOrderServices $services
  52. * @param $id
  53. * @return mixed
  54. */
  55. public function update_refund(Request $request, StoreOrderServices $services, $id)
  56. {
  57. $data = $request->postMore([
  58. ['refund_price', 0],
  59. ['type', 1]
  60. ]);
  61. if (!$id) {
  62. return app('json')->fail('Data does not exist!');
  63. }
  64. $orderRefund = $this->services->get($id);
  65. if (!$orderRefund) {
  66. return app('json')->fail('Data does not exist!');
  67. }
  68. if ($orderRefund['is_cancel'] == 1) {
  69. return app('json')->fail('用户已取消申请');
  70. }
  71. $order = $services->get((int)$orderRefund['store_order_id']);
  72. if (!$order) {
  73. return app('json')->fail('Data does not exist!');
  74. }
  75. if (!in_array($orderRefund['refund_type'], [1, 2, 5])) {
  76. return app('json')->fail('售后订单状态不支持该操作');
  77. }
  78. if ($data['type'] == 1) {
  79. $data['refund_type'] = 6;
  80. } else if ($data['type'] == 2) {
  81. $data['refund_type'] = 3;
  82. }
  83. $data['refunded_time'] = time();
  84. $type = $data['type'];
  85. //拒绝退款
  86. if ($type == 2) {
  87. $this->services->refuseRefund((int)$id, $data, $orderRefund);
  88. return app('json')->successful('修改退款状态成功!');
  89. } else {
  90. //0元退款
  91. if ($orderRefund['refund_price'] == 0) {
  92. $refund_price = 0;
  93. } else {
  94. if (!$data['refund_price']) {
  95. return app('json')->fail('请输入退款金额');
  96. }
  97. if ($orderRefund['refund_price'] == $orderRefund['refunded_price']) {
  98. return app('json')->fail('已退完支付金额!不能再退款了');
  99. }
  100. $refund_price = $data['refund_price'];
  101. $data['refunded_price'] = bcadd($data['refund_price'], $orderRefund['refunded_price'], 2);
  102. $bj = bccomp((string)$orderRefund['refund_price'], (string)$data['refunded_price'], 2);
  103. if ($bj < 0) {
  104. return app('json')->fail('退款金额大于支付金额,请修改退款金额');
  105. }
  106. }
  107. unset($data['type']);
  108. $refund_data['pay_price'] = $order['pay_price'];
  109. $refund_data['refund_price'] = $refund_price;
  110. //修改订单退款状态
  111. unset($data['refund_price']);
  112. if ($this->services->agreeRefund($id, $refund_data)) {
  113. $this->services->update($id, $data);
  114. return app('json')->success('退款成功');
  115. } else {
  116. $this->services->storeProductOrderRefundYFasle((int)$id, $refund_price);
  117. return app('json')->fail('退款失败');
  118. }
  119. }
  120. }
  121. /**
  122. * 商家同意退货退款
  123. * @return mixed
  124. */
  125. public function agreeRefund()
  126. {
  127. [$order_id] = $this->request->getMore([
  128. ['order_id', '']
  129. ], true);
  130. $this->services->agreeRefundProdcut((int)$order_id);
  131. return app('json')->success('操作成功');
  132. }
  133. /**
  134. * 修改备注
  135. * @param $id
  136. * @return mixed
  137. */
  138. public function remark($id)
  139. {
  140. $data = $this->request->postMore([['remark', '']]);
  141. if (!$data['remark'])
  142. return app('json')->fail('请输入要备注的内容');
  143. if (!$id)
  144. return app('json')->fail('缺少参数');
  145. if (!$order = $this->services->get($id)) {
  146. return app('json')->fail('修改的订单不存在!');
  147. }
  148. $order->remark = $data['remark'];
  149. if ($order->save()) {
  150. return app('json')->success('备注成功');
  151. } else
  152. return app('json')->fail('备注失败');
  153. }
  154. }