StoreOrderRefundServices.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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\services\order;
  12. use app\dao\order\StoreOrderRefundDao;
  13. use app\services\activity\advance\StoreAdvanceServices;
  14. use app\services\activity\bargain\StoreBargainServices;
  15. use app\services\activity\combination\StoreCombinationServices;
  16. use app\services\activity\combination\StorePinkServices;
  17. use app\services\activity\seckill\StoreSeckillServices;
  18. use app\services\BaseServices;
  19. use app\services\activity\coupon\StoreCouponIssueUserServices;
  20. use app\services\activity\coupon\StoreCouponUserServices;
  21. use app\services\pay\PayServices;
  22. use app\services\product\product\StoreProductServices;
  23. use app\services\shipping\ExpressServices;
  24. use app\services\statistic\CapitalFlowServices;
  25. use app\services\user\UserBillServices;
  26. use app\services\user\UserBrokerageServices;
  27. use app\services\user\UserMoneyServices;
  28. use app\services\user\UserServices;
  29. use crmeb\services\AliPayService;
  30. use crmeb\services\CacheService;
  31. use crmeb\services\FormBuilder as Form;
  32. use crmeb\services\MiniProgramService;
  33. use crmeb\services\WechatService;
  34. use crmeb\services\workerman\ChannelService;
  35. use think\exception\ValidateException;
  36. use think\facade\Log;
  37. /**
  38. * 订单退款
  39. * Class StoreOrderRefundServices
  40. * @method getOrderRefundMoneyByWhere
  41. * @package app\services\order
  42. */
  43. class StoreOrderRefundServices extends BaseServices
  44. {
  45. /**
  46. * 订单services
  47. * @var StoreOrderServices
  48. */
  49. protected $storeOrderServices;
  50. /**
  51. * 构造方法
  52. * StoreOrderRefundServices constructor.
  53. * @param StoreOrderRefundDao $dao
  54. */
  55. public function __construct(StoreOrderRefundDao $dao, StoreOrderServices $storeOrderServices)
  56. {
  57. $this->dao = $dao;
  58. $this->storeOrderServices = $storeOrderServices;
  59. }
  60. /**
  61. * 订单退款表单
  62. * @param int $id
  63. * @return array
  64. * @throws \FormBuilder\Exception\FormBuilderException
  65. */
  66. public function refundOrderForm(int $id, $type = 'refund')
  67. {
  68. if ($type == 'refund') {//售后订单
  69. $orderRefund = $this->dao->get($id);
  70. if (!$orderRefund) {
  71. throw new ValidateException('未查到订单');
  72. }
  73. $order = $this->storeOrderServices->get((int)$orderRefund['store_order_id']);
  74. if (!$order) {
  75. throw new ValidateException('未查到订单');
  76. }
  77. if (!$order['paid']) {
  78. throw new ValidateException('未支付无法退款');
  79. }
  80. if ($orderRefund['refund_price'] > 0 && in_array($orderRefund['refund_type'], [1, 5])) {
  81. if ($orderRefund['refund_price'] <= $orderRefund['refunded_price']) {
  82. throw new ValidateException('订单已退款');
  83. }
  84. }
  85. $f[] = Form::input('order_id', '退款单号', $orderRefund->getData('order_id'))->disabled(true);
  86. $f[] = Form::number('refund_price', '退款金额', (float)bcsub((string)$orderRefund->getData('refund_price'), (string)$orderRefund->getData('refunded_price'), 2))->min(0)->required('请输入退款金额');
  87. return create_form('退款处理', $f, $this->url('/refund/refund/' . $id), 'PUT');
  88. } else {//订单主动退款
  89. $order = $this->storeOrderServices->get((int)$id);
  90. if (!$order) {
  91. throw new ValidateException('未查到订单');
  92. }
  93. if (!$order['paid']) {
  94. throw new ValidateException('未支付无法退款');
  95. }
  96. if ($order['pay_price'] > 0 && in_array($order['refund_status'], [0, 1])) {
  97. if ($order['pay_price'] <= $order['refund_price']) {
  98. throw new ValidateException('订单已退款');
  99. }
  100. }
  101. $f[] = Form::input('order_id', '退款单号', $order->getData('order_id'))->disabled(true);
  102. $f[] = Form::number('refund_price', '退款金额', (float)bcsub((string)$order->getData('pay_price'), (string)$order->getData('refund_price'), 2))->precision(2)->required('请输入退款金额');
  103. return create_form('退款处理', $f, $this->url('/order/refund/' . $id), 'PUT');
  104. }
  105. }
  106. /**
  107. * 同意退款:拆分退款单、退积分、佣金等
  108. * @param int $id
  109. * @param array $refundData
  110. * @return bool
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\DbException
  113. * @throws \think\db\exception\ModelNotFoundException
  114. */
  115. public function agreeRefund(int $id, array $refundData)
  116. {
  117. $order = $this->transaction(function () use ($id, $refundData) {
  118. //退款拆分
  119. $orderRefundInfo = $this->dao->get($id);
  120. if (!$orderRefundInfo) throw new ValidateException('数据不存在');
  121. $cart_ids = [];
  122. if ($orderRefundInfo['cart_info']) {
  123. foreach ($orderRefundInfo['cart_info'] as $cart) {
  124. $cart_ids[] = [
  125. 'cart_id' => $cart['id'],
  126. 'cart_num' => $cart['cart_num'],
  127. ];
  128. }
  129. }
  130. if (!$cart_ids) return false;
  131. $orderInfo = $this->storeOrderServices->get($orderRefundInfo['store_order_id']);
  132. /** @var StoreOrderSplitServices $storeOrderSplitServices */
  133. $storeOrderSplitServices = app()->make(StoreOrderSplitServices::class);
  134. [$splitOrderInfo, $otherOrder] = $storeOrderSplitServices->equalSplit($orderRefundInfo['store_order_id'], $cart_ids, $orderInfo);
  135. //回退积分和优惠卷
  136. if (!$this->integralAndCouponBack($splitOrderInfo)) {
  137. throw new ValidateException('回退积分和优惠卷失败');
  138. }
  139. //退拼团
  140. if ($splitOrderInfo['pid'] == 0 && !$splitOrderInfo['pink_id']) {
  141. /** @var StorePinkServices $pinkServices */
  142. $pinkServices = app()->make(StorePinkServices::class);
  143. if (!$pinkServices->setRefundPink($splitOrderInfo)) {
  144. throw new ValidateException('拼团修改失败!');
  145. }
  146. }
  147. //退佣金
  148. /** @var UserBrokerageServices $userBrokerageServices */
  149. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  150. if (!$userBrokerageServices->orderRefundBrokerageBack($splitOrderInfo)) {
  151. throw new ValidateException('回退佣金失败');
  152. }
  153. //回退库存
  154. if ($splitOrderInfo['status'] == 0) {
  155. /** @var StoreOrderStatusServices $services */
  156. $services = app()->make(StoreOrderStatusServices::class);
  157. if (!$services->count(['oid' => $splitOrderInfo['id'], 'change_type' => 'refund_price'])) {
  158. $this->regressionStock($splitOrderInfo);
  159. }
  160. }
  161. //退金额
  162. if ($refundData['refund_price'] > 0) {
  163. if (!isset($refundData['refund_id']) || !$refundData['refund_id']) {
  164. mt_srand();
  165. $refundData['refund_id'] = $splitOrderInfo['order_id'] . rand(100, 999);
  166. }
  167. if ($splitOrderInfo['pid'] > 0) {//子订单
  168. $refundOrder = $this->storeOrderServices->get((int)$splitOrderInfo['pid']);
  169. $refundData['pay_price'] = $refundOrder['pay_price'];
  170. } else {
  171. $refundOrder = $splitOrderInfo;
  172. }
  173. switch ($refundOrder['pay_type']) {
  174. case PayServices::WEIXIN_PAY:
  175. $no = $refundOrder['order_id'];
  176. if ($refundOrder['trade_no']) {
  177. $no = $refundOrder['trade_no'];
  178. $refundData['type'] = 'trade_no';
  179. }
  180. if ($refundOrder['is_channel'] == 1) {
  181. //小程序退款
  182. MiniProgramService::payOrderRefund($no, $refundData);//小程序
  183. } else {
  184. //微信公众号退款
  185. WechatService::payOrderRefund($no, $refundData);//公众号
  186. }
  187. break;
  188. case PayServices::YUE_PAY:
  189. //余额退款
  190. if (!$this->yueRefund($refundOrder, $refundData)) {
  191. throw new ValidateException('余额退款失败');
  192. }
  193. break;
  194. case PayServices::ALIAPY_PAY:
  195. mt_srand();
  196. $refund_id = $refundData['refund_id'] ?? $refundOrder['order_id'] . rand(100, 999);
  197. //支付宝退款
  198. AliPayService::instance()->refund(strpos($refundOrder['trade_no'], '_') !== false ? $refundOrder['trade_no'] : $refundOrder['order_id'], floatval($refundData['refund_price']), $refund_id);
  199. break;
  200. }
  201. }
  202. //订单记录
  203. /** @var StoreOrderStatusServices $statusService */
  204. $statusService = app()->make(StoreOrderStatusServices::class);
  205. $statusService->save([
  206. 'oid' => $splitOrderInfo['id'],
  207. 'change_type' => 'refund_price',
  208. 'change_message' => '退款给用户:' . $refundData['refund_price'] . '元',
  209. 'change_time' => time()
  210. ]);
  211. $this->storeOrderServices->update($splitOrderInfo['id'], [
  212. 'status' => -2,
  213. 'refund_status' => 2,
  214. 'refund_type' => $orderRefundInfo['refund_type'],
  215. 'refund_express' => $orderRefundInfo['refund_express'],
  216. 'refund_express_name' => $orderRefundInfo['refund_express_name'],
  217. 'refund_reason_wap_img' => $orderRefundInfo['refund_img'],
  218. 'refund_reason_wap_explain' => $orderRefundInfo['refund_explain'],
  219. 'refund_reason_time' => $orderRefundInfo['refunded_time'],
  220. 'refund_reason_wap' => $orderRefundInfo['refund_reason'],
  221. 'refund_price' => $orderRefundInfo['refund_price'],
  222. ], 'id');
  223. $this->dao->update($id, ['store_order_id' => $splitOrderInfo['id']]);
  224. if ($otherOrder['id'] != 0 && $orderInfo['id'] != $otherOrder['id']) {//拆分生成新订单了
  225. //修改原订单还在申请的退款单
  226. $this->dao->update(['store_order_id' => $orderInfo['id']], ['store_order_id' => $otherOrder['id']]);
  227. }
  228. /** @var CapitalFlowServices $capitalFlowServices */
  229. $capitalFlowServices = app()->make(CapitalFlowServices::class);
  230. /** @var UserServices $userServices */
  231. $userServices = app()->make(UserServices::class);
  232. $userInfo = $userServices->get($splitOrderInfo['uid']);
  233. $splitOrderInfo['nickname'] = $userInfo['nickname'];
  234. $splitOrderInfo['phone'] = $userInfo['phone'];
  235. if ($splitOrderInfo['pay_type'] == 'alipay' || $splitOrderInfo['pay_type'] == 'weixin' || $splitOrderInfo['pay_type'] == 'offline') {
  236. $capitalFlowServices->setFlow($splitOrderInfo, 'refund');
  237. }
  238. return $splitOrderInfo;
  239. });
  240. //订单同意退款事件
  241. event('order.refund', [$refundData, $order, 'order_refund']);
  242. event('notice.notice', [['data' => $refundData, 'order' => $order], 'order_refund']);
  243. return true;
  244. }
  245. /**
  246. * 商家同意用户退货
  247. * @param $id
  248. * @return bool
  249. */
  250. public function agreeExpress($id)
  251. {
  252. $this->dao->update($id, ['refund_type' => 4], 'id');
  253. return true;
  254. }
  255. /**
  256. * 订单退款处理
  257. * @param int $type
  258. * @param $order
  259. * @param array $refundData
  260. * @return mixed
  261. */
  262. public function payOrderRefund(int $type, $order, array $refundData)
  263. {
  264. return $this->transaction(function () use ($type, $order, $refundData) {
  265. //回退积分和优惠卷
  266. if (!$this->integralAndCouponBack($order)) {
  267. throw new ValidateException('回退积分和优惠卷失败');
  268. }
  269. //虚拟商品优惠券退款处理
  270. if ($order['virtual_type'] == 2) {
  271. /** @var StoreCouponUserServices $couponUser */
  272. $couponUser = app()->make(StoreCouponUserServices::class);
  273. $res = $couponUser->delUserCoupon(['cid' => $order['virtual_info'], 'uid' => $order['uid'], 'status' => 0]);
  274. if (!$res) throw new ValidateException('购买的优惠券已使用或者已过期');
  275. /** @var StoreCouponIssueUserServices $couponIssueUser */
  276. $couponIssueUser = app()->make(StoreCouponIssueUserServices::class);
  277. $couponIssueUser->delIssueUserCoupon(['issue_coupon_id' => $order['virtual_info'], 'uid' => $order['uid']]);
  278. }
  279. //退拼团
  280. if ($type == 1) {
  281. /** @var StorePinkServices $pinkServices */
  282. $pinkServices = app()->make(StorePinkServices::class);
  283. if (!$pinkServices->setRefundPink($order)) {
  284. throw new ValidateException('拼团修改失败!');
  285. }
  286. }
  287. //退佣金
  288. /** @var UserBrokerageServices $userBrokerageServices */
  289. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  290. if (!$userBrokerageServices->orderRefundBrokerageBack($order)) {
  291. throw new ValidateException('回退佣金失败');
  292. }
  293. //回退库存
  294. if ($order['status'] == 0) {
  295. /** @var StoreOrderStatusServices $services */
  296. $services = app()->make(StoreOrderStatusServices::class);
  297. if (!$services->count(['oid' => $order['id'], 'change_type' => 'refund_price'])) {
  298. $this->regressionStock($order);
  299. }
  300. }
  301. //退金额
  302. if ($refundData['refund_price'] > 0) {
  303. if (!isset($refundData['refund_id']) || !$refundData['refund_id']) {
  304. mt_srand();
  305. $refundData['refund_id'] = $order['order_id'] . rand(100, 999);
  306. }
  307. if ($order['pid'] > 0) {//子订单
  308. $refundOrder = $this->storeOrderServices->get((int)$order['pid']);
  309. $refundData['pay_price'] = $refundOrder['pay_price'];
  310. } else {
  311. $refundOrder = $order;
  312. }
  313. switch ($refundOrder['pay_type']) {
  314. case PayServices::WEIXIN_PAY:
  315. $no = $refundOrder['order_id'];
  316. if ($refundOrder['trade_no']) {
  317. $no = $refundOrder['trade_no'];
  318. $refundData['type'] = 'trade_no';
  319. }
  320. if ($refundOrder['is_channel'] == 1) {
  321. //小程序退款
  322. MiniProgramService::payOrderRefund($no, $refundData);//小程序
  323. } else {
  324. //微信公众号退款
  325. WechatService::payOrderRefund($no, $refundData);//公众号
  326. }
  327. break;
  328. case PayServices::YUE_PAY:
  329. //余额退款
  330. if (!$this->yueRefund($refundOrder, $refundData)) {
  331. throw new ValidateException('余额退款失败');
  332. }
  333. break;
  334. case PayServices::ALIAPY_PAY:
  335. mt_srand();
  336. $refund_id = $refundData['refund_id'] ?? $refundOrder['order_id'] . rand(100, 999);
  337. //支付宝退款
  338. AliPayService::instance()->refund(strpos($refundOrder['trade_no'], '_') !== false ? $refundOrder['trade_no'] : $refundOrder['order_id'], floatval($refundData['refund_price']), $refund_id);
  339. break;
  340. }
  341. }
  342. //修改开票数据退款状态
  343. $orderInvoiceServices = app()->make(StoreOrderInvoiceServices::class);
  344. $orderInvoiceServices->update(['order_id' => $order['id']], ['is_refund' => 1]);
  345. });
  346. }
  347. /**
  348. * 余额退款
  349. * @param $order
  350. * @param array $refundData
  351. * @return bool
  352. */
  353. public function yueRefund($order, array $refundData)
  354. {
  355. /** @var UserServices $userServices */
  356. $userServices = app()->make(UserServices::class);
  357. $userMoney = $userServices->value(['uid' => $order['uid']], 'now_money');
  358. $res = $userServices->bcInc($order['uid'], 'now_money', $refundData['refund_price'], 'uid');
  359. /** @var UserMoneyServices $userMoneyServices */
  360. $userMoneyServices = app()->make(UserMoneyServices::class);
  361. return $res && $userMoneyServices->income('pay_product_refund', $order['uid'], $refundData['refund_price'], bcadd((string)$userMoney, (string)$refundData['refund_price'], 2), $order['id']);
  362. }
  363. /**
  364. * 回退积分和优惠卷
  365. * @param $order
  366. * @return bool
  367. */
  368. public function integralAndCouponBack($order)
  369. {
  370. $res = true;
  371. //回退优惠卷 拆分子订单不退优惠券
  372. if (!$order['pid'] && $order['coupon_id'] && $order['coupon_price']) {
  373. /** @var StoreCouponUserServices $coumonUserServices */
  374. $coumonUserServices = app()->make(StoreCouponUserServices::class);
  375. $res = $res && $coumonUserServices->recoverCoupon((int)$order['coupon_id']);
  376. }
  377. //回退积分
  378. $order = $this->regressionIntegral($order);
  379. /** @var StoreOrderStatusServices $statusService */
  380. $statusService = app()->make(StoreOrderStatusServices::class);
  381. $statusService->save([
  382. 'oid' => $order['id'],
  383. 'change_type' => 'integral_back',
  384. 'change_message' => '商品退积分',
  385. 'change_time' => time()
  386. ]);
  387. return $res && $order->save();
  388. }
  389. /**
  390. * 回退使用积分和赠送积分
  391. * @param $order
  392. * @return bool
  393. */
  394. public function regressionIntegral($order)
  395. {
  396. /** @var UserServices $userServices */
  397. $userServices = app()->make(UserServices::class);
  398. $userInfo = $userServices->get($order['uid'], ['integral']);
  399. if (!$userInfo) {
  400. $order->back_integral = $order->use_integral;
  401. return $order;
  402. }
  403. $integral = $userInfo['integral'];
  404. if ($order['status'] == -2 || $order['is_del']) {
  405. return $order;
  406. }
  407. $res1 = $res2 = $res3 = $res4 = true;
  408. //订单赠送积分
  409. /** @var UserBillServices $userBillServices */
  410. $userBillServices = app()->make(UserBillServices::class);
  411. $order_gain = $userBillServices->sum([
  412. 'category' => 'integral',
  413. 'type' => 'gain',
  414. 'link_id' => $order['id'],
  415. 'uid' => $order['uid']
  416. ], 'number');
  417. //商品赠送
  418. $product_gain = $userBillServices->sum([
  419. 'category' => 'integral',
  420. 'type' => 'product_gain',
  421. 'link_id' => $order['id'],
  422. 'uid' => $order['uid']
  423. ], 'number');
  424. $give_integral = $order_gain + $product_gain;
  425. if ($give_integral) {
  426. //判断订单是否已经回退积分
  427. $count = $userBillServices->count(['category' => 'integral', 'type' => 'integral_refund', 'link_id' => $order['id']]);
  428. if (!$count) {
  429. $res1 = $userServices->bcDec($order['uid'], 'integral', $give_integral);
  430. //记录赠送积分收回
  431. $integral = $integral - $give_integral;
  432. $res2 = $userBillServices->income('integral_refund', $order['uid'], $give_integral, $integral, $order['id']);
  433. }
  434. }
  435. //返还下单使用积分
  436. $use_integral = $order['use_integral'];
  437. if ($use_integral > 0) {
  438. $res3 = $userServices->bcInc($order['uid'], 'integral', $use_integral);
  439. //记录下单使用积分还回
  440. $res4 = $userBillServices->income('pay_product_integral_back', $order['uid'], (int)$use_integral, $integral + $use_integral, $order['id']);
  441. }
  442. if (!($res1 && $res2 && $res3 && $res4)) {
  443. throw new ValidateException('回退积分增加失败');
  444. }
  445. if ($use_integral > $give_integral) {
  446. $order->back_integral = bcsub($use_integral, $give_integral, 2);
  447. }
  448. return $order;
  449. }
  450. /**
  451. * 回退库存
  452. * @param $order
  453. * @return bool
  454. */
  455. public function regressionStock($order)
  456. {
  457. if ($order['status'] == -2 || $order['is_del']) return true;
  458. $combination_id = $order['combination_id'];
  459. $seckill_id = $order['seckill_id'];
  460. $bargain_id = $order['bargain_id'];
  461. $advance_id = $order['advance_id'];
  462. $res5 = true;
  463. /** @var StoreOrderCartInfoServices $cartServices */
  464. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  465. /** @var StoreProductServices $services */
  466. $services = app()->make(StoreProductServices::class);
  467. /** @var StoreSeckillServices $seckillServices */
  468. $seckillServices = app()->make(StoreSeckillServices::class);
  469. /** @var StoreCombinationServices $pinkServices */
  470. $pinkServices = app()->make(StoreCombinationServices::class);
  471. /** @var StoreBargainServices $bargainServices */
  472. $bargainServices = app()->make(StoreBargainServices::class);
  473. /** @var StoreAdvanceServices $advanceServices */
  474. $advanceServices = app()->make(StoreAdvanceServices::class);
  475. $cartInfo = $cartServices->getCartInfoList(['cart_id' => $order['cart_id']], ['cart_info']);
  476. foreach ($cartInfo as $cart) {
  477. $cart['cart_info'] = is_array($cart['cart_info']) ? $cart['cart_info'] : json_decode($cart['cart_info'], true);
  478. //增库存减销量
  479. $unique = isset($cart['cart_info']['productInfo']['attrInfo']) ? $cart['cart_info']['productInfo']['attrInfo']['unique'] : '';
  480. $cart_num = (int)$cart['cart_info']['cart_num'];
  481. $type = 0;
  482. if ($combination_id) {
  483. $type = 3;
  484. $res5 = $res5 && $pinkServices->incCombinationStock($cart_num, (int)$combination_id, $unique);
  485. } else if ($seckill_id) {
  486. $type = 1;
  487. $res5 = $res5 && $seckillServices->incSeckillStock($cart_num, (int)$seckill_id, $unique);
  488. } else if ($bargain_id) {
  489. $type = 2;
  490. $res5 = $res5 && $bargainServices->incBargainStock($cart_num, (int)$bargain_id, $unique);
  491. } else {
  492. $res5 = $res5 && $services->incProductStock($cart_num, (int)$cart['cart_info']['productInfo']['id'], $unique);
  493. }
  494. if ($type) CacheService::setStock($unique, $cart_num, $type, false);
  495. }
  496. return $res5;
  497. }
  498. /**
  499. * 同意退款成功发送模板消息和记录订单状态
  500. * @param $data
  501. * @param $order
  502. * @param $refund_price
  503. * @param $id
  504. */
  505. public function storeProductOrderRefundY($data, $order, $refund_price)
  506. {
  507. /** @var StoreOrderStatusServices $statusService */
  508. $statusService = app()->make(StoreOrderStatusServices::class);
  509. $statusService->save([
  510. 'oid' => $order['id'],
  511. 'change_type' => 'refund_price',
  512. 'change_message' => '退款给用户:' . $refund_price . '元',
  513. 'change_time' => time()
  514. ]);
  515. /** @var CapitalFlowServices $capitalFlowServices */
  516. $capitalFlowServices = app()->make(CapitalFlowServices::class);
  517. /** @var UserServices $userServices */
  518. $userServices = app()->make(UserServices::class);
  519. $userInfo = $userServices->get($order['uid']);
  520. $order['nickname'] = $userInfo['nickname'];
  521. $order['phone'] = $userInfo['phone'];
  522. if ($order['pay_type'] == 'alipay' || $order['pay_type'] == 'weixin' || $order['pay_type'] == 'offline') {
  523. $capitalFlowServices->setFlow($order, 'refund');
  524. }
  525. event('notice.notice', [['data' => $data, 'order' => $order], 'order_refund']);
  526. }
  527. /**
  528. * 同意退款退款失败写入订单记录
  529. * @param int $id
  530. * @param $refund_price
  531. */
  532. public function storeProductOrderRefundYFasle(int $id, $refund_price)
  533. {
  534. /** @var StoreOrderStatusServices $statusService */
  535. $statusService = app()->make(StoreOrderStatusServices::class);
  536. $statusService->save([
  537. 'oid' => $id,
  538. 'change_type' => 'refund_price',
  539. 'change_message' => '退款给用户:' . $refund_price . '元失败',
  540. 'change_time' => time()
  541. ]);
  542. }
  543. /**
  544. * 不退款记录订单变更状态
  545. * @param int $id
  546. * @param string $refundReason
  547. */
  548. public function storeProductOrderRefundNo(int $id, string $refundReason)
  549. {
  550. /** @var StoreOrderStatusServices $statusService */
  551. $statusService = app()->make(StoreOrderStatusServices::class);
  552. $statusService->save([
  553. 'oid' => $id,
  554. 'change_type' => 'refund_n',
  555. 'change_message' => '不退款原因:' . $refundReason,
  556. 'change_time' => time()
  557. ]);
  558. }
  559. /**
  560. * 不退款表单
  561. * @param int $id
  562. * @return array
  563. * @throws \FormBuilder\Exception\FormBuilderException
  564. */
  565. public function noRefundForm(int $id)
  566. {
  567. $order = $this->dao->get($id);
  568. if (!$order) {
  569. throw new ValidateException('Data does not exist!');
  570. }
  571. $f[] = Form::input('order_id', '不退款单号', $order->getData('order_id'))->disabled(true);
  572. $f[] = Form::input('refund_reason', '不退款原因')->type('textarea')->required('请填写不退款原因');
  573. return create_form('不退款原因', $f, $this->url('refund/no_refund/' . $id), 'PUT');
  574. }
  575. /**
  576. * 拒绝退款
  577. * @param int $id
  578. * @param array $data
  579. * @param array $orderRefundInfo
  580. * @return bool
  581. * @throws \think\db\exception\DataNotFoundException
  582. * @throws \think\db\exception\DbException
  583. * @throws \think\db\exception\ModelNotFoundException
  584. */
  585. public function refuseRefund(int $id, array $data, $orderRefundInfo = [])
  586. {
  587. if (!$orderRefundInfo) {
  588. $orderRefundInfo = $this->dao->get(['id' => $id, 'is_cancel' => 0]);
  589. }
  590. if (!$orderRefundInfo) {
  591. throw new ValidateException('售后订单不存在');
  592. }
  593. /** @var StoreOrderServices $storeOrderServices */
  594. $storeOrderServices = app()->make(StoreOrderServices::class);
  595. $this->transaction(function () use ($id, $data, $orderRefundInfo, $storeOrderServices) {
  596. //处理售后订单
  597. $this->dao->update($id, $data);
  598. //处理订单
  599. $oid = (int)$orderRefundInfo['store_order_id'];
  600. $storeOrderServices->update($oid, ['refund_status' => 0, 'refund_type' => 3]);
  601. //处理订单商品cart_info
  602. $this->cancelOrderRefundCartInfo($id, $oid, $orderRefundInfo);
  603. //记录
  604. /** @var StoreOrderStatusServices $statusService */
  605. $statusService = app()->make(StoreOrderStatusServices::class);
  606. $statusService->save([
  607. 'oid' => $id,
  608. 'change_type' => 'refund_n',
  609. 'change_message' => '不退款原因:' . ($data['refund_reason'] ?? ''),
  610. 'change_time' => time()
  611. ]);
  612. });
  613. event('notice.notice', [['orderInfo' => $orderRefundInfo], 'send_order_refund_no_status']);
  614. return true;
  615. }
  616. /**
  617. * 退积分表单创建
  618. * @param int $id
  619. * @return array
  620. * @throws \FormBuilder\Exception\FormBuilderException
  621. */
  622. public function refundIntegralForm(int $id)
  623. {
  624. if (!$orderInfo = $this->dao->get($id))
  625. throw new ValidateException('订单不存在');
  626. if ($orderInfo->use_integral < 0 || $orderInfo->use_integral == $orderInfo->back_integral)
  627. throw new ValidateException('积分已退或者积分为零无法再退');
  628. if (!$orderInfo->paid)
  629. throw new ValidateException('未支付无法退积分');
  630. $f[] = Form::input('order_id', '退款单号', $orderInfo->getData('order_id'))->disabled(1);
  631. $f[] = Form::number('use_integral', '使用的积分', (float)$orderInfo->getData('use_integral'))->min(0)->disabled(1);
  632. $f[] = Form::number('use_integrals', '已退积分', (float)$orderInfo->getData('back_integral'))->min(0)->disabled(1);
  633. $f[] = Form::number('back_integral', '可退积分', (float)bcsub($orderInfo->getData('use_integral'), $orderInfo->getData('back_integral')))->min(0)->precision(0)->required('请输入可退积分');
  634. return create_form('退积分', $f, $this->url('/order/refund_integral/' . $id), 'PUT');
  635. }
  636. /**
  637. * 单独退积分处理
  638. * @param $orderInfo
  639. * @param $back_integral
  640. */
  641. public function refundIntegral($orderInfo, $back_integral)
  642. {
  643. /** @var UserServices $userServices */
  644. $userServices = app()->make(UserServices::class);
  645. $integral = $userServices->value(['uid' => $orderInfo['uid']], 'integral');
  646. return $this->transaction(function () use ($userServices, $orderInfo, $back_integral, $integral) {
  647. $res1 = $userServices->bcInc($orderInfo['uid'], 'integral', $back_integral, 'uid');
  648. /** @var UserBillServices $userBillServices */
  649. $userBillServices = app()->make(UserBillServices::class);
  650. $res2 = $userBillServices->income('pay_product_integral_back', $orderInfo['uid'], (int)$back_integral, $integral + $back_integral, $orderInfo['id']);
  651. /** @var StoreOrderStatusServices $statusService */
  652. $statusService = app()->make(StoreOrderStatusServices::class);
  653. $res3 = $statusService->save([
  654. 'oid' => $orderInfo['id'],
  655. 'change_type' => 'integral_back',
  656. 'change_message' => '商品退积分:' . $back_integral,
  657. 'change_time' => time()
  658. ]);
  659. $res4 = $orderInfo->save();
  660. $res = $res1 && $res2 && $res3 && $res4;
  661. if (!$res) {
  662. throw new ValidateException('订单退积分失败');
  663. }
  664. return true;
  665. });
  666. }
  667. /**
  668. * 订单申请退款
  669. * @param $uni
  670. * @param $uid
  671. * @param string $refundReasonWap
  672. * @param string $refundReasonWapExplain
  673. * @param array $refundReasonWapImg
  674. * @return bool|void
  675. */
  676. public function orderApplyRefund($order, string $refundReasonWap = '', string $refundReasonWapExplain = '', array $refundReasonWapImg = [], int $refundType = 0, $cart_id = 0, $refund_num = 0)
  677. {
  678. if (!$order) {
  679. throw new ValidateException('支付订单不存在!');
  680. }
  681. if ($order['refund_status'] == 2) {
  682. throw new ValidateException('订单已退款!');
  683. }
  684. if ($order['refund_status'] == 1) {
  685. throw new ValidateException('正在申请退款中!');
  686. }
  687. if ($order['total_num'] < $refund_num) {
  688. throw new ValidateException('退款件数大于订单件数!');
  689. }
  690. $this->transaction(function () use ($order, $refundReasonWap, $refundReasonWapExplain, $refundReasonWapImg, $refundType, $refund_num, $cart_id) {
  691. $status = 0;
  692. $order_id = (int)$order['id'];
  693. if ($cart_id) {
  694. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  695. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  696. $cart_ids = [];
  697. $cart_ids[0] = ['cart_id' => $cart_id, 'cart_num' => $refund_num];
  698. /** @var StoreOrderSplitServices $storeOrderSplitServices */
  699. $storeOrderSplitServices = app()->make(StoreOrderSplitServices::class);
  700. //拆单
  701. $status = $order['status'];
  702. $order = $storeOrderSplitServices->split($order_id, $cart_ids, $order);
  703. } elseif (in_array($order['pid'], [0, -1]) && $this->storeOrderServices->count(['pid' => $order_id])) {
  704. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  705. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  706. $cart_info = $storeOrderCartInfoServices->getSplitCartList($order_id, 'cart_info');
  707. if (!$cart_info) {
  708. throw new ValidateException('该订单已全部拆分发货,请去自订单申请');
  709. }
  710. $cart_ids = [];
  711. foreach ($cart_info as $key => $cart) {
  712. $cart_ids[$key] = ['cart_id' => $cart['id'], 'cart_num' => $refund_num];
  713. }
  714. /** @var StoreOrderSplitServices $storeOrderSplitServices */
  715. $storeOrderSplitServices = app()->make(StoreOrderSplitServices::class);
  716. //拆单
  717. $status = $order['status'];
  718. $order = $storeOrderSplitServices->split($order_id, $cart_ids, $order);
  719. }
  720. $data = [
  721. 'refund_status' => 1,
  722. 'refund_reason_time' => time(),
  723. 'refund_reason_wap' => $refundReasonWap,
  724. 'refund_reason_wap_explain' => $refundReasonWapExplain,
  725. 'refund_reason_wap_img' => json_encode($refundReasonWapImg),
  726. 'refund_type' => $refundType
  727. ];
  728. if ($status) $data['status'] = $status;
  729. /** @var StoreOrderStatusServices $statusService */
  730. $statusService = app()->make(StoreOrderStatusServices::class);
  731. $res1 = false !== $statusService->save([
  732. 'oid' => $order['id'],
  733. 'change_type' => 'apply_refund',
  734. 'change_message' => '用户申请退款,原因:' . $refundReasonWap,
  735. 'change_time' => time()
  736. ]);
  737. $res2 = false !== $this->storeOrderServices->update(['id' => $order['id']], $data);
  738. $res = $res1 && $res2;
  739. if (!$res)
  740. throw new ValidateException('申请退款失败!');
  741. //子订单申请退款
  742. if ($order['pid'] > 0) {
  743. $p_order = $this->storeOrderServices->get((int)$order['pid']);
  744. $split_order = $this->storeOrderServices->count(['pid' => $order['pid'], 'refund_status' => 0]);
  745. if ($split_order || (!$split_order && $p_order['status'] == 4) || $cart_id) {
  746. $this->storeOrderServices->update(['id' => $order['pid']], ['refund_status' => 3, 'refund_reason_time' => time()]);
  747. } else {
  748. $this->storeOrderServices->update(['id' => $order['pid']], ['refund_status' => 4, 'refund_reason_time' => time()]);
  749. }
  750. } else {
  751. /** @var StoreOrderCartInfoServices $orderCartInfoService */
  752. $orderCartInfoService = app()->make(StoreOrderCartInfoServices::class);
  753. // if (!$orderCartInfoService->getSplitCartList()) {
  754. //
  755. // }
  756. }
  757. });
  758. try {
  759. ChannelService::instance()->send('NEW_REFUND_ORDER', ['order_id' => $order['order_id']]);
  760. } catch (\Exception $e) {
  761. }
  762. //提醒推送
  763. event('notice.notice', [['order' => $order], 'send_order_apply_refund']);
  764. return true;
  765. }
  766. /**
  767. * 写入退款快递单号
  768. * @param $order
  769. * @param $express
  770. * @return bool
  771. */
  772. public function editRefundExpress($data)
  773. {
  774. $this->transaction(function () use ($data) {
  775. $id = $data['id'];
  776. $data['refund_type'] = 5;
  777. /** @var StoreOrderStatusServices $statusService */
  778. $statusService = app()->make(StoreOrderStatusServices::class);
  779. $res1 = false !== $statusService->save([
  780. 'oid' => $id,
  781. 'change_type' => 'refund_express',
  782. 'change_message' => '用户已退货,订单号:' . $data['refund_express'],
  783. 'change_time' => time()
  784. ]);
  785. if ($data['refund_img'] != '') unset($data['refund_img']);
  786. if ($data['refund_explain'] != '') unset($data['refund_explain']);
  787. $res2 = false !== $this->dao->update(['id' => $id], $data);
  788. $res = $res1 && $res2;
  789. if (!$res)
  790. throw new ValidateException('提交失败!');
  791. });
  792. return true;
  793. }
  794. /**
  795. * 订单申请退款
  796. * @param int $id
  797. * @param int $uid
  798. * @param array $order
  799. * @param array $cart_ids
  800. * @param int $refundType
  801. * @param float $refundPrice
  802. * @param array $refundData
  803. * @param int $isPink
  804. * @return mixed
  805. * @throws \Psr\SimpleCache\InvalidArgumentException
  806. * @throws \think\db\exception\DataNotFoundException
  807. * @throws \think\db\exception\DbException
  808. * @throws \think\db\exception\ModelNotFoundException
  809. */
  810. public function applyRefund(int $id, int $uid, $order = [], array $cart_ids = [], int $refundType = 0, float $refundPrice = 0.00, array $refundData = [], $isPink = 0)
  811. {
  812. /** 查询订单是否存在 */
  813. /** @var StoreOrderServices $orderServices */
  814. $orderServices = app()->make(StoreOrderServices::class);
  815. if (!$order) {
  816. $order = $orderServices->get($id);
  817. }
  818. if (!$order) {
  819. throw new ValidateException('支付订单不存在!');
  820. }
  821. $is_now = $this->dao->getCount([
  822. ['store_order_id', '=', $id],
  823. ['refund_type', 'in', [1, 2, 4, 5]],
  824. ['is_cancel', '=', 0],
  825. ['is_del', '=', 0],
  826. ['is_pink_cancel', '=', 0]
  827. ]);
  828. if ($is_now) throw new ValidateException('存在待处理退款单,请稍后再试');
  829. $refund_num = $order['total_num'];
  830. $refund_price = $order['pay_price'];
  831. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  832. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  833. //退部分
  834. $cartInfo = [];
  835. $cartInfos = $storeOrderCartInfoServices->getCartColunm(['oid' => $id], 'id,cart_id,cart_num,refund_num,cart_info');
  836. if ($cart_ids) {
  837. $cartInfo = array_combine(array_column($cartInfos, 'cart_id'), $cartInfos);
  838. $refund_num = 0;
  839. foreach ($cart_ids as $cart) {
  840. if ($cart['cart_num'] + $cartInfo[$cart['cart_id']]['refund_num'] > $cartInfo[$cart['cart_id']]['cart_num']) {
  841. throw new ValidateException('超出订单中商品数量,请重新选择!');
  842. }
  843. $refund_num = bcadd((string)$refund_num, (string)$cart['cart_num'], 0);
  844. }
  845. //总共申请多少件
  846. $total_num = array_sum(array_column($cart_ids, 'cart_num'));
  847. if ($total_num < $order['total_num']) {
  848. /** @var StoreOrderSplitServices $storeOrderSpliteServices */
  849. $storeOrderSpliteServices = app()->make(StoreOrderSplitServices::class);
  850. $cartInfos = $storeOrderSpliteServices->getSplitOrderCartInfo($id, $cart_ids, $order);
  851. $total_price = $pay_postage = 0;
  852. foreach ($cartInfos as $cart) {
  853. $_info = is_string($cart['cart_info']) ? json_decode($cart['cart_info'], true) : $cart['cart_info'];
  854. $total_price = bcadd((string)$total_price, bcmul((string)($_info['truePrice'] ?? 0), (string)$cart['cart_num'], 4), 2);
  855. $pay_postage = bcadd((string)$pay_postage, (string)($_info['postage_price'] ?? 0), 2);
  856. }
  857. $refund_pay_price = bcadd((string)$total_price, (string)$pay_postage, 2);
  858. //订单实际支付金额
  859. $order_pay_price = bcsub((string)bcadd((string)$order['total_price'], (string)$order['pay_postage'], 2), (string)bcadd((string)$order['deduction_price'], (string)$order['coupon_price'], 2), 2);
  860. if ($order_pay_price != $order['pay_price'] && $refund_pay_price != $order_pay_price) {//有改价
  861. $refund_price = bcmul((string)bcdiv((string)$order['pay_price'], (string)$order_pay_price, 4), (string)$refund_pay_price, 2);
  862. } else {
  863. $refund_price = $refund_pay_price;
  864. }
  865. }
  866. } else {
  867. foreach ($cartInfos as $cart) {
  868. if ($cart['refund_num'] > 0) {
  869. throw new ValidateException('超出订单中商品数量,请重新选择!');
  870. }
  871. }
  872. }
  873. foreach ($cartInfos as &$cart) {
  874. $cart['cart_info'] = is_string($cart['cart_info']) ? json_decode($cart['cart_info'], true) : $cart['cart_info'];
  875. }
  876. $refundData['uid'] = $uid;
  877. $refundData['store_id'] = $order['store_id'];
  878. $refundData['store_order_id'] = $id;
  879. $refundData['refund_num'] = $refund_num;
  880. $refundData['refund_type'] = $refundType;
  881. $refundData['refund_price'] = $refund_price;
  882. $refundData['order_id'] = app()->make(StoreOrderCreateServices::class)->getNewOrderId('');
  883. $refundData['add_time'] = time();
  884. $refundData['cart_info'] = json_encode(array_column($cartInfos, 'cart_info'));
  885. $refundData['is_pink_cancel'] = $isPink;
  886. $res = $this->transaction(function () use ($id, $order, $cart_ids, $refundData, $storeOrderCartInfoServices, $cartInfo, $orderServices, $cartInfos) {
  887. /** @var StoreOrderStatusServices $statusService */
  888. $statusService = app()->make(StoreOrderStatusServices::class);
  889. $res1 = false !== $statusService->save([
  890. 'oid' => $order['id'],
  891. 'change_type' => 'apply_refund',
  892. 'change_message' => '用户申请退款,原因:' . $refundData['refund_reason'],
  893. 'change_time' => time()
  894. ]);
  895. $res2 = true;
  896. //添加退款数据
  897. /** @var StoreOrderRefundServices $storeOrderRefundServices */
  898. $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
  899. $res3 = $storeOrderRefundServices->save($refundData);
  900. if (!$res3) {
  901. throw new ValidateException('添加退款申请失败');
  902. }
  903. $res4 = true;
  904. if ($cart_ids) {
  905. //修改订单商品退款信息
  906. foreach ($cart_ids as $cart) {
  907. $res4 = $res4 && $storeOrderCartInfoServices->update(['oid' => $id, 'cart_id' => $cart['cart_id']], ['refund_num' => (($cartInfo[$cart['cart_id']]['refund_num'] ?? 0) + $cart['cart_num'])]);
  908. }
  909. } else {
  910. //修改原订单状态
  911. $res2 = false !== $orderServices->update(['id' => $order['id']], ['refund_status' => 1]);
  912. foreach ($cartInfos as $cart) {
  913. $res4 = $res4 && $storeOrderCartInfoServices->update(['oid' => $id, 'cart_id' => $cart['cart_id']], ['refund_num' => $cart['cart_num']]);
  914. }
  915. }
  916. return $res1 && $res2 && $res3 && $res4;
  917. });
  918. $storeOrderCartInfoServices->clearOrderCartInfo($order['id']);
  919. //申请退款事件
  920. event('order.orderRefund', [$order]);
  921. return $res;
  922. }
  923. /**
  924. * 获取某个字段总金额
  925. * @param $cartInfo
  926. * @param string $key
  927. * @param bool $is_unit
  928. * @return int|string
  929. */
  930. public function getOrderSumPrice($cartInfo, $key = 'truePrice', $is_unit = true)
  931. {
  932. $SumPrice = 0;
  933. foreach ($cartInfo as $cart) {
  934. if (isset($cart['cart_info'])) $cart = $cart['cart_info'];
  935. if ($is_unit) {
  936. $SumPrice = bcadd($SumPrice, bcmul($cart['cart_num'] ?? 1, $cart[$key] ?? 0, 2), 2);
  937. } else {
  938. $SumPrice = bcadd($SumPrice, $cart[$key] ?? 0, 2);
  939. }
  940. }
  941. return $SumPrice;
  942. }
  943. /**
  944. * 退款订单列表
  945. * @param $where
  946. * @return array
  947. */
  948. public function refundList($where)
  949. {
  950. [$page, $limit] = $this->getPageValue();
  951. $list = $this->dao->getList($where, $page, $limit);
  952. $count = $this->dao->count($where);
  953. if ($list) {
  954. foreach ($list as &$item) {
  955. $item['paid'] = 1;
  956. $item['add_time'] = isset($item['add_time']) ? date('Y-m-d H:i', (int)$item['add_time']) : '';
  957. $item['cartInfo'] = $item['cart_info'];
  958. if (in_array($item['refund_type'], [1, 2, 4, 5])) {
  959. $item['refund_status'] = 1;
  960. } elseif ($item['refund_type'] == 6) {
  961. $item['refund_status'] = 2;
  962. } elseif ($item['refund_type'] == 3) {
  963. $item['refund_status'] = 3;
  964. }
  965. foreach ($item['cart_info'] as $items) {
  966. $item['_info'][]['cart_info'] = $items;
  967. }
  968. $item['total_num'] = $item['refund_num'];
  969. $item['pay_price'] = $item['refund_price'];
  970. $item['pay_postage'] = floatval($this->getOrderSumPrice($item['cart_info'], 'postage_price', false));
  971. if (in_array($item['refund_type'], [1, 2, 4, 5])) {
  972. $_type = -1;
  973. $_title = '申请退款中';
  974. } elseif ($item['refund_type'] == 3) {
  975. $_type = -3;
  976. $_title = '拒绝退款';
  977. } else {
  978. $_type = -2;
  979. $_title = '已退款';
  980. }
  981. $item['_status'] = [
  982. '_type' => $_type,
  983. '_title' => $_title,
  984. ];
  985. }
  986. }
  987. $data['list'] = $list;
  988. $data['count'] = $count;
  989. $del_where = ['is_cancel' => 0];
  990. $data['num'] = [
  991. 0 => ['name' => '全部', 'num' => $this->dao->count($del_where)],
  992. 1 => ['name' => '仅退款', 'num' => $this->dao->count($del_where + ['refund_type' => 1])],
  993. 2 => ['name' => '退货退款', 'num' => $this->dao->count($del_where + ['refund_type' => 2])],
  994. 3 => ['name' => '拒绝退款', 'num' => $this->dao->count($del_where + ['refund_type' => 3])],
  995. 4 => ['name' => '商品待退货', 'num' => $this->dao->count($del_where + ['refund_type' => 4])],
  996. 5 => ['name' => '退货待收货', 'num' => $this->dao->count($del_where + ['refund_type' => 5])],
  997. 6 => ['name' => '已退款', 'num' => $this->dao->count($del_where + ['refund_type' => 6])]
  998. ];
  999. return $data;
  1000. }
  1001. /**
  1002. * 退款订单详情
  1003. * @param $uni
  1004. * @return mixed
  1005. */
  1006. public function refundDetail($uni)
  1007. {
  1008. if (!strlen(trim($uni))) throw new ValidateException('参数错误');
  1009. $order = $this->dao->get(['order_id' => $uni], ['*']);
  1010. if (!$order) throw new ValidateException('订单不存在');
  1011. $order = $order->toArray();
  1012. /** @var StoreOrderServices $orderServices */
  1013. $orderServices = app()->make(StoreOrderServices::class);
  1014. $orderInfo = $orderServices->get($order['store_order_id']);
  1015. /** @var UserServices $userServices */
  1016. $userServices = app()->make(UserServices::class);
  1017. $userInfo = $userServices->get($order['uid']);
  1018. $order['mapKey'] = sys_config('tengxun_map_key');
  1019. $order['yue_pay_status'] = (int)sys_config('balance_func_status') && (int)sys_config('yue_pay_status') == 1 ? (int)1 : (int)2;//余额支付 1 开启 2 关闭
  1020. $order['pay_weixin_open'] = (int)sys_config('pay_weixin_open') ?? 0;//微信支付 1 开启 0 关闭
  1021. $order['ali_pay_status'] = sys_config('ali_pay_status') ? true : false;//支付包支付 1 开启 0 关闭
  1022. $orderData = $order;
  1023. $orderData['store_order_sn'] = $orderInfo['order_id'];
  1024. $orderData['cartInfo'] = $orderData['cart_info'];
  1025. //核算优惠金额
  1026. $vipTruePrice = 0;
  1027. $total_price = 0;
  1028. foreach ($orderData['cartInfo'] ?? [] as $key => &$cart) {
  1029. if (!isset($cart['sum_true_price'])) $cart['sum_true_price'] = bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
  1030. $cart['vip_sum_truePrice'] = bcmul($cart['vip_truePrice'], $cart['cart_num'] ? $cart['cart_num'] : 1, 2);
  1031. $vipTruePrice = bcadd((string)$vipTruePrice, (string)$cart['vip_sum_truePrice'], 2);
  1032. if (isset($order['split']) && $order['split']) {
  1033. $orderData['cartInfo'][$key]['cart_num'] = $cart['surplus_num'];
  1034. if (!$cart['surplus_num']) unset($orderData['cartInfo'][$key]);
  1035. }
  1036. $total_price = bcadd($total_price, $cart['sum_true_price'], 2);
  1037. }
  1038. $orderData['use_integral'] = $this->getOrderSumPrice($orderData['cartInfo'], 'use_integral', false);
  1039. $orderData['integral_price'] = $this->getOrderSumPrice($orderData['cartInfo'], 'integral_price', false);
  1040. $orderData['coupon_price'] = $this->getOrderSumPrice($orderData['cartInfo'], 'coupon_price', false);
  1041. $orderData['deduction_price'] = $this->getOrderSumPrice($orderData['cartInfo'], 'integral_price', false);
  1042. $total_price = bcadd((string)$total_price, (string)bcadd((string)$orderData['deduction_price'], (string)$orderData['coupon_price'], 2), 2);
  1043. $orderData['vip_true_price'] = $vipTruePrice;
  1044. $orderData['postage_price'] = 0;
  1045. $orderData['pay_postage'] = $this->getOrderSumPrice($orderData['cart_info'], 'postage_price', false);
  1046. $orderData['member_price'] = 0;
  1047. $orderData['routine_contact_type'] = sys_config('routine_contact_type', 0);
  1048. switch ($orderInfo['pay_type']) {
  1049. case PayServices::WEIXIN_PAY:
  1050. $pay_type_name = '微信支付';
  1051. break;
  1052. case PayServices::YUE_PAY:
  1053. $pay_type_name = '余额支付';
  1054. break;
  1055. case PayServices::OFFLINE_PAY:
  1056. $pay_type_name = '线下支付';
  1057. break;
  1058. case PayServices::ALIAPY_PAY:
  1059. $pay_type_name = '支付宝支付';
  1060. break;
  1061. default:
  1062. $pay_type_name = '其他支付';
  1063. break;
  1064. }
  1065. $orderData['_add_time'] = date('Y-m-d H:i:s', $orderData['add_time']);
  1066. $orderData['add_time_y'] = date('Y-m-d', $orderData['add_time']);
  1067. $orderData['add_time_h'] = date('H:i:s', $orderData['add_time']);
  1068. if (in_array($orderData['refund_type'], [1, 2, 4, 5])) {
  1069. $_type = -1;
  1070. $_msg = '商家审核中,请耐心等待';
  1071. $_title = '申请退款中';
  1072. } elseif ($orderData['refund_type'] == 3) {
  1073. $_type = -3;
  1074. $_title = '拒绝退款';
  1075. $_msg = '商家拒绝退款,请联系商家';
  1076. } else {
  1077. $_type = -2;
  1078. $_title = '已退款';
  1079. $_msg = '已为您退款,感谢您的支持';
  1080. }
  1081. $refund_name = sys_config('refund_name', '');
  1082. $refund_phone = sys_config('refund_phone', '');
  1083. $refund_address = sys_config('refund_address', '');
  1084. $orderData['_status'] = [
  1085. '_type' => $_type,
  1086. '_title' => $_title,
  1087. '_msg' => $_msg ?? '',
  1088. '_payType' => $pay_type_name,
  1089. 'refund_name' => $refund_name,
  1090. 'refund_phone' => $refund_phone,
  1091. 'refund_address' => $refund_address,
  1092. ];
  1093. $orderData['real_name'] = $orderInfo['real_name'];
  1094. $orderData['user_phone'] = $orderInfo['user_phone'];
  1095. $orderData['user_address'] = $orderInfo['user_address'];
  1096. $orderData['nickname'] = $userInfo['nickname'] ?? '';
  1097. $orderData['total_num'] = $orderData['refund_num'];
  1098. $orderData['pay_price'] = $orderData['refund_price'];
  1099. $orderData['refund_status'] = in_array($orderData['refund_type'], [1, 2, 4, 5]) ? 1 : 2;
  1100. $orderData['total_price'] = $total_price;
  1101. $orderData['paid'] = 1;
  1102. $orderData['mark'] = $orderData['refund_explain'];
  1103. $orderData['express_list'] = $orderData['refund_type'] == 4 ? app()->make(ExpressServices::class)->expressList(['is_show' => 1]) : [];
  1104. $orderData['custom_form'] = [];
  1105. $orderData['help_info'] = [
  1106. 'pay_uid' => $orderInfo['pay_uid'],
  1107. 'pay_nickname' => '',
  1108. 'pay_avatar' => '',
  1109. 'help_status' => 0
  1110. ];
  1111. if ($orderInfo['uid'] != $orderInfo['pay_uid']) {
  1112. /** @var UserServices $userServices */
  1113. $userServices = app()->make(UserServices::class);
  1114. $payUser = $userServices->get($orderInfo['pay_uid']);
  1115. $orderData['help_info'] = [
  1116. 'pay_uid' => $orderInfo['pay_uid'],
  1117. 'pay_nickname' => $payUser['nickname'],
  1118. 'pay_avatar' => $payUser['avatar'],
  1119. 'help_status' => 1
  1120. ];
  1121. }
  1122. return $orderData;
  1123. }
  1124. /**
  1125. * 取消申请、后台拒绝处理cart_info refund_num数据
  1126. * @param int $id
  1127. * @param int $oid
  1128. * @param array $orderRefundInfo
  1129. * @return bool
  1130. * @throws \think\db\exception\DataNotFoundException
  1131. * @throws \think\db\exception\DbException
  1132. * @throws \think\db\exception\ModelNotFoundException
  1133. */
  1134. public function cancelOrderRefundCartInfo(int $id, int $oid, $orderRefundInfo = [])
  1135. {
  1136. if (!$orderRefundInfo) {
  1137. $orderRefundInfo = $this->dao->get(['id' => $id, 'is_cancel' => 0]);
  1138. }
  1139. if (!$orderRefundInfo) {
  1140. throw new ValidateException('售后订单不存在');
  1141. }
  1142. $cart_ids = array_column($orderRefundInfo['cart_info'], 'id');
  1143. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  1144. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  1145. $cartInfos = $storeOrderCartInfoServices->getColumn([['oid', '=', $oid], ['cart_id', 'in', $cart_ids]], 'cart_id,refund_num', 'cart_id');
  1146. foreach ($orderRefundInfo['cart_info'] as $cart) {
  1147. $cart_refund_num = $cartInfos[$cart['id']]['refund_num'] ?? 0;
  1148. if ($cart['cart_num'] >= $cart_refund_num) {
  1149. $refund_num = 0;
  1150. } else {
  1151. $refund_num = bcsub((string)$cart_refund_num, (string)$cart['cart_num'], 0);
  1152. }
  1153. $storeOrderCartInfoServices->update(['oid' => $oid, 'cart_id' => $cart['id']], ['refund_num' => $refund_num]);
  1154. }
  1155. $storeOrderCartInfoServices->clearOrderCartInfo($oid);
  1156. return true;
  1157. }
  1158. }