StoreOrderComputedServices.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\order;
  12. use app\services\BaseServices;
  13. use app\dao\order\StoreOrderDao;
  14. use app\services\pay\PayServices;
  15. use app\services\product\product\StoreCategoryServices;
  16. use app\services\user\member\MemberCardServices;
  17. use app\services\user\UserBillServices;
  18. use app\services\user\UserServices;
  19. use crmeb\exceptions\ApiException;
  20. use app\services\user\UserAddressServices;
  21. use app\services\activity\coupon\StoreCouponUserServices;
  22. use app\services\shipping\ShippingTemplatesFreeServices;
  23. use app\services\shipping\ShippingTemplatesRegionServices;
  24. use app\services\shipping\ShippingTemplatesServices;
  25. /**
  26. * 订单计算金额
  27. * Class StoreOrderComputedServices
  28. * @package app\services\order
  29. */
  30. class StoreOrderComputedServices extends BaseServices
  31. {
  32. /**
  33. * 支付类型
  34. * @var string[]
  35. */
  36. public $payType = ['weixin' => '微信支付', 'yue' => '余额支付', 'offline' => '线下支付', 'pc' => 'pc'];
  37. /**
  38. * 额外参数
  39. * @var array
  40. */
  41. protected $paramData = [];
  42. /**
  43. * StoreOrderComputedServices constructor.
  44. * @param StoreOrderDao $dao
  45. */
  46. public function __construct(StoreOrderDao $dao)
  47. {
  48. $this->dao = $dao;
  49. }
  50. /**
  51. * 设置额外参数
  52. * @param array $paramData
  53. * @return $this
  54. */
  55. public function setParamData(array $paramData)
  56. {
  57. $this->paramData = $paramData;
  58. return $this;
  59. }
  60. /**
  61. * 计算订单金额
  62. * @param int $uid
  63. * @param string $key
  64. * @param array $cartGroup
  65. * @param int $addressId
  66. * @param string $payType
  67. * @param bool $useIntegral
  68. * @param int $couponId
  69. * @param bool $is_create
  70. * @param int $shipping_type
  71. * @return array
  72. */
  73. public function computedOrder(int $uid, array $userInfo = [], array $cartGroup, int $addressId, string $payType, bool $useIntegral = false, int $couponId = 0, bool $isCreate = false, int $shippingType = 1, int $is_gift = 0)
  74. {
  75. $offlinePayStatus = (int)sys_config('offline_pay_status') ?? (int)2;
  76. $systemPayType = PayServices::PAY_TYPE;
  77. if ($offlinePayStatus == 2) unset($systemPayType['offline']);
  78. if (!$userInfo) {
  79. /** @var UserServices $userServices */
  80. $userServices = app()->make(UserServices::class);
  81. $userInfo = $userServices->getUserInfo($uid);
  82. if (!$userInfo) {
  83. throw new ApiException(410032);
  84. }
  85. }
  86. $cartInfo = $cartGroup['cartInfo'];
  87. $priceGroup = $cartGroup['priceGroup'];
  88. $other = $cartGroup['other'];
  89. $payPrice = (float)$priceGroup['totalPrice'];
  90. $addr = $cartGroup['addr'] ?? [];
  91. $postage = $priceGroup;
  92. if (!$addr || $addr['id'] != $addressId) {
  93. /** @var UserAddressServices $addressServices */
  94. $addressServices = app()->make(UserAddressServices::class);
  95. $addr = $addressServices->getAddress($addressId) ?? [];
  96. if ($addr) {
  97. $addr = $addr->toArray();
  98. }
  99. //改变地址重新计算邮费
  100. $postage = [];
  101. }
  102. $combinationId = $this->paramData['combinationId'] ?? 0;
  103. $seckillId = $this->paramData['seckill_id'] ?? 0;
  104. $bargainId = $this->paramData['bargainId'] ?? 0;
  105. $isActivity = $combinationId || $seckillId || $bargainId;
  106. if (!$isActivity) {
  107. //使用优惠劵
  108. [$payPrice, $couponPrice] = $this->useCouponId($couponId, $uid, $cartInfo, $payPrice, $isCreate);
  109. //使用积分
  110. [$payPrice, $deductionPrice, $usedIntegral, $SurplusIntegral] = $this->useIntegral($useIntegral, $userInfo, $payPrice, $other);
  111. }
  112. //计算邮费
  113. [$payPrice, $payPostage, $storePostageDiscount, $storeFreePostage, $isStoreFreePostage] = $this->computedPayPostage($shippingType, $payType, $cartInfo, $addr, $payPrice, $postage, $other, $userInfo, $is_gift);
  114. //赠送商品计算
  115. $payPrice = bcadd($payPrice, $priceGroup['giftPrice'], 2);
  116. $result = [
  117. 'total_price' => $priceGroup['totalPrice'],
  118. 'gift_price' => $priceGroup['giftPrice'],
  119. 'pay_price' => $payPrice > 0 ? $payPrice : 0,
  120. 'pay_postage' => $payPostage,
  121. 'coupon_price' => $couponPrice ?? 0,
  122. 'deduction_price' => $deductionPrice ?? 0,
  123. 'usedIntegral' => $usedIntegral ?? 0,
  124. 'SurplusIntegral' => $SurplusIntegral ?? 0,
  125. 'storePostageDiscount' => $storePostageDiscount ?? 0,
  126. 'isStoreFreePostage' => $isStoreFreePostage ?? false,
  127. 'storeFreePostage' => $storeFreePostage ?? 0
  128. ];
  129. $this->paramData = [];
  130. return $result;
  131. }
  132. /**
  133. * 使用优惠卷
  134. * @param int $couponId
  135. * @param int $uid
  136. * @param $cartInfo
  137. * @param $payPrice
  138. * @param bool $is_create
  139. */
  140. public function useCouponId(int $couponId, int $uid, $cartInfo, $payPrice, bool $isCreate)
  141. {
  142. //使用优惠劵
  143. $res1 = true;
  144. if ($couponId) {
  145. /** @var StoreCouponUserServices $couponServices */
  146. $couponServices = app()->make(StoreCouponUserServices::class);
  147. $couponInfo = $couponServices->getOne([['id', '=', $couponId], ['uid', '=', $uid], ['is_fail', '=', 0], ['status', '=', 0], ['start_time', '<', time()], ['end_time', '>', time()]], '*', ['issue']);
  148. if (!$couponInfo) {
  149. throw new ApiException(410242);
  150. }
  151. $type = $couponInfo['applicable_type'] ?? 0;
  152. $flag = false;
  153. $price = 0;
  154. $count = 0;
  155. switch ($type) {
  156. case 0:
  157. case 3:
  158. foreach ($cartInfo as $cart) {
  159. $price = bcadd($price, bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
  160. $count++;
  161. }
  162. break;
  163. case 1://品类券
  164. /** @var StoreCategoryServices $storeCategoryServices */
  165. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  166. $coupon_category = explode(',', (string)$couponInfo['category_id']);
  167. $category_ids = $storeCategoryServices->getAllById($coupon_category);
  168. if ($category_ids) {
  169. $cateIds = array_column($category_ids, 'id');
  170. foreach ($cartInfo as $cart) {
  171. if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
  172. $price = bcadd($price, bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
  173. $count++;
  174. }
  175. }
  176. }
  177. break;
  178. case 2:
  179. foreach ($cartInfo as $cart) {
  180. if (isset($cart['product_id']) && in_array($cart['product_id'], explode(',', $couponInfo['product_id']))) {
  181. $price = bcadd($price, bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
  182. $count++;
  183. }
  184. }
  185. break;
  186. }
  187. if ($count && $couponInfo['use_min_price'] <= $price) {
  188. $flag = true;
  189. }
  190. if (!$flag) {
  191. throw new ApiException(410243);
  192. }
  193. if ($isCreate) {
  194. $res1 = $couponServices->useCoupon($couponId);
  195. }
  196. $couponPrice = $couponInfo['coupon_price'] > $price ? $price : $couponInfo['coupon_price'];
  197. $payPrice = (float)bcsub((string)$payPrice, (string)$couponPrice, 2);
  198. } else {
  199. $couponPrice = 0;
  200. }
  201. if (!$res1) {
  202. throw new ApiException(410244);
  203. }
  204. return [$payPrice, $couponPrice];
  205. }
  206. /**
  207. * 使用积分
  208. * @param $useIntegral
  209. * @param $userInfo
  210. * @param $payPrice
  211. * @param $other
  212. * @return array
  213. */
  214. public function useIntegral(bool $useIntegral, $userInfo, string $payPrice, array $other)
  215. {
  216. /** @var UserBillServices $userBillServices */
  217. $userBillServices = app()->make(UserBillServices::class);
  218. // 可用积分
  219. $usable = bcsub((string)$userInfo['integral'], (string)$userBillServices->getBillSum(['uid' => $userInfo['uid'], 'is_frozen' => 1]), 0);
  220. $SurplusIntegral = $usable;
  221. if ($useIntegral && $userInfo['integral'] > 0 && $other['integralRatio'] > 0) {
  222. //积分抵扣上限
  223. $integralMaxNum = sys_config('integral_max_num', 200);
  224. if ($integralMaxNum > 0 && $usable > $integralMaxNum) {
  225. $integral = $integralMaxNum;
  226. } else {
  227. $integral = $usable;
  228. }
  229. $deductionPrice = (float)bcmul((string)$integral, (string)$other['integralRatio'], 2);
  230. if ($deductionPrice < $payPrice) {
  231. $payPrice = bcsub((string)$payPrice, (string)$deductionPrice, 2);
  232. $usedIntegral = $integral;
  233. } else {
  234. $deductionPrice = $payPrice;
  235. $usedIntegral = (int)ceil(bcdiv((string)$payPrice, (string)$other['integralRatio'], 2));
  236. $payPrice = 0;
  237. }
  238. $deductionPrice = $deductionPrice > 0 ? $deductionPrice : 0;
  239. $usedIntegral = $usedIntegral > 0 ? $usedIntegral : 0;
  240. $SurplusIntegral = (int)bcsub((string)$usable, $usedIntegral, 0);
  241. } else {
  242. $deductionPrice = 0;
  243. $usedIntegral = 0;
  244. }
  245. if ($payPrice <= 0) $payPrice = 0;
  246. return [$payPrice, $deductionPrice, $usedIntegral, $SurplusIntegral];
  247. }
  248. /**
  249. * 计算邮费
  250. * @param int $shipping_type
  251. * @param string $payType
  252. * @param array $cartInfo
  253. * @param array $addr
  254. * @param string $payPrice
  255. * @param array $other
  256. * @return array
  257. */
  258. public function computedPayPostage(int $shipping_type, string $payType, array $cartInfo, array $addr, string $payPrice, array $postage = [], array $other, $userInfo = [], $is_gift = 0)
  259. {
  260. $storePostageDiscount = 0;
  261. $storeFreePostage = $postage['storeFreePostage'] ?? 0;
  262. $isStoreFreePostage = false;
  263. if ($is_gift == 1) {
  264. $shipping_type = 0;
  265. $addr = [];
  266. }
  267. if (!$storeFreePostage) {
  268. $storeFreePostage = floatval(sys_config('store_free_postage')) ?: 0;//满额包邮金额
  269. }
  270. if (!$addr && !isset($addr['id']) || !$cartInfo) {
  271. $payPostage = 0;
  272. } else {
  273. //$shipping_type = 1 快递发货 $shipping_type = 2 门店自提
  274. if ($shipping_type == 2) {
  275. $store_self_mention = sys_config('store_self_mention') ?? 0;
  276. if (!$store_self_mention) $shipping_type = 1;
  277. }
  278. //门店自提 || (线下支付 && 线下支付包邮) 没有邮费支付
  279. if ($shipping_type === 2 || ($payType == 'offline' && ((isset($other['offlinePostage']) && $other['offlinePostage']) || sys_config('offline_postage')) == 1)) {
  280. $payPostage = 0;
  281. } else {
  282. if (!$postage || !isset($postage['storePostage']) || !isset($postage['storePostageDiscount'])) {
  283. $postage = $this->getOrderPriceGroup($storeFreePostage, $cartInfo, $addr, $userInfo);
  284. }
  285. $payPostage = $postage['storePostage'];
  286. $storePostageDiscount = $postage['storePostageDiscount'];
  287. $isStoreFreePostage = $postage['isStoreFreePostage'] ?? false;
  288. $payPrice = (float)bcadd((string)$payPrice, (string)$payPostage, 2);
  289. }
  290. }
  291. return [$payPrice, $payPostage, $storePostageDiscount, $storeFreePostage, $isStoreFreePostage];
  292. }
  293. /**
  294. * 运费计算,总金额计算
  295. * @param $cartInfo
  296. * @param $addr
  297. * @param array $userInfo
  298. * @return array
  299. */
  300. public function getOrderPriceGroup($storeFreePostage, $cartInfo, $addr, $userInfo = [], $shipping_type = 1, $is_gift = 0)
  301. {
  302. $storePostage = 0;
  303. $storePostageDiscount = 0;
  304. $giftPrice = 0;
  305. $isStoreFreePostage = false;//是否满额包邮
  306. $sumPrice = $this->getOrderSumPrice($cartInfo, 'sum_price');//获取订单原总金额
  307. $totalPrice = $this->getOrderSumPrice($cartInfo, 'truePrice');//获取订单svip、用户等级优惠之后总金额
  308. $costPrice = $this->getOrderSumPrice($cartInfo, 'costPrice');//获取订单成本价
  309. $vipPrice = $this->getOrderSumPrice($cartInfo, 'vip_truePrice');//获取订单等级和付费会员总优惠金额
  310. $levelPrice = $this->getOrderSumPrice($cartInfo, 'level');//获取会员等级优惠
  311. $memberPrice = $this->getOrderSumPrice($cartInfo, 'member');//获取付费会员优惠
  312. // 判断商品包邮和固定运费
  313. foreach ($cartInfo as $key => &$item) {
  314. $item['postage_price'] = 0;
  315. if ($shipping_type == 1) {
  316. if ($item['productInfo']['freight'] == 1) {
  317. $item['postage_price'] = 0;
  318. } elseif ($item['productInfo']['freight'] == 2) {
  319. $item['postage_price'] = bcmul((string)$item['productInfo']['postage'], (string)$item['cart_num'], 2);
  320. $item['origin_postage_price'] = bcmul((string)$item['productInfo']['postage'], (string)$item['cart_num'], 2);
  321. $storePostage = bcadd((string)$storePostage, (string)$item['postage_price'], 2);
  322. }
  323. if ($sumPrice >= $storeFreePostage) {
  324. $item['postage_price'] = $item['origin_postage_price'] = $storePostage = 0;
  325. }
  326. }
  327. if ($is_gift == 1) {
  328. $item['postage_price'] = $item['origin_postage_price'] = $storePostage = 0;
  329. $giftPrice = bcadd((string)$giftPrice, (string)$item['productInfo']['gift_price'], 2);
  330. $addr = [];
  331. }
  332. }
  333. $postageArr = [];
  334. if (isset($cartInfo[0]['productInfo']['is_virtual']) && $cartInfo[0]['productInfo']['is_virtual'] == 1) {
  335. $storePostage = 0;
  336. } elseif ($storeFreePostage && $cartInfo && $addr) {
  337. if ($sumPrice >= $storeFreePostage) {//如果总价大于等于满额包邮 邮费等于0
  338. $isStoreFreePostage = true;
  339. $storePostage = 0;
  340. } else {
  341. //按照运费模板计算每个运费模板下商品的件数/重量/体积以及总金额 按照首重倒序排列
  342. $cityId = $addr['city_id'] ?? 0;
  343. $tempIds[] = 1;
  344. foreach ($cartInfo as $key_c => $item_c) {
  345. if (isset($item_c['productInfo']['freight']) && $item_c['productInfo']['freight'] == 3) {
  346. $tempIds[] = $item_c['productInfo']['temp_id'];
  347. }
  348. }
  349. $tempIds = array_unique($tempIds);
  350. /** @var ShippingTemplatesServices $shippServices */
  351. $shippServices = app()->make(ShippingTemplatesServices::class);
  352. $temp = $shippServices->getShippingColumn(['id' => $tempIds], 'type,appoint', 'id');
  353. /** @var ShippingTemplatesRegionServices $regionServices */
  354. $regionServices = app()->make(ShippingTemplatesRegionServices::class);
  355. $regions = $regionServices->getTempRegionList($tempIds, [$cityId, 0], 'temp_id,first,first_price,continue,continue_price', 'temp_id');
  356. $temp_num = [];
  357. foreach ($cartInfo as $cart) {
  358. if (isset($cart['productInfo']['freight']) && in_array($cart['productInfo']['freight'], [1, 2])) {
  359. continue;
  360. }
  361. $tempId = $cart['productInfo']['temp_id'] ?? 1;
  362. $type = $temp[$tempId]['type'] ?? $temp[1]['type'];
  363. if ($type == 1) {
  364. $num = $cart['cart_num'];
  365. } elseif ($type == 2) {
  366. $num = $cart['cart_num'] * $cart['productInfo']['attrInfo']['weight'];
  367. } else {
  368. $num = $cart['cart_num'] * $cart['productInfo']['attrInfo']['volume'];
  369. }
  370. $region = $regions[$tempId] ?? ($regions[1] ?? []);
  371. if (!$region) continue;
  372. if (!isset($temp_num[$tempId])) {
  373. $temp_num[$tempId] = [
  374. 'number' => $num,
  375. 'type' => $type,
  376. 'price' => bcmul($cart['cart_num'], $cart['truePrice'], 2),
  377. 'first' => $region['first'],
  378. 'first_price' => $region['first_price'],
  379. 'continue' => $region['continue'],
  380. 'continue_price' => $region['continue_price'],
  381. 'temp_id' => $tempId
  382. ];
  383. } else {
  384. $temp_num[$tempId]['number'] += $num;
  385. $temp_num[$tempId]['price'] += bcmul($cart['cart_num'], $cart['truePrice'], 2);
  386. }
  387. }
  388. /** @var ShippingTemplatesFreeServices $freeServices */
  389. $freeServices = app()->make(ShippingTemplatesFreeServices::class);
  390. $freeList = $freeServices->isFreeList($tempIds, $addr['city_id'], 0, 'temp_id,number,price', 'temp_id');
  391. if ($freeList) {
  392. foreach ($temp_num as $k => $v) {
  393. if (isset($temp[$v['temp_id']]['appoint']) && $temp[$v['temp_id']]['appoint'] && isset($freeList[$v['temp_id']])) {
  394. $free = $freeList[$v['temp_id']];
  395. $condition = $free['number'] <= $v['number'];
  396. if ($free['price'] <= $v['price'] && $condition) {
  397. unset($temp_num[$k]);
  398. }
  399. }
  400. }
  401. }
  402. //首件运费最大值
  403. $maxFirstPrice = $temp_num ? max(array_column($temp_num, 'first_price')) : 0;
  404. //初始运费为0
  405. $storePostage_arr = [];
  406. $i = 0;
  407. //循环运费数组
  408. foreach ($temp_num as $fk => $fv) {
  409. //找到首件运费等于最大值
  410. if ($fv['first_price'] == $maxFirstPrice) {
  411. //每次循环设置初始值
  412. $tempArr = $temp_num;
  413. $Postage = 0;
  414. //计算首件运费
  415. if ($fv['number'] <= $fv['first']) {
  416. $Postage = bcadd($Postage, $fv['first_price'], 2);
  417. } else {
  418. if ($fv['continue'] <= 0) {
  419. $Postage = $Postage;
  420. } else {
  421. $Postage = bcadd(bcadd($Postage, $fv['first_price'], 2), bcmul(ceil(bcdiv(bcsub($fv['number'], $fv['first'], 2), $fv['continue'] ?? 0, 2)), $fv['continue_price'], 4), 2);
  422. }
  423. }
  424. $postageArr[$i]['data'][$fk] = $Postage;
  425. //删除计算过的首件数据
  426. unset($tempArr[$fk]);
  427. //循环计算剩余运费
  428. foreach ($tempArr as $ck => $cv) {
  429. if ($cv['continue'] <= 0) {
  430. $Postage = $Postage;
  431. } else {
  432. $one_postage = bcmul(ceil(bcdiv($cv['number'], $cv['continue'] ?? 0, 2)), $cv['continue_price'], 2);
  433. $Postage = bcadd($Postage, $one_postage, 2);
  434. $postageArr[$i]['data'][$ck] = $one_postage;
  435. }
  436. }
  437. $postageArr[$i]['sum'] = $Postage;
  438. $storePostage_arr[] = $Postage;
  439. }
  440. }
  441. if (count($storePostage_arr)) {
  442. $maxStorePostage = max($storePostage_arr);
  443. //获取运费计算中的最大值
  444. $storePostage = bcadd((string)$storePostage, (string)$maxStorePostage, 2);
  445. }
  446. }
  447. }
  448. //会员邮费享受折扣
  449. if ($storePostage) {
  450. $express_rule_number = 100;
  451. if (!$userInfo) {
  452. /** @var UserServices $userService */
  453. $userService = app()->make(UserServices::class);
  454. $userInfo = $userService->getUserInfo($addr['uid']);
  455. }
  456. if ($userInfo && isset($userInfo['is_money_level']) && $userInfo['is_money_level'] > 0) {
  457. //看是否开启会员折扣奖励
  458. /** @var MemberCardServices $memberCardService */
  459. $memberCardService = app()->make(MemberCardServices::class);
  460. $express_rule_number = $memberCardService->isOpenMemberCard('express');
  461. $express_rule_number = $express_rule_number <= 0 ? 0 : $express_rule_number;
  462. }
  463. $discountRate = bcdiv($express_rule_number, 100, 4);
  464. $truePostageArr = [];
  465. foreach ($postageArr as $postitem) {
  466. if ($postitem['sum'] == ($maxStorePostage ?? 0)) {
  467. $truePostageArr = $postitem['data'];
  468. break;
  469. }
  470. }
  471. $cartAlready = [];
  472. foreach ($cartInfo as &$item) {
  473. if (isset($item['productInfo']['freight']) && in_array($item['productInfo']['freight'], [1, 2])) {
  474. if ($item['productInfo']['freight'] == 2) {
  475. $item['postage_price'] = sprintf("%.2f", bcmul($item['postage_price'], $discountRate, 6));
  476. }
  477. continue;
  478. }
  479. $tempId = $item['productInfo']['temp_id'] ?? 0;
  480. $tempPostage = $truePostageArr[$tempId] ?? 0;
  481. $tempNumber = $temp_num[$tempId]['number'] ?? 0;
  482. if (!$tempId || !$tempPostage) continue;
  483. $type = $temp_num[$tempId]['type'];
  484. if ($type == 1) {
  485. $num = $item['cart_num'];
  486. } elseif ($type == 2) {
  487. $num = $item['cart_num'] * $item['productInfo']['attrInfo']['weight'];
  488. } else {
  489. $num = $item['cart_num'] * $item['productInfo']['attrInfo']['volume'];
  490. }
  491. if ((($cartAlready[$tempId]['number'] ?? 0) + $num) >= $tempNumber) {
  492. $price = isset($cartAlready[$tempId]['price']) ? bcsub((string)$tempPostage, (string)$cartAlready[$tempId]['price'], 6) : $tempPostage;
  493. } else {
  494. $price = bcmul((string)$tempPostage, bcdiv((string)$num, (string)$tempNumber, 6), 6);
  495. }
  496. $cartAlready[$tempId]['number'] = bcadd((string)($cartAlready[$tempId]['number'] ?? 0), (string)$num, 4);
  497. $cartAlready[$tempId]['price'] = bcadd((string)($cartAlready[$tempId]['price'] ?? 0.00), (string)$price, 4);
  498. if ($express_rule_number && $express_rule_number < 100) {
  499. $price = bcmul($price, $discountRate, 4);
  500. }
  501. $item['postage_price'] = sprintf("%.2f", $price);
  502. }
  503. if ($express_rule_number && $express_rule_number < 100) {
  504. $storePostageDiscount = $storePostage;
  505. $storePostage = bcmul($storePostage, bcdiv($express_rule_number, 100, 4), 2);
  506. $storePostageDiscount = bcsub($storePostageDiscount, $storePostage, 2);
  507. } else {
  508. $storePostageDiscount = 0;
  509. $storePostage = $storePostage;
  510. }
  511. }
  512. return compact('storePostage', 'storeFreePostage', 'isStoreFreePostage', 'sumPrice', 'totalPrice', 'costPrice', 'vipPrice', 'storePostageDiscount', 'cartInfo', 'levelPrice', 'memberPrice', 'giftPrice');
  513. }
  514. /**
  515. * 获取某个字段总金额
  516. * @param $cartInfo
  517. * @param string $key
  518. * @param bool $is_unit
  519. * @return int|string
  520. */
  521. public function getOrderSumPrice($cartInfo, $key = 'truePrice', $is_unit = true)
  522. {
  523. $SumPrice = 0;
  524. foreach ($cartInfo as $cart) {
  525. if (isset($cart['cart_info'])) $cart = $cart['cart_info'];
  526. if ($is_unit) {
  527. if ($key == 'level' || $key == 'member') {
  528. if ($cart['price_type'] == $key) {
  529. $SumPrice = bcadd($SumPrice, bcmul($cart['cart_num'], $cart['vip_truePrice'], 2), 2);
  530. }
  531. } else {
  532. $SumPrice = bcadd($SumPrice, bcmul($cart['cart_num'], $cart[$key], 2), 2);
  533. }
  534. } else {
  535. $SumPrice = bcadd($SumPrice, $cart[$key], 2);
  536. }
  537. }
  538. return $SumPrice;
  539. }
  540. }