Refund.php 7.7 KB

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