StoreOrderRefundServices.php 62 KB

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