Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/App/Order/Calculator/General/InsuranceCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ public function calculate(): void
* use those shipment-specific bounds.
*
* - NULL or DISABLED (0): use carrier minimum.
* - INHERIT (-1): fall back to settings; if settings do not enable insurance, use carrier default.
* - Explicit amount: resolve to nearest valid tier.
* - INHERIT (-1) or ENABLED (1): fall back to settings; if settings do not enable insurance,
* use carrier default.
* - Explicit amount (> 1): resolve to nearest valid tier.
*
* @param null|int $amount
*
Expand All @@ -93,7 +94,12 @@ private function calculateInsurance(?int $amount): int
return $carrierMin;
}

if (TriStateService::INHERIT === $amount) {
// ENABLED means "insurance is on, derive the amount from the carrier settings" — never a
// literal insured amount of 1 cent. TriStateOptionCalculator resolves INHERIT to ENABLED
// from the exportInsurance setting before this calculator runs, and
// CapabilitiesOptionCalculator forces ENABLED for required options, so this is the value
// real exports arrive with.
if (TriStateService::INHERIT === $amount || TriStateService::ENABLED === $amount) {
return $this->calculateFromSettings($carrier, $carrierMin, $carrierMax, $carrierDefault);
}

Expand Down
9 changes: 5 additions & 4 deletions tests/Unit/App/Endpoint/GetDeliveryOptionsEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,11 @@

$content = json_decode($response->getContent(), true);

// After TriStateOptionCalculator resolves INHERIT → ENABLED (1), InsuranceCalculator treats
// the value as an explicit amount. Tier lookup: first capabilities tier ≥ 1 = 10000 (€100 floor).
// 10000 cents (€100) → 100_000_000 micros
expect($content['shipmentOptions']['insurance']['amount'])->toBe(10000 * 10_000);
// TriStateOptionCalculator resolves INHERIT → ENABLED (1), which InsuranceCalculator reads as
// "derive the amount from the carrier settings" rather than as a literal 1-cent amount.
// Order price 100000 → nearest tier in [0,10000,25000,50000,100000] = 100000, capped by
// exportInsuranceUpTo (50000). 50000 cents (€500) → 500_000_000 micros.
expect($content['shipmentOptions']['insurance']['amount'])->toBe(50000 * 10_000);

Pdk::get(PdkSettingsRepositoryInterface::class)->reset();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use MyParcelNL\Pdk\App\Options\Definition\InsuranceDefinition;
use MyParcelNL\Pdk\App\Order\Calculator\General\InsuranceCalculator;
use MyParcelNL\Pdk\App\Order\Calculator\General\TriStateOptionCalculator;
use MyParcelNL\Pdk\App\Order\Contract\PdkOrderOptionsServiceInterface;
use MyParcelNL\Pdk\App\Order\Contract\PdkProductRepositoryInterface;
use MyParcelNL\Pdk\App\Order\Model\PdkOrder;
Expand Down Expand Up @@ -756,3 +757,73 @@
// orderPrice 5000 < fromAmount 10000 → would return 0, but carrier min=50000 takes precedence
expect($newOrder->deliveryOptions->shipmentOptions->insurance)->toBe(50000);
});

/**
* Every other test in this file runs InsuranceCalculator in isolation, which leaves
* shipmentOptions->insurance at INHERIT. In production TriStateOptionCalculator runs first and
* resolves it from the exportInsurance setting to ENABLED (1), so the ENABLED branch — not the
* INHERIT one — is what real exports take. These cases run both calculators together to cover it.
*/
it('applies the insure-from threshold when insurance is enabled via carrier settings', function (
array $input,
int $result
) {
mockPdkProperty('orderCalculators', [TriStateOptionCalculator::class, InsuranceCalculator::class]);

factory(Settings::class)
->withCarrier(RefCapabilitiesSharedCarrierV2::POSTNL, [
(new InsuranceDefinition())->getCarrierSettingsKey() => true,
CarrierSettings::EXPORT_INSURANCE_FROM_AMOUNT => 700,
CarrierSettings::EXPORT_INSURANCE_PRICE_PERCENTAGE => 100,
CarrierSettings::EXPORT_INSURANCE_UP_TO => $input['upTo'],
])
->store();

factory(PdkProduct::class)
->withExternalIdentifier('threshold-product')
->withPrice($input['orderPrice'])
->store();

$order = factory(PdkOrder::class)
->withShippingAddress(factory(ShippingAddress::class)->withCc('NL'))
->withDeliveryOptions(factory(DeliveryOptions::class)->withCarrier(RefCapabilitiesSharedCarrierV2::POSTNL))
->withLines([
factory(PdkOrderLine::class)
->withProduct('threshold-product')
->withPrice($input['orderPrice']),
])
->make();

/** @var \MyParcelNL\Pdk\App\Order\Contract\PdkOrderOptionsServiceInterface $service */
$service = Pdk::get(PdkOrderOptionsServiceInterface::class);
$newOrder = $service->calculate($order);

expect($newOrder->deliveryOptions->shipmentOptions->insurance)->toBe($result);
})
->with([
'value € 100, insured from € 700 -> not insured' => [
[
'orderPrice' => 10000,
'upTo' => 500000,
],
'result' => 0,
],

'value € 800, insured from € 700 -> rounded up to € 1000' => [
[
'orderPrice' => 80000,
'upTo' => 500000,
],
'result' => 100000,
],

// € 0,00 is the lowest option of the "insure up to" select, so selecting it is an explicit
// merchant choice to cap insurance at zero rather than an unconfigured value to ignore.
'value € 800, insured up to € 0 -> not insured' => [
[
'orderPrice' => 80000,
'upTo' => 0,
],
'result' => 0,
],
]);
Loading