diff --git a/dpdbaltics.php b/dpdbaltics.php index e22b3855..893deca8 100644 --- a/dpdbaltics.php +++ b/dpdbaltics.php @@ -464,13 +464,18 @@ public function getOrderShippingCostExternal($cart) $currentCountryProvider = $this->getModuleContainer('invertus.dpdbaltics.provider.current_country_provider'); $deliveryAddress = new Address($cart->id_address_delivery); + $carrier = new Carrier($this->id_carrier); + +// if (empty($zoneRepository->findZoneInRangeByAddress($deliveryAddress))) { +// return false; +// } + + $availableDpdProductsByZone = $zoneRepository->findProductsInZoneRange($deliveryAddress, $carrier->id_reference); - if (empty($zoneRepository->findZoneInRangeByAddress($deliveryAddress))) { + if (empty($availableDpdProductsByZone)) { return false; } - $carrier = new Carrier($this->id_carrier); - if (!$productAvailabilityService->checkIfCarrierIsAvailable($carrier->id_reference)) { return false; } diff --git a/src/Repository/ZoneRepository.php b/src/Repository/ZoneRepository.php index a26f3d77..9bf2ba95 100644 --- a/src/Repository/ZoneRepository.php +++ b/src/Repository/ZoneRepository.php @@ -114,4 +114,46 @@ public function findZoneInRangeByAddress(Address $address) return $result ?: []; } + + public function findProductsInZoneRange(Address $address, string $carrierReference) + { + $idCountry = $address->id_country ?: (int)\Configuration::get('PS_COUNTRY_DEFAULT'); + $zipCode = $address->postcode; + + $query = new DbQuery(); + + $query->select('dp.*'); + $query->from('dpd_product', 'dp'); + $query->where('dp.id_reference = ' . pSql($carrierReference)); + $query->where('dp.active = ' . (int) 1); + $query->where('dp.all_zones = ' . (int) 1); + + $result = $this->db->executeS($query); + + if (!empty($result)) { + return $result; + } + + $query = new DbQuery(); + $query->select('dp.*'); + $query->from('dpd_zone', 'dz'); + $query->leftJoin('dpd_zone_range', 'dzr', 'dzr.id_dpd_zone = dz.id_dpd_zone'); + $query->leftJoin('dpd_product_zone', 'dpz', 'dpz.id_dpd_zone = dz.id_dpd_zone'); + $query->leftJoin('dpd_product', 'dp', 'dp.id_dpd_product = dpz.id_dpd_product'); + + $query->where('dp.active = ' . (int) 1); + $query->where('dzr.id_country = ' . (int)$idCountry); + $query->where('dzr.include_all_zip_codes = 1 OR (dzr.zip_code_from_numeric <= \'' . pSQL($zipCode) . '\' AND dzr.zip_code_to_numeric >= \'' . pSQL($zipCode) . '\')'); + + $productsIdReferences = $this->db->executeS($query); + $result = []; + + foreach($productsIdReferences as $product) { + if ($product['id_reference'] === $carrierReference) { + $result[] = $product['id_reference']; + } + } + + return $result ?: []; + } } diff --git a/src/Service/ShippingPriceCalculationService.php b/src/Service/ShippingPriceCalculationService.php index 1bbc8e3f..7151814e 100644 --- a/src/Service/ShippingPriceCalculationService.php +++ b/src/Service/ShippingPriceCalculationService.php @@ -53,6 +53,9 @@ public function __construct( public function calculate(Cart $cart, \Carrier $carrier, Address $deliveryAddress) { $shippingCosts = 0.0; + //todo price rules do not exist if setting is all price rule exist + // if prz all zones = 1 + // check if carrier set to all zones if yes we take price rule of the all_zones $priceRulesIds = $this->priceRuleRepository->getByCarrierReference( $deliveryAddress,