Marion Muszynski ffbb3232b9 modif soflex
2016-05-09 17:43:57 +02:00

298 lines
12 KiB
PHP

<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to a commercial license from Common-Services Co., Ltd.
* Use, copy, modification or distribution of this source file without written
* license agreement from the SARL SMC is strictly forbidden.
* In order to obtain a license, please contact us: contact@common-services.com
* ...........................................................................
* INFORMATION SUR LA LICENCE D'UTILISATION
*
* L'utilisation de ce fichier source est soumise a une licence commerciale
* concedee par la societe Common-Services Co., Ltd.
* Toute utilisation, reproduction, modification ou distribution du present
* fichier source sans contrat de licence ecrit de la part de la Common-Services Co. Ltd. est
* expressement interdite.
* Pour obtenir une licence, veuillez contacter Common-Services Co., Ltd. a l'adresse: contact@common-services.com
* ...........................................................................
*
* @package So Colissimo Flexibilite
* @author Alexandre D.
* @copyright Copyright (c) 2011-2015 Common Services Co Ltd - 90/25 Sukhumvit 81 - 10260 Bangkok - Thailand
* @license Commercial license
* Support by mail : support.soflexibilite@common-services.com
*/
require_once(dirname(__FILE__).'/../../../config/config.inc.php');
require_once(dirname(__FILE__).'/../../../init.php');
require_once(_PS_MODULE_DIR_.'soflexibilite'.DIRECTORY_SEPARATOR.'soflexibilite.php');
require_once(_PS_MODULE_DIR_.'soflexibilite'.DIRECTORY_SEPARATOR.'classes'.DIRECTORY_SEPARATOR.'SoFlexibiliteWebService.php');
class SoFlexibilitePointList extends Module
{
private $soflexibilite = null;
public function __construct()
{
$this->soflexibilite = new SoFlexibilite();
parent::__construct();
}
public function l($string, $specific = false, $id_lang = null)
{
if (!$specific) {
$specific = 'point_list';
}
return ($this->soflexibilite->l($string, $specific, $id_lang));
}
protected function getActiveSoColissimoPickupCarriers()
{
$relay = Configuration::get('SOFLEXIBILITE_A2P_ID');
$cityssimo = Configuration::get('SOFLEXIBILITE_CIT_ID');
$bpr = Configuration::get('SOFLEXIBILITE_BPR_ID');
$relay_types = array_filter(array((int)$relay, (int)$cityssimo, (int)$bpr));
$active_carriers = Db::getInstance()->executeS('
SELECT `id_carrier`, `active`
FROM `'._DB_PREFIX_.'carrier`
WHERE `id_carrier` IN ('.implode(', ', $relay_types).')'
);
$carriers = array();
if (is_array($active_carriers) && count($active_carriers)) {
foreach ($active_carriers as $carrier) {
$carriers[$carrier['id_carrier']] = $carrier['active'];
}
} else {
$carriers = array(
$relay => false,
$cityssimo => false,
$bpr => false
);
}
return ($carriers);
}
public function dispatch()
{
$error = '';
$horaires = array();
$fermetures_relay = array();
$test = null;
$ferme_relay = null;
$points = array();
$point = null;
$relay = Configuration::get('SOFLEXIBILITE_A2P_ID');
$cityssimo = Configuration::get('SOFLEXIBILITE_CIT_ID');
$bpr = Configuration::get('SOFLEXIBILITE_BPR_ID');
$active_carriers = $this->getActiveSoColissimoPickupCarriers();
$relay = isset($active_carriers[$relay]) ? (bool)$active_carriers[$relay] : false;
$cityssimo = isset($active_carriers[$cityssimo]) ? (bool)$active_carriers[$cityssimo] : false;
$bpr = isset($active_carriers[$bpr]) ? (bool)$active_carriers[$bpr] : false;
ob_start();
$pass = true;
if (Tools::getValue('adresseRecherche') && Tools::getValue('address') && Tools::getValue('postcode') && Tools::getValue('city')) {
$address = array(
'address' => pSQL(Tools::getValue('address')),
'postcode' => (Tools::getValue('postcode')),
'city' => pSQL(Tools::getValue('city')),
'country' => pSQL(Tools::getValue('country')),
'weight' => '1',
'date' => date('d/m/Y', time() + 86400 * 2)
);
} else {
$error = $this->l('Invalid Query');
$pass = false;
}
if ($pass) {
$so_colissimo = new SoFlexibiliteWebService();
$point = $so_colissimo->getRelays($address, $relay);
}
if (isset($point->return->listePointRetraitAcheminement) && is_array($point->return->listePointRetraitAcheminement) && count($point->return->listePointRetraitAcheminement)) {
$listePointRetraitAcheminement = $point->return->listePointRetraitAcheminement;
$i = 0;
foreach ($listePointRetraitAcheminement as $pointRetraitAcheminement) {
if (!$pointRetraitAcheminement instanceof StdClass) {
continue;
} elseif (!$cityssimo && $pointRetraitAcheminement->typeDePoint == 'CIT') {
continue;
} elseif (!$bpr && ($pointRetraitAcheminement->typeDePoint == 'BPR' || $pointRetraitAcheminement->typeDePoint == 'BDP' || $pointRetraitAcheminement->typeDePoint == 'CDI')) {
continue;
}
$points[$i] = $pointRetraitAcheminement;
$horaires[$i] = $pointRetraitAcheminement->horairesOuvertureLundi.' '
.$pointRetraitAcheminement->horairesOuvertureMardi.' '
.$pointRetraitAcheminement->horairesOuvertureMercredi.' '
.$pointRetraitAcheminement->horairesOuvertureJeudi.' '
.$pointRetraitAcheminement->horairesOuvertureVendredi.' '
.$pointRetraitAcheminement->horairesOuvertureSamedi.' '
.$pointRetraitAcheminement->horairesOuvertureDimanche;
if (isset($pointRetraitAcheminement->listeConges->calendarDeDebut)) {
$fermetures_relay[$i] = Tools::substr(str_replace('-', '/', $pointRetraitAcheminement->listeConges->calendarDeDebut), 0, 10)
.'-'.Tools::substr(str_replace('-', '/', $pointRetraitAcheminement->listeConges->calendarDeFin), 0, 10);
} else {
$fermetures_relay[$i] = null;
}
$i++;
}
} elseif (isset($point->return->errorCode) && $point->return->errorCode) {
$error = sprintf('%s (%d): %s', $this->l('Error'), $point->return->errorCode, $point->return->errorMessage);
} else {
$error = sprintf('%s: %s', $this->l('Error'), $this->l('Web Service call failed'));
}
foreach ($horaires as $str) {
$html = '<table class="tableHoFe"><thead><td>'.$this->l('Mon').'</td><td>'.$this->l('Tue').'</td><td>'.$this->l('Wed')
.'</td><td>'.$this->l('Thu').'</td><td>'.$this->l('Fri').'</td><td>'.$this->l('Sat').'</td><td>'.$this->l('Sun').'</td></thead>';
$tps = explode(' ', $str);
for ($i = 0, $j = 0; $i < 14; $i += 2, $j += 1) {
$matin = explode('-', $tps[$i]);
$aprem = explode('-', $tps[$i + 1]);
$o_matin = $matin[0];
$c_matin = $matin[1];
$o_aprem = $aprem[0];
$c_aprem = $aprem[1];
$tranche1_ouverture = strtotime('Monday') + 80000 + strtotime($o_matin);
$tranche1_fermeture = strtotime('Monday') + 80000 + strtotime($c_matin);
$tranche2_ouverture = strtotime('Monday') + 80000 + strtotime($o_aprem);
$tranche2_fermeture = strtotime('Monday') + 80000 + strtotime($c_aprem);
$jour = date('l', (strtotime('Monday') + 80000 + (86400 * $j)));
$jours = array();
$jours['Monday'] = $this->l('Monday');
$jours['Tuesday'] = $this->l('Tuesday');
$jours['Wednesday'] = $this->l('Wednesday');
$jours['Thursday'] = $this->l('Thursday');
$jours['Friday'] = $this->l('Friday');
$jours['Saturday'] = $this->l('Saturday');
$jours['Sunday'] = $this->l('Sunday');
$jour = $jours[$jour];
if ($tranche1_fermeture - $tranche1_ouverture) {
$debutMat = $o_matin;
$finMat = $c_matin;
$debutAprem = $o_aprem;
$finAprem = $c_aprem;
if ($tranche2_fermeture - $tranche2_ouverture) {
$class = 'hoClose';
$ranges_html = '<table class="hoLeft"><thead>'.
'<tr><td>&nbsp;</td></tr></thead>'.
'<tbody>'.
'<tr><td>'.$debutMat.' - '.$finMat.'</td></tr>'.
'<tr><td>'.$finMat.' - '.$debutAprem.'</td></tr>'.
'<tr><td>'.$debutAprem.' - '.$finAprem.'</td></tr>'.
'</tbody>'.
'</table>';
} else {
$class = 'hoOpen';
$ranges_html = '<table class="hoLeft"><thead>'.
'<tr><td>&nbsp;</td></tr></thead>'.
'<tbody>'.
'<tr><td>'.$debutMat.' - 12h00</td></tr>'.
'<tr><td>12h00 - 14h00</td></tr>'.
'<tr><td>14h00 - '.$finMat.'</td></tr>'.
'</tbody>'.
'</table>';
}
$html .= '<td><div class="hoOpen">&nbsp;</div><div class="'.$class.'">&nbsp;<br></div><div class="hoOpen">&nbsp;</div></td>';
} else {
$html .= '<td><div class="hoClose">&nbsp;</div><div class="hoClose">&nbsp;<br></div><div class="hoClose">&nbsp;</div></td>';
$ranges_html = '<table class="hoLeft"><thead>'.
'<tr><td>&nbsp;</td></tr></thead>'.
'<tbody>'.
'<tr><td>08h00 - 12h00</td></tr>'.
'<tr><td>12h00 - 14h00</td></tr>'.
'<tr><td>14h00 - 18h00</td></tr>'.
'</tbody>'.
'</table>';
}
}
$html .= '</tr>';
$html .= '</table>';
$end_html = '<div class="hoClean"></div>';
$test[] .= $ranges_html.$html.$end_html;
}
// Formatage des fermetures
$html = null;
foreach ($fermetures_relay as $val) {
if ($val) {
$ho = explode('-', $val);
$today = date('Y/m/d');
if (strtotime($today) >= strtotime($ho[0]) && strtotime($today.' +3 days') <= strtotime($ho[1])) {
$html = 'disabled';
} else {
$ho[0] = date('d/m/Y', strtotime($ho[0]));
$ho[1] = date('d/m/Y', strtotime($ho[1]));
$html = '<span style="font-weight: bold;">'.$this->l('from').' '.$ho[0].' '.$this->l('to').' '.$ho[1].'</span>';
}
$ferme_relay[] .= $html;
} else {
$ferme_relay[] .= 'N/A';
}
}
$result = ob_get_clean();
if ($result) {
$output = $result;
} else {
$output = null;
}
$points ? $status = true : $status = false;
$json = array(
'output' => $output,
'status' => $status,
'points' => $points,
'error' => $error,
'horaires' => $test,
'fermetures' => $ferme_relay
);
die(Tools::jsonEncode($json));
}
}
$_soFlexibilite_PointList = new SoFlexibilitePointList();
$_soFlexibilite_PointList->dispatch();