From 42eded714ffa3bc1395a48972e837ed36f1b252f Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Wed, 11 Dec 2024 13:19:21 +0200 Subject: [PATCH 1/2] [DGS-319] initialize revert --- config/config.yml | 1 + config/verification.yml | 12 +++++ src/Entity/DPDPriceRule.php | 10 ++-- src/Repository/ZoneRangeRepository.php | 26 ++++++++++ src/Verification/IsAddressInRange.php | 54 +++++++++++++++++++ src/Verification/IsAddressInZone.php | 72 ++++++++++++++++++++++++++ 6 files changed, 172 insertions(+), 3 deletions(-) create mode 100644 config/verification.yml create mode 100644 src/Verification/IsAddressInRange.php create mode 100644 src/Verification/IsAddressInZone.php diff --git a/config/config.yml b/config/config.yml index 0f0eb6a3..8953dfef 100644 --- a/config/config.yml +++ b/config/config.yml @@ -16,6 +16,7 @@ imports: - { resource: command.yml } - { resource: presenter.yml } - { resource: templateRender.yml } + - { resource: verification.yml } services: _defaults: diff --git a/config/verification.yml b/config/verification.yml new file mode 100644 index 00000000..fa1f7c2a --- /dev/null +++ b/config/verification.yml @@ -0,0 +1,12 @@ +services: + _defaults: + public: true + + invertus.dpdbaltics.verification.is_address_in_zone: + class: 'Invertus\dpdBaltics\Verification\IsAddressInZone' + arguments: + - '@invertus.dpdbaltics.repository.zone_range_repository' + - '@invertus.dpdbaltics.verification.is_address_in_range' + + invertus.dpdbaltics.verification.is_address_in_range: + class: 'Invertus\dpdBaltics\Verification\IsAddressInRange' \ No newline at end of file diff --git a/src/Entity/DPDPriceRule.php b/src/Entity/DPDPriceRule.php index 889354ad..7bf39503 100644 --- a/src/Entity/DPDPriceRule.php +++ b/src/Entity/DPDPriceRule.php @@ -257,10 +257,14 @@ public function isApplicableByAddress(Address $address) /** @var DPDBaltics $module */ $module = Module::getInstanceByName('dpdbaltics'); - /** @var ZoneRepository $zoneRepo */ - $zoneRepo = $module->getModuleContainer()->get('invertus.dpdbaltics.repository.zone_repository'); + /** @var \Invertus\dpdBaltics\Verification\IsAddressInZone $isAddressInZone */ + $isAddressInZone = $module->getModuleContainer('invertus.dpdbaltics.verification.is_address_in_zone'); - return $zoneRepo->findZoneInRangeByAddress($address); + /** @var DPDZoneRepository $repo */ + $repo = $module->getModuleContainer()->get('invertus.dpdbaltics.repository.dpdzone_repository'); + $zonesIds = $repo->getZonesIdsByPriceRule($this->id); + + return $isAddressInZone->verify($address, $zonesIds); } public function isApplicableByPrice($price, $idCurrency) diff --git a/src/Repository/ZoneRangeRepository.php b/src/Repository/ZoneRangeRepository.php index 75a7c01c..4ac05ce9 100644 --- a/src/Repository/ZoneRangeRepository.php +++ b/src/Repository/ZoneRangeRepository.php @@ -50,4 +50,30 @@ public function findAllZoneRangeCountryIds() FROM `' . _DB_PREFIX_ . 'dpd_zone_range` '); } + + public function findBy(array $criteria, $limit = null, array $notEqualsCriteria = []) + { + $query = new DbQuery(); + $query->select( + 'dzr.id_dpd_zone_range, dzr.id_dpd_zone, dzr.id_country, dzr.include_all_zip_codes' + ); + $query->select('dzr.zip_code_from, dzr.zip_code_to'); + $query->from('dpd_zone_range', 'dzr'); + + foreach ($criteria as $field => $value) { + $query->where('dzr.'.bqSQL($field).' = "'.pSQL($value).'"'); + } + + foreach ($notEqualsCriteria as $field => $value) { + $query->where('dzr.'.bqSQL($field).' != "'.pSQL($value).'"'); + } + + if ($limit) { + $query->limit((int) $limit); + } + + $result = $this->db->executeS($query); + + return $result ? $result : []; + } } diff --git a/src/Verification/IsAddressInRange.php b/src/Verification/IsAddressInRange.php new file mode 100644 index 00000000..ee0cd44d --- /dev/null +++ b/src/Verification/IsAddressInRange.php @@ -0,0 +1,54 @@ + + * @copyright Copyright (c) permanent, INVERTUS, UAB + * @license Addons PrestaShop license limitation + * @see /LICENSE + * + * International Registered Trademark & Property of INVERTUS, UAB + */ + +namespace Invertus\dpdBaltics\Verification; + +use Invertus\dpdBaltics\Repository\ZoneRangeRepository; +use Invertus\dpdBaltics\Validate\Zone\ZoneRangeValidate; + +class IsAddressInRange +{ + /** + * Check if address falls into given zones + * + * @param \Address $address + * @param array $zones + * + * @return bool + * @throws \PrestaShopDatabaseException + * @throws \PrestaShopException + */ + public function verify(\Address $address, array $range) + { + $idCountry = $address->id_country ?: (int)\Configuration::get('PS_COUNTRY_DEFAULT'); + + //NOTE: if countries do not match, we continue. + if ((int) $idCountry !== (int) $address->id_country) { + return false; + } + + if ($range['include_all_zip_codes']) { + return true; + } + + if (ZoneRangeValidate::isZipCodeInRange( + $address->postcode, + $range['zip_code_from'], + $range['zip_code_to'], + $idCountry + )) { + return true; + } + + return false; + } +} \ No newline at end of file diff --git a/src/Verification/IsAddressInZone.php b/src/Verification/IsAddressInZone.php new file mode 100644 index 00000000..fddacb02 --- /dev/null +++ b/src/Verification/IsAddressInZone.php @@ -0,0 +1,72 @@ + + * @copyright Copyright (c) permanent, INVERTUS, UAB + * @license Addons PrestaShop license limitation + * @see /LICENSE + * + * International Registered Trademark & Property of INVERTUS, UAB + */ + +namespace Invertus\dpdBaltics\Verification; + +use Invertus\dpdBaltics\Repository\ZoneRangeRepository; +use Invertus\dpdBaltics\Validate\Zone\ZoneRangeValidate; + +class IsAddressInZone +{ + /** + * @var ZoneRangeRepository + */ + private $zoneRangeRepository; + /** + * @var IsAddressInRange + */ + private $isAddressInRange; + + public function __construct( + ZoneRangeRepository $zoneRangeRepository, + IsAddressInRange $isAddressInRange + ) { + $this->zoneRangeRepository = $zoneRangeRepository; + $this->isAddressInRange = $isAddressInRange; + } + + /** + * Check if address falls into given zones + * + * @param \Address $address + * @param array $zones + * + * @return bool + * @throws \PrestaShopDatabaseException + * @throws \PrestaShopException + */ + public function verify(\Address $address, array $zones) + { + $idCountry = $address->id_country ?: (int)\Configuration::get('PS_COUNTRY_DEFAULT'); + + foreach ($zones as $zone) { + // Get ranges by zone and country + $ranges = $this->zoneRangeRepository->findBy([ + 'id_dpd_zone' => $zone['id'], + // Check by country as well, because the zone must match the country of address + 'id_country' => $idCountry, + ]); + + if (empty($ranges)) { + continue; + } + + foreach ($ranges as $range) { + if ($this->isAddressInRange->verify($address, $range)) { + return true; + } + } + } + + return false; + } +} \ No newline at end of file From f172d00d81bf27269bfbf571b1934045ea35c0de Mon Sep 17 00:00:00 2001 From: MarijusCoding Date: Wed, 11 Dec 2024 11:20:08 +0000 Subject: [PATCH 2/2] Adding auto indexes --- src/Verification/index.php | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/Verification/index.php diff --git a/src/Verification/index.php b/src/Verification/index.php new file mode 100644 index 00000000..15aba820 --- /dev/null +++ b/src/Verification/index.php @@ -0,0 +1,11 @@ +