StoreOrderComputedServices.php 25 KB

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