src/Entity/Formation/Classe.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Formation;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Entity\User;
  5. use App\Repository\Formation\ClasseRepository;
  6. use App\Traits\Actions;
  7. use App\Entity\Schoolyear;
  8. use DateInterval;
  9. use DatePeriod;
  10. use DateTime;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. #[ORM\Entity(repositoryClassClasseRepository::class)]
  15. #[ORM\Table(name'hs_tc_formation_classe')]
  16. #[ApiResource]
  17. class Classe implements \Stringable
  18. {
  19.     use Actions;
  20.     #[ORM\ManyToOne(inversedBy'classes')]
  21.     private ?Activity $activity null;
  22.     #[ORM\ManyToOne(targetEntitySchoolyear::class)]
  23.     #[ORM\JoinColumn(nullabletrue)]
  24.     private ?Schoolyear $schoolyear null;
  25.     #[ORM\OneToMany(mappedBy"classe"targetEntityParticipant::class)]
  26.     #[ORM\OrderBy(["regNumber" => "ASC"])]
  27.     private Collection $participants;
  28.     #[ORM\OneToMany(mappedBy"classe"targetEntityAttribution::class, cascade: ["persist"], orphanRemovaltrue)]
  29.     private Collection $attributions;
  30.     #[ORM\OneToMany(mappedBy"classe"targetEntityTimetable::class, cascade: ["persist"], orphanRemovaltrue)]
  31.     private Collection $timetables;
  32.     #[ORM\ManyToOne(targetEntityUser::class, inversedBy"classes")]
  33.     #[ORM\JoinColumn(nullabletrue)]
  34.     private ?User $responsable;
  35.     #[ORM\Column(type"string"nullabletrue)]
  36.     private ?string $label;
  37.     #[ORM\Column(name'start_at'type'datetime')]
  38.     private \DateTime $startAt;
  39.     #[ORM\Column(name'end_at'type'datetime')]
  40.     private \DateTime $endAt;
  41.     #[ORM\Column(name"monthly_payment"type"float"nullabletrue)]
  42.     private ?float $monthlyPayment;
  43.     #[ORM\Column(name"register_costs"type"float"nullabletrue)]
  44.     private ?float $registerCosts;
  45.     #[ORM\Column(name"other_costs"type"string"nullabletrue)]
  46.     private ?string $otherCosts;
  47.     #[ORM\Column(type"integer"nullabletrue)]
  48.     private int $nbrMonths=0;
  49.     #[ORM\Column(type"json"nullabletrue)]
  50.     private array $listMonths=[];
  51.     #[ORM\Column(name'graduation_at'type'datetime'nullabletrue)]
  52.     private ?\DateTime $graduationAt=null;
  53.     #[ORM\Column(type'boolean')]
  54.     private bool $opened false;
  55.     public function __construct()
  56.     {
  57.         $this->participants = new ArrayCollection();
  58.         // date_default_timezone_set('Africa/Casablanca');
  59.         $this->createAt=new \DateTime('now');
  60.         $this->published=true;
  61.     }
  62.     /**
  63.      * Set activity
  64.      *
  65.      *
  66.      * @return Classe
  67.      */
  68.     public function setActivity(Activity $activity)
  69.     {
  70.         $this->activity $activity;
  71.         return $this;
  72.     }
  73.     /**
  74.      * Get activity
  75.      *
  76.      * @return Activity
  77.      */
  78.     public function getActivity()
  79.     {
  80.         return $this->activity;
  81.     }
  82.     /**
  83.      * @return Schoolyear
  84.      */
  85.     public function getSchoolyear()
  86.     {
  87.         return $this->schoolyear;
  88.     }
  89.     /**
  90.      * @param Schoolyear|null $schoolyear
  91.      * @return Classe
  92.      */
  93.     public function setSchoolyear(Schoolyear $schoolyear=null)
  94.     {
  95.         $this->schoolyear $schoolyear;
  96.         return $this;
  97.     }
  98.     public function getLabel(): ?string
  99.     {
  100.         return $this->label;
  101.     }
  102.     public function setLabel(?string $label): void
  103.     {
  104.         $this->label $label;
  105.     }
  106.     /**
  107.      * Add participant
  108.      *
  109.      *
  110.      * @return Classe
  111.      */
  112.     public function addParticipant(Participant $participant)
  113.     {
  114.         $participant->setClasse($this);
  115.         $this->participants[] = $participant;
  116.         return $this;
  117.     }
  118.     /**
  119.      * Remove participant
  120.      */
  121.     public function removeParticipant(Participant $participant)
  122.     {
  123.         $this->participants->removeElement($participant);
  124.     }
  125.     /**
  126.      * Get participants
  127.      *
  128.      * @return ArrayCollection
  129.      */
  130.     public function getParticipants()
  131.     {
  132.         return $this->participants;
  133.     }
  134.     public function getNbrParticipants(){
  135.         //$month=(new \DateTime('now'))->format('m-y');
  136.         $n=0;
  137.         /** @var Participant $participant */
  138.         foreach ($this->participants as $participant){
  139.             if($participant->getStatus()!="Abandonné"$n++;
  140.         }
  141.         return $n;
  142.     }
  143.     public function getNbrAbandonnes(){
  144.         //$month=(new \DateTime('now'))->format('m-y');
  145.         $n=0;
  146.         /** @var Participant $participant */
  147.         foreach ($this->participants as $participant){
  148.             if($participant->getStatus()=="Abandonné"$n++;
  149.         }
  150.         return $n;
  151.     }
  152.     /**
  153.      * Add attribution
  154.      *
  155.      *
  156.      * @return Classe
  157.      */
  158.     public function addAttribution(Attribution $attribution)
  159.     {
  160.         $attribution->setClasse($this);
  161.         $this->attributions[] = $attribution;
  162.         return $this;
  163.     }
  164.     /**
  165.      * Remove attribution
  166.      */
  167.     public function removeAttribution(Attribution $attribution)
  168.     {
  169.         $this->attributions->removeElement($attribution);
  170.     }
  171.     /**
  172.      * Get attributions
  173.      *
  174.      * @return Collection
  175.      */
  176.     public function getAttributions()
  177.     {
  178.         return $this->attributions;
  179.     }
  180.     /**
  181.      * Add timetable
  182.      *
  183.      *
  184.      * @return Classe
  185.      */
  186.     public function addTimetable(Timetable $timetable)
  187.     {
  188.         $timetable->setClasse($this);
  189.         $this->timetables[] = $timetable;
  190.         return $this;
  191.     }
  192.     /**
  193.      * Remove timetable
  194.      */
  195.     public function removeTimetable(Timetable $timetable)
  196.     {
  197.         $this->timetables->removeElement($timetable);
  198.     }
  199.     /**
  200.      * Get timetables
  201.      *
  202.      * @return Collection
  203.      */
  204.     public function getTimetables()
  205.     {
  206.         return $this->timetables;
  207.     }
  208.     public function getResponsable(): ?User
  209.     {
  210.         return $this->responsable;
  211.     }
  212.     public function setResponsable(?User $responsable): void
  213.     {
  214.         $this->responsable $responsable;
  215.     }
  216.     public function getMonthlyPayment(): ?float
  217.     {
  218.         return $this->monthlyPayment;
  219.     }
  220.     public function setMonthlyPayment(?float $monthlyPayment): void
  221.     {
  222.         $this->monthlyPayment $monthlyPayment;
  223.     }
  224.     public function getRegisterCosts(): ?float
  225.     {
  226.         return $this->registerCosts;
  227.     }
  228.     public function setRegisterCosts(?float $registerCosts): void
  229.     {
  230.         $this->registerCosts $registerCosts;
  231.     }
  232.     public function getNbrMonths(): int
  233.     {
  234.         return $this->nbrMonths;
  235.     }
  236.     public function setNbrMonths(int $nbrMonths): void
  237.     {
  238.         $this->nbrMonths $nbrMonths;
  239.     }
  240.     public function getListMonths(): array
  241.     {
  242.         return $this->listMonths;
  243.     }
  244.     public function setListMonths(array $listMmonths): Classe
  245.     {
  246.         $this->listMonths $listMmonths;
  247.         return $this;
  248.     }
  249.     public function getStartAt(): \DateTime
  250.     {
  251.         return $this->startAt;
  252.     }
  253.     public function setStartAt(\DateTime $startAt): void
  254.     {
  255.         $this->startAt $startAt;
  256.     }
  257.     public function getEndAt(): \DateTime
  258.     {
  259.         return $this->endAt;
  260.     }
  261.     public function setEndAt(\DateTime $endAt): void
  262.     {
  263.         $this->endAt $endAt;
  264.     }
  265.     public function getGraduationAt(): ?\DateTime
  266.     {
  267.         return $this->graduationAt;
  268.     }
  269.     public function setGraduationAt(?\DateTime $graduationAt): void
  270.     {
  271.         $this->graduationAt $graduationAt;
  272.     }
  273.     public function isOpened(): bool
  274.     {
  275.         return $this->opened;
  276.     }
  277.     public function setOpened(bool $opened): void
  278.     {
  279.         $this->opened $opened;
  280.     }
  281.     public function setPublished(bool $published): void
  282.     {
  283.         if(!$published$this->opened false;
  284.         $this->published $published;
  285.     }
  286.     public function getOtherCosts(): ?string
  287.     {
  288.         return $this->otherCosts;
  289.     }
  290.     public function setOtherCosts(?string $otherCosts): void
  291.     {
  292.         $this->otherCosts $otherCosts;
  293.     }
  294.     public function getOtherCostsArray()
  295.     {
  296.         $costs=[];
  297.         if(trim($this->getOtherCosts())!=null){
  298.             $otherCosts=explode(';'trim($this->getOtherCosts()));
  299.             if(count($otherCosts)>0){
  300.                 foreach ($otherCosts as $otherCost) {
  301.                     if (trim($otherCost)!='') {
  302.                         $costArray=explode('=>',trim($otherCost));
  303.                         if(count($costArray)==2){
  304.                             if(trim($costArray[0])!='' && intval(trim($costArray[1]))>0){
  305.                                 $costs[]=[trim($costArray[0]),intval(trim($costArray[1]))];
  306.                             }
  307.                         }
  308.                     }
  309.                 }
  310.             }
  311.         }
  312.         return $costs;
  313.     }
  314.     public function getMonths(): array
  315.     {
  316.         $months = [];
  317.         $start $this->getStartAt();
  318.         $end $this->getEndAt();
  319.         $interval DateInterval::createFromDateString('1 month');
  320.         $period = new DatePeriod($start$interval$end);
  321.         foreach ($period as $date) {
  322.             $months[] = $date->format('m/y');
  323.         }
  324.         return $months;
  325.     }
  326.     public function getDiplome(){
  327.         return $this->getActivity()->getDiploma();
  328.     }
  329.     public function getButtonOpened()
  330.     {
  331.         $confirm="data-toggle='confirmation'
  332.                 data-btn-ok-label='Oui' 
  333.                 data-btn-ok-icon='glyphicon glyphicon-ok'
  334.                 data-btn-ok-class='btn-success'
  335.                 data-btn-cancel-label='Non' 
  336.                 data-btn-cancel-icon='glyphicon glyphicon-remove'
  337.                 data-btn-cancel-class='btn-danger'
  338.                 data-title=\"Ouvrir l'inscription ?\"
  339.                 data-popout='true'
  340.                 data-content=\"Voulez-vous vraiment ouvrir l'inscription pour cet année scolaire actuelle ? \"";
  341.         if($this->opened)
  342.             $btn="<a ".$confirm." href='javascript:void(0)' class='boledit-redirect' data-id='".$this->id."' data-route='".$this->getRoute('opened')."'><i class=\"text-green fa fa-toggle-on\"> Oui</i></a>";
  343.         else
  344.             $btn="<a ".$confirm." href='javascript:void(0)' class='boledit-redirect' data-id='".$this->id."' data-route='".$this->getRoute('opened')."'><i class=\"text-red fa fa-toggle-off\"> Non</i></a>";
  345.         return $btn;
  346.     }
  347.     public function getActionsS($actionsItems=['edit','show','remove','add_participant'])
  348.     {
  349.         $actions="<div class='btn-group'>" .
  350.             "<button type='button' class='btn btn-default btn-flat dropdown-toggle' data-toggle='dropdown' aria-expanded='false'>" .
  351.             "<span class='fa fa-ellipsis-v'></span>" .
  352.             "<span class='sr-only'>Toggle Dropdown</span>" .
  353.             "</button>" .
  354.             "<ul class='dropdown-menu dropdown-menu-right' role='menu'>";
  355.         if(in_array('edit'$actionsItems))
  356.             $actions.="<li><a title='Modifier' class='action' target='_self' href='javascript:void(0)' data-id='".$this->id."' data-route='".$this->getRoute('edit')."'>"
  357.             ."<i class='fa text-warning fa-fw fa-edit'></i> Modifier</a></li>";
  358.         if(in_array('show'$actionsItems))
  359.             $actions.="<li><a title='Afficher' class='action' target='_self' href='javascript:void(0)' data-id='".$this->id."' data-route='".$this->getRoute('show')."'>"
  360.             ."<i class='fa text-info fa-fw fa-eye'></i> Afficher</a></li>";
  361.         if(in_array('remove'$actionsItems))
  362.             $actions.="<li><a title='Supprimer ?' 
  363.                 data-toggle='confirmation'
  364.                 data-btn-ok-label='Supprimer' 
  365.                 data-btn-ok-icon='glyphicon glyphicon-ok'
  366.                 data-btn-ok-class='btn-success'
  367.                 data-btn-cancel-label='Annuler' 
  368.                 data-btn-cancel-icon='glyphicon glyphicon-remove'
  369.                 data-btn-cancel-class='btn-danger'
  370.                 data-title='Supprimer ?'
  371.                 data-popout='true'
  372.                 data-content='Voulez-vous vraiment supprimer cet élément ?' 
  373.                 class='action' target='_self' href='javascript:void(0)' data-id='".$this->id."' data-route='".$this->getRoute('remove')."'>"
  374.             ."<i class='fa text-red fa-fw fa-trash-o'></i> Supprimer</a></li>";
  375. //        $actions.="<li><a title='Liste des stagiaires' class='action' target='_self' href='javascript:void(0)' data-id='".$this->id."' data-route='hs_activity_center_participant_list'>"
  376. //            ."<i class='fa text-info fa-fw fa-list-alt'></i> Stagiaires</a></li>";
  377.         if(in_array('add_participant'$actionsItems))
  378.             $actions.="<li><a title='Ajouter un nouveau participant' class='action' target='_self' href='javascript:void(0)' data-id='".$this->id."' data-route='hs_training_center_participant_new'>"
  379.             ."<i class='fa text-success fa-fw fa-plus'></i> Participant</a></li>";
  380. //        $actions.="<li><a title='Ajouter un nouveau stagiaire' class='action' target='_self' href='javascript:void(0)' data-id='".$this->id."' data-route='hs_tc_faabsence_index'>"
  381. //            ."<i class='fa fa-check-circle'></i> Marquer l'aabsence</a></li>";
  382.         $actions.="</ul></div>";
  383.         return $actions;
  384.     }
  385.     public function getPay(){
  386.         $pay=0;
  387.         foreach ($this->getParticipants() as $participant) {
  388.             $pay+=$participant->getPay();
  389.         }
  390.         return $pay;
  391.     }
  392.     public function getBgClass($m){
  393.         if($this->getCurrent()==$m){
  394.             return 'bg-yellow';
  395.         }
  396.         elseif($this->getCurrent()=='before'){
  397.             return 'bg-red';
  398.         }
  399.         else return 'bg-gray';
  400.     }
  401.     public function getCurrent(){
  402.         // date_default_timezone_set('Africa/Casablanca');
  403.         $now=new \DateTime('now');
  404.         $endAt= clone $this->getEndAt();
  405.         //$endAt->modify('+1 years');
  406.         if($now<$this->getStartAt()) return 'before';
  407.         elseif($now>$endAt) return 'after';
  408.         else return $now->format('m-y');
  409.     }
  410.     public function __toString(): string
  411.     {
  412.         return $this->label??$this->activity->getTitle()." ".$this->schoolyear;
  413.     }
  414.     function getAbreviation() {
  415.         $mots explode(' '$this);
  416.         $abreviation '';
  417.         foreach ($mots as $mot) {
  418.             $abreviation .= strtoupper($mot[0]);
  419.         }
  420.         return $abreviation;
  421.     }
  422.     public function getParticipantsSortedByAbsence($nombre$startAt$endAt){
  423.         // Récupérer tous les stagiaires
  424.         $participants $this->getParticipants();
  425.         // Créer un tableau associatif pour stocker le nombre d'absences de chaque stagiaire
  426.         $participantAbsences = [];
  427.         /** @var Participant $participant */
  428.         foreach ($participants as $participant) {
  429.             // Compter les absences pour chaque stagiaire
  430.             if($participant->getStatus()!="Abandonné")
  431.                 $participantAbsences[] = [
  432.                     'participant' => $participant,
  433.                     'absenceCount' => count($participant->getAbsencesByPeriod($startAt$endAt)),
  434.                 ];
  435.         }
  436.         usort($participantAbsences, function ($a$b) {
  437.             return $b['absenceCount'] <=> $a['absenceCount'];
  438.         });
  439.         // Extraire les 10 stagiaires avec le plus d'absences
  440.         $top10Participants array_slice($participantAbsences0$nombretrue);
  441.         return array_column($top10Participants'participant');
  442.     }
  443.     public function getUnitesSortedByAbsence($nombre){
  444.         // Récupérer tous les stagiaires
  445.         $assigns $this->getAttributions();
  446.         // Créer un tableau associatif pour stocker le nombre d'absences de chaque stagiaire
  447.         $assignsAbsences = [];
  448.         /** @var Participant $participant */
  449.         foreach ($assigns as $assign) {
  450.             // Compter les absences pour chaque stagiaire
  451.             $assignsAbsences[] = [
  452.                 'assign' => $assign,
  453.                 'absenceCount' => count($assign->getAbsences()),
  454.             ];
  455.         }
  456.         usort($assignsAbsences, function ($a$b) {
  457.             return $b['absenceCount'] <=> $a['absenceCount'];
  458.         });
  459.         // Extraire les 10 stagiaires avec le plus d'absences
  460.         $top10Assigns array_slice($assignsAbsences0$nombretrue);
  461.         return array_column($top10Assigns'assign');
  462.     }
  463.     public function getSeances()
  464.     {
  465.         $allSeances = [];
  466.         foreach ($this->getAttributions() as $assign) {
  467.             $allSeances=array_merge($allSeances$assign->getSeances());
  468.         }
  469.         usort($allSeances, function($a$b) {
  470.             $dateA DateTime::createFromFormat('d-m-Y H:i'$a->getTakesplaceOn()->format('d-m-Y').' '.$a->getStartAt());
  471.             $dateB DateTime::createFromFormat('d-m-Y H:i'$b->getTakesplaceOn()->format('d-m-Y').' '.$b->getStartAt());
  472.             return $dateA <=> $dateB;
  473.         });
  474.         return $allSeances;
  475.     }
  476.     public function getSeancesCancelled()
  477.     {
  478.         $allSeances = [];
  479.         foreach ($this->getAttributions() as $assign) {
  480.             $allSeances=array_merge($allSeances$assign->getSeancesCancelled());
  481.         }
  482.         usort($allSeances, function($a$b) {
  483.             $dateA DateTime::createFromFormat('d-m-Y H:i'$a['seanceDate'].' '.$a['timetableitem']->getStartAt());
  484.             $dateB DateTime::createFromFormat('d-m-Y H:i'$b['seanceDate'].' '.$b['timetableitem']->getStartAt());
  485.             return $dateA <=> $dateB;
  486.         });
  487.         return $allSeances;
  488.     }
  489.     public function getSeancesNotCreate()
  490.     {
  491.         $allSeances = [];
  492.         foreach ($this->getAttributions() as $assign) {
  493.             $allSeances=array_merge($allSeances$assign->getSeancesNotCreate());
  494.         }
  495.         usort($allSeances, function($a$b) {
  496.             $dateA DateTime::createFromFormat('d-m-Y H:i'$a['seanceDate'].' '.$a['timetableitem']->getStartAt());
  497.             $dateB DateTime::createFromFormat('d-m-Y H:i'$b['seanceDate'].' '.$b['timetableitem']->getStartAt());
  498.             return $dateA <=> $dateB;
  499.         });
  500.         return $allSeances;
  501.     }
  502.     public function getToPay($type){
  503.         $mCosts=0;
  504.         foreach ($this->participants as $participant) {
  505.             $mCosts+=$participant->getPay($type);
  506.         }
  507.         return $mCosts;
  508.     }
  509.     public function getTotalPay($costs){
  510.         $m=0;
  511.         /** @var Participant $participant */
  512.         foreach ($this->participants as $participant) {
  513.             $m+=$participant->getTotalPay($costs);
  514.         }
  515.         return $m;
  516.     }
  517.     public function getTotalPayByMonth($month)
  518.     {
  519.         $sum=0;
  520.         /** @var Participant $participant */
  521.         foreach ($this->participants as $participant){
  522.             if ($participant->getCostByMonth($month)!==null)
  523.             $sum+=$participant->getCostByMonth($month)->amountPaied();
  524.         }
  525.         return $sum;
  526.     }
  527.     public function getRestPay($costs){
  528.         $m=0;
  529.         foreach ($this->participants as $participant) {
  530.             $m+=$participant->getRestPay($costs);
  531.         }
  532.         return $m;
  533.     }
  534.     public function getNbrRetard($costs){
  535.         $m=0;
  536.         foreach ($this->participants as $participant) {
  537.             $m=$participant->getRestPay($costs)>0?$m+1:$m;
  538.         }
  539.         return $m;
  540.     }
  541.     public function getAdvance($costs){
  542.         $m=0;
  543.         foreach ($this->participants as $participant) {
  544.             $m+=$participant->getAdvance($costs);
  545.         }
  546.         return $m;
  547.     }
  548. }