src/Entity/Formation/Attribution.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Formation;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Repository\Formation\AttributionRepository;
  5. use App\Traits\Actions;
  6. use DateInterval;
  7. use DateTime;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. #[ORM\Entity(repositoryClassAttributionRepository::class)]
  12. #[ORM\Table(name'hs_tc_formation_attribution')]
  13. #[ApiResource]
  14. class Attribution implements \Stringable
  15. {
  16.     use Actions;
  17.     #[ORM\ManyToOne(targetEntityUnite::class, inversedBy"attributions")]
  18.     #[ORM\JoinColumn(nullablefalse)]
  19.     private Unite $unite;
  20.     #[ORM\ManyToOne(targetEntityIntervenant::class, inversedBy"attributions")]
  21.     #[ORM\JoinColumn(nullablefalse)]
  22.     private Intervenant $intervenant;
  23.     #[ORM\ManyToOne(targetEntityClasse::class, inversedBy"attributions")]
  24.     #[ORM\JoinColumn(nullablefalse)]
  25.     private Classe $classe;
  26.     #[ORM\Column(name'type_pay'type'string'length255nullabletrue)]
  27.     private ?string $typePay null;
  28.     #[ORM\Column(name'pc_pay'type'float'nullabletrue)]
  29.     private ?float $pcPay null;
  30.     #[ORM\Column()]
  31.     private ?int $hourlyVolume null;
  32.     #[ORM\OneToMany(mappedBy'attribution'targetEntityTimetableitem::class)]
  33.     private Collection $timetableitems;
  34.     
  35.     public function __construct()
  36.     {
  37.         // date_default_timezone_set('Africa/Casablanca');
  38.         $this->createAt=new \DateTime('now');
  39.         $this->published=true;
  40.         $this->timetableitems = new ArrayCollection();
  41.     }
  42.     /**
  43.      * @return Unite
  44.      */
  45.     public function getUnite(): ?Unite
  46.     {
  47.         return $this->unite;
  48.     }
  49.     /**
  50.      * @param Unite $unite
  51.      */
  52.     public function setUnite(?Unite $unite): Attribution
  53.     {
  54.         $this->unite $unite;
  55.         return $this;
  56.     }
  57.     /**
  58.      * @return Intervenant
  59.      */
  60.     public function getIntervenant(): ?Intervenant
  61.     {
  62.         return $this->intervenant;
  63.     }
  64.     /**
  65.      * @param Intervenant $intervenant
  66.      */
  67.     public function setIntervenant(?Intervenant $intervenant): Attribution
  68.     {
  69.         $this->intervenant $intervenant;
  70.         return $this;
  71.     }
  72.     /**
  73.      * @return Classe
  74.      */
  75.     public function getClasse(): ?Classe
  76.     {
  77.         return $this->classe;
  78.     }
  79.     /**
  80.      * @param Classe $classe
  81.      */
  82.     public function setClasse(?Classe $classe): Attribution
  83.     {
  84.         $this->classe $classe;
  85.         return $this;
  86.     }
  87.     public function getTypePay(): ?string
  88.     {
  89.         return $this->typePay;
  90.     }
  91.     public function setTypePay(?string $typePay): void
  92.     {
  93.         $this->typePay $typePay;
  94.     }
  95.     public function getPcPay(): ?float
  96.     {
  97.         return $this->pcPay;
  98.     }
  99.     public function setPcPay(?float $pcPay): void
  100.     {
  101.         $this->pcPay $pcPay;
  102.     }
  103.     public function getTypePayText()
  104.     {
  105.         return match ($this->typePay) {
  106.             'Pourcentage' => 'Pourcentage',
  107.             'hourly' => 'à l\'heure',
  108.             'Salary' => 'Salaire mensuel',
  109.             default => 'non indiqué',
  110.         };
  111.     }
  112.     public function __toString(): string
  113.     {
  114.         return $this->classe.'<--->'.$this->unite;
  115.     }
  116.     /**
  117.      * @return Collection<int, Timetableitem>
  118.      */
  119.     public function getHourlyVolume(): ?int
  120.     {
  121.         return $this->hourlyVolume;
  122.     }
  123.     public function setHourlyVolume(?int $hourlyVolume): void
  124.     {
  125.         $this->hourlyVolume $hourlyVolume;
  126.     }
  127.     function getHourlyVolumeAsseconds() {
  128.         return ($this->getHourlyVolume() * 60 60) ;
  129.     }
  130.     public function getTimetableitems(): Collection
  131.     {
  132.         return $this->timetableitems;
  133.     }
  134.     public function addTimetableitem(Timetableitem $timetableitem): self
  135.     {
  136.         if (!$this->timetableitems->contains($timetableitem)) {
  137.             $this->timetableitems->add($timetableitem);
  138.             $timetableitem->setAttribution($this);
  139.         }
  140.         return $this;
  141.     }
  142.     public function removeTimetableitem(Timetableitem $timetableitem): self
  143.     {
  144.         if ($this->timetableitems->removeElement($timetableitem)) {
  145.             // set the owning side to null (unless already changed)
  146.             if ($timetableitem->getAttribution() === $this) {
  147.                 $timetableitem->setAttribution(null);
  148.             }
  149.         }
  150.         return $this;
  151.     }
  152.     public function getAseances()
  153.     {
  154.         $aseances=new ArrayCollection();
  155.         foreach ($this->getTimetableitems() as $timetableitem){
  156.             foreach ($timetableitem->getAseances() as $aseance){
  157.                 $aseances->add($aseance);
  158.             }
  159.         }
  160.         return $aseances;
  161.     }
  162.     public function getSeances()
  163.     {
  164.         $allSeances = [];
  165.         // Parcourir chaque TimetableItem
  166.         foreach ($this->getTimetableItems() as $timetableItem) {
  167.             // Ajouter chaque séance de TimetableItem à la collection $allSeances
  168.             foreach ($timetableItem->getAseances() as $seance) {
  169.                 $allSeances[]=$seance;
  170.             }
  171.         }
  172.         usort($allSeances, function($a$b) {
  173.             $dateA DateTime::createFromFormat('d-m-Y H:i'$a->getTakesplaceOn()->format('d-m-Y').' '.$a->getStartAt());
  174.             $dateB DateTime::createFromFormat('d-m-Y H:i'$b->getTakesplaceOn()->format('d-m-Y').' '.$b->getStartAt());
  175.             return $dateA <=> $dateB;
  176.         });
  177.         return $allSeances;
  178.     }
  179.     public function getSeancesCancelled()
  180.     {
  181.         $allSeances = [];
  182.         foreach ($this->getTimetableItems() as $timetableItem) {
  183.             foreach ($timetableItem->getSeancesCancelled() as $seance ) {
  184.                 $allSeances[] = ['timetableitem'=>$timetableItem'seanceDate'=>$seance];
  185.             }
  186.         }
  187.         usort($allSeances, function($a$b) {
  188.             $dateA DateTime::createFromFormat('d-m-Y H:i'$a['seanceDate'].' '.$a['timetableitem']->getStartAt());
  189.             $dateB DateTime::createFromFormat('d-m-Y H:i'$b['seanceDate'].' '.$b['timetableitem']->getStartAt());
  190.             return $dateA <=> $dateB;
  191.         });
  192.         return $allSeances;
  193.     }
  194.     public function getSeancesNotCreate()
  195.     {
  196.         $allSeances = [];
  197.         foreach ($this->getTimetableItems() as $timetableItem) {
  198.             foreach ($timetableItem->getSeancesNotCreate() as $seance ) {
  199.                 $allSeances[] = ['timetableitem'=>$timetableItem'seanceDate'=>$seance];
  200.             }
  201.         }
  202.         usort($allSeances, function($a$b) {
  203.             $dateA DateTime::createFromFormat('d-m-Y H:i'$a['seanceDate'].' '.$a['timetableitem']->getStartAt());
  204.             $dateB DateTime::createFromFormat('d-m-Y H:i'$b['seanceDate'].' '.$b['timetableitem']->getStartAt());
  205.             return $dateA <=> $dateB;
  206.         });
  207.         return $allSeances;
  208.     }
  209.     public function getAbsences()
  210.     {
  211.         $absences=[];
  212.         /** @var Aseance $seance */
  213.         foreach ($this->getAseances() as $seance){
  214.             /** @var Aabsence $absence */
  215.             foreach ($seance->getAbsencesPublished() as $absence){
  216.                 $absences[]=$absence;
  217.             }
  218.         }
  219.         return $absences;
  220.     }
  221.     public function getHoulyAchieved(string $type)
  222.     {
  223. //        foreach ($this->get)
  224.         $date1 = new DateTime();
  225.         $date2 = clone $date1;
  226.         $date2->modify("+{$this->getHoulyAchievedAsSeconds($type)} seconds");
  227.         return $date1->diff($date2);
  228.     }
  229.     public function getHoulyAchievedAsSeconds(string $type)
  230.     {
  231.         $total=0;
  232.         if($type=='aseance'){
  233.             foreach ($this->getTimetableitems() as $timetableitem){
  234.                 foreach ($timetableitem->getAseances() as $aseance){
  235.                     $total+=$aseance->getLengthAsSeconds();
  236.                 }
  237.             }
  238.         }
  239.         else{
  240.             foreach ($this->getTimetableitems() as $timetableitem){
  241.                 $total+=$timetableitem->getLengthAsSeconds();
  242.             }
  243.         }
  244.         return $total;
  245.     }
  246.     public function getHoulyAchievedAsHoure(string $type)
  247.     {
  248.         return $this->getHoulyAchievedAsSeconds($type)/3600;
  249.     }
  250.     public function getHoulyAchievedAsStr(string $type)
  251.     {
  252.         //return $this->getHoulyAchieved()->format('%hh%imin');
  253.         $h=floor($this->getHoulyAchievedAsSeconds($type)/3600);
  254.         $m=($this->getHoulyAchievedAsSeconds($type)%3600)/60;
  255.         return $h.'h'.$m.'min';
  256.     }
  257.     public function getHoulyPercentage(string $type){
  258.         return $this->getHourlyVolumeAsseconds()==0?0:$this->getHoulyAchievedAsSeconds($type)/($this->getHourlyVolumeAsseconds()??1)*100;
  259.     }
  260.     public function getRemunerationDue()
  261.     {
  262.         if($this->intervenant->getTypePay()=='Salary') return 0;
  263.         if($this->getTypePay()=='hourly')
  264.             return $this->getHoulyAchievedAsHoure('aseance')*$this->getPcPay();
  265.         if($this->getTypePay()=='Pourcentage')
  266.             return $this->pcPay*$this->getClasse()->getTotalPay('monthlyPayment')/100;
  267.         else return 0;
  268.     }
  269.     
  270.     public function getSeancesByMonth($month)
  271.     {
  272.         $seances=new ArrayCollection();
  273.         foreach ($this->getAseances() as $aseance){
  274.             if($aseance->getTakesplaceOn()->format('m-y')==$month){
  275.                 $seances->add($aseance);
  276.             }
  277.         }
  278.         return $seances;
  279.     }
  280.     public function getSecondsWorkedByMonth($month)
  281.     {
  282.        $s=0;
  283.         foreach ($this->getSeancesByMonth($month) as $aseance){
  284.             $s+=$aseance->getLengthAsSeconds();
  285.         }
  286.         return $s;
  287.     }
  288.     public function getHourelyWorkedByMonth($month)
  289.     {
  290.         return $this->getSecondsWorkedByMonth($month)/3600;
  291.     }
  292.     public function getHourelyWorkedByMonthAsStr($month)
  293.     {
  294.         $h=floor($this->getSecondsWorkedByMonth($month)/3600);
  295.         $m=floor(($this->getSecondsWorkedByMonth($month)%3600)/60);
  296.         return $h.'h'.$m.'min';
  297.     }
  298.     public function getStartAt()
  299.     {
  300.         $start null;
  301.         foreach ($this->getAseances() as $aseance) {
  302.             if ($aseance->getTakesplaceOn() instanceof \DateTimeInterface) {
  303.                 if ($start === null || $aseance->getTakesplaceOn() < $start) {
  304.                     $start $aseance->getTakesplaceOn();
  305.                 }
  306.             }
  307.         }
  308.         return $start;
  309.     }
  310.     public function getEndAt()
  311.     {
  312.         $end null;
  313.         foreach ($this->getAseances() as $aseance) {
  314.             if ($aseance->getTakesplaceOn() instanceof \DateTimeInterface) {
  315.                 if ($end === null || $aseance->getTakesplaceOn() > $end) {
  316.                     $end $aseance->getTakesplaceOn();
  317.                 }
  318.             }
  319.         }
  320.         return $end;
  321.     }
  322.     public function getRemunerationByMonth($month)
  323.     {
  324.         if($this->intervenant->getTypePay()=='Salary') return 0;
  325.         if($this->typePay=='hourly') return $this->pcPay*$this->getHourelyWorkedByMonth($month);
  326.         elseif ($this->typePay=='Pourcentage') return $this->pcPay*$this->getClasse()->getTotalPayByMonth($month)/100;
  327.         else return 0;
  328.     }
  329. }