OutStoreOrderRefundServices.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. $totalPrice = bcadd($totalPrice, $cart['sum_true_price'], 2);
  108. }
  109. $refund['vip_true_price'] = $vipTruePrice;
  110. /** @var StoreOrderRefundServices $refundServices */
  111. $refundServices = app()->make(StoreOrderRefundServices::class);
  112. $refund['use_integral'] = $refundServices->getOrderSumPrice($refund['cart_info'], 'use_integral', false);
  113. $refund['coupon_price'] = $refundServices->getOrderSumPrice($refund['cart_info'], 'coupon_price', false);
  114. $refund['deduction_price'] = $refundServices->getOrderSumPrice($refund['cart_info'], 'integral_price', false);
  115. $refund['pay_postage'] = $refundServices->getOrderSumPrice($refund['cart_info'], 'postage_price', false);
  116. $refund['total_price'] = bcadd((string)$totalPrice, bcadd((string)$refund['deduction_price'], (string)$refund['coupon_price'], 2), 2);
  117. $refund['items'] = $this->tidyCartList($refund['cart_info']);
  118. if (in_array($refund['refund_type'], [1, 2, 4, 5])) {
  119. $title = '申请退款中';
  120. } elseif ($refund['refund_type'] == 3) {
  121. $title = '拒绝退款';
  122. } else {
  123. $title = '已退款';
  124. }
  125. $refund['refund_type_name'] = $title;
  126. $refund['pay_type_name'] = PayServices::PAY_TYPE[$refund['pay_type']] ?? '其他方式';
  127. unset($refund['cart_info']);
  128. return $refund;
  129. }
  130. /**
  131. * 修改售后单备注
  132. * @param string $orderId 售后单号
  133. * @param string $remark 备注
  134. * @return bool
  135. * @throws \think\db\exception\DataNotFoundException
  136. * @throws \think\db\exception\DbException
  137. * @throws \think\db\exception\ModelNotFoundException
  138. */
  139. public function remark(string $orderId, string $remark): bool
  140. {
  141. $order = $this->dao->get(['order_id' => $orderId]);
  142. if (!$order) {
  143. throw new ApiException(410173);
  144. }
  145. /** @var StoreOrderRefundServices $refundServices */
  146. $refundServices = app()->make(StoreOrderRefundServices::class);
  147. return $refundServices->updateRemark((int)$order['id'], $remark);
  148. }
  149. /**
  150. * 订单退款
  151. * @param string $orderId 售后单号
  152. * @param string $refundPrice 退款金额
  153. * @return bool
  154. * @throws \think\db\exception\DataNotFoundException
  155. * @throws \think\db\exception\DbException
  156. * @throws \think\db\exception\ModelNotFoundException
  157. */
  158. public function refundPrice(string $orderId, string $refundPrice): bool
  159. {
  160. $orderRefund = $this->dao->get(['order_id' => $orderId]);
  161. if (!$orderRefund) {
  162. throw new ApiException(100026);
  163. }
  164. if ($orderRefund['is_cancel'] == 1) {
  165. throw new ApiException(400118);
  166. }
  167. $order = $this->storeOrderServices->get((int)$orderRefund['store_order_id']);
  168. if (!$order) {
  169. throw new ApiException(100026);
  170. }
  171. if (!in_array($orderRefund['refund_type'], [1, 5])) {
  172. throw new ApiException(400144);
  173. }
  174. $data['refund_type'] = 6;
  175. $data['refunded_time'] = time();
  176. /** @var StoreOrderRefundServices $refundServices */
  177. $refundServices = app()->make(StoreOrderRefundServices::class);
  178. //0元退款
  179. if ($orderRefund['refund_price'] == 0 && in_array($orderRefund['refund_type'], [1, 5])) {
  180. $refundPrice = 0;
  181. } else {
  182. if (!$refundPrice) {
  183. throw new ApiException(400146);
  184. }
  185. if ($orderRefund['refund_price'] == $orderRefund['refunded_price']) {
  186. throw new ApiException(400147);
  187. }
  188. $data['refunded_price'] = bcadd($refundPrice, $orderRefund['refunded_price'], 2);
  189. $bj = bccomp((string)$orderRefund['refund_price'], $data['refunded_price'], 2);
  190. if ($bj < 0) {
  191. throw new ApiException(400148);
  192. }
  193. }
  194. $refundData['pay_price'] = $order['pay_price'];
  195. $refundData['refund_price'] = $refundPrice;
  196. if ($order['refund_price'] > 0) {
  197. mt_srand();
  198. $refundData['refund_id'] = $order['order_id'] . rand(100, 999);
  199. }
  200. $refundData['order_id'] = $orderId;
  201. //修改订单退款状态
  202. if ($refundServices->agreeRefund((int)$orderRefund['id'], $refundData)) {
  203. $refundServices->update((int)$orderRefund['id'], $data);
  204. return true;
  205. } else {
  206. $refundServices->storeProductOrderRefundYFasle((int)$orderRefund['id'], $refundPrice);
  207. throw new ApiException(400150);
  208. }
  209. }
  210. /**
  211. * 同意退款
  212. * @param string $orderId 售后单号
  213. * @return bool
  214. * @throws \think\db\exception\DataNotFoundException
  215. * @throws \think\db\exception\DbException
  216. * @throws \think\db\exception\ModelNotFoundException
  217. */
  218. public function agree(string $orderId): bool
  219. {
  220. $orderRefund = $this->dao->get(['order_id' => $orderId]);
  221. if (!$orderRefund) {
  222. throw new ApiException(100026);
  223. }
  224. /** @var StoreOrderRefundServices $refundServices */
  225. $refundServices = app()->make(StoreOrderRefundServices::class);
  226. return $refundServices->agreeExpress((int)$orderRefund['id']);
  227. }
  228. /**
  229. * 拒绝退款
  230. * @param string $orderId 售后单号
  231. * @param string $refundReason 不退款原因
  232. * @return bool
  233. * @throws \think\db\exception\DataNotFoundException
  234. * @throws \think\db\exception\DbException
  235. * @throws \think\db\exception\ModelNotFoundException
  236. */
  237. public function refuse(string $orderId, string $refundReason): bool
  238. {
  239. $orderRefund = $this->dao->get(['order_id' => $orderId]);
  240. if (!$orderRefund) {
  241. throw new ApiException(100026);
  242. }
  243. /** @var StoreOrderRefundServices $refundServices */
  244. $refundServices = app()->make(StoreOrderRefundServices::class);
  245. $refundServices->refuse((int)$orderRefund['id'], $refundReason);
  246. return true;
  247. }
  248. /**
  249. * 售后单生成
  250. * @param int $id
  251. * @param string $pushUrl
  252. * @return bool
  253. */
  254. public function refundCreatePush(int $id, string $pushUrl): bool
  255. {
  256. $refundInfo = $this->getInfo('', $id);
  257. /** @var OutStoreOrderServices $orderServices */
  258. $orderServices = app()->make(OutStoreOrderServices::class);
  259. $orderInfo = $orderServices->get($refundInfo['store_order_id'], ['id', 'order_id']);
  260. if (!$orderInfo) {
  261. throw new AdminException(400118);
  262. }
  263. $refundInfo['order'] = $orderInfo->toArray();
  264. return out_push($pushUrl, $refundInfo, '售后单');
  265. }
  266. /**
  267. * 售后单取消
  268. * @param int $id
  269. * @param string $pushUrl
  270. * @return bool
  271. */
  272. public function cancelApplyPush(int $id, string $pushUrl): bool
  273. {
  274. $refundInfo = $this->getInfo('', $id);
  275. /** @var OutStoreOrderServices $orderServices */
  276. $orderServices = app()->make(OutStoreOrderServices::class);
  277. $orderInfo = $orderServices->get($refundInfo['store_order_id'], ['id', 'order_id']);
  278. if (!$orderInfo) {
  279. throw new AdminException(400118);
  280. }
  281. $refundInfo['order'] = $orderInfo->toArray();
  282. return out_push($pushUrl, $refundInfo, '取消售后单');
  283. }
  284. }