StoreOrderSplitServices.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\order;
  12. use app\services\BaseServices;
  13. use app\dao\order\StoreOrderDao;
  14. use crmeb\exceptions\AdminException;
  15. /**
  16. * 订单拆分
  17. * Class StoreOrderSplitServices
  18. * @package app\services\order
  19. */
  20. class StoreOrderSplitServices extends BaseServices
  21. {
  22. /**
  23. * 需要清空恢复默认数据字段
  24. * @var string[]
  25. */
  26. protected $order_data = ['id', 'status', 'refund_status', 'refund_type', 'refund_express', 'refund_reason_wap_img', 'refund_reason_wap_explain', 'refund_reason_time', 'refund_reason_wap', 'refund_reason', 'refund_price', 'delivery_name', 'delivery_code', 'delivery_type', 'delivery_id', 'fictitious_content', 'delivery_uid'];
  27. /**
  28. * 构造方法
  29. * StoreOrderRefundServices constructor.
  30. * @param StoreOrderDao $dao
  31. */
  32. public function __construct(StoreOrderDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * 主订单平行拆单
  38. * @param $id
  39. * @param $cart_ids
  40. * @param $orderInfo
  41. * @return false|mixed
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\DbException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. */
  46. public function equalSplit($id, $cart_ids, $orderInfo)
  47. {
  48. /** @var StoreOrderCreateServices $storeOrderCreateServices */
  49. $storeOrderCreateServices = app()->make(StoreOrderCreateServices::class);
  50. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  51. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  52. /** @var StoreOrderStatusServices $statusService */
  53. $statusService = app()->make(StoreOrderStatusServices::class);
  54. $ids = array_unique(array_column($cart_ids, 'cart_id'));
  55. if (!$cart_ids || !$ids) return false;
  56. if (!$orderInfo) $orderInfo = $this->dao->get($id, ['*']);
  57. if (!$orderInfo) throw new AdminException(400118);
  58. $old_order = $orderInfo;
  59. $orderInfo = $orderInfoOld = is_object($orderInfo) ? $orderInfo->toArray() : $orderInfo;
  60. foreach ($this->order_data as $field) {
  61. unset($orderInfo[$field]);
  62. }
  63. unset($orderInfoOld['id']);
  64. //获取拆分之后的两个cart_id
  65. $cartInfo = $storeOrderCartInfoServices->getColumn(['oid' => $id], 'cart_num,surplus_num,cart_info', 'cart_id');
  66. $new_cart_ids = array_combine(array_column($cart_ids, 'cart_id'), $cart_ids);
  67. $other_cart_ids = [];
  68. foreach ($cartInfo as $cart_id => $cart) {
  69. if (!isset($new_cart_ids[$cart_id]) && $cart['surplus_num']) {//无拆分
  70. $other = ['cart_id' => (string)$cart_id, 'cart_num' => $cart['surplus_num']];
  71. } else if ($new_cart_ids[$cart_id]['cart_num'] < $cart['surplus_num']) {
  72. $other = ['cart_id' => (string)$cart_id, 'cart_num' => bcsub((string)$cart['surplus_num'], (string)$new_cart_ids[$cart_id]['cart_num'], 0)];
  73. } else {
  74. continue;
  75. }
  76. $other_cart_ids[] = $other;
  77. }
  78. $cart_ids_arr = ['new' => $cart_ids, 'other' => $other_cart_ids];
  79. if (empty($cart_ids_arr['other'])) return [$old_order, ['id' => 0]];
  80. return $this->transaction(function () use ($id, $cart_ids_arr, $orderInfo, $orderInfoOld, $cartInfo, $storeOrderCreateServices, $storeOrderCartInfoServices, $statusService) {
  81. $order = $otherOrder = [];
  82. $statusData = $statusService->selectList(['oid' => $id])->toArray();
  83. //订单实际支付金额
  84. $order_pay_price = bcsub((string)bcadd((string)$orderInfo['total_price'], (string)$orderInfo['pay_postage'], 2), (string)bcadd((string)$orderInfo['deduction_price'], (string)$orderInfo['coupon_price'], 2), 2);
  85. //有改价
  86. $change_price = $order_pay_price != $orderInfo['pay_price'];
  87. foreach ($cart_ids_arr as $key => $cart_ids) {
  88. if ($orderInfo['pid'] == 0 || ($orderInfo['pid'] > 0 && $key == 'new')) {
  89. $order_data = $key == 'other' ? $orderInfoOld : $orderInfo;
  90. $order_data['pid'] = $orderInfo['pid'] > 0 ? $orderInfo['pid'] : $id;
  91. mt_srand();
  92. $order_data['order_id'] = $storeOrderCreateServices->getNewOrderId('cp');
  93. $order_data['cart_id'] = [];
  94. $order_data['unique'] = $storeOrderCreateServices->getNewOrderId('');
  95. $new_order = $this->dao->save($order_data);
  96. if (!$new_order) {
  97. throw new AdminException(400544);
  98. }
  99. $new_id = (int)$new_order->id;
  100. $allData = [];
  101. foreach ($statusData as $data) {
  102. $data['oid'] = $new_id;
  103. $data['change_time'] = strtotime($data['change_time']);
  104. $allData[] = $data;
  105. }
  106. if ($allData) {
  107. $statusService->saveAll($allData);
  108. }
  109. } else {
  110. $new_id = $id;
  111. }
  112. $statusService->save([
  113. 'oid' => $new_id,
  114. 'change_type' => 'split_create_order',
  115. 'change_message' => '拆分订单生成',
  116. 'change_time' => time()
  117. ]);
  118. $cart_data_all = [];
  119. foreach ($cart_ids as $cart) {
  120. if ($orderInfo['pid'] == 0 || $orderInfo['pid'] > 0 && $key == 'new') {
  121. $_info = is_string($cartInfo[$cart['cart_id']]['cart_info']) ? json_decode($cartInfo[$cart['cart_id']]['cart_info'], true) : $cartInfo[$cart['cart_id']]['cart_info'];
  122. $new_cart_data['oid'] = $new_id;
  123. $new_cart_data['uid'] = $orderInfo['uid'];
  124. $new_cart_data['cart_id'] = $storeOrderCreateServices->getNewOrderId('');
  125. $new_cart_data['product_id'] = $_info['product_id'];
  126. $new_cart_data['old_cart_id'] = $cart['cart_id'];
  127. $new_cart_data['cart_num'] = $cart['cart_num'];
  128. $new_cart_data['surplus_num'] = $cart['cart_num'];
  129. $new_cart_data['unique'] = md5($new_cart_data['cart_id'] . '_' . $new_cart_data['oid']);
  130. $_info = $this->slpitComputeOrderCart($new_cart_data['cart_num'], $_info, $key);
  131. $_info['id'] = $new_cart_data['cart_id'];
  132. $new_cart_data['cart_info'] = json_encode($_info);
  133. $cart_data_all[] = $new_cart_data;
  134. } else {
  135. $cart_info = $storeOrderCartInfoServices->get(['cart_id' => $cart['cart_id']]);
  136. $_info = is_string($cartInfo[$cart['cart_id']]['cart_info']) ? json_decode($cartInfo[$cart['cart_id']]['cart_info'], true) : $cartInfo[$cart['cart_id']]['cart_info'];
  137. $cart_info->cart_num = $cart['cart_num'];
  138. $cart_info->refund_num = 0;
  139. $cart_info->surplus_num = $cart['cart_num'];
  140. $_info = $this->slpitComputeOrderCart($cart['cart_num'], $_info, $key);
  141. $_info['id'] = $cart_info['cart_id'];
  142. $cart_info->cart_info = json_encode($_info);
  143. $cart_info->save();
  144. $cart_data_all[] = [
  145. 'oid' => $new_id,
  146. 'uid' => $orderInfo['uid'],
  147. 'cart_id' => $cart_info->cart_id,
  148. 'product_id' => $cart_info->product_id,
  149. 'old_cart_id' => $cart_info->old_cart_id,
  150. 'cart_num' => $cart_info->cart_num,
  151. 'surplus_num' => $cart_info->surplus_num,
  152. 'unique' => $cart_info->unique,
  153. 'cart_info' => json_encode($_info),
  154. ];
  155. }
  156. $storeOrderCartInfoServices->update(['oid' => $id, 'cart_id' => $cart['cart_id']], ['surplus_num' => 0, 'split_status' => 2]);
  157. }
  158. if ($orderInfo['pid'] > 0 && $key == 'other') {
  159. $storeOrderCartInfoServices->delete(['oid' => $new_id]);
  160. }
  161. $storeOrderCartInfoServices->saveAll($cart_data_all);
  162. $new_order = $this->dao->get($new_id);
  163. $storeOrderCartInfoServices->clearOrderCartInfo($new_id);
  164. $this->splitComputeOrder((int)$new_id, $cart_data_all, (float)($change_price ? $order_pay_price : 0), (float)$orderInfo['pay_price'], (float)($new_order['pay_price'] ?? 0));
  165. $new_order = $this->dao->get($new_id);
  166. if ($key == 'new') {
  167. $order = $new_order;
  168. } else {
  169. $otherOrder = $new_order;
  170. }
  171. }
  172. if (!$orderInfo['pid']) $this->dao->update($id, ['pid' => -1]);
  173. //处理申请开票记录
  174. /** @var StoreOrderInvoiceServices $storeOrderInvoiceServics */
  175. $storeOrderInvoiceServics = app()->make(StoreOrderInvoiceServices::class);
  176. $storeOrderInvoiceServics->splitOrderInvoice((int)$id);
  177. return [$order, $otherOrder];
  178. });
  179. }
  180. /**
  181. * 订单拆分
  182. * @param int $id
  183. * @param array $cart_ids
  184. * @param array $orderInfo
  185. * @throws \think\db\exception\DataNotFoundException
  186. * @throws \think\db\exception\DbException
  187. * @throws \think\db\exception\ModelNotFoundException
  188. */
  189. public function split(int $id, array $cart_ids, $orderInfo = [])
  190. {
  191. $ids = array_unique(array_column($cart_ids, 'cart_id'));
  192. if (!$cart_ids || !$ids) {
  193. return false;
  194. }
  195. if (!$orderInfo) {
  196. $orderInfo = $this->dao->get($id, ['*']);
  197. }
  198. if (!$orderInfo) {
  199. throw new AdminException(400118);
  200. }
  201. /** @var StoreOrderCreateServices $storeOrderCreateServices */
  202. $storeOrderCreateServices = app()->make(StoreOrderCreateServices::class);
  203. $orderInfo = is_object($orderInfo) ? $orderInfo->toArray() : $orderInfo;
  204. foreach ($this->order_data as $field) {
  205. unset($orderInfo[$field]);
  206. }
  207. $order_data = $orderInfo;
  208. $order_data['pid'] = $id;
  209. mt_srand();
  210. $order_data['order_id'] = $orderInfo['order_id'] . '_' . rand(100, 999);
  211. $order_data['cart_id'] = [];
  212. $order_data['unique'] = $storeOrderCreateServices->getNewOrderId('');
  213. $order_data['add_time'] = time();
  214. $new_order = $this->dao->save($order_data);
  215. if (!$new_order) {
  216. throw new AdminException(400544);
  217. }
  218. $new_id = (int)$new_order->id;
  219. /** @var StoreOrderStatusServices $statusService */
  220. $statusService = app()->make(StoreOrderStatusServices::class);
  221. $statusService->save([
  222. 'oid' => $new_id,
  223. 'change_type' => 'split_create_order',
  224. 'change_message' => '发货拆分订单生成',
  225. 'change_time' => time()
  226. ]);
  227. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  228. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  229. //订单下原商品信息
  230. $cartInfo = $storeOrderCartInfoServices->getColumn(['oid' => $id, 'cart_id' => $ids], 'cart_num,surplus_num,cart_info', 'cart_id');
  231. $cart_data = $cart_data_all = $update_data = [];
  232. $cart_data['oid'] = $new_id;
  233. foreach ($cart_ids as $cart) {
  234. $surplus_num = $cartInfo[$cart['cart_id']]['surplus_num'] ?? 0;
  235. if (!isset($cartInfo[$cart['cart_id']]) || !$surplus_num) continue;
  236. $_info = is_string($cartInfo[$cart['cart_id']]['cart_info']) ? json_decode($cartInfo[$cart['cart_id']]['cart_info'], true) : $cartInfo[$cart['cart_id']]['cart_info'];
  237. $cart_data['cart_id'] = $storeOrderCreateServices->getNewOrderId('');
  238. $cart_data['product_id'] = $_info['product_id'];
  239. $cart_data['old_cart_id'] = $cart['cart_id'];
  240. $cart_data['cart_num'] = $cart['cart_num'];
  241. $cart_data['unique'] = md5($cart_data['cart_id'] . '_' . $cart_data['oid']);
  242. if ($cart['cart_num'] >= $surplus_num) {//拆分完成
  243. $cart_data['cart_num'] = $surplus_num;
  244. $update_data['split_status'] = 2;
  245. $update_data['surplus_num'] = 0;
  246. } else {//拆分部分数量
  247. $update_data['surplus_num'] = bcsub((string)$surplus_num, $cart['cart_num'], 0);
  248. $update_data['split_status'] = $update_data['surplus_num'] > 0 ? 1 : 2;
  249. }
  250. $_info = $this->slpitComputeOrderCart($cart_data['cart_num'], $_info);
  251. $_info['id'] = $cart_data['cart_id'];
  252. $cart_data['cart_info'] = json_encode($_info);
  253. //修改原来订单商品信息
  254. if (false === $storeOrderCartInfoServices->update(['oid' => $id, 'cart_id' => $cart['cart_id']], $update_data)) {
  255. throw new AdminException(400545);
  256. }
  257. $cart_data_all[] = $cart_data;
  258. }
  259. if (!$storeOrderCartInfoServices->saveAll($cart_data_all)) {
  260. throw new AdminException(400546);
  261. }
  262. $new_order = $this->dao->get($new_id);
  263. $this->splitComputeOrder($new_id, $cart_data_all, $new_order);
  264. return $new_order;
  265. }
  266. /**
  267. * 重新计算新订单中价格等信息
  268. * @param int $id
  269. * @param $orderInfo
  270. * @param array $cart_info_data
  271. */
  272. public function splitComputeOrder(int $id, array $cart_info_data, float $order_pay_price = 0.00, float $pay_price = 0.00, float $pre_pay_price = 0.00)
  273. {
  274. $order_update['cart_id'] = array_column($cart_info_data, 'cart_id');
  275. $order_update['total_num'] = array_sum(array_column($cart_info_data, 'cart_num'));
  276. $total_price = $coupon_price = $deduction_price = $use_integral = $pay_postage = $gainIntegral = $one_brokerage = $two_brokerage = $staffBrokerage = $agentBrokerage = $divisionBrokerage = 0;
  277. foreach ($cart_info_data as $cart) {
  278. $_info = json_decode($cart['cart_info'], true);
  279. $total_price = bcadd((string)$total_price, (string)$_info['sum_true_price'], 2);
  280. $deduction_price = bcadd((string)$deduction_price, (string)$_info['integral_price'], 2);
  281. $coupon_price = bcadd((string)$coupon_price, (string)$_info['coupon_price'], 2);
  282. $use_integral = bcadd((string)$use_integral, (string)$_info['use_integral'], 0);
  283. $pay_postage = isset($_info['postage_price']) ? bcadd((string)$pay_postage, (string)$_info['postage_price'], 2) : 0;
  284. $cartInfoGainIntegral = bcmul((string)$cart['cart_num'], (string)($_info['productInfo']['give_integral'] ?? '0'), 0);
  285. $gainIntegral = bcadd((string)$gainIntegral, (string)$cartInfoGainIntegral, 0);
  286. $one_brokerage = bcadd((string)$one_brokerage, (string)$_info['one_brokerage'], 2);
  287. $two_brokerage = bcadd((string)$two_brokerage, (string)$_info['two_brokerage'], 2);
  288. $staffBrokerage = bcadd((string)$staffBrokerage, (string)$_info['staff_brokerage'], 2);
  289. $agentBrokerage = bcadd((string)$agentBrokerage, (string)$_info['agent_brokerage'], 2);
  290. $divisionBrokerage = bcadd((string)$divisionBrokerage, (string)$_info['division_brokerage'], 2);
  291. }
  292. $order_update['coupon_id'] = array_unique(array_column($cart_info_data, 'coupon_id'));
  293. $order_update['pay_price'] = bcadd((string)$total_price, (string)$pay_postage, 2);
  294. //有订单原来支付金额 改价订单
  295. if ($order_pay_price) {
  296. if ($pre_pay_price) {//上一个已经计算 这里减法
  297. $order_update['pay_price'] = bcsub((string)$pay_price, (string)$pre_pay_price, 2);
  298. } else {//按比例计算实际支付金额
  299. $order_update['pay_price'] = bcmul((string)bcdiv((string)$pay_price, (string)$order_pay_price, 4), (string)$order_update['pay_price'], 2);
  300. }
  301. }
  302. $order_update['total_price'] = bcadd((string)$total_price, (string)bcadd((string)$deduction_price, (string)$coupon_price, 2), 2);
  303. $order_update['deduction_price'] = $deduction_price;
  304. $order_update['coupon_price'] = $coupon_price;
  305. $order_update['use_integral'] = $use_integral;
  306. $order_update['gain_integral'] = $gainIntegral;
  307. $order_update['pay_postage'] = $pay_postage;
  308. $order_update['one_brokerage'] = $one_brokerage;
  309. $order_update['two_brokerage'] = $two_brokerage;
  310. $order_update['staff_brokerage'] = $staffBrokerage;
  311. $order_update['agent_brokerage'] = $agentBrokerage;
  312. $order_update['division_brokerage'] = $divisionBrokerage;
  313. if (false === $this->dao->update($id, $order_update, 'id')) {
  314. throw new AdminException(400547);
  315. }
  316. return true;
  317. }
  318. /**
  319. * 部分发货重新计算订单商品:实际金额、优惠、积分等金额
  320. * @param int $cart_num
  321. * @param array $cart_info
  322. * @param string $orderType
  323. * @return array
  324. */
  325. public function slpitComputeOrderCart(int $cart_num, array $cart_info, $orderType = 'new')
  326. {
  327. if (!$cart_num || !$cart_info) return [];
  328. if ($cart_num >= $cart_info['cart_num']) return $cart_info;
  329. $new_cart_info = $cart_info;
  330. $new_cart_info['cart_num'] = $cart_num;
  331. $compute_arr = ['coupon_price', 'integral_price', 'postage_price', 'use_integral', 'one_brokerage', 'two_brokerage', 'staff_brokerage', 'agent_brokerage', 'division_brokerage', 'sum_true_price'];
  332. foreach ($compute_arr as $field) {
  333. if (!isset($cart_info[$field]) || !$cart_info[$field]) {
  334. $new_cart_info[$field] = 0;
  335. continue;
  336. }
  337. $scale = 2;
  338. if ($field == 'use_integral') $scale = 0;
  339. $new_cart_info[$field] = bcmul((string)$cart_num, bcdiv((string)$cart_info[$field], (string)$cart_info['cart_num'], 4), $scale);
  340. if ($orderType == 'new') {//拆出
  341. $new_cart_info[$field] = bcmul((string)$cart_num, bcdiv((string)$cart_info[$field], (string)$cart_info['cart_num'], 4), $scale);
  342. } else {
  343. $field_number = bcmul((string)bcsub((string)$cart_info['cart_num'], (string)$cart_num, 0), bcdiv((string)$cart_info[$field], (string)$cart_info['cart_num'], 4), $scale);
  344. $new_cart_info[$field] = bcsub((string)$cart_info[$field], (string)$field_number, $scale);
  345. }
  346. }
  347. return $new_cart_info;
  348. }
  349. /**
  350. * 获取整理后的订单商品信息
  351. * @param int $id
  352. * @param array $cart_ids
  353. * @param array $orderInfo
  354. * @return array|false
  355. * @throws \think\db\exception\DataNotFoundException
  356. * @throws \think\db\exception\DbException
  357. * @throws \think\db\exception\ModelNotFoundException
  358. */
  359. public function getSplitOrderCartInfo(int $id, array $cart_ids, $orderInfo = [])
  360. {
  361. $ids = array_unique(array_column($cart_ids, 'cart_id'));
  362. if (!$cart_ids || !$ids) {
  363. return false;
  364. }
  365. if (!$orderInfo) {
  366. $orderInfo = $this->dao->get($id, ['*']);
  367. }
  368. if (!$orderInfo) {
  369. throw new AdminException(400118);
  370. }
  371. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  372. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  373. $cartInfo = $storeOrderCartInfoServices->getCartColunm(['oid' => $id, 'cart_id' => $ids], '*', 'cart_id');
  374. $cart_data_all = [];
  375. foreach ($cart_ids as $cart) {
  376. $surplus_num = $cartInfo[$cart['cart_id']]['surplus_num'] ?? 0;
  377. if (!isset($cartInfo[$cart['cart_id']]) || !$surplus_num) continue;
  378. $_info = is_string($cartInfo[$cart['cart_id']]['cart_info']) ? json_decode($cartInfo[$cart['cart_id']]['cart_info'], true) : $cartInfo[$cart['cart_id']]['cart_info'];
  379. $cart_data = $cartInfo[$cart['cart_id']];
  380. $cart_data['oid'] = $id;
  381. $cart_data['product_id'] = $_info['product_id'];
  382. $cart_data['old_cart_id'] = $cart['cart_id'];
  383. $cart_data['cart_num'] = $cart['cart_num'];
  384. $cart_data['surplus_num'] = $cart['cart_num'];
  385. $cart_data['split_surplus_num'] = $cart['cart_num'];
  386. $_info = $this->slpitComputeOrderCart($cart_data['cart_num'], $_info);
  387. $_info['id'] = $cart_data['cart_id'];
  388. $cart_data['cart_info'] = $_info;
  389. $cart_data_all[] = $cart_data;
  390. unset($cartInfo[$cart['cart_id']]);
  391. }
  392. return $cart_data_all;
  393. }
  394. }