<?php
namespace App\Entity;
use App\Repository\UserRoleRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: UserRoleRepository::class)]
class UserRole
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 255)]
private $name;
#[ORM\Column(type: 'float', nullable: true)]
private $cost;
#[ORM\ManyToOne(targetEntity: Department::class, inversedBy: 'userRoles')]
#[ORM\JoinColumn(nullable: false)]
private $department;
#[ORM\OneToMany(targetEntity: PersonalInfo::class, mappedBy: 'userRole')]
private $personalInfo;
#[ORM\OneToMany(targetEntity: ProjectAllocatedHours::class, mappedBy: 'userRole')]
private $projectAllocatedHours;
#[ORM\OneToMany(targetEntity: DepartmentCost::class, mappedBy: 'userRole', cascade: ['persist'], orphanRemoval: true)]
private $departmentCosts;
public function __construct()
{
$this->personalInfo = new ArrayCollection();
$this->projectAllocatedHours = new ArrayCollection();
$this->departmentCosts = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getCost(): ?float
{
return $this->cost;
}
public function setCost(float $cost): self
{
$this->cost = $cost;
return $this;
}
public function getDepartment(): ?Department
{
return $this->department;
}
public function setDepartment(?Department $department): self
{
$this->department = $department;
return $this;
}
/**
* @return Collection|PersonalInfo[]
*/
public function getPersonalInfo(): Collection
{
return $this->personalInfo;
}
public function addPersonalInfo(PersonalInfo $personalInfo): self
{
if (!$this->personalInfo->contains($personalInfo)) {
$this->personalInfo[] = $personalInfo;
$personalInfo->setUserRole($this);
}
return $this;
}
public function removePersonalInfo(PersonalInfo $personalInfo): self
{
if ($this->personalInfo->removeElement($personalInfo)) {
// set the owning side to null (unless already changed)
if ($personalInfo->getUserRole() === $this) {
$personalInfo->setUserRole(null);
}
}
return $this;
}
/**
* @return Collection|ProjectAllocatedHours[]
*/
public function getProjectAllocatedHours(): Collection
{
return $this->projectAllocatedHours;
}
public function addProjectAllocatedHour(ProjectAllocatedHours $projectAllocatedHour): self
{
if (!$this->projectAllocatedHours->contains($projectAllocatedHour)) {
$this->projectAllocatedHours[] = $projectAllocatedHour;
$projectAllocatedHour->setUserRole($this);
}
return $this;
}
public function removeProjectAllocatedHour(ProjectAllocatedHours $projectAllocatedHour): self
{
if ($this->projectAllocatedHours->removeElement($projectAllocatedHour)) {
// set the owning side to null (unless already changed)
if ($projectAllocatedHour->getUserRole() === $this) {
$projectAllocatedHour->setUserRole(null);
}
}
return $this;
}
/**
* @return Collection|DepartmentCost[]
*/
public function getDepartmentCosts(): Collection
{
return $this->departmentCosts;
}
public function addDepartmentCost(DepartmentCost $departmentCost): self
{
if (!$this->departmentCosts->contains($departmentCost)) {
$this->departmentCosts[] = $departmentCost;
$departmentCost->setUserRole($this);
}
return $this;
}
public function removeDepartmentCost(DepartmentCost $departmentCost): self
{
if ($this->departmentCosts->removeElement($departmentCost)) {
// set the owning side to null (unless already changed)
if ($departmentCost->getUserRole() === $this) {
$departmentCost->setUserRole(null);
}
}
return $this;
}
public function getLatestDepartmentRate()
{
$departmentCosts = $this->getDepartmentCosts();
$latestDepartmentCost = null;
foreach ($departmentCosts as $departmentCost) {
if ($latestDepartmentCost == null) {
$latestDepartmentCost = $departmentCost;
} else {
if ($departmentCost->getValidAt() > $latestDepartmentCost->getValidAt() && $departmentCost->getValidAt() < new \DateTime) {
$latestDepartmentCost = $departmentCost;
}
}
}
return $latestDepartmentCost;
}
}