Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add package type package_small #471

Merged
merged 9 commits into from
Mar 13, 2024
Merged
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
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 @@
];
}

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;

Check warning on line 133 in src/Services/ConsignmentEncode.php

View check run for this annotation

Codecov / codecov/patch

src/Services/ConsignmentEncode.php#L133

Added line #L133 was not covered by tests
}

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

Check warning on line 137 in src/Services/ConsignmentEncode.php

View check run for this annotation

Codecov / codecov/patch

src/Services/ConsignmentEncode.php#L137

Added line #L137 was not covered by tests
} 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 @@
* @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 @@
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).'

Check warning on line 364 in src/Services/ConsignmentEncode.php

View check run for this annotation

Codecov / codecov/patch

src/Services/ConsignmentEncode.php#L363-L364

Added lines #L363 - L364 were not covered by tests
);
}

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
Loading