Skip to content

Commit

Permalink
Merge pull request #83 from mijora/Develop
Browse files Browse the repository at this point in the history
Develop from v2.0.21
  • Loading branch information
markakk authored Aug 9, 2024
2 parents cc9946f + f5cc6d6 commit 0b3fe03
Show file tree
Hide file tree
Showing 89 changed files with 4,513 additions and 734 deletions.
11 changes: 11 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
= 2.1.0 =
# Improved
- Added compatibility with One Page Checkout PS (onepagecheckoutps) module
- The module is adapted to work with Omniva OMX

# Changed
- Changed the type of the "Company country code" field to Select in the module settings to avoid an errors when entered an invalid value

# Updated
- API library to 1.1.0

= 2.0.21 =
# Improved
- Added that Omniva delivery methods would not be displayed if the dimensions of the cart exceed the maximum dimensions specified in the carrier's settings
Expand Down
56 changes: 0 additions & 56 deletions classes/Omniva18PlusProduct.php

This file was deleted.

211 changes: 171 additions & 40 deletions classes/OmnivaApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,40 +66,19 @@ public function createShipment($id_order)

$sendOffCountry = $this->getSendOffCountry($orderAdress);
$service = $this->getServiceCode($order->id_carrier, $sendOffCountry);

$sender_iso_code = strtoupper((string) Configuration::get('omnivalt_countrycode'));
if ($service === 'CD' && $sender_iso_code === 'LT') {
return ['msg' => 'Sending to Finland terminals is not available for LT senders'];
}

$is_terminal_service = ($service == "PA" || $service == "PU" || $service == 'CD');

$additionalServices = [];
if ($is_terminal_service)
{
$additionalServices[] = "ST";
if(Configuration::get('send_delivery_email'))
{
$additionalServices[] = "SF";
}
}
if ($omnivaOrder->cod) {
$additionalServices[] = "BP";
if ($country_iso == 'FI' && $is_terminal_service) {
return ['msg' => 'Additional service COD is not available in this country'];
}

if ($is_terminal_service && !self::isOmnivaMethodAllowed('pt', $country_iso)) {
$countries_txt = array('LT' => 'Lithuania', 'LV' => 'Latvia', 'EE' => 'Estonia', 'FI' => 'Finland');
$receiver_country_txt = (isset($countries_txt[$country_iso])) ? $countries_txt[$country_iso] : $country_iso;
$sender_country_code = strtoupper((string) Configuration::get('omnivalt_countrycode'));
$sender_country_txt = (isset($countries_txt[$sender_country_code])) ? $countries_txt[$sender_country_code] : $sender_country_code;
return ['msg' => 'Sending to ' . $receiver_country_txt . ' terminals from ' . $sender_country_txt . ' is not available for ' . $sender_iso_code . ' users'];
}

// 18+ check
foreach ($order->getProducts() as $orderProduct) {
$productId = (int) $orderProduct['product_id'];

$isProduct18Plus = Omniva18PlusProduct::get18PlusStatus($productId, true);

if ($isProduct18Plus) {
$additionalServices[] = "PC";
}
}
$additionalServices = self::getAdditionalServices($order);

// calculate weight
$pack_weight = (float) $omnivaOrder->weight;
Expand All @@ -111,13 +90,26 @@ public function createShipment($id_order)
$pack_weight = round($pack_weight / (int) $omnivaOrder->packs, 2);
}

$is_consolidated = ($omnivaOrder->packs > 1 && $omnivaOrder->cod) ? true : false;

for ($i = 0; $i < $omnivaOrder->packs; $i++)
{
$package_id = (string) $id_order;
if ($omnivaOrder->packs > 1 && ! $is_consolidated) {
$package_id .= '_' . ($i + 1);
}

$package = new Package();
$package->setId($package_id);
$package->setService($service);
$additionalServiceObj = [];
foreach ($additionalServices as $additionalServiceCode)
{
if ($is_consolidated && $i > 0) {
if ($additionalServiceCode !== 'BC') {
continue;
}
}
$additionalServiceObj[] = (new AdditionalService())->setServiceCode($additionalServiceCode);
}
$package->setAdditionalServices($additionalServiceObj);
Expand All @@ -127,7 +119,8 @@ public function createShipment($id_order)
$package->setMeasures($measures);

//set COD
if($omnivaOrder->cod)
$allow_cod = ($is_consolidated && $i > 0) ? false : true;
if($omnivaOrder->cod && $allow_cod)
{
$company = Configuration::get('omnivalt_company');
$bank_account = Configuration::get('omnivalt_bank_account');
Expand Down Expand Up @@ -206,6 +199,49 @@ public function createShipment($id_order)
}
}

public static function getAdditionalServices($order)
{
$omnivaOrder = new OmnivaOrder($order->id);
$orderAdress = new \Address($order->id_address_delivery);
$country_iso = Country::getIsoById($orderAdress->id_country);
$sendOffCountry = self::getSendOffCountry($orderAdress);
$service = self::getServiceCode($order->id_carrier, $sendOffCountry);
$additionalServices = [];

$is_terminal_service = ($service == "PA" || $service == "PU" || $service == 'CD');

if ($is_terminal_service) {
$additionalServices[] = "ST";
if(Configuration::get('send_delivery_email')) {
$additionalServices[] = "SF";
}
}

if ($omnivaOrder->cod) {
$additionalServices[] = "BP";
if ($country_iso == 'FI' && $is_terminal_service) {
return ['error' => 'Additional service COD is not available in this country'];
}
}

// Products services check
foreach ($order->getProducts() as $orderProduct) {
$productId = (int) $orderProduct['product_id'];

$isProduct18Plus = OmnivaProduct::get18PlusStatus($productId, true);
if ($isProduct18Plus) {
$additionalServices[] = "PC";
}

$isFragile = OmnivaProduct::getFragileStatus($productId, true);
if ($isFragile) {
$additionalServices[] = "BC";
}
}

return $additionalServices;
}

private function getReceiverName($orderAdress)
{
$reveicer_name = $orderAdress->firstname . ' ' . $orderAdress->lastname;
Expand All @@ -216,7 +252,7 @@ private function getReceiverName($orderAdress)
return trim($reveicer_name);
}

public function getServiceCode($id_carrier, $sendOffCountry)
public static function getServiceCode($id_carrier, $sendOffCountry)
{
$send_method = '';
$terminals = OmnivaltShipping::getCarrierIds(['omnivalt_pt']);
Expand Down Expand Up @@ -252,7 +288,7 @@ public function getServiceCode($id_carrier, $sendOffCountry)
return $service;
}

public function getSendOffCountry($address = null)
public static function getSendOffCountry($address = null)
{
$api_country = Configuration::get('omnivalt_api_country');
$ee_service_enabled = Configuration::get('omnivalt_ee_service');
Expand Down Expand Up @@ -291,6 +327,84 @@ public function getSendOffCountry($address = null)
return $country_iso == 'FI' ? 'finland' : 'baltic';
}

/**
* Check if the Omniva method is allowed to ship to the specified country
*
* @param string $method_key - Method key. 'pt' for terminal, 'c' for courier
* @param string $receiver_country - The country code to which the delivery is being attempted
* @return boolean - Is allowed or not
*/
public static function isOmnivaMethodAllowed($method_key, $receiver_country)
{
$api_country = Configuration::get('omnivalt_api_country');
$ee_service_enabled = Configuration::get('omnivalt_ee_service');
$fi_service_enabled = Configuration::get('omnivalt_fi_service');
$sender_country = Configuration::get('omnivalt_countrycode');

if (empty($api_country) || empty($sender_country)) {
return false;
}

$api_country = strtoupper($api_country);
$sender_country = strtoupper($sender_country);
$receiver_country = strtoupper($receiver_country);

if ($api_country == 'EE' && $receiver_country == 'EE' && !$ee_service_enabled) {
return false;
}
if ($api_country == 'EE' && $receiver_country == 'FI' && !$fi_service_enabled) {
return false;
}

/* Allowed countries structure:
* 'api_countries' => array(
* 'sender_country' => array('receiver_country')
* )
*/
$allowed_terminal_countries = array(
'LT,LV,EE' => array(
'LT' => array('LT', 'LV', 'EE', 'FI'),
'LV' => array('LT', 'LV', 'EE', 'FI'),
'EE' => array('LT', 'LV', 'EE', 'FI'),
),
);
$allowed_courier_countries = array(
'LT,LV' => array(
'LT' => array('LT', 'LV', 'EE'),
'LV' => array('LT', 'LV', 'EE'),
'EE' => array('LT', 'LV', 'EE'),
),
'EE' => array(
'LT' => array('LT', 'LV', 'EE', 'FI'),
'LV' => array('LT', 'LV', 'EE', 'FI'),
'EE' => array('LT', 'LV', 'EE', 'FI'),
),
);

switch ($method_key) {
case 'pt':
$allowed_countries = $allowed_terminal_countries;
break;
case 'c':
$allowed_countries = $allowed_courier_countries;
break;
default:
$allowed_countries = array();
}

$is_allow = false;
foreach ($allowed_countries as $api_countries => $shipping_countries) {
$splited_api_countries = explode(',', $api_countries);
if (in_array($api_country, $splited_api_countries) && isset($shipping_countries[$sender_country])) {
if (in_array($receiver_country, $shipping_countries[$sender_country])) {
$is_allow = true;
}
}
}

return $is_allow;
}

private function getSenderContact()
{
$senderContact = new Contact();
Expand Down Expand Up @@ -362,6 +476,7 @@ public function getManifest()
foreach ($barcodes as $barcode)
{
$order = new Order();
$order->setOrderNumber($omnivaOrder->id);
$order->setTracking($barcode);
$order->setQuantity(1);
$order->setWeight(round($omnivaOrder->weight / $num_packages, 2));
Expand Down Expand Up @@ -438,15 +553,31 @@ public function callCarrier()
if (empty($pickup_start)) $pickup_start = '8:00';
if (empty($pickup_end)) $pickup_end = '17:00';

$call = new CallCourier();
$call->setDestinationCountry($this->getSendOffCountry());
$call->setEarliestPickupTime($pickup_start);
$call->setLatestPickupTime($pickup_end);
$this->setAuth($call);
$call->setSender($this->getSenderContact());

try {
return $call->callCourier();
$call = new CallCourier();
$call->setDestinationCountry($this->getSendOffCountry());
$call->setEarliestPickupTime($pickup_start);
$call->setLatestPickupTime($pickup_end);
$this->setAuth($call);
$call->setSender($this->getSenderContact());

$result = $call->callCourier();
if ($result) {
$result_data = $call->getResponseBody();
$call_data = array(
'id' => $result_data['courierOrderNumber'],
'start' => date('Y-m-d H:i:s', strtotime($result_data['startTime'] . ' UTC')),
'end' => date('Y-m-d H:i:s', strtotime($result_data['endTime'] . ' UTC'))
);
OmnivaHelper::addScheduledCourierCall($call_data['id'], $call_data['start'], $call_data['end']);
return array(
'status' => true,
'call_id' => $call_data['id'],
'start_time' => $call_data['start'],
'end_time' => $call_data['end'],
);
}
return $result;
}
catch (OmnivaException $e)
{
Expand Down
2 changes: 1 addition & 1 deletion classes/OmnivaDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class OmnivaDb
'omniva_order',
'omniva_cart_terminal',
'omniva_order_history',
'omniva_18_plus_product'
'omniva_product'
];
/**
* Create tables for module
Expand Down
Loading

0 comments on commit 0b3fe03

Please sign in to comment.