Files
bio-concept-pharma/web/modules/tntofficiel/classes/TNTOfficielParcel.php
2019-11-17 19:14:07 +01:00

320 lines
9.2 KiB
PHP

<?php
/**
* TNT OFFICIAL MODULE FOR PRESTASHOP
*
* @author GFI Informatique <www.gfi.world>
* @copyright 2016-2019 GFI Informatique, 2016-2019 TNT
* @license https://opensource.org/licenses/MIT MIT License
*/
require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_ClassLoader.php';
/**
* Class TNTOfficielParcel
*/
class TNTOfficielParcel extends ObjectModel
{
// id_tntofficiel_parcel
public $id;
public $id_order;
public $weight;
public $tracking_url;
public $parcel_number;
public $pod_url;
public static $definition = array(
'table' => 'tntofficiel_parcels',
'primary' => 'id_tntofficiel_parcel',
'fields' => array(
'id_order' => array(
'type' => ObjectModel::TYPE_INT,
'validate' => 'isUnsignedId',
),
'weight' => array(
'type' => ObjectModel::TYPE_FLOAT,
'validate' => 'isFloat'
),
'tracking_url' => array(
'type' => ObjectModel::TYPE_STRING,
),
'parcel_number' => array(
'type' => ObjectModel::TYPE_STRING,
'size' => 16
),
'pod_url' => array(
'type' => ObjectModel::TYPE_STRING,
),
),
);
// cache and prevent race condition.
private static $arrLoadedEntities = array();
/**
* Creates the tables needed by the model.
*
* @return bool
*/
public static function createTables()
{
TNTOfficiel_Logstack::log();
$strLogMessage = sprintf('%s::%s', __CLASS__, __FUNCTION__);
$strTablePrefix = _DB_PREFIX_;
$strTableEngine = _MYSQL_ENGINE_;
$strTableName = $strTablePrefix.TNTOfficielParcel::$definition['table'];
// Create table.
$strSQLCreateParcel = <<<SQL
CREATE TABLE IF NOT EXISTS `${strTableName}` (
`id_tntofficiel_parcel` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`id_order` INT(10) UNSIGNED NOT NULL,
`weight` DECIMAL(20,6) NOT NULL DEFAULT '0.000000',
`tracking_url` TEXT,
`parcel_number` VARCHAR(16),
`pod_url` TEXT,
-- Key.
PRIMARY KEY (`id_tntofficiel_parcel`)
) ENGINE = ${strTableEngine} DEFAULT CHARSET='utf8' COLLATE='utf8_general_ci';
SQL;
$objDB = Db::getInstance();
if (!$objDB->execute($strSQLCreateParcel)) {
TNTOfficiel_Logger::logInstall($strLogMessage.' : '.$objDB->getMsgError(), false);
return false;
}
TNTOfficiel_Logger::logInstall($strLogMessage);
return true;
}
/**
* Constructor.
*/
public function __construct($intArgID = null)
{
TNTOfficiel_Logstack::log();
parent::__construct($intArgID);
}
/**
* Load existing object model or optionally create a new one with a new ID.
*
* @param $intArgParcelID
* @param bool $boolArgCreate
*
* @return TNTOfficielParcel|null
*/
public static function loadParcelID($intArgParcelID = null)
{
TNTOfficiel_Logstack::log();
$intParcelID = (int)$intArgParcelID;
// Create.
if ($intParcelID === 0) {
// Create a new TNT account entry.
$objTNTParcelModelCreate = new TNTOfficielParcel(null);
$objTNTParcelModelCreate->save();
$intParcelID = (int)$objTNTParcelModelCreate->id;
unset($objTNTParcelModelCreate);
}
// No new account ID.
if (!($intParcelID > 0)) {
return null;
}
$strEntityID = $intParcelID.'-'.(int)null.'-'.(int)null;
// If already loaded.
if (array_key_exists($strEntityID, TNTOfficielParcel::$arrLoadedEntities)) {
$objTNTParcelModel = TNTOfficielParcel::$arrLoadedEntities[$strEntityID];
// Check.
if ((int)$objTNTParcelModel->id === $intParcelID && Validate::isLoadedObject($objTNTParcelModel)) {
return $objTNTParcelModel;
}
}
// Load existing TNT parcel entry.
// or reload after create, to get default DB values after creation.
$objTNTParcelModel = new TNTOfficielParcel($intParcelID);
// Check.
if ((int)$objTNTParcelModel->id !== $intParcelID || !Validate::isLoadedObject($objTNTParcelModel)) {
return null;
}
$objTNTParcelModel->id = (int)$objTNTParcelModel->id;
$objTNTParcelModel->id_order = (int)$objTNTParcelModel->id_order;
$objTNTParcelModel->weight = (float)$objTNTParcelModel->weight;
TNTOfficielParcel::$arrLoadedEntities[$strEntityID] = $objTNTParcelModel;
return $objTNTParcelModel;
}
/**
* Search for a list of existing parcel object model, via a order ID.
*
* @param int $intArgOrderID
*
* @return array list of TNTOfficielParcel model found.
*/
public static function searchOrderID($intArgOrderID)
{
TNTOfficiel_Logstack::log();
$arrObjTNTParcelModelList = array();
$intOrderID = (int)$intArgOrderID;
// No new customer ID.
if (!($intOrderID > 0)) {
return $arrObjTNTParcelModelList;
}
// Search row for customer ID.
$objDbQuery = new DbQuery();
$objDbQuery->select('*');
$objDbQuery->from(TNTOfficielParcel::$definition['table']);
$objDbQuery->where('id_order = '.$intOrderID);
$objDB = Db::getInstance();
$arrResult = $objDB->executeS($objDbQuery);
// If row found and match address ID.
if (count($arrResult) > 0) {
foreach ($arrResult as $arrValue) {
if ($intOrderID === (int)$arrValue['id_order']) {
$objTNTParcelModel = TNTOfficielParcel::loadParcelID((int)$arrValue['id_tntofficiel_parcel']);
// If
if ($objTNTParcelModel !== null) {
$arrObjTNTParcelModelList[] = $objTNTParcelModel;
}
}
}
}
return $arrObjTNTParcelModelList;
}
/**
*
* @param float $fltArgWeight
*
* @return bool
*/
public function setWeight($fltArgWeight)
{
$this->weight = max(round((float)$fltArgWeight, 1, PHP_ROUND_HALF_UP), 0.1);
return $this->save();
}
/**
* Update a parcel weight, matching an order ID.
*
* @param type $intArgOrderID
* @param type $fltArgWeight
*
* @return string|bool string if error.
*/
public function updateParcel($intArgOrderID, $fltArgWeight)
{
TNTOfficiel_Logstack::log();
$intOrderID = (int)$intArgOrderID;
$fltWeight = (float)$fltArgWeight;
// Load TNT order.
$objTNTOrderModel = TNTOfficielOrder::loadOrderID($intOrderID, false);
// If fail.
if ($objTNTOrderModel === null) {
return 'Unable to load TNTOfficielOrder #'.$intOrderID;
}
// If order ID already set, check match.
if ($this->id_order > 0 && $this->id_order !== $intOrderID) {
return 'Unable to match TNTOfficielOrder #'.$intOrderID;
}
// Load an existing TNT carrier.
$objTNTCarrierModel = $objTNTOrderModel->getTNTCarrierModel();
// If fail or carrier is not from TNT module.
if ($objTNTCarrierModel === null) {
return 'Unable to load TNTOfficielCarrier #'.$objTNTCarrierModel->id_carrier;
}
$fltMaxPackageWeight = $objTNTCarrierModel->getMaxPackageWeight();
if ($fltWeight > $fltMaxPackageWeight) {
return 'Le poids d\'un colis ne peut dépasser '.$fltMaxPackageWeight.'Kg';
}
$this->id_order = $intOrderID;
$boolResult = $this->setWeight($fltWeight);
if (!$boolResult) {
return 'Unable to save TNTOfficielParcel #'.$this->id;
}
return true;
}
/**
* Remove a parcel.
*
* @param $parcelId int
*
* @return bool
*/
public static function removeParcel($parcelId)
{
TNTOfficiel_Logstack::log();
return Db::getInstance()->delete(
TNTOfficielParcel::$definition['table'],
'id_tntofficiel_parcel = '.(int)$parcelId
);
}
/**
* Save the Proof Of Delivery.
*
* @param $data
* @param $parcelId
*
* @return bool
*/
public static function savePODURL($data, $parcelId)
{
TNTOfficiel_Logstack::log();
$data = (array)$data;
$result = false;
$strPODURL = false;
// Get POD Url from parcel data.
if (isset($data['primaryPODUrl']) && $data['primaryPODUrl']) {
$strPODURL = $data['primaryPODUrl'];
} elseif (isset($data['secondaryPODUrl']) && $data['secondaryPODUrl']) {
$strPODURL = $data['secondaryPODUrl'];
}
if ($strPODURL) {
$result = Db::getInstance()->update(
TNTOfficielParcel::$definition['table'],
array('pod_url' => pSQL($strPODURL)),
'id_tntofficiel_parcel = '.(int)$parcelId
);
}
return $result;
}
}