<?php
namespace App\Entity\GDA;
use \JsonSerializable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\GDA\AccueilRepository")
* @ORM\Table(name="efc.gda_accueil")
*/
class Accueil implements JsonSerializable
{
const DATE_FORMAT = 'd-m-Y';
const TYPE_NOUVEAUTE_ARRIVEE = "Arrivée";
const TYPE_NOUVEAUTE_ARRIVEEE = "Arrivee";
const TYPE_NOUVEAUTE_PREMIER_ACCUEIL = "1er Accueil";
const TYPE_NOUVEAUTE_MODIFICATION = "Modification";
const TYPE_NOUVEAUTE_DEPART = "Départ";
const TYPE_NOUVEAUTE_DEEPART = "Depart";
use \App\Entity\Commun\IdTrait;
// <editor-fold defaultstate="collapsed" desc="Attributs privés">
/**
* Id de l'assmat dans le SI du département (donnée dupliquée dans les accueils pour faciliter la restitution des données au département)
* @ORM\Column(type="string", length=70)
*/
private $id_fonctionnel_assmat;
/**
* @Assert\Type("\DateTimeInterface")
* @Assert\LessThan("tomorrow midnight")
* @Assert\GreaterThan(propertyPath="enfant.date_naissance", message="Vous ne pouvez pas accueillir un enfant avant sa date de naissance.")
* @ORM\Column(type="date")
*/
private $date_debut;
/**
* @ORM\Column(type="date", nullable=true)
* @Assert\GreaterThan(propertyPath="date_debut", message="La date de fin d'accueil doit être postérieure à la date de début de l'accueil.")
*/
private $date_fin;
/**
* @ORM\Column(type="date", nullable=true)
* @Assert\GreaterThan(propertyPath="date_debut", message="La date prévisionnelle de fin d'accueil doit être postérieure à la date de début de l'accueil.")
*/
private $date_fin_prevue;
/**
* @Assert\Length(
* max = 250,
* maxMessage = "Votre message est trop long."
* )
* @ORM\Column(type="string", length=250, nullable=true)
*/
private $commentaire = '';
/**
* Indique si l'enfant est gardé de nuit par l'assmat
* @ORM\Column(type="boolean", nullable=false)
*/
private $est_nuit;
/**
* Indique si l'accueil de l'enfant suit des horaires atypiques
* @ORM\Column(type="boolean", nullable=false)
*/
private $est_atypique;
/**
* @Assert\NotNull()
* @ORM\ManyToOne(targetEntity="App\Entity\AssistantMaternel\AssistantMaternel", inversedBy="accueils")
* @ORM\JoinColumn(name="id_assmat", referencedColumnName="id")
*/
private $assmat;
//NB : cascade={"persist"} permet de créer les entités filles en cascade
/**
* @Assert\NotNull()
* @Assert\Valid
* @ORM\ManyToOne(targetEntity="App\Entity\GDA\Enfant", inversedBy="accueils", cascade={"persist", "remove"})
* @ORM\JoinColumn(name="id_enfant", referencedColumnName="id")
*/
private $enfant;
/**
* @Assert\NotNull()
* @ORM\ManyToOne(targetEntity="App\Entity\Referentiel\TypeAccueil")
* @ORM\JoinColumn(name="id_type_accueil", referencedColumnName="id")
*/
private $type_accueil;
/**
* @Assert\NotNull()
* @ORM\ManyToOne(targetEntity="App\Entity\Referentiel\LieuAccueil")
* @ORM\JoinColumn(name="id_lieu_accueil", referencedColumnName="id")
*/
private $lieu_accueil;
/**
* @ORM\OneToMany(targetEntity="App\Entity\GDA\PlanningAccueil", mappedBy="accueil", cascade={"persist", "remove"})
* @ORM\OrderBy({"periode" = "ASC","tranche_horaire" = "ASC","jour_semaine" = "ASC"})
*/
private $planning_accueil;
/**
* Indique si l'enfant est absent
* si oui, le planning rattaché à l'accueil n'est pas applicable actuellement = l'enfant n'occupe pas de place d'agrément
* @ORM\Column(type="boolean", nullable=false, options={"default":false})
*/
private $est_absent;
/**
* @Assert\Type("\DateTimeInterface")
* @Assert\NotNull()
* @ORM\Column(type="datetime")
*/
private $date_creation;
/**
* @Assert\Type("\DateTimeInterface")
* @Assert\NotNull()
* @ORM\Column(type="datetime")
*/
private $date_derniere_modification;
/**
* Indique si la déclaration de cet accueil est le 1er pour l'assmat depuis le début de son agrément
* si oui, indiquerea aux pros du département que le 2nd volet de formation doit être planifié
* @ORM\Column(type="boolean", nullable=false, options={"default":false})
*/
private $est_premier_accueil;
/**
* Indique si cet accueil est déclaré comme dérogatoire
* (sans distingo : avec accord préalable pour l'accueil ou nouveauté 2021 50h/mois)
* @ORM\Column(type="boolean", nullable=false, options={"default":false})
*/
private $est_derogation;
/**
* @ORM\OneToOne(targetEntity="App\Entity\GDA\LogAccueil", mappedBy="accueil", cascade={"persist", "remove"})
*/
private $log_accueil; //TODO : traçabilité : à terme sera OneToMany : on aura autant de lignes que de modifs (date, nom du champ, ancienne valeur(tostring) - à plat pour enfant, respleg, planning)
// </editor-fold>
/**
* @Assert\Callback
*/
public function verifierAge(ExecutionContextInterface $context, $payload)
{
if ($this->getEnfant()->getAgeAnnees($this->date_debut) >= 18) {
$context->buildViolation("L'enfant accueilli doit avoir moins de 18 ans.")->atPath('date_naissance')->addViolation();
}
}
// <editor-fold desc="Accesseurs">
/**
* Get assmat
*
* @return Assmat
*/
public function getAssmat(): \App\Entity\AssistantMaternel\AssistantMaternel
{
return $this->assmat;
}
/**
* Set assmat
*
* @param Assmat $assmat
*
* @return Accueil
*/
public function setAssmat($assmat)
{
$this->assmat = $assmat;
$this->id_fonctionnel_assmat = $assmat->getIdFonctionnel();
return $this;
}
/**
* Get enfant
*
* @return Enfant
*/
public function getEnfant()
{
return $this->enfant;
}
/**
* Set enfant
*
* @param Enfant $enfant
*
* @return Accueil
*/
public function setEnfant($enfant)
{
$this->enfant = $enfant;
return $this;
}
/**
* Get lieu_accueil
*
* @return LieuAccueil
*/
public function getLieuAccueil()
{
return $this->lieu_accueil;
}
/**
* Set lieu_accueil
*
* @param LieuAccueil $lieuAccueil
*
* @return Accueil
*/
public function setLieuAccueil($lieuAccueil)
{
$this->lieu_accueil = $lieuAccueil;
return $this;
}
/**
* Get type_accueil
*
* @return TypeAccueil
*/
public function getTypeAccueil()
{
return $this->type_accueil;
}
/**
* Set type_accueil
*
* @param TypeAccueil $typeAccueil
*
* @return Accueil
*/
public function setTypeAccueil($typeAccueil)
{
$this->type_accueil = $typeAccueil;
return $this;
}
/**
* Get date_debut
*
* @return date
*/
public function getDateDebut()
{
return $this->date_debut;
}
/**
* Set date_debut
*
* @param Date $dateDebut
*
* @return Accueil
*/
public function setDateDebut($dateDebut)
{
$this->date_debut = $dateDebut;
return $this;
}
/**
* Get date_fin
*
* @return date
*/
public function getDateFin()
{
return $this->date_fin;
}
/**
* Set date_fin
*
* @param Date $dateFin
*
* @return Accueil
*/
public function setDateFin($dateFin)
{
$this->date_fin = $dateFin;
return $this;
}
/**
* Get date_fin_prevue
*
* @return date
*/
public function getDateFinPrevue()
{
return $this->date_fin_prevue;
}
/**
* Set date_fin
*
* @param Date $dateFinPrevue
*
* @return Accueil
*/
public function setDateFinPrevue($dateFinPrevue)
{
$this->date_fin_prevue = $dateFinPrevue;
return $this;
}
/**
* Obtient le texte à afficher pour la date de fin prévionnelle
* Affiche un texte de remplacement si la valeur est nulle
*
* @return string
*/
public function getTextForDateFinPrevue()
{
if ($this->date_fin_prevue) {
$text = date_format($this->date_fin_prevue, "d/m/Y");
} else {
$text = 'non renseignée';
}
return $text;
}
/**
* Get commentaire
*
* @return string
*/
public function getCommentaire()
{
return $this->commentaire;
}
/**
* Set commentaire
*
* @param string $commentaire
*
* @return Accueil
*/
public function setCommentaire($commentaire)
{
$this->commentaire = $commentaire;
return $this;
}
/**
* Get estNuit
*
* @return string
*/
public function getEstNuit()
{
return $this->est_nuit;
}
/**
* Set estNuit
*
* @param string $estNuit
*
* @return Accueil
*/
public function setEstNuit($estNuit)
{
$this->est_nuit = $estNuit;
return $this;
}
/**
* Get estAtypique
*
* @return string
*/
public function getEstAtypique()
{
return $this->est_atypique;
}
/**
* Set estAtypique
*
* @param string $estAtypique
*
* @return Accueil
*/
public function setEstAtypique($estAtypique)
{
$this->est_atypique = $estAtypique;
return $this;
}
/**
* Get estAbsent
*
* @return string
*/
public function getEstAbsent()
{
return $this->est_absent;
}
/**
* Set estAbsent
*
* @param string $estAbsent
*
* @return Accueil
*/
public function setEstAbsent($estAbsent)
{
$this->est_absent = $estAbsent;
return $this;
}
/**
* Get date_creation
*
* @return date
*/
public function getDateCreation()
{
return $this->date_creation;
}
/**
* Set date_creation
*
* @param Date $dateCreation
*
* @return Accueil
*/
public function setDateCreation($dateCreation)
{
$this->date_creation = $dateCreation;
$this->log_accueil->setDateCreation($dateCreation);
return $this;
}
/**
* Get date_derniere_modification
*
* @return date
*/
public function getDateDerniereModification()
{
return $this->date_derniere_modification;
}
/**
* Set date_modification
*
* @param Date $dateModification
*
* @return Accueil
*/
public function setDateModification($dateModification)
{
$this->date_derniere_modification = $dateModification;
$this->log_accueil->setDateModification($dateModification);
return $this;
}
/**
* Get UtilisateurCreation
*
* @return UtilisateurCreation
*/
public function getUtilisateurCreation()
{
return $this->log_accueil->getUtilisateurCreation();
}
/**
* Set utilisateur_creation
*
* @param Utilisateur $utilisateurCreation
*
* @return Accueil
*/
public function setUtilisateurCreation($utilisateurCreation)
{
$this->log_accueil->setUtilisateurCreation($utilisateurCreation);
return $this;
}
/**
* Get UtilisateurModification
*
* @return UtilisateurModification
*/
public function getUtilisateurModification()
{
return $this->log_accueil->getUtilisateurModification();
}
/**
* Set utilisateur_modification
*
* @param Utilisateur $utilisateurModification
*
* @return Accueil
*/
public function setUtilisateurModification($utilisateurModification)
{
$this->log_accueil->setUtilisateurModification($utilisateurModification);
return $this;
}
/**
* Set log_accueil
*
* @param LogAccueil $logAccueil
*
* @return Accueil
*/
public function setLogAccueil($logAccueil)
{
$this->log_accueil = $logAccueil;
return $this;
}
/**
* Get LogAccueil
*
* @return LogAccueil
*/
public function getLogAccueil()
{
return $this->log_accueil;
}
/**
* Get estPremierAccueil
*
* @return bool
*/
public function getEstPremierAccueil()
{
return $this->est_premier_accueil;
}
/**
* Set estPremierAccueil
*
* @param bool $estPremierAccueil
*
* @return Accueil
*/
public function setEstPremierAccueil($estPremierAccueil)
{
$this->est_premier_accueil = $estPremierAccueil;
return $this;
}
/**
* Get estDerogation
*
* @return bool
*/
public function getEstDerogation()
{
return $this->est_derogation;
}
/**
* Set estDerogation
*
* @param bool $estDerogation
*
* @return Accueil
*/
public function setEstDerogation($estDerogation)
{
$this->est_derogation = $estDerogation;
return $this;
}
/* gestion du planning d'accueil */
public function getPlanningAccueil()
{
return $this->planning_accueil;
}
public function setPlanningAccueil($planning_accueil)
{
$this->planning_accueil = $planning_accueil;
return $this;
}
public function addPlanningAccueil(PlanningAccueil $planning_accueil)
{
$this->planning_accueil[] = $planning_accueil;
return $this;
}
public function removePlanningAccueil(PlanningAccueil $planning_accueil)
{
$this->planning_accueil->removeElement($planning_accueil);
}
/*
* Indique si l'accueil est actif
* RG : l'accueil est actif si la date de fin est null
*/
public function isActif()
{
return is_null($this->date_fin);
}
/*
* Indique si l'accueil est terminé
* RG : l'accueil est terminé si la date de fin n'est pas null et est dans le passé
*/
public function isPast()
{
return !is_null($this->date_fin) && $this->date_fin < new \DateTime();
}
// </editor-fold>
// <editor-fold desc="Méthodes publiques">
/**
* Constructor
*/
public function __construct()
{
}
public function __toString()
{
return json_encode($this->jsonSerialize());
}
/**
* Obtient une nouvelle instance d'Accueil, initialisée à partir du tableau associatif fourni en paramètre
*
* @param array $data
* @return Accueil
*/
public static function createFromArray(array $data)
{
$newInstance = new Accueil();
return $newInstance->initFromArray($data);
}
/**
* mappe les propriétés sur l'instance à partir d'un tableau associatif
*
* @param array $data
* @return void
*/
public function initFromArray(array $data)
{
foreach ($data as $prop => $value) {
$this->{$prop} = $value;
}
return $this;
}
/*
* Implémentation de JsonSerializable
* Ne liste que les champs modifiables par les utilisateurs
*/
public function jsonSerialize()
{
return [
'id' => $this->id,
'assmat' => $this->assmat === null ? null : $this->assmat->getId(),
'enfant' => $this->enfant === null ? null : $this->enfant->jsonSerialize(),
'type_accueil' => $this->type_accueil->jsonSerialize(),
'lieu_accueil' => $this->lieu_accueil->jsonSerialize(),
'id_fonctionnel_assmat' => $this->id_fonctionnel_assmat,
'date_debut' => $this->date_debut === null ? null : $this->date_debut->format(self::DATE_FORMAT),
'date_fin' => $this->date_fin === null ? null : $this->date_fin->format(self::DATE_FORMAT),
'date_fin_prevue' => $this->date_fin_prevue === null ? null : $this->date_fin_prevue->format(self::DATE_FORMAT),
'commentaire' => $this->commentaire,
'est_nuit' => $this->est_nuit,
'est_atypique' => $this->est_atypique,
'est_derogation' => $this->est_derogation,
'est_absent' => $this->est_absent,
'est_premier_accueil' => $this->est_premier_accueil
];
}
/**
* Enregistre la date création/modification et l'utilisateur création/modification
* selon que l'accueil est en train d'être créé ou non
* @param type $currentUser
*/
public function enregistrerInfosCreationModification($currentUser)
{
// Date création et modification : maintenant
$maintenant = new \DateTime();
if (!$this->getId()) { //si c'est une création
$this->setDateCreation($maintenant);
$this->setUtilisateurCreation($currentUser);
}
$this->setDateModification($maintenant);
$this->setUtilisateurModification($currentUser);
}
/**
* Quel est le dernier évènement de modification de l'accueil
* Pour la fonctionnalité Nouveautés
* Arrivée = création de l'accueil
* Départ = fermeture de l'accueil
* Modification = toute autre modification
* @return string
*/
public function getTypeNouveaute(): string
{
if ($this->date_creation == $this->date_derniere_modification) {
if ($this->est_premier_accueil) {
return self::TYPE_NOUVEAUTE_PREMIER_ACCUEIL;
} else {
return self::TYPE_NOUVEAUTE_ARRIVEE;
}
}
if ($this->date_fin) {
return self::TYPE_NOUVEAUTE_DEPART;
}
return self::TYPE_NOUVEAUTE_MODIFICATION;
}
// </editor-fold>
}