Refund.php 7.5 KB

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