<?php
namespace App\Entity\Training;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Schoolyear;
use App\Repository\Training\FormerRepository;
use App\Service\SchoolyearService;
use App\Traits\Actions;
use App\Entity\Training\Assignation;
use DateInterval;
use DatePeriod;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\User;
#[ORM\Entity(repositoryClass: FormerRepository::class)]
#[ORM\Table(name: 'hs_tc_training_former')]
#[ApiResource]
class Former implements \Stringable
{
use Actions;
#[ORM\Column(name: "first_name", type: "string", length: 255)]
private string $firstName;
#[ORM\Column(name: "last_name", type: "string", length: 255)]
private string $lastName;
#[ORM\Column(name: "email", type: "string", length: 255, nullable: true)]
private ?string $email;
#[ORM\Column(name: "phone", type: "string", length: 255, nullable: true)]
private ?string $phone;
#[ORM\Column(name: 'wp', type: 'string', length: 255, nullable: true)]
private ?string $wp = null;
#[ORM\Column(name: "specialty", type: "string", length: 255, nullable: true)]
private ?string $specialty;
#[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(name: 'min_pay', type: 'float', nullable: true)]
private ?float $minPay = null;
#[ORM\Column(type: "json", nullable: true)]
private array $months=[];
#[ORM\OneToOne(inversedBy: "former", targetEntity: User::class, cascade: ["persist", "remove"])]
#[ORM\JoinColumn(nullable: true)]
private User $user;
#[ORM\OneToMany(mappedBy: "former", targetEntity: "Assignation")]
private Collection $assignations;
#[ORM\OneToMany(mappedBy: 'former', targetEntity: Fregulation::class, cascade: ["persist"], orphanRemoval: true)]
private Collection $regulations;
public function __construct()
{
// date_default_timezone_set('Africa/Casablanca');
$this->createAt=new \DateTime('now');
$this->published=false;
$this->wp="212";
}
/**
* @return string
*/
public function getFirstName(): ?string
{
return $this->firstName;
}
/**
* @param string $firstName
*/
public function setFirstName(?string $firstName): Former
{
$this->firstName = $firstName;
return $this;
}
/**
* @return string
*/
public function getLastName(): ?string
{
return $this->lastName;
}
/**
* @param string $lastName
*/
public function setLastName(?string $lastName): Former
{
$this->lastName = $lastName;
return $this;
}
/**
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* @param string $email
* @return Former
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* @return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* @param string $phone
* @return Former
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
public function getWp(): ?string
{
return $this->wp;
}
public function setWp(?string $wp): void
{
$this->wp = $wp;
}
/**
* @return string
*/
public function getSpecialty()
{
return $this->specialty;
}
/**
* @param string $specialty
* @return Former
*/
public function setSpecialty($specialty)
{
$this->specialty = $specialty;
return $this;
}
public function __toString(): string
{
return $this->lastName.' '.$this->firstName;
}
/**
* @return mixed
*/
public function getUser()
{
return $this->user;
}
/**
* @return Former
*/
public function setUser(mixed $user)
{
$this->user = $user;
return $this;
}
/**
* Add assignation
*
*
* @return Former
*/
public function addAssignation(Assignation $assignation)
{
$assignation->setFormer($this);
$this->assignations[] = $assignation;
return $this;
}
/**
* Remove assignation
*/
public function removeAssignation(Assignation $assignation)
{
$this->assignations->removeElement($assignation);
}
/**
* Get assignations
*
* @return Collection
*/
public function getAssignations()
{
return $this->assignations;
}
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é',
};
}
public function getMinPay(): ?float
{
return $this->minPay;
}
public function setMinPay(?float $minPay): void
{
$this->minPay = $minPay;
}
public function getMonths(): array
{
return $this->months;
}
public function setMonths(array $months): void
{
$this->months = $months;
}
public function getFregulations(): Collection
{
return $this->regulations;
}
public function addFregulation(Fregulation $fregulation): self
{
if (!$this->regulations->contains($fregulation)) {
$this->regulations->add($fregulation);
$fregulation->setFormer($this);
}
return $this;
}
public function removeFregulation(Fregulation $fregulation): self
{
if ($this->regulations->removeElement($fregulation)) {
// set the owning side to null (unless already changed)
if ($fregulation->getFormer() === $this) {
$fregulation->setFormer(null);
}
}
return $this;
}
public function getAssignationsBySY(Schoolyear $schoolyear)
{
return $this->assignations->filter(function (Assignation $assignation) use ($schoolyear) {
return $assignation->getSchoolYear() === $schoolyear;
});
}
public function getStartWorkInSY(Schoolyear $schoolyear)
{
$start = null;
foreach ($this->getAssignationsBySY($schoolyear) as $att) {
if ($att->getStartAt() !== null) {
if ($start === null || $att->getStartAt() < $start) {
$start = $att->getStartAt();
}
}
}
return $start;
}
/**
* Retourne la date de fin de travail la plus tardive pour l'année scolaire donnée.
*
* @param Schoolyear $schoolyear
* @return null|\DateTimeInterface
*/
public function getEndWorkInSY(Schoolyear $schoolyear): ?\DateTimeInterface
{
$end = null;
foreach ($this->getAssignationsBySY($schoolyear) as $att) {
$endAt = $att->getEndAt();
if ($endAt instanceof \DateTimeInterface) {
if ($end === null || $endAt > $end) {
$end = $endAt;
}
}
}
return $end;
}
/**
* Retourne la liste des mois (format m/y) couverts par le formateur pour l'année scolaire donnée.
*
* @param Schoolyear $schoolyear
* @return array
*/
public function getMonthBySY(Schoolyear $schoolyear): array
{
$months = [];
if ($this->typePay != 'Salary') {
$start = $this->getStartWorkInSY($schoolyear);
$end = $this->getEndWorkInSY($schoolyear);
if ($start instanceof \DateTimeInterface && $end instanceof \DateTimeInterface) {
// On inclut le mois de fin dans la période
$endInclusive = (clone $end)->modify('first day of next month');
$interval = \DateInterval::createFromDateString('1 month');
$period = new \DatePeriod($start, $interval, $endInclusive);
foreach ($period as $date) {
$months[] = $date->format('m/y');
}
}
} else {
foreach ($schoolyear->getMonths('m/y') as $month) {
if (in_array($month, $this->months)) {
$months[] = $month;
}
}
}
return $months;
}
public function getHourlyBySY(Schoolyear $schoolyear)
{
$hourly=0;
/** @var Assignation $assignation */
foreach($this->getAssignationsBySY($schoolyear) as $assignation){
$hourly+=$assignation->getHoulyCountedAsHoure();
}
return $hourly;
}
public function getHourlyBySYOnStr(Schoolyear $schoolyear)
{
$h=floor($this->getHourlyBySY($schoolyear));
$m=($this->getHourlyBySY($schoolyear)-$h)*60;
return $h.'h'.$m.'min';
}
// public function getMonthBySY(Schoolyear $schoolyear)
// {
// $m=0;
// foreach ($schoolyear->getMonths('m/y') as $month){
// if(in_array($month, $this->months)) $m++;
// }
// return $m;
// }
public function getRemunerationDueBySY(Schoolyear $schoolyear)
{
if($this->typePay!='Salary'){
$m=0;
foreach($this->getAssignationsBySY($schoolyear) as $assignation){
$m+=$assignation->getRemunerationDue();
}
return $m;
}
else{
$m=0;
foreach ($schoolyear->getMonths('m/y') as $month){
if(in_array($month, $this->months)) $m++;
}
return $m*$this->getPcPay();
}
}
public function getRemunerationRegledBySY(Schoolyear $schoolyear)
{
$m=0;
foreach($this->getFregulations() as $fregulation){
if($fregulation->getSchoolyear() === $schoolyear)
$m+=$fregulation->getAmount();
}
return $m;
}
public function getRemunerationResteBySY(Schoolyear $schoolyear)
{
return $this->getRemunerationDueBySY($schoolyear)-$this->getRemunerationRegledBySY($schoolyear);
}
public function getSeancesByMonth($month){
$items=[];
foreach ($this->getAssignations() as $assignation){
foreach ($assignation->getSeancesByMonth($month) as $seance){
$items[]=($seance);
}
}
usort($items, 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 $items;
}
public function getHourlyByMonth($month)
{
$hourly=0;
foreach($this->getAssignations() as $assignation){
$hourly+=$assignation->getHourelyWorkedByMonth($month);
}
return $hourly;
}
public function getHourlyByMonthOnStr($month)
{
$h=floor($this->getHourlyByMonth($month));
$m=floor(($this->getHourlyByMonth($month)-$h)*60);
return $h.'h'.$m.'min';
}
public function getRemunerationDueByMonth($month)
{
$m=0;
foreach ($this->getAssignations() as $assignation){
$m+=$assignation->getRemunerationByMonth($month);
}
return $m;
}
public function getTableServiceBySY(Schoolyear $schoolyear)
{
$array=[];
foreach($this->getAssignationsBySY($schoolyear) as $assignation){
foreach ($assignation->getSeances() as $seance){
$array[$assignation->getId()][$seance->getTakesplaceOn()->format('m/y')][]=$seance;
}
}
return $array;
}
public function getRegulationDetails(Schoolyear $schoolyear)
{
$details=[];
$total=$this->getRemunerationRegledBySY($schoolyear);
foreach ($schoolyear->getMonths('m/y') as $month){
$details[$month]=min($total,$this->getRemunerationDueByMonth($month));
$total=max($total-$this->getRemunerationDueByMonth($month),0);
}
$details['av']=$total;
return $details;
}
public function getRegulationByMonth(Schoolyear $schoolyear, $month)
{
if(in_array($month, $schoolyear->getMonths('m/y'))){
return $this->getRegulationDetails($schoolyear)[$month];
}
return 0;
}
public function getResteByMonth(Schoolyear $schoolyear, $month)
{
return max($this->getRemunerationDueByMonth($month)-$this->getRegulationByMonth($schoolyear, $month),0);
}
public function getAvanceBySY(Schoolyear $schoolyear)
{
return $this->getRegulationDetails($schoolyear)['av'];
}
}