Skip to content

Commit

Permalink
feat: add receipt code (#314)
Browse files Browse the repository at this point in the history
INT-684
  • Loading branch information
GravendeelJochem authored Nov 13, 2024
1 parent df95031 commit 103b856
Show file tree
Hide file tree
Showing 55 changed files with 608 additions and 35 deletions.
2 changes: 2 additions & 0 deletions config/pdk-business-logic.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use MyParcelNL\Pdk\App\Options\Definition\SameDayDeliveryDefinition;
use MyParcelNL\Pdk\App\Options\Definition\SignatureDefinition;
use MyParcelNL\Pdk\App\Options\Definition\TrackedDefinition;
use MyParcelNL\Pdk\App\Options\Definition\ReceiptCodeDefinition;
use MyParcelNL\Pdk\App\Order\Calculator\General\AllowedInCarrierCalculator;
use MyParcelNL\Pdk\App\Order\Calculator\General\CarrierSpecificCalculator;
use MyParcelNL\Pdk\App\Order\Calculator\General\CustomerInformationCalculator;
Expand Down Expand Up @@ -74,6 +75,7 @@
new SameDayDeliveryDefinition(),
new SignatureDefinition(),
new TrackedDefinition(),
new ReceiptCodeDefinition(),
];
}),

Expand Down
1 change: 1 addition & 0 deletions config/pdk-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
'dropOffDaysDeviations' => [],
],
CarrierSettings::EXPORT_AGE_CHECK => false,
CarrierSettings::EXPORT_RECEIPT_CODE => false,
CarrierSettings::EXPORT_INSURANCE => false,
CarrierSettings::EXPORT_INSURANCE_FROM_AMOUNT => 0,
CarrierSettings::EXPORT_INSURANCE_PRICE_PERCENTAGE => 100,
Expand Down
1 change: 1 addition & 0 deletions config/platform/myparcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'return' => true,
'sameDayDelivery' => false,
'signature' => true,
'receiptCode' => true,
'insurance' => [
0,
10000,
Expand Down
3 changes: 3 additions & 0 deletions config/schema/myparcel/order/postnl/be_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
},
"tracked": {
"enum": [0, 1]
},
"receiptCode": {
"enum": [-1]
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions config/schema/myparcel/order/postnl/nl_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
500000,
null
]
},
"receiptCode": {
"enum": [-1, 0, 1]
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions config/schema/order.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@
},
"tracked": {
"enum": [0, 1]
},
"receiptCode": {
"enum": [0, 1]
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/App/Options/Definition/ReceiptCodeDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Pdk\App\Options\Definition;

use MyParcelNL\Pdk\App\Options\Contract\OrderOptionDefinitionInterface;
use MyParcelNL\Pdk\Settings\Model\CarrierSettings;
use MyParcelNL\Pdk\Settings\Model\ProductSettings;
use MyParcelNL\Pdk\Shipment\Model\ShipmentOptions;
use MyParcelNL\Pdk\Validation\Validator\CarrierSchema;

final class ReceiptCodeDefinition implements OrderOptionDefinitionInterface
{
public function getCarrierSettingsKey(): ?string
{
return CarrierSettings::EXPORT_RECEIPT_CODE;
}

public function getProductSettingsKey(): ?string
{
return null;
}

public function getShipmentOptionsKey(): ?string
{
return ShipmentOptions::RECEIPT_CODE;
}

public function validate(CarrierSchema $carrierSchema): bool
{
return $carrierSchema->canHaveReceiptCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function calculate(): void
ShipmentOptions::ONLY_RECIPIENT => TriStateService::DISABLED,
ShipmentOptions::SAME_DAY_DELIVERY => TriStateService::DISABLED,
ShipmentOptions::SIGNATURE => TriStateService::DISABLED,
ShipmentOptions::RECEIPT_CODE => TriStateService::DISABLED,
ShipmentOptions::TRACKED => $this->calculateTracked(),
]);
}
Expand Down
1 change: 1 addition & 0 deletions src/App/Order/Calculator/PostNl/PostNLCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ protected function getCalculators(): array
return [
PostNLCountryShipmentOptionsCalculator::class,
PostNLAgeCheckCalculator::class,
PostNLReceiptCodeCalculator::class,
PostNLDeliveryTypeCalculator::class,
];
}
Expand Down
85 changes: 85 additions & 0 deletions src/App/Order/Calculator/PostNl/PostNLReceiptCodeCalculator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Pdk\App\Order\Calculator\PostNl;

use MyParcelNL\Pdk\App\Order\Calculator\AbstractPdkOrderOptionCalculator;
use MyParcelNL\Pdk\Base\Service\CountryCodes;
use MyParcelNL\Pdk\Types\Service\TriStateService;
use MyParcelNL\Pdk\Validation\Validator\CarrierSchema;
use MyParcelNL\Pdk\Facade\Pdk;

/**
* When receipt code is enabled, insurance is required.
*/
final class PostNLReceiptCodeCalculator extends AbstractPdkOrderOptionCalculator
{
/**
* Calculates the receipt code options for PostNL shipments.
* When receipt code is enabled:
* - Shipment must be to the Netherlands
* - Receipt code will be disabled if age check is active
* - Signature and only recipient will be disabled
* - Large format will be disabled
* - Return will be disabled
* - Insurance will be enabled (minimum €100) when no insurance is active
*/
public function calculate(): void
{
$shipmentOptions = $this->order->deliveryOptions->shipmentOptions;

if (TriStateService::ENABLED !== $shipmentOptions->receiptCode) {
return;
}

if ($this->order->shippingAddress->cc !== CountryCodes::CC_NL) {
$shipmentOptions->receiptCode = TriStateService::DISABLED;
return;
}

if (TriStateService::ENABLED === $shipmentOptions->ageCheck) {
$shipmentOptions->receiptCode = TriStateService::DISABLED;
return;
}

$shipmentOptions->signature = TriStateService::DISABLED;
$shipmentOptions->onlyRecipient = TriStateService::DISABLED;
$shipmentOptions->largeFormat = TriStateService::DISABLED;
$shipmentOptions->return = TriStateService::DISABLED;

if ($shipmentOptions->insurance === TriStateService::DISABLED) {
/** @var \MyParcelNL\Pdk\Validation\Validator\CarrierSchema $schema */
$schema = Pdk::get(CarrierSchema::class);
$allowedInsuranceAmounts = $schema
->setCarrier($this->order->deliveryOptions->carrier)
->getAllowedInsuranceAmounts();

$shipmentOptions->insurance = $this->getLowestInsuranceAmount($allowedInsuranceAmounts);
}
}

/**
* Gets the lowest allowed insurance amount that is greater than 0.
*
* @param int[] $insuranceAmount
*
* @return int
*/
private function getLowestInsuranceAmount(array $insuranceAmount): int
{
$lowestAmount = null;

foreach ($insuranceAmount as $allowedInsuranceAmount) {
if ($allowedInsuranceAmount <= 0) {
continue;
}

if (null === $lowestAmount || $allowedInsuranceAmount < $lowestAmount) {
$lowestAmount = $allowedInsuranceAmount;
}
}

return $lowestAmount ?? 0;
}
}
4 changes: 4 additions & 0 deletions src/Frontend/View/CarrierSettingsItemView.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ function (FormOperationBuilder $builder) {
: []
),

$this->carrierSchema->canHaveReceiptCode() ? [
new InteractiveElement(CarrierSettings::EXPORT_RECEIPT_CODE, Components::INPUT_TOGGLE),
] : [],

$this->carrierSchema->canHaveLargeFormat()
? [new InteractiveElement(CarrierSettings::EXPORT_LARGE_FORMAT, Components::INPUT_TOGGLE)]
: [],
Expand Down
3 changes: 3 additions & 0 deletions src/Settings/Model/CarrierSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class CarrierSettings extends AbstractSettingsModel
public const EXPORT_RETURN_PACKAGE_TYPE = 'exportReturnPackageType';
public const EXPORT_SIGNATURE = 'exportSignature';
public const EXPORT_TRACKED = 'exportTracked';
public const EXPORT_RECEIPT_CODE = 'exportReceiptCode';
public const PRICE_DELIVERY_TYPE_EVENING = 'priceDeliveryTypeEvening';
public const PRICE_DELIVERY_TYPE_MONDAY = 'priceDeliveryTypeMonday';
public const PRICE_DELIVERY_TYPE_MORNING = 'priceDeliveryTypeMorning';
Expand Down Expand Up @@ -144,6 +145,7 @@ class CarrierSettings extends AbstractSettingsModel
self::DROP_OFF_DELAY => 0,
self::DROP_OFF_POSSIBILITIES => DropOffPossibilities::class,
self::EXPORT_AGE_CHECK => false,
self::EXPORT_RECEIPT_CODE => false,
self::EXPORT_HIDE_SENDER => false,
self::EXPORT_INSURANCE => false,
self::EXPORT_INSURANCE_FROM_AMOUNT => 0,
Expand Down Expand Up @@ -197,6 +199,7 @@ class CarrierSettings extends AbstractSettingsModel
self::DROP_OFF_DELAY => 'int',
self::DROP_OFF_POSSIBILITIES => DropOffPossibilities::class,
self::EXPORT_AGE_CHECK => 'bool',
self::EXPORT_RECEIPT_CODE => 'bool',
self::EXPORT_INSURANCE => 'bool',
self::EXPORT_INSURANCE_FROM_AMOUNT => 'int',
self::EXPORT_INSURANCE_PRICE_PERCENTAGE => 'float',
Expand Down
4 changes: 4 additions & 0 deletions src/Shipment/Model/ShipmentOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @property int<-1|0|1> $sameDayDelivery
* @property int<-1|0|1> $signature
* @property int<-1|0|1> $tracked
* @property int<-1|0|1> $receiptCode
*/
class ShipmentOptions extends Model
{
Expand All @@ -31,6 +32,7 @@ class ShipmentOptions extends Model
public const SAME_DAY_DELIVERY = 'sameDayDelivery';
public const SIGNATURE = 'signature';
public const TRACKED = 'tracked';
public const RECEIPT_CODE = 'receiptCode';

protected $attributes = [
self::LABEL_DESCRIPTION => null,
Expand All @@ -43,6 +45,7 @@ class ShipmentOptions extends Model
self::SAME_DAY_DELIVERY => TriStateService::INHERIT,
self::SIGNATURE => TriStateService::INHERIT,
self::TRACKED => TriStateService::INHERIT,
self::RECEIPT_CODE => TriStateService::INHERIT,
];

protected $casts = [
Expand All @@ -56,5 +59,6 @@ class ShipmentOptions extends Model
self::SAME_DAY_DELIVERY => TriStateService::TYPE_STRICT,
self::SIGNATURE => TriStateService::TYPE_STRICT,
self::TRACKED => TriStateService::TYPE_STRICT,
self::RECEIPT_CODE => TriStateService::TYPE_STRICT,
];
}
6 changes: 6 additions & 0 deletions src/Validation/Validator/CarrierSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use MyParcelNL\Pdk\App\Options\Definition\InsuranceDefinition;
use MyParcelNL\Pdk\App\Options\Definition\LargeFormatDefinition;
use MyParcelNL\Pdk\App\Options\Definition\OnlyRecipientDefinition;
use MyParcelNL\Pdk\App\Options\Definition\ReceiptCodeDefinition;
use MyParcelNL\Pdk\App\Options\Definition\SameDayDeliveryDefinition;
use MyParcelNL\Pdk\App\Options\Definition\SignatureDefinition;
use MyParcelNL\Pdk\App\Options\Definition\TrackedDefinition;
Expand Down Expand Up @@ -127,6 +128,11 @@ public function canHavePickup(): bool
return $this->hasDeliveryType(DeliveryOptions::DELIVERY_TYPE_PICKUP_NAME);
}

public function canHaveReceiptCode(): bool
{
return $this->canHave(ReceiptCodeDefinition::class);
}

public function canHaveSameDayDelivery(): bool
{
return $this->canHave(SameDayDeliveryDefinition::class);
Expand Down
Loading

0 comments on commit 103b856

Please sign in to comment.