StoreOrderCreateServices.php 48 KB

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