StoreOrderRefundServices.php 55 KB

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