StoreOrderSplitServices.php 21 KB

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