Refactor handling cost calculation logic: simplify null checks using Objects.requireNonNullElse, adjust handling and disposal calculations, and improve clarity in annual cost computations.

This commit is contained in:
Jan 2026-01-04 18:59:53 +01:00
parent f8d2745d32
commit 3a203d1c7e

View file

@ -14,6 +14,7 @@ import org.springframework.stereotype.Service;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.util.Objects;
@Service @Service
public class HandlingCostCalculationService { public class HandlingCostCalculationService {
@ -48,19 +49,31 @@ public class HandlingCostCalculationService {
BigDecimal multiplier = shippingFreq.compareTo(huAnnualAmount) > 0 ? shippingFreq : huAnnualAmount; BigDecimal multiplier = shippingFreq.compareTo(huAnnualAmount) > 0 ? shippingFreq : huAnnualAmount;
BigDecimal handling = destinationHandling != null ? destinationHandling : BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.GLT_HANDLING, setId).orElseThrow().getCurrentValue())); BigDecimal handling = BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.KLT_HANDLING, setId).orElseThrow().getCurrentValue()));
BigDecimal release = BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.GLT_RELEASE, setId).orElseThrow().getCurrentValue())); BigDecimal release = BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.KLT_RELEASE, setId).orElseThrow().getCurrentValue()));
BigDecimal dispatch = BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.GLT_DISPATCH, setId).orElseThrow().getCurrentValue())); BigDecimal dispatch = BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.KLT_DISPATCH, setId).orElseThrow().getCurrentValue()));
BigDecimal disposal = destinationDisposal != null ? destinationDisposal : (addRepackingAndDisposalCost ? BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.DISPOSAL, setId).orElseThrow().getCurrentValue())) : BigDecimal.ZERO); BigDecimal disposal = (addRepackingAndDisposalCost ? BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.DISPOSAL, setId).orElseThrow().getCurrentValue())) : BigDecimal.ZERO);
BigDecimal wageFactor = BigDecimal.valueOf(Double.parseDouble(countryPropertyRepository.getByMappingIdAndCountryId(CountryPropertyMappingId.WAGE, setId, destination.getCountryId()).orElseThrow().getCurrentValue())); BigDecimal wageFactor = BigDecimal.valueOf(Double.parseDouble(countryPropertyRepository.getByMappingIdAndCountryId(CountryPropertyMappingId.WAGE, setId, destination.getCountryId()).orElseThrow().getCurrentValue()));
BigDecimal booking = BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.BOOKING_KLT, setId).orElseThrow().getCurrentValue())); BigDecimal booking = BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.BOOKING_KLT, setId).orElseThrow().getCurrentValue()));
BigDecimal annualRepacking = Objects.requireNonNullElse(destinationRepacking, getRepackingCost(setId, hu, loadCarrierType, addRepackingAndDisposalCost, destinationRepacking).multiply(wageFactor)).multiply(huAnnualAmount);
BigDecimal annualDisposal = Objects.requireNonNullElse(destinationDisposal.multiply(huAnnualAmount), BigDecimal.ZERO);
// BigDecimal annualHandling = Objects.requireNonNullElse(destinationHandling, handling.add(release).add(dispatch).add(disposal).multiply(wageFactor)).multiply(multiplier);
BigDecimal annualHandling;
if(destinationHandling != null)
annualHandling = destinationHandling.multiply(multiplier);
else
annualHandling = (((handling.multiply(multiplier)).add((dispatch.multiply(huAnnualAmount))).add((release.multiply(huAnnualAmount)))).add(booking.multiply(shippingFreq))).multiply(wageFactor);
return new HandlingResult(LoadCarrierType.SLC, return new HandlingResult(LoadCarrierType.SLC,
getRepackingCost(setId, hu, loadCarrierType, addRepackingAndDisposalCost, destinationRepacking).multiply(huAnnualAmount), annualRepacking,
handling.multiply(huAnnualAmount), annualHandling,
destinationDisposal == null ? BigDecimal.ZERO : (disposal.multiply(huAnnualAmount)), //TODO: disposal SLC, ignore? annualDisposal,
huAnnualAmount.multiply((handling.add(booking).add(release).add(dispatch).add(getRepackingCost(setId, hu, loadCarrierType, addRepackingAndDisposalCost, destinationRepacking)))).multiply(wageFactor)); annualHandling.add(annualRepacking).add(annualDisposal));
} }
@ -84,7 +97,6 @@ public class HandlingCostCalculationService {
private HandlingResult getLLCCost(Integer setId, Premise premise, Destination destination, PackagingDimension hu, LoadCarrierType type, boolean addRepackingAndDisposalCost, ContainerCalculationResult containerCalculationResult) { private HandlingResult getLLCCost(Integer setId, Premise premise, Destination destination, PackagingDimension hu, LoadCarrierType type, boolean addRepackingAndDisposalCost, ContainerCalculationResult containerCalculationResult) {
var destinationHandling = destination.getHandlingCost(); var destinationHandling = destination.getHandlingCost();
var destinationDisposal = destination.getDisposalCost(); var destinationDisposal = destination.getDisposalCost();
var destinationRepacking = destination.getRepackingCost(); var destinationRepacking = destination.getRepackingCost();
@ -94,19 +106,24 @@ public class HandlingCostCalculationService {
BigDecimal multiplier = shippingFreq.compareTo(huAnnualAmount) > 0 ? shippingFreq : huAnnualAmount; BigDecimal multiplier = shippingFreq.compareTo(huAnnualAmount) > 0 ? shippingFreq : huAnnualAmount;
BigDecimal handling = destinationHandling != null ? destinationHandling : BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.GLT_HANDLING, setId).orElseThrow().getCurrentValue())); BigDecimal handling = BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.GLT_HANDLING, setId).orElseThrow().getCurrentValue()));
BigDecimal release = BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.GLT_RELEASE, setId).orElseThrow().getCurrentValue())); BigDecimal release = BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.GLT_RELEASE, setId).orElseThrow().getCurrentValue()));
BigDecimal dispatch = BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.GLT_DISPATCH, setId).orElseThrow().getCurrentValue())); BigDecimal dispatch = BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.GLT_DISPATCH, setId).orElseThrow().getCurrentValue()));
BigDecimal disposal = destinationDisposal != null ? destinationDisposal : (addRepackingAndDisposalCost ? BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.DISPOSAL, setId).orElseThrow().getCurrentValue())) : BigDecimal.ZERO); BigDecimal disposal = (addRepackingAndDisposalCost ? BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.DISPOSAL, setId).orElseThrow().getCurrentValue())) : BigDecimal.ZERO);
BigDecimal wageFactor = BigDecimal.valueOf(Double.parseDouble(countryPropertyRepository.getByMappingIdAndCountryId(CountryPropertyMappingId.WAGE, setId, destination.getCountryId()).orElseThrow().getCurrentValue())); BigDecimal wageFactor = BigDecimal.valueOf(Double.parseDouble(countryPropertyRepository.getByMappingIdAndCountryId(CountryPropertyMappingId.WAGE, setId, destination.getCountryId()).orElseThrow().getCurrentValue()));
BigDecimal booking = BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.BOOKING, setId).orElseThrow().getCurrentValue())); BigDecimal booking = BigDecimal.valueOf(Double.parseDouble(propertyRepository.getPropertyByMappingId(SystemPropertyMappingId.BOOKING, setId).orElseThrow().getCurrentValue()));
var annualRepacking = getRepackingCost(setId, hu, type, addRepackingAndDisposalCost, destinationRepacking).multiply(wageFactor).multiply( huAnnualAmount); BigDecimal annualRepacking = Objects.requireNonNullElse(destinationRepacking, getRepackingCost(setId, hu, type, addRepackingAndDisposalCost, destinationRepacking).multiply(wageFactor)).multiply(huAnnualAmount);
var annualHandling = (((handling.multiply(multiplier)).add((dispatch.multiply(huAnnualAmount))).add((release.multiply(huAnnualAmount)))).add(booking.multiply(shippingFreq))).multiply(wageFactor);
var annualDisposal = (disposal.multiply(huAnnualAmount)); BigDecimal annualHandling;
if(destinationHandling != null)
annualHandling = destinationHandling.multiply(multiplier);
else
annualHandling = (((handling.multiply(multiplier)).add((dispatch.multiply(huAnnualAmount))).add((release.multiply(huAnnualAmount)))).add(booking.multiply(shippingFreq))).multiply(wageFactor);
BigDecimal annualDisposal = Objects.requireNonNullElse(destinationDisposal, disposal).multiply(huAnnualAmount);
return new HandlingResult(LoadCarrierType.LLC, annualRepacking, annualHandling, annualDisposal, annualRepacking.add(annualHandling).add(annualDisposal)); return new HandlingResult(LoadCarrierType.LLC, annualRepacking, annualHandling, annualDisposal, annualRepacking.add(annualHandling).add(annualDisposal));