OutStoreOrderRefundServices.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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\services\order;
  12. use app\dao\order\StoreOrderRefundDao;
  13. use app\services\BaseServices;
  14. use app\services\pay\PayServices;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\exceptions\ApiException;
  17. use crmeb\services\HttpService;
  18. use think\facade\Log;
  19. /**
  20. * 售后单
  21. * Class OutStoreOrderRefundServices
  22. * @package app\services\order
  23. */
  24. class OutStoreOrderRefundServices extends BaseServices
  25. {
  26. /**
  27. * 订单services
  28. * @var StoreOrderServices
  29. */
  30. protected $storeOrderServices;
  31. /**
  32. * 构造方法
  33. * OutStoreOrderRefundServices constructor.
  34. * @param StoreOrderRefundDao $dao
  35. */
  36. public function __construct(StoreOrderRefundDao $dao, OutStoreOrderServices $storeOrderServices)
  37. {
  38. $this->dao = $dao;
  39. $this->storeOrderServices = $storeOrderServices;
  40. }
  41. /**
  42. * 售后单列表
  43. * @param array $where
  44. * @return void
  45. */
  46. public function refundList(array $where)
  47. {
  48. [$page, $limit] = $this->getPageValue();
  49. $field = 'id, store_order_id, uid, order_id, refund_type, refund_num, refund_price, refunded_price, refund_phone, refund_express, refund_express_name,
  50. refund_explain, refund_img, refund_reason, refuse_reason, remark, refunded_time, cart_info, is_cancel, is_del, is_pink_cancel, add_time';
  51. $list = $this->dao->getList($where, $page, $limit, $field);
  52. $count = $this->dao->count($where);
  53. foreach ($list as $key => &$item) {
  54. $item['pay_price'] = $item['refund_price'];
  55. unset($item['refund_price']);
  56. $item['items'] = $this->tidyCartList($item['cart_info']);
  57. unset($list[$key]['cart_info']);
  58. }
  59. return compact('list', 'count');
  60. }
  61. /**
  62. * 格式化订单商品
  63. * @param array $carts
  64. * @return array
  65. */
  66. public function tidyCartList(array $carts): array
  67. {
  68. $list = [];
  69. foreach ($carts as $cart) {
  70. $list[] = [
  71. 'store_name' => $cart['productInfo']['store_name'] ?? '',
  72. 'suk' => $cart['productInfo']['attrInfo']['suk'] ?? '',
  73. 'image' => $cart['productInfo']['attrInfo']['image'] ?: $cart['productInfo']['image'],
  74. 'price' => sprintf("%.2f", $cart['truePrice'] ?? '0.00'),
  75. 'cart_num' => $cart['cart_num'] ?? 0
  76. ];
  77. }
  78. return $list;
  79. }
  80. /**
  81. * 退款订单详情
  82. * @param string $orderId 售后单号
  83. * @param int $id 售后单ID
  84. * @return mixed
  85. */
  86. public function getInfo(string $orderId = '', int $id = 0)
  87. {
  88. $field = ['id', 'store_order_id', 'order_id', 'uid', 'refund_type', 'refund_num', 'refund_price',
  89. 'refunded_price', 'refund_phone', 'refund_express', 'refund_express_name', 'refund_explain',
  90. 'refund_img', 'refund_reason', 'refuse_reason', 'remark', 'refunded_time', 'cart_info', 'is_cancel',
  91. 'is_pink_cancel', 'is_del', 'add_time'];
  92. if ($id > 0) {
  93. $where = $id;
  94. } else {
  95. $where = ['order_id' => $orderId];
  96. }
  97. $refund = $this->dao->get($where, $field, ['orderData']);
  98. if (!$refund) throw new ApiException(410173);
  99. $refund = $refund->toArray();
  100. //核算优惠金额
  101. $totalPrice = 0;
  102. $vipTruePrice = 0;
  103. foreach ($refund['cart_info'] ?? [] as $key => &$cart) {
  104. $cart['sum_true_price'] = sprintf("%.2f", $cart['sum_true_price'] ?? bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2));
  105. $cart['vip_sum_truePrice'] = bcmul($cart['vip_truePrice'], $cart['cart_num'] ?: 1, 2);
  106. $vipTruePrice = bcadd((string)$vipTruePrice, $cart['vip_sum_truePrice'], 2);
  107. if (isset($order['split']) && $order['split']) {
  108. $refund['cart_info'][$key]['cart_num'] = $cart['surplus_num'];
  109. if (!$cart['surplus_num']) unset($refund['cart_info'][$key]);
  110. }
  111. $totalPrice = bcadd($totalPrice, $cart['sum_true_price'], 2);
  112. }
  113. $refund['vip_true_price'] = $vipTruePrice;
  114. /** @var StoreOrderRefundServices $refundServices */
  115. $refundServices = app()->make(StoreOrderRefundServices::class);
  116. $refund['use_integral'] = $refundServices->getOrderSumPrice($refund['cart_info'], 'use_integral', false);
  117. $refund['coupon_price'] = $refundServices->getOrderSumPrice($refund['cart_info'], 'coupon_price', false);
  118. $refund['deduction_price'] = $refundServices->getOrderSumPrice($refund['cart_info'], 'integral_price', false);
  119. $refund['pay_postage'] = $refundServices->getOrderSumPrice($refund['cart_info'], 'postage_price', false);
  120. $refund['total_price'] = bcadd((string)$totalPrice, bcadd((string)$refund['deduction_price'], (string)$refund['coupon_price'], 2), 2);
  121. $refund['items'] = $this->tidyCartList($refund['cart_info']);
  122. if (in_array($refund['refund_type'], [1, 2, 4, 5])) {
  123. $title = '申请退款中';
  124. } elseif ($refund['refund_type'] == 3) {
  125. $title = '拒绝退款';
  126. } else {
  127. $title = '已退款';
  128. }
  129. $refund['refund_type_name'] = $title;
  130. $refund['pay_type_name'] = PayServices::PAY_TYPE[$refund['pay_type']] ?? '其他方式';
  131. unset($refund['cart_info']);
  132. return $refund;
  133. }
  134. /**
  135. * 修改售后单备注
  136. * @param string $orderId 售后单号
  137. * @param string $remark 备注
  138. * @return bool
  139. * @throws \think\db\exception\DataNotFoundException
  140. * @throws \think\db\exception\DbException
  141. * @throws \think\db\exception\ModelNotFoundException
  142. */
  143. public function remark(string $orderId, string $remark): bool
  144. {
  145. $order = $this->dao->get(['order_id' => $orderId]);
  146. if (!$order) {
  147. throw new ApiException(410173);
  148. }
  149. /** @var StoreOrderRefundServices $refundServices */
  150. $refundServices = app()->make(StoreOrderRefundServices::class);
  151. return $refundServices->updateRemark((int)$order['id'], $remark);
  152. }
  153. /**
  154. * 订单退款
  155. * @param string $orderId 售后单号
  156. * @param string $refundPrice 退款金额
  157. * @return bool
  158. * @throws \think\db\exception\DataNotFoundException
  159. * @throws \think\db\exception\DbException
  160. * @throws \think\db\exception\ModelNotFoundException
  161. */
  162. public function refundPrice(string $orderId, string $refundPrice): bool
  163. {
  164. $orderRefund = $this->dao->get(['order_id' => $orderId]);
  165. if (!$orderRefund) {
  166. throw new ApiException(100026);
  167. }
  168. if ($orderRefund['is_cancel'] == 1) {
  169. throw new ApiException(400118);
  170. }
  171. $order = $this->storeOrderServices->get((int)$orderRefund['store_order_id']);
  172. if (!$order) {
  173. throw new ApiException(100026);
  174. }
  175. if (!in_array($orderRefund['refund_type'], [1, 5])) {
  176. throw new ApiException(400144);
  177. }
  178. $data['refund_type'] = 6;
  179. $data['refunded_time'] = time();
  180. /** @var StoreOrderRefundServices $refundServices */
  181. $refundServices = app()->make(StoreOrderRefundServices::class);
  182. //0元退款
  183. if ($orderRefund['refund_price'] == 0 && in_array($orderRefund['refund_type'], [1, 5])) {
  184. $refundPrice = 0;
  185. } else {
  186. if (!$refundPrice) {
  187. throw new ApiException(400146);
  188. }
  189. if ($orderRefund['refund_price'] == $orderRefund['refunded_price']) {
  190. throw new ApiException(400147);
  191. }
  192. $data['refunded_price'] = bcadd($refundPrice, $orderRefund['refunded_price'], 2);
  193. $bj = bccomp((string)$orderRefund['refund_price'], $data['refunded_price'], 2);
  194. if ($bj < 0) {
  195. throw new ApiException(400148);
  196. }
  197. }
  198. $refundData['pay_price'] = $order['pay_price'];
  199. $refundData['refund_price'] = $refundPrice;
  200. if ($order['refund_price'] > 0) {
  201. mt_srand();
  202. $refundData['refund_id'] = $order['order_id'] . rand(100, 999);
  203. }
  204. //修改订单退款状态
  205. if ($refundServices->agreeRefund((int)$orderRefund['id'], $refundData)) {
  206. $refundServices->update((int)$orderRefund['id'], $data);
  207. return true;
  208. } else {
  209. $refundServices->storeProductOrderRefundYFasle((int)$orderRefund['id'], $refundPrice);
  210. throw new ApiException(400150);
  211. }
  212. }
  213. /**
  214. * 同意退款
  215. * @param string $orderId 售后单号
  216. * @return bool
  217. * @throws \think\db\exception\DataNotFoundException
  218. * @throws \think\db\exception\DbException
  219. * @throws \think\db\exception\ModelNotFoundException
  220. */
  221. public function agree(string $orderId): bool
  222. {
  223. $orderRefund = $this->dao->get(['order_id' => $orderId]);
  224. if (!$orderRefund) {
  225. throw new ApiException(100026);
  226. }
  227. /** @var StoreOrderRefundServices $refundServices */
  228. $refundServices = app()->make(StoreOrderRefundServices::class);
  229. return $refundServices->agreeExpress((int)$orderRefund['id']);
  230. }
  231. /**
  232. * 拒绝退款
  233. * @param string $orderId 售后单号
  234. * @param string $refundReason 不退款原因
  235. * @return bool
  236. * @throws \think\db\exception\DataNotFoundException
  237. * @throws \think\db\exception\DbException
  238. * @throws \think\db\exception\ModelNotFoundException
  239. */
  240. public function refuse(string $orderId, string $refundReason): bool
  241. {
  242. $orderRefund = $this->dao->get(['order_id' => $orderId]);
  243. if (!$orderRefund) {
  244. throw new ApiException(100026);
  245. }
  246. /** @var StoreOrderRefundServices $refundServices */
  247. $refundServices = app()->make(StoreOrderRefundServices::class);
  248. $refundServices->refuse((int)$orderRefund['id'], $refundReason);
  249. return true;
  250. }
  251. /**
  252. * 售后单生成
  253. * @param int $id
  254. * @param string $pushUrl
  255. * @return bool
  256. */
  257. public function refundCreatePush(int $id, string $pushUrl): bool
  258. {
  259. $refundInfo = $this->getInfo('', $id);
  260. /** @var OutStoreOrderServices $orderServices */
  261. $orderServices = app()->make(OutStoreOrderServices::class);
  262. $orderInfo = $orderServices->get($refundInfo['store_order_id'], ['id', 'order_id']);
  263. if (!$orderInfo) {
  264. throw new AdminException(400118);
  265. }
  266. $refundInfo['order'] = $orderInfo->toArray();
  267. return out_push($pushUrl, $refundInfo, '售后单');
  268. }
  269. /**
  270. * 售后单取消
  271. * @param int $id
  272. * @param string $pushUrl
  273. * @return bool
  274. */
  275. public function cancelApplyPush(int $id, string $pushUrl): bool
  276. {
  277. $refundInfo = $this->getInfo('', $id);
  278. /** @var OutStoreOrderServices $orderServices */
  279. $orderServices = app()->make(OutStoreOrderServices::class);
  280. $orderInfo = $orderServices->get($refundInfo['store_order_id'], ['id', 'order_id']);
  281. if (!$orderInfo) {
  282. throw new AdminException(400118);
  283. }
  284. $refundInfo['order'] = $orderInfo->toArray();
  285. return out_push($pushUrl, $refundInfo, '取消售后单');
  286. }
  287. }