Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DGS-18 init functionality for same day delivery lithuania #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,17 @@ public function setMedia($isNewTheme = false)
*/
public function getList($idLlang, $orderBy = null, $orderWay = null, $start = 0, $limit = null, $idLangShop = false)
{
/** @var \Invertus\dpdBaltics\Provider\CurrentCountryProvider $currentCountryProvider */
$currentCountryProvider = $this->module->getModuleContainer(\Invertus\dpdBaltics\Provider\CurrentCountryProvider::class);
$countryCode = $currentCountryProvider->getCurrentCountryIsoCode();

$sameDayDeliveryType = Config::getSameDeliveryDayParcelTypeByCountryCode($countryCode);

$this->_select = 'p.`day`, p.`interval_start`, p.`interval_end`';

$this->_join = 'LEFT JOIN `'._DB_PREFIX_.'dpd_product_availability` p ON p.`product_reference`= a.`product_reference`';

$this->_where = ' AND (a.`product_reference` = "' . Config::PRODUCT_TYPE_SAME_DAY_DELIVERY .
$this->_where = ' AND (a.`product_reference` = "' . $sameDayDeliveryType .
'" OR a.`product_reference` = "' . Config::PRODUCT_TYPE_SATURDAY_DELIVERY . '")';

$this->_group = 'GROUP BY a.product_reference';
Expand Down
10 changes: 2 additions & 8 deletions controllers/front/Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,11 @@ public function postProcess()

private function searchPudoServices($countryCode, $city, $carrierId, $cartId, $street = '')
{
$carrier = new Carrier($carrierId);
/** @var ParcelShopService $parcelShopService */

//TODO check if this function work correctly as some code is removed
/** @var ProductRepository $productRepo */
$parcelShopService = $this->module->getModuleContainer(ParcelShopService::class);
$productRepo = $this->module->getModuleContainer(ProductRepository::class);

$product = $productRepo->findProductByCarrierReference($carrier->id_reference);
$isSameDayDelivery = ($product['product_reference'] === Config::PRODUCT_TYPE_SAME_DAY_DELIVERY);
if ($isSameDayDelivery) {
$city = Config::SAME_DAY_DELIVERY_CITY;
}
if ($street) {
$parcelShops = $parcelShopService->getFilteredParcels($countryCode, $city, $street);
} else {
Expand Down
22 changes: 16 additions & 6 deletions dpdbaltics.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ public function hookActionValidateStepComplete(&$params)
$cart = $params['cart'];
$carrier = new Carrier($cart->id_carrier);
$idShop = $this->context->shop->id;
/** @var Invertus\dpdBaltics\Provider\CurrentCountryProvider $currentCountryProvider */
$currentCountryProvider = $this->getModuleContainer(
\Invertus\dpdBaltics\Provider\CurrentCountryProvider::class
);

$currentCountry = $currentCountryProvider->getCurrentCountryIsoCode($cart);

/** @var Invertus\dpdBaltics\Repository\CarrierRepository $carrierRepo */
/** @var Invertus\dpdBaltics\Repository\ProductRepository $productRepo */
Expand All @@ -274,7 +280,8 @@ public function hookActionValidateStepComplete(&$params)
$isDpdCarrier = true;
$productId = $productRepo->getProductIdByCarrierReference($carrier->id_reference);
$product = new DPDProduct($productId);
$isSameDayDelivery = $product->product_reference === Config::PRODUCT_TYPE_SAME_DAY_DELIVERY;
$isSameDayDelivery =
$product->product_reference === Config::getSameDeliveryDayParcelTypeByCountryCode($currentCountry);
break;
}
}
Expand All @@ -287,7 +294,9 @@ public function hookActionValidateStepComplete(&$params)
$pudoRepo = $this->getModuleContainer(PudoRepository::class);
$pudoId = $pudoRepo->getIdByCart($cart->id);
$selectedPudo = new DPDPudo($pudoId);
if ($selectedPudo->city !== Config::SAME_DAY_DELIVERY_CITY) {
$pudoCity = strtolower($selectedPudo->city);

if (!in_array($pudoCity, Config::getSameDeliveryDayCities($currentCountry), true)) {
$this->context->controller->errors[] =
$this->l('This carrier can\'t deliver to your selected city');
$params['completed'] = false;
Expand Down Expand Up @@ -448,7 +457,7 @@ public function getOrderShippingCostExternal($cart)
return false;
}

if ($serviceCarrier['product_reference'] === Config::PRODUCT_TYPE_SAME_DAY_DELIVERY) {
if ($serviceCarrier['product_reference'] === Config::getSameDeliveryDayParcelTypeByCountryCode($countryCode)) {
$isSameDayAvailable = \Invertus\dpdBaltics\Util\ProductUtility::validateSameDayDelivery(
$countryCode,
$deliveryAddress->city
Expand Down Expand Up @@ -505,7 +514,7 @@ public function hookDisplayCarrierExtraContent($params)
$productId = $productRepo->getProductIdByCarrierReference($carrier->id_reference);
$dpdProduct = new DPDProduct($productId);
$return = '';
if ($dpdProduct->getProductReference() === Config::PRODUCT_TYPE_SAME_DAY_DELIVERY) {
if ($dpdProduct->getProductReference() === Config::getSameDeliveryDayParcelTypeByCountryCode($countryCode)) {
/** @var \Invertus\dpdBaltics\Presenter\SameDayDeliveryMessagePresenter $sameDayDeliveryPresenter */
$sameDayDeliveryPresenter = $this->getModuleContainer()->get(\Invertus\dpdBaltics\Presenter\SameDayDeliveryMessagePresenter::class);
$return .= $sameDayDeliveryPresenter->getSameDayDeliveryMessageTemplate();
Expand All @@ -528,7 +537,8 @@ public function hookDisplayCarrierExtraContent($params)
$parcelShopRepo = $this->getModuleContainer(ParcelShopRepository::class);
$productRepo = $this->getModuleContainer(ProductRepository::class);
$product = $productRepo->findProductByCarrierReference($carrier->id_reference);
$isSameDayDelivery = ($product['product_reference'] === Config::PRODUCT_TYPE_SAME_DAY_DELIVERY);
$isSameDayDelivery =
($product['product_reference'] === Config::getSameDeliveryDayParcelTypeByCountryCode($countryCode));

$pudoId = $pudoRepo->getIdByCart($cart->id);
$selectedPudo = new DPDPudo($pudoId);
Expand Down Expand Up @@ -594,7 +604,7 @@ public function hookDisplayCarrierExtraContent($params)
}

if ($isSameDayDelivery) {
$cityList['Rīga'] = 'Rīga';
$cityList = Config::getSameDeliveryDayCities($countryCode, false);
} else {
$cityList = $parcelShopRepo->getAllCitiesByCountryCode($countryCode);
}
Expand Down
111 changes: 111 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,13 @@ class Config
const PRODUCT_TYPE_SATURDAY_DELIVERY = 'D-B2C-SAT';
const PRODUCT_TYPE_SATURDAY_DELIVERY_COD = 'D-B2C-COD-SAT';
const PRODUCT_TYPE_SAME_DAY_DELIVERY = '274';
const PRODUCT_TYPE_SAME_DAY_DELIVERY_LITHUANIA = 'SDB2C';
const PRODUCT_TYPE_SAME_DAY_DELIVERY_LITHUANIA_COD = 'SDB2C-COD';

const PS_VERSION_1_7_7 = '1.7.7.0';



const PRODUCT_NAME_B2B = [
'LT' => 'Pristatymas privatiems asmenims',
'EE' => 'DPD kuller',
Expand Down Expand Up @@ -277,6 +282,25 @@ class Config
'EN' => 'Same day Delivery',
];

const PRODUCT_NAME_SAME_DAY_DELIVERY_LITHUANIA = [
'LT' => 'Pristatymas tą pačią dieną',
'EE' => 'Same day Delivery',
'LV' => 'Same day Delivery',
'EN' => 'Same day Delivery',
];

const PRODUCT_NAME_SAME_DAY_DELIVERY_LITHUANIA_COD = [
'LT' => 'Pristatymas tą pačią dieną, atsiskaitymas grynaisiais',
'EE' => 'Same day Delivery',
'LV' => 'Same day Delivery',
'EN' => 'Same day Delivery',
];

/**
* @param string $webServiceCountry
*
* @return DPDProductInstallCollection
*/
public static function getProducts($webServiceCountry = 'EN')
{
$collection = new DPDProductInstallCollection();
Expand Down Expand Up @@ -432,6 +456,24 @@ public static function getProductByReference($productReference, $countryCode = '
$product->setIsCod(0);
return $product;
break;
case self::PRODUCT_TYPE_SAME_DAY_DELIVERY_LITHUANIA:
$product = new DPDProductInstall();
$product->setName(self::PRODUCT_NAME_SAME_DAY_DELIVERY_LITHUANIA[$countryCode]);
$product->setId(self::PRODUCT_TYPE_SAME_DAY_DELIVERY_LITHUANIA);
$product->setDelay('Your delivery experts');
$product->setIsPudo(1);
$product->setIsCod(0);
return $product;
break;
case self::PRODUCT_TYPE_SAME_DAY_DELIVERY_LITHUANIA_COD:
$product = new DPDProductInstall();
$product->setName(self::PRODUCT_NAME_SAME_DAY_DELIVERY_LITHUANIA_COD[$countryCode]);
$product->setId(self::PRODUCT_TYPE_SAME_DAY_DELIVERY_LITHUANIA_COD);
$product->setDelay('Your delivery experts');
$product->setIsPudo(1);
$product->setIsCod(1);
return $product;
break;
default:
return false;
break;
Expand Down Expand Up @@ -459,6 +501,8 @@ public static function getDefaultServiceWeights($countryIso, $productReference)
case self::PRODUCT_TYPE_PUDO:
case self::PRODUCT_TYPE_PUDO_COD:
return 20;
case self::PRODUCT_TYPE_SAME_DAY_DELIVERY_LITHUANIA:
case self::PRODUCT_TYPE_SAME_DAY_DELIVERY_LITHUANIA_COD:
case self::PRODUCT_TYPE_SAME_DAY_DELIVERY:
return 31.5;
case self::PRODUCT_TYPE_SATURDAY_DELIVERY_COD:
Expand Down Expand Up @@ -522,6 +566,73 @@ public static function getTimeFrameCountries($countryIso)
}
}

/**
* @param $countryIso
* @param bool $toLowerCase
*
* @return array|string[]
*/
public static function getSameDeliveryDayCities($countryIso, $toLowerCase = true): array
{
$cities = [];
switch ($countryIso) {
case self::LITHUANIA_ISO_CODE:
$cities = [
'Vilnius',
'Kaunas',
];
break;
case self::ESTONIA_ISO_CODE:
$cities = [
'Tallinn',
'Tartu',
'Pärnu',
'Jõhvi',
'Rakvere',
'Haapsalu',
];
break;
case self::LATVIA_ISO_CODE:
$cities = [
'Rīga',
];
break;
default:
$cities = [];
}

if (!empty($cities) && $toLowerCase) {
$cities = array_map('strtolower', $cities);
}

return $cities;
}

/**
* @return string[]
*/
public static function getSameDeliveryDayCountries()
{
return [self::ESTONIA_ISO_CODE, self::LITHUANIA_ISO_CODE, self::LATVIA_ISO_CODE];
}

/**
* @param $countryCode
* @return string
*/
public static function getSameDeliveryDayParcelTypeByCountryCode($countryCode)
{
switch ($countryCode) {
case self::LATVIA_ISO_CODE:
case self::ESTONIA_ISO_CODE:
return self::PRODUCT_TYPE_SAME_DAY_DELIVERY;
break;
case self::LITHUANIA_ISO_CODE:
return self::PRODUCT_TYPE_SAME_DAY_DELIVERY_LITHUANIA;

}
}

public static function getDeliveryTimes($isoCode)
{
switch ($isoCode) {
Expand Down
30 changes: 16 additions & 14 deletions src/Service/Product/ProductService.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,31 +99,33 @@ public function addProduct($productReference, $countryCode = null)

public function updateCarriersOnCountryChange($newCountryIsoCode)
{
$productId = $this->productRepository->getProductIdByProductReference(
Config::PRODUCT_TYPE_PUDO_COD
);
$productId = $this->productRepository
->getProductIdByProductReference(Config::PRODUCT_TYPE_PUDO_COD);
if ($newCountryIsoCode === Config::LATVIA_ISO_CODE) {
$this->deleteProduct(Config::PRODUCT_TYPE_PUDO_COD);
} elseif (!$productId) {
$this->addProduct(Config::PRODUCT_TYPE_PUDO_COD, $newCountryIsoCode);
$this->addProduct(Config::PRODUCT_TYPE_PUDO_COD);
}

$productId = $this->productRepository->getProductIdByProductReference(
Config::PRODUCT_TYPE_SAME_DAY_DELIVERY
);
if ($newCountryIsoCode !== Config::LATVIA_ISO_CODE) {
$parcelType = Config::getSameDeliveryDayParcelTypeByCountryCode($newCountryIsoCode);
$productId = $this->productRepository->getProductIdByProductReference($parcelType);

if ($newCountryIsoCode === Config::LITHUANIA_ISO_CODE) {
$this->deleteProduct(Config::PRODUCT_TYPE_SAME_DAY_DELIVERY);
} elseif (!$productId) {
$this->addProduct(Config::PRODUCT_TYPE_SAME_DAY_DELIVERY, $newCountryIsoCode);
} else {
$this->deleteProduct(Config::PRODUCT_TYPE_SAME_DAY_DELIVERY_LITHUANIA);
}

if (!$productId) {
$this->addProduct($parcelType);
}

$productId = $this->productRepository->getProductIdByProductReference(
Config::PRODUCT_TYPE_SATURDAY_DELIVERY_COD
);
$productId = $this->productRepository
->getProductIdByProductReference(Config::PRODUCT_TYPE_SATURDAY_DELIVERY_COD);
if ($newCountryIsoCode === Config::LATVIA_ISO_CODE) {
$this->deleteProduct(Config::PRODUCT_TYPE_SATURDAY_DELIVERY_COD);
} elseif (!$productId) {
$this->addProduct(Config::PRODUCT_TYPE_SATURDAY_DELIVERY_COD, $newCountryIsoCode);
$this->addProduct(Config::PRODUCT_TYPE_SATURDAY_DELIVERY_COD);
}

return true;
Expand Down
12 changes: 7 additions & 5 deletions src/Util/ProductUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class ProductUtility
public static function hasAvailability($productReference) {
if ($productReference === Config::PRODUCT_TYPE_SATURDAY_DELIVERY ||
$productReference === Config::PRODUCT_TYPE_SATURDAY_DELIVERY_COD ||
$productReference === Config::PRODUCT_TYPE_SAME_DAY_DELIVERY) {
$productReference === Config::PRODUCT_TYPE_SAME_DAY_DELIVERY ||
$productReference === Config::PRODUCT_TYPE_SAME_DAY_DELIVERY_LITHUANIA ||
$productReference === Config::PRODUCT_TYPE_SAME_DAY_DELIVERY_LITHUANIA_COD) {
return true;
}

Expand All @@ -18,15 +20,15 @@ public static function hasAvailability($productReference) {

public static function validateSameDayDelivery($countryIso, $city)
{
if ($countryIso !== Config::LATVIA_ISO_CODE) {
$city = strtolower($city);
if (!in_array($countryIso, Config::getSameDeliveryDayCountries(), true)) {
return false;
}

if (strtolower($city) === 'riga' || strtolower($city) === 'rīga') {
}
if (in_array($city, Config::getSameDeliveryDayCities($countryIso), true)) {
return true;
}

return false;
}

}