Refund.php 7.6 KB

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