bebeboutik/override/classes/ProductSale.php

175 lines
5.9 KiB
PHP

<?php
class ProductSale extends ProductSaleCore
{
public static function getBestSalesInFamilyOf($id_product, $id_sale, $id_lang, $pageNumber = 0, $nbProducts = 10, $orderBy=NULL, $orderWay=NULL, $filterPrice=NULL)
{
$result = self::getBestSalesForCategories(
array(
'categories' => self::_getCategoriesFromCache($id_sale),
'id_lang' => (int)$id_lang,
'page_number' => $pageNumber,
'nb_products' => $nbProducts,
'order_by' => $orderBy,
'order_way' => $orderWay,
'filter_price' => $filterPrice,
'include_category_children' => FALSE
)
);
return $result;
}
public static function getBestSalesVp($id_lang, $pageNumber = 0, $nbProducts = 10, $orderBy=NULL, $orderWay=NULL, $filterPrice=NULL)
{
return self::getBestSalesForCategories(
array(
'categories' => self::_getIdCategoryPrivateSales(),
'id_lang' => $id_lang,
'page_number' => $pageNumber,
'nb_products' => $nbProducts,
'order_by' => $orderBy,
'order_way' => $orderWay,
'filter_price' => $filterPrice,
'include_category_children' => TRUE
)
);
}
private static function getBestSalesForCategories($params)
{
$id_category_vp = $params['categories'];
$id_lang = $params['id_lang'];
$pageNumber = $params['page_number'];
$nbProducts = $params['nb_products'];
$orderBy = $params['order_by'];
$orderWay = $params['order_way'];
$filterPrice = $params['filter_price'];
// $id_category_vp = array_unique($id_category_vp);
if ($pageNumber < 0) $pageNumber = 0;
if ($nbProducts < 1) $nbProducts = 10;
if (empty($orderBy) || $orderBy == 'position') $orderBy = 'sales';
if (empty($orderWay)) $orderWay = 'DESC';
$groups = FrontController::getCurrentCustomerGroups();
$sqlGroups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
$result = array();
foreach ($id_category_vp as $key => $sale) {
$categories = array($sale['id_category']);
if ($params['include_category_children']) {
$children = Category::getChildren($id_category , 1);
if(count($children) > 0){
foreach ($children as $key => $child) {
$categories[] = $child['id_category'];
}
}
}
$sale_product = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT
DISTINCT(p.id_product), p.out_of_stock, p.id_category_default, p.ean13,
pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, m.`name` AS manufacturer_name, p.`id_manufacturer` as id_manufacturer,
i.`id_image`, il.`legend`,
ps.`quantity` AS sales, t.`rate`, pl.`meta_keywords`, pl.`meta_title`, pl.`meta_description`,
DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 AS new
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`)
LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group`
AND tr.`id_country` = '.(int)Country::getDefaultCountryId().'
AND tr.`id_state` = 0)
LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`)
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_product` = p.`id_product`)
LEFT JOIN `'._DB_PREFIX_.'product_sale` ps ON (ps.`id_product` = p.`id_product`)
WHERE p.`active` = 1
AND p.`quantity` > 0
AND p.id_product = (
SELECT ps.id_product
FROM `'._DB_PREFIX_.'product_sale` ps
INNER JOIN `'._DB_PREFIX_.'category_product` cp ON ( cp.id_product = ps.id_product )
WHERE cp.id_category IN ('. implode(',', $categories).')
ORDER BY ps.quantity DESC
LIMIT 1
)
');
if (!$sale_product)
continue;
if (self::alreadyInArray($sale_product[0]['id_product'], $result)) {
continue;
}
$data = Product::getProductsProperties($id_lang, $sale_product, false);
if (isset($filterPrice) && is_int($filterPrice) && $data[0]['price']<$filterPrice) {
continue;
}
$result[] = $data[0];
unset($categories);
}
$final_array = self::bubble_sort($result);
$final_array = array_reverse($final_array);
return array_slice($final_array,0,$nbProducts);
}
public static function alreadyInArray($id_product, $result)
{
foreach ($result as $row) {
if ($row['id_product'] == $id_product) {
return TRUE;
}
}
return FALSE;
}
public static function bubble_sort($array)
{
$i = count($array);
if ($i <= 0) return false;
$ok = false;
do
{
$ok = false;
for($j=$i-1; $j!=0; $j--)
{
if ($array[$j]['sales'] < $array[$j-1]['sales'])
{
$tmp = $array[$j];
$array[$j] = $array[$j-1];
$array[$j-1] = $tmp;
$ok = true;
}
}
}
while($ok);
return $array;
}
private static function _getIdCategoryPrivateSales()
{
global $site_version_front;
$query = '
SELECT p.`id_category`
FROM `'._DB_PREFIX_.'privatesale` p
LEFT JOIN `'._DB_PREFIX_.'privatesale_site_version` v ON v.`id_sale` = p.`id_sale`
WHERE p.`date_start` <= NOW()
AND p.`date_end` >= NOW()
AND v.`version` = "'.pSql($site_version_front).'"
';
return $sales = Db::getInstance()->ExecuteS($query);
}
private static function _getCategoriesFromCache($id_sale)
{
$query = '
SELECT `id_category`
FROM `'._DB_PREFIX_.'privatesale_category`
WHERE `id_sale` = '.pSql($id_sale);
return Db::getInstance()->ExecuteS($query);
}
}