<?php
namespace App\Utils;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Twig\Environment;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use App\Services\ToolService;
use App\Services\iresaService;
use App\Utils\IresaManager;
use App\Services\TravellerService;
use App\Entity\assurance;
use App\Entity\commande;
use App\Entity\Destination;
use App\Entity\Package;
use App\Entity\InfosDiscover;
use App\Entity\InfosDiscoverLink;
use App\Entity\InfosEvent;
use App\Entity\InfosFormuleBase;
use App\Entity\InfosResidence;
use App\Entity\OptionResidence;
use App\Entity\Residence;
use App\Entity\Service;
use App\Entity\Typologie;
use App\Entity\Language;
class Residences
{
const IRESA_LANG_DEFAULT = 'fr';
private $em;
private $toolService;
private $iresaManager;
private $iresaService;
private $travellerService;
private $router;
private $locale;
private $lang;
private $langDefault;
private $twig;
private $packageIds;
private $session;
private $translator;
private $residences;
public function __construct(
EntityManagerInterface $em,
ToolService $toolService,
IresaManager $iresaManager,
iresaService $iresaService,
TravellerService $travellerService,
Environment $twig,
SessionInterface $session,
TranslatorInterface $translator,
RouterInterface $router
) {
$this->em = $em;
$this->session = $session;
$this->toolService = $toolService;
$this->iresaService = $iresaService;
$this->iresaManager = $iresaManager;
$this->translator = $translator;
$this->travellerService = $travellerService;
$this->router = $router;
$this->twig = $twig;
$this->locale = "fr";
$this->lang = null;
$this->langDefault = null;
$this->packageIds = [];
}
public function setLocale($locale) {
$this->locale = $locale;
$this->lang = $this->em->getRepository(Language::class)->findOneByCode($locale);
$this->langDefault = $this->em->getRepository(Language::class)->findOneByCode('fr');
}
public function getPlanning($request, $datearrivee, $duree, $type, $delta, $residence) {
$id = $residence->getIdiresa();
$locale = $this->locale;
$dispoPrestations = [];
$prestationDefault = [];
$planning = [];
$response = [
"packages" => [],
"prestations" => [],
"listDatesDebut" => [],
"listDatesFin" => [],
"listDurees" => [],
"idPrestationDefault" => []
];
// les prestations de la date d'arrivée sélectionnée uniquement
$prestations = $this->getPrestations($id, $datearrivee, $duree, $locale, $delta);
// Si pas de prestation avec la durée sélectionnée recherche sans préciser la durée
if(count($prestations['ListReponses']) == 0) {
$message[] = "Pas de prestation sélectionnée avec cette date et cette durée";
$prestations = $this->getPrestations($id, $datearrivee, null, $locale, $delta);
}
// Si toujours pas de réponse
if(count($prestations['ListReponses']) == 0) {
$travellers = $this->travellerService->getTravellers();
$this->travellerService->init();
$response['redirect'] = true;
return $response;
}
$packages = $this->getPackages($id, $prestations);
$idPrestationDefault = $prestations['ListReponses'][0]['IdPrestation'];
/*
foreach ($prestations['ListReponses'] as $prestation) {
$dispoPrestations[] = $prestation;
}
if ($dispoPrestations) {
$idPrestationDefault = $dispoPrestations[0]['IdPrestation'];
$message[] = "Prestations disponibles";
$idPrestationDefault = $dispoPrestations[0]['IdPrestation'];
$prestationDefault = [
'id' => $idPrestationDefault,
'nom_commercial' => $prestations[$idPrestationDefault]['zero']['nom_commercial'],
'IdTypePrestation' => $dispoPrestations[0]['IdTypePrestation'],
];
}
*/
$now = new \DateTime();
$queryParams = [
'ListLieuHebergement' => $id,
'IdPrestation' => $idPrestationDefault,
'noCache' => $now
];
$planning = $this->iresaManager->getPlanning($queryParams, $datearrivee, $type);
/*
if($request->isXmlHttpRequest()) {
// Si appel ajax, on a déjà le planning du mois, on recherche uniquement le planning pour le jour sélectionné
$planning = $this->iresaManager->getPlanning($queryParams, $datearrivee, "day");
} else {
// Si appel direct on veut connaitre le plannign du mois pour l'afficher
$planning = $this->iresaManager->getPlanning($queryParams, $datearrivee, "month");
}
*/
if(empty($planning['ListDatesDebut'])) {
if($request->isXmlHttpRequest()) {
return $response;
}
}
$listDatesDebut = [];
foreach ($planning['ListDatesDebut'] as $date) {
$listDatesDebut[] = substr($date, 8, 2) . '/' .substr($date, 5, 2) . '/' . substr($date, 0, 4);
}
$listDurees = $planning['ListDurees'];
$listDatesFin = [];
foreach ($planning['ListDatesDebut'] as $date) {
foreach($listDurees as $duree) {
// Ajouter la duree à la date et remplir le tableau $listDatesFin
$dateFin = new \DateTime($date);
$dateFin->add(new \DateInterval('P'.$duree.'D'));
// push dateFin in french format to listrDatesFin if only it not present in array
if(!in_array($dateFin->format('d/m/Y'), $listDatesFin)) {
$listDatesFin[] = $dateFin->format('d/m/Y');
}
}
}
$this->session->set('idPrestation', $idPrestationDefault);
$response = [
"packages" => $packages,
"prestations" => $prestations,
"listDatesDebut" => $listDatesDebut,
"listDatesFin" => $listDatesFin,
"listDurees" => $listDurees,
"idPrestationDefault" => $idPrestationDefault
];
//throw $this->createNotFoundException('Résidence inexistante');
return $response;
}
public function getPackages($id, $prestations) {
$residence = $this->em->getRepository(Residence::class)->findOneBy(['idiresa' => $id]);
if(!$residence) {
return [];
//throw new \Exception('Résidence inexistante');
}
$params = [
'lang' => $this->lang,
'residence' => $residence
];
$infosResidence = $this->em->getRepository(InfosResidence::class)->findOneBy($params);
$formuleBase = $this->em->getRepository(InfosFormuleBase::class)->findOneBy($params);
$params['commercialisation'] = true;
$listPackages = $this->em->getRepository(Package::class)->findBy($params);
$packageIds = [];
foreach($listPackages as $package) {
$packageIds[] = $package->getIdIresa();
}
$packages = [];
$haslocative = 0;
foreach ($prestations['ListReponses'] as $reponse) {
if( $reponse['IdPrestationHebergement'] != 0) {
$idP = $reponse['IdPrestationHebergement'];
} else {
$idP = $reponse['IdPrestation'];
$haslocative = 1;
}
if($haslocative == 1){
$packages[0] = [
'idIresa' => 0,
'type' => 'formule_base',
'entity' => $formuleBase,
'nom' => $formuleBase->getNom(),
'picto' => $formuleBase->getPicto(),
'photoProfilDefault' => 'build/images/front/locatif-default.jpg',
'photoProfil' => $formuleBase->getPhotoProfil(),
'photoPopin' => $formuleBase->getPhotoPopin(),
'descriptionPopin' => $formuleBase->getDescriptionPopin(),
'prestaInc' => $formuleBase->getPrestaInc(),
'prestaNoinc' => $formuleBase->getPrestaNoinc(),
'serviceInc' => $formuleBase->getServiceInc(),
'serviceNoinc' => $formuleBase->getServiceNoinc(),
'description' => $formuleBase->getDescription(),
//'resShortdesc' => $infosResidence->getResShortdesc(),
'resBigdesc' => $infosResidence->getResBigdesc()
];
}
}
foreach($prestations['FichesPrestation'] as $prestation) {
$package = $this->em->getRepository(Package::class)->findOneBy([
'lang' => $this->lang,
'idIresa' => $prestation['IdElement'],
'residence' => $residence
]);
if(array_key_exists("pack_heb", $prestation['ListFiches']) && in_array($package->getIdIresa(), $packageIds)) {
$packages[] = [
'idIresa' => $package->getIdIresa(),
'type' => 'pack_heb',
'entity' => $package,
'nom' => $package->getNom(),
'picto' => $package->getPicto(),
'photoProfilDefault' => 'build/images/front/package-default.jpg',
'photoProfil' => $package->getPhotoProfil(),
'photoPopin' => $package->getPhotoPopin(),
'descriptionPopin' => $package->getDescriptionPopin(),
'prestaInc' => $formuleBase->getPrestaInc(),
'prestaNoinc' => $formuleBase->getPrestaNoinc(),
'serviceInc' => $package->getServiceInc(),
'serviceNoinc' => $package->getServiceNoinc(),
'description' => $package->getDescription(),
//'resShortdesc' => $infosResidence->getResShortdesc(),
'resBigdesc' => $infosResidence->getResBigdesc()
];
}
if(array_key_exists("pack_loisir", $prestation['ListFiches']) && in_array($package->getIdIresa(), $packageIds)) {
$packages[] = [
'idIresa' => $package->getIdIresa(),
'type' => 'pack_loisir',
'entity' => $package,
'nom' => $package->getNom(),
'picto' => $package->getPicto(),
'photoProfilDefault' => 'build/images/front/package-default.jpg',
'photoProfil' => $package->getPhotoProfil(),
'photoPopin' => $package->getPhotoPopin(),
'descriptionPopin' => $package->getDescriptionPopin(),
'prestaInc' => $formuleBase->getPrestaInc(),
'prestaNoinc' => $formuleBase->getPrestaNoinc(),
'serviceInc' => $package->getServiceInc(),
'serviceNoinc' => $package->getServiceNoinc(),
'description' => $package->getDescription(),
//'resShortdesc' => $infosResidence->getResShortdesc(),
'resBigdesc' => $infosResidence->getResBigdesc()
];
}
}
return $packages;
}
public function getHebergements($prestations, $selectedHebergementId, $mode = "") {
$disabled = "";
$formLoc = "Formule temporaire";
$travellers = $this->travellerService->getTravellers();
$nbPax = $this->travellerService->travellers['nbPax'];
$dateArrivee = $this->travellerService->travellers['actdatearrivee'] ;
$duree = $this->travellerService->travellers['actnbnuits'] ;
$id = $travellers['idResidence'];
$prestations = $this->getPrestations($id, $dateArrivee, $duree, $this->locale, 0);
$packages = $this->getPackages($id, $prestations);
$contents = "";
if(count($prestations['ListReponses']) > 0) {
$countHide = 0;
foreach ($prestations['ListReponses'] as $key => $reponse) {
if($reponse['IdPrestationHebergement'] == 0) {
$idiresa = $reponse['IdPrestation'];
} else {
$idiresa = $reponse['IdPrestationHebergement'];
}
$params = [
'lang' => $this->lang,
'idiresa' => $idiresa
];
$typologie = $this->em->getRepository(Typologie::class)->findOneBy($params);
$typologie_nom = "";
$typologie_paxmax = "";
if($typologie instanceof Typologie) {
$typologie_nom = $typologie->getNom();
$typologie_paxmax = $typologie->getPaxMax();
} else {
$typologie = "";
}
$prestations['ListReponses'][$key]['NomTypologie'] = $typologie_nom;
$prestations['ListReponses'][$key]['PaxMax'] = $typologie_paxmax;
}
foreach ($prestations['Hebergements'] as $idPrestation => $ptsHeb) {
foreach ($ptsHeb as $ptHeb) {
$hebergement = $prestations['ListReponses'][$ptHeb];
if (array_key_exists('IdPrestationHebergement', $hebergement )) {
if ($hebergement['IdPrestationHebergement'] == 0) {
$idPrestationHebergement = $hebergement['IdPrestation'];
$idpack = 0;
$fichesPrestation = $prestations['ListReponses'][$ptHeb];
$formLoc = $packages[0]['nom'];
} else {
$idPrestationHebergement = $prestations['ListReponses'][$ptHeb];
$idpack = $hebergement['IdPrestation'];
foreach($packages as $package) {
if($package['idIresa'] == $idpack) {
$formLoc = $package['nom'];
}
}
}
$capaciteMax = $hebergement['CapaciteMax'];
if ($nbPax <= $capaciteMax) {
$contents .= $this->twig->render('/front/Produit/recaptarif.html.twig', [
'hebergement' => $hebergement,
'idpack' => $idpack,
'formLoc' => $formLoc,
'paxMax' => $capaciteMax,
'selectedHebergement' => $selectedHebergementId,
'idPrestationHebergement' => $idPrestationHebergement
]);
} else {
echo "<!-- Hébergement: " . $idPrestationHebergement . " | PAX: " . $nbPax . " - CapaciteMax: " . $capaciteMax . " -->";
$countHide++;
}
}
}
}
if ( $countHide == count($prestations['ListReponses'])) {
$contents .= '<div class="width-100 recherche-fp-tarif detailform margin-off">
<p class="text-align-center">' . $this->translator->trans('produit.residence.capacite_insufisante' , [], 'app') . '</p>
</div>';
$disabled = "disabled";
}
if(empty($travellerService->travellers['actdatearrivee'])) {
$disabled = "disabled";
}
}
$contents .= '<div id="error-resa" class="error formule-bloc" style="display:none"></div>';
if($mode == "edit") {
$contents .= '<button type="submit" class="submitResaProduit" ' . $disabled . ' >' . $this->translator->trans('ancre.actualiser_reservation' , [], 'app') . '</button>';
} else {
$contents .= '<button type="submit" class="submitResaProduit" ' . $disabled . ' >' . $this->translator->trans('ancre.reserver' , [], 'app') . '</button>';
}
return $contents;
}
function getPrestations($id, $dateDebut, $duree, $locale, $deltaJour) {
// Recherche formule locative
$locale = "fr";
$queryParams = [
'ListLieuHebergement' => $id,
'DateDebut' => $dateDebut,
'DetailParPrestation' => 1,
'NbPages' => 50,
'NbDeltaJour' => $deltaJour,
'PageSize' => 360,
'Page' => 1
];
if ($duree > 0 ) {
$queryParams['Duree'] = $duree;
}
$prestations = $this->iresaManager->getAllPrestations($queryParams, $locale);
$this->packageIds = $prestations['prestations'];
$prestations['params'] = $queryParams;
return $prestations;
}
public function getFicheHebergement($detail) {
$details = json_decode($detail, true);
$reservation = null;
$formule = null;
if(!is_null($details)) {
$idPackage = array_key_first($details['reservation']);
$reservation = $details['reservation'][$idPackage];
}
$className = $details['formule']['className'];
if($className != commande::FORMULE_UNKNOWN){
$formule = $this->em->getRepository("App\\Entity\\{$className}")->findOneBy([
'lang' => $this->lang,
'id' => $details['residence'],
'idIresa' => $details['formule']['idIresa']
]);
}
$typologie = $this->em->getRepository(Typologie::class)->findOneBy([
'lang' => $this->lang,
'idiresa' => $details['typologie']
]);
$residence = $this->em->getRepository(Residence::class)->findOneByIdiresa($reservation['idLieu']);
$fiche = [
"reservation" => $reservation,
"residence" => $residence,
"formule" => $formule,
"typologie" => $typologie
];
return $fiche;
}
public function getSejours($hebergements, $reponses, $lang, $nbPax = 0) {
// On parcourt le tableau afin de ne garder que la typologie de CapaciteMax correspondant au nbPax et le tarif le plus petit pour la résidence
$fichesAll = [];
foreach ($reponses as $reponse) {
if($reponse['IdLieuHebergement'] > 0) {
$idLieuHebergement = $reponse['IdLieuHebergement'];
$capaciteMax = $reponse['CapaciteMax'];
$prixTotal = $reponse['PrixTotal'];
// Si la capacité max est insuffisante, passer à l'itération suivante
if (!($nbPax === 0 || $capaciteMax >= $nbPax)) {
continue;
}
// Si le lieu n'est pas encore enregistré ou si le prix est plus bas, on enregistre la réponse
if (!isset($fichesAll[$idLieuHebergement]) || $prixTotal < $fichesAll[$idLieuHebergement]['PrixTotal']) {
$fichesAll[$idLieuHebergement] = $reponse;
}
}
}
$fiches = [];
foreach($fichesAll as $idLieuHebergement => $fiche) {
$residence = $this->em->getRepository(Residence::class)->findOneEnabledByIdiresa($lang, $idLieuHebergement);
if(!$residence) {
continue;
}
$params = [
'residence' => $residence,
'lang_id' => $this->lang->getId()
];
$infosResidence = $this->em->getRepository(InfosResidence::class)->findOneByLang($params);
$formuleBase = $this->em->getRepository(InfosFormuleBase::class)->findOneByLang($params);
$presta_inc = [];
$presta_noinc = [];
$nom = "";
$res_lieu = "";
$famille= "";
$res_shortdesc = "";
if(!is_null($formuleBase)) {
$presta_inc = (Array) json_decode($formuleBase->getPrestaInc());
$presta_noinc = (Array) json_decode($formuleBase->getPrestaNoinc());
}
//$famille = $hebergement['ListFiches']['zero']['famille'];
$nom = $residence->getNom();
$res_lieu = $infosResidence->getResLieu();
$res_shortdesc = $infosResidence->getResShortdesc();
$infosHebergement = [
'residence' => $residence,
'presta_inc' => $presta_inc,
'presta_noinc' => $presta_noinc,
'nom' => $nom,
'res_lieu' => $res_lieu,
//'famille' => $famille,
'res_shortdesc' => $res_shortdesc,
];
$fiches[] = array_merge($fiche, $infosHebergement);
}
return $fiches;
}
public function getBreadCrumb($residence) {
$destinations[] = [
'nom' => strtolower($residence->getNom()),
];
$currentDestinationId = $residence->getDestinationIdiresa();
if ($currentDestinationId != null) {
// get destination record width idiresa and lang
$currentDestination = $this->em->getRepository(Destination::class)->findOneBy(['idIresa' => $currentDestinationId, 'lang' => $this->lang]);
$currentDestination = $currentDestination->getParent();
// Tant que la destination actuelle a un parent, on continue de remonter
while ($currentDestination !== null) {
// On ajoute la destination actuelle au début du tableau des destinations
$destination = [
'nom' => strtolower($currentDestination->getTitreCourt()),
'url' => $this->router->generate('ac_platform_destination', [
'slug' => $currentDestination->getSlug(),
'id' => $currentDestination->getIdIresa()
])
];
array_unshift($destinations, $destination);
// On remonte à la destination parente
$currentDestination = $currentDestination->getParent();
}
}
$destination = [
'nom' => "Accueil",
'url' => $this->router->generate('ac_platform_homepage')
];
array_unshift($destinations, $destination);
return $destinations;
}
}