应付计算evri和CTT模板调整计费

This commit is contained in:
liuminghui
2026-11-19 16:49:30 +08:00
parent 43a8bd61e8
commit 908975a7d6
13 changed files with 824 additions and 120 deletions
@@ -50,11 +50,41 @@ class PayCostCalculatorTest
PayOrderCostResult over = PayCostCalculator.calculate(product, new BigDecimal("7"),
null, null, null, null, "Mainland / Mad High Density", null, 1);
assertTrue(hasFee(over, "基础运费", "3.18"));
assertTrue(hasFee(over, "每公斤续重", "0.54"));
assertTrue(hasFee(over, "基础运费", "3.72"));
assertTrue(over.getFees().stream().noneMatch(fee -> "每公斤续重".equals(fee.getFeeName())));
assertEquals(new BigDecimal("3.72"), over.getPayTotal());
}
@Test
void continuationChargesAFullStepWhenTheExcessIsUnderOneKilogram()
{
PayProduct product = new PayProduct();
product.setProductName("CTT 48H");
product.setCurrency("EUR");
PayWeightBand first = band("0", "1", "2.40", "Medium");
first.setSectionName("派送费");
first.setChargeName("派送费");
first.setPriceUnit("EUR/ITEM");
product.setWeightBands(new ArrayList<>(List.of(first)));
PaySurcharge extra = fee(11L, "每公斤续重", "0.34", "EUR", "EXTRA_KG", "1");
extra.setSectionName("派送费");
extra.setZoneName("Medium");
extra.setPriceUnit("EUR/KG");
extra.setChargeText("每增加 1Kg 0.34 EUR/KG");
product.setSurcharges(List.of(extra));
PayOrderCostResult partial = PayCostCalculator.calculate(product, new BigDecimal("1.2"),
null, null, null, null, "Medium", null, 1);
assertTrue(hasFee(partial, "基础运费", "2.74"));
assertTrue(partial.getFees().stream().noneMatch(fee -> "每公斤续重".equals(fee.getFeeName())));
assertEquals(new BigDecimal("2.74"), partial.getPayTotal());
PayOrderCostResult twoSteps = PayCostCalculator.calculate(product, new BigDecimal("2.2"),
null, null, null, null, "Medium", null, 1);
assertTrue(hasFee(twoSteps, "基础运费", "3.08"));
assertTrue(twoSteps.getFees().stream().noneMatch(fee -> "每公斤续重".equals(fee.getFeeName())));
}
@Test
void boundSurchargesKeepTheCardContinuation()
{
@@ -81,8 +111,8 @@ class PayCostCalculatorTest
PayOrderCostResult over = PayCostCalculator.calculate(product, new BigDecimal("3"),
null, null, null, "KY4 0DN", "UK Mainland", null, 1);
assertTrue(hasFee(over, "基础运费", "10"));
assertTrue(hasFee(over, "每公斤续重", "4"));
assertTrue(hasFee(over, "基础运费", "14"));
assertTrue(over.getFees().stream().noneMatch(fee -> "每公斤续重".equals(fee.getFeeName())));
assertTrue(hasFee(over, "燃油", "1"));
assertFalse(over.getFees().stream().anyMatch(fee -> fee.getFeeName().contains("偏远")));
assertFalse(over.getFees().stream().anyMatch(fee -> "超重续收".equals(fee.getFeeName())));
@@ -166,9 +196,8 @@ class PayCostCalculatorTest
PayOrderCostResult over = PayCostCalculator.calculate(product, new BigDecimal("2"),
null, null, null, null, "Canary Islands / Minor Islands", null, 1);
assertTrue(hasFee(over, "基础运费", "13.12"));
assertTrue(hasFee(over, "每公斤续重", "1.86"));
assertEquals(1, over.getFees().stream().filter(fee -> "每公斤续重".equals(fee.getFeeName())).count());
assertTrue(hasFee(over, "基础运费", "14.98"));
assertTrue(over.getFees().stream().noneMatch(fee -> "每公斤续重".equals(fee.getFeeName())));
assertEquals(new BigDecimal("14.98"), over.getPayTotal());
}
@@ -233,6 +262,43 @@ class PayCostCalculatorTest
assertTrue(mainland.getFees().stream().noneMatch(fee -> "偏远附加费".equals(fee.getFeeName())));
}
@Test
void evriRemoteWeightBandAddsTheSameWeightRow()
{
PayProduct product = new PayProduct();
product.setProductName("Evri 48H");
product.setCurrency("GBP");
PayWeightBand mainlandLight = band("0", "1", "1.51", "UK Mainland\n英国大陆");
mainlandLight.setChargeName("派送费");
mainlandLight.setSectionName("派送费");
PayWeightBand mainlandNext = band("1", "2", "1.53", "UK Mainland\n英国大陆");
mainlandNext.setChargeName("派送费");
mainlandNext.setSectionName("派送费");
PayWeightBand highlandsLight = band("0", "1", "5.95", "Highlands& Islands\n高地和岛屿");
highlandsLight.setChargeName("偏远附加费");
highlandsLight.setSectionName("偏远附加费");
PayWeightBand highlandsNext = band("1", "2", "5.95", "Highlands& Islands\n高地和岛屿");
highlandsNext.setChargeName("偏远附加费");
highlandsNext.setSectionName("偏远附加费");
PayWeightBand heavyRemote = band("15", "20", "8.2", "Highlands& Islands\n高地和岛屿");
heavyRemote.setChargeName("偏远附加费");
heavyRemote.setSectionName("偏远附加费");
product.setWeightBands(new ArrayList<>(List.of(mainlandLight, mainlandNext, highlandsLight, highlandsNext, heavyRemote)));
PayOrderCostResult mainland = PayCostCalculator.calculate(product, new BigDecimal("0.5"),
null, null, null, "LL22 8PJ", "UK Mainland\n英国大陆", null, 1);
assertTrue(hasFee(mainland, "基础运费", "1.51"));
assertTrue(mainland.getFees().stream().noneMatch(fee -> "偏远附加费".equals(fee.getFeeName())));
assertEquals(new BigDecimal("1.51"), mainland.getPayTotal());
PayOrderCostResult remote = PayCostCalculator.calculate(product, new BigDecimal("0.5"),
null, null, null, "IV1 1AA", "Highlands& Islands\n高地和岛屿", null, 1);
assertTrue(hasFee(remote, "基础运费", "1.51"));
assertTrue(hasFee(remote, "偏远附加费", "5.95"));
assertEquals(1, remote.getFees().stream().filter(fee -> "偏远附加费".equals(fee.getFeeName())).count());
assertEquals(new BigDecimal("7.46"), remote.getPayTotal());
}
@Test
void spanishPrefixChargesFirstWeightThenExtraKg()
{
@@ -284,9 +350,11 @@ class PayCostCalculatorTest
PayWeightBand lineHaul = band("0", "1", "0.09", "Mad High Density 马德里高密度地区");
lineHaul.setSectionName("额外线路运输费");
lineHaul.setChargeName("额外线路运输费");
PayWeightBand remote = band("0", "1", "7.1", "Balearic Islands 07邮编开头");
PayWeightBand remote = band("0", "1", "7.1", "Balearic Islands");
remote.setSectionName("偏远附加费");
remote.setChargeName("偏远附加费");
remote.setCalcRule("POSTCODE");
remote.setCalcParam("07");
product.setWeightBands(new ArrayList<>(List.of(delivery, lineHaul, remote)));
PaySurcharge deliveryExtra = fee(11L, "每公斤续重", "0.29", "EUR", "EXTRA_KG", "1");
deliveryExtra.setSectionName("派送费");
@@ -296,21 +364,19 @@ class PayCostCalculatorTest
lineExtra.setZoneName("Mad High Density 马德里高密度地区");
PaySurcharge remoteExtra = fee(13L, "每公斤续重", "4", "EUR", "EXTRA_KG", "1");
remoteExtra.setSectionName("偏远附加费");
remoteExtra.setZoneName("Balearic Islands 07邮编开头");
remoteExtra.setZoneName("Balearic Islands");
product.setSurcharges(List.of(deliveryExtra, lineExtra, remoteExtra));
PayOrderCostResult island = PayCostCalculator.calculate(product, new BigDecimal("3"),
null, null, null, "07008", "Mad High Density 马德里高密度地区", null, 1);
assertTrue(hasFee(island, "基础运费", "2.05"));
assertTrue(hasFee(island, "每公斤续重", "0.58"));
assertTrue(hasFee(island, "额外线路运输费", "0.09"));
assertTrue(hasFee(island, "额外线路运输费", "0.10"));
assertTrue(hasFee(island, "偏远附加费", "7.1"));
assertTrue(hasFee(island, "偏远附加费", "8"));
null, null, null, "07008", "Mad High Density 马德里高密度地区", null, 1, List.of(), true, true, "ES", "西班牙");
assertTrue(hasFee(island, "基础运费", "15.1"));
assertTrue(island.getFees().stream().noneMatch(fee -> "每公斤续重".equals(fee.getFeeName())
|| fee.getAmount().compareTo(new BigDecimal("2.05")) == 0
|| fee.getFeeName().contains("额外线路") || "偏远附加费".equals(fee.getFeeName())));
PayOrderCostResult mainland = PayCostCalculator.calculate(product, new BigDecimal("3"),
null, null, null, "28001", "Mad High Density 马德里高密度地区", null, 1);
assertTrue(hasFee(mainland, "基础运费", "2.05"));
assertTrue(hasFee(mainland, "基础运费", "2.63"));
assertTrue(hasFee(mainland, "额外线路运输费", "0.09"));
assertTrue(mainland.getFees().stream().noneMatch(fee -> "偏远附加费".equals(fee.getFeeName())));
assertEquals("07", PaySurchargeRules.remotePrefixes("Balearic Islands 07邮编开头"));
@@ -319,6 +385,40 @@ class PayCostCalculatorTest
assertEquals("", PaySurchargeRules.remotePrefixes("岛屿邮编地区"));
}
@Test
void weightBandConditionSelectsSizeOrWeightTier()
{
PayProduct product = new PayProduct();
product.setProductName("尺寸重量");
product.setCurrency("EUR");
PayWeightBand normal = band("0", "30", "2.00", "普通");
normal.setSectionName("派送费");
normal.setChargeName("派送费");
PayWeightBand heavy = band("0", "30", "8.00", "超重");
heavy.setSectionName("派送费");
heavy.setChargeName("派送费");
heavy.setCalcRule("WEIGHT");
heavy.setCalcParam("5");
PayWeightBand bulky = band("0", "30", "12.00", "超尺寸");
bulky.setSectionName("派送费");
bulky.setChargeName("派送费");
bulky.setCalcRule("SIZE");
bulky.setCalcParam("side:100");
product.setWeightBands(new ArrayList<>(List.of(normal, heavy, bulky)));
PayOrderCostResult plain = PayCostCalculator.calculate(product, new BigDecimal("2"),
new BigDecimal("40"), new BigDecimal("30"), new BigDecimal("20"), "28001", "普通", null, 1);
assertTrue(hasFee(plain, "基础运费", "2"));
PayOrderCostResult overWeight = PayCostCalculator.calculate(product, new BigDecimal("6"),
new BigDecimal("40"), new BigDecimal("30"), new BigDecimal("20"), "28001", "普通", null, 1);
assertTrue(hasFee(overWeight, "基础运费", "8"));
PayOrderCostResult overSize = PayCostCalculator.calculate(product, new BigDecimal("6"),
new BigDecimal("120"), new BigDecimal("30"), new BigDecimal("20"), "28001", "普通", null, 1);
assertTrue(hasFee(overSize, "基础运费", "12"));
}
@Test
void productPostcodeBookChoosesTheMatchingRemoteFee()
{
@@ -37,8 +37,24 @@ class PayWorkbookParserTest
assertTrue(ctt.getWeightBands().stream().anyMatch(band -> band.getWeightLabel() != null && band.getWeightLabel().contains("首重") && decimal(band, "2.05")));
assertTrue(ctt.getWeightBands().stream().noneMatch(band -> band.getWeightLabel() != null && band.getWeightLabel().contains("每增加")));
assertTrue(ctt.getSurcharges().stream().anyMatch(fee -> "每公斤续重".equals(fee.getFeeName()) && fee.getAmount() != null && fee.getAmount().compareTo(new BigDecimal("0.29")) == 0));
assertTrue(ctt.getWeightBands().stream().anyMatch(band -> band.getChargeName() != null && band.getChargeName().contains("偏远") && decimal(band, "7.1")));
assertTrue(ctt.getWeightBands().stream().anyMatch(band -> band.getChargeName() != null && (band.getChargeName().contains("额外") || band.getChargeName().toLowerCase().contains("line-haul")) && decimal(band, "0.09")));
assertTrue(ctt.getWeightBands().stream().anyMatch(band -> "西班牙(ES)".equals(band.getCountryName()) && "ES".equals(band.getCountryCode())
&& band.getZoneName() != null && band.getZoneName().contains("马德里") && decimal(band, "2.05")));
assertTrue(ctt.getWeightBands().stream().anyMatch(band -> "葡萄牙(PT)".equals(band.getCountryName()) && "PT".equals(band.getCountryCode())
&& band.getZoneName() != null && band.getZoneName().contains("Mainland") && decimal(band, "2.63")));
assertTrue(ctt.getWeightBands().stream().noneMatch(band -> band.getCountryName() != null && band.getCountryName().contains("西班牙") && band.getCountryName().contains("葡萄牙")));
assertEquals(1, ctt.getWeightBands().stream().filter(band -> "葡萄牙(PT)".equals(band.getCountryName()) && "派送费".equals(band.getChargeName())).count());
assertTrue(ctt.getWeightBands().stream().anyMatch(band -> band.getChargeName() != null && band.getChargeName().contains("偏远")
&& "西班牙(ES)".equals(band.getCountryName()) && "Balearic Islands".equals(band.getZoneName())
&& "POSTCODE".equals(band.getCalcRule()) && "07".equals(band.getCalcParam()) && decimal(band, "7.1")));
assertTrue(ctt.getWeightBands().stream().anyMatch(band -> band.getChargeName() != null && band.getChargeName().contains("偏远")
&& "葡萄牙(PT)".equals(band.getCountryName()) && "Portuguese Island".equals(band.getZoneName())
&& "POSTCODE".equals(band.getCalcRule()) && "9".equals(band.getCalcParam())));
assertTrue(ctt.getWeightBands().stream().noneMatch(band -> band.getZoneName() != null && band.getZoneName().contains("邮编")));
assertTrue(ctt.getSurcharges().stream().anyMatch(fee -> "每公斤续重".equals(fee.getFeeName()) && fee.getSectionName() != null && fee.getSectionName().contains("偏远")
&& fee.getZoneName() != null && "Balearic Islands".equals(fee.getZoneName())
&& fee.getAmount() != null && fee.getAmount().compareTo(new BigDecimal("4")) == 0));
assertTrue(ctt.getWeightBands().stream().noneMatch(band -> band.getChargeName() != null && (band.getChargeName().contains("额外") || band.getChargeName().toLowerCase().contains("line-haul"))));
assertTrue(ctt.getSurcharges().stream().noneMatch(fee -> fee.getFeeName() != null && (fee.getFeeName().contains("额外线路") || fee.getFeeName().toLowerCase().contains("line-haul") || fee.getSectionName() != null && fee.getSectionName().toLowerCase().contains("line-haul"))));
}
@Test
@@ -225,14 +241,24 @@ class PayWorkbookParserTest
assertEquals("Evri 48H", evri.getDictValue());
assertEquals("Evri 48H", evri.getSheetName());
assertFalse(evri.getWeightBands().isEmpty());
assertTrue(evri.getWeightBands().stream().allMatch(band -> "派送费".equals(band.getChargeName())));
assertTrue(evri.getWeightBands().stream().noneMatch(band -> bandText(band).contains("偏远") || bandText(band).contains("Highlands")));
assertTrue(evri.getWeightBands().stream().anyMatch(band -> decimal(band, "1.51") && band.getZoneName().contains("Mainland")));
PaySurcharge highlands = evri.getSurcharges().stream()
.filter(fee -> "偏远附加费".equals(fee.getFeeName()) && fee.getZoneName().contains("Highlands") && decimalFee(fee, "5.95"))
assertTrue(evri.getWeightBands().stream().filter(band -> "派送费".equals(band.getChargeName()))
.allMatch(band -> band.getZoneName() != null && band.getZoneName().contains("Mainland")));
assertTrue(evri.getWeightBands().stream().anyMatch(band -> decimal(band, "1.51") && band.getZoneName().contains("Mainland")
&& "派送费".equals(band.getChargeName())));
PayWeightBand highlands = evri.getWeightBands().stream()
.filter(band -> "偏远附加费".equals(band.getChargeName()) && band.getZoneName().contains("Highlands") && decimal(band, "5.95"))
.findFirst().orElseThrow();
assertEquals("ZONE", highlands.getCalcRule());
assertTrue(highlands.getWeightMax() != null && highlands.getWeightMax().compareTo(java.math.BigDecimal.ONE) == 0);
assertTrue(highlands.getWeightMin() != null && highlands.getWeightMin().compareTo(java.math.BigDecimal.ZERO) == 0);
assertEquals(5, evri.getWeightBands().stream()
.filter(band -> "偏远附加费".equals(band.getChargeName()) && band.getWeightMax() != null
&& band.getWeightMax().compareTo(java.math.BigDecimal.ONE) == 0)
.count());
assertTrue(evri.getWeightBands().stream().anyMatch(band -> "偏远附加费".equals(band.getChargeName())
&& band.getZoneName().contains("Isle of Man") && decimal(band, "8.2")
&& band.getWeightMin() != null && band.getWeightMin().compareTo(new java.math.BigDecimal("15")) == 0));
assertTrue(evri.getSurcharges().stream().noneMatch(fee -> "偏远附加费".equals(fee.getFeeName()) && decimalFee(fee, "5.95")));
assertTrue(evri.getSurcharges().stream().anyMatch(fee -> "战争附加费".equals(fee.getFeeName())));
}
@Test
@@ -458,29 +484,18 @@ class PayWorkbookParserTest
PayPreview preview = parser.parseMatched(Files.readAllBytes(file), file.getFileName().toString(), "CTT 48H",
List.of("CTT 48H"));
PayProduct ctt = preview.getProducts().get(0);
assertTrue(ctt.getWeightBands().stream().allMatch(band -> "派送费".equals(band.getChargeName())),
ctt.getWeightBands().stream().map(band -> band.getChargeName() + "/" + band.getZoneName() + "/" + band.getPrice()).toList().toString());
assertTrue(ctt.getWeightBands().stream().anyMatch(band -> decimal(band, "2.05") && band.getZoneName().contains("马德里")));
assertTrue(ctt.getWeightBands().stream().noneMatch(band -> bandText(band).contains("偏远") || bandText(band).contains("额外")
|| bandText(band).toLowerCase().contains("line-haul")));
PaySurcharge balearic = ctt.getSurcharges().stream()
.filter(fee -> "偏远附加费".equals(fee.getFeeName()) && fee.getZoneName().contains("Balearic") && decimalFee(fee, "7.1"))
.findFirst().orElseThrow();
assertTrue(balearic.getFeeDesc().contains("首重"));
assertEquals("POSTCODE", balearic.getCalcRule());
assertTrue(balearic.getCalcParam() != null && balearic.getCalcParam().contains("07"));
assertTrue(balearic.getWeightMax() != null && balearic.getWeightMax().compareTo(new java.math.BigDecimal("1")) == 0);
PaySurcharge canary = ctt.getSurcharges().stream()
.filter(fee -> "偏远附加费".equals(fee.getFeeName()) && fee.getZoneName().contains("Canary") && decimalFee(fee, "4.5")
&& fee.getChargeText().contains("KG"))
.findFirst().orElseThrow();
assertEquals("EXTRA_KG", canary.getCalcRule());
assertEquals("1", canary.getCalcParam());
assertTrue(canary.getPostcodeParam() != null && canary.getPostcodeParam().contains("35") && canary.getPostcodeParam().contains("38"));
assertTrue(ctt.getSurcharges().stream().anyMatch(fee -> fee.getFeeName().contains("额外线路运输费") && "MANUAL".equals(fee.getCalcRule())));
assertTrue(ctt.getSurcharges().stream().anyMatch(fee -> fee.getFeeName().contains("额外线路运输费") && decimalFee(fee, "0.09")));
assertTrue(ctt.getSurcharges().stream().anyMatch(fee -> fee.getFeeName().contains("额外线路运输费")
&& fee.getFeeDesc().contains("800公里")));
assertTrue(ctt.getWeightBands().stream().anyMatch(band -> "派送费".equals(band.getChargeName()) && "西班牙(ES)".equals(band.getCountryName())
&& decimal(band, "2.05") && band.getZoneName().contains("马德里")));
assertTrue(ctt.getWeightBands().stream().anyMatch(band -> "派送费".equals(band.getChargeName()) && "葡萄牙(PT)".equals(band.getCountryName())
&& band.getZoneName() != null && band.getZoneName().contains("Mainland")));
assertTrue(ctt.getWeightBands().stream().anyMatch(band -> band.getChargeName() != null && band.getChargeName().contains("偏远")
&& "西班牙(ES)".equals(band.getCountryName()) && band.getZoneName() != null && band.getZoneName().contains("Balearic") && decimal(band, "7.1")));
assertTrue(ctt.getSurcharges().stream().anyMatch(fee -> "每公斤续重".equals(fee.getFeeName()) && fee.getSectionName() != null
&& fee.getSectionName().contains("偏远") && fee.getZoneName() != null && fee.getZoneName().contains("Canary") && decimalFee(fee, "4.5")));
assertTrue(ctt.getWeightBands().stream().noneMatch(band -> band.getChargeName() != null
&& (band.getChargeName().contains("额外") || band.getChargeName().toLowerCase().contains("line-haul"))));
assertTrue(ctt.getSurcharges().stream().noneMatch(fee -> (fee.getFeeName() != null && (fee.getFeeName().contains("额外线路") || fee.getFeeName().toLowerCase().contains("line-haul")))
|| (fee.getSectionName() != null && (fee.getSectionName().contains("额外线路") || fee.getSectionName().toLowerCase().contains("line-haul")))));
assertTrue(ctt.getSurcharges().stream().anyMatch(fee -> "超重附加费".equals(fee.getFeeName()) && decimalFee(fee, "5.98")));
}
@@ -588,13 +603,13 @@ class PayWorkbookParserTest
PayOrderCostResult twoKg = PayCostCalculator.calculate(ctt, new java.math.BigDecimal("2"), null, null, null, null,
"Balearic Islands Economy 48H / Majorca", null, 1);
assertEquals(new java.math.BigDecimal("4.24"), twoKg.getPayTotal());
assertEquals(1, twoKg.getFees().stream().filter(fee -> "每公斤续重".equals(fee.getFeeName())
&& fee.getAmount().compareTo(new java.math.BigDecimal("0.85")) == 0).count());
assertTrue(twoKg.getFees().stream().anyMatch(fee -> "基础运费".equals(fee.getFeeName())
&& fee.getAmount().compareTo(new java.math.BigDecimal("4.24")) == 0));
assertTrue(twoKg.getFees().stream().noneMatch(fee -> "每公斤续重".equals(fee.getFeeName())));
PayOrderCostResult island = PayCostCalculator.calculate(ctt, new java.math.BigDecimal("2"), null, null, null, null,
"Portuguese Island / Island", null, 1);
assertEquals(new java.math.BigDecimal("12.41"), island.getPayTotal());
assertEquals(1, island.getFees().stream().filter(fee -> "每公斤续重".equals(fee.getFeeName())
&& fee.getAmount().compareTo(new java.math.BigDecimal("3.34")) == 0).count());
assertTrue(island.getFees().stream().noneMatch(fee -> "每公斤续重".equals(fee.getFeeName())));
}
@Test