Skip to content

Commit

Permalink
Merge pull request #139 from Invertus/DGS-319/revert-changes-zones-fa…
Browse files Browse the repository at this point in the history
…ilure

DGS-319 Revert changes that broke zones logic
  • Loading branch information
MarijusCoding authored Dec 17, 2024
2 parents e9290a5 + f172d00 commit 2740a95
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 3 deletions.
1 change: 1 addition & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ imports:
- { resource: command.yml }
- { resource: presenter.yml }
- { resource: templateRender.yml }
- { resource: verification.yml }

services:
_defaults:
Expand Down
12 changes: 12 additions & 0 deletions config/verification.yml
Original file line number Diff line number Diff line change
@@ -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'
10 changes: 7 additions & 3 deletions src/Entity/DPDPriceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions src/Repository/ZoneRangeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 : [];
}
}
54 changes: 54 additions & 0 deletions src/Verification/IsAddressInRange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* NOTICE OF LICENSE
*
* @author INVERTUS, UAB www.invertus.eu <[email protected]>
* @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;
}
}
72 changes: 72 additions & 0 deletions src/Verification/IsAddressInZone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* NOTICE OF LICENSE
*
* @author INVERTUS, UAB www.invertus.eu <[email protected]>
* @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;
}
}
11 changes: 11 additions & 0 deletions src/Verification/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

header("Location: ../");
exit;

0 comments on commit 2740a95

Please sign in to comment.