From f4f144057dc514e27ef27332fffe8fec952aad29 Mon Sep 17 00:00:00 2001 From: Marion Muszynski Date: Mon, 25 Jul 2016 12:05:57 +0200 Subject: [PATCH] modif date calcul method to include we and check if the last day is a business day --- .../BusinessDaysCalculator.php | 11 +++++++ modules/privatesales_delay/saledelay.php | 32 ++++++++++++------- override/controllers/ProductController.php | 2 +- themes/site/product.tpl | 8 ++++- 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/modules/privatesales_delay/BusinessDaysCalculator.php b/modules/privatesales_delay/BusinessDaysCalculator.php index 22cab6e4..c96478ab 100644 --- a/modules/privatesales_delay/BusinessDaysCalculator.php +++ b/modules/privatesales_delay/BusinessDaysCalculator.php @@ -46,6 +46,17 @@ if(!class_exists('BusinessDaysCalculator')) { } } + public function addDaysWithCheckLastDay($howManyDays) { + $i = 0; + while ($i < $howManyDays) { + $this->date->modify("+1 day"); + $i++; + } + if (!$this->isBusinessDay($this->date)) { + $this->date->modify("+1 day"); + } + } + public function getDate() { return $this->date; } diff --git a/modules/privatesales_delay/saledelay.php b/modules/privatesales_delay/saledelay.php index bd65f40c..59c43705 100755 --- a/modules/privatesales_delay/saledelay.php +++ b/modules/privatesales_delay/saledelay.php @@ -45,7 +45,7 @@ if (!class_exists('SaleDelay')) { return $products_final; } - public static function getDeliveryDate(array $delays, $id_lang = null, $date_calcul = null) + public static function getDeliveryDate(array $delays, $id_lang = null, $date_calcul = null, $include_we =false) { global $cookie; if (!$date_calcul) { @@ -62,28 +62,38 @@ if (!class_exists('SaleDelay')) { 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); + $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); + $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); + $delivery_date[$delay]['date_end'] = self::getDateWithDelay((int) $delivery_date[$delay]['value_max'], $date_calcul, $include_we); } } } return $delivery_date; } - public static function getDateWithDelay($nb_day, $date) + public static function getDateWithDelay($nb_day, $date, $include_we = false) { - $calculator = new BusinessDaysCalculator( - new DateTime($date->format('Y-m-d H:i:s')), - array(), - [BusinessDaysCalculator::SATURDAY, BusinessDaysCalculator::SUNDAY] - ); - $calculator->addBusinessDays($nb_day); + 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(); } diff --git a/override/controllers/ProductController.php b/override/controllers/ProductController.php index 56d2021f..dfb9b8c5 100755 --- a/override/controllers/ProductController.php +++ b/override/controllers/ProductController.php @@ -19,7 +19,7 @@ class ProductController extends ProductControllerCore { require_once _PS_ROOT_DIR_.'/modules/privatesales_delay/saledelay.php'; } $date = new DateTime(); - $delivery_date = SaleDelay::getDeliveryDate(array($sale->delivery_delay), null, $date); + $delivery_date = SaleDelay::getDeliveryDate(array($sale->delivery_delay), null, $date, true); } } diff --git a/themes/site/product.tpl b/themes/site/product.tpl index 94564c82..46db2a2f 100755 --- a/themes/site/product.tpl +++ b/themes/site/product.tpl @@ -484,10 +484,16 @@ var fieldRequired = '{l s='Please fill in all required fields, then save the cus {if isset($delivery_date)}
- {foreach from=$delivery_date item=date} + {foreach from=$delivery_date key=key item=date} + {if $key == 1}

{$date.name} : {l s='Date de réception prévue entre le'} {$date.date_start|date_format:'%d/%m/%Y'} {l s='et le'} {$date.date_end|date_format:'%d/%m/%Y'}

+ {elseif $key == 5} +

+ {$date.name} : {l s='Livraison prévue avant le'} {l s='24/12'} +

+ {/if} {/foreach}
{/if}