StoreOrderRefundServices.php 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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\discounts\StoreDiscountsServices;
  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\newcomer\StoreNewcomerServices;
  18. use app\services\activity\seckill\StoreSeckillServices;
  19. use app\services\BaseServices;
  20. use app\services\activity\coupon\StoreCouponUserServices;
  21. use app\services\other\ExpressServices;
  22. use app\services\pay\PayServices;
  23. use app\services\product\product\StoreProductServices;
  24. use app\services\store\SystemStoreServices;
  25. use app\services\supplier\SystemSupplierServices;
  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 app\services\wechat\WechatUserServices;
  31. use crmeb\services\AliPayService;
  32. use crmeb\services\CacheService;
  33. use crmeb\services\FormBuilder as Form;
  34. use crmeb\services\wechat\Payment;
  35. use crmeb\traits\ServicesTrait;
  36. use think\exception\ValidateException;
  37. /**
  38. * 订单退款
  39. * Class StoreOrderRefundServices
  40. * @package app\services\order
  41. * @mixin StoreOrderRefundDao
  42. */
  43. class StoreOrderRefundServices extends BaseServices
  44. {
  45. use ServicesTrait;
  46. /**
  47. * 订单services
  48. * @var StoreOrderServices
  49. */
  50. protected $storeOrderServices;
  51. /**
  52. * 构造方法
  53. * StoreOrderRefundServices constructor.
  54. * @param StoreOrderRefundDao $dao
  55. * @param StoreOrderServices $storeOrderServices
  56. */
  57. public function __construct(StoreOrderRefundDao $dao, StoreOrderServices $storeOrderServices)
  58. {
  59. $this->dao = $dao;
  60. $this->storeOrderServices = $storeOrderServices;
  61. }
  62. /**
  63. * 退款订单列表
  64. * @param array $where
  65. * @param array $with
  66. * @return array
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\DbException
  69. * @throws \think\db\exception\ModelNotFoundException
  70. */
  71. public function refundList(array $where, array $with = ['user'])
  72. {
  73. $where['is_cancel'] = 0;
  74. $where['store_id'] = isset($where['store_id']) ? $where['store_id'] : 0;
  75. if (isset($where['time']) && $where['time'] != '') {
  76. $where['time'] = is_string($where['time']) ? explode('-', $where['time']) : $where['time'];
  77. }
  78. [$page, $limit] = $this->getPageValue();
  79. $with = array_merge($with, ['order' => function ($query) {
  80. $query->field('id,shipping_type')->bind(['shipping_type']);
  81. }]);
  82. $list = $this->dao->getRefundList($where, '*', $with, $page, $limit);
  83. $count = $this->dao->count($where);
  84. if ($list) {
  85. foreach ($list as &$item) {
  86. $item['refund'] = [];
  87. $item['is_all_refund'] = 1;
  88. $item['paid'] = 1;
  89. $item['add_time'] = isset($item['add_time']) ? date('Y-m-d H:i', (int)$item['add_time']) : '';
  90. $item['cartInfo'] = $item['cart_info'];
  91. if (in_array($item['refund_type'], [1, 2, 4, 5])) {
  92. $item['refund_status'] = 1;
  93. } elseif ($item['refund_type'] == 6) {
  94. $item['refund_status'] = 2;
  95. } elseif ($item['refund_type'] == 3) {
  96. $item['refund_status'] = 3;
  97. }
  98. foreach ($item['cart_info'] as $items) {
  99. $item['_info'][]['cart_info'] = $items;
  100. }
  101. $item['total_num'] = $item['refund_num'];
  102. $item['pay_price'] = $item['refund_price'];
  103. $item['pay_postage'] = 0;
  104. if (isset($item['shipping_type']) && !in_array($item['shipping_type'], [2, 4])) {
  105. $item['pay_postage'] = floatval($this->getOrderSumPrice($item['cart_info'], 'postage_price', false));
  106. }
  107. $item['status_name'] = [
  108. 'pic' => [],
  109. 'status_name' => ''
  110. ];
  111. unset($item['cart_info']);
  112. if (in_array($item['refund_type'], [1, 2, 4, 5])) {
  113. $_type = -1;
  114. $_title = '申请退款中';
  115. if ($item['refund_type'] == 1) {
  116. $item['status_name']['status_name'] = '仅退款';
  117. } elseif ($item['refund_type'] == 2) {
  118. $item['status_name']['status_name'] = '退货退款';
  119. } elseif ($item['refund_type'] == 4) {
  120. $item['status_name']['status_name'] = '等待用户退货';
  121. } elseif ($item['refund_type'] == 5) {
  122. $item['status_name']['status_name'] = '商家待收货';
  123. }
  124. } elseif ($item['refund_type'] == 3) {
  125. $_type = -3;
  126. $_title = '拒绝退款';
  127. $item['status_name']['status_name'] = '拒绝退款';
  128. } else {
  129. $_type = -2;
  130. $_title = '已退款';
  131. $item['status_name']['status_name'] = '已退款';
  132. }
  133. $item['_status'] = [
  134. '_type' => $_type,
  135. '_title' => $_title,
  136. ];
  137. }
  138. }
  139. $data['list'] = $list;
  140. $data['count'] = $count;
  141. $supplierId = $where['supplier_id'] ?? 0;
  142. if ($supplierId) {
  143. $del_where = ['supplier_id' => $supplierId, 'is_cancel' => 0];
  144. } else {
  145. $del_where = ['store_id' => $where['store_id'], 'is_cancel' => 0];
  146. }
  147. $data['num'] = [
  148. 0 => ['name' => '全部', 'num' => $this->dao->count($del_where)],
  149. 1 => ['name' => '仅退款', 'num' => $this->dao->count($del_where + ['refund_type' => 1])],
  150. 2 => ['name' => '退货退款', 'num' => $this->dao->count($del_where + ['refund_type' => 2])],
  151. 3 => ['name' => '拒绝退款', 'num' => $this->dao->count($del_where + ['refund_type' => 3])],
  152. 4 => ['name' => '商品待退货', 'num' => $this->dao->count($del_where + ['refund_type' => 4])],
  153. 5 => ['name' => '退货待收货', 'num' => $this->dao->count($del_where + ['refund_type' => 5])],
  154. 6 => ['name' => '已退款', 'num' => $this->dao->count($del_where + ['refund_type' => 6])]
  155. ];
  156. return $data;
  157. }
  158. /**
  159. * 前端订单列表
  160. * @param array $where
  161. * @param array|string[] $field
  162. * @param array $with
  163. * @return mixed
  164. */
  165. public function getRefundOrderList(array $where, string $field = '*', array $with = [])
  166. {
  167. [$page, $limit] = $this->getPageValue();
  168. $where['is_cancel'] = 0;
  169. $where['is_del'] = 0;
  170. $data = $this->dao->getRefundList($where, $field, $with, $page, $limit);
  171. foreach ($data as &$item) {
  172. $item['add_time'] = isset($item['add_time']) ? date('Y-m-d H:i', (int)$item['add_time']) : '';
  173. $item['cartInfo'] = $item['cart_info'];
  174. unset($item['cart_info']);
  175. }
  176. return $data;
  177. }
  178. /**
  179. * 订单申请退款
  180. * @param int $id
  181. * @param int $uid
  182. * @param array $order
  183. * @param array $cart_ids
  184. * @param int $refundType
  185. * @param float $refundPrice
  186. * @param array $refundData
  187. * @return mixed
  188. * @throws \Psr\SimpleCache\InvalidArgumentException
  189. * @throws \think\db\exception\DataNotFoundException
  190. * @throws \think\db\exception\DbException
  191. * @throws \think\db\exception\ModelNotFoundException
  192. */
  193. public function applyRefund(int $id, int $uid, $order = [], array $cart_ids = [], int $refundType = 0, float $refundPrice = 0.00, array $refundData = [], int $origin = 0, bool $isSync = true)
  194. {
  195. if (!$order) {
  196. $order = $this->storeOrderServices->get($id);
  197. }
  198. if (!$order) {
  199. throw new ValidateException('支付订单不存在!');
  200. }
  201. if (!sys_config('erp_open')) {
  202. $is_now = $this->dao->getCount([
  203. ['store_order_id', '=', $id],
  204. ['refund_type', 'in', [1, 2, 4, 5]],
  205. ['is_cancel', '=', 0],
  206. ['is_del', '=', 0]
  207. ]);
  208. if ($is_now) throw new ValidateException('退款处理中,请联系商家');
  209. }
  210. $refund_num = $order['total_num'];
  211. $refund_price = $order['pay_price'];
  212. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  213. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  214. //退部分
  215. $cartInfo = [];
  216. $cartInfos = $storeOrderCartInfoServices->getCartColunm(['oid' => $id], 'id,cart_id,product_type,is_support_refund,cart_num,refund_num,cart_info');
  217. if ($cart_ids) {
  218. $cartInfo = array_combine(array_column($cartInfos, 'cart_id'), $cartInfos);
  219. $refund_num = 0;
  220. foreach ($cart_ids as $cart) {
  221. if (!isset($cartInfo[$cart['cart_id']])) throw new ValidateException('该订单中商品不存在,请重新选择!');
  222. if (!$cartInfo[$cart['cart_id']]['is_support_refund'] && $origin == 0) {
  223. throw new ValidateException('该订单中有商品不支持退款,请联系管理员');
  224. }
  225. if ($cart['cart_num'] + $cartInfo[$cart['cart_id']]['refund_num'] > $cartInfo[$cart['cart_id']]['cart_num']) {
  226. throw new ValidateException('超出订单中商品数量,请重新选择!');
  227. }
  228. $refund_num = bcadd((string)$refund_num, (string)$cart['cart_num'], 0);
  229. }
  230. //总共申请多少件
  231. $total_num = array_sum(array_column($cart_ids, 'cart_num'));
  232. if ($total_num < $order['total_num']) {
  233. /** @var StoreOrderSplitServices $storeOrderSpliteServices */
  234. $storeOrderSpliteServices = app()->make(StoreOrderSplitServices::class);
  235. $cartInfos = $storeOrderSpliteServices->getSplitOrderCartInfo($id, $cart_ids, $order);
  236. $total_price = $pay_postage = 0;
  237. foreach ($cartInfos as $cart) {
  238. $_info = is_string($cart['cart_info']) ? json_decode($cart['cart_info'], true) : $cart['cart_info'];
  239. $total_price = bcadd((string)$total_price, bcmul((string)($_info['truePrice'] ?? 0), (string)$cart['cart_num'], 4), 2);
  240. if (!in_array($order['shipping_type'], [2, 4])) {
  241. $pay_postage = bcadd((string)$pay_postage, (string)($_info['postage_price'] ?? 0), 2);
  242. }
  243. }
  244. //实际退款金额
  245. $refund_pay_price = bcadd((string)$total_price, (string)$pay_postage, 2);
  246. if (isset($order['change_price']) && (float)$order['change_price']) {//有改价 且是拆分
  247. //订单原实际支付金额
  248. $order_pay_price = bcadd((string)$order['change_price'], (string)$order['pay_price'], 2);
  249. $refund_price = bcmul((string)bcdiv((string)$order['pay_price'], (string)$order_pay_price, 4), (string)$refund_pay_price, 2);
  250. } else {
  251. $refund_price = $refund_pay_price;
  252. }
  253. }
  254. } else {//整单退款
  255. foreach ($cartInfos as $cart) {
  256. if (!$cart['is_support_refund']) {
  257. throw new ValidateException('该订单中有商品不支持退款,请联系管理员');
  258. }
  259. if ($cart['refund_num'] > 0) {
  260. throw new ValidateException('超出订单中商品数量,请重新选择!');
  261. }
  262. }
  263. }
  264. foreach ($cartInfos as &$cart) {
  265. $cart['cart_info'] = is_string($cart['cart_info']) ? json_decode($cart['cart_info'], true) : $cart['cart_info'];
  266. }
  267. $refundData['uid'] = $uid;
  268. $refundData['store_id'] = $order['store_id'];
  269. $refundData['supplier_id'] = $order['supplier_id'];
  270. $refundData['store_order_id'] = $id;
  271. $refundData['refund_num'] = $refund_num;
  272. $refundData['refund_type'] = $refundType;
  273. $refundData['refund_price'] = $refund_price;
  274. $refundData['order_id'] = app()->make(StoreOrderCreateServices::class)->getNewOrderId('');
  275. $refundData['add_time'] = time();
  276. $refundData['cart_info'] = json_encode(array_column($cartInfos, 'cart_info'));
  277. $refundId = $this->transaction(function () use ($id, $order, $cart_ids, $refundData, $storeOrderCartInfoServices, $cartInfo, $cartInfos) {
  278. /** @var StoreOrderStatusServices $statusService */
  279. $statusService = app()->make(StoreOrderStatusServices::class);
  280. $res1 = false !== $statusService->save([
  281. 'oid' => $order['id'],
  282. 'change_type' => 'apply_refund',
  283. 'change_message' => '用户申请退款,原因:' . $refundData['refund_reason'],
  284. 'change_time' => time()
  285. ]);
  286. $res2 = true;
  287. //添加退款数据
  288. /** @var StoreOrderRefundServices $storeOrderRefundServices */
  289. $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
  290. $res3 = $storeOrderRefundServices->save($refundData);
  291. if (!$res3) {
  292. throw new ValidateException('添加退款申请失败');
  293. }
  294. $res4 = true;
  295. if ($cart_ids) {
  296. //修改订单商品退款信息
  297. foreach ($cart_ids as $cart) {
  298. $res4 = $res4 && $storeOrderCartInfoServices->update(['oid' => $id, 'cart_id' => $cart['cart_id']], ['refund_num' => (($cartInfo[$cart['cart_id']]['refund_num'] ?? 0) + $cart['cart_num'])]);
  299. }
  300. } else {//整单退款
  301. //修改原订单状态
  302. // $res2 = false !== $this->storeOrderServices->update(['id' => $order['id']], ['refund_status' => 1]);
  303. foreach ($cartInfos as $cart) {
  304. $res4 = $res4 && $storeOrderCartInfoServices->update(['oid' => $id, 'cart_id' => $cart['cart_id']], ['refund_num' => $cart['cart_num']]);
  305. }
  306. }
  307. if ($res1 && $res2 && $res3 && $res4) {
  308. return (int)$res3->id;
  309. } else {
  310. return false;
  311. }
  312. });
  313. $storeOrderCartInfoServices->clearOrderCartInfo($order['id']);
  314. //申请退款事件
  315. event('order.applyRefund', [$order, $refundId, $isSync]);
  316. return $refundId;
  317. }
  318. /**后台操作退款
  319. * @param int $id
  320. * @param $order
  321. * @param array $cart_ids
  322. * @param int $refundType
  323. * @param float $refundPrice
  324. * @param array $refundData
  325. * @param int $origin
  326. * @param bool $isSync
  327. * @return mixed
  328. * @throws \Psr\SimpleCache\InvalidArgumentException
  329. * @throws \think\db\exception\DataNotFoundException
  330. * @throws \think\db\exception\DbException
  331. * @throws \think\db\exception\ModelNotFoundException
  332. */
  333. public function splitApplyRefund(int $id, $order = [], array $cart_ids = [], int $refundType = 0, float $refundPrice = 0.00, array $refundData = [], int $origin = 0, bool $isSync = true)
  334. {
  335. if (!$order) {
  336. $order = $this->storeOrderServices->get($id);
  337. }
  338. if (!$order) {
  339. throw new ValidateException('支付订单不存在!');
  340. }
  341. if (!sys_config('erp_open')) {
  342. $is_now = $this->dao->getCount([
  343. ['store_order_id', '=', $id],
  344. ['refund_type', 'in', [1, 2, 4, 5]],
  345. ['is_cancel', '=', 0],
  346. ['is_del', '=', 0]
  347. ]);
  348. if ($is_now) throw new ValidateException('退款处理中,请联系商家');
  349. }
  350. $refund_num = $order['total_num'];
  351. $refund_price = $order['pay_price'];
  352. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  353. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  354. //退部分
  355. $cartInfo = [];
  356. $cartInfos = $storeOrderCartInfoServices->getCartColunm(['oid' => $id], 'id,cart_id,product_type,is_support_refund,cart_num,refund_num,cart_info');
  357. if ($cart_ids) {
  358. $cartInfo = array_combine(array_column($cartInfos, 'cart_id'), $cartInfos);
  359. $refund_num = 0;
  360. foreach ($cart_ids as $cart) {
  361. if (!isset($cartInfo[$cart['cart_id']])) throw new ValidateException('该订单中商品不存在,请重新选择!');
  362. if ($cart['cart_num'] + $cartInfo[$cart['cart_id']]['refund_num'] > $cartInfo[$cart['cart_id']]['cart_num']) {
  363. throw new ValidateException('超出订单中商品数量,请重新选择!');
  364. }
  365. $refund_num = bcadd((string)$refund_num, (string)$cart['cart_num'], 0);
  366. }
  367. //总共申请多少件
  368. $total_num = array_sum(array_column($cart_ids, 'cart_num'));
  369. if ($total_num < $order['total_num']) {
  370. /** @var StoreOrderSplitServices $storeOrderSpliteServices */
  371. $storeOrderSpliteServices = app()->make(StoreOrderSplitServices::class);
  372. $cartInfos = $storeOrderSpliteServices->getSplitOrderCartInfo($id, $cart_ids, $order);
  373. $total_price = $pay_postage = 0;
  374. foreach ($cartInfos as $cart) {
  375. $_info = is_string($cart['cart_info']) ? json_decode($cart['cart_info'], true) : $cart['cart_info'];
  376. $total_price = bcadd((string)$total_price, bcmul((string)($_info['truePrice'] ?? 0), (string)$cart['cart_num'], 4), 2);
  377. if (!in_array($order['shipping_type'], [2, 4])) {
  378. $pay_postage = bcadd((string)$pay_postage, (string)($_info['postage_price'] ?? 0), 2);
  379. }
  380. }
  381. //实际退款金额
  382. $refund_pay_price = bcadd((string)$total_price, (string)$pay_postage, 2);
  383. if (isset($order['change_price']) && (float)$order['change_price']) {//有改价 且是拆分
  384. //订单原实际支付金额
  385. $order_pay_price = bcadd((string)$order['change_price'], (string)$order['pay_price'], 2);
  386. $refund_price = bcmul((string)bcdiv((string)$order['pay_price'], (string)$order_pay_price, 4), (string)$refund_pay_price, 2);
  387. } else {
  388. $refund_price = $refund_pay_price;
  389. }
  390. }
  391. } else {//整单退款
  392. foreach ($cartInfos as $cart) {
  393. if ($cart['refund_num'] > 0) {
  394. throw new ValidateException('超出订单中商品数量,请重新选择!');
  395. }
  396. }
  397. }
  398. foreach ($cartInfos as &$cart) {
  399. $cart['cart_info'] = is_string($cart['cart_info']) ? json_decode($cart['cart_info'], true) : $cart['cart_info'];
  400. }
  401. $refundData['uid'] = $order['uid'];
  402. $refundData['store_id'] = $order['store_id'];
  403. $refundData['supplier_id'] = $order['supplier_id'];
  404. $refundData['store_order_id'] = $id;
  405. $refundData['refund_num'] = $refund_num;
  406. $refundData['refund_type'] = $refundType;
  407. $refundData['refund_price'] = $refund_price;
  408. $refundData['order_id'] = app()->make(StoreOrderCreateServices::class)->getNewOrderId('');
  409. $refundData['add_time'] = time();
  410. $refundData['cart_info'] = json_encode(array_column($cartInfos, 'cart_info'));
  411. $refundId = $this->transaction(function () use ($id, $order, $cart_ids, $refundData, $storeOrderCartInfoServices, $cartInfo, $cartInfos) {
  412. /** @var StoreOrderStatusServices $statusService */
  413. $statusService = app()->make(StoreOrderStatusServices::class);
  414. $res1 = false !== $statusService->save([
  415. 'oid' => $order['id'],
  416. 'change_type' => 'apply_refund',
  417. 'change_message' => '管理员操作退款',
  418. 'change_time' => time()
  419. ]);
  420. $res2 = true;
  421. //添加退款数据
  422. /** @var StoreOrderRefundServices $storeOrderRefundServices */
  423. $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
  424. $res3 = $storeOrderRefundServices->save($refundData);
  425. if (!$res3) {
  426. throw new ValidateException('添加退款申请失败');
  427. }
  428. $res4 = true;
  429. if ($cart_ids) {
  430. //修改订单商品退款信息
  431. foreach ($cart_ids as $cart) {
  432. $res4 = $res4 && $storeOrderCartInfoServices->update(['oid' => $id, 'cart_id' => $cart['cart_id']], ['refund_num' => (($cartInfo[$cart['cart_id']]['refund_num'] ?? 0) + $cart['cart_num'])]);
  433. }
  434. } else {//整单退款
  435. //修改原订单状态
  436. foreach ($cartInfos as $cart) {
  437. $res4 = $res4 && $storeOrderCartInfoServices->update(['oid' => $id, 'cart_id' => $cart['cart_id']], ['refund_num' => $cart['cart_num']]);
  438. }
  439. }
  440. if ($res1 && $res2 && $res3 && $res4) {
  441. return (int)$res3->id;
  442. } else {
  443. return false;
  444. }
  445. });
  446. $storeOrderCartInfoServices->clearOrderCartInfo($order['id']);
  447. return $refundId;
  448. }
  449. /**
  450. * 拒绝退款
  451. * @param int $id
  452. * @param array $data
  453. * @param array $orderRefundInfo
  454. * @return bool
  455. * @throws \think\db\exception\DataNotFoundException
  456. * @throws \think\db\exception\DbException
  457. * @throws \think\db\exception\ModelNotFoundException
  458. */
  459. public function refuseRefund(int $id, array $data, $orderRefundInfo = [])
  460. {
  461. if (!$orderRefundInfo) {
  462. $orderRefundInfo = $this->dao->get(['id' => $id, 'is_cancel' => 0]);
  463. }
  464. if (!$orderRefundInfo) {
  465. throw new ValidateException('售后订单不存在');
  466. }
  467. $this->transaction(function () use ($id, $orderRefundInfo, $data) {
  468. //处理售后订单
  469. if (isset($data['refund_price'])) unset($data['refund_price']);
  470. $this->dao->update($id, $data);
  471. //处理订单
  472. $oid = (int)$orderRefundInfo['store_order_id'];
  473. $this->storeOrderServices->update($oid, ['refund_status' => 0, 'refund_type' => 3]);
  474. //处理订单商品cart_info
  475. $this->cancelOrderRefundCartInfo($id, $oid, $orderRefundInfo);
  476. //记录
  477. /** @var StoreOrderStatusServices $statusService */
  478. $statusService = app()->make(StoreOrderStatusServices::class);
  479. $statusService->save([
  480. 'oid' => $id,
  481. 'change_type' => 'refund_n',
  482. 'change_message' => '不退款原因:' . ($data['refund_reason'] ?? $data['refuse_reason'] ?? ''),
  483. 'change_time' => time()
  484. ]);
  485. });
  486. $orderInfo = $this->storeOrderServices->get((int)$orderRefundInfo['store_order_id']);
  487. //订单拒绝退款事件
  488. event('order.refuseRefund', [$orderInfo]);
  489. return true;
  490. }
  491. /**
  492. * 取消申请、后台拒绝处理cart_info refund_num数据
  493. * @param int $id
  494. * @param int $oid
  495. * @param array $orderRefundInfo
  496. * @return bool
  497. * @throws \think\db\exception\DataNotFoundException
  498. * @throws \think\db\exception\DbException
  499. * @throws \think\db\exception\ModelNotFoundException
  500. */
  501. public function cancelOrderRefundCartInfo(int $id, int $oid, $orderRefundInfo = [])
  502. {
  503. if (!$orderRefundInfo) {
  504. $orderRefundInfo = $this->dao->get(['id' => $id, 'is_cancel' => 0]);
  505. }
  506. if (!$orderRefundInfo) {
  507. throw new ValidateException('售后订单不存在');
  508. }
  509. /** @var StoreOrderServices $storeOrderServices */
  510. $storeOrderServices = app()->make(StoreOrderServices::class);
  511. $orderInfo = $storeOrderServices->get($oid, ['id', 'type', 'pink_id', 'activity_id']);
  512. if (!$orderInfo) {
  513. throw new ValidateException('订单不存在');
  514. }
  515. //拼团订单处理拼团
  516. if ($orderInfo['type'] == 3 && $orderInfo['pink_id']) {
  517. /** @var StorePinkServices $pinkServices */
  518. $pinkServices = app()->make(StorePinkServices::class);
  519. $addTime = $pinkServices->value(['id' => $orderInfo['pink_id']], 'add_time');
  520. /** @var StoreCombinationServices $services */
  521. $services = app()->make(StoreCombinationServices::class);
  522. $product = $services->getOne(['id' => $orderInfo['activity_id']], 'effective_time,title,people');
  523. $pinkServices->update(['id' => $orderInfo['pink_id']], ['status' => 1, 'stop_time' => $addTime + $product['effective_time'] * 3600]);
  524. }
  525. $cart_ids = array_column($orderRefundInfo['cart_info'], 'id');
  526. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  527. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  528. $cartInfos = $storeOrderCartInfoServices->getColumn([['oid', '=', $oid], ['cart_id', 'in', $cart_ids]], 'cart_id,refund_num', 'cart_id');
  529. foreach ($orderRefundInfo['cart_info'] as $cart) {
  530. $cart_refund_num = $cartInfos[$cart['id']]['refund_num'] ?? 0;
  531. if ($cart['cart_num'] >= $cart_refund_num) {
  532. $refund_num = 0;
  533. } else {
  534. $refund_num = bcsub((string)$cart_refund_num, (string)$cart['cart_num'], 0);
  535. }
  536. $storeOrderCartInfoServices->update(['oid' => $oid, 'cart_id' => $cart['id']], ['refund_num' => $refund_num]);
  537. }
  538. $storeOrderCartInfoServices->clearOrderCartInfo($oid);
  539. return true;
  540. }
  541. /**
  542. * 商家同意退货退款,等待客户退货
  543. * @param int $id
  544. * @return bool
  545. */
  546. public function agreeRefundProdcut(int $id)
  547. {
  548. $res = $this->dao->update(['id' => $id], ['refund_type' => 4]);
  549. /** @var StoreOrderStatusServices $statusService */
  550. $statusService = app()->make(StoreOrderStatusServices::class);
  551. $statusService->save([
  552. 'oid' => $id,
  553. 'change_type' => 'refund_express',
  554. 'change_message' => '等待用户退货',
  555. 'change_time' => time()
  556. ]);
  557. if ($res) return true;
  558. throw new ValidateException('操作失败');
  559. }
  560. /**
  561. * 同意退款:拆分退款单、退积分、佣金等
  562. * @param int $id
  563. * @param array $refundData
  564. * @return bool
  565. * @throws \think\db\exception\DataNotFoundException
  566. * @throws \think\db\exception\DbException
  567. * @throws \think\db\exception\ModelNotFoundException
  568. */
  569. public function agreeRefund(int $id, array $refundData)
  570. {
  571. $order = $this->transaction(function () use ($id, $refundData) {
  572. //退款拆分
  573. $order = $this->agreeSplitRefundOrder($id);
  574. //回退积分和优惠卷
  575. if (!$this->integralAndCouponBack($order)) {
  576. throw new ValidateException('回退积分和优惠卷失败');
  577. }
  578. //退拼团
  579. if ($order['pid'] == 0 && $order['type'] == 3) {
  580. /** @var StorePinkServices $pinkServices */
  581. $pinkServices = app()->make(StorePinkServices::class);
  582. if (!$pinkServices->setRefundPink($order)) {
  583. throw new ValidateException('拼团修改失败!');
  584. }
  585. }
  586. //退佣金
  587. /** @var UserBrokerageServices $userBrokerageServices */
  588. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  589. if (!$userBrokerageServices->orderRefundBrokerageBack($order)) {
  590. throw new ValidateException('回退佣金失败');
  591. }
  592. //回退库存
  593. if ($order['status'] == 0) {
  594. /** @var StoreOrderStatusServices $services */
  595. $services = app()->make(StoreOrderStatusServices::class);
  596. if (!$services->count(['oid' => $order['id'], 'change_type' => 'refund_price'])) {
  597. $this->regressionStock($order);
  598. }
  599. }
  600. //退金额
  601. if ($refundData['refund_price'] > 0) {
  602. if (!isset($refundData['refund_id']) || !$refundData['refund_id']) {
  603. mt_srand();
  604. $refundData['refund_id'] = $order['order_id'] . rand(100, 999);
  605. }
  606. if ($order['pid'] > 0) {//子订单
  607. $refundOrder = $this->storeOrderServices->get((int)$order['pid']);
  608. $refundData['pay_price'] = $refundOrder['pay_price'];
  609. } else {
  610. $refundOrder = $order;
  611. }
  612. switch ($refundOrder['pay_type']) {
  613. case PayServices::WEIXIN_PAY:
  614. $no = $refundOrder['order_id'];
  615. if ($refundOrder['trade_no'] && $refundOrder['trade_no'] != $refundOrder['order_id']) {
  616. $no = $refundOrder['trade_no'];
  617. $refundData['type'] = 'trade_no';
  618. }
  619. if ($refundOrder['is_channel'] == 1) {
  620. //小程序退款
  621. //判断是不是小程序支付 TODO 之后可根据订单判断
  622. $pay_routine_open = (bool)sys_config('pay_routine_open', 0);
  623. if ($pay_routine_open) {
  624. $refundData['refund_no'] = $refundOrder['order_id']; // 退款订单号
  625. /** @var WechatUserServices $wechatUserServices */
  626. $wechatUserServices = app()->make(WechatUserServices::class);
  627. $refundData['open_id'] = $wechatUserServices->value(['uid' => (int)$order['uid']], 'openid');
  628. //判断订单是不是重新支付订单
  629. if (in_array(substr($refundOrder['unique'], 0, 2), ['wx', 'cp', 'hy', 'cz'])) {
  630. $refundData['routine_order_id'] = $refundOrder['unique'];
  631. } else {
  632. $refundData['routine_order_id'] = $refundOrder['order_id'];
  633. }
  634. $refundData['pay_routine_open'] = true;
  635. }
  636. Payment::instance()->setAccessEnd(Payment::MINI)->payOrderRefund($no, $refundData);//小程序
  637. } else {
  638. //微信公众号退款
  639. Payment::instance()->setAccessEnd(Payment::WEB)->payOrderRefund($no, $refundData);//公众号
  640. }
  641. break;
  642. case PayServices::YUE_PAY:
  643. //余额退款
  644. if (!$this->yueRefund($refundOrder, $refundData)) {
  645. throw new ValidateException('余额退款失败');
  646. }
  647. break;
  648. case PayServices::ALIAPY_PAY:
  649. mt_srand();
  650. $refund_id = $refundData['refund_id'] ?? $refundOrder['order_id'] . rand(100, 999);
  651. //支付宝退款
  652. AliPayService::instance()->refund(strpos($refundOrder['trade_no'], '_') !== false ? $refundOrder['trade_no'] : $refundOrder['order_id'], floatval($refundData['refund_price']), $refund_id);
  653. break;
  654. }
  655. }
  656. //订单记录
  657. /** @var StoreOrderStatusServices $statusService */
  658. $statusService = app()->make(StoreOrderStatusServices::class);
  659. $statusService->save([
  660. 'oid' => $order['id'],
  661. 'change_type' => 'refund_price',
  662. 'change_message' => '退款给用户:' . $refundData['refund_price'] . '元',
  663. 'change_time' => time()
  664. ]);
  665. return $order;
  666. });
  667. //订单同意退款事件
  668. event('order.refund', [$refundData, $order, 'order_refund']);
  669. return true;
  670. }
  671. /**
  672. * 处理退款 拆分订单
  673. * @param int $id
  674. * @param array $orderRefundInfo
  675. * @return bool
  676. * @throws \think\db\exception\DataNotFoundException
  677. * @throws \think\db\exception\DbException
  678. * @throws \think\db\exception\ModelNotFoundException
  679. */
  680. public function agreeSplitRefundOrder(int $id, $orderRefundInfo = [])
  681. {
  682. if (!$orderRefundInfo) {
  683. $orderRefundInfo = $this->dao->get($id);
  684. }
  685. if (!$orderRefundInfo) {
  686. throw new ValidateException('数据不存在');
  687. }
  688. $cart_ids = [];
  689. if ($orderRefundInfo['cart_info']) {
  690. foreach ($orderRefundInfo['cart_info'] as $cart) {
  691. $cart_ids[] = [
  692. 'cart_id' => $cart['id'],
  693. 'cart_num' => $cart['cart_num'],
  694. ];
  695. }
  696. }
  697. return $this->transaction(function () use ($orderRefundInfo, $cart_ids) {
  698. /** @var StoreOrderSplitServices $storeOrderSplitServices */
  699. $storeOrderSplitServices = app()->make(StoreOrderSplitServices::class);
  700. $oid = (int)$orderRefundInfo['store_order_id'];
  701. $splitResult = $storeOrderSplitServices->equalSplit($oid, $cart_ids, [], 0, true);
  702. $orderInfo = [];
  703. if ($splitResult) {//拆分发货
  704. [$orderInfo, $otherOrder] = $splitResult;
  705. }
  706. if ($orderInfo) {
  707. /** @var StoreOrderServices $storeOrderServices */
  708. $storeOrderServices = app()->make(StoreOrderServices::class);
  709. //原订单退款状态清空
  710. $storeOrderServices->update($oid, ['refund_status' => 0, 'refund_type' => 0]);
  711. //修改新生成拆分退款订单状态
  712. $storeOrderServices->update($orderInfo['id'], ['refund_status' => 2, 'refund_type' => 6]);
  713. //修改售后订单 关联退款订单
  714. $this->dao->update($orderRefundInfo['id'], ['store_order_id' => $orderInfo['id']]);
  715. if ($oid != $otherOrder['id']) {//拆分生成新订单了
  716. //修改原订单还在申请的退款单
  717. $this->dao->update(['store_order_id' => $oid], ['store_order_id' => $otherOrder['id']]);
  718. }
  719. $orderInfo = $storeOrderServices->get($orderInfo['id']);
  720. } else {//整单退款
  721. /** @var StoreOrderServices $storeOrderServices */
  722. $storeOrderServices = app()->make(StoreOrderServices::class);
  723. $storeOrderServices->update($oid, ['refund_status' => 2, 'refund_type' => 6]);
  724. //修改订单商品申请退款数量
  725. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  726. $storeOrderCartInfoServices = app()->make(StoreOrderCreateServices::class);
  727. $storeOrderCartInfoServices->update(['oid' => $oid], ['refund_num' => 0]);
  728. $orderInfo = $storeOrderServices->get($oid);
  729. }
  730. return $orderInfo;
  731. });
  732. }
  733. /**
  734. * 订单退款表单
  735. * @param int $id
  736. * @param string $type
  737. * @return mixed
  738. * @throws \think\db\exception\DataNotFoundException
  739. * @throws \think\db\exception\DbException
  740. * @throws \think\db\exception\ModelNotFoundException
  741. */
  742. public function refundOrderForm(int $id, string $type = 'refund')
  743. {
  744. if ($type == 'refund') {//售后订单
  745. $orderRefund = $this->dao->get($id);
  746. if (!$orderRefund) {
  747. throw new ValidateException('未查到订单');
  748. }
  749. $order = $this->storeOrderServices->get((int)$orderRefund['store_order_id']);
  750. if (!$order) {
  751. throw new ValidateException('未查到订单');
  752. }
  753. if (!$order['paid']) {
  754. throw new ValidateException('未支付无法退款');
  755. }
  756. if ($orderRefund['refund_price'] > 0 && in_array($orderRefund['refund_type'], [1, 5])) {
  757. if ($orderRefund['refund_price'] <= $orderRefund['refunded_price']) {
  758. throw new ValidateException('订单已退款');
  759. }
  760. }
  761. $f[] = Form::input('order_id', '退款单号', $orderRefund->getData('order_id'))->disabled(true);
  762. $f[] = Form::number('refund_price', '退款金额', (float)bcsub((string)$orderRefund->getData('refund_price'), (string)$orderRefund->getData('refunded_price'), 2))->min(0)->required('请输入退款金额');
  763. return create_form('退款处理', $f, $this->url('/refund/refund/' . $id), 'PUT');
  764. } else {//订单主动退款
  765. $order = $this->storeOrderServices->get((int)$id);
  766. if (!$order) {
  767. throw new ValidateException('未查到订单');
  768. }
  769. if (!$order['paid']) {
  770. throw new ValidateException('未支付无法退款');
  771. }
  772. if ($order['pay_price'] > 0 && in_array($order['refund_status'], [0, 1])) {
  773. if ($order['pay_price'] <= $order['refund_price']) {
  774. throw new ValidateException('订单已退款');
  775. }
  776. }
  777. if ($order['pid'] >= 0) {//未拆分主订单、已拆分子订单
  778. /** @var StoreOrderRefundServices $storeOrderRefundServices */
  779. $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
  780. if ($storeOrderRefundServices->count(['store_order_id' => $id, 'refund_type' => [1, 2, 4, 5, 6], 'is_cancel' => 0, 'is_del' => 0])) {
  781. throw new ValidateException('请到售后订单列表处理');
  782. }
  783. } else {//已拆分发货
  784. throw new ValidateException('主订单已拆分发货,暂不支持整单主动退款');
  785. }
  786. $f[] = Form::input('order_id', '退款单号', $order->getData('order_id'))->disabled(true);
  787. $f[] = Form::number('refund_price', '退款金额', (float)bcsub((string)$order->getData('pay_price'), (string)$order->getData('refund_price'), 2))->min(0)->required('请输入退款金额');
  788. return create_form('退款处理', $f, $this->url('/order/refund/' . $id), 'PUT');
  789. }
  790. }
  791. /**
  792. * 余额退款
  793. * @param $order
  794. * @param array $refundData
  795. * @return bool
  796. */
  797. public function yueRefund($order, array $refundData)
  798. {
  799. /** @var UserServices $userServices */
  800. $userServices = app()->make(UserServices::class);
  801. if (!$userServices->getUserInfo($order['uid'])) {
  802. return false;
  803. }
  804. $usermoney = $userServices->value(['uid' => $order['uid']], 'now_money');
  805. $res = $userServices->bcInc($order['uid'], 'now_money', $refundData['refund_price'], 'uid');
  806. /** @var UserMoneyServices $userMoneyServices */
  807. $userMoneyServices = app()->make(UserMoneyServices::class);
  808. return $res && $userMoneyServices->income('pay_product_refund', $order['uid'], $refundData['refund_price'], bcadd((string)$usermoney, (string)$refundData['refund_price'], 2), $order['id']);
  809. }
  810. /**
  811. * 回退积分和优惠卷
  812. * @param $order
  813. * @return bool
  814. */
  815. public function integralAndCouponBack($order)
  816. {
  817. $res = true;
  818. //回退优惠卷 拆分子订单不退优惠券
  819. if (!$order['pid'] && $order['coupon_id'] && $order['coupon_price']) {
  820. /** @var StoreCouponUserServices $coumonUserServices */
  821. $coumonUserServices = app()->make(StoreCouponUserServices::class);
  822. $res = $res && $coumonUserServices->recoverCoupon((int)$order['coupon_id']);
  823. }
  824. //回退积分
  825. [$order, $changeIntegral] = $this->regressionIntegral($order);
  826. /** @var StoreOrderStatusServices $statusService */
  827. $statusService = app()->make(StoreOrderStatusServices::class);
  828. $statusService->save([
  829. 'oid' => $order['id'],
  830. 'change_type' => 'integral_back',
  831. 'change_message' => '商品退积分:' . $changeIntegral,
  832. 'change_time' => time()
  833. ]);
  834. return $res && $order->save();
  835. }
  836. /**
  837. * 回退使用积分和赠送积分
  838. * @param $order
  839. * @return array
  840. * @throws \think\db\exception\DataNotFoundException
  841. * @throws \think\db\exception\DbException
  842. * @throws \think\db\exception\ModelNotFoundException
  843. */
  844. public function regressionIntegral($order)
  845. {
  846. /** @var UserServices $userServices */
  847. $userServices = app()->make(UserServices::class);
  848. $userInfo = $userServices->get($order['uid'], ['integral']);
  849. if (!$userInfo) {
  850. $order->back_integral = $order->use_integral;
  851. return [$order, 0];
  852. }
  853. $integral = $userInfo['integral'];
  854. if ($order['status'] == -2 || $order['is_del']) {
  855. return [$order, 0];
  856. }
  857. $res1 = $res2 = $res3 = $res4 = true;
  858. //订单赠送积分
  859. /** @var UserBillServices $userBillServices */
  860. $userBillServices = app()->make(UserBillServices::class);
  861. $where = [
  862. 'uid' => $order['uid'],
  863. 'category' => 'integral',
  864. 'type' => 'gain',
  865. 'link_id' => $order['id']
  866. ];
  867. $give_integral = $userBillServices->sum($where, 'number');
  868. if ((int)$order['refund_status'] != 2 && $order['back_integral'] >= $order['use_integral']) {
  869. return [$order, 0];
  870. }
  871. //子订单退款 再次查询主订单
  872. if (!$give_integral && $order['pid']) {
  873. $where['link_id'] = $order['pid'];
  874. $give_integral = $userBillServices->sum($where, 'number');
  875. if ($give_integral) {
  876. $p_order = $this->storeOrderServices->get($order['pid']);
  877. $give_integral = bcmul((string)$give_integral, (string)bcdiv((string)$order['pay_price'], (string)$p_order['pay_price'], 4), 0);
  878. }
  879. }
  880. if ($give_integral) {
  881. //判断订单是否已经回退积分
  882. $count = $userBillServices->count(['category' => 'integral', 'type' => 'deduction', 'link_id' => $order['id']]);
  883. if (!$count) {
  884. if ($integral > $give_integral) {
  885. $integral = bcsub((string)$integral, (string)$give_integral);
  886. } else {
  887. $integral = 0;
  888. }
  889. //记录赠送积分收回
  890. $res1 = $userBillServices->income('integral_refund', $order['uid'], (int)$give_integral, (int)$integral, $order['id']);
  891. }
  892. }
  893. //返还下单使用积分
  894. $use_integral = $order['use_integral'];
  895. if ($use_integral > 0) {
  896. $integral = bcadd((string)$integral, (string)$use_integral);
  897. //记录下单使用积分还回
  898. $res2 = $userBillServices->income('pay_product_integral_back', $order['uid'], (int)$use_integral, (int)$integral, $order['id']);
  899. }
  900. $res3 = $userServices->update($order['uid'], ['integral' => $integral]);
  901. if (!($res1 && $res2 && $res3)) {
  902. throw new ValidateException('回退积分增加失败');
  903. }
  904. if ($use_integral > $give_integral) {
  905. $order->back_integral = bcsub($use_integral, $give_integral, 2);
  906. }
  907. return [$order, bcsub((string)$integral, (string)$userInfo['integral'], 0)];
  908. }
  909. /**
  910. * 回退库存
  911. * @param $order
  912. * @return bool
  913. */
  914. public function regressionStock($order)
  915. {
  916. if ($order['status'] == -2 || $order['is_del']) return true;
  917. $res5 = true;
  918. /** @var StoreOrderCartInfoServices $cartServices */
  919. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  920. /** @var StoreProductServices $services */
  921. $services = app()->make(StoreProductServices::class);
  922. /** @var StoreSeckillServices $seckillServices */
  923. $seckillServices = app()->make(StoreSeckillServices::class);
  924. /** @var StoreCombinationServices $pinkServices */
  925. $pinkServices = app()->make(StoreCombinationServices::class);
  926. /** @var StoreBargainServices $bargainServices */
  927. $bargainServices = app()->make(StoreBargainServices::class);
  928. /** @var StoreDiscountsServices $discountServices */
  929. $discountServices = app()->make(StoreDiscountsServices::class);
  930. /** @var StoreNewcomerServices $storeNewcomerServices */
  931. $storeNewcomerServices = app()->make(StoreNewcomerServices::class);
  932. $activity_id = (int)$order['activity_id'];
  933. $store_id = (int)$order['store_id'] ?? 0;
  934. $cartInfo = $cartServices->getCartInfoList(['cart_id' => $order['cart_id']], ['cart_info']);
  935. foreach ($cartInfo as $cart) {
  936. $cart['cart_info'] = is_array($cart['cart_info']) ? $cart['cart_info'] : json_decode($cart['cart_info'], true);
  937. //增库存减销量
  938. $unique = isset($cart['cart_info']['productInfo']['attrInfo']) ? $cart['cart_info']['productInfo']['attrInfo']['unique'] : '';
  939. $cart_num = (int)$cart['cart_info']['cart_num'];
  940. $product_id = (int)$cart['cart_info']['productInfo']['id'];
  941. switch ($order['type']) {
  942. case 0://普通
  943. case 6://预售
  944. case 8://抽奖
  945. case 9://拼单
  946. case 10://桌码
  947. $res5 = $res5 && $services->incProductStock($cart_num, $product_id, $unique, $store_id);
  948. break;
  949. case 1://秒杀
  950. $res5 = $res5 && $seckillServices->incSeckillStock($cart_num, $activity_id, $unique, $store_id);
  951. break;
  952. case 2://砍价
  953. $res5 = $res5 && $bargainServices->incBargainStock($cart_num, $activity_id, $unique, $store_id);
  954. break;
  955. case 3://拼团
  956. $res5 = $res5 && $pinkServices->incCombinationStock($cart_num, $activity_id, $unique, $store_id);
  957. break;
  958. case 5://套餐
  959. CacheService::setStock(md5($activity_id), 1, 5, false);
  960. $res5 = $res5 && $discountServices->incDiscountStock($cart_num, $activity_id, (int)($cart['cart_info']['discount_product_id'] ?? 0), (int)($cart['cart_info']['product_id'] ?? 0), $unique, $store_id);
  961. break;
  962. case 7://新人专享
  963. $res5 = $res5 && $storeNewcomerServices->incNewcomerStock($cart_num, $activity_id, $unique, $store_id);
  964. break;
  965. default:
  966. $res5 = $res5 && $services->incProductStock($cart_num, $product_id, $unique, $store_id);
  967. break;
  968. }
  969. if (in_array($order['type'], [1, 2, 3])) CacheService::setStock($unique, $cart_num, (int)$order['type'], false);
  970. }
  971. if ($order['type'] == 5) {
  972. //改变套餐限量
  973. $res5 = $res5 && $discountServices->changeDiscountLimit($activity_id, false);
  974. }
  975. $this->regressionRedisStock($order);
  976. return $res5;
  977. }
  978. /**
  979. * 回退redis占用库存
  980. * @param $order
  981. * @return bool
  982. */
  983. public function regressionRedisStock($order)
  984. {
  985. if ($order['status'] == -2 || $order['is_del']) return true;
  986. $type = $order['type'] ?? 0;
  987. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  988. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  989. $cartInfo = $storeOrderCartInfoServices->getOrderCartInfo((int)$order['id']);
  990. //回退套餐限量库
  991. if ($type == 5 && $order['activity_id']) CacheService::setStock(md5($order['activity_id']), 1, 5, false);
  992. foreach ($cartInfo as $item) {//回退redis占用
  993. if (!isset($item['product_attr_unique']) || !$item['product_attr_unique']) continue;
  994. $type = $item['type'];
  995. if (in_array($type, [1, 2, 3])) CacheService::setStock($item['product_attr_unique'], (int)$item['cart_num'], $type, false);
  996. }
  997. return true;
  998. }
  999. /**
  1000. * 同意退款退款失败写入订单记录
  1001. * @param int $id
  1002. * @param $refund_price
  1003. */
  1004. public function storeProductOrderRefundYFasle(int $id, $refund_price)
  1005. {
  1006. /** @var StoreOrderStatusServices $statusService */
  1007. $statusService = app()->make(StoreOrderStatusServices::class);
  1008. $statusService->save([
  1009. 'oid' => $id,
  1010. 'change_type' => 'refund_price',
  1011. 'change_message' => '退款给用户:' . $refund_price . '元失败',
  1012. 'change_time' => time()
  1013. ]);
  1014. }
  1015. /**
  1016. * 不退款表单
  1017. * @param int $id
  1018. * @return array
  1019. * @throws \FormBuilder\Exception\FormBuilderException
  1020. */
  1021. public function noRefundForm(int $id)
  1022. {
  1023. $orderRefund = $this->dao->get($id);
  1024. if (!$orderRefund) {
  1025. throw new ValidateException('未查到订单');
  1026. }
  1027. $order = $this->storeOrderServices->get((int)$orderRefund['store_order_id']);
  1028. if (!$order) {
  1029. throw new ValidateException('未查到订单');
  1030. }
  1031. $f[] = Form::input('order_id', '不退款单号', $order->getData('order_id'))->disabled(true);
  1032. $f[] = Form::input('refund_reason', '不退款原因')->type('textarea')->required('请填写不退款原因');
  1033. return create_form('不退款原因', $f, $this->url('order/no_refund/' . $id), 'PUT');
  1034. }
  1035. /**
  1036. * 退积分表单创建
  1037. * @param int $id
  1038. * @return array
  1039. * @throws \FormBuilder\Exception\FormBuilderException
  1040. */
  1041. public function refundIntegralForm(int $id)
  1042. {
  1043. if (!$orderInfo = $this->storeOrderServices->get($id))
  1044. throw new ValidateException('订单不存在');
  1045. if ($orderInfo->use_integral < 0 || $orderInfo->use_integral == $orderInfo->back_integral)
  1046. throw new ValidateException('积分已退或者积分为零无法再退');
  1047. if (!$orderInfo->paid)
  1048. throw new ValidateException('未支付无法退积分');
  1049. $f[] = Form::input('order_id', '退款单号', $orderInfo->getData('order_id'))->disabled(1);
  1050. $f[] = Form::number('use_integral', '使用的积分', (float)$orderInfo->getData('use_integral'))->min(0)->disabled(1);
  1051. $f[] = Form::number('use_integrals', '已退积分', (float)$orderInfo->getData('back_integral'))->min(0)->disabled(1);
  1052. $f[] = Form::number('back_integral', '可退积分', (float)bcsub($orderInfo->getData('use_integral'), $orderInfo->getData('back_integral')))->min(0)->precision(0)->required('请输入可退积分');
  1053. return create_form('退积分', $f, $this->url('/order/refund_integral/' . $id), 'PUT');
  1054. }
  1055. /**
  1056. * 单独退积分处理
  1057. * @param $orderInfo
  1058. * @param $back_integral
  1059. */
  1060. public function refundIntegral($orderInfo, $back_integral)
  1061. {
  1062. /** @var UserServices $userServices */
  1063. $userServices = app()->make(UserServices::class);
  1064. $integral = $userServices->value(['uid' => $orderInfo['uid']], 'integral');
  1065. return $this->transaction(function () use ($userServices, $orderInfo, $back_integral, $integral) {
  1066. $res1 = $userServices->bcInc($orderInfo['uid'], 'integral', $back_integral, 'uid');
  1067. /** @var UserBillServices $userBillServices */
  1068. $userBillServices = app()->make(UserBillServices::class);
  1069. $res2 = $userBillServices->income('pay_product_integral_back', $orderInfo['uid'], $back_integral, $integral + $back_integral, $orderInfo['id']);
  1070. /** @var StoreOrderStatusServices $statusService */
  1071. $statusService = app()->make(StoreOrderStatusServices::class);
  1072. $res3 = $statusService->save([
  1073. 'oid' => $orderInfo['id'],
  1074. 'change_type' => 'integral_back',
  1075. 'change_message' => '商品退积分:' . $back_integral,
  1076. 'change_time' => time()
  1077. ]);
  1078. $res4 = $orderInfo->save();
  1079. $res = $res1 && $res2 && $res3 && $res4;
  1080. if (!$res) {
  1081. throw new ValidateException('订单退积分失败');
  1082. }
  1083. return true;
  1084. });
  1085. }
  1086. /**
  1087. * 写入退款快递单号
  1088. * @param $order
  1089. * @param $express
  1090. * @return bool
  1091. */
  1092. public function editRefundExpress($data)
  1093. {
  1094. $id = (int)$data['id'];
  1095. $refundOrder = $this->dao->get($id);
  1096. if (!$refundOrder) {
  1097. throw new ValidateException('退款订单不存在');
  1098. }
  1099. $this->transaction(function () use ($id, $refundOrder, $data) {
  1100. $data['refund_type'] = 5;
  1101. /** @var StoreOrderStatusServices $statusService */
  1102. $statusService = app()->make(StoreOrderStatusServices::class);
  1103. $res1 = false !== $statusService->save([
  1104. 'oid' => $refundOrder['store_order_id'],
  1105. 'change_type' => 'refund_express',
  1106. 'change_message' => '用户已退货,快递单号:' . $data['refund_express'],
  1107. 'change_time' => time()
  1108. ]);
  1109. $res2 = false !== $this->dao->update(['id' => $id], $data);
  1110. $res = $res1 && $res2;
  1111. if (!$res)
  1112. throw new ValidateException('提交失败!');
  1113. });
  1114. return true;
  1115. }
  1116. /**
  1117. * 退款订单详情
  1118. * @param $uni
  1119. * @param array $field
  1120. * @param array $with
  1121. * @return mixed
  1122. */
  1123. public function refundDetail($uni, array $field = ['*'], array $with = ['invoice', 'virtual'])
  1124. {
  1125. if (!strlen(trim($uni))) throw new ValidateException('参数错误');
  1126. $order = $this->dao->get(['id|order_id' => $uni], ['*']);
  1127. if (!$order) throw new ValidateException('订单不存在');
  1128. $order = $order->toArray();
  1129. /** @var StoreOrderServices $orderServices */
  1130. $orderServices = app()->make(StoreOrderServices::class);
  1131. $orderInfo = $orderServices->get($order['store_order_id'], $field, $with);
  1132. $orderInfo = $orderInfo->toArray();
  1133. $orderInfo = $orderServices->tidyOrder($orderInfo, true, true);
  1134. /** @var UserServices $userServices */
  1135. $userServices = app()->make(UserServices::class);
  1136. $userInfo = $userServices->getUserWithTrashedInfo($order['uid']);
  1137. $order['mapKey'] = sys_config('tengxun_map_key');
  1138. $order['yue_pay_status'] = (int)sys_config('balance_func_status') && (int)sys_config('yue_pay_status') == 1 ? (int)1 : (int)2;//余额支付 1 开启 2 关闭
  1139. $order['pay_weixin_open'] = (int)sys_config('pay_weixin_open') ?? 0;//微信支付 1 开启 0 关闭
  1140. $order['ali_pay_status'] = (bool)sys_config('ali_pay_status');//支付包支付 1 开启 0 关闭
  1141. $orderData = $order;
  1142. $orderData['refunded_price'] = floatval($orderData['refunded_price']) ?: $orderData['refund_price'];
  1143. $orderData['store_order_sn'] = $orderInfo['order_id'];
  1144. $orderData['product_type'] = $orderInfo['product_type'];
  1145. $orderData['supplier_id'] = $orderInfo['supplier_id'] ?? 0;
  1146. $orderData['supplierInfo'] = $orderInfo['supplierInfo'] ?? null;
  1147. $orderData['cartInfo'] = $orderData['cart_info'];
  1148. $orderData['invoice'] = $orderInfo['invoice'];
  1149. $orderData['virtual'] = $orderInfo['virtual'];
  1150. $orderData['virtual_info'] = $orderInfo['virtual_info'];
  1151. $orderData['custom_form'] = is_string($orderInfo['custom_form']) ? json_decode($orderInfo['custom_form'], true) : $orderInfo['custom_form'];
  1152. $orderData['first_order_price'] = $orderInfo['first_order_price'];
  1153. $cateData = [];
  1154. if (isset($orderData['cartInfo']) && $orderData['cartInfo']) {
  1155. $productId = array_column($orderData['cartInfo'], 'product_id');
  1156. /** @var StoreProductServices $productServices */
  1157. $productServices = app()->make(StoreProductServices::class);
  1158. $cateData = $productServices->productIdByProductCateName($productId);
  1159. }
  1160. //核算优惠金额
  1161. $vipTruePrice = 0;
  1162. $total_price = 0;
  1163. $promotionsPrice = 0;
  1164. foreach ($orderData['cartInfo'] ?? [] as $key => &$cart) {
  1165. if (!isset($cart['sum_true_price'])) $cart['sum_true_price'] = bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
  1166. $cart['vip_sum_truePrice'] = bcmul($cart['vip_truePrice'], $cart['cart_num'] ? $cart['cart_num'] : 1, 2);
  1167. $vipTruePrice = bcadd((string)$vipTruePrice, (string)$cart['vip_sum_truePrice'], 2);
  1168. if (isset($order['split']) && $order['split']) {
  1169. $orderData['cartInfo'][$key]['cart_num'] = $cart['surplus_num'];
  1170. if (!$cart['surplus_num']) unset($orderData['cartInfo'][$key]);
  1171. }
  1172. $total_price = bcadd($total_price, bcmul((string)$cart['sum_price'], (string)$cart['cart_num'], 2), 2);
  1173. $orderData['cartInfo'][$key]['class_name'] = $cateData[$cart['product_id']] ?? '';
  1174. $promotionsPrice = bcadd($promotionsPrice, bcmul((string)($cart['promotions_true_price'] ?? 0), (string)$cart['cart_num'], 2), 2);
  1175. }
  1176. //优惠活动优惠详情
  1177. /** @var StoreOrderPromotionsServices $storeOrderPromotiosServices */
  1178. $storeOrderPromotiosServices = app()->make(StoreOrderPromotionsServices::class);
  1179. if ($orderData['refund_type'] == 6) {
  1180. $orderData['promotions_detail'] = $storeOrderPromotiosServices->getOrderPromotionsDetail((int)$orderData['store_order_id']);
  1181. } else {
  1182. $orderData['promotions_detail'] = $storeOrderPromotiosServices->applyRefundOrderPromotions((int)$orderData['store_order_id'], $orderData['cartInfo']);
  1183. }
  1184. if (!$orderData['promotions_detail'] && $promotionsPrice) {
  1185. $orderData['promotions_detail'][] = [
  1186. 'name' => '优惠活动',
  1187. 'title' => '优惠活动',
  1188. 'promotions_price' => $promotionsPrice,
  1189. ];
  1190. }
  1191. $orderData['use_integral'] = $this->getOrderSumPrice($orderData['cartInfo'], 'use_integral', false);
  1192. $orderData['integral_price'] = $this->getOrderSumPrice($orderData['cartInfo'], 'integral_price', false);
  1193. $orderData['coupon_id'] = $orderInfo['coupon_id'];
  1194. $orderData['coupon_price'] = $this->getOrderSumPrice($orderData['cartInfo'], 'coupon_price', false);
  1195. $orderData['deduction_price'] = $this->getOrderSumPrice($orderData['cartInfo'], 'integral_price', false);
  1196. $orderData['vip_true_price'] = $vipTruePrice;
  1197. $orderData['postage_price'] = 0;
  1198. $orderData['pay_postage'] = 0;
  1199. if (!in_array($orderInfo['shipping_type'], [2, 4])) {
  1200. $orderData['pay_postage'] = $this->getOrderSumPrice($orderData['cart_info'], 'postage_price', false);
  1201. }
  1202. $orderData['member_price'] = 0;
  1203. $orderData['routine_contact_type'] = sys_config('routine_contact_type', 0);
  1204. switch ($orderInfo['pay_type']) {
  1205. case PayServices::WEIXIN_PAY:
  1206. $pay_type_name = '微信支付';
  1207. break;
  1208. case PayServices::YUE_PAY:
  1209. $pay_type_name = '余额支付';
  1210. break;
  1211. case PayServices::OFFLINE_PAY:
  1212. $pay_type_name = '线下支付';
  1213. break;
  1214. case PayServices::ALIAPY_PAY:
  1215. $pay_type_name = '支付宝支付';
  1216. break;
  1217. case PayServices::CASH_PAY:
  1218. $pay_type_name = '现金支付';
  1219. break;
  1220. default:
  1221. $pay_type_name = '其他支付';
  1222. break;
  1223. }
  1224. $orderData['_add_time'] = date('Y-m-d H:i:s', $orderData['add_time']);
  1225. $orderData['add_time_y'] = date('Y-m-d', $orderData['add_time']);
  1226. $orderData['add_time_h'] = date('H:i:s', $orderData['add_time']);
  1227. if (in_array($orderData['refund_type'], [1, 2, 4, 5])) {
  1228. $_type = -1;
  1229. $_msg = '商家审核中,请耐心等待';
  1230. $_title = '申请退款中';
  1231. } elseif ($orderData['refund_type'] == 3) {
  1232. $_type = -3;
  1233. $_title = '拒绝退款';
  1234. $_msg = '商家拒绝退款,请联系商家';
  1235. } else {
  1236. $_type = -2;
  1237. $_title = '已退款';
  1238. $_msg = '已为您退款,感谢您的支持';
  1239. }
  1240. if ($orderData['store_id']) {
  1241. /** @var SystemStoreServices $storeServices */
  1242. $storeServices = app()->make(SystemStoreServices::class);
  1243. $storeInfo = $storeServices->get($orderData['store_id']);
  1244. $refund_name = $storeInfo['name'];
  1245. $refund_phone = $storeInfo['phone'];
  1246. $refund_address = $storeInfo['detailed_address'];
  1247. } elseif ($orderData['supplier_id']) {
  1248. /** @var SystemSupplierServices $supplierServices */
  1249. $supplierServices = app()->make(SystemSupplierServices::class);
  1250. $supplierIno = $supplierServices->get($orderData['supplier_id']);
  1251. $refund_name = $supplierIno['supplier_name'];
  1252. $refund_phone = $supplierIno['phone'];
  1253. $refund_address = $supplierIno['detailed_address'];
  1254. } else {
  1255. $refund_name = sys_config('refund_name', '');
  1256. $refund_phone = sys_config('refund_phone', '');
  1257. $refund_address = sys_config('refund_address', '');
  1258. }
  1259. $orderData['_status'] = [
  1260. '_type' => $_type,
  1261. '_title' => $_title,
  1262. '_msg' => $_msg ?? '',
  1263. '_payType' => $pay_type_name,
  1264. 'refund_name' => $refund_name,
  1265. 'refund_phone' => $refund_phone,
  1266. 'refund_address' => $refund_address,
  1267. ];
  1268. $orderData['shipping_type'] = $orderInfo['shipping_type'];
  1269. $orderData['real_name'] = $orderInfo['real_name'];
  1270. $orderData['user_phone'] = $orderInfo['user_phone'];
  1271. $orderData['user_address'] = $orderInfo['user_address'];
  1272. $orderData['_pay_time'] = $orderInfo['pay_time'] ? date('Y-m-d H:i:s', $orderInfo['pay_time']) : '';
  1273. $orderData['_add_time'] = $orderInfo['add_time'] ? date('Y-m-d H:i:s', $orderInfo['add_time']) : '';
  1274. $orderData['_refund_time'] = $orderData['add_time'] ? date('Y-m-d H:i:s', $orderData['add_time']) : '';
  1275. $orderData['nickname'] = $userInfo['nickname'] ?? '';
  1276. $orderData['total_num'] = $orderData['refund_num'];
  1277. $orderData['pay_price'] = $orderData['refund_price'];
  1278. $orderData['refund_status'] = in_array($orderData['refund_type'], [1, 2, 4, 5]) ? 1 : 2;
  1279. $orderData['total_price'] = floatval(bcsub((string)$total_price, (string)$vipTruePrice, 2));
  1280. $orderData['paid'] = 1;
  1281. $orderData['mark'] = $orderInfo['mark'];
  1282. $orderData['express_list'] = $orderData['refund_type'] == 4 ? app()->make(ExpressServices::class)->expressList(['is_show' => 1]) : [];
  1283. $orderData['spread_uid'] = $orderInfo['spread_uid'] ?? 0;
  1284. $orderData['orderStatus'] = $orderInfo['_status'];
  1285. return $orderData;
  1286. }
  1287. /**
  1288. * 获取某个字段总金额
  1289. * @param $cartInfo
  1290. * @param string $key
  1291. * @param bool $is_unit
  1292. * @return int|string
  1293. */
  1294. public function getOrderSumPrice($cartInfo, $key = 'truePrice', $is_unit = true)
  1295. {
  1296. $SumPrice = 0;
  1297. foreach ($cartInfo as $cart) {
  1298. if (isset($cart['cart_info'])) $cart = $cart['cart_info'];
  1299. if ($is_unit) {
  1300. $SumPrice = bcadd($SumPrice, bcmul($cart['cart_num'] ?? 1, $cart[$key] ?? 0, 2), 2);
  1301. } else {
  1302. $SumPrice = bcadd($SumPrice, $cart[$key] ?? 0, 2);
  1303. }
  1304. }
  1305. return $SumPrice;
  1306. }
  1307. }