src/Entity/Training/Assignation.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Training;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Entity\Training\Noteassign;
  5. use App\Repository\Training\AssignationRepository;
  6. use App\Traits\Actions;
  7. use DateInterval;
  8. use DateTime;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. #[ORM\Entity(repositoryClassAssignationRepository::class)]
  13. #[ORM\Table(name'hs_tc_training_assignation')]
  14. #[ApiResource]
  15. class Assignation implements \Stringable
  16. {
  17.     use Actions;
  18.     #[ORM\ManyToOne(targetEntityModule::class, inversedBy"assignations")]
  19.     #[ORM\JoinColumn(nullablefalse)]
  20.     private Module $module;
  21.     #[ORM\ManyToOne(targetEntityFormer::class, inversedBy"assignations")]
  22.     #[ORM\JoinColumn(nullablefalse)]
  23.     private Former $former;
  24.     #[ORM\ManyToOne(targetEntityGroupe::class, inversedBy"assignations")]
  25.     #[ORM\JoinColumn(nullablefalse)]
  26.     private Groupe $groupe;
  27.     #[ORM\OneToMany(mappedBy"assignation"targetEntityNoteassign::class, cascade: ["persist"], orphanRemovaltrue)]
  28.     private Collection $notes;
  29.     #[ORM\Column(name"nbr_controle"type"integer"length255)]
  30.     private int $nbrControle;
  31.     #[ORM\Column()]
  32.     private ?int $hourlyVolume null;
  33.     #[ORM\Column(name'type_pay'type'string'length255nullabletrue)]
  34.     private ?string $typePay null;
  35.     #[ORM\Column(name'pc_pay'type'float'nullabletrue)]
  36.     private ?float $pcPay null;
  37.     #[ORM\OneToMany(mappedBy'assignation'targetEntityScheduleitem::class)]
  38.     private Collection $scheduleitems;
  39.     
  40.     public function __construct()
  41.     {
  42.         // date_default_timezone_set('Africa/Casablanca');
  43.         $this->createAt=new \DateTime('now');
  44.         $this->published=true;
  45.         $this->notes=new ArrayCollection();
  46.         $this->scheduleitems = new ArrayCollection();
  47.     }
  48.     /**
  49.      * @return Module
  50.      */
  51.     public function getModule(): ?Module
  52.     {
  53.         return $this->module;
  54.     }
  55.     /**
  56.      * @param Module $module
  57.      */
  58.     public function setModule(?Module $module): Assignation
  59.     {
  60.         $this->module $module;
  61.         /** @var Notemodule $notemodule */
  62.         foreach ($module->getNotes() as $notemodule){
  63.             $note=new Noteassign();
  64.             $note->setNotemodule($notemodule);
  65.             $this->addNote($note);
  66.         }
  67.         return $this;
  68.     }
  69.     /**
  70.      * @return Former
  71.      */
  72.     public function getFormer(): ?Former
  73.     {
  74.         return $this->former;
  75.     }
  76.     /**
  77.      * @param Former $former
  78.      */
  79.     public function setFormer(?Former $former): Assignation
  80.     {
  81.         $this->former $former;
  82.         return $this;
  83.     }
  84.     /**
  85.      * @return Groupe
  86.      */
  87.     public function getGroupe(): ?Groupe
  88.     {
  89.         return $this->groupe;
  90.     }
  91.     /**
  92.      * @param Groupe $groupe
  93.      */
  94.     public function setGroupe(?Groupe $groupe): Assignation
  95.     {
  96.         $this->groupe $groupe;
  97.         return $this;
  98.     }
  99.     public function getNbrControle(): int
  100.     {
  101.         return $this->nbrControle!=null?$this->nbrControle:$this->module->getNbrCC();
  102.     }
  103.     public function setNbrControle(int $nbrControle): Assignation
  104.     {
  105.         $this->nbrControle $nbrControle;
  106.         return $this;
  107.     }
  108.     /**
  109.      * Add note
  110.      *
  111.      *
  112.      * @return Assignation
  113.      */
  114.     public function addNote(Noteassign $note)
  115.     {
  116.         $note->setAssignation($this);
  117.         $this->notes->add($note);
  118.         return $this;
  119.     }
  120.     /**
  121.      * Remove note
  122.      */
  123.     public function removeNote(Noteassign $note)
  124.     {
  125.         $this->notes->removeElement($note);
  126.     }
  127.     /**
  128.      * Get notes
  129.      *
  130.      * @return Collection
  131.      */
  132.     public function getNotes()
  133.     {
  134.         return $this->notes;
  135.     }
  136.     public function getNoteAssignByNoteModule(Notemodule $notemodule): ?Noteassign
  137.     {
  138.         foreach($this->notes as $note){
  139.             if ($note->getNotemodule()==$notemodule){
  140.                 return $note;
  141.             }
  142.         }
  143.         return null;
  144.     }
  145.     public function getPCInput(){
  146.         $sum=0;
  147.         $i=0;
  148.         /** @var Noteassign $line */
  149.         foreach ($this->getNotes() as $line){
  150.             /** @var Note $note */
  151.             foreach ($line->getNotes() as $note){
  152.                 if($note->getTrainee()->getStatus()=='En cours'){
  153.                     $sum++;
  154.                     if($note->getVal()!== null$i++;
  155.                 }
  156.             }
  157.         }
  158.         if($sum!=0) return floor($i/$sum*100);
  159.         else return 0;
  160.     }
  161.     public function __toString(): string
  162.     {
  163.         return $this->groupe.'<--->'.$this->module;
  164.     }
  165.     public function getSY(){
  166.         return match ($this->getModule()->getSemester()){
  167.             'Semestre 1''Semestre 2' => $this->getGroupe()->getFirstYear(),
  168.             'Semestre 3''Semestre 4' => $this->getGroupe()->getSecondYear(),
  169.             'Semestre 5''Semestre 6' => $this->getGroupe()->getThirdYear(),
  170.         };
  171.     }
  172.     /**
  173.      * @return Collection<int, Scheduleitem>
  174.      */
  175.     public function getHourlyVolume(): ?int
  176.     {
  177.         return $this->hourlyVolume;
  178.     }
  179.     public function setHourlyVolume(?int $hourlyVolume): void
  180.     {
  181.         $this->hourlyVolume $hourlyVolume;
  182.     }
  183.     public function getTypePay(): ?string
  184.     {
  185.         return $this->typePay;
  186.     }
  187.     public function setTypePay(?string $typePay): void
  188.     {
  189.         $this->typePay $typePay;
  190.     }
  191.     public function getPcPay(): ?float
  192.     {
  193.         return $this->pcPay;
  194.     }
  195.     public function setPcPay(?float $pcPay): void
  196.     {
  197.         $this->pcPay $pcPay;
  198.     }
  199.     public function getTypePayText()
  200.     {
  201.         return match ($this->typePay) {
  202.             'hourly' => 'à l\'heure',
  203.             'Salary' => 'Salaire mensuel',
  204.             default => 'non indiqué',
  205.         };
  206.     }
  207.     function getHourlyVolumeAsseconds() {
  208.         return ($this->getHourlyVolume() * 60 60) ;
  209.     }
  210.     public function getScheduleitems(): Collection
  211.     {
  212.         return $this->scheduleitems;
  213.     }
  214.     public function addScheduleitem(Scheduleitem $scheduleitem): self
  215.     {
  216.         if (!$this->scheduleitems->contains($scheduleitem)) {
  217.             $this->scheduleitems->add($scheduleitem);
  218.             $scheduleitem->setAssignation($this);
  219.         }
  220.         return $this;
  221.     }
  222.     public function removeScheduleitem(Scheduleitem $scheduleitem): self
  223.     {
  224.         if ($this->scheduleitems->removeElement($scheduleitem)) {
  225.             // set the owning side to null (unless already changed)
  226.             if ($scheduleitem->getAssignation() === $this) {
  227.                 $scheduleitem->setAssignation(null);
  228.             }
  229.         }
  230.         return $this;
  231.     }
  232.     public function getDateControle($name): ?\DateTime{
  233.         foreach ($this->getNotes() as $note){
  234.             if($note->getNotemodule()->getName()===$name) return $note->getCarriedOutAt();
  235.         }
  236.         return null;
  237.     }
  238.     public function getSeances()
  239.     {
  240.         $allSeances = [];
  241.         // Parcourir chaque ScheduleItem
  242.         foreach ($this->getScheduleItems() as $scheduleItem) {
  243.             // Ajouter chaque séance de ScheduleItem à la collection $allSeances
  244.             foreach ($scheduleItem->getSeances() as $seance) {
  245.                 $allSeances[]=$seance;
  246.             }
  247.         }
  248.         usort($allSeances, function($a$b) {
  249.             $dateA DateTime::createFromFormat('d-m-Y H:i'$a->getTakesplaceOn()->format('d-m-Y').' '.$a->getStartAt());
  250.             $dateB DateTime::createFromFormat('d-m-Y H:i'$b->getTakesplaceOn()->format('d-m-Y').' '.$b->getStartAt());
  251.             return $dateA <=> $dateB;
  252.         });
  253.         return $allSeances;
  254.     }
  255.     public function getSeancesCancelled()
  256.     {
  257.         $allSeances = [];
  258.         foreach ($this->getScheduleItems() as $scheduleItem) {
  259.             foreach ($scheduleItem->getSeancesCancelled() as $seance ) {
  260.                 $allSeances[] = ['scheduleitem'=>$scheduleItem'seanceDate'=>$seance];
  261.             }
  262.         }
  263.         usort($allSeances, function($a$b) {
  264.             $dateA DateTime::createFromFormat('d-m-Y H:i'$a['seanceDate'].' '.$a['scheduleitem']->getStartAt());
  265.             $dateB DateTime::createFromFormat('d-m-Y H:i'$b['seanceDate'].' '.$b['scheduleitem']->getStartAt());
  266.             return $dateA <=> $dateB;
  267.         });
  268.         return $allSeances;
  269.     }
  270.     public function getSeancesNotCreate()
  271.     {
  272.         $allSeances = [];
  273.         foreach ($this->getScheduleItems() as $scheduleItem) {
  274.             foreach ($scheduleItem->getSeancesNotCreate() as $seance ) {
  275.                 $allSeances[] = ['scheduleitem'=>$scheduleItem'seanceDate'=>$seance];
  276.             }
  277.         }
  278.         usort($allSeances, function($a$b) {
  279.             $dateA DateTime::createFromFormat('d-m-Y H:i'$a['seanceDate'].' '.$a['scheduleitem']->getStartAt());
  280.             $dateB DateTime::createFromFormat('d-m-Y H:i'$b['seanceDate'].' '.$b['scheduleitem']->getStartAt());
  281.             return $dateA <=> $dateB;
  282.         });
  283.         return $allSeances;
  284.     }
  285.     public function getSeancesCounted()
  286.     {
  287.         $items=[];
  288.         /** @var Seance $seance */
  289.         foreach ($this->getSeances() as $seance){
  290.             if ($seance->isPublished()) $items[]=$seance;
  291.         }
  292.         return $items;
  293.     }
  294.     public function getStartAt()
  295.     {
  296.         $start=null;
  297.         /** @var Seance $seance */
  298.         foreach ($this->getSeances() as $seance){
  299.             if($start==null || $seance->getTakesplaceOn()<$start){
  300.                 $start=$seance->getTakesplaceOn();
  301.             }
  302.         }
  303.         return $start;
  304.     }
  305.     public function getAbsences()
  306.     {
  307.         $absences=[];
  308.         /** @var Seance $seance */
  309.         foreach ($this->getSeances() as $seance){
  310.             /** @var Absence $absence */
  311.             foreach ($seance->getAbsencesPublished() as $absence){
  312.                 $absences[]=$absence;
  313.             }
  314.         }
  315.         return $absences;
  316.     }
  317.     /**
  318.      * Retourne la date de la dernière séance (takesPlaceOn) de l'assignation.
  319.      *
  320.      * @return null|\DateTimeInterface
  321.      */
  322.     public function getEndAt(): ?\DateTimeInterface
  323.     {
  324.         $end null;
  325.         /** @var Seance $seance */
  326.         foreach ($this->getSeances() as $seance) {
  327.             $date $seance->getTakesplaceOn();
  328.             if ($date instanceof \DateTimeInterface) {
  329.                 if ($end === null || $date $end) {
  330.                     $end $date;
  331.                 }
  332.             }
  333.         }
  334.         return $end;
  335.     }
  336.     public function getHoulyAchieved(string $type)
  337.     {
  338. //        foreach ($this->get)
  339.         $date1 = new DateTime();
  340.         $date2 = clone $date1;
  341.         $date2->modify("+{$this->getHoulyAchievedAsSeconds($type)} seconds");
  342.         return $date1->diff($date2);
  343.     }
  344.     public function getHoulyAchievedAsSeconds(string $type)
  345.     {
  346.         $total=0;
  347.         if($type=='seance'){
  348.             foreach ($this->getScheduleitems() as $scheduleitem){
  349.                 foreach ($scheduleitem->getSeances() as $seance){
  350.                     $total+=$seance->getLengthAsSeconds();
  351.                 }
  352.             }
  353.         }
  354.         else{
  355.             foreach ($this->getScheduleitems() as $scheduleitem){
  356.                 $total+=$scheduleitem->getLengthAsSeconds();
  357.             }
  358.         }
  359.         return $total;
  360.     }
  361.     public function getHoulyCountedAsSeconds()
  362.     {
  363.         $total=0;
  364.         foreach ($this->getSeancesCounted() as $seance){
  365.             $total+=$seance->getLengthAsSeconds();
  366.         }
  367.         return $total;
  368.     }
  369.     public function getHoulyAchievedAsHoure(string $type)
  370.     {
  371.         return $this->getHoulyAchievedAsSeconds($type)/3600;
  372.     }
  373.     public function getHoulyCountedAsHoure()
  374.     {
  375.         return $this->getHoulyCountedAsSeconds()/3600;
  376.     }
  377.     public function getHoulyAchievedAsStr(string $type)
  378.     {
  379.         //return $this->getHoulyAchieved()->format('%hh%imin');
  380.         $h=floor($this->getHoulyAchievedAsSeconds($type)/3600);
  381.         $m=($this->getHoulyAchievedAsSeconds($type)%3600)/60;
  382.         return $h.'h'.$m.'min';
  383.     }
  384.     public function getHoulyCountedAsStr()
  385.     {
  386.         $h=floor($this->getHoulyCountedAsSeconds()/3600);
  387.         $m=($this->getHoulyCountedAsSeconds()%3600)/60;
  388.         return $h.'h'.$m.'min';
  389.     }
  390.     public function getHoulyPercentage(string $type){
  391.         return $this->getHourlyVolumeAsseconds()==0?0:$this->getHoulyAchievedAsSeconds($type)/$this->getHourlyVolumeAsseconds()*100;
  392.     }
  393.     public function getSchoolYear()
  394.     {
  395.         $level=$this->getModule()->getLevel();
  396.         return $this->groupe->geSchoolYear($level);
  397.     }
  398.     public function getRemunerationDue()
  399.     {
  400.         if($this->former->getTypePay()!=='Salary')
  401.         return $this->getHoulyCountedAsHoure()*$this->getPcPay();
  402.         else return 0;
  403.     }
  404.     public function getSeancesByMonth($month)
  405.     {
  406.         $seances=new ArrayCollection();
  407.         foreach ($this->getScheduleitems() as $scheduleitem){
  408.             foreach ($scheduleitem->getSeances() as $seance){
  409.                 if($seance->getTakesplaceOn()->format('m/y')==$month$seances->add($seance);
  410.             }
  411.         }
  412.         return $seances;
  413.     }
  414.     public function getSeancesCountedByMonth($month)
  415.     {
  416.         $seances=new ArrayCollection();
  417.         foreach ($this->getScheduleitems() as $scheduleitem){
  418.             /** @var Seance $seance */
  419.             foreach ($scheduleitem->getSeances() as $seance){
  420.                 if($seance->getTakesplaceOn()->format('m/y')==$month && $seance->isPublished()) $seances->add($seance);
  421.             }
  422.         }
  423.         return $seances;
  424.     }
  425.     public function getSecondsWorkedByMonth($month)
  426.     {
  427.        $s=0;
  428.         foreach ($this->getSeancesByMonth($month) as $seance){
  429.             $s+=$seance->getLengthAsSeconds();
  430.         }
  431.         return $s;
  432.     }
  433.     public function getSecondsWorkedCountedByMonth($month)
  434.     {
  435.        $s=0;
  436.         foreach ($this->getSeancesCountedByMonth($month) as $seance){
  437.             $s+=$seance->getLengthAsSeconds();
  438.         }
  439.         return $s;
  440.     }
  441.     public function getHourelyWorkedByMonth($month)
  442.     {
  443.         return $this->getSecondsWorkedByMonth($month)/3600;
  444.     }
  445.     public function getHourelyWorkedCountedByMonth($month)
  446.     {
  447.         return $this->getSecondsWorkedCountedByMonth($month)/3600;
  448.     }
  449.     public function getHourelyWorkedByMonthAsStr($month)
  450.     {
  451.         $h=floor($this->getSecondsWorkedByMonth($month)/3600);
  452.         $m=floor(($this->getSecondsWorkedByMonth($month)%3600)/60);
  453.         return $h.'h'.$m.'min';
  454.     }
  455.     public function getHourelyWorkedCountedByMonthAsStr($month)
  456.     {
  457.         $h=floor($this->getSecondsWorkedCountedByMonth($month)/3600);
  458.         $m=floor(($this->getSecondsWorkedCountedByMonth($month)%3600)/60);
  459.         return $h.'h'.$m.'min';
  460.     }
  461.     public function getRemunerationByMonth($month)
  462.     {
  463.         if($this->typePay=='hourly') return $this->pcPay*$this->getHourelyWorkedCountedByMonth($month);
  464.         else return $this->pcPay;
  465.     }
  466. }