src/Entity/Employee.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Helper\Timestamps;
  4. use App\Enum\Employee\RelationShipStatus;
  5. use App\Enum\Employee\Gender;
  6. use App\Repository\EmployeeRepository;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Symfony\Component\HttpFoundation\File\File;
  11. use Symfony\Component\Serializer\Annotation\Ignore;
  12. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  13. /**
  14.  * @ORM\Entity(repositoryClass=EmployeeRepository::class)
  15.  * @ORM\HasLifecycleCallbacks()
  16.  * @Vich\Uploadable()
  17.  */
  18. class Employee implements \Serializable
  19. {
  20.     use Timestamps;
  21.     /**
  22.      * @ORM\Id
  23.      * @ORM\GeneratedValue
  24.      * @ORM\Column(type="integer")
  25.      */
  26.     private $id;
  27.     /**
  28.      * @ORM\OneToOne(targetEntity=User::class, inversedBy="employee", cascade={"persist", "remove"})
  29.      */
  30.     private $user;
  31.     /**
  32.      * @ORM\Column(type="string", length=255)
  33.      */
  34.     private $firstName;
  35.     /**
  36.      * @ORM\Column(type="string", length=255)
  37.      */
  38.     private $lastName;
  39.     /**
  40.      * @ORM\Column(type="date", nullable=true)
  41.      */
  42.     private $dateOfBirth;
  43.     /**
  44.      * @ORM\Column(type="string", length=255, nullable=true)
  45.      */
  46.     private $cityOfBirth;
  47.     /**
  48.      * @ORM\Column(type="gender")
  49.      */
  50.     private ?Gender $gender null;
  51.     /**
  52.      * @ORM\Column(type="integer", nullable=true)
  53.      */
  54.     private $numberOfChildrens;
  55.     /**
  56.      * @ORM\Column(type="string", length=255, nullable=true)
  57.      */
  58.     private $phoneNumber;
  59.     /**
  60.      * @ORM\Column(type="string", length=255, nullable=true)
  61.      */
  62.     private $cin;
  63.     /**
  64.      * @ORM\Column(type="string", length=255, nullable=true)
  65.      */
  66.     private $cnss;
  67.     /**
  68.      * @ORM\OneToMany(targetEntity=EmployeeHistory::class, mappedBy="employee")
  69.      */
  70.     private $employeeHistories;
  71.     /**
  72.      * @ORM\Column(type="relationShipStatus")
  73.      */
  74.     private ?RelationShipStatus $relationShipStatus null;
  75.     /**
  76.      * @Vich\UploadableField(mapping="pictures", fileNameProperty="picture")
  77.      * @var File|null
  78.      * @Ignore()
  79.      */
  80.     private ?File $pictureFile null;
  81.     /**
  82.      * @ORM\Column(type="string", length=255, nullable=true)
  83.      */
  84.     private ?string $picture null;
  85.     /**
  86.      * @ORM\OneToMany(targetEntity=EmployeeHourLog::class, mappedBy="employee", orphanRemoval=true)
  87.      * @ORM\OrderBy({"date" = "ASC"})
  88.      */
  89.     private $employeeHourLogs;
  90.     /**
  91.      * @ORM\OneToOne(targetEntity=EmployeeHistory::class, cascade={"persist", "remove"})
  92.      */
  93.     private $lastHistory;
  94.     /**
  95.      * @ORM\Column(type="datetime", nullable=true)
  96.      */
  97.     private $dateOld;
  98.     /**
  99.      * @ORM\Column(type="datetime", nullable=true)
  100.      */
  101.     private $dateCnss;
  102.     /**
  103.      * @ORM\Column(type="integer", nullable=true)
  104.      */
  105.     private $registration_number;
  106.     /**
  107.      * @ORM\OneToMany(targetEntity=EmployeeAdvance::class, mappedBy="employee", orphanRemoval=true)
  108.      */
  109.     private $employeeAdvances;
  110.     public function __construct()
  111.     {
  112.         $this->employeeHistories = new ArrayCollection();
  113.         $this->employeeHourLogs = new ArrayCollection();
  114.         $this->employeeAdvances = new ArrayCollection();
  115.     }
  116.     public function getId(): ?int
  117.     {
  118.         return $this->id;
  119.     }
  120.     public function getUser(): ?User
  121.     {
  122.         return $this->user;
  123.     }
  124.     public function setUser(?User $user): self
  125.     {
  126.         $this->user $user;
  127.         return $this;
  128.     }
  129.     public function getFirstName(): ?string
  130.     {
  131.         return $this->firstName;
  132.     }
  133.     public function setFirstName(string $firstName): self
  134.     {
  135.         $this->firstName $firstName;
  136.         return $this;
  137.     }
  138.     public function getLastName(): ?string
  139.     {
  140.         return $this->lastName;
  141.     }
  142.     public function setLastName(string $lastName): self
  143.     {
  144.         $this->lastName $lastName;
  145.         return $this;
  146.     }
  147.     public function getDateOfBirth(): ?\DateTimeInterface
  148.     {
  149.         return $this->dateOfBirth;
  150.     }
  151.     public function setDateOfBirth(?\DateTimeInterface $dateOfBirth): self
  152.     {
  153.         $this->dateOfBirth $dateOfBirth;
  154.         return $this;
  155.     }
  156.     public function getCityOfBirth(): ?string
  157.     {
  158.         return $this->cityOfBirth;
  159.     }
  160.     public function setCityOfBirth(?string $cityOfBirth): self
  161.     {
  162.         $this->cityOfBirth $cityOfBirth;
  163.         return $this;
  164.     }
  165.     public function getGender(): ?Gender
  166.     {
  167.         return $this->gender;
  168.     }
  169.     public function setGender(?Gender $gender): self
  170.     {
  171.         $this->gender $gender;
  172.         return $this;
  173.     }
  174.     public function getNumberOfChildrens(): ?int
  175.     {
  176.         return $this->numberOfChildrens;
  177.     }
  178.     public function setNumberOfChildrens(int $numberOfChildrens): self
  179.     {
  180.         $this->numberOfChildrens $numberOfChildrens;
  181.         return $this;
  182.     }
  183.     public function getPhoneNumber(): ?string
  184.     {
  185.         return $this->phoneNumber;
  186.     }
  187.     public function setPhoneNumber(?string $phoneNumber): self
  188.     {
  189.         $this->phoneNumber $phoneNumber;
  190.         return $this;
  191.     }
  192.     public function getCnss(): ?string
  193.     {
  194.         return $this->cnss;
  195.     }
  196.     public function setCnss(string $cnss): self
  197.     {
  198.         $this->cnss $cnss;
  199.         return $this;
  200.     }
  201.     public function getCin(): ?string
  202.     {
  203.         return $this->cin;
  204.     }
  205.     public function setCin(string $cin): self
  206.     {
  207.         $this->cin $cin;
  208.         return $this;
  209.     }
  210.     /**
  211.      * @return Collection<int, EmployeeHistory>
  212.      */
  213.     public function getEmployeeHistories(): Collection
  214.     {
  215.         return $this->employeeHistories;
  216.     }
  217.     public function addEmployeeHistory(EmployeeHistory $employeeHistory): self
  218.     {
  219.         if (!$this->employeeHistories->contains($employeeHistory)) {
  220.             $this->employeeHistories[] = $employeeHistory;
  221.             $employeeHistory->setEmploye($this);
  222.         }
  223.         return $this;
  224.     }
  225.     public function removeEmployeeHistory(EmployeeHistory $employeeHistory): self
  226.     {
  227.         if ($this->employeeHistories->removeElement($employeeHistory)) {
  228.             // set the owning side to null (unless already changed)
  229.             if ($employeeHistory->getEmploye() === $this) {
  230.                 $employeeHistory->setEmploye(null);
  231.             }
  232.         }
  233.         return $this;
  234.     }
  235.     public function getRelationShipStatus(): ?RelationShipStatus
  236.     {
  237.         return $this->relationShipStatus;
  238.     }
  239.     public function setRelationShipStatus(?RelationShipStatus $relationShipStatus): self
  240.     {
  241.         $this->relationShipStatus $relationShipStatus;
  242.         return $this;
  243.     }
  244.     /**
  245.      * @return File|null
  246.      */
  247.     public function getPictureFile(): ?File
  248.     {
  249.         return $this->pictureFile;
  250.     }
  251.     /**
  252.      * @param File|null $pictureFile
  253.      * @return Employee
  254.      */
  255.     public function setPictureFile(?File $pictureFile): self
  256.     {
  257.         $this->pictureFile $pictureFile;
  258.         return $this;
  259.     }
  260.     /**
  261.      * @return string|null
  262.      */
  263.     public function getPicture(): ?string
  264.     {
  265.         return $this->picture;
  266.     }
  267.     /**
  268.      * @param string|null $picture
  269.      * @return self
  270.      */
  271.     public function setPicture(?string $picture): self
  272.     {
  273.         $this->picture $picture;
  274.         return $this;
  275.     }
  276.     /**
  277.      * Get Full Name of the employee
  278.      * @return string
  279.      */
  280.     public function getFullName(): string
  281.     {
  282.         return sprintf('%s %s'$this->lastName$this->firstName);
  283.     }
  284.     /**
  285.      * Get last history data
  286.      * @return EmployeeHistory|bool
  287.      */
  288.     public function getLastHistoryOrNull(): EmployeeHistory|bool
  289.     {
  290.         return !empty($this->lastHistory) ? $this->lastHistory false;
  291.     }
  292.     /**
  293.      * Get last history data
  294.      * @return EmployeeHistory|bool
  295.      * @deprecated Check the newest method
  296.      */
  297.     public function getLastHistoryOrNullOld(): EmployeeHistory|bool
  298.     {
  299.         return $this->hasHistory() ? $this->getEmployeeHistories()->last() : false;
  300.     }
  301.     /**
  302.      * Check if there is experiences
  303.      * @return EmployeeHistory|bool
  304.      */
  305.     public function hasHistory(): bool
  306.     {
  307.         return !empty($this->getLastHistory());
  308.     }
  309.     /**
  310.      * Check if the employee is Active
  311.      * @return bool
  312.      */
  313.     public function isActiveEmployee(): bool
  314.     {
  315.         return $this->hasHistory() && (
  316.                 empty($this->getLastHistoryOrNull()->getEndDate()) || $this->getLastHistoryOrNull()->getEndDate() >= new \DateTime()
  317.             );
  318.     }
  319.     public function serialize(): string
  320.     {
  321.         return serialize(
  322.             [
  323.                 $this->id,
  324.             ]
  325.         );
  326.     }
  327.     /**
  328.      * @param string $serialized
  329.      */
  330.     public function unserialize($serialized): void
  331.     {
  332.         [
  333.             $this->id,
  334.         ] = unserialize($serialized);
  335.     }
  336.     public function __serialize(): array
  337.     {
  338.         return [
  339.         ];
  340.     }
  341.     public function __unserialize(array $data): void
  342.     {
  343.         // TODO: Implement __unserialize() method.
  344.     }
  345.     /**
  346.      * @return Collection<int, EmployeeHourLog>
  347.      */
  348.     public function getEmployeeHourLogs(): Collection
  349.     {
  350.         return $this->employeeHourLogs;
  351.     }
  352.     /**
  353.      * Get Only Hour Logs For Given Date
  354.      * @param $startDate
  355.      * @param $endDate
  356.      * @return Collection<int, EmployeeHourLog>
  357.      */
  358.     public function getDateEmployeeHourLogs($startDate$endDate): Collection
  359.     {
  360.         return $this->employeeHourLogs->filter(function ($employeeHourLog) use ($startDate$endDate) {
  361.             /** @var EmployeeHourLog $employeeHourLog */
  362.             $startDate \DateTime::createFromFormat('d/m/Y H:i'sprintf('%s %s'$startDate'00:00'));
  363.             $endDate \DateTime::createFromFormat('d/m/Y H:i'sprintf('%s %s'$endDate'23:59'));
  364.             return $startDate <= $employeeHourLog->getDate() &&
  365.                 $endDate >= $employeeHourLog->getDate();
  366.         });
  367.     }
  368.     public function addEmployeeHourLog(EmployeeHourLog $employeeHourLog): self
  369.     {
  370.         if (!$this->employeeHourLogs->contains($employeeHourLog)) {
  371.             $this->employeeHourLogs[] = $employeeHourLog;
  372.             $employeeHourLog->setEmployee($this);
  373.         }
  374.         return $this;
  375.     }
  376.     public function removeEmployeeHourLog(EmployeeHourLog $employeeHourLog): self
  377.     {
  378.         if ($this->employeeHourLogs->removeElement($employeeHourLog)) {
  379.             // set the owning side to null (unless already changed)
  380.             if ($employeeHourLog->getEmployee() === $this) {
  381.                 $employeeHourLog->setEmployee(null);
  382.             }
  383.         }
  384.         return $this;
  385.     }
  386.     public function getLastHistory(): ?EmployeeHistory
  387.     {
  388.         return $this->lastHistory;
  389.     }
  390.     public function setLastHistory(?EmployeeHistory $lastHistory): self
  391.     {
  392.         $this->lastHistory $lastHistory;
  393.         return $this;
  394.     }
  395.     public function getDateOld(): ?\DateTimeInterface
  396.     {
  397.         return $this->dateOld;
  398.     }
  399.     public function setDateOld(?\DateTimeInterface $dateOld): self
  400.     {
  401.         $this->dateOld $dateOld;
  402.         return $this;
  403.     }
  404.     public function getDateCnss(): ?\DateTimeInterface
  405.     {
  406.         return $this->dateCnss;
  407.     }
  408.     public function setDateCnss(?\DateTimeInterface $dateCnss): self
  409.     {
  410.         $this->dateCnss $dateCnss;
  411.         return $this;
  412.     }
  413.     public function getRegistrationNumber(): ?int
  414.     {
  415.         return $this->registration_number;
  416.     }
  417.     public function setRegistrationNumber(?int $registration_number): self
  418.     {
  419.         $this->registration_number $registration_number;
  420.         return $this;
  421.     }
  422.     /**
  423.      * @return Collection<int, EmployeeAdvance>
  424.      */
  425.     public function getEmployeeAdvances(): Collection
  426.     {
  427.         return $this->employeeAdvances;
  428.     }
  429.     public function addEmployeeAdvance(EmployeeAdvance $employeeAdvance): self
  430.     {
  431.         if (!$this->employeeAdvances->contains($employeeAdvance)) {
  432.             $this->employeeAdvances[] = $employeeAdvance;
  433.             $employeeAdvance->setEmployee($this);
  434.         }
  435.         return $this;
  436.     }
  437.     public function removeEmployeeAdvance(EmployeeAdvance $employeeAdvance): self
  438.     {
  439.         if ($this->employeeAdvances->removeElement($employeeAdvance)) {
  440.             // set the owning side to null (unless already changed)
  441.             if ($employeeAdvance->getEmployee() === $this) {
  442.                 $employeeAdvance->setEmployee(null);
  443.             }
  444.         }
  445.         return $this;
  446.     }
  447.     public function getEmployeeAdvance1($year$month)
  448.     {
  449.         return $this->employeeAdvances->filter(function (EmployeeAdvance $employeeAdvance) use ($year$month) {
  450.             if ($year !== $employeeAdvance->getYear() || $month !== $employeeAdvance->getMonth()) {
  451.                 return false;
  452.             }
  453.             return $employeeAdvance->getAdvance1();
  454.         });
  455.     }
  456. }