From a360ae8151cc4fb0aac8ece9df97c14ea05efbd7 Mon Sep 17 00:00:00 2001 From: Mark Ernst <33826576+Mark-Ernst@users.noreply.github.com> Date: Tue, 14 Nov 2023 14:59:13 +0100 Subject: [PATCH] fix(calculator): always send insurance for dpd (#219) INT-7 --- config/platform/belgie.php | 2 +- config/platform/myparcel.php | 3 ++ .../General/InsuranceCalculator.php | 7 +++- src/Frontend/View/CarrierSettingsItemView.php | 6 ++++ .../General/CarrierSpecificCalculatorTest.php | 1 - .../General/InsuranceCalculatorTest.php | 34 +++++++++++++++++++ ...s_carriers_from_name_with_(belgie)__1.json | 2 +- ...carriers_from_name_with_(myparcel)__1.json | 3 +- ...st__it_gets_carriers_with_(belgie)__1.json | 2 +- ...__it_gets_carriers_with_(myparcel)__1.json | 3 +- ...ig_for_each_platform_with_(belgie)__1.json | 2 +- ..._for_each_platform_with_(myparcel)__1.json | 3 +- .../Model/CarrierCapabilitiesFactory.php | 19 +++++++++-- 13 files changed, 76 insertions(+), 11 deletions(-) diff --git a/config/platform/belgie.php b/config/platform/belgie.php index 8e82512aa..96c13469b 100644 --- a/config/platform/belgie.php +++ b/config/platform/belgie.php @@ -88,7 +88,7 @@ DeliveryOptions::DELIVERY_TYPE_PICKUP_NAME, ], 'shipmentOptions' => [ - 'insurance' => [0], + 'insurance' => [52000], ], 'features' => [ 'dropOffAtPostalPoint' => true, diff --git a/config/platform/myparcel.php b/config/platform/myparcel.php index 51b2bfdc0..c4d2d6a59 100644 --- a/config/platform/myparcel.php +++ b/config/platform/myparcel.php @@ -292,6 +292,9 @@ 'ageCheck' => false, 'onlyRecipient' => false, 'return' => false, + 'insurance' => [ + 52000, + ], ], 'features' => [ 'labelDescriptionLength' => 45, diff --git a/src/App/Order/Calculator/General/InsuranceCalculator.php b/src/App/Order/Calculator/General/InsuranceCalculator.php index fcfb2b6c2..a7ca0df2f 100644 --- a/src/App/Order/Calculator/General/InsuranceCalculator.php +++ b/src/App/Order/Calculator/General/InsuranceCalculator.php @@ -12,6 +12,7 @@ use MyParcelNL\Pdk\Facade\Platform; use MyParcelNL\Pdk\Settings\Model\CarrierSettings; use MyParcelNL\Pdk\Types\Service\TriStateService; +use MyParcelNL\Pdk\Validation\Validator\CarrierSchema; final class InsuranceCalculator extends AbstractPdkOrderOptionCalculator { @@ -56,7 +57,11 @@ public function calculate(): void private function calculateInsurance(?int $amount): int { if (null === $amount || TriStateService::DISABLED === $amount) { - return 0; + /** @var \MyParcelNL\Pdk\Validation\Validator\CarrierSchema $schema */ + $schema = Pdk::get(CarrierSchema::class); + $carrierSchema = $schema->setCarrier($this->order->deliveryOptions->carrier); + + return $carrierSchema->getAllowedInsuranceAmounts()[0] ?? 0; } $carrierSettings = CarrierSettings::fromCarrier($this->order->deliveryOptions->carrier); diff --git a/src/Frontend/View/CarrierSettingsItemView.php b/src/Frontend/View/CarrierSettingsItemView.php index 145fbd1fa..b45c67c19 100644 --- a/src/Frontend/View/CarrierSettingsItemView.php +++ b/src/Frontend/View/CarrierSettingsItemView.php @@ -404,6 +404,12 @@ function (FormOperationBuilder $builder) { */ private function getExportInsuranceFields(): array { + $insuranceAmounts = $this->carrierSchema->getAllowedInsuranceAmounts(); + + if (count($insuranceAmounts) <= 1) { + return []; + } + return [ new InteractiveElement(CarrierSettings::EXPORT_INSURANCE, Components::INPUT_TOGGLE), diff --git a/tests/Unit/App/Order/Calculator/General/CarrierSpecificCalculatorTest.php b/tests/Unit/App/Order/Calculator/General/CarrierSpecificCalculatorTest.php index b3f1128c5..958e0e78e 100644 --- a/tests/Unit/App/Order/Calculator/General/CarrierSpecificCalculatorTest.php +++ b/tests/Unit/App/Order/Calculator/General/CarrierSpecificCalculatorTest.php @@ -193,5 +193,4 @@ function () { doTest($carrierName, null, factory(ShipmentOptions::class)->withAllOptions(), $result); })->with([ Carrier::CARRIER_BPOST_NAME, - Carrier::CARRIER_DPD_NAME, ]); diff --git a/tests/Unit/App/Order/Calculator/General/InsuranceCalculatorTest.php b/tests/Unit/App/Order/Calculator/General/InsuranceCalculatorTest.php index e02b26574..6d89fe405 100644 --- a/tests/Unit/App/Order/Calculator/General/InsuranceCalculatorTest.php +++ b/tests/Unit/App/Order/Calculator/General/InsuranceCalculatorTest.php @@ -13,10 +13,12 @@ use MyParcelNL\Pdk\App\Order\Model\PdkProduct; use MyParcelNL\Pdk\App\Order\Model\ShippingAddress; use MyParcelNL\Pdk\Carrier\Model\Carrier; +use MyParcelNL\Pdk\Carrier\Model\CarrierCapabilities; use MyParcelNL\Pdk\Facade\Pdk; use MyParcelNL\Pdk\Settings\Model\CarrierSettings; use MyParcelNL\Pdk\Settings\Model\Settings; use MyParcelNL\Pdk\Shipment\Model\DeliveryOptions; +use MyParcelNL\Pdk\Shipment\Model\ShipmentOptions; use MyParcelNL\Pdk\Tests\Bootstrap\MockPdkProductRepository; use MyParcelNL\Pdk\Tests\Uses\UsesMockPdkInstance; use function MyParcelNL\Pdk\Tests\factory; @@ -333,3 +335,35 @@ 'result' => 400000, ], ]); + +it('calculates insurance for fixed insurance amount when insurance is disabled', function () { + mockPdkProperty('orderCalculators', [InsuranceCalculator::class]); + + factory(Settings::class) + ->withCarrier( + Carrier::CARRIER_DPD_NAME, + [ + CarrierSettings::EXPORT_INSURANCE => false, + ] + ) + ->store(); + + $carrier = factory(Carrier::class) + ->withName(Carrier::CARRIER_DPD_NAME) + ->withCapabilities(factory(CarrierCapabilities::class)->withShipmentOptions(['insurance' => [52000]])) + ->make(); + + $order = factory(PdkOrder::class) + ->withDeliveryOptions( + factory(DeliveryOptions::class) + ->withCarrier($carrier) + ->withShipmentOptions(factory(ShipmentOptions::class)->withInsurance(0)) + ) + ->make(); + + /** @var \MyParcelNL\Pdk\App\Order\Contract\PdkOrderOptionsServiceInterface $service */ + $service = Pdk::get(PdkOrderOptionsServiceInterface::class); + $newOrder = $service->calculate($order); + + expect($newOrder->deliveryOptions->shipmentOptions->insurance)->toBe(52000); +}); diff --git a/tests/__snapshots__/CarrierTest__it_instantiates_carriers_from_name_with_(belgie)__1.json b/tests/__snapshots__/CarrierTest__it_instantiates_carriers_from_name_with_(belgie)__1.json index 86e9fe8ae..6e4ef19bd 100644 --- a/tests/__snapshots__/CarrierTest__it_instantiates_carriers_from_name_with_(belgie)__1.json +++ b/tests/__snapshots__/CarrierTest__it_instantiates_carriers_from_name_with_(belgie)__1.json @@ -73,7 +73,7 @@ }, "packageTypes": ["package"], "shipmentOptions": { - "insurance": [0] + "insurance": [52000] } }, "returnCapabilities": { diff --git a/tests/__snapshots__/CarrierTest__it_instantiates_carriers_from_name_with_(myparcel)__1.json b/tests/__snapshots__/CarrierTest__it_instantiates_carriers_from_name_with_(myparcel)__1.json index 3c73da8ca..a649a996b 100644 --- a/tests/__snapshots__/CarrierTest__it_instantiates_carriers_from_name_with_(myparcel)__1.json +++ b/tests/__snapshots__/CarrierTest__it_instantiates_carriers_from_name_with_(myparcel)__1.json @@ -61,7 +61,8 @@ "shipmentOptions": { "ageCheck": false, "onlyRecipient": false, - "return": false + "return": false, + "insurance": [52000] } } }, diff --git a/tests/__snapshots__/PlatformManagerTest__it_gets_carriers_with_(belgie)__1.json b/tests/__snapshots__/PlatformManagerTest__it_gets_carriers_with_(belgie)__1.json index 86e9fe8ae..6e4ef19bd 100644 --- a/tests/__snapshots__/PlatformManagerTest__it_gets_carriers_with_(belgie)__1.json +++ b/tests/__snapshots__/PlatformManagerTest__it_gets_carriers_with_(belgie)__1.json @@ -73,7 +73,7 @@ }, "packageTypes": ["package"], "shipmentOptions": { - "insurance": [0] + "insurance": [52000] } }, "returnCapabilities": { diff --git a/tests/__snapshots__/PlatformManagerTest__it_gets_carriers_with_(myparcel)__1.json b/tests/__snapshots__/PlatformManagerTest__it_gets_carriers_with_(myparcel)__1.json index 3c73da8ca..a649a996b 100644 --- a/tests/__snapshots__/PlatformManagerTest__it_gets_carriers_with_(myparcel)__1.json +++ b/tests/__snapshots__/PlatformManagerTest__it_gets_carriers_with_(myparcel)__1.json @@ -61,7 +61,8 @@ "shipmentOptions": { "ageCheck": false, "onlyRecipient": false, - "return": false + "return": false, + "insurance": [52000] } } }, diff --git a/tests/__snapshots__/PlatformManagerTest__it_retrieves_config_for_each_platform_with_(belgie)__1.json b/tests/__snapshots__/PlatformManagerTest__it_retrieves_config_for_each_platform_with_(belgie)__1.json index dcf3475d9..b0b1dbf1e 100644 --- a/tests/__snapshots__/PlatformManagerTest__it_retrieves_config_for_each_platform_with_(belgie)__1.json +++ b/tests/__snapshots__/PlatformManagerTest__it_retrieves_config_for_each_platform_with_(belgie)__1.json @@ -53,7 +53,7 @@ "packageTypes": ["package"], "deliveryTypes": ["standard", "pickup"], "shipmentOptions": { - "insurance": [0] + "insurance": [52000] }, "features": { "dropOffAtPostalPoint": true, diff --git a/tests/__snapshots__/PlatformManagerTest__it_retrieves_config_for_each_platform_with_(myparcel)__1.json b/tests/__snapshots__/PlatformManagerTest__it_retrieves_config_for_each_platform_with_(myparcel)__1.json index 07fc30adb..d2f8a8295 100644 --- a/tests/__snapshots__/PlatformManagerTest__it_retrieves_config_for_each_platform_with_(myparcel)__1.json +++ b/tests/__snapshots__/PlatformManagerTest__it_retrieves_config_for_each_platform_with_(myparcel)__1.json @@ -163,7 +163,8 @@ "shipmentOptions": { "ageCheck": false, "onlyRecipient": false, - "return": false + "return": false, + "insurance": [52000] }, "features": { "labelDescriptionLength": 45, diff --git a/tests/factories/Carrier/Model/CarrierCapabilitiesFactory.php b/tests/factories/Carrier/Model/CarrierCapabilitiesFactory.php index a93619c0c..49cc7c97a 100644 --- a/tests/factories/Carrier/Model/CarrierCapabilitiesFactory.php +++ b/tests/factories/Carrier/Model/CarrierCapabilitiesFactory.php @@ -70,12 +70,27 @@ public function withAllOptions(): self ShipmentOptions::AGE_CHECK, ShipmentOptions::DIRECT_RETURN, ShipmentOptions::HIDE_SENDER, - ShipmentOptions::INSURANCE, ShipmentOptions::LARGE_FORMAT, ShipmentOptions::ONLY_RECIPIENT, ShipmentOptions::SAME_DAY_DELIVERY, ShipmentOptions::SIGNATURE, - ], true) + ], true) + [ + ShipmentOptions::INSURANCE => [ + 0, + 10000, + 25000, + 50000, + 100000, + 150000, + 200000, + 250000, + 300000, + 350000, + 400000, + 450000, + 500000, + ], + ] ); }