StoreOrderComputedServices.php 27 KB

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