StoreOrderRefundServices.php 59 KB

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