StoreOrderComputedServices.php 26 KB

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