StoreOrderSplitServices.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. $storeOrderCartInfoServices->clearOrderCartInfo($new_id);
  163. $this->splitComputeOrder((int)$new_id, $cart_data_all, (float)($change_price ? $order_pay_price : 0), (float)$orderInfo['pay_price'], (float)($order['pay_price'] ?? 0));
  164. $new_order = $this->dao->get($new_id);
  165. if ($key == 'new') {
  166. $order = $new_order;
  167. } else {
  168. $otherOrder = $new_order;
  169. }
  170. }
  171. if (!$orderInfo['pid']) $this->dao->update($id, ['pid' => -1]);
  172. //处理申请开票记录
  173. /** @var StoreOrderInvoiceServices $storeOrderInvoiceServics */
  174. $storeOrderInvoiceServics = app()->make(StoreOrderInvoiceServices::class);
  175. $storeOrderInvoiceServics->splitOrderInvoice((int)$id);
  176. return [$order, $otherOrder];
  177. });
  178. }
  179. /**
  180. * 订单拆分
  181. * @param int $id
  182. * @param array $cart_ids
  183. * @param array $orderInfo
  184. * @throws \think\db\exception\DataNotFoundException
  185. * @throws \think\db\exception\DbException
  186. * @throws \think\db\exception\ModelNotFoundException
  187. */
  188. public function split(int $id, array $cart_ids, $orderInfo = [])
  189. {
  190. $ids = array_unique(array_column($cart_ids, 'cart_id'));
  191. if (!$cart_ids || !$ids) {
  192. return false;
  193. }
  194. if (!$orderInfo) {
  195. $orderInfo = $this->dao->get($id, ['*']);
  196. }
  197. if (!$orderInfo) {
  198. throw new AdminException(400118);
  199. }
  200. /** @var StoreOrderCreateServices $storeOrderCreateServices */
  201. $storeOrderCreateServices = app()->make(StoreOrderCreateServices::class);
  202. $orderInfo = is_object($orderInfo) ? $orderInfo->toArray() : $orderInfo;
  203. foreach ($this->order_data as $field) {
  204. unset($orderInfo[$field]);
  205. }
  206. $order_data = $orderInfo;
  207. $order_data['pid'] = $id;
  208. mt_srand();
  209. $order_data['order_id'] = $orderInfo['order_id'] . '_' . rand(100, 999);
  210. $order_data['cart_id'] = [];
  211. $order_data['unique'] = $storeOrderCreateServices->getNewOrderId('');
  212. $order_data['add_time'] = time();
  213. $new_order = $this->dao->save($order_data);
  214. if (!$new_order) {
  215. throw new AdminException(400544);
  216. }
  217. $new_id = (int)$new_order->id;
  218. /** @var StoreOrderStatusServices $statusService */
  219. $statusService = app()->make(StoreOrderStatusServices::class);
  220. $statusService->save([
  221. 'oid' => $new_id,
  222. 'change_type' => 'split_create_order',
  223. 'change_message' => '发货拆分订单生成',
  224. 'change_time' => time()
  225. ]);
  226. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  227. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  228. //订单下原商品信息
  229. $cartInfo = $storeOrderCartInfoServices->getColumn(['oid' => $id, 'cart_id' => $ids], 'cart_num,surplus_num,cart_info', 'cart_id');
  230. $cart_data = $cart_data_all = $update_data = [];
  231. $cart_data['oid'] = $new_id;
  232. foreach ($cart_ids as $cart) {
  233. $surplus_num = $cartInfo[$cart['cart_id']]['surplus_num'] ?? 0;
  234. if (!isset($cartInfo[$cart['cart_id']]) || !$surplus_num) continue;
  235. $_info = is_string($cartInfo[$cart['cart_id']]['cart_info']) ? json_decode($cartInfo[$cart['cart_id']]['cart_info'], true) : $cartInfo[$cart['cart_id']]['cart_info'];
  236. $cart_data['cart_id'] = $storeOrderCreateServices->getNewOrderId('');
  237. $cart_data['product_id'] = $_info['product_id'];
  238. $cart_data['old_cart_id'] = $cart['cart_id'];
  239. $cart_data['cart_num'] = $cart['cart_num'];
  240. $cart_data['unique'] = md5($cart_data['cart_id'] . '_' . $cart_data['oid']);
  241. if ($cart['cart_num'] >= $surplus_num) {//拆分完成
  242. $cart_data['cart_num'] = $surplus_num;
  243. $update_data['split_status'] = 2;
  244. $update_data['surplus_num'] = 0;
  245. } else {//拆分部分数量
  246. $update_data['surplus_num'] = bcsub((string)$surplus_num, $cart['cart_num'], 0);
  247. $update_data['split_status'] = $update_data['surplus_num'] > 0 ? 1 : 2;
  248. }
  249. $_info = $this->slpitComputeOrderCart($cart_data['cart_num'], $_info);
  250. $_info['id'] = $cart_data['cart_id'];
  251. $cart_data['cart_info'] = json_encode($_info);
  252. //修改原来订单商品信息
  253. if (false === $storeOrderCartInfoServices->update(['oid' => $id, 'cart_id' => $cart['cart_id']], $update_data)) {
  254. throw new AdminException(400545);
  255. }
  256. $cart_data_all[] = $cart_data;
  257. }
  258. if (!$storeOrderCartInfoServices->saveAll($cart_data_all)) {
  259. throw new AdminException(400546);
  260. }
  261. $new_order = $this->dao->get($new_id);
  262. $this->splitComputeOrder($new_id, $cart_data_all, $new_order);
  263. return $new_order;
  264. }
  265. /**
  266. * 重新计算新订单中价格等信息
  267. * @param int $id
  268. * @param $orderInfo
  269. * @param array $cart_info_data
  270. */
  271. 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)
  272. {
  273. $order_update['cart_id'] = array_column($cart_info_data, 'cart_id');
  274. $order_update['total_num'] = array_sum(array_column($cart_info_data, 'cart_num'));
  275. $total_price = $coupon_price = $deduction_price = $use_integral = $pay_postage = $gainIntegral = $one_brokerage = $two_brokerage = $staffBrokerage = $agentBrokerage = $divisionBrokerage = 0;
  276. foreach ($cart_info_data as $cart) {
  277. $_info = json_decode($cart['cart_info'], true);
  278. $total_price = bcadd((string)$total_price, (string)$_info['sum_true_price'], 2);
  279. $deduction_price = bcadd((string)$deduction_price, (string)$_info['integral_price'], 2);
  280. $coupon_price = bcadd((string)$coupon_price, (string)$_info['coupon_price'], 2);
  281. $use_integral = bcadd((string)$use_integral, (string)$_info['use_integral'], 0);
  282. $pay_postage = isset($_info['postage_price']) ? bcadd((string)$pay_postage, (string)$_info['postage_price'], 2) : 0;
  283. $cartInfoGainIntegral = bcmul((string)$cart['cart_num'], (string)($_info['productInfo']['give_integral'] ?? '0'), 0);
  284. $gainIntegral = bcadd((string)$gainIntegral, (string)$cartInfoGainIntegral, 0);
  285. $one_brokerage = bcadd((string)$one_brokerage, (string)$_info['one_brokerage'], 2);
  286. $two_brokerage = bcadd((string)$two_brokerage, (string)$_info['two_brokerage'], 2);
  287. $staffBrokerage = bcadd((string)$staffBrokerage, (string)$_info['staff_brokerage'], 2);
  288. $agentBrokerage = bcadd((string)$agentBrokerage, (string)$_info['agent_brokerage'], 2);
  289. $divisionBrokerage = bcadd((string)$divisionBrokerage, (string)$_info['division_brokerage'], 2);
  290. }
  291. $order_update['coupon_id'] = array_unique(array_column($cart_info_data, 'coupon_id'));
  292. $order_update['pay_price'] = bcadd((string)$total_price, (string)$pay_postage, 2);
  293. //有订单原来支付金额 改价订单
  294. if ($order_pay_price) {
  295. if ($pre_pay_price) {//上一个已经计算 这里减法
  296. $order_update['pay_price'] = bcsub((string)$pay_price, (string)$pre_pay_price, 2);
  297. } else {//按比例计算实际支付金额
  298. $order_update['pay_price'] = bcmul((string)bcdiv((string)$order_update['pay_price'], (string)$order_pay_price, 4), (string)$pay_price, 2);
  299. }
  300. }
  301. $order_update['total_price'] = bcadd((string)$total_price, (string)bcadd((string)$deduction_price, (string)$coupon_price, 2), 2);
  302. $order_update['deduction_price'] = $deduction_price;
  303. $order_update['coupon_price'] = $coupon_price;
  304. $order_update['use_integral'] = $use_integral;
  305. $order_update['gain_integral'] = $gainIntegral;
  306. $order_update['pay_postage'] = $pay_postage;
  307. $order_update['one_brokerage'] = $one_brokerage;
  308. $order_update['two_brokerage'] = $two_brokerage;
  309. $order_update['staff_brokerage'] = $staffBrokerage;
  310. $order_update['agent_brokerage'] = $agentBrokerage;
  311. $order_update['division_brokerage'] = $divisionBrokerage;
  312. if (false === $this->dao->update($id, $order_update, 'id')) {
  313. throw new AdminException(400547);
  314. }
  315. return true;
  316. }
  317. /**
  318. * 部分发货重新计算订单商品:实际金额、优惠、积分等金额
  319. * @param int $cart_num
  320. * @param array $cart_info
  321. * @param string $orderType
  322. * @return array
  323. */
  324. public function slpitComputeOrderCart(int $cart_num, array $cart_info, $orderType = 'new')
  325. {
  326. if (!$cart_num || !$cart_info) return [];
  327. if ($cart_num >= $cart_info['cart_num']) return $cart_info;
  328. $new_cart_info = $cart_info;
  329. $new_cart_info['cart_num'] = $cart_num;
  330. $compute_arr = ['coupon_price', 'integral_price', 'postage_price', 'use_integral', 'one_brokerage', 'two_brokerage', 'staff_brokerage', 'agent_brokerage', 'division_brokerage', 'sum_true_price'];
  331. foreach ($compute_arr as $field) {
  332. if (!isset($cart_info[$field]) || !$cart_info[$field]) {
  333. $new_cart_info[$field] = 0;
  334. continue;
  335. }
  336. $scale = 2;
  337. if ($field == 'use_integral') $scale = 0;
  338. $new_cart_info[$field] = bcmul((string)$cart_num, bcdiv((string)$cart_info[$field], (string)$cart_info['cart_num'], 4), $scale);
  339. if ($orderType == 'new') {//拆出
  340. if ($field == 'sum_true_price') {
  341. $new_cart_info[$field] = round(bcmul((string)$cart_num, bcdiv((string)$cart_info[$field], (string)$cart_info['cart_num'], 4), 4), 2, PHP_ROUND_HALF_UP);
  342. } else {
  343. $new_cart_info[$field] = bcmul((string)$cart_num, bcdiv((string)$cart_info[$field], (string)$cart_info['cart_num'], 4), $scale);
  344. }
  345. } else {
  346. if ($field == 'sum_true_price') {
  347. $field_number = round(bcmul((string)bcsub((string)$cart_info['cart_num'], (string)$cart_num, 0), bcdiv((string)$cart_info[$field], (string)$cart_info['cart_num'], 4), 4), 2, PHP_ROUND_HALF_UP);
  348. } else {
  349. $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);
  350. }
  351. $new_cart_info[$field] = bcsub((string)$cart_info[$field], (string)$field_number, $scale);
  352. }
  353. }
  354. return $new_cart_info;
  355. }
  356. /**
  357. * 获取整理后的订单商品信息
  358. * @param int $id
  359. * @param array $cart_ids
  360. * @param array $orderInfo
  361. * @return array|false
  362. * @throws \think\db\exception\DataNotFoundException
  363. * @throws \think\db\exception\DbException
  364. * @throws \think\db\exception\ModelNotFoundException
  365. */
  366. public function getSplitOrderCartInfo(int $id, array $cart_ids, $orderInfo = [])
  367. {
  368. $ids = array_unique(array_column($cart_ids, 'cart_id'));
  369. if (!$cart_ids || !$ids) {
  370. return false;
  371. }
  372. if (!$orderInfo) {
  373. $orderInfo = $this->dao->get($id, ['*']);
  374. }
  375. if (!$orderInfo) {
  376. throw new AdminException(400118);
  377. }
  378. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  379. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  380. $cartInfo = $storeOrderCartInfoServices->getCartColunm(['oid' => $id, 'cart_id' => $ids], '*', 'cart_id');
  381. $cart_data_all = [];
  382. foreach ($cart_ids as $cart) {
  383. $surplus_num = $cartInfo[$cart['cart_id']]['surplus_num'] ?? 0;
  384. if (!isset($cartInfo[$cart['cart_id']]) || !$surplus_num) continue;
  385. $_info = is_string($cartInfo[$cart['cart_id']]['cart_info']) ? json_decode($cartInfo[$cart['cart_id']]['cart_info'], true) : $cartInfo[$cart['cart_id']]['cart_info'];
  386. $cart_data = $cartInfo[$cart['cart_id']];
  387. $cart_data['oid'] = $id;
  388. $cart_data['product_id'] = $_info['product_id'];
  389. $cart_data['old_cart_id'] = $cart['cart_id'];
  390. $cart_data['cart_num'] = $cart['cart_num'];
  391. $cart_data['surplus_num'] = $cart['cart_num'];
  392. $cart_data['split_surplus_num'] = $cart['cart_num'];
  393. $_info = $this->slpitComputeOrderCart($cart_data['cart_num'], $_info);
  394. $_info['id'] = $cart_data['cart_id'];
  395. $cart_data['cart_info'] = $_info;
  396. $cart_data_all[] = $cart_data;
  397. unset($cartInfo[$cart['cart_id']]);
  398. }
  399. return $cart_data_all;
  400. }
  401. }