* @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 TNTOfficielPickup */ class TNTOfficielPickup extends ObjectModel { // id_tntofficiel_pickup public $id; public $id_shop; public $account_number; public $pickup_date; public static $definition = array( 'table' => 'tntofficiel_pickup', 'primary' => 'id_tntofficiel_pickup', 'fields' => array( 'id_shop' => array( 'type' => ObjectModel::TYPE_INT, 'size' => 10, 'validate' => 'isUnsignedId', ), 'account_number' => array( 'type' => ObjectModel::TYPE_STRING, 'size' => 8 ), 'pickup_date' => array( 'type' => ObjectModel::TYPE_DATE, 'validate' => 'isDateFormat', ), ), ); // 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.TNTOfficielPickup::$definition['table']; // Create table. $strSQLCreatePickup = <<execute($strSQLCreatePickup)) { TNTOfficiel_Logger::logInstall($strLogMessage.' : '.$objDB->getMsgError(), false); return false; } TNTOfficiel_Logger::logInstall($strLogMessage); return true; } /** * Constructor. */ public function __construct($intArgId = null, $intArgLangId = null, $intArgShopId = null) { TNTOfficiel_Logstack::log(); parent::__construct($intArgId, $intArgLangId, $intArgShopId); } /** * Load existing object model or optionally create a new one for it's ID. * * @param int $intArgPickupID * @param bool $boolArgCreate * @param int $intArgLangID * @param int $intArgShopID * * @return TNTOfficielPickup|null */ public static function loadPickupID($intArgPickupID = null) { TNTOfficiel_Logstack::log(); $intPickupID = (int)$intArgPickupID; // Create. if ($intPickupID === 0) { // Create a new TNT pickup entry. $objTNTPickupModelCreate = new TNTOfficielPickup(null); // Apply default. $objTNTPickupModelCreate->save(); $intPickupID = (int)$objTNTPickupModelCreate->id; unset($objTNTPickupModelCreate); } // No new pickup ID. if (!($intPickupID > 0)) { return null; } $strEntityID = $intPickupID.'-'.(int)null.'-'.(int)null; // If already loaded. if (array_key_exists($strEntityID, TNTOfficielPickup::$arrLoadedEntities)) { $objTNTPickupModel = TNTOfficielPickup::$arrLoadedEntities[$strEntityID]; // Check. if ((int)$objTNTPickupModel->id === $intPickupID && Validate::isLoadedObject($objTNTPickupModel)) { return $objTNTPickupModel; } } // Load existing TNT pickup entry. // or reload after create, to get default DB values after creation. $objTNTPickupModel = new TNTOfficielPickup($intPickupID); // Check. if ((int)$objTNTPickupModel->id !== $intPickupID || !Validate::isLoadedObject($objTNTPickupModel)) { return null; } $objTNTPickupModel->id = (int)$objTNTPickupModel->id; $objTNTPickupModel->id_shop = (int)$objTNTPickupModel->id_shop; TNTOfficielPickup::$arrLoadedEntities[$strEntityID] = $objTNTPickupModel; return $objTNTPickupModel; } /** * Search for a list of existing pickup object model, via a shop ID and account number. * * @param int $intArgShopID * * @return array list of TNTOfficielPickup model found. */ public static function searchShopIDAccountNumber($intArgShopID, $strArgAccountNumber) { TNTOfficiel_Logstack::log(); $arrObjTNTPickupModelList = array(); $intShopID = (int)$intArgShopID; $strAccountNumber = (string)$strArgAccountNumber; // No new customer ID. if (!($intShopID > 0)) { return $arrObjTNTPickupModelList; } // Search row for customer ID. $objDbQuery = new DbQuery(); $objDbQuery->select('*'); $objDbQuery->from(TNTOfficielPickup::$definition['table']); $objDbQuery->where('id_shop = '.$intShopID); $objDbQuery->where('account_number = \''.pSQL($strAccountNumber).'\''); $objDB = Db::getInstance(); $arrResult = $objDB->executeS($objDbQuery); // If row found and match address ID. if (count($arrResult) > 0) { foreach ($arrResult as $arrValue) { if ($intShopID === (int)$arrValue['id_shop']) { $objTNTPickupModel = TNTOfficielPickup::loadPickupID((int)$arrValue['id_tntofficiel_pickup']); // If if ($objTNTPickupModel !== null) { $arrObjTNTPickupModelList[] = $objTNTPickupModel; } } } } return $arrObjTNTPickupModelList; } }