Merge branch 'ticket-9599'

This commit is contained in:
Marion Muszynski 2016-08-25 17:18:51 +02:00
commit 1ed3c9df79
3 changed files with 71 additions and 1 deletions

View File

@ -1831,6 +1831,63 @@ class Sale {
}
return false;
}
/**
* Update the percentage or amount in a promotion string
* for each sale with the maximum reduction percentage or amount
* of its products.
*
* @param string $date_day the sales starting day (to express in "Y-m-d")
*/
public static function updateSalesReductionAtDate()
{
$id_field = 2; // id=2 for the extra field sale "pourcentage"
// for all sales at "date_day" and hour > "from_hour"
// return the maximal reduction applied from all products of each sale
$sql = "SELECT MAX(sp.reduction) as reduc,
s.id_sale, sf.value, sf.id_field, sf.id_lang
FROM `"._DB_PREFIX_."specific_price` sp
JOIN `"._DB_PREFIX_."category_product` cp
ON cp.id_product = sp.id_product
JOIN `"._DB_PREFIX_."privatesale_category` psc
ON psc.id_category = cp.id_category
JOIN `"._DB_PREFIX_."privatesale` s
ON s.id_sale = psc.id_sale
JOIN `"._DB_PREFIX_."privatesale_extrafield_sale` sf
ON sf.id_sale = s.id_sale
WHERE
sp.reduction_type='percentage'
AND CAST(s.date_start AS DATE) = CAST(NOW() AS DATE)
AND sf.id_field = ".$id_field."
AND sf.`value` IS NOT NULL
AND sf.`value` <> ''
GROUP BY s.id_sale, sf.value, sf.id_field, sf.id_lang
";
$results = Db::getInstance()->ExecuteS($sql);
// update the sentence with the max reduction, if any, for
// each sale
foreach ($results as $result) {
$reduction_in_percent = round($result['reduc']*100);
$new_value = preg_replace(
'/\d+\s*%/',
$reduction_in_percent."%",
$result['value']
);
$sql = "UPDATE `"._DB_PREFIX_."privatesale_extrafield_sale`
SET value=\"".$new_value."\"
WHERE id_field = ".$id_field."
AND id_lang = '".$result['id_lang']."'
AND id_sale = '".$result['id_sale']."'
";
Db::getInstance()->Execute($sql);
}
}
}

View File

@ -5,7 +5,6 @@ $_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
include(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/Sale.php');
$sales = Sale::getSales(TRUE, NULL, NULL, FALSE);
$file = fopen('lastupdate.txt', 'w');
@ -15,3 +14,5 @@ fclose($file);
foreach($sales as $sale) {
$sale->end(TRUE);
}

View File

@ -0,0 +1,12 @@
<?php
if(isset($_SERVER['REMOTE_ADDR'])) exit;
$_SERVER['SERVER_PORT'] = 80;
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
include(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/Sale.php');
Sale::updateSalesReductionAtDate();