bebeboutik/override/classes/ProductSale.php

109 lines
4.1 KiB
PHP
Raw Normal View History

2016-01-04 12:48:08 +01:00
<?php
class ProductSale extends ProductSaleCore
{
public static function getBestSalesVp($id_lang, $pageNumber = 0, $nbProducts = 10, $orderBy=NULL, $orderWay=NULL)
{
$id_category_vp = self::_getIdCategoryPrivateSales();
// $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']);
$children = Category::getChildren($sale['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),
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;
$data = Product::getProductsProperties($id_lang, $sale_product, false);
$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 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);
}
}