StoreOrderComputedServices.php 27 KB

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