StoreOrderRefundServices.php 58 KB

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