<?php
namespace App\Entity\Formation;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\Formation\AttributionRepository;
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: AttributionRepository::class)]
#[ORM\Table(name: 'hs_tc_formation_attribution')]
#[ApiResource]
class Attribution implements \Stringable
{
use Actions;
#[ORM\ManyToOne(targetEntity: Unite::class, inversedBy: "attributions")]
#[ORM\JoinColumn(nullable: false)]
private Unite $unite;
#[ORM\ManyToOne(targetEntity: Intervenant::class, inversedBy: "attributions")]
#[ORM\JoinColumn(nullable: false)]
private Intervenant $intervenant;
#[ORM\ManyToOne(targetEntity: Classe::class, inversedBy: "attributions")]
#[ORM\JoinColumn(nullable: false)]
private Classe $classe;
#[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\Column()]
private ?int $hourlyVolume = null;
#[ORM\OneToMany(mappedBy: 'attribution', targetEntity: Timetableitem::class)]
private Collection $timetableitems;
public function __construct()
{
// date_default_timezone_set('Africa/Casablanca');
$this->createAt=new \DateTime('now');
$this->published=true;
$this->timetableitems = new ArrayCollection();
}
/**
* @return Unite
*/
public function getUnite(): ?Unite
{
return $this->unite;
}
/**
* @param Unite $unite
*/
public function setUnite(?Unite $unite): Attribution
{
$this->unite = $unite;
return $this;
}
/**
* @return Intervenant
*/
public function getIntervenant(): ?Intervenant
{
return $this->intervenant;
}
/**
* @param Intervenant $intervenant
*/
public function setIntervenant(?Intervenant $intervenant): Attribution
{
$this->intervenant = $intervenant;
return $this;
}
/**
* @return Classe
*/
public function getClasse(): ?Classe
{
return $this->classe;
}
/**
* @param Classe $classe
*/
public function setClasse(?Classe $classe): Attribution
{
$this->classe = $classe;
return $this;
}
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) {
'Pourcentage' => 'Pourcentage',
'hourly' => 'à l\'heure',
'Salary' => 'Salaire mensuel',
default => 'non indiqué',
};
}
public function __toString(): string
{
return $this->classe.'<--->'.$this->unite;
}
/**
* @return Collection<int, Timetableitem>
*/
public function getHourlyVolume(): ?int
{
return $this->hourlyVolume;
}
public function setHourlyVolume(?int $hourlyVolume): void
{
$this->hourlyVolume = $hourlyVolume;
}
function getHourlyVolumeAsseconds() {
return ($this->getHourlyVolume() * 60 * 60) ;
}
public function getTimetableitems(): Collection
{
return $this->timetableitems;
}
public function addTimetableitem(Timetableitem $timetableitem): self
{
if (!$this->timetableitems->contains($timetableitem)) {
$this->timetableitems->add($timetableitem);
$timetableitem->setAttribution($this);
}
return $this;
}
public function removeTimetableitem(Timetableitem $timetableitem): self
{
if ($this->timetableitems->removeElement($timetableitem)) {
// set the owning side to null (unless already changed)
if ($timetableitem->getAttribution() === $this) {
$timetableitem->setAttribution(null);
}
}
return $this;
}
public function getAseances()
{
$aseances=new ArrayCollection();
foreach ($this->getTimetableitems() as $timetableitem){
foreach ($timetableitem->getAseances() as $aseance){
$aseances->add($aseance);
}
}
return $aseances;
}
public function getSeances()
{
$allSeances = [];
// Parcourir chaque TimetableItem
foreach ($this->getTimetableItems() as $timetableItem) {
// Ajouter chaque séance de TimetableItem à la collection $allSeances
foreach ($timetableItem->getAseances() 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->getTimetableItems() as $timetableItem) {
foreach ($timetableItem->getSeancesCancelled() as $seance ) {
$allSeances[] = ['timetableitem'=>$timetableItem, 'seanceDate'=>$seance];
}
}
usort($allSeances, function($a, $b) {
$dateA = DateTime::createFromFormat('d-m-Y H:i', $a['seanceDate'].' '.$a['timetableitem']->getStartAt());
$dateB = DateTime::createFromFormat('d-m-Y H:i', $b['seanceDate'].' '.$b['timetableitem']->getStartAt());
return $dateA <=> $dateB;
});
return $allSeances;
}
public function getSeancesNotCreate()
{
$allSeances = [];
foreach ($this->getTimetableItems() as $timetableItem) {
foreach ($timetableItem->getSeancesNotCreate() as $seance ) {
$allSeances[] = ['timetableitem'=>$timetableItem, 'seanceDate'=>$seance];
}
}
usort($allSeances, function($a, $b) {
$dateA = DateTime::createFromFormat('d-m-Y H:i', $a['seanceDate'].' '.$a['timetableitem']->getStartAt());
$dateB = DateTime::createFromFormat('d-m-Y H:i', $b['seanceDate'].' '.$b['timetableitem']->getStartAt());
return $dateA <=> $dateB;
});
return $allSeances;
}
public function getAbsences()
{
$absences=[];
/** @var Aseance $seance */
foreach ($this->getAseances() as $seance){
/** @var Aabsence $absence */
foreach ($seance->getAbsencesPublished() as $absence){
$absences[]=$absence;
}
}
return $absences;
}
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=='aseance'){
foreach ($this->getTimetableitems() as $timetableitem){
foreach ($timetableitem->getAseances() as $aseance){
$total+=$aseance->getLengthAsSeconds();
}
}
}
else{
foreach ($this->getTimetableitems() as $timetableitem){
$total+=$timetableitem->getLengthAsSeconds();
}
}
return $total;
}
public function getHoulyAchievedAsHoure(string $type)
{
return $this->getHoulyAchievedAsSeconds($type)/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 getHoulyPercentage(string $type){
return $this->getHourlyVolumeAsseconds()==0?0:$this->getHoulyAchievedAsSeconds($type)/($this->getHourlyVolumeAsseconds()??1)*100;
}
public function getRemunerationDue()
{
if($this->intervenant->getTypePay()=='Salary') return 0;
if($this->getTypePay()=='hourly')
return $this->getHoulyAchievedAsHoure('aseance')*$this->getPcPay();
if($this->getTypePay()=='Pourcentage')
return $this->pcPay*$this->getClasse()->getTotalPay('monthlyPayment')/100;
else return 0;
}
public function getSeancesByMonth($month)
{
$seances=new ArrayCollection();
foreach ($this->getAseances() as $aseance){
if($aseance->getTakesplaceOn()->format('m-y')==$month){
$seances->add($aseance);
}
}
return $seances;
}
public function getSecondsWorkedByMonth($month)
{
$s=0;
foreach ($this->getSeancesByMonth($month) as $aseance){
$s+=$aseance->getLengthAsSeconds();
}
return $s;
}
public function getHourelyWorkedByMonth($month)
{
return $this->getSecondsWorkedByMonth($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 getStartAt()
{
$start = null;
foreach ($this->getAseances() as $aseance) {
if ($aseance->getTakesplaceOn() instanceof \DateTimeInterface) {
if ($start === null || $aseance->getTakesplaceOn() < $start) {
$start = $aseance->getTakesplaceOn();
}
}
}
return $start;
}
public function getEndAt()
{
$end = null;
foreach ($this->getAseances() as $aseance) {
if ($aseance->getTakesplaceOn() instanceof \DateTimeInterface) {
if ($end === null || $aseance->getTakesplaceOn() > $end) {
$end = $aseance->getTakesplaceOn();
}
}
}
return $end;
}
public function getRemunerationByMonth($month)
{
if($this->intervenant->getTypePay()=='Salary') return 0;
if($this->typePay=='hourly') return $this->pcPay*$this->getHourelyWorkedByMonth($month);
elseif ($this->typePay=='Pourcentage') return $this->pcPay*$this->getClasse()->getTotalPayByMonth($month)/100;
else return 0;
}
}