Skip to content

Commit

Permalink
feat: add package type package_small (#471)
Browse files Browse the repository at this point in the history
* feat: add package type package_small

* feat: add package type package_small

* test: fix test

* test: fix coverage

* remove var_dump

* fix: disable tracked for package_small nl

* fix

* fix

* fix: correct shipmentoptions for package small
  • Loading branch information
Mark-Ernst authored Mar 13, 2024
1 parent a6938c3 commit dd72573
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 8 deletions.
15 changes: 10 additions & 5 deletions src/Model/Consignment/AbstractConsignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,31 +126,36 @@ abstract class AbstractConsignment
public const PACKAGE_TYPE_MAILBOX = 2;
public const PACKAGE_TYPE_LETTER = 3;
public const PACKAGE_TYPE_DIGITAL_STAMP = 4;
public const PACKAGE_TYPE_PACKAGE_SMALL = 6;

public const PACKAGE_TYPE_PACKAGE_NAME = 'package';
public const PACKAGE_TYPE_MAILBOX_NAME = 'mailbox';
public const PACKAGE_TYPE_LETTER_NAME = 'letter';
public const PACKAGE_TYPE_DIGITAL_STAMP_NAME = 'digital_stamp';
public const PACKAGE_TYPE_PACKAGE_SMALL_NAME = 'package_small';

public const PACKAGE_TYPES_IDS = [
self::PACKAGE_TYPE_PACKAGE,
self::PACKAGE_TYPE_MAILBOX,
self::PACKAGE_TYPE_LETTER,
self::PACKAGE_TYPE_DIGITAL_STAMP,
self::PACKAGE_TYPE_PACKAGE_SMALL,
];

public const PACKAGE_TYPES_NAMES = [
self::PACKAGE_TYPE_PACKAGE_NAME,
self::PACKAGE_TYPE_MAILBOX_NAME,
self::PACKAGE_TYPE_LETTER_NAME,
self::PACKAGE_TYPE_DIGITAL_STAMP_NAME,
self::PACKAGE_TYPE_PACKAGE_SMALL_NAME,
];

public const PACKAGE_TYPES_NAMES_IDS_MAP = [
self::PACKAGE_TYPE_PACKAGE_NAME => self::PACKAGE_TYPE_PACKAGE,
self::PACKAGE_TYPE_MAILBOX_NAME => self::PACKAGE_TYPE_MAILBOX,
self::PACKAGE_TYPE_LETTER_NAME => self::PACKAGE_TYPE_LETTER,
self::PACKAGE_TYPE_DIGITAL_STAMP_NAME => self::PACKAGE_TYPE_DIGITAL_STAMP,
self::PACKAGE_TYPE_PACKAGE_SMALL_NAME => self::PACKAGE_TYPE_PACKAGE_SMALL,
];

public const DEFAULT_PACKAGE_TYPE = self::PACKAGE_TYPE_PACKAGE;
Expand Down Expand Up @@ -536,12 +541,12 @@ public function canHavePackageType(string $packageType): bool
*/
public function canHaveShipmentOption(string $option): bool
{
$isPackage = self::PACKAGE_TYPE_PACKAGE === $this->getPackageType();
$isPickup = self::DELIVERY_TYPE_PICKUP === $this->getDeliveryType();
$optionIsAvailable = in_array($option, $this->getAllowedShipmentOptions(), true);
$pickupAllowed = in_array($option, $this->getAllowedShipmentOptionsForPickup(), true);
$canHaveShipmentOptions = in_array($this->getPackageType(), [self::PACKAGE_TYPE_PACKAGE, self::PACKAGE_TYPE_PACKAGE_SMALL]);
$isPickup = self::DELIVERY_TYPE_PICKUP === $this->getDeliveryType();
$optionIsAvailable = in_array($option, $this->getAllowedShipmentOptions(), true);
$pickupAllowed = in_array($option, $this->getAllowedShipmentOptionsForPickup(), true);

return $isPackage && $optionIsAvailable && ($pickupAllowed || ! $isPickup);
return $canHaveShipmentOptions && $optionIsAvailable && ($pickupAllowed || ! $isPickup);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Model/Consignment/PostNLConsignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function getAllowedPackageTypes(): array
self::PACKAGE_TYPE_MAILBOX_NAME,
self::PACKAGE_TYPE_LETTER_NAME,
self::PACKAGE_TYPE_DIGITAL_STAMP_NAME,
self::PACKAGE_TYPE_PACKAGE_SMALL_NAME,
];
}

Expand Down
7 changes: 7 additions & 0 deletions src/Rule/Consignment/MaximumWeightRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class MaximumWeightRule extends Rule
* @var int
*/
public const MAX_COLLO_WEIGHT_DIGITAL_STAMP_GRAMS = 2000;
/**
* @var int
*/
public const MAX_COLLO_WEIGHT_PACKAGE_SMALL_GRAMS = 2000;

/**
* @param \MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment $validationSubject
Expand All @@ -50,6 +54,9 @@ public function validate($validationSubject): void
case AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP:
$weightLimit = self::MAX_COLLO_WEIGHT_DIGITAL_STAMP_GRAMS;
break;
case AbstractConsignment::PACKAGE_TYPE_PACKAGE_SMALL:
$weightLimit = self::MAX_COLLO_WEIGHT_PACKAGE_SMALL_GRAMS;
break;
default:
$weightLimit = null;
}
Expand Down
33 changes: 30 additions & 3 deletions src/Services/ConsignmentEncode.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

class ConsignmentEncode
{
public const DEFAULT_CURRENCY = 'EUR';
public const DEFAULT_CURRENCY = 'EUR';
private const MAX_INSURANCE_PACKETS_ROW = 5000;

/**
* @var array
Expand Down Expand Up @@ -124,6 +125,21 @@ public static function encodeExtraOptions(array $consignmentEncoded, AbstractCon
];
}

if (AbstractConsignment::PACKAGE_TYPE_PACKAGE_SMALL === $consignment->getPackageType()) {
$consignmentEncoded['options']['large_format'] = 0;

if (AbstractConsignment::CC_NL !== $consignment->getCountry()
&& self::MAX_INSURANCE_PACKETS_ROW < $consignment->getInsurance() * 100) {
$consignmentEncoded['options']['insurance']['amount'] = self::MAX_INSURANCE_PACKETS_ROW;
}

if (AbstractConsignment::CC_NL === $consignment->getCountry()) {
$consignmentEncoded['options']['tracked'] = 0;
} else {
$consignmentEncoded['options']['tracked'] = 1;
}
}

foreach ($consignment->getMandatoryShipmentOptions() as $option) {
$key = "options.$option";
$value = Arr::get($consignmentEncoded, $key);
Expand Down Expand Up @@ -271,6 +287,7 @@ private function encodeCdCountry(): self
* @var \MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment $consignment
*/
$consignment = Arr::first($this->consignments);

if ($consignment->isToEuCountry()) {
return $this;
}
Expand Down Expand Up @@ -334,8 +351,18 @@ private function validateCdConsignment(AbstractConsignment $consignment): void
throw new MissingFieldException('Product data must be set for international MyParcel shipments. Use addItem().');
}

if ($consignment->getPackageType() !== AbstractConsignment::PACKAGE_TYPE_PACKAGE && $consignment->getPackageType() !== AbstractConsignment::PACKAGE_TYPE_LETTER) {
throw new MissingFieldException('For international shipments, package_type must be 1 (normal package) or 3 (letter).');
if (! in_array(
$consignment->getPackageType(),
[
AbstractConsignment::PACKAGE_TYPE_PACKAGE,
AbstractConsignment::PACKAGE_TYPE_LETTER,
AbstractConsignment::PACKAGE_TYPE_PACKAGE_SMALL,
],
true
)) {
throw new MissingFieldException(
'For international shipments, package_type must be 1 (normal package), 3 (letter) or 6 (small package).'
);
}

if (empty($consignment->getInvoice())) {
Expand Down
7 changes: 7 additions & 0 deletions test/Bootstrap/ConsignmentTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,13 @@ protected function getDefaultAddress(string $country = AbstractConsignment::CC_N
self::POSTAL_CODE => '2000',
self::CITY => 'Antwerpen',
];
case 'DE':
return [
self::COUNTRY => 'DE',
self::FULL_STREET => 'Kurfürstendamm 195',
self::POSTAL_CODE => '10707',
self::CITY => 'Berlin',
];
case 'CA':
return [
self::COUNTRY => 'CA',
Expand Down
13 changes: 13 additions & 0 deletions test/Model/Consignment/PostNLConsignmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ public function providePostNLConsignmentsData(): array
self::expected(self::ONLY_RECIPIENT) => true,
self::expected(self::SIGNATURE) => true,
],
'Letter' => [
self::PACKAGE_TYPE => AbstractConsignment::PACKAGE_TYPE_LETTER,
self::expected(self::DELIVERY_TYPE) => AbstractConsignment::DELIVERY_TYPE_STANDARD,
],
'Small package' => $this->getDefaultAddress('DE') + [
self::PACKAGE_TYPE => AbstractConsignment::PACKAGE_TYPE_PACKAGE_SMALL,
self::expected(self::DELIVERY_TYPE) => AbstractConsignment::DELIVERY_TYPE_STANDARD,
],
'Customs declaration' => array_merge($this->getDefaultConsignmentData(), $this->getDefaultAddress('CA'), [
self::PACKAGE_TYPE => AbstractConsignment::PACKAGE_TYPE_PACKAGE,
self::CUSTOMS_DECLARATION => $this->getDefaultCustomsDeclaration(),
self::expected(self::DELIVERY_TYPE) => AbstractConsignment::DELIVERY_TYPE_STANDARD,
]),
]);
}

Expand Down

0 comments on commit dd72573

Please sign in to comment.