StoreOrderCreateServices.php 48 KB

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