2016-01-04 12:49:26 +01:00
|
|
|
<?php
|
|
|
|
require 'BusinessDaysCalculator.php';
|
|
|
|
|
|
|
|
if (!class_exists('SaleDelay')) {
|
|
|
|
|
2017-09-18 11:47:26 +02:00
|
|
|
class SaleDelay {
|
|
|
|
|
|
|
|
public static function associateDelay($products)
|
|
|
|
{
|
|
|
|
global $cookie;
|
|
|
|
|
|
|
|
// products cart group by delay
|
|
|
|
$products_final = array();
|
|
|
|
$categories_cached = array();
|
|
|
|
$sales_cached = array();
|
|
|
|
|
|
|
|
foreach ($products as $key => $product) {
|
|
|
|
if (!isset($product['id_category_default'])) {
|
|
|
|
$product['id_category_default'] = Db::getInstance()->getValue('
|
|
|
|
SELECT `id_category_default`
|
|
|
|
FROM `ps_product`
|
|
|
|
WHERE `id_product` = '.(int)$product['product_id'] .'
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array($product['id_category_default'], $categories_cached)) {
|
|
|
|
$sale = Sale::getSaleFromCategory((int) $product['id_category_default']);
|
|
|
|
$sales_cached[(int) $product['id_category_default']] = $sale;
|
|
|
|
} else {
|
|
|
|
$sale = $sales_cached[(int) $product['id_category_default']];
|
|
|
|
}
|
|
|
|
$delay = !empty($sale->delivery_delay) ? $sale->delivery_delay : 1;
|
|
|
|
|
|
|
|
if(!isset($products_final[(int)$delay])) {
|
|
|
|
$products_final[(int)$delay] = array();
|
|
|
|
$array_delay = self::getDelay($delay, $cookie->id_lang);
|
|
|
|
|
|
|
|
$products_final[(int)$delay]['title'] = $array_delay['short_name'];
|
|
|
|
$products_final[(int)$delay]['products'] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$products_final[(int)$delay]['products'][] = $product;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $products_final;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getDeliveryDate(array $delays, $id_lang = null, $date_calcul = null, $include_we =false)
|
|
|
|
{
|
|
|
|
global $cookie;
|
|
|
|
if (!$date_calcul) {
|
|
|
|
$date_calcul = new DateTime();
|
|
|
|
}
|
|
|
|
if (!$id_lang) {
|
|
|
|
$id_lang = $cookie->id_lang;
|
|
|
|
}
|
|
|
|
if (empty($delays)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$delivery_date = array();
|
|
|
|
foreach ($delays as $key => $delay) {
|
|
|
|
if (!in_array($delay, array_keys($delivery_date))) {
|
|
|
|
$delivery_date[$delay] = self::getDayDelay($delay, $cookie->id_lang);
|
|
|
|
$delivery_date[$delay]['date_start'] = self::getDateWithDelay((int)$delivery_date[$delay]['value'], $date_calcul, $include_we);
|
|
|
|
|
|
|
|
// exception noel
|
|
|
|
if ($delay == 5) {
|
|
|
|
$delivery_date[$delay]['date_end'] = self::getDateWithDelay((int) $delivery_date[$delay]['value'], $date_calcul, $include_we);
|
|
|
|
} else {
|
|
|
|
$delivery_date[$delay]['date_end'] = self::getDateWithDelay((int) $delivery_date[$delay]['value_max'], $date_calcul, $include_we);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($delay != 5) {
|
|
|
|
$diff = (int)$delivery_date[$delay]['value_max'] - (int)$delivery_date[$delay]['value'];
|
|
|
|
$interval = $delivery_date[$delay]['date_end']->diff($delivery_date[$delay]['date_start']);
|
|
|
|
if((int)$diff != (int)$interval->format('%a')){
|
|
|
|
$day_to_add = (int)$diff - (int)$interval->format('%a');
|
|
|
|
if($day_to_add>1){
|
|
|
|
$delivery_date[$delay]['date_end'] = $delivery_date[$delay]['date_end']->modify('+'.$day_to_add.' days');
|
|
|
|
}else{
|
|
|
|
$delivery_date[$delay]['date_end'] = $delivery_date[$delay]['date_end']->modify('+'.$day_to_add.' day');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $delivery_date;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getDateWithDelay($nb_day, $date, $include_we = false)
|
|
|
|
{
|
|
|
|
|
|
|
|
if ($include_we) {
|
|
|
|
$calculator = new BusinessDaysCalculator(
|
|
|
|
new DateTime($date->format('Y-m-d H:i:s')),
|
|
|
|
array(),
|
|
|
|
[BusinessDaysCalculator::SUNDAY]
|
|
|
|
);
|
|
|
|
$calculator->addDaysWithCheckLastDay($nb_day);
|
|
|
|
} else {
|
|
|
|
$calculator = new BusinessDaysCalculator(
|
|
|
|
new DateTime($date->format('Y-m-d H:i:s')),
|
|
|
|
array(),
|
|
|
|
[BusinessDaysCalculator::SATURDAY, BusinessDaysCalculator::SUNDAY]
|
|
|
|
);
|
|
|
|
$calculator->addBusinessDays($nb_day);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $calculator->getDate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getDayDelay($id_delay, $id_lang)
|
|
|
|
{
|
|
|
|
return Db::getInstance()->getRow('
|
|
|
|
SELECT `value`, `value_max`, `name`
|
|
|
|
FROM `ps_privatesale_delay_lang`
|
|
|
|
WHERE `id_delay` = '.(int) $id_delay.'
|
|
|
|
AND `id_lang` = '.(int) $id_lang.'
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getDelay($id_delay, $id_lang)
|
|
|
|
{
|
|
|
|
return Db::getInstance()->getRow('
|
|
|
|
SELECT d.`id_delay`, dl.`name`, dl.`short_name`, dl.`value`
|
|
|
|
FROM `'._DB_PREFIX_.'privatesale_delay_lang` d
|
|
|
|
LEFT JOIN `'._DB_PREFIX_.'privatesale_delay_lang` dl ON (d.`id_delay` = dl.`id_delay`)
|
|
|
|
WHERE dl.`id_lang` = '.(int) $id_lang.'
|
|
|
|
AND d.`id_delay` = '.(int) $id_delay.'
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo - remove this function to use short name
|
|
|
|
*/
|
|
|
|
public static function getDelaySmallName($id_delay, $id_lang) {
|
|
|
|
if($id_lang == 2) {
|
|
|
|
switch ($id_delay) {
|
|
|
|
case '1':
|
|
|
|
return 'Classique';
|
|
|
|
break;
|
|
|
|
case '2':
|
|
|
|
return '48h';
|
|
|
|
break;
|
|
|
|
case '3':
|
|
|
|
return '7j';
|
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
return '10j';
|
|
|
|
break;
|
|
|
|
case '5':
|
|
|
|
return 'Noël';
|
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
return 'Star';
|
|
|
|
break;
|
|
|
|
case '7':
|
|
|
|
return '72h';
|
|
|
|
break;
|
|
|
|
case '8':
|
|
|
|
return '48h Noël';
|
|
|
|
break;
|
2017-11-14 17:09:17 +01:00
|
|
|
case '10':
|
|
|
|
return '72h Noël';
|
|
|
|
break;
|
2017-09-18 11:47:26 +02:00
|
|
|
case '9':
|
|
|
|
return 'Spé';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 'Inconnu';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} elseif($id_lang == 3){
|
|
|
|
switch ($id_delay) {
|
|
|
|
case '1':
|
|
|
|
return 'Classic';
|
|
|
|
break;
|
|
|
|
case '2':
|
|
|
|
return '48h';
|
|
|
|
break;
|
|
|
|
case '3':
|
|
|
|
return '7j';
|
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
return '10j';
|
|
|
|
break;
|
|
|
|
case '5':
|
|
|
|
return 'Noël';
|
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
return 'Star';
|
|
|
|
break;
|
|
|
|
case '7':
|
|
|
|
return '72h';
|
|
|
|
break;
|
|
|
|
case '9':
|
|
|
|
return 'Spé';
|
|
|
|
break;
|
|
|
|
case '8':
|
|
|
|
return '48h Noël';
|
|
|
|
break;
|
2017-11-14 17:09:17 +01:00
|
|
|
case '10':
|
|
|
|
return '72h Noël';
|
|
|
|
break;
|
2017-09-18 11:47:26 +02:00
|
|
|
default:
|
|
|
|
return 'Unknown';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 'Unknown';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo - remove this function to use short name
|
|
|
|
*/
|
|
|
|
public static function getDelayFrontSmallName($id_delay, $id_lang) {
|
|
|
|
if($id_lang == 2) {
|
|
|
|
switch ($id_delay) {
|
|
|
|
case '1':
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'sous 3 semaines';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
|
|
|
case '2':
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'sous 48h';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
|
|
|
case '3':
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'sous 7 jours';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
|
|
|
case '4':
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'sous 10 jours';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
|
|
|
case '5':
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'avant Noël';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
|
|
|
case '6':
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'spécifique';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
|
|
|
case '7':
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'sous 72h';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
|
|
|
case '8':
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'sous 48h';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
2017-11-14 17:09:17 +01:00
|
|
|
case '10':
|
|
|
|
return 'sous 72h';
|
|
|
|
break;
|
2017-09-18 11:47:26 +02:00
|
|
|
case '9':
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'spécifique';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
|
|
|
default:
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'sous quelques jours';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} elseif($id_lang == 3){
|
|
|
|
switch ($id_delay) {
|
|
|
|
case '1':
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'de 3 semanas';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
|
|
|
case '2':
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'de 48h';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
|
|
|
case '3':
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'de 7 días';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
|
|
|
case '4':
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'de 10 días';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
|
|
|
case '5':
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'antes de Navidad';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
|
|
|
case '6':
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'diferent';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
|
|
|
case '7':
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'de 72h';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
|
|
|
case '9':
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'diferent';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
|
|
|
case '8':
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'de 48h';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
2017-11-14 17:09:17 +01:00
|
|
|
case '10':
|
|
|
|
return 'de 72h';
|
|
|
|
break;
|
2017-09-18 11:47:26 +02:00
|
|
|
default:
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'de un poco días';
|
2017-09-18 11:47:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-20 16:44:15 +02:00
|
|
|
return 'de un poco días';
|
2017-09-18 11:47:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-01-04 12:49:26 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|