<?php
namespace App\Entity\Training;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Training\Noteassign;
use App\Repository\Training\AssignationRepository;
use App\Traits\Actions;
use DateInterval;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: AssignationRepository::class)]
#[ORM\Table(name: 'hs_tc_training_assignation')]
#[ApiResource]
class Assignation implements \Stringable
{
use Actions;
#[ORM\ManyToOne(targetEntity: Module::class, inversedBy: "assignations")]
#[ORM\JoinColumn(nullable: false)]
private Module $module;
#[ORM\ManyToOne(targetEntity: Former::class, inversedBy: "assignations")]
#[ORM\JoinColumn(nullable: false)]
private Former $former;
#[ORM\ManyToOne(targetEntity: Groupe::class, inversedBy: "assignations")]
#[ORM\JoinColumn(nullable: false)]
private Groupe $groupe;
#[ORM\OneToMany(mappedBy: "assignation", targetEntity: Noteassign::class, cascade: ["persist"], orphanRemoval: true)]
private Collection $notes;
#[ORM\Column(name: "nbr_controle", type: "integer", length: 255)]
private int $nbrControle;
#[ORM\Column()]
private ?int $hourlyVolume = null;
#[ORM\Column(name: 'type_pay', type: 'string', length: 255, nullable: true)]
private ?string $typePay = null;
#[ORM\Column(name: 'pc_pay', type: 'float', nullable: true)]
private ?float $pcPay = null;
#[ORM\OneToMany(mappedBy: 'assignation', targetEntity: Scheduleitem::class)]
private Collection $scheduleitems;
public function __construct()
{
// date_default_timezone_set('Africa/Casablanca');
$this->createAt=new \DateTime('now');
$this->published=true;
$this->notes=new ArrayCollection();
$this->scheduleitems = new ArrayCollection();
}
/**
* @return Module
*/
public function getModule(): ?Module
{
return $this->module;
}
/**
* @param Module $module
*/
public function setModule(?Module $module): Assignation
{
$this->module = $module;
/** @var Notemodule $notemodule */
foreach ($module->getNotes() as $notemodule){
$note=new Noteassign();
$note->setNotemodule($notemodule);
$this->addNote($note);
}
return $this;
}
/**
* @return Former
*/
public function getFormer(): ?Former
{
return $this->former;
}
/**
* @param Former $former
*/
public function setFormer(?Former $former): Assignation
{
$this->former = $former;
return $this;
}
/**
* @return Groupe
*/
public function getGroupe(): ?Groupe
{
return $this->groupe;
}
/**
* @param Groupe $groupe
*/
public function setGroupe(?Groupe $groupe): Assignation
{
$this->groupe = $groupe;
return $this;
}
public function getNbrControle(): int
{
return $this->nbrControle!=null?$this->nbrControle:$this->module->getNbrCC();
}
public function setNbrControle(int $nbrControle): Assignation
{
$this->nbrControle = $nbrControle;
return $this;
}
/**
* Add note
*
*
* @return Assignation
*/
public function addNote(Noteassign $note)
{
$note->setAssignation($this);
$this->notes->add($note);
return $this;
}
/**
* Remove note
*/
public function removeNote(Noteassign $note)
{
$this->notes->removeElement($note);
}
/**
* Get notes
*
* @return Collection
*/
public function getNotes()
{
return $this->notes;
}
public function getNoteAssignByNoteModule(Notemodule $notemodule): ?Noteassign
{
foreach($this->notes as $note){
if ($note->getNotemodule()==$notemodule){
return $note;
}
}
return null;
}
public function getPCInput(){
$sum=0;
$i=0;
/** @var Noteassign $line */
foreach ($this->getNotes() as $line){
/** @var Note $note */
foreach ($line->getNotes() as $note){
if($note->getTrainee()->getStatus()=='En cours'){
$sum++;
if($note->getVal()!== null) $i++;
}
}
}
if($sum!=0) return floor($i/$sum*100);
else return 0;
}
public function __toString(): string
{
return $this->groupe.'<--->'.$this->module;
}
public function getSY(){
return match ($this->getModule()->getSemester()){
'Semestre 1', 'Semestre 2' => $this->getGroupe()->getFirstYear(),
'Semestre 3', 'Semestre 4' => $this->getGroupe()->getSecondYear(),
'Semestre 5', 'Semestre 6' => $this->getGroupe()->getThirdYear(),
};
}
/**
* @return Collection<int, Scheduleitem>
*/
public function getHourlyVolume(): ?int
{
return $this->hourlyVolume;
}
public function setHourlyVolume(?int $hourlyVolume): void
{
$this->hourlyVolume = $hourlyVolume;
}
public function getTypePay(): ?string
{
return $this->typePay;
}
public function setTypePay(?string $typePay): void
{
$this->typePay = $typePay;
}
public function getPcPay(): ?float
{
return $this->pcPay;
}
public function setPcPay(?float $pcPay): void
{
$this->pcPay = $pcPay;
}
public function getTypePayText()
{
return match ($this->typePay) {
'hourly' => 'à l\'heure',
'Salary' => 'Salaire mensuel',
default => 'non indiqué',
};
}
function getHourlyVolumeAsseconds() {
return ($this->getHourlyVolume() * 60 * 60) ;
}
public function getScheduleitems(): Collection
{
return $this->scheduleitems;
}
public function addScheduleitem(Scheduleitem $scheduleitem): self
{
if (!$this->scheduleitems->contains($scheduleitem)) {
$this->scheduleitems->add($scheduleitem);
$scheduleitem->setAssignation($this);
}
return $this;
}
public function removeScheduleitem(Scheduleitem $scheduleitem): self
{
if ($this->scheduleitems->removeElement($scheduleitem)) {
// set the owning side to null (unless already changed)
if ($scheduleitem->getAssignation() === $this) {
$scheduleitem->setAssignation(null);
}
}
return $this;
}
public function getDateControle($name): ?\DateTime{
foreach ($this->getNotes() as $note){
if($note->getNotemodule()->getName()===$name) return $note->getCarriedOutAt();
}
return null;
}
public function getSeances()
{
$allSeances = [];
// Parcourir chaque ScheduleItem
foreach ($this->getScheduleItems() as $scheduleItem) {
// Ajouter chaque séance de ScheduleItem à la collection $allSeances
foreach ($scheduleItem->getSeances() as $seance) {
$allSeances[]=$seance;
}
}
usort($allSeances, function($a, $b) {
$dateA = DateTime::createFromFormat('d-m-Y H:i', $a->getTakesplaceOn()->format('d-m-Y').' '.$a->getStartAt());
$dateB = DateTime::createFromFormat('d-m-Y H:i', $b->getTakesplaceOn()->format('d-m-Y').' '.$b->getStartAt());
return $dateA <=> $dateB;
});
return $allSeances;
}
public function getSeancesCancelled()
{
$allSeances = [];
foreach ($this->getScheduleItems() as $scheduleItem) {
foreach ($scheduleItem->getSeancesCancelled() as $seance ) {
$allSeances[] = ['scheduleitem'=>$scheduleItem, 'seanceDate'=>$seance];
}
}
usort($allSeances, function($a, $b) {
$dateA = DateTime::createFromFormat('d-m-Y H:i', $a['seanceDate'].' '.$a['scheduleitem']->getStartAt());
$dateB = DateTime::createFromFormat('d-m-Y H:i', $b['seanceDate'].' '.$b['scheduleitem']->getStartAt());
return $dateA <=> $dateB;
});
return $allSeances;
}
public function getSeancesNotCreate()
{
$allSeances = [];
foreach ($this->getScheduleItems() as $scheduleItem) {
foreach ($scheduleItem->getSeancesNotCreate() as $seance ) {
$allSeances[] = ['scheduleitem'=>$scheduleItem, 'seanceDate'=>$seance];
}
}
usort($allSeances, function($a, $b) {
$dateA = DateTime::createFromFormat('d-m-Y H:i', $a['seanceDate'].' '.$a['scheduleitem']->getStartAt());
$dateB = DateTime::createFromFormat('d-m-Y H:i', $b['seanceDate'].' '.$b['scheduleitem']->getStartAt());
return $dateA <=> $dateB;
});
return $allSeances;
}
public function getSeancesCounted()
{
$items=[];
/** @var Seance $seance */
foreach ($this->getSeances() as $seance){
if ($seance->isPublished()) $items[]=$seance;
}
return $items;
}
public function getStartAt()
{
$start=null;
/** @var Seance $seance */
foreach ($this->getSeances() as $seance){
if($start==null || $seance->getTakesplaceOn()<$start){
$start=$seance->getTakesplaceOn();
}
}
return $start;
}
public function getAbsences()
{
$absences=[];
/** @var Seance $seance */
foreach ($this->getSeances() as $seance){
/** @var Absence $absence */
foreach ($seance->getAbsencesPublished() as $absence){
$absences[]=$absence;
}
}
return $absences;
}
/**
* Retourne la date de la dernière séance (takesPlaceOn) de l'assignation.
*
* @return null|\DateTimeInterface
*/
public function getEndAt(): ?\DateTimeInterface
{
$end = null;
/** @var Seance $seance */
foreach ($this->getSeances() as $seance) {
$date = $seance->getTakesplaceOn();
if ($date instanceof \DateTimeInterface) {
if ($end === null || $date > $end) {
$end = $date;
}
}
}
return $end;
}
public function getHoulyAchieved(string $type)
{
// foreach ($this->get)
$date1 = new DateTime();
$date2 = clone $date1;
$date2->modify("+{$this->getHoulyAchievedAsSeconds($type)} seconds");
return $date1->diff($date2);
}
public function getHoulyAchievedAsSeconds(string $type)
{
$total=0;
if($type=='seance'){
foreach ($this->getScheduleitems() as $scheduleitem){
foreach ($scheduleitem->getSeances() as $seance){
$total+=$seance->getLengthAsSeconds();
}
}
}
else{
foreach ($this->getScheduleitems() as $scheduleitem){
$total+=$scheduleitem->getLengthAsSeconds();
}
}
return $total;
}
public function getHoulyCountedAsSeconds()
{
$total=0;
foreach ($this->getSeancesCounted() as $seance){
$total+=$seance->getLengthAsSeconds();
}
return $total;
}
public function getHoulyAchievedAsHoure(string $type)
{
return $this->getHoulyAchievedAsSeconds($type)/3600;
}
public function getHoulyCountedAsHoure()
{
return $this->getHoulyCountedAsSeconds()/3600;
}
public function getHoulyAchievedAsStr(string $type)
{
//return $this->getHoulyAchieved()->format('%hh%imin');
$h=floor($this->getHoulyAchievedAsSeconds($type)/3600);
$m=($this->getHoulyAchievedAsSeconds($type)%3600)/60;
return $h.'h'.$m.'min';
}
public function getHoulyCountedAsStr()
{
$h=floor($this->getHoulyCountedAsSeconds()/3600);
$m=($this->getHoulyCountedAsSeconds()%3600)/60;
return $h.'h'.$m.'min';
}
public function getHoulyPercentage(string $type){
return $this->getHourlyVolumeAsseconds()==0?0:$this->getHoulyAchievedAsSeconds($type)/$this->getHourlyVolumeAsseconds()*100;
}
public function getSchoolYear()
{
$level=$this->getModule()->getLevel();
return $this->groupe->geSchoolYear($level);
}
public function getRemunerationDue()
{
if($this->former->getTypePay()!=='Salary')
return $this->getHoulyCountedAsHoure()*$this->getPcPay();
else return 0;
}
public function getSeancesByMonth($month)
{
$seances=new ArrayCollection();
foreach ($this->getScheduleitems() as $scheduleitem){
foreach ($scheduleitem->getSeances() as $seance){
if($seance->getTakesplaceOn()->format('m/y')==$month) $seances->add($seance);
}
}
return $seances;
}
public function getSeancesCountedByMonth($month)
{
$seances=new ArrayCollection();
foreach ($this->getScheduleitems() as $scheduleitem){
/** @var Seance $seance */
foreach ($scheduleitem->getSeances() as $seance){
if($seance->getTakesplaceOn()->format('m/y')==$month && $seance->isPublished()) $seances->add($seance);
}
}
return $seances;
}
public function getSecondsWorkedByMonth($month)
{
$s=0;
foreach ($this->getSeancesByMonth($month) as $seance){
$s+=$seance->getLengthAsSeconds();
}
return $s;
}
public function getSecondsWorkedCountedByMonth($month)
{
$s=0;
foreach ($this->getSeancesCountedByMonth($month) as $seance){
$s+=$seance->getLengthAsSeconds();
}
return $s;
}
public function getHourelyWorkedByMonth($month)
{
return $this->getSecondsWorkedByMonth($month)/3600;
}
public function getHourelyWorkedCountedByMonth($month)
{
return $this->getSecondsWorkedCountedByMonth($month)/3600;
}
public function getHourelyWorkedByMonthAsStr($month)
{
$h=floor($this->getSecondsWorkedByMonth($month)/3600);
$m=floor(($this->getSecondsWorkedByMonth($month)%3600)/60);
return $h.'h'.$m.'min';
}
public function getHourelyWorkedCountedByMonthAsStr($month)
{
$h=floor($this->getSecondsWorkedCountedByMonth($month)/3600);
$m=floor(($this->getSecondsWorkedCountedByMonth($month)%3600)/60);
return $h.'h'.$m.'min';
}
public function getRemunerationByMonth($month)
{
if($this->typePay=='hourly') return $this->pcPay*$this->getHourelyWorkedCountedByMonth($month);
else return $this->pcPay;
}
}