2017-08-30 11:37:48 +02:00

330 lines
11 KiB
PHP

<?php
/**
* TNT OFFICIAL MODULE FOR PRESTASHOP
*
* @author GFI Informatique <www.gfi.fr>
* @copyright 2016-2017 GFI Informatique, 2016-2017 TNT
* @license https://opensource.org/licenses/MIT MIT License
*/
require_once _PS_MODULE_DIR_.'tntofficiel/tntofficiel.php';
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Debug.php';
class TNTOfficiel_Carrier
{
/**
* Configuration name.
*
* @var string
*/
private static $strConfigNameCarrierID = 'TNT_CARRIER_ID';
/**
* Create a new TNT global carrier and save it as current one.
*
* @return bool
*/
public static function createGlobalCarrier()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
// Create new carrier.
$objCarrierNew = new Carrier();
$objCarrierNew->active = true;
$objCarrierNew->deleted = false;
// Carrier used for module.
$objCarrierNew->is_module = true;
$objCarrierNew->external_module_name = TNTOfficiel::MODULE_NAME;
// Carrier name.
$objCarrierNew->name = 'TNT Express France';
// Carrier delay description per language ISO code.
// Mandatory, but not used.
$objCarrierNew->delay = array(
//'fr' => 'Livraison en 1 à 3 jours en France métropolitaine (Intra et Corse)',
Configuration::get('PS_LANG_DEFAULT') => 'TNT'
);
// Disable applying tax rules group.
$objCarrierNew->id_tax_rules_group = 0;
// Disable adding handling charges from config PS_SHIPPING_HANDLING.
$objCarrierNew->shipping_handling = false;
// Enable use of Cart getPackageShippingCost, getOrderShippingCost or getOrderShippingCostExternal
$objCarrierNew->shipping_external = true;
// Enable calculations for the ranges.
$objCarrierNew->need_range = true;
$objCarrierNew->range_behavior = 0;
// Unable to create new carrier.
if (!$objCarrierNew->add()) {
return false;
}
$objDB = Db::getInstance();
$groups = Group::getGroups(true);
foreach ($groups as $group) {
$objDB->insert(
'carrier_group',
array(
'id_carrier' => (int)$objCarrierNew->id,
'id_group' => (int)$group['id_group'],
)
);
}
$objRangePrice = new RangePrice();
$objRangePrice->id_carrier = $objCarrierNew->id;
$objRangePrice->delimiter1 = '0';
$objRangePrice->delimiter2 = '1000000';
$objRangePrice->add();
$objRangeWeight = new RangeWeight();
$objRangeWeight->id_carrier = $objCarrierNew->id;
$objRangeWeight->delimiter1 = '0';
$objRangeWeight->delimiter2 = '1000000';
$objRangeWeight->add();
// Get active zones list.
$arrZoneList = Zone::getZones(true);
foreach ($arrZoneList as $arrZone) {
$objDB->insert(
'carrier_zone',
array(
'id_carrier' => (int)$objCarrierNew->id,
'id_zone' => (int)$arrZone['id_zone']
)
);
$objDB->insert(
'delivery',
array(
'id_carrier' => (int)$objCarrierNew->id,
'id_range_price' => (int)$objRangePrice->id,
'id_range_weight' => null,
'id_zone' => (int)$arrZone['id_zone'],
'price' => '0'
),
true,
false
);
$objDB->insert(
'delivery',
array(
'id_carrier' => (int)$objCarrierNew->id,
'id_range_price' => null,
'id_range_weight' => (int)$objRangeWeight->id,
'id_zone' => (int)$arrZone['id_zone'],
'price' => '0'
),
true,
false
);
}
// Save the carrier ID.
$boolResult = TNTOfficiel_Carrier::setCarrierID($objCarrierNew->id);
return $boolResult;
}
/**
* Create a logo for a carrier.
*
* @param string $strArgModuleDir Module absolute Path.
* @param int $intArgCarrierIDTNT
* @return bool
*/
public static function createLogo($strArgModuleDir, $intArgCarrierIDTNT)
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
// Add carrier logo.
$boolResult = copy($strArgModuleDir.'/views/img/carriers/tntofficiel.jpg', _PS_SHIP_IMG_DIR_.(int)$intArgCarrierIDTNT.'.jpg');
return $boolResult;
}
/**
* Undelete the existing current TNT global carrier by setting its flag to not deleted.
*
* @return bool
*/
public static function undeleteGlobalCarrier()
{
// Load current TNT carrier object.
$objCarrierTNT = TNTOfficiel_Carrier::loadGlobalCarrier();
// If TNT carrier object not available.
if ($objCarrierTNT === null) {
// Unable to undelete.
return false;
}
$objCarrierTNT->active = true;
$objCarrierTNT->deleted = false;
// Always set the right module name.
$objCarrierTNT->external_module_name = TNTOfficiel::MODULE_NAME;
$boolResult = $objCarrierTNT->save();
return $boolResult;
}
/**
* Delete the existing current TNT global carrier by setting its flag to deleted.
*
* @return bool
*/
public static function deleteGlobalCarrier()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
// Load current TNT carrier object.
$objCarrierTNT = TNTOfficiel_Carrier::loadGlobalCarrier();
// If TNT carrier object not available.
if ($objCarrierTNT === null) {
// Nothing to delete.
return true;
}
$objCarrierTNT->active = false;
$objCarrierTNT->deleted = true;
$boolResult = $objCarrierTNT->save();
return $boolResult;
}
/**
* Get the current TNT carrier ID.
*
* @return int|null
*/
public static function getGlobalCarrierID()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
// Get current TNT carrier.
$intCarrierIDTNT = Configuration::get(TNTOfficiel_Carrier::$strConfigNameCarrierID);
// Carrier ID must be an integer greater than 0.
if (empty($intCarrierIDTNT) || $intCarrierIDTNT != (int)$intCarrierIDTNT || !((int)$intCarrierIDTNT > 0) ) {
return null;
}
return (int)$intCarrierIDTNT;
}
/**
* Set the current TNT carrier ID.
*
* @param int $intArgCarrierID
* @return bool
*/
public static function setCarrierID($intArgCarrierID)
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
// Carrier ID must be an integer greater than 0.
if (empty($intArgCarrierID) || $intArgCarrierID != (int)$intArgCarrierID || !((int)$intArgCarrierID > 0) ) {
return false;
}
return Configuration::updateValue(TNTOfficiel_Carrier::$strConfigNameCarrierID, $intArgCarrierID);
}
/**
* Load current TNT global carrier object model.
*
* @return Carrier|null
*/
public static function loadGlobalCarrier()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
// Get current TNT carrier ID.
$intCarrierIDTNT = TNTOfficiel_Carrier::getGlobalCarrierID();
// If TNT carrier ID not availaible.
if ($intCarrierIDTNT === null) {
return null;
}
// Load TNT carrier.
$objCarrierTNT = new Carrier($intCarrierIDTNT);
// If TNT carrier object not available.
if (!(Validate::isLoadedObject($objCarrierTNT) && (int)$objCarrierTNT->id === $intCarrierIDTNT)) {
return null;
}
return $objCarrierTNT;
}
/**
* Force some current carrier settings. Should be removed in the near future.
*
* @return bool
*/
public static function forceGlobalCarrierDefaultValues()
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
// Load current TNT carrier object.
$objCarrierTNT = TNTOfficiel_Carrier::loadGlobalCarrier();
// If TNT carrier correctly loaded.
if ($objCarrierTNT !== null)
{
// Get all users groups associated with the TNT carrier.
$arrCarrierGroups = $objCarrierTNT->getGroups();
// If there is currently at least one users groups set.
if (is_array($arrCarrierGroups) && count($arrCarrierGroups) > 0)
{
// Current users groups set.
$arrCarrierGroupsSet = array();
foreach ($arrCarrierGroups as $arrRowCarrierGroup) {
// DB request fail. stop here.
if (!array_key_exists( 'id_group', $arrRowCarrierGroup )) {
return false;
}
$arrCarrierGroupsSet[] = (int)$arrRowCarrierGroup['id_group'];
}
// Users groups to exclude.
$arrCarrierGroupsExclude = array(
(int)Configuration::get('PS_UNIDENTIFIED_GROUP'),
(int)Configuration::get('PS_GUEST_GROUP')
// PS_CUSTOMER_GROUP (default) preserved
// CUSTOM GROUP preserved
);
// Get groups previously set, minus groups to exclude.
$arrCarrierGroupsApply = array_diff($arrCarrierGroupsSet, $arrCarrierGroupsExclude);
// If groups change.
if(count(array_diff($arrCarrierGroupsSet, $arrCarrierGroupsApply)) > 0) {
// Force carrier users groups (delete all, then set).
$objCarrierTNT->setGroups($arrCarrierGroupsApply, true);
}
}
}
return true;
}
/**
* Check if a carrier ID is a TNT one.
*
* @param int $intArgCarrierID
* @return boolean
*/
public static function isTNTOfficielCarrierID($intArgCarrierID)
{
TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__));
// Carrier ID must be an integer greater than 0.
if (empty($intArgCarrierID) || $intArgCarrierID != (int)$intArgCarrierID || !((int)$intArgCarrierID > 0) ) {
return false;
}
$intCarrierID = (int)$intArgCarrierID;
$obCarrier = new Carrier($intCarrierID);
return $obCarrier->external_module_name === TNTOfficiel::MODULE_NAME;
}
}