StoreOrderComputedServices.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. $cateGorys = $storeCategoryServices->getAllById((int)$couponInfo['category_id']);
  169. if ($cateGorys) {
  170. $cateIds = array_column($cateGorys, 'id');
  171. foreach ($cartInfo as $cart) {
  172. if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
  173. $price = bcadd($price, bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
  174. $count++;
  175. }
  176. }
  177. }
  178. break;
  179. case 2:
  180. foreach ($cartInfo as $cart) {
  181. if (isset($cart['product_id']) && in_array($cart['product_id'], explode(',', $couponInfo['product_id']))) {
  182. $price = bcadd($price, bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
  183. $count++;
  184. }
  185. }
  186. break;
  187. }
  188. if ($count && $couponInfo['use_min_price'] <= $price) {
  189. $flag = true;
  190. }
  191. if (!$flag) {
  192. throw new ApiException(410243);
  193. }
  194. if ($isCreate) {
  195. $res1 = $couponServices->useCoupon($couponId);
  196. }
  197. $couponPrice = $couponInfo['coupon_price'] > $price ? $price : $couponInfo['coupon_price'];
  198. $payPrice = (float)bcsub((string)$payPrice, (string)$couponPrice, 2);
  199. } else {
  200. $couponPrice = 0;
  201. }
  202. if (!$res1) {
  203. throw new ApiException(410244);
  204. }
  205. return [$payPrice, $couponPrice];
  206. }
  207. /**
  208. * 使用积分
  209. * @param $useIntegral
  210. * @param $userInfo
  211. * @param $payPrice
  212. * @param $other
  213. * @return array
  214. */
  215. public function useIntegral(bool $useIntegral, $userInfo, string $payPrice, array $other)
  216. {
  217. /** @var UserBillServices $userBillServices */
  218. $userBillServices = app()->make(UserBillServices::class);
  219. // 可用积分
  220. $usable = bcsub((string)$userInfo['integral'], (string)$userBillServices->getBillSum(['uid' => $userInfo['uid'], 'is_frozen' => 1]), 0);
  221. $SurplusIntegral = 0;
  222. if ($useIntegral && $userInfo['integral'] > 0) {
  223. //积分抵扣上限
  224. $integralMaxNum = sys_config('integral_max_num', 200);
  225. if ($integralMaxNum > 0 && $usable > $integralMaxNum) {
  226. $integral = $integralMaxNum;
  227. } else {
  228. $integral = $usable;
  229. }
  230. $deductionPrice = (float)bcmul((string)$integral, (string)$other['integralRatio'], 2);
  231. if ($deductionPrice < $payPrice) {
  232. $payPrice = bcsub((string)$payPrice, (string)$deductionPrice, 2);
  233. $usedIntegral = $integral;
  234. } else {
  235. $deductionPrice = $payPrice;
  236. $usedIntegral = (int)ceil(bcdiv((string)$payPrice, (string)$other['integralRatio'], 2));
  237. $payPrice = 0;
  238. }
  239. $deductionPrice = $deductionPrice > 0 ? $deductionPrice : 0;
  240. $usedIntegral = $usedIntegral > 0 ? $usedIntegral : 0;
  241. $SurplusIntegral = (int)bcsub((string)$usable, $usedIntegral, 0);
  242. } else {
  243. $deductionPrice = 0;
  244. $usedIntegral = 0;
  245. }
  246. if ($payPrice <= 0) $payPrice = 0;
  247. return [$payPrice, $deductionPrice, $usedIntegral, $SurplusIntegral];
  248. }
  249. /**
  250. * 计算邮费
  251. * @param int $shipping_type
  252. * @param string $payType
  253. * @param array $cartInfo
  254. * @param array $addr
  255. * @param string $payPrice
  256. * @param array $other
  257. * @return array
  258. */
  259. public function computedPayPostage(int $shipping_type, string $payType, array $cartInfo, array $addr, string $payPrice, array $postage = [], array $other, $userInfo = [])
  260. {
  261. $storePostageDiscount = 0;
  262. $storeFreePostage = $postage['storeFreePostage'] ?? 0;
  263. $isStoreFreePostage = false;
  264. if (!$storeFreePostage) {
  265. $storeFreePostage = floatval(sys_config('store_free_postage')) ?: 0;//满额包邮金额
  266. }
  267. if (!$addr && !isset($addr['id']) || !$cartInfo) {
  268. $payPostage = 0;
  269. } else {
  270. //$shipping_type = 1 快递发货 $shipping_type = 2 门店自提
  271. if ($shipping_type == 2) {
  272. $store_self_mention = sys_config('store_self_mention') ?? 0;
  273. if (!$store_self_mention) $shipping_type = 1;
  274. }
  275. //门店自提 || (线下支付 && 线下支付包邮) 没有邮费支付
  276. if ($shipping_type === 2 || ($payType == 'offline' && ((isset($other['offlinePostage']) && $other['offlinePostage']) || sys_config('offline_postage')) == 1)) {
  277. $payPostage = 0;
  278. } else {
  279. if (!$postage || !isset($postage['storePostage']) || !isset($postage['storePostageDiscount'])) {
  280. $postage = $this->getOrderPriceGroup($storeFreePostage, $cartInfo, $addr, $userInfo);
  281. }
  282. $payPostage = $postage['storePostage'];
  283. $storePostageDiscount = $postage['storePostageDiscount'];
  284. $isStoreFreePostage = $postage['isStoreFreePostage'] ?? false;
  285. $payPrice = (float)bcadd((string)$payPrice, (string)$payPostage, 2);
  286. }
  287. }
  288. return [$payPrice, $payPostage, $storePostageDiscount, $storeFreePostage, $isStoreFreePostage];
  289. }
  290. /**
  291. * 运费计算,总金额计算
  292. * @param $cartInfo
  293. * @param $addr
  294. * @param array $userInfo
  295. * @return array
  296. */
  297. public function getOrderPriceGroup($storeFreePostage, $cartInfo, $addr, $userInfo = [])
  298. {
  299. $sumPrice = $totalPrice = $costPrice = $vipPrice = 0;
  300. $storePostage = 0;
  301. $storePostageDiscount = 0;
  302. $isStoreFreePostage = false;//是否满额包邮
  303. $sumPrice = $this->getOrderSumPrice($cartInfo, 'sum_price');//获取订单原总金额
  304. $totalPrice = $this->getOrderSumPrice($cartInfo, 'truePrice');//获取订单svip、用户等级优惠之后总金额
  305. $costPrice = $this->getOrderSumPrice($cartInfo, 'costPrice');//获取订单成本价
  306. $vipPrice = $this->getOrderSumPrice($cartInfo, 'vip_truePrice');//获取订单会员优惠金额
  307. // 判断商品包邮和固定运费
  308. foreach ($cartInfo as $key => &$item) {
  309. $item['postage_price'] = 0;
  310. if ($item['productInfo']['freight'] == 1) {
  311. $item['postage_price'] = 0;
  312. } elseif ($item['productInfo']['freight'] == 2) {
  313. $item['postage_price'] = bcmul((string)$item['productInfo']['postage'], (string)$item['cart_num'], 2);
  314. $item['origin_postage_price'] = bcmul((string)$item['productInfo']['postage'], (string)$item['cart_num'], 2);
  315. $storePostage = bcadd((string)$storePostage, (string)$item['postage_price'], 2);
  316. }
  317. }
  318. $postageArr = [];
  319. if (isset($cartInfo[0]['productInfo']['is_virtual']) && $cartInfo[0]['productInfo']['is_virtual'] == 1) {
  320. $storePostage = 0;
  321. } elseif ($storeFreePostage && $cartInfo && $addr) {
  322. if ($sumPrice >= $storeFreePostage) {//如果总价大于等于满额包邮 邮费等于0
  323. $isStoreFreePostage = true;
  324. $storePostage = 0;
  325. } else {
  326. //按照运费模板计算每个运费模板下商品的件数/重量/体积以及总金额 按照首重倒序排列
  327. $cityId = $addr['city_id'] ?? 0;
  328. $tempIds[] = 1;
  329. foreach ($cartInfo as $key_c => $item_c) {
  330. if (isset($item_c['productInfo']['freight']) && $item_c['productInfo']['freight'] == 3) {
  331. $tempIds[] = $item_c['productInfo']['temp_id'];
  332. }
  333. }
  334. $tempIds = array_unique($tempIds);
  335. /** @var ShippingTemplatesServices $shippServices */
  336. $shippServices = app()->make(ShippingTemplatesServices::class);
  337. $temp = $shippServices->getShippingColumn(['id' => $tempIds], 'type,appoint', 'id');
  338. /** @var ShippingTemplatesRegionServices $regionServices */
  339. $regionServices = app()->make(ShippingTemplatesRegionServices::class);
  340. $regions = $regionServices->getTempRegionList($tempIds, [$cityId, 0], 'temp_id,first,first_price,continue,continue_price', 'temp_id');
  341. $temp_num = [];
  342. foreach ($cartInfo as $cart) {
  343. if (isset($cart['productInfo']['freight']) && in_array($cart['productInfo']['freight'], [1, 2])) {
  344. continue;
  345. }
  346. $tempId = $cart['productInfo']['temp_id'] ?? 1;
  347. $type = $temp[$tempId]['type'] ?? $temp[1]['type'];
  348. if ($type == 1) {
  349. $num = $cart['cart_num'];
  350. } elseif ($type == 2) {
  351. $num = $cart['cart_num'] * $cart['productInfo']['attrInfo']['weight'];
  352. } else {
  353. $num = $cart['cart_num'] * $cart['productInfo']['attrInfo']['volume'];
  354. }
  355. $region = $regions[$tempId] ?? ($regions[1] ?? []);
  356. if (!$region) continue;
  357. if (!isset($temp_num[$tempId])) {
  358. $temp_num[$tempId] = [
  359. 'number' => $num,
  360. 'type' => $type,
  361. 'price' => bcmul($cart['cart_num'], $cart['truePrice'], 2),
  362. 'first' => $region['first'],
  363. 'first_price' => $region['first_price'],
  364. 'continue' => $region['continue'],
  365. 'continue_price' => $region['continue_price'],
  366. 'temp_id' => $tempId
  367. ];
  368. } else {
  369. $temp_num[$tempId]['number'] += $num;
  370. $temp_num[$tempId]['price'] += bcmul($cart['cart_num'], $cart['truePrice'], 2);
  371. }
  372. }
  373. /** @var ShippingTemplatesFreeServices $freeServices */
  374. $freeServices = app()->make(ShippingTemplatesFreeServices::class);
  375. $freeList = $freeServices->isFreeList($tempIds, $addr['city_id'], 0, 'temp_id,number,price', 'temp_id');
  376. if ($freeList) {
  377. foreach ($temp_num as $k => $v) {
  378. if (isset($temp[$v['temp_id']]['appoint']) && $temp[$v['temp_id']]['appoint'] && isset($freeList[$v['temp_id']])) {
  379. $free = $freeList[$v['temp_id']];
  380. $condition = $v['type'] == 1 ? $free['number'] <= $v['number'] : $free['number'] >= $v['number'];
  381. if ($free['price'] <= $v['price'] && $condition) {
  382. unset($temp_num[$k]);
  383. }
  384. }
  385. }
  386. }
  387. //首件运费最大值
  388. $maxFirstPrice = $temp_num ? max(array_column($temp_num, 'first_price')) : 0;
  389. //初始运费为0
  390. $storePostage_arr = [];
  391. $i = 0;
  392. //循环运费数组
  393. foreach ($temp_num as $fk => $fv) {
  394. //找到首件运费等于最大值
  395. if ($fv['first_price'] == $maxFirstPrice) {
  396. //每次循环设置初始值
  397. $tempArr = $temp_num;
  398. $Postage = 0;
  399. //计算首件运费
  400. if ($fv['number'] <= $fv['first']) {
  401. $Postage = bcadd($Postage, $fv['first_price'], 2);
  402. } else {
  403. if ($fv['continue'] <= 0) {
  404. $Postage = $Postage;
  405. } else {
  406. $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);
  407. }
  408. }
  409. $postageArr[$i]['data'][$fk] = $Postage;
  410. //删除计算过的首件数据
  411. unset($tempArr[$fk]);
  412. //循环计算剩余运费
  413. foreach ($tempArr as $ck => $cv) {
  414. if ($cv['continue'] <= 0) {
  415. $Postage = $Postage;
  416. } else {
  417. $one_postage = bcmul(ceil(bcdiv($cv['number'], $cv['continue'] ?? 0, 2)), $cv['continue_price'], 2);
  418. $Postage = bcadd($Postage, $one_postage, 2);
  419. $postageArr[$i]['data'][$ck] = $one_postage;
  420. }
  421. }
  422. $postageArr[$i]['sum'] = $Postage;
  423. $storePostage_arr[] = $Postage;
  424. }
  425. }
  426. if (count($storePostage_arr)) {
  427. $maxStorePostage = max($storePostage_arr);
  428. //获取运费计算中的最大值
  429. $storePostage = bcadd((string)$storePostage, (string)$maxStorePostage, 2);
  430. }
  431. }
  432. }
  433. //会员邮费享受折扣
  434. if ($storePostage) {
  435. $express_rule_number = 0;
  436. if (!$userInfo) {
  437. /** @var UserServices $userService */
  438. $userService = app()->make(UserServices::class);
  439. $userInfo = $userService->getUserInfo($addr['uid']);
  440. }
  441. if ($userInfo && isset($userInfo['is_money_level']) && $userInfo['is_money_level'] > 0) {
  442. //看是否开启会员折扣奖励
  443. /** @var MemberCardServices $memberCardService */
  444. $memberCardService = app()->make(MemberCardServices::class);
  445. $express_rule_number = $memberCardService->isOpenMemberCard('express');
  446. $express_rule_number = $express_rule_number <= 0 ? 0 : $express_rule_number;
  447. }
  448. $discountRate = bcdiv($express_rule_number, 100, 4);
  449. $truePostageArr = [];
  450. foreach ($postageArr as $postitem) {
  451. if ($postitem['sum'] == ($maxStorePostage ?? 0)) {
  452. $truePostageArr = $postitem['data'];
  453. break;
  454. }
  455. }
  456. $cartAlready = [];
  457. foreach ($cartInfo as &$item) {
  458. if (isset($item['productInfo']['freight']) && in_array($item['productInfo']['freight'], [1, 2])) {
  459. if ($item['productInfo']['freight'] == 2) {
  460. $item['postage_price'] = sprintf("%.2f", bcmul($item['postage_price'], $discountRate, 6));
  461. }
  462. continue;
  463. }
  464. $tempId = $item['productInfo']['temp_id'] ?? 0;
  465. $tempPostage = $truePostageArr[$tempId] ?? 0;
  466. $tempNumber = $temp_num[$tempId]['number'] ?? 0;
  467. if (!$tempId || !$tempPostage || !$tempNumber) continue;
  468. $type = $temp_num[$tempId]['type'];
  469. $cartNumber = $item['cart_num'];
  470. if ((($cartAlready[$tempId]['number'] ?? 0) + $cartNumber) >= $tempNumber) {
  471. $price = isset($cartAlready[$tempId]['price']) ? bcsub((string)$tempPostage, (string)$cartAlready[$tempId]['price'], 6) : $tempPostage;
  472. } else {
  473. $price = bcmul((string)$tempPostage, bcdiv((string)$cartNumber, (string)$tempNumber, 6), 6);
  474. }
  475. $cartAlready[$tempId]['number'] = bcadd((string)($cartNumber[$tempId]['number'] ?? 0), (string)$cartNumber, 4);
  476. $cartAlready[$tempId]['price'] = bcadd((string)($cartNumber[$tempId]['price'] ?? 0.00), (string)$price, 4);
  477. if ($express_rule_number && $express_rule_number < 100) {
  478. $price = bcmul($price, $discountRate, 4);
  479. }
  480. if ($type == 2) {
  481. $price = bcmul($price, $item['productInfo']['attrInfo']['weight'], 6);
  482. } elseif ($type == 3) {
  483. $price = bcmul($price, $item['productInfo']['attrInfo']['volume'], 6);
  484. }
  485. $price = sprintf("%.2f", $price);
  486. $item['postage_price'] = $price;
  487. }
  488. if ($express_rule_number && $express_rule_number < 100) {
  489. $storePostageDiscount = $storePostage;
  490. $storePostage = bcmul($storePostage, bcdiv($express_rule_number, 100, 4), 2);
  491. $storePostageDiscount = bcsub($storePostageDiscount, $storePostage, 2);
  492. } else {
  493. $storePostageDiscount = 0;
  494. $storePostage = $storePostage;
  495. }
  496. }
  497. return compact('storePostage', 'storeFreePostage', 'isStoreFreePostage', 'sumPrice', 'totalPrice', 'costPrice', 'vipPrice', 'storePostageDiscount', 'cartInfo');
  498. }
  499. /**
  500. * 获取某个字段总金额
  501. * @param $cartInfo
  502. * @param string $key
  503. * @param bool $is_unit
  504. * @return int|string
  505. */
  506. public function getOrderSumPrice($cartInfo, $key = 'truePrice', $is_unit = true)
  507. {
  508. $SumPrice = 0;
  509. foreach ($cartInfo as $cart) {
  510. if (isset($cart['cart_info'])) $cart = $cart['cart_info'];
  511. if ($is_unit) {
  512. $SumPrice = bcadd($SumPrice, bcmul($cart['cart_num'], $cart[$key], 2), 2);
  513. } else {
  514. $SumPrice = bcadd($SumPrice, $cart[$key], 2);
  515. }
  516. }
  517. return $SumPrice;
  518. }
  519. }