Skip to content

Commit

Permalink
Merge pull request #48 from Invertus/DGS-142
Browse files Browse the repository at this point in the history
Dgs 142 logic to change post code format for certain countries
  • Loading branch information
arturas-bes authored Dec 15, 2021
2 parents a4dee3a + e037ada commit 3eead0c
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.



3 changes: 3 additions & 0 deletions config/adapter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
1 change: 1 addition & 0 deletions config/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
16 changes: 16 additions & 0 deletions src/Adapter/AddressAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Address;
use Country;
use DPDBaltics;
use Invertus\dpdBaltics\Config\Config;
use Tools;

/**
Expand Down Expand Up @@ -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);
}
}
2 changes: 2 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', 'GB', 'NL'];

const PRODUCT_NAME_B2B = [
'LT' => 'Pristatymas privatiems asmenims',
'EE' => 'DPD kuller',
Expand Down
12 changes: 10 additions & 2 deletions src/Service/API/ShipmentApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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.
Expand All @@ -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,
Expand Down
55 changes: 55 additions & 0 deletions tests/Unit/Adapter/AddressAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

use Invertus\dpdBaltics\Adapter\AddressAdapter;
use PHPUnit\Framework\TestCase;

class AddressAdapterTest extends TestCase
{
/**
* @dataProvider getPostCodeData
*/
public function testFormatPostCodeByCountry($postCode, $isoCode, $expectedResult)
{

$addressAdapter = new AddressAdapter();
$result = $addressAdapter->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'
];

yield 'Post code UK' => [
'CH 65UZ',
'GB',
'CH65UZ'
];

yield 'Post code NL' => [
'1012 AB',
'NL',
'1012AB'
];
}
}

0 comments on commit 3eead0c

Please sign in to comment.