src/Entity/Training/Former.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Training;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Entity\Schoolyear;
  5. use App\Repository\Training\FormerRepository;
  6. use App\Service\SchoolyearService;
  7. use App\Traits\Actions;
  8. use App\Entity\Training\Assignation;
  9. use DateInterval;
  10. use DatePeriod;
  11. use DateTime;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use App\Entity\User;
  16. #[ORM\Entity(repositoryClassFormerRepository::class)]
  17. #[ORM\Table(name'hs_tc_training_former')]
  18. #[ApiResource]
  19. class Former implements \Stringable
  20. {
  21.     use Actions;
  22.     #[ORM\Column(name"first_name"type"string"length255)]
  23.     private string $firstName;
  24.     #[ORM\Column(name"last_name"type"string"length255)]
  25.     private string $lastName;
  26.     #[ORM\Column(name"email"type"string"length255nullabletrue)]
  27.     private ?string $email;
  28.     #[ORM\Column(name"phone"type"string"length255nullabletrue)]
  29.     private ?string $phone;
  30.     #[ORM\Column(name'wp'type'string'length255nullabletrue)]
  31.     private ?string $wp null;
  32.     #[ORM\Column(name"specialty"type"string"length255nullabletrue)]
  33.     private ?string $specialty;
  34.     #[ORM\Column(name'type_pay'type'string'length255nullabletrue)]
  35.     private ?string $typePay null;
  36.     #[ORM\Column(name'pc_pay'type'float'nullabletrue)]
  37.     private ?float $pcPay null;
  38.     #[ORM\Column(name'min_pay'type'float'nullabletrue)]
  39.     private ?float $minPay null;
  40.     #[ORM\Column(type"json"nullabletrue)]
  41.     private array $months=[];
  42.     #[ORM\OneToOne(inversedBy"former"targetEntityUser::class, cascade: ["persist""remove"])]
  43.     #[ORM\JoinColumn(nullabletrue)]
  44.     private User $user;
  45.     #[ORM\OneToMany(mappedBy"former"targetEntity"Assignation")]
  46.     private Collection $assignations;
  47.     #[ORM\OneToMany(mappedBy'former'targetEntityFregulation::class, cascade: ["persist"], orphanRemovaltrue)]
  48.     private Collection $regulations;
  49.     public function __construct()
  50.     {
  51.         // date_default_timezone_set('Africa/Casablanca');
  52.         $this->createAt=new \DateTime('now');
  53.         $this->published=false;
  54.         $this->wp="212";
  55.     }
  56.     /**
  57.      * @return string
  58.      */
  59.     public function getFirstName(): ?string
  60.     {
  61.         return $this->firstName;
  62.     }
  63.     /**
  64.      * @param string $firstName
  65.      */
  66.     public function setFirstName(?string $firstName): Former
  67.     {
  68.         $this->firstName $firstName;
  69.         return $this;
  70.     }
  71.     /**
  72.      * @return string
  73.      */
  74.     public function getLastName(): ?string
  75.     {
  76.         return $this->lastName;
  77.     }
  78.     /**
  79.      * @param string $lastName
  80.      */
  81.     public function setLastName(?string $lastName): Former
  82.     {
  83.         $this->lastName $lastName;
  84.         return $this;
  85.     }
  86.     /**
  87.      * @return string
  88.      */
  89.     public function getEmail()
  90.     {
  91.         return $this->email;
  92.     }
  93.     /**
  94.      * @param string $email
  95.      * @return Former
  96.      */
  97.     public function setEmail($email)
  98.     {
  99.         $this->email $email;
  100.         return $this;
  101.     }
  102.     /**
  103.      * @return string
  104.      */
  105.     public function getPhone()
  106.     {
  107.         return $this->phone;
  108.     }
  109.     /**
  110.      * @param string $phone
  111.      * @return Former
  112.      */
  113.     public function setPhone($phone)
  114.     {
  115.         $this->phone $phone;
  116.         return $this;
  117.     }
  118.     public function getWp(): ?string
  119.     {
  120.         return $this->wp;
  121.     }
  122.     public function setWp(?string $wp): void
  123.     {
  124.         $this->wp $wp;
  125.     }
  126.     /**
  127.      * @return string
  128.      */
  129.     public function getSpecialty()
  130.     {
  131.         return $this->specialty;
  132.     }
  133.     /**
  134.      * @param string $specialty
  135.      * @return Former
  136.      */
  137.     public function setSpecialty($specialty)
  138.     {
  139.         $this->specialty $specialty;
  140.         return $this;
  141.     }
  142.     public function __toString(): string
  143.     {
  144.         return $this->lastName.' '.$this->firstName;
  145.     }
  146.     /**
  147.      * @return mixed
  148.      */
  149.     public function getUser()
  150.     {
  151.         return $this->user;
  152.     }
  153.     /**
  154.      * @return Former
  155.      */
  156.     public function setUser(mixed $user)
  157.     {
  158.         $this->user $user;
  159.         return $this;
  160.     }
  161.     /**
  162.      * Add assignation
  163.      *
  164.      *
  165.      * @return Former
  166.      */
  167.     public function addAssignation(Assignation $assignation)
  168.     {
  169.         $assignation->setFormer($this);
  170.         $this->assignations[] = $assignation;
  171.         return $this;
  172.     }
  173.     /**
  174.      * Remove assignation
  175.      */
  176.     public function removeAssignation(Assignation $assignation)
  177.     {
  178.         $this->assignations->removeElement($assignation);
  179.     }
  180.     /**
  181.      * Get assignations
  182.      *
  183.      * @return Collection
  184.      */
  185.     public function getAssignations()
  186.     {
  187.         return $this->assignations;
  188.     }
  189.     public function getTypePay(): ?string
  190.     {
  191.         return $this->typePay;
  192.     }
  193.     public function setTypePay(?string $typePay): void
  194.     {
  195.         $this->typePay $typePay;
  196.     }
  197.     public function getPcPay(): ?float
  198.     {
  199.         return $this->pcPay;
  200.     }
  201.     public function setPcPay(?float $pcPay): void
  202.     {
  203.         $this->pcPay $pcPay;
  204.     }
  205.     public function getTypePayText()
  206.     {
  207.         return match ($this->typePay) {
  208.             'hourly' => 'à l\'heure',
  209.             'Salary' => 'Salaire mensuel',
  210.             default => 'non indiqué',
  211.         };
  212.     }
  213.     public function getMinPay(): ?float
  214.     {
  215.         return $this->minPay;
  216.     }
  217.     public function setMinPay(?float $minPay): void
  218.     {
  219.         $this->minPay $minPay;
  220.     }
  221.     public function getMonths(): array
  222.     {
  223.         return $this->months;
  224.     }
  225.     public function setMonths(array $months): void
  226.     {
  227.         $this->months $months;
  228.     }
  229.     public function getFregulations(): Collection
  230.     {
  231.         return $this->regulations;
  232.     }
  233.     public function addFregulation(Fregulation $fregulation): self
  234.     {
  235.         if (!$this->regulations->contains($fregulation)) {
  236.             $this->regulations->add($fregulation);
  237.             $fregulation->setFormer($this);
  238.         }
  239.         return $this;
  240.     }
  241.     public function removeFregulation(Fregulation $fregulation): self
  242.     {
  243.         if ($this->regulations->removeElement($fregulation)) {
  244.             // set the owning side to null (unless already changed)
  245.             if ($fregulation->getFormer() === $this) {
  246.                 $fregulation->setFormer(null);
  247.             }
  248.         }
  249.         return $this;
  250.     }
  251.     public function getAssignationsBySY(Schoolyear $schoolyear)
  252.     {
  253.         return $this->assignations->filter(function (Assignation $assignation) use ($schoolyear) {
  254.             return $assignation->getSchoolYear() === $schoolyear;
  255.         });
  256.     }
  257.     public function getStartWorkInSY(Schoolyear $schoolyear)
  258.     {
  259.         $start null;
  260.         foreach ($this->getAssignationsBySY($schoolyear) as $att) {
  261.             if ($att->getStartAt() !== null) {
  262.                 if ($start === null || $att->getStartAt() < $start) {
  263.                     $start $att->getStartAt();
  264.                 }
  265.             }
  266.         }
  267.         return $start;
  268.     }
  269.     /**
  270.      * Retourne la date de fin de travail la plus tardive pour l'année scolaire donnée.
  271.      *
  272.      * @param Schoolyear $schoolyear
  273.      * @return null|\DateTimeInterface
  274.      */
  275.     public function getEndWorkInSY(Schoolyear $schoolyear): ?\DateTimeInterface
  276.     {
  277.         $end null;
  278.         foreach ($this->getAssignationsBySY($schoolyear) as $att) {
  279.             $endAt $att->getEndAt();
  280.             if ($endAt instanceof \DateTimeInterface) {
  281.                 if ($end === null || $endAt $end) {
  282.                     $end $endAt;
  283.                 }
  284.             }
  285.         }
  286.         return $end;
  287.     }
  288.     /**
  289.      * Retourne la liste des mois (format m/y) couverts par le formateur pour l'année scolaire donnée.
  290.      *
  291.      * @param Schoolyear $schoolyear
  292.      * @return array
  293.      */
  294.     public function getMonthBySY(Schoolyear $schoolyear): array
  295.     {
  296.         $months = [];
  297.         if ($this->typePay != 'Salary') {
  298.             $start $this->getStartWorkInSY($schoolyear);
  299.             $end $this->getEndWorkInSY($schoolyear);
  300.             if ($start instanceof \DateTimeInterface && $end instanceof \DateTimeInterface) {
  301.                 // On inclut le mois de fin dans la période
  302.                 $endInclusive = (clone $end)->modify('first day of next month');
  303.                 $interval \DateInterval::createFromDateString('1 month');
  304.                 $period = new \DatePeriod($start$interval$endInclusive);
  305.                 foreach ($period as $date) {
  306.                     $months[] = $date->format('m/y');
  307.                 }
  308.             }
  309.         } else {
  310.             foreach ($schoolyear->getMonths('m/y') as $month) {
  311.                 if (in_array($month$this->months)) {
  312.                     $months[] = $month;
  313.                 }
  314.             }
  315.         }
  316.         return $months;
  317.     }
  318.     public function getHourlyBySY(Schoolyear $schoolyear)
  319.     {
  320.         $hourly=0;
  321.         /** @var Assignation $assignation */
  322.         foreach($this->getAssignationsBySY($schoolyear) as $assignation){
  323.             $hourly+=$assignation->getHoulyCountedAsHoure();
  324.         }
  325.         return $hourly;
  326.     }
  327.     public function getHourlyBySYOnStr(Schoolyear $schoolyear)
  328.     {
  329.         $h=floor($this->getHourlyBySY($schoolyear));
  330.         $m=($this->getHourlyBySY($schoolyear)-$h)*60;
  331.         return $h.'h'.$m.'min';
  332.     }
  333. //    public function getMonthBySY(Schoolyear $schoolyear)
  334. //    {
  335. //            $m=0;
  336. //            foreach ($schoolyear->getMonths('m/y') as $month){
  337. //                if(in_array($month, $this->months)) $m++;
  338. //            }
  339. //            return $m;
  340. //    }
  341.     public function getRemunerationDueBySY(Schoolyear $schoolyear)
  342.     {
  343.         if($this->typePay!='Salary'){
  344.             $m=0;
  345.             foreach($this->getAssignationsBySY($schoolyear) as $assignation){
  346.                 $m+=$assignation->getRemunerationDue();
  347.             }
  348.             return $m;
  349.         }
  350.         else{
  351.             $m=0;
  352.             foreach ($schoolyear->getMonths('m/y') as $month){
  353.                 if(in_array($month$this->months)) $m++;
  354.             }
  355.             return $m*$this->getPcPay();
  356.         }
  357.     }
  358.     public function getRemunerationRegledBySY(Schoolyear $schoolyear)
  359.     {
  360.         $m=0;
  361.         foreach($this->getFregulations() as $fregulation){
  362.             if($fregulation->getSchoolyear() === $schoolyear)
  363.             $m+=$fregulation->getAmount();
  364.         }
  365.         return $m;
  366.     }
  367.     public function getRemunerationResteBySY(Schoolyear $schoolyear)
  368.     {
  369.         return $this->getRemunerationDueBySY($schoolyear)-$this->getRemunerationRegledBySY($schoolyear);
  370.     }
  371.     public function getSeancesByMonth($month){
  372.         $items=[];
  373.         foreach ($this->getAssignations() as $assignation){
  374.             foreach ($assignation->getSeancesByMonth($month) as $seance){
  375.                 $items[]=($seance);
  376.             }
  377.         }
  378.         usort($items, function($a$b) {
  379.             $dateA DateTime::createFromFormat('d-m-Y H:i'$a->getTakesplaceOn()->format('d-m-Y').' '.$a->getStartAt());
  380.             $dateB DateTime::createFromFormat('d-m-Y H:i'$b->getTakesplaceOn()->format('d-m-Y').' '.$b->getStartAt());
  381.             return $dateA <=> $dateB;
  382.         });
  383.         return $items;
  384.     }
  385.     public function getHourlyByMonth($month)
  386.     {
  387.         $hourly=0;
  388.         foreach($this->getAssignations() as $assignation){
  389.             $hourly+=$assignation->getHourelyWorkedByMonth($month);
  390.         }
  391.         return $hourly;
  392.     }
  393.     public function getHourlyByMonthOnStr($month)
  394.     {
  395.         $h=floor($this->getHourlyByMonth($month));
  396.         $m=floor(($this->getHourlyByMonth($month)-$h)*60);
  397.         return $h.'h'.$m.'min';
  398.     }
  399.     public function getRemunerationDueByMonth($month)
  400.     {
  401.         $m=0;
  402.         foreach ($this->getAssignations() as $assignation){
  403.             $m+=$assignation->getRemunerationByMonth($month);
  404.         }
  405.         return $m;
  406.     }
  407.     public function getTableServiceBySY(Schoolyear $schoolyear)
  408.     {
  409.         $array=[];
  410.         foreach($this->getAssignationsBySY($schoolyear) as $assignation){
  411.             foreach ($assignation->getSeances() as $seance){
  412.                 $array[$assignation->getId()][$seance->getTakesplaceOn()->format('m/y')][]=$seance;
  413.             }
  414.         }
  415.         return $array;
  416.     }
  417.     public function getRegulationDetails(Schoolyear $schoolyear)
  418.     {
  419.         $details=[];
  420.         $total=$this->getRemunerationRegledBySY($schoolyear);
  421.         foreach ($schoolyear->getMonths('m/y') as $month){
  422.             $details[$month]=min($total,$this->getRemunerationDueByMonth($month));
  423.             $total=max($total-$this->getRemunerationDueByMonth($month),0);
  424.         }
  425.         $details['av']=$total;
  426.         return $details;
  427.     }
  428.     public function getRegulationByMonth(Schoolyear $schoolyear$month)
  429.     {
  430.         if(in_array($month$schoolyear->getMonths('m/y'))){
  431.             return $this->getRegulationDetails($schoolyear)[$month];
  432.         }
  433.         return 0;
  434.     }
  435.     public function getResteByMonth(Schoolyear $schoolyear$month)
  436.     {
  437.         return max($this->getRemunerationDueByMonth($month)-$this->getRegulationByMonth($schoolyear$month),0);
  438.     }
  439.     public function getAvanceBySY(Schoolyear $schoolyear)
  440.     {
  441.         return $this->getRegulationDetails($schoolyear)['av'];
  442.     }
  443. }