StoreOrderCreateServices.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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\jobs\activity\StorePromotionsJob;
  13. use app\services\activity\discounts\StoreDiscountsServices;
  14. use app\services\activity\newcomer\StoreNewcomerServices;
  15. use app\services\agent\AgentLevelServices;
  16. use app\services\activity\coupon\StoreCouponUserServices;
  17. use app\services\other\CityAreaServices;
  18. use app\services\pay\PayServices;
  19. use app\services\product\branch\StoreBranchProductServices;
  20. use app\services\product\brand\StoreBrandServices;
  21. use app\services\product\category\StoreProductCategoryServices;
  22. use app\services\product\shipping\ShippingTemplatesFreeServices;
  23. use app\services\product\shipping\ShippingTemplatesRegionServices;
  24. use app\services\product\shipping\ShippingTemplatesServices;
  25. use app\services\wechat\WechatUserServices;
  26. use app\services\BaseServices;
  27. use crmeb\services\CacheService;
  28. use app\dao\order\StoreOrderDao;
  29. use app\services\user\UserServices;
  30. use crmeb\traits\ServicesTrait;
  31. use think\exception\ValidateException;
  32. use app\services\user\UserBillServices;
  33. use app\services\user\UserAddressServices;
  34. use app\services\activity\bargain\StoreBargainServices;
  35. use app\services\activity\seckill\StoreSeckillServices;
  36. use app\services\store\SystemStoreServices;
  37. use app\services\activity\combination\StoreCombinationServices;
  38. use app\services\product\product\StoreProductServices;
  39. use app\services\activity\collage\UserCollageServices;
  40. use app\services\activity\collage\UserCollagePartakeServices;
  41. use think\facade\Cache;
  42. use think\facade\Log;
  43. /**
  44. * 订单创建
  45. * Class StoreOrderCreateServices
  46. * @package app\services\order
  47. * @mixin StoreOrderDao
  48. */
  49. class StoreOrderCreateServices extends BaseServices
  50. {
  51. use ServicesTrait;
  52. /**
  53. * StoreOrderCreateServices constructor.
  54. * @param StoreOrderDao $dao
  55. */
  56. public function __construct(StoreOrderDao $dao)
  57. {
  58. $this->dao = $dao;
  59. }
  60. /**
  61. * 使用雪花算法生成订单ID
  62. * @return string
  63. * @throws \Exception
  64. */
  65. public function getNewOrderId(string $prefix = 'wx')
  66. {
  67. $snowflake = new \Godruoyi\Snowflake\Snowflake();
  68. $is_callable = function ($currentTime) {
  69. // if (is_win()) {
  70. $redis = Cache::store('redis');
  71. $swooleSequenceResolver = new \Godruoyi\Snowflake\RedisSequenceResolver($redis->handler());
  72. return $swooleSequenceResolver->sequence($currentTime);
  73. // } else {
  74. // $swooleSequenceResolver = new \Godruoyi\Snowflake\SwooleSequenceResolver();
  75. // return $swooleSequenceResolver->sequence($currentTime);
  76. // }
  77. };
  78. //32位
  79. if (PHP_INT_SIZE == 4) {
  80. $id = abs($snowflake->setSequenceResolver($is_callable)->id());
  81. } else {
  82. $id = $snowflake->setStartTimeStamp(strtotime('2020-06-05') * 1000)->setSequenceResolver($is_callable)->id();
  83. }
  84. return $prefix . $id;
  85. }
  86. /**
  87. * 核销订单生成核销码
  88. * @return false|string
  89. */
  90. public function getStoreCode()
  91. {
  92. mt_srand();
  93. [$msec, $sec] = explode(' ', microtime());
  94. $num = time() + mt_rand(10, 999999) . '' . substr($msec, 2, 3);//生成随机数
  95. if (strlen($num) < 12)
  96. $num = str_pad((string)$num, 12, 0, STR_PAD_RIGHT);
  97. else
  98. $num = substr($num, 0, 12);
  99. if ($this->dao->count(['verify_code' => $num])) {
  100. return $this->getStoreCode();
  101. }
  102. return $num;
  103. }
  104. /**
  105. * 创建订单
  106. * @param int $uid
  107. * @param string $key
  108. * @param array $cartGroup
  109. * @param int $addressId
  110. * @param string $payType
  111. * @param array $addressInfo
  112. * @param array $userInfo
  113. * @param bool $useIntegral
  114. * @param int $couponId
  115. * @param string $mark
  116. * @param int $pinkId
  117. * @param int $isChannel
  118. * @param int $shippingType
  119. * @param int $storeId
  120. * @param false $news
  121. * @param array $customForm
  122. * @param int $invoice_id
  123. * @param string $from
  124. * @return mixed
  125. * @throws \think\db\exception\DataNotFoundException
  126. * @throws \think\db\exception\DbException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. */
  129. public function createOrder(int $uid, string $key, array $cartGroup, int $addressId, string $payType, array $addressInfo, array $userInfo = [], bool $useIntegral = false, $couponId = 0, $mark = '', $pinkId = 0, $isChannel = 0, $shippingType = 1, $storeId = 0, $news = false, $customForm = [], int $invoice_id = 0, string $from = '', int $collate_code_id = 0)
  130. {
  131. /** @var StoreOrderComputedServices $computedServices */
  132. $computedServices = app()->make(StoreOrderComputedServices::class);
  133. $priceData = $computedServices->computedOrder($uid, $userInfo, $cartGroup, $addressId, $payType, $useIntegral, $couponId, $shippingType);
  134. $cartInfo = $cartGroup['cartInfo'];
  135. $priceGroup = $cartGroup['priceGroup'];
  136. if ($priceGroup['awardPrice'] > 0 && $useIntegral) throw new ValidateException('增值商品不能使用消费分抵扣!');
  137. $cartIds = [];
  138. $totalNum = 0;
  139. $gainIntegral = 0;
  140. foreach ($cartInfo as $cart) {
  141. $cartIds[] = $cart['id'];
  142. $totalNum += $cart['cart_num'];
  143. $cartInfoGainIntegral = isset($cart['productInfo']['give_integral']) ? bcmul((string)$cart['cart_num'], (string)$cart['productInfo']['give_integral'], 0) : 0;
  144. $gainIntegral = bcadd((string)$gainIntegral, (string)$cartInfoGainIntegral, 0);
  145. }
  146. $deduction = $cartGroup['deduction'];
  147. $other = $cartGroup['other'];
  148. $promotions_give = [
  149. 'give_integral' => $other['give_integral'] ?? 0,
  150. 'give_coupon' => $other['give_coupon'] ?? [],
  151. 'give_product' => $other['give_product'] ?? [],
  152. 'promotions' => $other['promotions'] ?? []
  153. ];
  154. $type = (int)$deduction['type'] ?? 0;
  155. $activity_id = (int)$deduction['activity_id'] ?? 0;
  156. $collateCodeId = (int)$deduction['collate_code_id'] ?? 0;
  157. $product_type = (int)$deduction['product_type'] ?? 0;
  158. /** @var UserCollageServices $collageServices */
  159. $collageServices = app()->make(UserCollageServices::class);
  160. if (in_array($type, [1, 2, 3, 5])) {
  161. $couponId = 0;
  162. if ($type != 5) $useIntegral = false;
  163. $systemPayType = PayServices::PAY_TYPE;
  164. unset($systemPayType['offline']);
  165. if ($from != 'pc' && !array_key_exists($payType, $systemPayType)) {
  166. throw new ValidateException('营销商品不能使用线下支付!');
  167. }
  168. } else if ($type == 8) {
  169. $gainIntegral = 0;
  170. } else if ($type == 9 || $type == 10) {
  171. if ($collateCodeId != $collate_code_id) throw new ValidateException('拼单/桌码ID有误!');
  172. $status = $collageServices->value(['id' => $collate_code_id], 'status');
  173. if ($status >= 2) throw new ValidateException($type == 10 ? '桌码' : '拼单' . '已生成订单!');
  174. $activity_id = $collate_code_id;
  175. }
  176. //$shipping_type = 1 快递发货 $shipping_type = 2 门店自提
  177. if (!sys_config('store_func_status', 1) || !sys_config('store_self_mention', 1)) $shippingType = 1;
  178. $userAddress = $addressInfo['province'] . ' ' . $addressInfo['city'] . ' ' . $addressInfo['district'] . ' ' . $addressInfo['street'] . ' ' . $addressInfo['detail'];
  179. $userLocation = $addressInfo['longitude'] . ' ' . $addressInfo['latitude'];
  180. $orderInfo = [
  181. 'uid' => $uid,
  182. 'type' => $type,
  183. 'order_id' => $this->getNewOrderId(),
  184. 'real_name' => $addressInfo['real_name'],
  185. 'user_phone' => $addressInfo['phone'],
  186. 'user_address' => $userAddress,
  187. 'order_province' => $addressInfo['province'],
  188. 'order_city' => $addressInfo['city'],
  189. 'order_district' => $addressInfo['district'],
  190. 'send_area_award' => 0,
  191. 'user_location' => $userLocation,
  192. 'cart_id' => $cartIds,
  193. 'total_num' => $totalNum,
  194. 'total_price' => $priceGroup['sumPrice'] ?? $priceGroup['totalPrice'],
  195. 'total_postage' => $priceData['total_postage'] ?? $priceGroup['storePostage'],
  196. 'coupon_id' => $couponId,
  197. 'coupon_price' => $priceData['coupon_price'],
  198. 'first_order_price' => $priceData['first_order_price'],
  199. 'promotions_price' => $priceData['promotions_price'],
  200. 'pay_price' => $priceData['pay_price'],
  201. 'pay_postage' => $priceData['pay_postage'],
  202. 'deduction_price' => $priceData['deduction_price'],
  203. 'paid' => 0,
  204. 'pay_type' => $payType,
  205. 'use_integral' => $priceData['usedIntegral'],
  206. 'gain_integral' => $gainIntegral,
  207. 'mark' => htmlspecialchars($mark),
  208. 'product_type' => $product_type,
  209. 'activity_id' => $activity_id,
  210. 'pink_id' => $pinkId,
  211. 'cost' => $priceGroup['costPrice'],
  212. 'award_price' => $priceGroup['awardPrice'],
  213. 'is_channel' => $isChannel,
  214. 'add_time' => time(),
  215. 'unique' => $key,
  216. 'shipping_type' => $shippingType,
  217. 'channel_type' => $userInfo['user_type'],
  218. 'province' => '',
  219. 'spread_uid' => 0,
  220. 'spread_two_uid' => 0,
  221. 'custom_form' => json_encode($customForm),
  222. 'promotions_give' => json_encode($promotions_give),
  223. 'give_integral' => $promotions_give['give_integral'] ?? 0,
  224. 'give_coupon' => implode(',', $promotions_give['give_coupon'] ?? []),
  225. 'store_id' => $storeId
  226. ];
  227. if ($orderInfo['award_price'] > sys_config('max_price', 0) && sys_config('max_price', 0) > 0 && $orderInfo['award_price'] > 0) {
  228. throw new ValidateException('下单金额不超过' . sys_config('max_price'));
  229. }
  230. if ($orderInfo['award_price'] < sys_config('min_price', 0) && sys_config('min_price', 0) > 0 && $orderInfo['award_price'] > 0) {
  231. throw new ValidateException('下单金额不低于' . sys_config('min_price'));
  232. }
  233. $orderPre = $this->dao->search(['uid' => $uid, 'is_del' => 0, 'is_system_del' => 0])->order('add_time', 'desc')->find();
  234. if ($orderPre && $orderPre['add_time'] > (time() - (sys_config('order_time', 0) * 60))) {
  235. throw new ValidateException('请等待' . ((sys_config('order_time', 0) * 60) - (time() - $orderPre['add_time'])) . '秒后再下单');
  236. }
  237. if ($userInfo['user_type'] == 'wechat' || $userInfo['user_type'] == 'routine') {
  238. /** @var WechatUserServices $wechatServices */
  239. $wechatServices = app()->make(WechatUserServices::class);
  240. $orderInfo['province'] = $wechatServices->value(['uid' => $uid, 'user_type' => $userInfo['user_type']], 'province') ?: '';
  241. }
  242. if ($shippingType == 2) {
  243. $orderInfo['verify_code'] = $this->getStoreCode();
  244. /** @var SystemStoreServices $storeServices */
  245. $storeServices = app()->make(SystemStoreServices::class);
  246. $orderInfo['store_id'] = $storeServices->getStoreDisposeCache($storeId, 'id');
  247. if (!$orderInfo['store_id']) {
  248. throw new ValidateException('暂无门店无法选择门店自提');
  249. }
  250. }
  251. $priceData['coupon_id'] = $couponId;
  252. $order = $this->transaction(function () use ($cartIds, $couponId, $orderInfo, $cartInfo, $key, $userInfo, $useIntegral, $priceData, $type, $activity_id, $uid, $addressId, $promotions_give, $storeId) {
  253. //创建订单
  254. $order = $this->dao->save($orderInfo);
  255. if ($couponId) {
  256. /** @var StoreCouponUserServices $couponServices */
  257. $couponServices = app()->make(StoreCouponUserServices::class);
  258. $couponServices->useCoupon($couponId, (int)$userInfo['uid'], $cartInfo, [], $storeId);
  259. }
  260. //抵扣积分
  261. $this->deductIntegral($userInfo, $useIntegral, $priceData, (int)$userInfo['uid'], $key);
  262. //扣库存
  263. $this->decGoodsStock($cartInfo, $type, $activity_id, $orderInfo['store_id'] ?? 0);
  264. //保存购物车商品信息
  265. /** @var StoreOrderCartInfoServices $cartServices */
  266. $cartServices = app()->make(StoreOrderCartInfoServices::class);
  267. $cartServices->setCartInfo($order['id'], $cartInfo, (int)$uid, $promotions_give['promotions'] ?? []);
  268. return $order;
  269. });
  270. if (in_array($type, [9, 10]) && $collate_code_id > 0 && $order) {
  271. //关联订单和拼单、桌码
  272. $collageServices->update($collate_code_id, ['oid' => $order['id'], 'status' => 2]);
  273. /** @var UserCollagePartakeServices $partakeService */
  274. $partakeService = app()->make(UserCollagePartakeServices::class);
  275. $partakeService->update(['collate_code_id' => $collate_code_id, 'is_settle' => 0], ['status' => 0]);
  276. }
  277. //保存购物车商品信息
  278. // StoreCartJob::dispatch([$order['id'], $cartInfo, $uid, $promotions_give['promotions'] ?? []]);
  279. //扣除优惠活动赠品限量
  280. StorePromotionsJob::dispatchDo('changeGiveLimit', [$promotions_give]);
  281. //订单创建事件
  282. event('order.create', [$order, $userInfo, compact('cartInfo', 'priceData', 'addressId', 'cartIds', 'news'), compact('type', 'activity_id'), $invoice_id]);
  283. return $order;
  284. }
  285. /**
  286. * 抵扣积分
  287. * @param array $userInfo
  288. * @param bool $useIntegral
  289. * @param array $priceData
  290. * @param int $uid
  291. * @param string $key
  292. */
  293. public function deductIntegral(array $userInfo, bool $useIntegral, array $priceData, int $uid, string $key)
  294. {
  295. $res2 = true;
  296. if (sys_config('integral_ratio_status', 1) && $userInfo && $useIntegral && $userInfo['integral'] > 0) {
  297. /** @var UserServices $userServices */
  298. $userServices = app()->make(UserServices::class);
  299. if (!$priceData['SurplusIntegral']) {
  300. $integral = 0;
  301. } else {
  302. $integral = bcsub((string)$userInfo['integral'], (string)$priceData['usedIntegral']);
  303. }
  304. $res2 = false !== $userServices->update($uid, ['integral' => $integral]);
  305. /** @var UserBillServices $userBillServices */
  306. $userBillServices = app()->make(UserBillServices::class);
  307. $res3 = $userBillServices->income('deduction', $uid, [
  308. 'number' => (int)$priceData['usedIntegral'],
  309. 'deductionPrice' => $priceData['deduction_price']
  310. ], $integral, $key);
  311. $res2 = $res2 && false != $res3;
  312. }
  313. if (!$res2) {
  314. throw new ValidateException('使用积分抵扣失败!');
  315. }
  316. }
  317. /**
  318. * 扣库存
  319. * @param array $cartInfo
  320. * @param int $type
  321. * @param int $activity_id
  322. * @param int $store_id
  323. */
  324. public function decGoodsStock(array $cartInfo, int $type, int $activity_id, int $store_id = 0)
  325. {
  326. $res5 = true;
  327. /** @var StoreProductServices $services */
  328. $services = app()->make(StoreProductServices::class);
  329. /** @var StoreSeckillServices $seckillServices */
  330. $seckillServices = app()->make(StoreSeckillServices::class);
  331. /** @var StoreCombinationServices $pinkServices */
  332. $pinkServices = app()->make(StoreCombinationServices::class);
  333. /** @var StoreBargainServices $bargainServices */
  334. $bargainServices = app()->make(StoreBargainServices::class);
  335. /** @var StoreDiscountsServices $discountServices */
  336. $discountServices = app()->make(StoreDiscountsServices::class);
  337. /** @var StoreNewcomerServices $storeNewcomerServices */
  338. $storeNewcomerServices = app()->make(StoreNewcomerServices::class);
  339. try {
  340. foreach ($cartInfo as $cart) {
  341. $unique = isset($cart['productInfo']['attrInfo']) ? $cart['productInfo']['attrInfo']['unique'] : '';
  342. $cart_num = (int)$cart['cart_num'];
  343. //减库存加销量
  344. switch ($type) {
  345. case 0://普通
  346. case 6://预售
  347. case 8://抽奖
  348. case 9://拼单
  349. case 10://桌码
  350. $res5 = $res5 && $services->decProductStock($cart_num, (int)$cart['productInfo']['id'], $unique, $store_id);
  351. break;
  352. case 1://秒杀
  353. $res5 = $res5 && $seckillServices->decSeckillStock($cart_num, $activity_id, $unique, $store_id);
  354. break;
  355. case 2://砍价
  356. $res5 = $res5 && $bargainServices->decBargainStock($cart_num, $activity_id, $unique, $store_id);
  357. break;
  358. case 3://拼团
  359. $res5 = $res5 && $pinkServices->decCombinationStock($cart_num, $activity_id, $unique, $store_id);
  360. break;
  361. case 5://套餐
  362. $res5 = $res5 && $discountServices->decDiscountStock($cart_num, $activity_id, (int)($cart['discount_product_id'] ?? 0), (int)($cart['product_id'] ?? 0), $unique, $store_id);
  363. break;
  364. case 7://新人专享
  365. $res5 = $res5 && $storeNewcomerServices->decNewcomerStock($cart_num, $activity_id, $unique, $store_id);
  366. break;
  367. default:
  368. $res5 = $res5 && $services->decProductStock($cart_num, (int)$cart['productInfo']['id'], $unique, $store_id);
  369. break;
  370. }
  371. }
  372. if ($type == 5 && $activity_id) {
  373. //改变套餐限量
  374. $res5 = $res5 && $discountServices->changeDiscountLimit($activity_id);
  375. }
  376. if (!$res5) {
  377. throw new ValidateException('库存不足!');
  378. }
  379. } catch (\Throwable $e) {
  380. throw new ValidateException('库存不足!');
  381. }
  382. }
  383. /**
  384. * 订单创建后的后置事件
  385. * @param UserAddressServices $addressServices
  386. * @param $order
  387. * @param array $group
  388. */
  389. public function orderCreateAfter($order, array $group)
  390. {
  391. /** @var UserAddressServices $addressServices */
  392. $addressServices = app()->make(UserAddressServices::class);
  393. //设置用户默认地址
  394. if ($order['uid'] && isset($group['addressId']) && $group['addressId'] && !$addressServices->be(['is_default' => 1, 'uid' => $order['uid']])) {
  395. $addressServices->setDefaultAddress($group['addressId'], $order['uid']);
  396. }
  397. //删除购物车
  398. if (isset($group['news']) && $group['news']) {
  399. array_map(function ($key) {
  400. CacheService::redisHandler()->delete($key);
  401. }, $group['cartIds']);
  402. } else {
  403. if (!isset($group['delCart']) || (isset($group['delCart']) && $group['delCart'] !== false)) {
  404. /** @var StoreCartServices $cartServices */
  405. $cartServices = app()->make(StoreCartServices::class);
  406. $cartServices->deleteCartStatus($group['cartIds'] ?? []);
  407. }
  408. }
  409. }
  410. /**
  411. * 计算订单每个商品真实付款价格
  412. * @param array $orderInfo
  413. * @param array $cartInfo
  414. * @param array $priceData
  415. * @param $addressId
  416. * @param int $uid
  417. * @param $userInfo
  418. * @return array
  419. */
  420. public function computeOrderProductTruePrice($orderInfo, array $cartInfo, array $priceData, $addressId, int $uid, $userInfo)
  421. {
  422. //统一放入默认数据
  423. foreach ($cartInfo as &$cart) {
  424. $cart['use_integral'] = 0;
  425. $cart['integral_price'] = 0.00;
  426. if (!isset($cart['coupon_price'])) {
  427. $cart['coupon_price'] = 0.00;
  428. }
  429. $cart['first_order_price'] = 0.00;
  430. $cart['one_brokerage'] = 0.00;
  431. $cart['two_brokerage'] = 0.00;
  432. }
  433. try {
  434. $promotionsGice = isset($orderInfo['promotions_give']) ? (is_string($orderInfo['promotions_give']) ? json_decode($orderInfo['promotions_give'], true) : $orderInfo['promotions_give']) : [];
  435. $promotions = [];
  436. if (isset($promotionsGice['promotions']) && $promotionsGice['promotions']) {
  437. $promotions = $promotionsGice['promotions'];
  438. }
  439. //$cartInfo = $this->computeOrderProductCoupon($cartInfo, $priceData, $promotions);
  440. $cartInfo = $this->computeOrderProductIntegral($cartInfo, $priceData);
  441. // $cartInfo = $this->computeOrderProductPostage($cartInfo, $priceData);
  442. $cartInfo = $this->computeOrderProductFirstDiscount($cartInfo, $priceData);
  443. } catch (\Throwable $e) {
  444. Log::error('订单商品结算失败,File:' . $e->getFile() . ',Line:' . $e->getLine() . ',Message:' . $e->getMessage());
  445. throw new ValidateException('订单商品结算失败');
  446. }
  447. //truePice实际支付单价(存在)
  448. //几件商品总体优惠 以及积分抵扣金额
  449. foreach ($cartInfo as &$cart) {
  450. $coupon_price = $cart['coupon_price'] ?? 0;
  451. $integral_price = $cart['integral_price'] ?? 0;
  452. $first_order_price = $cart['first_order_price'] ?? 0;
  453. $cart['sum_true_price'] = bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
  454. if ($coupon_price) {
  455. $cart['sum_true_price'] = bcsub((string)$cart['sum_true_price'], (string)$coupon_price, 2);
  456. $uni_coupon_price = (string)bcdiv((string)$coupon_price, (string)$cart['cart_num'], 4);
  457. $cart['truePrice'] = $cart['truePrice'] > $uni_coupon_price ? bcsub((string)$cart['truePrice'], $uni_coupon_price, 2) : 0;
  458. }
  459. if ($integral_price) {
  460. $cart['sum_true_price'] = bcsub((string)$cart['sum_true_price'], (string)$integral_price, 2);
  461. $uni_integral_price = (string)bcdiv((string)$integral_price, (string)$cart['cart_num'], 4);
  462. $cart['truePrice'] = $cart['truePrice'] > $uni_integral_price ? bcsub((string)$cart['truePrice'], $uni_integral_price, 2) : 0;
  463. }
  464. if ($first_order_price) {
  465. $cart['sum_true_price'] = bcsub((string)$cart['sum_true_price'], (string)$first_order_price, 2);
  466. $uni_first_order_price = (string)bcdiv((string)$first_order_price, (string)$cart['cart_num'], 4);
  467. $cart['truePrice'] = $cart['truePrice'] > $uni_first_order_price ? bcsub((string)$cart['truePrice'], $uni_first_order_price, 2) : 0;
  468. }
  469. }
  470. //计算佣金:1售价2实际支付金额
  471. [$cartInfo, $spread_ids] = $this->computeOrderProductBrokerage($uid, $cartInfo, $userInfo);
  472. return [$cartInfo, $spread_ids];
  473. }
  474. /**
  475. * 计算每个商品实际支付运费
  476. * @param array $cartInfo
  477. * @param array $priceData
  478. * @return array
  479. */
  480. public function computeOrderProductPostage(array $cartInfo, array $priceData, $addressId)
  481. {
  482. $storePostage = $priceData['pay_postage'] ?? 0;
  483. if ($storePostage) {
  484. /** @var UserAddressServices $addressServices */
  485. $addressServices = app()->make(UserAddressServices::class);
  486. $addr = $addressServices->getAdderssCache($addressId);
  487. if ($addr) {
  488. //按照运费模板计算每个运费模板下商品的件数/重量/体积以及总金额 按照首重倒序排列
  489. $cityId = $addr['city_id'] ?? 0;
  490. $ids = [];
  491. if ($cityId) {
  492. /** @var CityAreaServices $cityAreaServices */
  493. $cityAreaServices = app()->make(CityAreaServices::class);
  494. $ids = $cityAreaServices->getRelationCityIds($cityId);
  495. }
  496. $cityIds = array_merge([0], $ids);
  497. $tempIds[] = 1;
  498. foreach ($cartInfo as $key_c => $item_c) {
  499. $tempIds[] = $item_c['productInfo']['temp_id'];
  500. }
  501. $tempIds = array_unique($tempIds);
  502. /** @var ShippingTemplatesServices $shippServices */
  503. $shippServices = app()->make(ShippingTemplatesServices::class);
  504. $temp = $shippServices->getShippingColumn(['id' => $tempIds], 'type,appoint', 'id');
  505. /** @var ShippingTemplatesRegionServices $regionServices */
  506. $regionServices = app()->make(ShippingTemplatesRegionServices::class);
  507. $regions = $regionServices->getTempRegionList($tempIds, $cityIds, 'temp_id,first,first_price,continue,continue_price', 'temp_id');
  508. $temp_num = [];
  509. foreach ($cartInfo as $cart) {
  510. $tempId = $cart['productInfo']['temp_id'] ?? 1;
  511. $type = isset($temp[$tempId]['type']) ? $temp[$tempId]['type'] : $temp[1]['type'];
  512. if ($type == 1) {
  513. $num = $cart['cart_num'];
  514. } elseif ($type == 2) {
  515. $num = $cart['cart_num'] * $cart['productInfo']['attrInfo']['weight'];
  516. } else {
  517. $num = $cart['cart_num'] * $cart['productInfo']['attrInfo']['volume'];
  518. }
  519. $region = isset($regions[$tempId]) ? $regions[$tempId] : $regions[1];
  520. if (!isset($temp_num[$tempId])) {
  521. $temp_num[$tempId] = [
  522. 'cart_id' => [$cart['id']],
  523. 'number' => $num,
  524. 'type' => $type,
  525. 'price' => bcmul($cart['cart_num'], $cart['truePrice'], 2),
  526. 'first' => $region['first'],
  527. 'first_price' => $region['first_price'],
  528. 'continue' => $region['continue'],
  529. 'continue_price' => $region['continue_price'],
  530. 'temp_id' => $tempId
  531. ];
  532. } else {
  533. $temp_num[$tempId]['cart_id'][] = $cart['id'];
  534. $temp_num[$tempId]['number'] += $num;
  535. $temp_num[$tempId]['price'] += bcmul($cart['cart_num'], $cart['truePrice'], 2);
  536. }
  537. }
  538. $cartInfo = array_combine(array_column($cartInfo, 'id'), $cartInfo);
  539. /** @var ShippingTemplatesFreeServices $freeServices */
  540. $freeServices = app()->make(ShippingTemplatesFreeServices::class);
  541. $freeList = $freeServices->isFreeList($tempIds, $cityIds, 0, 'temp_id,number,price', 'temp_id');
  542. if ($freeList) {
  543. foreach ($temp_num as $k => $v) {
  544. if (isset($temp[$v['temp_id']]['appoint']) && $temp[$v['temp_id']]['appoint'] && isset($freeList[$v['temp_id']])) {
  545. $free = $freeList[$v['temp_id']];
  546. $condition = $free['number'] <= $v['number'];
  547. if ($free['price'] <= $v['price'] && $condition) {
  548. //免运费
  549. foreach ($v['cart_id'] as $c_id) {
  550. if (isset($cartInfo[$c_id])) $cartInfo[$c_id]['postage_price'] = 0.00;
  551. }
  552. }
  553. }
  554. }
  555. }
  556. $count = 0;
  557. $compute_price = 0.00;
  558. $total_price = 0;
  559. $postage_price = 0.00;
  560. foreach ($cartInfo as $cart) {
  561. if (isset($cart['postage_price'])) {//免运费
  562. continue;
  563. }
  564. if (isset($cart['is_gift']) && $cart['is_gift'] == 1) {
  565. continue;
  566. }
  567. $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
  568. $count++;
  569. }
  570. foreach ($cartInfo as &$cart) {
  571. if (isset($cart['postage_price'])) {//免运费
  572. continue;
  573. }
  574. if (isset($cart['is_gift']) && $cart['is_gift'] == 1) {
  575. continue;
  576. }
  577. if ($count > 1) {
  578. $postage_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$storePostage, 2);
  579. $compute_price = bcadd((string)$compute_price, (string)$postage_price, 2);
  580. } else {
  581. $postage_price = bcsub((string)$storePostage, $compute_price, 2);
  582. }
  583. $cart['postage_price'] = $postage_price;
  584. $count--;
  585. }
  586. $cartInfo = array_merge($cartInfo);
  587. }
  588. }
  589. return $cartInfo;
  590. }
  591. /**
  592. * 计算订单商品积分实际抵扣金额
  593. * @param array $cartInfo
  594. * @param array $priceData
  595. * @return array
  596. */
  597. public function computeOrderProductIntegral(array $cartInfo, array $priceData)
  598. {
  599. $usedIntegral = $priceData['usedIntegral'] ?? 0;
  600. $deduction_price = $priceData['deduction_price'] ?? 0;
  601. if ($deduction_price) {
  602. $count = 0;
  603. $total_price = 0.00;
  604. $compute_price = 0.00;
  605. $integral_price = 0.00;
  606. $use_integral = 0;
  607. $compute_integral = 0;
  608. foreach ($cartInfo as $cart) {
  609. if (isset($cart['is_gift']) && $cart['is_gift'] == 1) {
  610. continue;
  611. }
  612. $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
  613. $count++;
  614. }
  615. if ($total_price == $deduction_price) {
  616. $ratio = 1;
  617. } else {
  618. $ratio = bcdiv((string)$deduction_price, (string)$total_price, 4);
  619. }
  620. foreach ($cartInfo as &$cart) {
  621. if (isset($cart['is_gift']) && $cart['is_gift'] == 1) {
  622. continue;
  623. }
  624. if ($count > 1) {
  625. $integral_price = bcmul((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$ratio, 2);
  626. $compute_price = bcadd((string)$compute_price, (string)$integral_price, 2);
  627. $use_integral = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$usedIntegral, 0);
  628. $compute_integral = bcadd((string)$compute_integral, $use_integral, 0);
  629. } else {
  630. $integral_price = bcsub((string)$deduction_price, $compute_price, 2);
  631. $use_integral = bcsub((string)$usedIntegral, $compute_integral, 0);
  632. }
  633. $count--;
  634. $cart['integral_price'] = $integral_price;
  635. $cart['use_integral'] = $use_integral;
  636. }
  637. }
  638. return $cartInfo;
  639. }
  640. /**
  641. * 计算订单商品优惠券实际抵扣金额
  642. * @param array $cartInfo
  643. * @param array $priceData
  644. * @return array
  645. */
  646. public function computeOrderProductCoupon(array $cartInfo, array $priceData, array $promotions = [], int $store_id = 0)
  647. {
  648. if ($priceData['coupon_id'] && $priceData['coupon_price'] ?? 0) {
  649. $count = 0;
  650. $total_price = 0.00;
  651. $compute_price = 0.00;
  652. $coupon_price = 0.00;
  653. /** @var StoreCouponUserServices $couponServices */
  654. $couponServices = app()->make(StoreCouponUserServices::class);
  655. $couponInfo = $couponServices->getOne(['id' => $priceData['coupon_id']], '*', ['issue']);
  656. if ($couponInfo) {
  657. //验证是否适用门店
  658. if ($store_id && isset($couponInfo['applicable_type']) && isset($couponInfo['applicable_store_id'])) {
  659. $applicable_store_id = is_array($couponInfo['applicable_store_id']) ? $couponInfo['applicable_store_id'] : explode(',', $couponInfo['applicable_store_id']);
  660. //活动不适用该门店
  661. if ($couponInfo['applicable_type'] == 0 || ($couponInfo['applicable_type'] == 2 && !in_array($store_id, $applicable_store_id))) {
  662. return [[], 0];
  663. }
  664. }
  665. $promotionsList = [];
  666. if ($promotions) {
  667. $promotionsList = array_combine(array_column($promotions, 'id'), $promotions);
  668. }
  669. $isOverlay = function ($cart) use ($promotionsList) {
  670. $productInfo = $cart['productInfo'] ?? [];
  671. if (!$productInfo) {
  672. return false;
  673. }
  674. //门店独立商品 不使用优惠券
  675. $isBranchProduct = isset($productInfo['type']) && isset($productInfo['pid']) && $productInfo['type'] == 1 && !$productInfo['pid'];
  676. if ($isBranchProduct) {
  677. return false;
  678. }
  679. if (isset($cart['promotions_id']) && $cart['promotions_id']) {
  680. foreach ($cart['promotions_id'] as $key => $promotions_id) {
  681. $promotions = $promotionsList[$promotions_id] ?? [];
  682. if ($promotions && $promotions['promotions_type'] != 4) {
  683. $overlay = is_string($promotions['overlay']) ? explode(',', $promotions['overlay']) : $promotions['overlay'];
  684. if (!in_array(5, $overlay)) {
  685. return false;
  686. }
  687. }
  688. }
  689. }
  690. return true;
  691. };
  692. $type = $couponInfo['applicable_type'] ?? 0;
  693. $counpon_id = $couponInfo['id'];
  694. switch ($type) {
  695. case 0:
  696. foreach ($cartInfo as $cart) {
  697. if (!$isOverlay($cart) || (isset($cart['is_gift']) && $cart['is_gift'] == 1)) continue;
  698. $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
  699. $count++;
  700. }
  701. foreach ($cartInfo as &$cart) {
  702. $cart['coupon_id'] = 0;
  703. $cart['coupon_price'] = 0;
  704. if (!$isOverlay($cart) || (isset($cart['is_gift']) && $cart['is_gift'] == 1)) continue;
  705. if ($count > 1) {
  706. $coupon_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$priceData['coupon_price'], 2);
  707. $compute_price = bcadd((string)$compute_price, (string)$coupon_price, 2);
  708. } else {
  709. $coupon_price = bcsub((string)$priceData['coupon_price'], $compute_price, 2);
  710. }
  711. $cart['coupon_price'] = $coupon_price;
  712. $cart['coupon_id'] = $counpon_id;
  713. $count--;
  714. }
  715. break;
  716. case 1://品类券
  717. /** @var StoreProductCategoryServices $storeCategoryServices */
  718. $storeCategoryServices = app()->make(StoreProductCategoryServices::class);
  719. $cateGorys = $storeCategoryServices->getAllById((int)$couponInfo['category_id']);
  720. if ($cateGorys) {
  721. $cateIds = array_column($cateGorys, 'id');
  722. foreach ($cartInfo as $cart) {
  723. if (!$isOverlay($cart) || (isset($cart['is_gift']) && $cart['is_gift'] == 1)) continue;
  724. if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
  725. $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
  726. $count++;
  727. }
  728. }
  729. foreach ($cartInfo as &$cart) {
  730. $cart['coupon_id'] = 0;
  731. $cart['coupon_price'] = 0;
  732. if (!$isOverlay($cart) || (isset($cart['is_gift']) && $cart['is_gift'] == 1)) continue;
  733. if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
  734. if ($count > 1) {
  735. $coupon_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$priceData['coupon_price'], 2);
  736. $compute_price = bcadd((string)$compute_price, (string)$coupon_price, 2);
  737. } else {
  738. $coupon_price = bcsub((string)$priceData['coupon_price'], $compute_price, 2);
  739. }
  740. $cart['coupon_id'] = $counpon_id;
  741. $cart['coupon_price'] = $coupon_price;
  742. $count--;
  743. }
  744. }
  745. }
  746. break;
  747. case 2://商品劵
  748. foreach ($cartInfo as $cart) {
  749. if (!$isOverlay($cart) || (isset($cart['is_gift']) && $cart['is_gift'] == 1)) continue;
  750. $product_id = isset($cart['productInfo']['pid']) && $cart['productInfo']['pid'] ? $cart['productInfo']['pid'] : ($cart['product_id'] ?? 0);
  751. if ($product_id && in_array($product_id, explode(',', $couponInfo['product_id']))) {
  752. $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
  753. $count++;
  754. }
  755. }
  756. foreach ($cartInfo as &$cart) {
  757. $cart['coupon_id'] = 0;
  758. $cart['coupon_price'] = 0;
  759. if (!$isOverlay($cart) || (isset($cart['is_gift']) && $cart['is_gift'] == 1)) continue;
  760. $product_id = isset($cart['productInfo']['pid']) && $cart['productInfo']['pid'] ? $cart['productInfo']['pid'] : ($cart['product_id'] ?? 0);
  761. if ($product_id && in_array($product_id, explode(',', $couponInfo['product_id']))) {
  762. if ($count > 1) {
  763. $coupon_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$priceData['coupon_price'], 2);
  764. $compute_price = bcadd((string)$compute_price, (string)$coupon_price, 2);
  765. } else {
  766. $coupon_price = bcsub((string)$priceData['coupon_price'], $compute_price, 2);
  767. }
  768. $cart['coupon_id'] = $counpon_id;
  769. $cart['coupon_price'] = $coupon_price;
  770. $count--;
  771. }
  772. }
  773. break;
  774. case 3://品牌券
  775. /** @var StoreBrandServices $storeBrandServices */
  776. $storeBrandServices = app()->make(StoreBrandServices::class);
  777. $brands = $storeBrandServices->getAllById((int)$couponInfo['brand_id']);
  778. if ($brands) {
  779. $brandIds = array_column($brands, 'id');
  780. foreach ($cartInfo as $cart) {
  781. if (!$isOverlay($cart) || (isset($cart['is_gift']) && $cart['is_gift'] == 1)) continue;
  782. if (isset($cart['productInfo']['brand_id']) && in_array($cart['productInfo']['brand_id'], $brandIds)) {
  783. $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
  784. $count++;
  785. }
  786. }
  787. foreach ($cartInfo as &$cart) {
  788. $cart['coupon_id'] = 0;
  789. $cart['coupon_price'] = 0;
  790. if (!$isOverlay($cart) || (isset($cart['is_gift']) && $cart['is_gift'] == 1)) continue;
  791. if (isset($cart['productInfo']['brand_id']) && in_array($cart['productInfo']['brand_id'], $brandIds)) {
  792. if ($count > 1) {
  793. $coupon_price = bcmul((string)bcdiv((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$total_price, 4), (string)$priceData['coupon_price'], 2);
  794. $compute_price = bcadd((string)$compute_price, (string)$coupon_price, 2);
  795. } else {
  796. $coupon_price = bcsub((string)$priceData['coupon_price'], $compute_price, 2);
  797. }
  798. $cart['coupon_id'] = $counpon_id;
  799. $cart['coupon_price'] = $coupon_price;
  800. $count--;
  801. }
  802. }
  803. }
  804. break;
  805. }
  806. }
  807. }
  808. return $cartInfo;
  809. }
  810. /**
  811. * 计算实际佣金
  812. * @param int $uid
  813. * @param array $cartInfo
  814. * @param $userInfo
  815. * @return array
  816. * @throws \think\db\exception\DataNotFoundException
  817. * @throws \think\db\exception\DbException
  818. * @throws \think\db\exception\ModelNotFoundException
  819. */
  820. public function computeOrderProductBrokerage(int $uid, array $cartInfo, $userInfo)
  821. {
  822. //获取后台一级返佣比例
  823. $storeBrokerageRatio = sys_config('store_brokerage_ratio');
  824. //获取二级返佣比例
  825. $storeBrokerageTwo = sys_config('store_brokerage_two');
  826. //佣金计算方式
  827. $brokerageComputeType = sys_config('brokerage_compute_type', 1);
  828. /** @var AgentLevelServices $agentLevelServices */
  829. $agentLevelServices = app()->make(AgentLevelServices::class);
  830. [$one_brokerage_up, $two_brokerage_up, $spread_uid, $spread_two_uid] = $agentLevelServices->getAgentLevelBrokerage($uid, $userInfo);
  831. // 二级分销开关
  832. if (sys_config('brokerage_level', 2) == 1) {
  833. $storeBrokerageTwo = $spread_two_uid = 0;
  834. }
  835. foreach ($cartInfo as &$cart) {
  836. $oneBrokerage = '0';//一级返佣金额
  837. $twoBrokerage = '0';//二级返佣金额
  838. $cartNum = (string)$cart['cart_num'] ?? '0';
  839. if (isset($cart['productInfo']) && isset($cart['is_gift']) && $cart['is_gift'] == 0) {
  840. $productInfo = $cart['productInfo'];
  841. //指定返佣金额
  842. if (isset($productInfo['is_sub']) && $productInfo['is_sub'] == 1) {
  843. $oneBrokerage = bcmul((string)($productInfo['attrInfo']['brokerage'] ?? '0'), $cartNum, 2);
  844. $twoBrokerage = bcmul((string)($productInfo['attrInfo']['brokerage_two'] ?? '0'), $cartNum, 2);
  845. } else {//比例返佣
  846. $price = 0;
  847. switch ($brokerageComputeType) {
  848. case 1://售价
  849. if (isset($productInfo['attrInfo'])) {
  850. $price = bcmul((string)($productInfo['attrInfo']['price'] ?? '0'), $cartNum, 4);
  851. } else {
  852. $price = bcmul((string)($productInfo['price'] ?? '0'), $cartNum, 4);
  853. }
  854. break;
  855. case 2://实付金额
  856. $price = bcmul((string)($cart['truePrice'] ?? 0), $cartNum, 4);
  857. break;
  858. case 3://商品利润
  859. $price = bcmul(bcsub((string)($cart['truePrice'] ?? 0), (string)($cart['costPrice'] ?? 0), 2), $cartNum, 4);
  860. break;
  861. }
  862. if ($price > 0) {
  863. //一级返佣比例 小于等于零时直接返回 不返佣
  864. if ($storeBrokerageRatio > 0) {
  865. //计算获取一级返佣比例
  866. $brokerageRatio = bcdiv($storeBrokerageRatio, 100, 4);
  867. $oneBrokerage = bcmul((string)$price, (string)$brokerageRatio, 2);
  868. }
  869. //二级返佣比例小于等于0 直接返回
  870. if ($storeBrokerageTwo > 0) {
  871. //计算获取二级返佣比例
  872. $brokerageTwo = bcdiv($storeBrokerageTwo, 100, 4);
  873. $twoBrokerage = bcmul((string)$price, (string)$brokerageTwo, 2);
  874. }
  875. }
  876. }
  877. }
  878. //分销等级上浮佣金
  879. if ($one_brokerage_up) $oneBrokerage = bcadd((string)$oneBrokerage, (string)bcmul((string)$oneBrokerage, (string)bcdiv((string)$one_brokerage_up, '100', 2), 4), 2);
  880. if ($two_brokerage_up) $twoBrokerage = bcadd((string)$twoBrokerage, (string)bcmul((string)$twoBrokerage, (string)bcdiv((string)$two_brokerage_up, '100', 2), 4), 2);
  881. $cart['one_brokerage'] = $oneBrokerage;
  882. $cart['two_brokerage'] = $twoBrokerage;
  883. }
  884. return [$cartInfo, [$spread_uid, $spread_two_uid]];
  885. }
  886. /**
  887. * 计算订单商品信任首单优惠实际抵扣金额
  888. * @param array $cartInfo
  889. * @param array $priceData
  890. * @return array
  891. */
  892. public function computeOrderProductFirstDiscount(array $cartInfo, array $priceData)
  893. {
  894. $first_order_price = $priceData['first_order_price'] ?? 0;
  895. if ($first_order_price) {
  896. $count = 0;
  897. $total_price = 0.00;
  898. $compute_price = 0.00;
  899. $discount_price = 0.00;
  900. foreach ($cartInfo as $cart) {
  901. if (isset($cart['is_gift']) && $cart['is_gift'] == 1) {
  902. continue;
  903. }
  904. $total_price = bcadd((string)$total_price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 4), 2);
  905. $count++;
  906. }
  907. if ($total_price == $first_order_price) {
  908. $ratio = 1;
  909. } else {
  910. $ratio = bcdiv((string)$first_order_price, (string)$total_price, 4);
  911. }
  912. foreach ($cartInfo as &$cart) {
  913. if (isset($cart['is_gift']) && $cart['is_gift'] == 1) {
  914. continue;
  915. }
  916. if ($count > 1) {
  917. $discount_price = bcmul((string)bcmul((string)$cart['cart_num'], (string)$cart['truePrice'], 4), (string)$ratio, 2);
  918. $compute_price = bcadd((string)$compute_price, (string)$discount_price, 2);
  919. } else {
  920. $discount_price = bcsub((string)$first_order_price, $compute_price, 2);
  921. }
  922. $count--;
  923. $cart['first_order_price'] = $discount_price;
  924. }
  925. }
  926. return $cartInfo;
  927. }
  928. }