121 lines
4.2 KiB
PHP
121 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace PlanificationLivraison;
|
|
|
|
use DateInterval;
|
|
use DateTime;
|
|
use PointRetrait\Model\PdrScheduleQuery;
|
|
use Propel\Runtime\Connection\ConnectionInterface;
|
|
use Thelia\Core\Translation\Translator;
|
|
use Thelia\Install\Database;
|
|
use Thelia\Module\BaseModule;
|
|
|
|
class PlanificationLivraison extends BaseModule
|
|
{
|
|
/** @var string */
|
|
const DOMAIN_NAME = 'planificationlivraison';
|
|
const FORMAT_DATES = 'd/m/Y';
|
|
const FORMAT_DATE_AVEC_JOUR = 'w d/m/Y';
|
|
const FORMAT_DATE_COMPLETE = 'Y-m-d H:i:s';
|
|
const DAYS_OF_WEEK = [
|
|
0 => 'monday',
|
|
1 => 'tuesday',
|
|
2 => 'wednesday',
|
|
3 => 'thursday',
|
|
4 => 'friday',
|
|
5 => 'saturday',
|
|
6 => 'sunday'
|
|
];
|
|
|
|
const CONFIG_API_KEY = 'googlemap_api_key';
|
|
const CONFIG_MAP_CENTER_LAT = 'map_center_latitude';
|
|
const CONFIG_MAP_CENTER_LNG = 'map_center_longitude';
|
|
const CONFIG_RED_ALERT = 'delay_red_alert';
|
|
const CONFIG_ORANGE_ALERT = 'delay_orange_alert';
|
|
const CONFIG_PREPARATION_DELAY = 'orders_preparation_delay';
|
|
|
|
|
|
/**
|
|
* @param ConnectionInterface|null $con
|
|
*/
|
|
public function postActivation(ConnectionInterface $con = null)
|
|
{
|
|
$database = new Database($con->getWrappedConnection());
|
|
$database->insertSql(null, array(__DIR__ . '/Config/thelia.sql'));
|
|
}
|
|
|
|
|
|
static public function calculateRelativeDate($baseDay, $placeId = null)
|
|
{
|
|
$minimumDelayBeforeOrder = PlanificationLivraison::getConfigValue(PlanificationLivraison::CONFIG_PREPARATION_DELAY, 0, "en_US");
|
|
|
|
$date = new DateTime();
|
|
$date->add(new DateInterval('P'. $minimumDelayBeforeOrder . 'D'));
|
|
|
|
$nextPossibleDay = '';
|
|
if (null !== $baseDay) { // Cas où l'on a choisi une date
|
|
switch ($baseDay) {
|
|
case 0 :
|
|
$nextPossibleDay = $date->modify('next monday');
|
|
break;
|
|
case 1 :
|
|
$nextPossibleDay = $date->modify('next tuesday');
|
|
break;
|
|
case 2 :
|
|
$nextPossibleDay = $date->modify('next wednesday');
|
|
break;
|
|
case 3 :
|
|
$nextPossibleDay = $date->modify('next thursday');
|
|
break;
|
|
case 4 :
|
|
$nextPossibleDay = $date->modify('next friday');
|
|
break;
|
|
case 5 :
|
|
$nextPossibleDay = $date->modify('next saturday');
|
|
break;
|
|
case 6 :
|
|
$nextPossibleDay = $date->modify('next sunday');
|
|
break;
|
|
}
|
|
}
|
|
else {
|
|
|
|
// Cas du Click and Collect où l'on doit prendre la première date dispo
|
|
if (null !== $placeId) {
|
|
|
|
$allPossibleDaysOfWeek = PdrScheduleQuery::create()->filterByIdPlace($placeId)->orderByDay()->find()->getData();
|
|
$allPossibleDays = [];
|
|
foreach ($allPossibleDaysOfWeek as $theDay) {
|
|
$dow = self::DAYS_OF_WEEK[$theDay->getDay()];
|
|
array_push($allPossibleDays, date(self::FORMAT_DATE_COMPLETE, strtotime("next $dow", $date->getTimestamp())));
|
|
}
|
|
$nextPossibleDay = new DateTime(min($allPossibleDays));
|
|
}
|
|
}
|
|
|
|
return $nextPossibleDay;
|
|
}
|
|
|
|
|
|
static public function getDayLabel($int)
|
|
{
|
|
$translator = Translator::getInstance();
|
|
|
|
$days = [
|
|
$translator->trans("Monday", [], PlanificationLivraison::DOMAIN_NAME),
|
|
$translator->trans("Tuesday", [], PlanificationLivraison::DOMAIN_NAME),
|
|
$translator->trans("Wednesday", [], PlanificationLivraison::DOMAIN_NAME),
|
|
$translator->trans("Thursday", [], PlanificationLivraison::DOMAIN_NAME),
|
|
$translator->trans("Friday", [], PlanificationLivraison::DOMAIN_NAME),
|
|
$translator->trans("Saturday", [], PlanificationLivraison::DOMAIN_NAME),
|
|
$translator->trans("Sunday", [], PlanificationLivraison::DOMAIN_NAME)
|
|
];
|
|
|
|
if ($int === null)
|
|
return $days;
|
|
else
|
|
return $days[$int];
|
|
}
|
|
|
|
}
|