StoreOrderComputedServices.php 25 KB

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