RefundOrder.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\order;
  12. use app\adminapi\controller\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 app\services\wechat\WechatUserServices;
  18. use think\facade\App;
  19. /**
  20. * 退款订单
  21. * Class RefundOrder
  22. * @package app\adminapi\controller\v1\order
  23. */
  24. class RefundOrder extends AuthController
  25. {
  26. /**
  27. * RefundOrder constructor.
  28. * @param App $app
  29. * @param StoreOrderRefundServices $service
  30. * @method temp
  31. */
  32. public function __construct(App $app, StoreOrderRefundServices $service)
  33. {
  34. parent::__construct($app);
  35. $this->services = $service;
  36. }
  37. /**
  38. * 退款订单列表
  39. * @return mixed
  40. */
  41. public function getRefundList()
  42. {
  43. $where = $this->request->getMore([
  44. ['order_id', ''],
  45. ['time', ''],
  46. ['refund_type', 0]
  47. ]);
  48. $where['is_cancel'] = 0;
  49. $where['is_system_del'] = 0;
  50. return app('json')->success($this->services->refundList($where));
  51. }
  52. /**
  53. * 订单详情
  54. * @param $uni
  55. * @return \think\Response
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\DbException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. * @author 吴汐
  60. * @email 442384644@qq.com
  61. * @date 2023/03/02
  62. */
  63. public function getRefundInfo($uni)
  64. {
  65. $data['orderInfo'] = $this->services->refundDetail($uni);
  66. /** @var UserServices $userServices */
  67. $userServices = app()->make(UserServices::class);
  68. $data['userInfo'] = $userServices->get($data['orderInfo']['uid']);
  69. return app('json')->success($data);
  70. }
  71. /**
  72. * 商家同意退款
  73. * @return mixed
  74. */
  75. public function agreeExpress($id)
  76. {
  77. $this->services->agreeExpress($id);
  78. return app('json')->success(100010);
  79. }
  80. /**
  81. * 修改备注
  82. * @param $id
  83. * @return mixed
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. */
  88. public function remark($id)
  89. {
  90. [$remark] = $this->request->postMore([['remark', '']], true);
  91. $this->services->updateRemark((int)$id, $remark);
  92. return app('json')->success(100024);
  93. }
  94. /**
  95. * 退款表单生成
  96. * @param $id
  97. * @return mixed
  98. * @throws \FormBuilder\Exception\FormBuilderException
  99. */
  100. public function refund($id)
  101. {
  102. if (!$id) return app('json')->fail(100100);
  103. return app('json')->success($this->services->refundOrderForm((int)$id));
  104. }
  105. /**
  106. * 订单退款(产品)
  107. * @param Request $request
  108. * @param StoreOrderServices $services
  109. * @param $id
  110. * @return mixed
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\DbException
  113. * @throws \think\db\exception\ModelNotFoundException
  114. */
  115. public function refundPrice(Request $request, StoreOrderServices $services, $id)
  116. {
  117. $data = $request->postMore([
  118. ['refund_price', 0],
  119. ['type', 1]
  120. ]);
  121. if (!$id) {
  122. return app('json')->fail(100100);
  123. }
  124. $orderRefund = $this->services->get($id);
  125. if (!$orderRefund) {
  126. return app('json')->fail(100026);
  127. }
  128. if ($orderRefund['is_cancel'] == 1) {
  129. return app('json')->fail(400118);
  130. }
  131. $order = $services->get((int)$orderRefund['store_order_id']);
  132. if (!$order) {
  133. return app('json')->fail(100026);
  134. }
  135. if (!in_array($orderRefund['refund_type'], [1, 5])) {
  136. return app('json')->fail(400144);
  137. }
  138. if ($data['type'] == 1 || $data['type'] == 5) {
  139. $data['refund_type'] = 6;
  140. } else if ($data['type'] == 2) {
  141. $data['refund_type'] = 3;
  142. }
  143. $data['refunded_time'] = time();
  144. $type = $data['type'];
  145. //拒绝退款
  146. if ($type == 2) {
  147. $this->services->refuseRefund((int)$id, $data, $orderRefund);
  148. return app('json')->success(400145);
  149. } else {
  150. //0元退款
  151. if ($orderRefund['refund_price'] == 0 && in_array($orderRefund['refund_type'], [1, 5])) {
  152. $refund_price = 0;
  153. } else {
  154. if (!$data['refund_price']) {
  155. return app('json')->fail(400146);
  156. }
  157. if ($orderRefund['refund_price'] == $orderRefund['refunded_price']) {
  158. return app('json')->fail(400147);
  159. }
  160. $refund_price = $data['refund_price'];
  161. }
  162. $data['refunded_price'] = bcadd($data['refund_price'], $orderRefund['refunded_price'], 2);
  163. $bj = bccomp((string)$orderRefund['refund_price'], (string)$data['refunded_price'], 2);
  164. if ($bj < 0) {
  165. return app('json')->fail(400148);
  166. }
  167. unset($data['type']);
  168. $refund_data['pay_price'] = $order['pay_price'];
  169. $refund_data['refund_price'] = $refund_price;
  170. if ($order['refund_price'] > 0) {
  171. mt_srand();
  172. $refund_data['refund_id'] = $order['order_id'] . rand(100, 999);
  173. }
  174. ($order['pid'] > 0) ? $refund_data['order_id'] = $services->value(['id' => (int)$order['pid']], 'order_id') : $refund_data['order_id'] = $order['order_id'];
  175. /** @var WechatUserServices $wechatUserServices */
  176. $wechatUserServices = app()->make(WechatUserServices::class);
  177. $refund_data['open_id'] = $wechatUserServices->uidToOpenid((int)$order['uid'], 'routine') ?? '';
  178. $refund_data['refund_no'] = $orderRefund['order_id'];
  179. $refund_data['order_id'] = $orderRefund['order_id'];
  180. //修改订单退款状态
  181. // $data['refund_price'] = $data['refunded_price'];
  182. unset($data['refund_price']);
  183. if ($this->services->agreeRefund($id, $refund_data)) {
  184. $this->services->update($id, $data);
  185. return app('json')->success(400149);
  186. } else {
  187. $this->services->storeProductOrderRefundYFasle((int)$id, $refund_price);
  188. return app('json')->fail(400150);
  189. }
  190. }
  191. }
  192. /**
  193. * 不退款表单结构
  194. * @param $id
  195. * @return mixed
  196. * @throws \FormBuilder\Exception\FormBuilderException
  197. */
  198. public function noRefund($id)
  199. {
  200. if (!$id) return app('json')->fail(100100);
  201. return app('json')->success($this->services->noRefundForm((int)$id));
  202. }
  203. /**
  204. * 订单不退款
  205. * @param $id
  206. * @return mixed
  207. * @throws \think\db\exception\DataNotFoundException
  208. * @throws \think\db\exception\DbException
  209. * @throws \think\db\exception\ModelNotFoundException
  210. */
  211. public function refuseRefund($id)
  212. {
  213. [$refund_reason] = $this->request->postMore([['refund_reason', '']], true);
  214. $this->services->refuse($id, $refund_reason);
  215. return app('json')->success(100010);
  216. }
  217. }