StoreOrderRefundServices.php 54 KB

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