StoreOrderComputedServices.php 31 KB

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