roykin/modules/be2bill/classes/Be2billOrderReference.php
Serveur preprod be83006fb6 push dev
2016-06-01 11:53:40 +02:00

174 lines
5.0 KiB
PHP

<?php
/**
* 1997-2016 Quadra Informatique
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0) that is available
* through the world-wide-web at this URL: http://www.opensource.org/licenses/OSL-3.0
* If you are unable to obtain it through the world-wide-web, please send an email
* to modules@quadra-informatique.fr so we can send you a copy immediately.
*
* @author Quadra Informatique <modules@quadra-informatique.fr>
* @copyright 1997-2016 Quadra Informatique
* @license http://www.opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
class Be2billOrderReference extends ObjectModel
{
/**
* Object Cart ID
* @var integer
*/
public $id_cart;
/**
* Be2bill Order Reference
* @var string
*/
public $reference;
/**
* @see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'b2b_order_reference',
'primary' => 'id_cart',
'fields' => array(
'id_cart' => array(
'type' => self::TYPE_INT,
'validate' => 'isInt',
'required' => true
),
'reference' => array(
'type' => self::TYPE_STRING,
'required' => true
)
)
);
/**
* Create Order Reference table
* @return boolean
*/
public static function createBe2billOrderReferenceTable()
{
$sql = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'b2b_order_reference` (
`id_cart` INT(10) NOT NULL,
`reference` VARCHAR(128) NOT NULL,
PRIMARY KEY (`id_cart`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8';
return Db::getInstance()->execute($sql);
}
/**
* Add the reference at one cart
* @return the reference created
*/
public static function addCartReference($id_cart, $reference)
{
$already_add = self::getReferenceByCartId((int)$id_cart);
if (empty($already_add)) {
Db::getInstance()->insert('b2b_order_reference', array(
'id_cart' => pSQL((int)$id_cart),
'reference' => pSQL($reference)));
return $reference;
} else {
return $already_add;
}
}
/**
* Retrieve the Order Reference
* @return string
*/
public static function getReferenceByCartId($id_cart)
{
return Db::getInstance()->getValue('SELECT reference
FROM `'._DB_PREFIX_.'b2b_order_reference`
WHERE id_cart ='.pSQL((int)$id_cart));
}
/**
* Retrieve the cart id
* @return int
*/
public static function getIdCartByReference($reference)
{
return Db::getInstance()->getValue('SELECT id_cart
FROM `'._DB_PREFIX_.'b2b_order_reference`
WHERE reference="'.pSQL($reference).'"');
}
/**
* Return the reference if the order exist
* @return boolean
*/
public static function isOrderByReference($reference)
{
$result = Db::getInstance()->getValue('SELECT reference
FROM `'._DB_PREFIX_.'orders`
WHERE reference="'.pSQL($reference).'"');
// result_b2b purpose is to do the job even if the override isn't installed
$result_b2b = Db::getInstance()->getValue('SELECT id_cart
FROM `'._DB_PREFIX_.'b2b_order_reference`
WHERE reference="'.pSQL($reference).'"');
if (!empty($result)) {
return true;
} elseif (!empty($result_b2b)) {
$result = Db::getInstance()->getValue('SELECT reference
FROM `'._DB_PREFIX_.'orders`
WHERE id_cart="'.pSQL($result_b2b).'"');
if (!empty($result)) {
return true;
}
}
return false;
}
/**
* Return Prestashop Reference By Be2bill Reference
* It's possible thoses two don't match without the override installed
* @return varchar
*/
public static function getReferenceByBe2billReference($reference)
{
$result = Db::getInstance()->getValue('SELECT reference
FROM `'._DB_PREFIX_.'orders`
WHERE reference="'.pSQL($reference).'"');
// result_b2b purpose is to do the job even if the override isn't installed
$result_b2b = Db::getInstance()->getValue('SELECT id_cart
FROM `'._DB_PREFIX_.'b2b_order_reference`
WHERE reference="'.pSQL($reference).'"');
if (!empty($result)) {
return $result;
} elseif (!empty($result_b2b)) {
$result = Db::getInstance()->getValue(
'SELECT reference FROM `'._DB_PREFIX_.'orders` WHERE id_cart="'.(int)$result_b2b.'"'
);
if (!empty($result)) {
return $result;
}
}
return false;
}
/**
* Returns reference array from old tables or false
* @param none
* @return boolean|string
*/
public static function getOldList()
{
return Db::getInstance()->executeS('
SELECT *
FROM `'._DB_PREFIX_.'be2bill_order_reference`
');
}
}