<?php
namespace App\Entity;
use App\Entity\Helper\Timestamps;
use App\Enum\Employee\RelationShipStatus;
use App\Enum\Employee\Gender;
use App\Repository\EmployeeRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Annotation\Ignore;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass=EmployeeRepository::class)
* @ORM\HasLifecycleCallbacks()
* @Vich\Uploadable()
*/
class Employee implements \Serializable
{
use Timestamps;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToOne(targetEntity=User::class, inversedBy="employee", cascade={"persist", "remove"})
*/
private $user;
/**
* @ORM\Column(type="string", length=255)
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255)
*/
private $lastName;
/**
* @ORM\Column(type="date", nullable=true)
*/
private $dateOfBirth;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $cityOfBirth;
/**
* @ORM\Column(type="gender")
*/
private ?Gender $gender = null;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $numberOfChildrens;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $phoneNumber;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $cin;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $cnss;
/**
* @ORM\OneToMany(targetEntity=EmployeeHistory::class, mappedBy="employee")
*/
private $employeeHistories;
/**
* @ORM\Column(type="relationShipStatus")
*/
private ?RelationShipStatus $relationShipStatus = null;
/**
* @Vich\UploadableField(mapping="pictures", fileNameProperty="picture")
* @var File|null
* @Ignore()
*/
private ?File $pictureFile = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $picture = null;
/**
* @ORM\OneToMany(targetEntity=EmployeeHourLog::class, mappedBy="employee", orphanRemoval=true)
* @ORM\OrderBy({"date" = "ASC"})
*/
private $employeeHourLogs;
/**
* @ORM\OneToOne(targetEntity=EmployeeHistory::class, cascade={"persist", "remove"})
*/
private $lastHistory;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $dateOld;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $dateCnss;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $registration_number;
/**
* @ORM\OneToMany(targetEntity=EmployeeAdvance::class, mappedBy="employee", orphanRemoval=true)
*/
private $employeeAdvances;
public function __construct()
{
$this->employeeHistories = new ArrayCollection();
$this->employeeHourLogs = new ArrayCollection();
$this->employeeAdvances = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getDateOfBirth(): ?\DateTimeInterface
{
return $this->dateOfBirth;
}
public function setDateOfBirth(?\DateTimeInterface $dateOfBirth): self
{
$this->dateOfBirth = $dateOfBirth;
return $this;
}
public function getCityOfBirth(): ?string
{
return $this->cityOfBirth;
}
public function setCityOfBirth(?string $cityOfBirth): self
{
$this->cityOfBirth = $cityOfBirth;
return $this;
}
public function getGender(): ?Gender
{
return $this->gender;
}
public function setGender(?Gender $gender): self
{
$this->gender = $gender;
return $this;
}
public function getNumberOfChildrens(): ?int
{
return $this->numberOfChildrens;
}
public function setNumberOfChildrens(int $numberOfChildrens): self
{
$this->numberOfChildrens = $numberOfChildrens;
return $this;
}
public function getPhoneNumber(): ?string
{
return $this->phoneNumber;
}
public function setPhoneNumber(?string $phoneNumber): self
{
$this->phoneNumber = $phoneNumber;
return $this;
}
public function getCnss(): ?string
{
return $this->cnss;
}
public function setCnss(string $cnss): self
{
$this->cnss = $cnss;
return $this;
}
public function getCin(): ?string
{
return $this->cin;
}
public function setCin(string $cin): self
{
$this->cin = $cin;
return $this;
}
/**
* @return Collection<int, EmployeeHistory>
*/
public function getEmployeeHistories(): Collection
{
return $this->employeeHistories;
}
public function addEmployeeHistory(EmployeeHistory $employeeHistory): self
{
if (!$this->employeeHistories->contains($employeeHistory)) {
$this->employeeHistories[] = $employeeHistory;
$employeeHistory->setEmploye($this);
}
return $this;
}
public function removeEmployeeHistory(EmployeeHistory $employeeHistory): self
{
if ($this->employeeHistories->removeElement($employeeHistory)) {
// set the owning side to null (unless already changed)
if ($employeeHistory->getEmploye() === $this) {
$employeeHistory->setEmploye(null);
}
}
return $this;
}
public function getRelationShipStatus(): ?RelationShipStatus
{
return $this->relationShipStatus;
}
public function setRelationShipStatus(?RelationShipStatus $relationShipStatus): self
{
$this->relationShipStatus = $relationShipStatus;
return $this;
}
/**
* @return File|null
*/
public function getPictureFile(): ?File
{
return $this->pictureFile;
}
/**
* @param File|null $pictureFile
* @return Employee
*/
public function setPictureFile(?File $pictureFile): self
{
$this->pictureFile = $pictureFile;
return $this;
}
/**
* @return string|null
*/
public function getPicture(): ?string
{
return $this->picture;
}
/**
* @param string|null $picture
* @return self
*/
public function setPicture(?string $picture): self
{
$this->picture = $picture;
return $this;
}
/**
* Get Full Name of the employee
* @return string
*/
public function getFullName(): string
{
return sprintf('%s %s', $this->lastName, $this->firstName);
}
/**
* Get last history data
* @return EmployeeHistory|bool
*/
public function getLastHistoryOrNull(): EmployeeHistory|bool
{
return !empty($this->lastHistory) ? $this->lastHistory : false;
}
/**
* Get last history data
* @return EmployeeHistory|bool
* @deprecated Check the newest method
*/
public function getLastHistoryOrNullOld(): EmployeeHistory|bool
{
return $this->hasHistory() ? $this->getEmployeeHistories()->last() : false;
}
/**
* Check if there is experiences
* @return EmployeeHistory|bool
*/
public function hasHistory(): bool
{
return !empty($this->getLastHistory());
}
/**
* Check if the employee is Active
* @return bool
*/
public function isActiveEmployee(): bool
{
return $this->hasHistory() && (
empty($this->getLastHistoryOrNull()->getEndDate()) || $this->getLastHistoryOrNull()->getEndDate() >= new \DateTime()
);
}
public function serialize(): string
{
return serialize(
[
$this->id,
]
);
}
/**
* @param string $serialized
*/
public function unserialize($serialized): void
{
[
$this->id,
] = unserialize($serialized);
}
public function __serialize(): array
{
return [
];
}
public function __unserialize(array $data): void
{
// TODO: Implement __unserialize() method.
}
/**
* @return Collection<int, EmployeeHourLog>
*/
public function getEmployeeHourLogs(): Collection
{
return $this->employeeHourLogs;
}
/**
* Get Only Hour Logs For Given Date
* @param $startDate
* @param $endDate
* @return Collection<int, EmployeeHourLog>
*/
public function getDateEmployeeHourLogs($startDate, $endDate): Collection
{
return $this->employeeHourLogs->filter(function ($employeeHourLog) use ($startDate, $endDate) {
/** @var EmployeeHourLog $employeeHourLog */
$startDate = \DateTime::createFromFormat('d/m/Y H:i', sprintf('%s %s', $startDate, '00:00'));
$endDate = \DateTime::createFromFormat('d/m/Y H:i', sprintf('%s %s', $endDate, '23:59'));
return $startDate <= $employeeHourLog->getDate() &&
$endDate >= $employeeHourLog->getDate();
});
}
public function addEmployeeHourLog(EmployeeHourLog $employeeHourLog): self
{
if (!$this->employeeHourLogs->contains($employeeHourLog)) {
$this->employeeHourLogs[] = $employeeHourLog;
$employeeHourLog->setEmployee($this);
}
return $this;
}
public function removeEmployeeHourLog(EmployeeHourLog $employeeHourLog): self
{
if ($this->employeeHourLogs->removeElement($employeeHourLog)) {
// set the owning side to null (unless already changed)
if ($employeeHourLog->getEmployee() === $this) {
$employeeHourLog->setEmployee(null);
}
}
return $this;
}
public function getLastHistory(): ?EmployeeHistory
{
return $this->lastHistory;
}
public function setLastHistory(?EmployeeHistory $lastHistory): self
{
$this->lastHistory = $lastHistory;
return $this;
}
public function getDateOld(): ?\DateTimeInterface
{
return $this->dateOld;
}
public function setDateOld(?\DateTimeInterface $dateOld): self
{
$this->dateOld = $dateOld;
return $this;
}
public function getDateCnss(): ?\DateTimeInterface
{
return $this->dateCnss;
}
public function setDateCnss(?\DateTimeInterface $dateCnss): self
{
$this->dateCnss = $dateCnss;
return $this;
}
public function getRegistrationNumber(): ?int
{
return $this->registration_number;
}
public function setRegistrationNumber(?int $registration_number): self
{
$this->registration_number = $registration_number;
return $this;
}
/**
* @return Collection<int, EmployeeAdvance>
*/
public function getEmployeeAdvances(): Collection
{
return $this->employeeAdvances;
}
public function addEmployeeAdvance(EmployeeAdvance $employeeAdvance): self
{
if (!$this->employeeAdvances->contains($employeeAdvance)) {
$this->employeeAdvances[] = $employeeAdvance;
$employeeAdvance->setEmployee($this);
}
return $this;
}
public function removeEmployeeAdvance(EmployeeAdvance $employeeAdvance): self
{
if ($this->employeeAdvances->removeElement($employeeAdvance)) {
// set the owning side to null (unless already changed)
if ($employeeAdvance->getEmployee() === $this) {
$employeeAdvance->setEmployee(null);
}
}
return $this;
}
public function getEmployeeAdvance1($year, $month)
{
return $this->employeeAdvances->filter(function (EmployeeAdvance $employeeAdvance) use ($year, $month) {
if ($year !== $employeeAdvance->getYear() || $month !== $employeeAdvance->getMonth()) {
return false;
}
return $employeeAdvance->getAdvance1();
});
}
}