Serveur preprod f0c0c48223 first push
2016-04-14 16:14:31 +02:00

978 lines
25 KiB
PHP

<?php
/**
* Description of SaleCore
*
* @company Antadis
*/
class SaleCore extends ObjectModel{
const STATE_CURRENT = 'current';
const STATE_FUTURE = 'future';
const STATE_PAST = 'past';
const STATE_ARCHIVED = 'archived';
const STATE_HOTSPOT = 'hotspot';
private static $array_state = array(self::STATE_CURRENT, self::STATE_FUTURE, self::STATE_PAST, self::STATE_ARCHIVED, self::STATE_HOTSPOT);
public $id_privatesales;
public $date_start;
public $date_end;
public $date_start_hotspot;
public $date_end_hotspot;
public $date_shipping;
public $active;
public $univers;
public $id_category;
public $title;
public $subtitle;
public $description;
public $description_newsletter;
public $percent = null;
public $image;
public $imagehotspot;
public $imagefuture;
public $imagepast;
public $imagetrailer;
public $imagenewsletter;
public $imagenewsletter_new;
public $imagelogo;
public $imageflashsale;
public $archived;
public $flash;
public $news;
public $alert;
public $position;
public $id_groups;
public $id_brands;
public $link;
public $date_add;
public $date_upd;
public $picto;
public $picto_category;
public $otherdelay;
public $otherdelay_title;
public $extratab;
public $extratab_title;
public $extratab_text;
public $concours;
public $concours_delay;
public $products = null;
public $category = null;
/**
*
* @var type
*/
public static $definition = array(
'table' => 'privatesales',
'primary' => 'id_privatesales',
'multilang' => true,
'fields' => array(
'id_category' => array(
'type' => ObjectModel::TYPE_INT,
'required' => TRUE,
'validate' => 'isInt'
),
'position' => array(
'type' => ObjectModel::TYPE_INT,
'validate' => 'isInt'
),
'percent' => array(
'type' => ObjectModel::TYPE_INT,
),
'active' => array(
'type' => ObjectModel::TYPE_INT,
'validate' => 'isBool'
),
'univers' => array(
'type' => ObjectModel::TYPE_INT,
'validate' => 'isBool'
),
'archived' => array(
'type' => ObjectModel::TYPE_INT,
'validate' => 'isBool'
),
'flash' => array(
'type' => ObjectModel::TYPE_INT,
'validate' => 'isBool'
),
'news' => array(
'type' => ObjectModel::TYPE_INT,
'validate' => 'isBool'
),
'alert' => array(
'type' => ObjectModel::TYPE_INT,
'validate' => 'isBool'
),
'title' => array(
'type' => ObjectModel::TYPE_STRING,
'lang' => true,
'validate' => 'isString',
'required' => TRUE
),
'subtitle' => array(
'type' => ObjectModel::TYPE_STRING,
'lang' => true,
'validate' => 'isString'
),
'description' => array(
'type' => ObjectModel::TYPE_STRING,
'lang' => true,
'validate' => 'isString'
),
'description_newsletter' => array(
'type' => ObjectModel::TYPE_STRING,
'lang' => true,
'validate' => 'isString'
),
'date_shipping' => array(
'type' => ObjectModel::TYPE_DATE,
'required' => TRUE,
'validate' => 'isDate'
),
'date_start_hotspot' => array(
'type' => ObjectModel::TYPE_DATE,
'validate' => 'isDateFormat'
),
'date_end_hotspot' => array(
'type' => ObjectModel::TYPE_DATE,
'validate' => 'isDateFormat'
),
'date_start' => array(
'type' => ObjectModel::TYPE_DATE,
'required' => TRUE,
'validate' => 'isDate'
),
'date_end' => array(
'type' => ObjectModel::TYPE_DATE,
'required' => TRUE,
'validate' => 'isDate'
),
'picto' => array(
'type' => ObjectModel::TYPE_BOOL,
'validate' => 'isBool'
),
'picto_category' => array(
'type' => ObjectModel::TYPE_BOOL,
'validate' => 'isBool'
),
'otherdelay' => array(
'type' => ObjectModel::TYPE_BOOL,
'validate' => 'isBool'
),
'otherdelay_title' => array(
'type' => ObjectModel::TYPE_STRING,
'lang' => true,
'validate' => 'isString'
),
'extratab' => array(
'type' => ObjectModel::TYPE_BOOL,
'validate' => 'isBool'
),
'extratab_title' => array(
'type' => ObjectModel::TYPE_STRING,
'lang' => true,
'validate' => 'isString'
),
'extratab_text' => array(
'type' => self::TYPE_HTML,
'lang' => true,
'validate' => 'isCleanHtml'
),
'concours' => array(
'type' => ObjectModel::TYPE_BOOL,
'validate' => 'isBool'
),
'concours_delay' => array(
'type' => ObjectModel::TYPE_INT,
'validate' => 'isUnsignedInt'
),
'date_add' => array(
'type' => ObjectModel::TYPE_DATE
),
'date_upd' => array(
'type' => ObjectModel::TYPE_DATE
)
)
);
/**
*
*/
public function __construct($id = null, $id_lang = null, $id_shop = null){
parent::__construct($id, $id_lang, $id_shop);
if(Shop::isFeatureActive())
Context::getContext()->shop->addTableAssociation('privatesales', array('type' => 'shop'));
if (Validate::isLoadedObject($this))
$this->initExtra($id_lang);
}
public function decodeNewsletterContent()
{
if(is_array($this->description_newsletter))
foreach($this->description_newsletter as $k => $v)
$this->description_newsletter[$k] = html_entity_decode($v);
else
$this->description_newsletter = html_entity_decode($this->description_newsletter);
}
public function encodeNewsletterContent()
{
if(is_array($this->description_newsletter))
foreach($this->description_newsletter as $k => $v)
$this->description_newsletter[$k] = htmlentities($v);
else
$this->description_newsletter = htmlentities($this->description_newsletter);
}
public function add($autodate = true, $null_values = false)
{
$this->encodeNewsletterContent();
$add = parent::add($autodate, $null_values);
if($add){
$hist = new SaleHistorique();
$hist->id_privatesales = $this->id ;
$hist->date_start = $this->date_start ;
$hist->date_end = $this->date_end ;
$hist->date_add = $this->date_add ;
$hist->add();
}
return $add;
}
public function update($null_values = false)
{
$this->encodeNewsletterContent();
$oldSale = new SaleCore($this->id);
$update = parent::update($null_values);
if($update && ($this->date_start != $oldSale->date_start || $this->date_end != $oldSale->date_end ) ){
$hist = new SaleHistorique();
$hist->id_privatesales = $this->id ;
$hist->date_start = $this->date_start ;
$hist->date_end = $this->date_end ;
$hist->date_add = $this->date_update ;
$hist->add();
}
return $update;
}
/**
*
*/
public static function getSales($type = "all", $date_limit = 0, $active = TRUE, $archived = FALSE, $group = TRUE, $order_by = 'position', $order = 'ASC',$id_category = FALSE, $flashsale = FALSE, $newssale = NULL, $getCollection = FALSE){
$collection = new Collection('SaleCore', Context::getContext()->language->id);
if($type == self::STATE_CURRENT){
$collection->where('date_start', '<=', date('Y-m-d H:i:s'));
$collection->where('date_end', '>', date('Y-m-d H:i:s'));
$collection->where('date_end_hotspot', '<', date('Y-m-d H:i:s'));
}else if($type == self::STATE_FUTURE){
$collection->where('date_start', '>', date('Y-m-d H:i:s'));
if($date_limit > 0){
$collection->Sqlwhere('a0.`date_start` <= DATE_ADD(NOW(), INTERVAL ' . (int)$date_limit . ' DAY)');
}
}else if($type == self::STATE_PAST){
$collection->where('date_end', '<', date('Y-m-d H:i:s'));
if($date_limit > 0){
$collection->Sqlwhere('a0.`date_end` >= DATE_SUB(NOW(), INTERVAL ' . (int)$date_limit . ' DAY)');
}
}else if($type == self::STATE_ARCHIVED){
$collection->where('archived', '=', '1');
} else if($type == self::STATE_HOTSPOT){
$collection->where('date_start_hotspot', '<=', date('Y-m-d H:i:s'));
$collection->where('date_end_hotspot', '>', date('Y-m-d H:i:s'));
}
if($active !== null)
$collection->where('active', '=', 1);
if($archived !== null)
$collection->where('archived', '=', 0);
if ( $flashsale )
$collection->where('flash', '=', 1);
else
$collection->where('flash', '=', 0);
if ( $newssale !== null ){
if ( $newssale == 1 )
$collection->where('news', '=', 1);
else if ( $newssale == 2 )
$collection->where('news', '=', 0);
}
if($group){
$collection->Sqlwhere('EXISTS (
SELECT g.id_privatesales
FROM `' . _DB_PREFIX_ . 'privatesales_group` g
WHERE
a0.`id_privatesales` = g.`id_privatesales`
AND
g.id_group IN ('.implode(', ', Customer::getGroupsStatic((int)Context::getContext()->cookie->id_customer)).'))
');
}
if($id_category){
$collection->Sqlwhere('EXISTS (
SELECT pcs.id_privatesales
FROM `' . _DB_PREFIX_ . 'privatesales_category_sales` pcs
WHERE
a0.`id_privatesales` = pcs.`id_privatesales`
AND
pcs.id_privatesales_category IN ('.(int)$id_category.'))
');
}
if($order_by == 'position') {
if($order == 'ASC') {
$collection->sqlOrderBy('(DATE_FORMAT(a0.`date_start`, "%Y-%m-%d") = "'.date('Y-m-d').'") DESC');
$collection->orderBy($order_by,$order);
}
else {
$collection->orderBy($order_by,$order);
$collection->sqlOrderBy('(DATE_FORMAT(a0.`date_start`, "%Y-%m-%d") = "'.date('Y-m-d').'")', 'ASC');
}
}
elseif ($order_by == 'rand'){
$collection->sqlOrderBy('RAND()');
}
else {
$collection->orderBy($order_by,$order);
}
if(!$flashsale){
if ($getCollection)
return $collection;
return $collection->getAll();
}
else{
$result = array();
$sales = $collection->getAll();
foreach ($sales as $sale) {
$category = new Category($sale->id_category);
$product = $category->getProducts(Context::getContext()->cookie->id_lang, 1, 1);
$result[] = array(
'sale' => $sale,
'product' => new Product($product[0]['id_product'],true,Context::getContext()->cookie->id_lang)
);
}
return $result;
}
}
public static function getInSameSaleCategory($id_sales = false,$order_by = 'position', $order = 'ASC'){
if(!$id_sales){
return false;
}
$sale = new SaleCore($id_sales);
$cat_sale = $sale->getSalesCategory();
if($cat_sale){
$collection = new Collection('SaleCore', Context::getContext()->language->id);
$collection->where('date_start', '<=', date('Y-m-d H:i:s'));
$collection->where('date_end', '>', date('Y-m-d H:i:s'));
$collection->where('date_end_hotspot', '<', date('Y-m-d H:i:s'));
$collection->where('id_privatesales', '!=', $id_sales);
$collection->Sqlwhere('EXISTS (
SELECT g.id_privatesales
FROM `' . _DB_PREFIX_ . 'privatesales_group` g
WHERE
a0.`id_privatesales` = g.`id_privatesales`
AND
g.id_group IN ('.implode(', ', Customer::getGroupsStatic((int)Context::getContext()->cookie->id_customer)).'))
');
$collection->Sqlwhere('EXISTS (
SELECT cs.id_privatesales
FROM `' . _DB_PREFIX_ . 'privatesales_category_sales` cs
WHERE
a0.`id_privatesales` = cs.`id_privatesales`
AND
cs.id_privatesales_category IN ('.implode(', ',$cat_sale ).'))
');
$collection->orderBy($order_by,$order);
return $collection->getAll();
}else{
return false;
}
}
/**
*
*/
public static function getSalesAlert(){
$collection = new Collection('SaleCore', Context::getContext()->language->id);
$collection->where('date_start', '>', date('Y-m-d H:i:s'));
$collection->Sqlwhere('a0.`date_start` <= DATE_ADD(NOW(), INTERVAL ' . (int)Configuration::get('PRIVATESALES_SEND_ALERT_LIMIT') . ' MINUTE)');
$collection->where('alert', '=', 0);
return $collection->getAll();
}
/**
*
*/
public function hydrate(array $data, $id_lang = null){
parent::hydrate($data, $id_lang);
$this->initExtra($id_lang);
}
public function initExtra($id_lang){
$this->id_groups = $this->getGroups();
$this->id_brands = $this->getBrands();
$this->decodeNewsletterContent();
$link = new Link();
if ($this->id_lang !== NULL) {
$c = Db::getInstance()->getRow('
SELECT `name`, `id_lang`, `link_rewrite`
FROM `' . _DB_PREFIX_ . 'category_lang`
WHERE `id_category` = ' . (int)$this->id_category . '
AND `id_lang` = ' . (int)$this->id_lang . '
');
$this->name_cat = $c['name'];
$this->link_rewrite = $c['link_rewrite'];
$this->link = $link->getCategoryLink((int)$this->id_category, $c['link_rewrite'], (int)$this->id_lang);
} else {
$this->name_cat = array();
$this->link_rewrite = array();
$this->link = array();
foreach (Db::getInstance()->executeS('
SELECT `name`, `id_lang`, `link_rewrite`
FROM `' . _DB_PREFIX_ . 'category_lang`
WHERE `id_category` = ' . (int)$this->id_category . '
') as $name) {
$this->name_cat[$name['id_lang']] = $name['name'];
$this->link_rewrite[$name['id_lang']] = $name['link_rewrite'];
$this->link[$name['id_lang']] = $link->getCategoryLink((int)$this->id_category, $name['link_rewrite'], (int)$name['id_lang']);
}
}
if(file_exists(_PS_MODULE_DIR_.'privatesales/img/'. $this->id . '/current/'. $this->id . '_'.$id_lang.'.jpg')){
$this->image = true;
}else{
$this->image = false;
}
if(file_exists(_PS_MODULE_DIR_.'privatesales/img/'. $this->id . '/future/'. $this->id . '_'.$id_lang.'.jpg')){
$this->imagefuture = true;
}else{
$this->imagefuture = false;
}
if(file_exists(_PS_MODULE_DIR_.'privatesales/img/'. $this->id . '/past/'. $this->id . '_'.$id_lang.'.jpg')){
$this->imagepast = true;
}else{
$this->imagepast = false;
}
if(file_exists(_PS_MODULE_DIR_.'privatesales/img/'. $this->id . '/trailer/'. $this->id . '_'.$id_lang.'.jpg')){
$this->imagetrailer = true;
}else{
$this->imagetrailer = false;
}
if(file_exists(_PS_MODULE_DIR_.'privatesales/img/'. $this->id . '/newsletter/'. $this->id . '_'.$id_lang.'.jpg')){
$this->imagenewsletter = true;
}else{
$this->imagenewsletter = false;
}
if(file_exists(_PS_MODULE_DIR_.'privatesales/img/'. $this->id . '/newsletter_new/'. $this->id . '_'.$id_lang.'.jpg')){
$this->imagenewsletter_new = true;
}else{
$this->imagenewsletter_new = false;
}
if(file_exists(_PS_MODULE_DIR_.'privatesales/img/'. $this->id . '/logo/'. $this->id . '_'.$id_lang.'.jpg')){
$this->imagelogo = true;
}else{
$this->imagelogo = false;
}
if(file_exists(_PS_MODULE_DIR_.'privatesales/img/'. $this->id . '/flashsale/'. $this->id . '_'.$id_lang.'.jpg')){
$this->imageflashsale = true;
}else{
$this->imageflashsale = false;
}
if(file_exists(_PS_MODULE_DIR_.'privatesales/img/'. $this->id . '/hotspot/'. $this->id . '_'.$id_lang.'.jpg')){
$this->imagehotspot = true;
}else{
$this->imagehotspot = false;
}
}
public function getState()
{
$array_current_state = array();
foreach(self::$array_state as $state)
if($this->isState($state))
$array_current_state[] = $state;
return $array_current_state;
}
public function isState($type = "all", $date = null)
{
if($type == "all")
return true;
if($date === null || !Validate::isDate($date))
$date = date('Y-m-d H:i:s');
switch($type)
{
case self::STATE_CURRENT:
return ( $this->date_start <= $date
&& $this->date_end > $date
&& $this->date_end_hotspot < $date );
break;
case self::STATE_FUTURE:
return $this->date_start > $date;
break;
case self::STATE_PAST:
return $this->date_end < $date;
break;
case self::STATE_ARCHIVED:
return $this->archived == 1;
break;
case self::STATE_HOTSPOT:
return ( $this->date_start_hotspot <= $date
&& $this->date_end_hotspot > $date );
break;
}
}
public function isActive()
{
return $this->active == 1;
}
public function isOpen()
{
if (strtotime($this->date_start) > time() || !$this->active || strtotime($this->date_end) < time()) {
return false;
}
return true;
}
public static function getDir($id_sale, $root = true)
{
if($root === true)
return _PS_MODULE_DIR_.'privatesales/img/';
else
return Context::getContext()->shop->getBaseURL().'modules/privatesales/img/';
}
public function getFileImage($type, $id_lang = null, $root = true)
{
if($id_lang === null)
$id_lang = Context::getContext()->language->id;
$path = self::getDir($this->id, $root);
switch($type)
{
case 'current':
case 'future':
case 'past':
case 'trailer':
case 'newsletter':
case 'newsletter_new':
case 'logo':
case 'flashsale':
case 'hotspot':
return $path. $this->id . DS . $type . DS . $this->id . '_'.$id_lang.'.jpg';
break;
}
}
/**
*
*/
public function getGroups() {
if (!isset($this->groups)) {
$this->groups = array();
foreach (Db::getInstance()->executeS('
SELECT `id_group`
FROM `' . _DB_PREFIX_ . 'privatesales_group`
WHERE `id_privatesales` = ' . $this->id . '
') as $group) {
$this->groups[] = $group['id_group'];
}
}
return $this->groups;
}
public function getBrands()
{
if (!isset($this->brands)) {
$this->brands = array();
foreach (Db::getInstance()->executeS('
SELECT `id_brand`
FROM `' . _DB_PREFIX_ . 'privatesales_brand`
WHERE `id_privatesales` = ' . $this->id . '
') as $brand) {
$this->brands[] = $brand['id_brand'];
}
}
return $this->brands;
}
public function getSalesCategory()
{
if (!isset($this->salescategory)) {
$this->salescategory = array();
foreach (Db::getInstance()->executeS('
SELECT `id_privatesales_category`
FROM `' . _DB_PREFIX_ . 'privatesales_category_sales`
WHERE `id_privatesales` = ' . $this->id . '
') as $salescategory) {
$this->salescategory[] = $salescategory['id_privatesales_category'];
}
}
return $this->salescategory;
}
public function loadCategory()
{
if($this->category === null)
{
$this->category = new Category($this->id_category);
}
return $this->category;
}
public function getAllCategory($id_only = false)
{
$category = $this->loadCategory();
$res = $category->getAllChildren();
$res->offsetSet(null, $this->loadCategory());
if($id_only === true)
{
$ids = array();
foreach($res as $v)
$ids[] = $v->id;
return $ids;
}
return $res->getResults();
}
public function getProducts()
{
if($this->products === null)
{
$categories = $this->getAllCategory(true);
$this->products = array();
if(count($categories) > 0)
{
$products = Db::getInstance()->ExecuteS('
SELECT `id_product`
FROM `' . _DB_PREFIX_ . 'category_product`
WHERE `id_category` IN (' . implode(', ', $categories) . ')
GROUP BY id_product
');
foreach ($products as $product) {
$this->products[] = $product['id_product'];
}
}
}
return $this->products;
}
public function getDefaultProducts()
{
if($this->products === null)
{
$categories = $this->getAllCategory(true);
$this->products = array();
if(count($categories) > 0)
{
$products = Db::getInstance()->ExecuteS('
SELECT `id_product`
FROM `' . _DB_PREFIX_ . 'product`
WHERE `id_category_default` IN (' . implode(', ', $categories) . ')
GROUP BY id_product
');
foreach ($products as $product) {
$this->products[] = $product['id_product'];
}
}
}
return $this->products;
}
public static function getSaleByIdProduct($id_product,$id_lang= NULL){
if ($sales = Db::getInstance()->ExecuteS('
SELECT `id_sale`
FROM `' . _DB_PREFIX_ . 'product_ps_cache`
WHERE `id_product` = ' . (int)$id_product)
) {
foreach ($sales as $key => $sale) {
$ps = new SaleCore((int)$sale['id_sale'],$id_lang);
if ($ps->isOpen()) {
return $ps;
}
}
}
// if ($row = Db::getInstance()->getRow('
// SELECT `id_sale`
// FROM `' . _DB_PREFIX_ . 'product_ps_cache`
// WHERE `id_product` = ' . (int)$id_product)
// ) {
// return new SaleCore((int)$row['id_sale'],$id_lang);
// }
return false;
}
public static function getSalesByIdProduct($id_product,$id_lang= NULL){
$ps = array();
if ($sales = Db::getInstance()->executeS('
SELECT `id_sale`
FROM `' . _DB_PREFIX_ . 'product_ps_cache`
WHERE `id_product` = ' . (int)$id_product)
) {
foreach ($sales as $sale) {
$ps[] = new SaleCore((int)$sale['id_sale'],$id_lang);
}
}
if(!empty($ps)) {
return $ps;
} else {
return false;
}
}
/**
*
*/
public static function getRootCategory($id_sale){
if ($row = Db::getInstance()->getRow('
SELECT `id_category`
FROM `' . _DB_PREFIX_ . 'privatesales`
WHERE `id_privatesales` = ' . (int)$id_sale . '
')
) {
return (int)$row['id_category'];
}
return FALSE;
}
/**
*
*/
public static function getRootCategoryFromCategory($id_category) {
$id_parent = $id_category;
while ((int)$id_parent != (int)Configuration::get('PRIVATESALES_ROOT')) {
$row = Db::getInstance()->getRow('
SELECT `id_parent`
FROM `' . _DB_PREFIX_ . 'category`
WHERE `id_category` = ' . (int)$id_category . '
');
$id_category = $id_parent;
$id_parent = $row['id_parent'];
}
if ($row = Db::getInstance()->getRow('
SELECT `id_category`
FROM `' . _DB_PREFIX_ . 'privatesales`
WHERE `id_category` = ' . (int)$id_category . '
')
) {
return (int)$row['id_category'];
}
return FALSE;
}
/**
*
*/
public static function loadSaleFromCategoryId($id_category, $id_lang = NULL, $id_only = FALSE) {
$id_parent = $id_category;
while ((int)$id_parent != (int)Configuration::get('PRIVATESALES_ROOT') && (int)$id_parent != 0) {
$row = Db::getInstance()->getRow('
SELECT `id_parent`
FROM `' . _DB_PREFIX_ . 'category`
WHERE `id_category` = ' . (int)$id_category . '
');
$id_category = $id_parent;
$id_parent = $row['id_parent'];
}
if ($sale = Db::getInstance()->getRow('
SELECT `id_privatesales`
FROM `' . _DB_PREFIX_ . 'privatesales`
WHERE `id_category` = ' . (int)$id_category . '
')
) {
if ($id_only) {
return (int)$sale['id_privatesales'];
} else {
return new SaleCore((int)$sale['id_privatesales'], $id_lang);
}
}
return FALSE;
}
/**
*
*/
public function getFinishedSales() {
$sales = array();
foreach (Db::getInstance()->ExecuteS('
SELECT `id_privatesales`
FROM `' . _DB_PREFIX_ . 'privatesales`
WHERE `date_end` < NOW()
AND `archived` = 0
AND `enabled` = 1
') as $row) {
$sales[] = (int)$row['id_sale'];
}
return $sales;
}
public function updatePosition($way, $position)
{
$id = Context::getContext()->shop->id;
$id_shop = $id ? $id: Configuration::get('PS_SHOP_DEFAULT');
if (!$res = Db::getInstance()->executeS('
SELECT `id_privatesales`, `position`
FROM `'._DB_PREFIX_.'privatesales`
WHERE `date_start` <= NOW()
AND `date_end` > NOW()
ORDER BY `position` ASC'
))
return false;
foreach ($res as $privatesales)
if ((int)$privatesales['id_privatesales'] == (int)$this->id)
$moved_privatesales = $privatesales;
if (!isset($moved_privatesales) || !isset($position))
return false;
// < and > statements rather than BETWEEN operator
// since BETWEEN is treated differently according to databases
$sql = '
UPDATE `'._DB_PREFIX_.'privatesales`
SET `position`= `position` '.($way ? '- 1' : '+ 1').'
WHERE `date_start` <= NOW()
AND `date_end` > NOW()
AND `position`
'.($way
? '> '.(int)$moved_privatesales['position'].' AND `position` <= '.(int)$position
: '< '.(int)$moved_privatesales['position'].' AND `position` >= '.(int)$position);
$sql2 = '
UPDATE `'._DB_PREFIX_.'privatesales`
SET `position` = '.(int)$position.'
WHERE `date_start` <= NOW()
AND `date_end` > NOW()
AND `id_privatesales` = '.(int)$moved_privatesales['id_privatesales'];
return (Db::getInstance()->execute($sql)
&& Db::getInstance()->execute($sql2));
return $result;
}
public static function cleanPositions()
{
$return = true;
$result = Db::getInstance()->executeS('
SELECT `id_privatesales`
FROM `'._DB_PREFIX_.'privatesales`
WHERE `date_start` <= NOW()
AND `date_end` > NOW()
ORDER BY `position`
');
$total = count($result);
for ($i = 0; $i < $total; $i++){
$sql = '
UPDATE `'._DB_PREFIX_.'privatesales`
SET `position` = '.(int)($i+1).'
WHERE `id_privatesales` = '.(int)$result[$i]['id_privatesales'];
$return &= Db::getInstance()->execute($sql);
}
return $return;
}
public function getAlertCustomers(){
$customer = array();
if ($row = Db::getInstance()->getRow('
SELECT `id_user`
FROM `' . _DB_PREFIX_ . 'privatesales_alertmail`
WHERE `id_privatesales` = ' . (int)$this->id_privatesales . '
')
) {
$customer[] = $row['id_user'];
}
foreach($this->getSalesCategory() as $category){
if ($row = Db::getInstance()->getRow('
SELECT `id_user`
FROM `' . _DB_PREFIX_ . 'privatesales_alertmail_category`
WHERE `id_privatesales_category` = ' . (int)$category . '
')
) {
$customer[] = $row['id_user'];
}
}
return array_unique($customer);
}
public static function getHistoriqueStatic($id_sale,$date_start = null, $date_end = null){
if($id_sale) {
$collection = new Collection('SaleHistorique');
$collection->where('id_privatesales', '=', $id_sale);
if($date_start){
$collection->where('date_start', '>=', $date_start);
}
if($date_end){
$collection->where('date_end', '<=', $date_end);
}
return $collection->getAll();
}
}
public function getHistorique($date_start = null, $date_end = null){
return self::getHistoriqueStatic($this->id, $date_start, $date_end);
}
}