<?php
namespace App\Entity\Formation;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Schoolyear;
use App\Repository\Formation\IntervenantRepository;
use App\Traits\Actions;
use App\Entity\Formation\Attribution;
use DateInterval;
use DatePeriod;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\User;
#[ORM\Entity(repositoryClass: IntervenantRepository::class)]
#[ORM\Table(name: 'hs_tc_formation_intervenant')]
#[ApiResource]
class Intervenant 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\OneToOne(inversedBy: "intervenant", targetEntity: User::class, cascade: ["persist", "remove"])]
#[ORM\JoinColumn(nullable: true)]
private ?User $user=null;
#[ORM\OneToMany(mappedBy: "intervenant", targetEntity: "Attribution")]
private Collection $attributions;
#[ORM\OneToMany(mappedBy: 'intervenant', targetEntity: Aregulation::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): Intervenant
{
$this->firstName = $firstName;
return $this;
}
/**
* @return string
*/
public function getLastName(): ?string
{
return $this->lastName;
}
/**
* @param string $lastName
*/
public function setLastName(?string $lastName): Intervenant
{
$this->lastName = $lastName;
return $this;
}
/**
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* @param string $email
* @return Intervenant
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* @return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* @param string $phone
* @return Intervenant
*/
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 Intervenant
*/
public function setSpecialty($specialty)
{
$this->specialty = $specialty;
return $this;
}
public function __toString(): string
{
return $this->lastName.' '.$this->firstName;
}
public function getRegulations(): Collection
{
return $this->regulations;
}
public function setRegulations(Collection $regulations): void
{
$this->regulations = $regulations;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): void
{
$this->user = $user;
}
/**
* Add attribution
*
*
* @return Intervenant
*/
public function addAttribution(Attribution $attribution)
{
$attribution->setIntervenant($this);
$this->attributions[] = $attribution;
return $this;
}
/**
* Remove attribution
*/
public function removeAttribution(Attribution $attribution)
{
$this->attributions->removeElement($attribution);
}
/**
* Get attributions
*
* @return Collection
*/
public function getAttributions()
{
return $this->attributions;
}
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 getMinPay(): ?float
{
return $this->minPay;
}
public function setMinPay(?float $minPay): void
{
$this->minPay = $minPay;
}
public function getAregulations(): Collection
{
return $this->regulations;
}
public function addAregulation(Aregulation $aregulation): self
{
if (!$this->regulations->contains($aregulation)) {
$this->regulations->add($aregulation);
$aregulation->setIntervenant($this);
}
return $this;
}
public function removeAregulation(Aregulation $aregulation): self
{
if ($this->regulations->removeElement($aregulation)) {
// set the owning side to null (unless already changed)
if ($aregulation->getIntervenant() === $this) {
$aregulation->setIntervenant(null);
}
}
return $this;
}
public function getAttributionsBySY(Schoolyear $schoolyear)
{
return $this->attributions->filter(function (Attribution $attribution) use ($schoolyear) {
return $attribution->getClasse()->getSchoolYear() === $schoolyear;
});
}
public function getStartWorkInSY(Schoolyear $schoolyear)
{
$start = null;
foreach ($this->getAttributionsBySY($schoolyear) as $att) {
if ($att->getStartAt() instanceof \DateTimeInterface) {
if ($start === null || $att->getStartAt() < $start) {
$start = $att->getStartAt();
}
}
}
return $start;
}
public function getEndWorkInSY(Schoolyear $schoolyear)
{
$end = null;
foreach ($this->getAttributionsBySY($schoolyear) as $att) {
if ($att->getEndAt() instanceof \DateTimeInterface) {
if ($end === null || $att->getEndAt() > $end) {
$end = $att->getEndAt();
}
}
}
return $end;
}
public function getMonthBySY(Schoolyear $schoolyear)
{
$months = [];
$start = $this->getStartWorkInSY($schoolyear);
$end = $this->getEndWorkInSY($schoolyear);
if($start instanceof \DateTimeInterface && $end instanceof \DateTimeInterface) {
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
$months[] = $date->format('m-y');
}
}
return $months;
}
public function getHourlyBySY(Schoolyear $schoolyear)
{
$hourly=0;
foreach($this->getAttributionsBySY($schoolyear) as $attribution){
$hourly+=$attribution->getHoulyAchievedAsHoure('aseance');
}
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 getRemunerationDueBySY(Schoolyear $schoolyear)
{
if($this->typePay=='Salary') return $this->pcPay*count($this->getMonthBySY($schoolyear));
$m=0;
foreach ($this->getAttributionsBySY($schoolyear) as $attribution){
$m+=$attribution->getRemunerationDue();
}
return $m;
}
public function getRemunerationRegledBySY(Schoolyear $schoolyear)
{
$m=0;
foreach($this->getAregulations() 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 getHourlyByMonth($month)
{
$hourly=0;
foreach($this->getAttributions() as $attribution){
$hourly+=$attribution->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)
{
if ($this->getTypePay()=='Salary') return $this->pcPay;
$m=0;
foreach ($this->getAttributions() as $attribution){
$m+=$attribution->getRemunerationByMonth($month);
}
return $m;
}
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'];
}
}