优化:根据计抛比计算体积重。在体积重和重量重中取最大的值计算运费

This commit is contained in:
wuhui_zzw 2023-09-21 18:32:24 +08:00
parent bfc31e06c4
commit 6af1a100e0
2 changed files with 20 additions and 7 deletions

View File

@ -65,14 +65,18 @@ class Logistics extends Base{
}else{
// 循环判断:只要有一个商品不符合条件 则当前物流不可用;
foreach($productsList as $productItem){
$surplusWeight = $productItem['sum_weight'] - $firstWeight;// 总重量 - 减首重 = 剩余重量
// 计算体积重 并且取数值大的作为重量计算
$volumeWeight = $productItem['volume_weight'] / $logisticsItem['throwing_ratio'];
$sumWeight = $productItem['sum_weight'] > $volumeWeight ? $productItem['sum_weight'] : $volumeWeight;
// 总重量 - 减首重 = 剩余重量
$surplusWeight = $sumWeight - $firstWeight;
// 判断如果剩余重量小于等于0 则符合条件;否则计算续重区间
if($surplusWeight <= 0){
continue;
}else{
$eligibleCount = collect($weights)
->where('min','<=',$surplusWeight)
->where('max','>',$surplusWeight)
->where('min','<=',$sumWeight)//$surplusWeight
->where('max','>',$sumWeight)//$surplusWeight
->count();
if($eligibleCount <= 0) {
$logisticsItem = [];

View File

@ -56,19 +56,25 @@ class ShippingService{
self::$productsList = collect($products)->groupBy('product_id')->map(function($group){
$firstInfo = $group->first();
$productWeightInfo = Product::query()
->select(['id','weight','weight_class'])
->select(['id','weight','weight_class','length','width','height'])
->find($firstInfo['product_id']);
if($productWeightInfo) $productWeightInfo = $productWeightInfo->toArray();
// 重量转换 - 物流重量单位为 千克;商品重量需要进行转换
$weight = $productWeightInfo['weight'] ?? 0;
$sumQuantity = $group->sum('quantity');
$sumWeight = (float)sprintf("%.2f",($weight * $sumQuantity));
// 判断:如果是按批卖 则数量需要除以每批的数量 在计算总重量。按件卖则直接计算
$weightQuantity = $firstInfo['sales_method'] == 'batches' ? ($sumQuantity / $firstInfo['piece_to_batch']) : $sumQuantity;// 多少件/多少批 根据销售方式获取
$sumWeight = (float)sprintf("%.2f",($weight * $weightQuantity));
$weightClass = $productWeightInfo['weight_class'];
$sumWeight = Weight::convert($sumWeight,$weightClass);// 总重量 单位:克
// 获取 体积重 长cm*宽cm*高cm*数量/抛比=体积重
$volumeWeight = ($productWeightInfo['length'] * $productWeightInfo['width'] * $productWeightInfo['height']) * $weightQuantity;// 还需要除以计抛比
return [
'product_id' => $firstInfo['product_id'],
'sum_quantity' => $sumQuantity,
'sum_weight' => $sumWeight,
'volume_weight' => $volumeWeight,
'weight' => $weight,
'weight_class' => $weightClass,
];
@ -103,7 +109,7 @@ class ShippingService{
* @return array|mixed|mixed[]
*/
private static function getLogistics($logisticsId,$cart){
$logisticsField = ['id','type', 'name','first_weight','first_weight_fee','continuation_weight_max','add_weight','continuation_weight_fee','num_fee'];
$logisticsField = ['id','type', 'name','first_weight','first_weight_fee','continuation_weight_max','add_weight','continuation_weight_fee','num_fee','throwing_ratio'];
// 判断:客服端是否切换国家 如果存在切换的国家id 则使用切换的国家id不存在则继续执行
$countryId = (int)request()->input('products_country_id');
if($countryId > 0) {
@ -190,10 +196,13 @@ class ShippingService{
$continuationWeightFee = (float)$logisticsInfo['continuation_weight_fee'];// 续重运费
// 循环处理商品
foreach(self::$productsList as $productInfo){
// 计算体积重 并且取数值大的作为重量计算
$volumeWeight = $productInfo['volume_weight'] / $logisticsInfo['throwing_ratio'];
$sumWeight = $productInfo['sum_weight'] > $volumeWeight ? $productInfo['sum_weight'] : $volumeWeight;
// 首重运费
$amount += (float)sprintf("%.2f",$firstWeightFee);
// 判断:如果总重量超过首重 则计算续重运费
$surplus = (int)$productInfo['sum_weight'] - $firstWeight;
$surplus = (float)$sumWeight - $firstWeight;
if($surplus > 0) $amount += (float)sprintf("%.2f",($surplus / $addWeight) * $continuationWeightFee);
}