From 35a83026fe8c2b2eb52db296b1fc02304721681b Mon Sep 17 00:00:00 2001 From: Arturas Date: Tue, 14 Dec 2021 18:23:54 +0200 Subject: [PATCH 1/3] logic to change post code formating on certain countries --- config/adapter.yml | 3 +++ config/service.yml | 1 + src/Adapter/AddressAdapter.php | 16 ++++++++++++++++ src/Config/Config.php | 2 ++ src/Service/API/ShipmentApiService.php | 12 ++++++++++-- 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/config/adapter.yml b/config/adapter.yml index bd891f23..a501d930 100644 --- a/config/adapter.yml +++ b/config/adapter.yml @@ -7,3 +7,6 @@ services: invertus.dpdbaltics.adapter.link_adapter: class: 'Invertus\dpdBaltics\Adapter\LinkAdapter' + + invertus.dpdbaltics.adapter.address_adapter: + class: 'Invertus\dpdBaltics\Adapter\AddressAdapter' diff --git a/config/service.yml b/config/service.yml index badb368e..7da5f643 100644 --- a/config/service.yml +++ b/config/service.yml @@ -158,6 +158,7 @@ services: - '@invertus.dpdbaltics.repository.cod_payment_repository' - '@invertus.dpdbaltics.repository.parcel_tracking_email_handler' - '@invertus.dpdbaltics.service.parcel.parcel_shop_service' + - '@invertus.dpdbaltics.adapter.address_adapter' invertus.dpdbaltics.service.api.label_api_service: class: 'Invertus\dpdBaltics\Service\API\LabelApiService' diff --git a/src/Adapter/AddressAdapter.php b/src/Adapter/AddressAdapter.php index fe25b18d..6c8f47ea 100644 --- a/src/Adapter/AddressAdapter.php +++ b/src/Adapter/AddressAdapter.php @@ -15,6 +15,7 @@ use Address; use Country; use DPDBaltics; +use Invertus\dpdBaltics\Config\Config; use Tools; /** @@ -134,4 +135,19 @@ public function getFormattedZipCodePudoToPrestashop($iso, $zipCode) return $formattedZipCode; } + + /** + * @param $postCode + * @param $countryIsoCode + * + * @return array|string|string[]|null + */ + public function formatPostCodeByCountry($postCode, $countryIsoCode) + { + if (in_array($countryIsoCode, Config::COUNTRY_ISO_CODES_WITH_MIXED_CHARACTERS, true)) { + return str_replace(' ', '', $postCode); + } + + return preg_replace('/[^0-9]/', '', $postCode); + } } diff --git a/src/Config/Config.php b/src/Config/Config.php index 8e1e3c5d..a6721800 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -230,6 +230,8 @@ class Config const PRODUCT_TYPE_SAME_DAY_DELIVERY = '274'; const PS_VERSION_1_7_7 = '1.7.7.0'; + const COUNTRY_ISO_CODES_WITH_MIXED_CHARACTERS = ['IE']; + const PRODUCT_NAME_B2B = [ 'LT' => 'Pristatymas privatiems asmenims', 'EE' => 'DPD kuller', diff --git a/src/Service/API/ShipmentApiService.php b/src/Service/API/ShipmentApiService.php index 665ce378..368b4683 100644 --- a/src/Service/API/ShipmentApiService.php +++ b/src/Service/API/ShipmentApiService.php @@ -6,6 +6,7 @@ use Country; use DPDAddressTemplate; use DPDProduct; +use Invertus\dpdBaltics\Adapter\AddressAdapter; use Invertus\dpdBaltics\Config\Config; use Invertus\dpdBaltics\DTO\ShipmentData; use Invertus\dpdBaltics\Repository\CodPaymentRepository; @@ -33,17 +34,23 @@ class ShipmentApiService * @var ParcelShopService */ private $parcelShopService; + /** + * @var AddressAdapter + */ + private $addressAdapter; public function __construct( ShipmentCreationFactory $shipmentCreationFactory, CodPaymentRepository $codPaymentRepository, ParcelTrackingEmailHandler $emailHandler, - ParcelShopService $parcelShopService + ParcelShopService $parcelShopService, + AddressAdapter $addressAdapter ) { $this->shipmentCreationFactory = $shipmentCreationFactory; $this->codPaymentRepository = $codPaymentRepository; $this->emailHandler = $emailHandler; $this->parcelShopService = $parcelShopService; + $this->addressAdapter = $addressAdapter; } /** @@ -64,7 +71,6 @@ public function createShipment($addressId, ShipmentData $shipmentData, $orderId) $dpdProduct = new DPDProduct($shipmentData->getProduct()); $parcelType = $dpdProduct->getProductReference(); $country = Country::getIsoById($address->id_country); - $postCode = preg_replace('/[^0-9]/', '', $address->postcode); $hasAddressFields = (bool) !$postCode || !$firstName || !$address->city || !$country; // IF prestashop allows, we take selected parcel terminal address in case information is missing in checkout address in specific cases. @@ -78,6 +84,8 @@ public function createShipment($addressId, ShipmentData $shipmentData, $orderId) $country = $selectedParcel->getCountry(); } + $postCode = $this->addressAdapter->formatPostCodeByCountry($postCode, $country); + $shipmentCreationRequest = new ShipmentCreationRequest( $firstName, $address->address1, From 3289f7e3f1c71ed953c933217f98d207e4694587 Mon Sep 17 00:00:00 2001 From: Arturas Date: Wed, 15 Dec 2021 10:41:21 +0200 Subject: [PATCH 2/3] add unit test --- tests/Unit/Adapter/AddressAdapterTest.php | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/Unit/Adapter/AddressAdapterTest.php diff --git a/tests/Unit/Adapter/AddressAdapterTest.php b/tests/Unit/Adapter/AddressAdapterTest.php new file mode 100644 index 00000000..960961c7 --- /dev/null +++ b/tests/Unit/Adapter/AddressAdapterTest.php @@ -0,0 +1,43 @@ +formatPostCodeByCountry($postCode, $isoCode); + + $this->assertEquals($expectedResult, $result); + } + + public function getPostCodeData() + { + yield 'Post code Lithuania' => [ + 'LT56120', + 'LT', + '56120' + ]; + yield 'Post code Ireland' => [ + 'V42 A393', + 'IE', + 'V42A393' + ]; + yield 'Post code Estonia' => [ + '10EE14A9', + 'EE', + '10149' + ]; + yield 'Post code Latvia' => [ + 'LV-1050', + 'LV', + '1050' + ]; + } +} \ No newline at end of file From e037ada43041e627d11cebe35f709a2404376c45 Mon Sep 17 00:00:00 2001 From: Arturas Date: Wed, 15 Dec 2021 11:08:33 +0200 Subject: [PATCH 3/3] Compatability for shipments from: GB,IE,NL added, service for different post code formatting initiated --- CHANGELOG.md | 1 + src/Config/Config.php | 2 +- tests/Unit/Adapter/AddressAdapterTest.php | 12 ++++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 832a3448..6986e656 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -102,6 +102,7 @@ - Logic to execute code further if error occurs on tracking email send. - Bugfix when parcel terminal form is always visible in order - Translatable string added when no option available, translations updated for all baltic languages for pickup select. +- Compatability for shipments from: GB,IE,NL added, service for different post code formatting initiated. diff --git a/src/Config/Config.php b/src/Config/Config.php index a6721800..02af3fef 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -230,7 +230,7 @@ class Config const PRODUCT_TYPE_SAME_DAY_DELIVERY = '274'; const PS_VERSION_1_7_7 = '1.7.7.0'; - const COUNTRY_ISO_CODES_WITH_MIXED_CHARACTERS = ['IE']; + const COUNTRY_ISO_CODES_WITH_MIXED_CHARACTERS = ['IE', 'GB', 'NL']; const PRODUCT_NAME_B2B = [ 'LT' => 'Pristatymas privatiems asmenims', diff --git a/tests/Unit/Adapter/AddressAdapterTest.php b/tests/Unit/Adapter/AddressAdapterTest.php index 960961c7..56f00af8 100644 --- a/tests/Unit/Adapter/AddressAdapterTest.php +++ b/tests/Unit/Adapter/AddressAdapterTest.php @@ -39,5 +39,17 @@ public function getPostCodeData() 'LV', '1050' ]; + + yield 'Post code UK' => [ + 'CH 65UZ', + 'GB', + 'CH65UZ' + ]; + + yield 'Post code NL' => [ + '1012 AB', + 'NL', + '1012AB' + ]; } } \ No newline at end of file