StoreOrderRefundServices.php 65 KB

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