use of org.eevolution.service.FreightServiceInterface in project adempiere by adempiere.
the class FreightRule method calculate.
@Override
public BigDecimal calculate(Properties ctx, int productId, int shipperId, int locationFromId, int locationToId, int freightCategoryId, int currencyId, Timestamp date, String trxName) {
MProduct product = MProduct.get(ctx, productId);
BigDecimal freightRateAmount = BigDecimal.ZERO;
FreightServiceInterface freightService = new FreightService();
BigDecimal freightRate = getFreightRate(ctx, shipperId, freightCategoryId, currencyId, locationFromId, locationToId, date, trxName);
if (freightRate.signum() != 0)
freightRateAmount = product.getWeight().multiply(freightRate);
else
freightRateAmount = BigDecimal.ZERO;
return freightRateAmount;
}
use of org.eevolution.service.FreightServiceInterface in project adempiere by adempiere.
the class FreightRule method getFreightRate.
/**
* get Freight Rate
* @param ctx
* @param shipperId
* @param freightCategoryId
* @param currencyId
* @param date
* @param trxName
* @return
*/
public BigDecimal getFreightRate(Properties ctx, int shipperId, int freightCategoryId, int currencyId, int locationFromId, int locationToId, Timestamp date, String trxName) {
MLocation locationFrom = MLocation.get(ctx, locationFromId, trxName);
MLocation locationTo = MLocation.get(ctx, locationToId, trxName);
Optional<Integer> countryFromOptionalId;
Optional<Integer> regionFromOptionalId;
if (locationFrom != null && locationFrom.get_ID() > 0) {
countryFromOptionalId = Optional.ofNullable(locationFrom.getC_Country_ID());
regionFromOptionalId = Optional.ofNullable(locationFrom.getC_Region_ID());
} else {
countryFromOptionalId = Optional.empty();
regionFromOptionalId = Optional.empty();
}
Optional<Integer> countryToOptionalId;
Optional<Integer> regionToOptionalId;
if (locationTo != null && locationTo.get_ID() > 0) {
countryToOptionalId = Optional.ofNullable(locationTo.getC_Country_ID());
regionToOptionalId = Optional.ofNullable(locationTo.getC_Region_ID());
} else {
countryToOptionalId = Optional.empty();
regionToOptionalId = Optional.empty();
}
FreightServiceInterface freightService = new FreightService();
Optional<MFreight> freightOptioonal = freightService.getFreightValid(ctx, shipperId, freightCategoryId, currencyId, date, trxName).stream().filter(freight -> {
if (freight.getC_Country_ID() == 0 && freight.getTo_Country_ID() == 0)
return true;
else if (freight.getC_Country_ID() == 0 && (countryToOptionalId.isPresent() && countryToOptionalId.get() == freight.getTo_Country_ID()))
return true;
else if ((countryFromOptionalId.isPresent() && countryFromOptionalId.get() == freight.getC_Country_ID()) && (countryToOptionalId.isPresent() && countryToOptionalId.get() == freight.getTo_Country_ID()))
return true;
else
return false;
}).filter(freight -> {
if (freight.getC_Region_ID() == 0 && freight.getTo_Region_ID() == 0)
return true;
else if (freight.getC_Region_ID() == 0 && (regionToOptionalId.isPresent() && regionToOptionalId.get() == freight.getTo_Region_ID()))
return true;
else if ((regionFromOptionalId.isPresent() && regionFromOptionalId.get() == freight.getC_Region_ID()) && (regionToOptionalId.isPresent() && regionToOptionalId.get() == freight.getTo_Region_ID()))
return true;
else
return false;
}).findFirst();
if (freightOptioonal.isPresent()) {
return freightOptioonal.get().getFreightAmt();
} else {
return BigDecimal.ZERO;
}
}
Aggregations