Skip to content

Commit

Permalink
feat: add international mailbox support when shipping to belgium (#289)
Browse files Browse the repository at this point in the history
INT-584
  • Loading branch information
FlorianSDV authored Jul 29, 2024
1 parent 5cc7860 commit ef5c714
Show file tree
Hide file tree
Showing 54 changed files with 423 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/App/Cart/Service/CartCalculationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ private function allowMailboxToCountry(?string $cc): bool
return false;
}

$countryIsUnique = $this->countryService->isUnique($cc);
$countryIsLocal = $this->countryService->isLocalCountry($cc);
$allowInternationalMailbox = Settings::all()->carrier->contains(function (CarrierSettings $carrierSettings) {
$allowInternationalMailbox = $carrierSettings->allowInternationalMailbox;
$hasDeliveryOptions = $carrierSettings->deliveryOptionsEnabled;

return $allowInternationalMailbox && $hasDeliveryOptions;
});

return $countryIsUnique || $allowInternationalMailbox;
return $countryIsLocal || $allowInternationalMailbox;
}

/**
Expand Down
17 changes: 10 additions & 7 deletions src/App/DeliveryOptions/Service/DeliveryOptionsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ private function createCarrierSettings(Carrier $carrier, PdkCart $cart, string $
? $carrierSettings['dropOffDelay']
: $cart->shippingMethod->minimumDropOffDelay;

if ($this->shouldUseInternationalMailboxPrice($packageType, $cart->shippingMethod->shippingAddress->cc)) {
$cc = $cart->shippingMethod->shippingAddress->cc ?? null;
if (
$cc
&& $this->shouldUseInternationalMailboxPrice($packageType, $cc)) {
$carrierSettings->pricePackageTypeMailbox = $carrierSettings->priceInternationalMailbox;
}

Expand Down Expand Up @@ -250,16 +253,16 @@ function (Carrier $carrier) use ($cart, $weight, $packageType, $carrierSettings)
}

/**
* @param string $packageType
* @param null|string $cc
* @param string $packageType
* @param string $cc
*
* @return bool
*/
private function shouldUseInternationalMailboxPrice(string $packageType, ?string $cc): bool
private function shouldUseInternationalMailboxPrice(string $packageType, string $cc): bool
{
$isMailbox = $packageType === DeliveryOptions::PACKAGE_TYPE_MAILBOX_NAME;
$isNotUnique = $cc && ! $this->countryService->isUnique($cc);
$isMailbox = $packageType === DeliveryOptions::PACKAGE_TYPE_MAILBOX_NAME;
$isNotLocal = ! $this->countryService->isLocalCountry($cc);

return $isMailbox && $isNotUnique;
return $isMailbox && $isNotLocal;
}
}
6 changes: 4 additions & 2 deletions src/App/Order/Calculator/General/PackageTypeCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ private function isInternationalMailbox(Carrier $carrier): bool
$schema->setCarrier($carrier);

$isMailbox = $this->order->deliveryOptions->packageType === DeliveryOptions::PACKAGE_TYPE_MAILBOX_NAME;
$isNotUnique = ! $this->countryService->isUnique($this->order->shippingAddress->cc);
$isNotLocal = ! $this->countryService->isLocalCountry(
$this->order->shippingAddress->cc
);
$enabledInAccount = AccountSettings::hasCarrierSmallPackageContract();
$canHaveCarrierSmallPackageContract = $schema->canHaveCarrierSmallPackageContract();
$enabledInSettings = $carrierSettings->allowInternationalMailbox;

return $isMailbox && $isNotUnique && $enabledInAccount && $canHaveCarrierSmallPackageContract && $enabledInSettings;
return $isMailbox && $isNotLocal && $enabledInAccount && $canHaveCarrierSmallPackageContract && $enabledInSettings;
}
}
101 changes: 80 additions & 21 deletions tests/Unit/App/Action/Backend/Order/ExportOrderActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace MyParcelNL\Pdk\App\Action\Backend\Order;

use MyParcelNL\Pdk\Account\Model\AccountGeneralSettings;
use MyParcelNL\Pdk\App\Api\Backend\PdkBackendActions;
use MyParcelNL\Pdk\App\Order\Collection\PdkOrderCollection;
use MyParcelNL\Pdk\App\Order\Collection\PdkOrderCollectionFactory;
Expand All @@ -13,6 +14,7 @@
use MyParcelNL\Pdk\Base\Support\Arr;
use MyParcelNL\Pdk\Base\Support\Collection;
use MyParcelNL\Pdk\Carrier\Model\Carrier;
use MyParcelNL\Pdk\Carrier\Model\CarrierCapabilities;
use MyParcelNL\Pdk\Facade\Actions;
use MyParcelNL\Pdk\Facade\Notifications;
use MyParcelNL\Pdk\Facade\Pdk;
Expand All @@ -35,6 +37,7 @@
use MyParcelNL\Pdk\Tests\Uses\UsesMockPdkInstance;
use MyParcelNL\Pdk\Tests\Uses\UsesNotificationsMock;
use MyParcelNL\Pdk\Tests\Uses\UsesSettingsMock;
use MyParcelNL\Pdk\Validation\Validator\CarrierSchema;
use Symfony\Component\HttpFoundation\Response;
use function MyParcelNL\Pdk\Tests\factory;
use function MyParcelNL\Pdk\Tests\usesShared;
Expand Down Expand Up @@ -284,35 +287,54 @@
->and(Arr::pluck($responseShipments[0], 'id'))->each->toBeInt();
});

it('exports international orders', function (PdkOrderCollectionFactory $factory, bool $orderMode) {
MockApi::enqueue(
...$orderMode
? [new ExamplePostOrdersResponse(), new ExamplePostOrderNotesResponse()]
: [new ExamplePostShipmentsResponse()]
);
it(
'exports international orders',
function (
PdkOrderCollectionFactory $factory,
bool $accountHasCarrierSmallPackageContract,
bool $carrierHasInternationalMailboxAllowed,
bool $orderMode
) {
MockApi::enqueue(
...$orderMode
? [new ExamplePostOrdersResponse(), new ExamplePostOrderNotesResponse()]
: [new ExamplePostShipmentsResponse()]
);

factory(OrderSettings::class)
->withConceptShipments(true)
->store();
$collection = $factory
->store()
->make();
$fakeCarrier = $collection->first()->deliveryOptions->carrier;

$collection = $factory
->store()
->make();
factory(CarrierSettings::class, $fakeCarrier->externalIdentifier)
->withAllowInternationalMailbox($carrierHasInternationalMailboxAllowed)
->store();

$orderIds = $collection->pluck('externalIdentifier');
factory(OrderSettings::class)
->withConceptShipments(true)
->store();

Actions::execute(PdkBackendActions::EXPORT_ORDERS, ['orderIds' => $orderIds->toArray()]);
factory(AccountGeneralSettings::class)
->withHasCarrierSmallPackageContract($accountHasCarrierSmallPackageContract)
->store();

$lastRequest = MockApi::ensureLastRequest();
$stream = $lastRequest->getBody();
$orderIds = $collection->pluck('externalIdentifier');

assertMatchesJsonSnapshot($stream->getContents());
})
Actions::execute(PdkBackendActions::EXPORT_ORDERS, ['orderIds' => $orderIds->toArray()]);

$lastRequest = MockApi::ensureLastRequest();
$stream = $lastRequest->getBody();

assertMatchesJsonSnapshot($stream->getContents());
}
)
->with([
'without customs declaration' => [
function () {
return factory(PdkOrderCollection::class)->push(factory(PdkOrder::class)->toTheUnitedStates());
},
'accountHasCarrierSmallPackageContract' => false,
'carrierHasInternationalMailboxAllowed' => false,
],

'with customs declaration (deprecated)' => [
Expand All @@ -338,6 +360,35 @@ function () {
)
);
},
'accountHasCarrierSmallPackageContract' => false,
'carrierHasInternationalMailboxAllowed' => false,
],

'custom postnl with international mailbox to Belgium' => [
function () {
return factory(PdkOrderCollection::class)->push(
factory(PdkOrder::class)
->toBelgium()
->withDeliveryOptions(
factory(DeliveryOptions::class)
->withCarrier(
factory(Carrier::class)
->fromPostNL()
->withContractId(123456)
->withCapabilities(
factory(CarrierCapabilities::class)
->withFeatures(
['carrierSmallPackageContract' => CarrierSchema::FEATURE_CUSTOM_CONTRACT_ONLY]
)
->withPackageTypes([DeliveryOptions::PACKAGE_TYPE_MAILBOX_NAME])
)
)
->withPackageType(DeliveryOptions::PACKAGE_TYPE_MAILBOX_NAME)
)
);
},
'accountHasCarrierSmallPackageContract' => true,
'carrierHasInternationalMailboxAllowed' => true,
],

'custom postnl with international mailbox' => [
Expand All @@ -351,14 +402,20 @@ function () {
factory(Carrier::class)
->fromPostNL()
->withContractId(123456)
->withCapabilities([
'internationalMailbox' => true,
])
->withCapabilities(
factory(CarrierCapabilities::class)
->withFeatures(
['carrierSmallPackageContract' => CarrierSchema::FEATURE_CUSTOM_CONTRACT_ONLY]
)
->withPackageTypes([DeliveryOptions::PACKAGE_TYPE_MAILBOX_NAME])
)
)
->withPackageType(DeliveryOptions::PACKAGE_TYPE_MAILBOX_NAME)
)
);
},
'accountHasCarrierSmallPackageContract' => true,
'carrierHasInternationalMailboxAllowed' => true,
],

'postnl with international mailbox filtered out' => [
Expand All @@ -373,6 +430,8 @@ function () {
)
);
},
'accountHasCarrierSmallPackageContract' => true,
'carrierHasInternationalMailboxAllowed' => true,
],
])
->with('orderModeToggle');
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/App/Cart/Service/CartCalculationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use MyParcelNL\Pdk\App\Cart\Contract\CartCalculationServiceInterface;
use MyParcelNL\Pdk\App\Cart\Model\PdkCart;
use MyParcelNL\Pdk\Base\Service\CountryCodes;
use MyParcelNL\Pdk\Base\Support\Arr;
use MyParcelNL\Pdk\Carrier\Model\Carrier;
use MyParcelNL\Pdk\Facade\Pdk;
Expand Down Expand Up @@ -139,6 +140,16 @@
'region' => 'Drenthe',
'state' => 'Current',
];
const SHIPPING_ADDRESS_BE = [
'address1' => 'Adriaan Brouwerstraat 16',
'address2' => 'Appartement B',
'area' => 'Voor',
'cc' => CountryCodes::CC_BE,
'city' => 'Antwerpen',
'postalCode' => '1000',
'region' => 'Antwerpen',
'state' => 'Current',
];

uses()->group('checkout');
usesShared(new UsesMockPdkInstance());
Expand Down Expand Up @@ -195,6 +206,20 @@ function (array $lines, array $shippingAddress, array $result, bool $allowIntern
'address' => SHIPPING_ADDRESS_NL,
'result' => [DeliveryOptions::PACKAGE_TYPE_MAILBOX_NAME, DeliveryOptions::PACKAGE_TYPE_PACKAGE_NAME],
],
'fits in mailbox BE, allowed' => [
'lines' => LINES_FITS_IN_MAILBOX,
'address' => SHIPPING_ADDRESS_BE,
'result' => [
DeliveryOptions::PACKAGE_TYPE_MAILBOX_NAME,
DeliveryOptions::PACKAGE_TYPE_PACKAGE_NAME,
],
'allowInternationalMailbox' => true,
],
'fits in mailbox BE, not allowed' => [
'lines' => LINES_FITS_IN_MAILBOX,
'address' => SHIPPING_ADDRESS_BE,
'result' => [DeliveryOptions::PACKAGE_TYPE_PACKAGE_NAME],
],
'fits in mailbox EU, allowed' => [
'lines' => LINES_FITS_IN_MAILBOX,
'address' => SHIPPING_ADDRESS_EU,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,41 @@ function (
},
],

'custom postnl: be mailbox package' => [
'cart' => [
'carrier' => [
'externalIdentifier' => 'postnl:123',
],
'shippingMethod' => [
'shippingAddress' => ['cc' => 'BE'],
'allowedPackageTypes' => ['mailbox'],
],
'lines' => [
[
'quantity' => 1,
'product' => [
'weight' => 500,
'isDeliverable' => true,
'settings' => [
ProductSettings::FIT_IN_MAILBOX => 5,
ProductSettings::PACKAGE_TYPE => DeliveryOptions::PACKAGE_TYPE_MAILBOX_NAME,
],
],
],
],
],
'carrierFactory' => function () {
return factory(Carrier::class)->withExternalIdentifier('postnl:123');
},
'carrierSettingsFactory' => function () {
return function (CarrierSettingsFactory $factory) {
return $factory
->withAllowInternationalMailbox(true)
->withPriceInternationalMailbox(5);
};
},
],

'custom postnl: row mailbox package' => [
'cart' => [
'carrier' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@
];

const ACCOUNT_FLAG_ON_CARRIER_SETTING_ON = [
'accountFlag' => true,
'carrierSetting' => true,
'accountHasCarrierSmallPackageContract' => true,
'carrierHasInternationalMailboxAllowed' => true,
];

const ACCOUNT_FLAG_ON_CARRIER_SETTING_OFF = [
'accountFlag' => true,
'carrierSetting' => false,
'accountHasCarrierSmallPackageContract' => true,
'carrierHasInternationalMailboxAllowed' => false,
];

const ACCOUNT_FLAG_OFF_CARRIER_SETTING_ON = [
'accountFlag' => false,
'carrierSetting' => true,
'accountHasCarrierSmallPackageContract' => false,
'carrierHasInternationalMailboxAllowed' => true,
];

const ACCOUNT_FLAG_OFF_CARRIER_SETTING_OFF = [
'accountFlag' => false,
'carrierSetting' => false,
'accountHasCarrierSmallPackageContract' => false,
'carrierHasInternationalMailboxAllowed' => false,
];

const CONFIG = [
Expand All @@ -71,6 +71,7 @@
const DESTINATION_INTERNATIONAL_COUNTRIES = [
CountryCodes::CC_FR,
CountryCodes::CC_US,
CountryCodes::CC_BE,
];

usesShared(new UsesEachMockPdkInstance());
Expand Down Expand Up @@ -157,8 +158,8 @@
$country,
$carrierExternalIdentifier,
$carrierName,
$accountFlag,
$carrierSetting
$accountHasCarrierSmallPackageContract,
$carrierHasInternationalMailboxAllowed
) {
mockPlatform($platform);
mockPdkProperties([
Expand All @@ -173,7 +174,7 @@
->make();

factory(CarrierSettings::class, $fakeCarrier->externalIdentifier)
->withAllowInternationalMailbox($carrierSetting)
->withAllowInternationalMailbox($carrierHasInternationalMailboxAllowed)
->store();

$order = factory(PdkOrder::class)
Expand All @@ -189,7 +190,7 @@
->make();

factory(AccountGeneralSettings::class)
->withHasCarrierSmallPackageContract($accountFlag)
->withHasCarrierSmallPackageContract($accountHasCarrierSmallPackageContract)
->store();

/** @var \MyParcelNL\Pdk\App\Order\Contract\PdkOrderOptionsServiceInterface $service */
Expand Down
Loading

0 comments on commit ef5c714

Please sign in to comment.