Skip to content

Commit

Permalink
fix(calculator): always send insurance for dpd (#219)
Browse files Browse the repository at this point in the history
INT-7
  • Loading branch information
Mark-Ernst authored Nov 14, 2023
1 parent 120f71d commit a360ae8
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 11 deletions.
2 changes: 1 addition & 1 deletion config/platform/belgie.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
DeliveryOptions::DELIVERY_TYPE_PICKUP_NAME,
],
'shipmentOptions' => [
'insurance' => [0],
'insurance' => [52000],
],
'features' => [
'dropOffAtPostalPoint' => true,
Expand Down
3 changes: 3 additions & 0 deletions config/platform/myparcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@
'ageCheck' => false,
'onlyRecipient' => false,
'return' => false,
'insurance' => [
52000,
],
],
'features' => [
'labelDescriptionLength' => 45,
Expand Down
7 changes: 6 additions & 1 deletion src/App/Order/Calculator/General/InsuranceCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/Frontend/View/CarrierSettingsItemView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,4 @@ function () {
doTest($carrierName, null, factory(ShipmentOptions::class)->withAllOptions(), $result);
})->with([
Carrier::CARRIER_BPOST_NAME,
Carrier::CARRIER_DPD_NAME,
]);
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
},
"packageTypes": ["package"],
"shipmentOptions": {
"insurance": [0]
"insurance": [52000]
}
},
"returnCapabilities": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
"shipmentOptions": {
"ageCheck": false,
"onlyRecipient": false,
"return": false
"return": false,
"insurance": [52000]
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
},
"packageTypes": ["package"],
"shipmentOptions": {
"insurance": [0]
"insurance": [52000]
}
},
"returnCapabilities": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
"shipmentOptions": {
"ageCheck": false,
"onlyRecipient": false,
"return": false
"return": false,
"insurance": [52000]
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"packageTypes": ["package"],
"deliveryTypes": ["standard", "pickup"],
"shipmentOptions": {
"insurance": [0]
"insurance": [52000]
},
"features": {
"dropOffAtPostalPoint": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@
"shipmentOptions": {
"ageCheck": false,
"onlyRecipient": false,
"return": false
"return": false,
"insurance": [52000]
},
"features": {
"labelDescriptionLength": 45,
Expand Down
19 changes: 17 additions & 2 deletions tests/factories/Carrier/Model/CarrierCapabilitiesFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
]
);
}

Expand Down

0 comments on commit a360ae8

Please sign in to comment.