bebeboutik/modules/soflexibilite/classes/SoFlexibiliteDelivery.php
Srv Bebeboutik c48e673922 fix prod
2016-06-30 11:31:11 +02:00

693 lines
26 KiB
PHP

<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to a commercial license from Common-Services Co., Ltd.
* Use, copy, modification or distribution of this source file without written
* license agreement from the SARL SMC is strictly forbidden.
* In order to obtain a license, please contact us: contact@common-services.com
* ...........................................................................
* INFORMATION SUR LA LICENCE D'UTILISATION
*
* L'utilisation de ce fichier source est soumise a une licence commerciale
* concedee par la societe Common-Services Co., Ltd.
* Toute utilisation, reproduction, modification ou distribution du present
* fichier source sans contrat de licence ecrit de la part de la Common-Services Co. Ltd. est
* expressement interdite.
* Pour obtenir une licence, veuillez contacter Common-Services Co., Ltd. a l'adresse: contact@common-services.com
* ...........................................................................
*
* @package soflexibilite
* @author Alexandre D.
* @copyright Copyright (c) 2011-2015 Common Services Co Ltd - 90/25 Sukhumvit 81 - 10260 Bangkok - Thailand
* @license Commercial license
* Support by mail : support.soflexibilite@common-services.com
*/
class SoFlexibiliteDelivery
{
const MODE_PRESTASHOP = 1;
const MODE_THIRDPARTY = 2;
public $mode;
public $id;
public $id_order;
public $id_cart;
public $id_point;
public $id_customer;
public $firstname;
public $lastname;
public $company;
public $telephone;
public $email;
public $type;
public $libelle;
public $indice;
public $postcode;
public $city;
public $country;
public $address1;
public $address2;
public $lieudit;
public $informations;
protected $database_mapping_fields = array(
'id' => array('prid', 'point_id'),
'id_order' => array('order_id'),
'id_cart' => array('id_cart', 'cart_id'),
'id_point' => array('prid', 'point_id'),
'id_customer' => array('id_customer', 'customer_id'),
'firstname' => array('cefirstname', 'firstname'),
'lastname' => array('cename', 'lastname'),
'company' => array('cecompanyname', 'company'),
'telephone' => array('cephonenumber', 'telephone'),
'email' => array('ceemail', 'email'),
'type' => array('delivery_mode', 'type'),
'libelle' => array('prname', 'libelle'),
'indice' => array('prcompladress', 'indice'),
'postcode' => array('przipcode', 'code_postal'),
'city' => array('prtown', 'commune'),
'country' => array('cecountry', 'pays'),
'address1' => array('pradress1', 'adresse1'),
'address2' => array('pradress2', 'adresse2'),
'lieudit' => array('pradress3', 'lieudit'),
'informations' => array('cedeliveryinformation', 'informations'),
);
public $validation = array(
'company' => '/[<>=#{}]*/',
'firstname' => '/[0-9!<>,;?=+()@#"°{}_$%:]*/',
'lastname' => '/[0-9!<>,;?=+()@#"°{}_$%:]*/',
'address1' => '/[!<>?=+@{}_$%]*/',
'address2' => '/[!<>?=+@{}_$%]*/',
'postcode' => '/[^a-zA-Z 0-9-]/',
'city' => '/[!<>;?=+@#"°{}_$%]*/',
'phone' => '/[a-zA-Z^%$#@!-]/',
'phone_mobile' => '/[a-zA-Z^%$#@!-]/'
);
public function __construct($id_cart = null, $id_customer = null)
{
$this->mode = max(1, (int)Configuration::get('SOFLEXIBILITE_MODE')) == 1 ?
self::MODE_PRESTASHOP : self::MODE_THIRDPARTY;
if ($id_cart) {
$this->id_cart = (int)$id_cart;
}
if ($id_customer) {
$this->id_customer = (int)$id_customer;
}
}
public function loadDelivery()
{
if (!$this->isInitSoFlexibiliteDelivery()) {
return (false);
}
$id_cart = 'id_cart';
$id_customer = 'id_customer';
$table_name = 'socolissimo_delivery_info';
if ($this->mode == self::MODE_THIRDPARTY) {
$id_cart = 'cart_id';
$id_customer = 'customer_id';
$table_name = 'so_delivery';
}
if (!$this->tableExists($table_name)) {
return (false);
}
$result = Db::getInstance()->getRow(
'SELECT *
FROM `'._DB_PREFIX_.$table_name.'`
WHERE `'.$id_cart.'` = '.(int)$this->id_cart.'
AND `'.$id_customer.'` = '.(int)$this->id_customer
);
if (!isset($result[$id_cart]) || !(int)$result[$id_cart]) {
return (false);
}
foreach ($this->database_mapping_fields as $k => $arr) {
foreach ($result as $idx => $val) {
if (in_array($idx, $arr)) {
$this->{$k} = $val;
}
}
}
return (true);
}
public function isLoadedObject()
{
return ($this instanceof SoFlexibiliteDelivery && $this->id_cart && $this->id_customer);
}
public function createAddressForPickupPoint($id_address_delivery = null)
{
if (!$this->isInitSoFlexibiliteDelivery()) {
return (false);
}
if ($id_address_delivery) {
$customer_address = new Address($id_address_delivery);
} else {
$customer_address = null;
}
$address = new Address();
$address_rules = Address::getValidationRules('Address');
$this->country = ($this->country && Tools::strlen($this->country) === 2) ?
$this->country : Country::getIsoById($customer_address->id_country);
$this->id_customer = $this->id_customer ? $this->id_customer : $customer_address->id_customer;
//$address->id_country = (int)Country::getByIso($this->country);
$address->id_country = strtolower($this->country) == 'fr'? 8: (int)Country::getByIso($this->country);
$address->id_customer = (int)$this->id_customer;
$address->firstname = $customer_address->firstname;
$address->lastname = $customer_address->lastname;
$address->company = $this->libelle;
$address->address1 = $this->address1 ? $this->address1 : $customer_address->address1;
$address->address2 = $this->address2;
$address->postcode = $this->postcode ? $this->postcode : $customer_address->postcode;
$address->city = $this->city ? $this->city : $customer_address->city;
$address->phone_mobile = $this->telephone ? $this->telephone : $customer_address->phone_mobile;
$address->phone = $this->telephone ? $this->telephone : $customer_address->phone;
if (!$address->phone_mobile) {
$address->phone_mobile = '0661123456';
}
$address_fields = array(
'company',
'firstname',
'lastname',
'address1',
'address2',
'postcode',
'city',
'phone',
'phone_mobile'
);
foreach ($address_fields as $field) {
$address->{$field} = preg_replace($this->validation[$field], '', $address->{$field});
if (isset($address_rules['required'][$field]) && $address_rules['required'][$field]) {
if (empty($address->{$field})) {
$address->{$field} = '-';
}
}
if (isset($address_rules['size'][$field]) && $address_rules['size'][$field]) {
$address->{$field} = Tools::substr($address->{$field}, 0, $address_rules['size'][$field]);
}
}
$address->alias = 'So Colissimo - '.date('d-m-Y');
$address->active = 1;
$address->deleted = 1;
$address->add();
if (!Validate::isLoadedObject($address)) {
return (false);
}
return ((int)$address->id);
}
public function tableExists($table_name)
{
try {
$result = Db::getInstance()->ExecuteS('SHOW TABLES LIKE "'._DB_PREFIX_.pSQL($table_name).'"');
} catch (Exception $e) {
return (false);
}
if (!count($result)) {
return (false);
}
try {
$result = Db::getInstance()->ExecuteS('SHOW COLUMNS FROM `'._DB_PREFIX_.pSQL($table_name).'`');
} catch (Exception $e) {
return (false);
}
if (!count($result)) {
return (false);
}
return (true);
}
public function isInitSoFlexibiliteDelivery()
{
return ($this instanceof SoFlexibiliteDelivery && $this->id_cart && $this->id_customer);
}
public function removeDelivery($id_cart_to_delete)
{
if (!$id_cart_to_delete || !is_numeric($id_cart_to_delete)) {
return false;
}
$id_cart = 'id_cart';
$table_name = 'socolissimo_delivery_info';
if ($this->mode == self::MODE_THIRDPARTY) {
$id_cart = 'cart_id';
$table_name = 'so_delivery';
}
return (Db::getInstance()->delete(_DB_PREFIX_.$table_name, '`'.$id_cart.'` = '.(int)$id_cart_to_delete));
}
public function saveDelivery()
{
$this->installDeliveryTable();
if ($this->existsDelivery()) {
$result = $this->updateDelivery();
} else {
$result = $this->createDelivery();
}
return ($result);
}
public function updateDelivery()
{
if (!$this->isInitSoFlexibiliteDelivery()) {
return (false);
}
if ($this->mode == self::MODE_PRESTASHOP) {
$table_name = 'socolissimo_delivery_info';
$where = '`id_cart` = '.(int)$this->id_cart;
$values = array(
'id_cart' => (int)$this->id_cart,
'id_customer' => (int)$this->id_customer,
'delivery_mode' => pSQL($this->type),
'prid' => pSQL($this->id_point),
'prname' => pSQL($this->libelle),
// pSQL($this->lastname),
'prfirstname' => pSQL($this->firstname),
'prcompladress' => pSQL($this->indice),
'pradress4' => null,
'przipcode' => pSQL($this->postcode),
'prtown' => pSQL($this->city),
'cephonenumber' => pSQL(str_replace(' ', '', $this->telephone)),
'ceemail' => pSQL($this->email),
'cecompanyname' => pSQL($this->company),
'cedeliveryinformation' => pSQL($this->informations),
'cedoorcode1' => null,
'cedoorcode2' => null,
'cename' => pSQL($this->lastname),
'cefirstname' => pSQL($this->firstname),
'cecountry' => pSQL($this->country),
'codereseau' => in_array($this->type, array('CMT', 'BDP', 'BOM', 'BOS')) ? 'R12' : ''
);
if (in_array($this->type, array('DOM', 'DOS', 'RDV'))) {
$values['pradress1'] = pSQL($this->address2);
$values['pradress2'] = pSQL($this->lieudit);
$values['pradress3'] = pSQL($this->address1);
$values['prname'] = pSQL($this->lastname);
} else {
$values['pradress1'] = pSQL($this->address1);
$values['pradress2'] = pSQL($this->address2);
$values['pradress3'] = pSQL($this->lieudit);
}
} else {
$table_name = 'so_delivery';
$where = '`cart_id` = '.(int)$this->id_cart;
$values = array(
'cart_id' => (int)$this->id_cart,
'order_id' => (int)$this->id_order,
'customer_id' => (int)$this->id_customer,
'type' => pSQL($this->type),
'point_id' => pSQL($this->id_point),
'libelle' => pSQL($this->libelle),
'firstname' => pSQL($this->firstname),
'lastname' => pSQL($this->lastname),
'indice' => pSQL($this->indice),
'adresse1' => pSQL($this->address1),
'adresse2' => pSQL($this->address2),
'lieudit' => pSQL($this->lieudit),
'code_postal' => pSQL($this->postcode),
'commune' => pSQL($this->city),
'pays' => in_array($this->country, array('Belgique', 'BE')) ? 'BE' : 'FR',
'telephone' => pSQL(str_replace(' ', '', $this->telephone)),
'email' => pSQL($this->email),
'company' => pSQL($this->company),
'codereseau' => in_array($this->type, array('CMT', 'BDP', 'BOM', 'BOS')) ? 'R12' : ''
);
if (in_array($values['type'], array('DOM', 'DOS')) && $values['pays'] == 'BE') {
$values['codereseau'] = 'R12';
}
}
if (version_compare(_PS_VERSION_, '1.5', '>=')) {
$result = Db::getInstance()->update($table_name, $values, $where);
} else {
$result = Db::getInstance()->autoExecute(_DB_PREFIX_.$table_name, $values, 'UPDATE', $where);
}
return ($result);
}
public function createDelivery()
{
if (!$this->isInitSoFlexibiliteDelivery()) {
return (false);
}
if ($this->mode == self::MODE_PRESTASHOP) {
$table_name = 'socolissimo_delivery_info';
$values = array(
'id_cart' => (int)$this->id_cart,
'id_customer' => (int)$this->id_customer,
'delivery_mode' => pSQL($this->type),
'prid' => pSQL($this->id_point),
'prname' => pSQL($this->libelle),
// pSQL($this->lastname),
'prfirstname' => pSQL($this->firstname),
'prcompladress' => pSQL($this->indice),
'pradress4' => null,
'przipcode' => pSQL($this->postcode),
'prtown' => pSQL($this->city),
'cephonenumber' => pSQL(str_replace(' ', '', $this->telephone)),
'ceemail' => pSQL($this->email),
'cecompanyname' => pSQL($this->company),
'cedeliveryinformation' => null,
'cedoorcode1' => null,
'cedoorcode2' => null,
'cename' => pSQL($this->lastname),
'cefirstname' => pSQL($this->firstname),
'cecountry' => pSQL($this->country),
'codereseau' => in_array($this->type, array('CMT', 'BDP', 'BOM', 'BOS')) ? 'R12' : ''
);
if (in_array($this->type, array('DOM', 'DOS')) && $values['cecountry'] == 'BE') {
$values['codereseau'] = 'R12';
$values['delivery_mode'] = ($this->type == 'DOM') ? 'BOM' : 'BOS';
}
if ($this->type == 'CMT' && in_array($this->country, array('DE', 'ES', 'GB', 'LU', 'NL'))) {
$values['codereseau'] = 'R09';
} elseif (in_array($this->type, array('BDP', 'PCS')) && in_array($this->country, array('DE', 'ES', 'NL'))) {
$values['codereseau'] = 'X00';
}
if (in_array($this->type, array('DOM', 'DOS'))) {
$values['pradress1'] = pSQL($this->address2);
$values['pradress2'] = pSQL($this->lieudit);
$values['pradress3'] = pSQL($this->address1);
$values['prname'] = pSQL($this->lastname);
} else {
$values['pradress1'] = pSQL($this->address1);
$values['pradress2'] = pSQL($this->address2);
$values['pradress3'] = pSQL($this->lieudit);
}
} else {
$table_name = 'so_delivery';
$values = array(
'cart_id' => (int)$this->id_cart,
'order_id' => -(int)time(),
'customer_id' => (int)$this->id_customer,
'type' => pSQL($this->type),
'point_id' => pSQL($this->id_point),
'libelle' => pSQL($this->libelle),
'firstname' => pSQL($this->firstname),
'lastname' => pSQL($this->lastname),
'indice' => pSQL($this->indice),
'adresse1' => pSQL($this->address1),
'adresse2' => pSQL($this->address2),
'lieudit' => pSQL($this->lieudit),
'code_postal' => pSQL($this->postcode),
'commune' => pSQL($this->city),
'pays' => $this->country,
'telephone' => pSQL(str_replace(' ', '', $this->telephone)),
'email' => pSQL($this->email),
'company' => pSQL($this->company),
'codereseau' => in_array($this->type, array('CMT', 'BDP', 'BOM', 'BOS')) ? 'R12' : ''
);
if (in_array($values['type'], array('DOM', 'DOS')) && $values['pays'] == 'BE') {
$values['codereseau'] = 'R12';
$values['type'] = ($this->type == 'DOM') ? 'BOM' : 'BOS';
}
if ($this->type == 'CMT' && in_array($this->country, array('DE', 'ES', 'GB', 'LU', 'NL'))) {
$values['codereseau'] = 'R09';
} elseif (in_array($this->type, array('BDP', 'PCS')) && in_array($this->country, array('DE', 'ES', 'NL'))) {
$values['codereseau'] = 'X00';
}
}
if (version_compare(_PS_VERSION_, '1.5', '>=')) {
$result = Db::getInstance()->insert($table_name, $values);
} else {
$result = Db::getInstance()->autoExecute(_DB_PREFIX_.$table_name, $values, 'INSERT');
}
return ($result);
}
public function existsDelivery()
{
$id_cart = 'id_cart';
$id_customer = 'id_customer';
$table_name = 'socolissimo_delivery_info';
if ($this->mode == self::MODE_THIRDPARTY) {
$id_cart = 'cart_id';
$id_customer = 'customer_id';
$table_name = 'so_delivery';
}
if ($this->isInitSoFlexibiliteDelivery()) {
$result = Db::getInstance()->getRow(
'SELECT `'.$id_cart.'`
FROM `'._DB_PREFIX_.$table_name.'`
WHERE `'.$id_cart.'` = '.(int)$this->id_cart.'
AND `'.$id_customer.'` = '.(int)$this->id_customer
);
return (isset($result[$id_cart]) && (int)$result[$id_cart]);
}
return (false);
}
public function installDeliveryTable()
{
$pass = true;
$tables = array();
$query = Db::getInstance()->ExecuteS('SHOW TABLES');
foreach ($query as $rows) {
foreach ($rows as $table) {
$tables[$table] = 1;
}
}
if ($this->mode == self::MODE_PRESTASHOP) {
if (isset($tables[_DB_PREFIX_.'socolissimo_delivery_info'])) {
$pass = true;
$sqls = array();
$fields = array();
$query = Db::getInstance()->ExecuteS('SHOW COLUMNS FROM `'._DB_PREFIX_.'socolissimo_delivery_info`');
// Check existing field
if ($query) {
foreach ($query as $row) {
if ($row['Field'] === 'cephonenumber' &&
filter_var($row['Type'], FILTER_SANITIZE_NUMBER_INT) < 15
) {
Db::getInstance()->execute(
'ALTER TABLE `'._DB_PREFIX_.'socolissimo_delivery_info`
MODIFY `cephonenumber` VARCHAR(15)'
);
}
}
}
if ($query) {
foreach ($query as $row) {
$fields[$row['Field']] = 1;
}
}
// Add new field sample
if (!isset($fields['codereseau'])) {
$sqls[] =
'ALTER TABLE `'._DB_PREFIX_.'socolissimo_delivery_info`
ADD `codereseau` VARCHAR(3) NOT NULL AFTER `cedoorcode2`';
}
if (!isset($fields['cename'])) {
$sqls[] =
'ALTER TABLE `'._DB_PREFIX_.'socolissimo_delivery_info`
ADD `cename` VARCHAR(64) DEFAULT NULL';
}
if (!isset($fields['cefirstname'])) {
$sqls[] =
'ALTER TABLE `'._DB_PREFIX_.'socolissimo_delivery_info`
ADD `cefirstname` VARCHAR(64) DEFAULT NULL';
}
if (!isset($fields['cecountry'])) {
$sqls[] =
'ALTER TABLE `'._DB_PREFIX_.'socolissimo_delivery_info`
ADD `cecountry` VARCHAR(10) DEFAULT NULL AFTER `prtown`';
}
if (count($sqls)) {
foreach ($sqls as $sql) {
$pass = Db::getInstance()->Execute($sql) && $pass;
}
}
} else {
$sql = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'socolissimo_delivery_info` (
`id_cart` INT(10) NOT NULL,
`id_customer` INT(10) NOT NULL,
`delivery_mode` VARCHAR(3) NOT NULL,
`prid` TEXT(10) NOT NULL,
`prname` VARCHAR(64) NOT NULL,
`prfirstname` VARCHAR(64) NOT NULL,
`prcompladress` TEXT NOT NULL,
`pradress1` TEXT NOT NULL,
`pradress2` TEXT NOT NULL,
`pradress3` TEXT NOT NULL,
`pradress4` TEXT NOT NULL,
`przipcode` TEXT(10) NOT NULL,
`prtown` VARCHAR(64) NOT NULL,
`cecountry` VARCHAR(10) NOT NULL,
`cephonenumber` VARCHAR(15) NOT NULL,
`ceemail` VARCHAR(64) NOT NULL,
`cecompanyname` VARCHAR(64) NOT NULL,
`cedeliveryinformation` TEXT NOT NULL,
`cedoorcode1` VARCHAR(10) NOT NULL,
`cedoorcode2` VARCHAR(10) NOT NULL,
`codereseau` VARCHAR(3) DEFAULT NULL,
`cename` VARCHAR(64) DEFAULT NULL,
`cefirstname` VARCHAR(64) DEFAULT NULL,
PRIMARY KEY (`id_cart`,`id_customer`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;';
$pass = Db::getInstance()->execute($sql);
}
} else {
$pass = true;
// Laisser les champs de type TEXT a la fin pour des raisons d'optimisation SQL
$sql = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'so_delivery`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) DEFAULT NULL,
`cart_id` int(11) DEFAULT NULL,
`point_id` int(11) NOT NULL,
`customer_id` int(11) NOT NULL,
`firstname` varchar(38) DEFAULT NULL,
`lastname` varchar(38) DEFAULT NULL,
`company` varchar(38) DEFAULT NULL,
`telephone` varchar(10) DEFAULT NULL,
`email` varchar(64) DEFAULT NULL,
`type` varchar(3) DEFAULT NULL,
`libelle` varchar(50) DEFAULT NULL,
`indice` varchar(70) DEFAULT NULL,
`code_postal` varchar(5) DEFAULT NULL,
`commune` varchar(32) DEFAULT NULL,
`pays` varchar(32) NOT NULL,
`adresse1` varchar(38) DEFAULT NULL,
`adresse2` varchar(38) DEFAULT NULL,
`lieudit` varchar(38) DEFAULT NULL,
`codereseau` varchar(3) DEFAULT NULL,
`informations` text,
PRIMARY KEY (`id`),
UNIQUE KEY `u_order_id` (`order_id`),
UNIQUE KEY `u_cart_id` (`cart_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=10000000 ;';
if (!Db::getInstance()->Execute($sql)) {
return false;
}
$sqls = array();
// Alter the table to reflect new approach and add some
// REFIX: Add Fields
$query = Db::getInstance()->ExecuteS('SHOW COLUMNS FROM `'._DB_PREFIX_.'so_delivery`');
$fields = array();
foreach ($query as $row) {
$fields[$row['Field']] = 1;
}
if (!isset($fields['cart_id'])) {
$sqls[] = sprintf(
'ALTER TABLE `%s` ADD `cart_id` int(11) NULL AFTER `order_id`',
_DB_PREFIX_.'so_delivery'
);
}
if (!isset($fields['pays'])) {
$sqls[] = sprintf(
'ALTER TABLE `%s` ADD `pays` int(11) NULL AFTER `commune`',
_DB_PREFIX_.'so_delivery'
);
}
if (!isset($fields['codereseau'])) {
$sqls[] = sprintf(
'ALTER TABLE `%s` ADD `codereseau` varchar(3) DEFAULT NULL',
_DB_PREFIX_.'so_delivery'
);
}
// REFIX: Add Indexes
$query = Db::getInstance()->ExecuteS('SHOW INDEX FROM `'._DB_PREFIX_.'so_delivery`');
$fields = array();
foreach ($query as $row) {
$fields[$row['Key_name']] = 1;
}
if (!isset($fields['u_order_id'])) {
$sqls[] = sprintf(
'ALTER TABLE `%s` ADD UNIQUE `u_order_id` (`order_id`)',
_DB_PREFIX_.$this->_delivery
);
}
if (!isset($fields['u_order_id'])) {
$sqls[] = sprintf(
'ALTER TABLE `%s` ADD UNIQUE `u_cart_id` (`cart_id`)',
_DB_PREFIX_.$this->_delivery
);
}
// REFIX: Change Fields
$sqls[] = sprintf(
'ALTER TABLE `%s` CHANGE `order_id` `order_id` int(11) NULL',
_DB_PREFIX_.'so_delivery'
);
if ($sqls) {
foreach ($sqls as $sql) {
try {
if (!Db::getInstance()->Execute($sql)) {
$pass = false;
}
} catch (Exception $e) {
$pass = false;
echo $e->getMessage();
}
}
}
}
return $pass;
}
}