<?php
namespace App\Entity;
use App\Entity\Task\MonitoringObjective;
use App\Repository\EmployeeRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=EmployeeRepository::class)
*/
class Employee
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $role;
/**
* @ORM\Column(type="string", length=255)
*/
private $phone;
/**
* @ORM\Column(type="string", length=255)
*/
private $email;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $image;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $comment;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $lastName;
/**
* @ORM\ManyToOne(targetEntity=Position::class)
*/
private $position;
/**
* @ORM\OneToOne(targetEntity=User::class, inversedBy="employee", cascade={"persist", "remove"})
*/
private $user;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $password;
/**
* @ORM\ManyToOne(targetEntity=Company::class, inversedBy="employees")
*/
private $company;
/**
* @ORM\ManyToMany(targetEntity=Field::class, mappedBy="employees")
*/
private $fields;
/**
* @ORM\OneToMany(targetEntity=MonitoringObjective::class, mappedBy="viewer")
*/
private $monitoringObjectives;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="employees")
*/
private $creator;
public function __construct()
{
$this->fields = new ArrayCollection();
$this->monitoringObjectives = 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 getLastDateConnect(): ?\DateTimeInterface
{
// if($this->getUser()) {
// return $this->getUser()->getLastConnect();
// }
return null;
}
public function getRole(): ?string
{
return $this->role;
}
public function getRoleName(string $role): ?string
{
if($role === Role::ADMIN){
return 'admin';
}
return 'scout';
}
public function setRole(string $role): self
{
$this->role = $role;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getAccess(): ?string
{
if($this->role === Role::ADMIN){
return 'full';
}
return 'scout';
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
public function getComment(): ?string
{
return $this->comment;
}
public function setComment(string $comment): self
{
$this->comment = $comment;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(?string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getPosition(): ?Position
{
return $this->position;
}
public function setPosition(?Position $position): self
{
$this->position = $position;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getClients()
{
$response = [];
if($this->getFields()->count()){
foreach ($this->getFields() as $field) {
$response[$field->getClient()->getId()] = $field->getClient();
}
}
return $response;
}
public function getClientIds()
{
$response = [];
$clients = $this->getClients();
// if(!empty($clients)){
// foreach ($clients as $client) {
// $response[$client->getId()] = $client->getId();
// }
// }
if($this->getCompany()){
if($this->getCompany()->getCompanyGroup()){
if($this->getCompany()->getCompanyGroup()->getCompanies()->count() > 0){
/** @var Company $company */
foreach ($this->getCompany()->getCompanyGroup()->getCompanies() as $company) {
foreach ($company->getClients() as $client) {
$response[$client->getId()] = $client->getId();
}
}
}
}
}
return $response;
}
public function getFieldIds()
{
$response = [];
if($this->getFields()->count()){
foreach ($this->getFields() as $field) {
$response[] = $field->getId();
}
}
return $response;
}
public function getFieldsClient()
{
$response = [];
if($this->getFields()->count()){
foreach ($this->getFields() as $field) {
$response[$field->getClient()->getName()][] = $field;
}
}
return $response;
}
public function getFieldsClientIds(): array
{
$response = [];
if($this->getFields()->count() > 0){
foreach ($this->getFields() as $field) {
if(!in_array($field->getClient()->getId(),$response) && !$field->isRemoved()){
$response[] = $field->getClient()->getId();
}
}
}
return array_unique($response);
}
public function getFieldsTechcardIds(): array
{
$response = [];
if($this->getFields()->count()){
foreach ($this->getFields() as $field) {
if(!empty($field->getTechcard()) && !in_array($field->getTechcard()->getId(),$response) && !$field->isRemoved()){
$response[] = $field->getTechcard()->getId();
}
}
}
return $response;
}
public function getFullName(): string
{
$fullName = $this->name . ' ' . $this->lastName;
if(empty($fullName)){
$fullName = $this->email;
}
return $fullName;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(?string $password): self
{
$this->password = $password;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
/**
* @return Collection|Field[]
*/
public function getFields($removed = false): Collection
{
if($removed) {
return $this->fields;
} else {
return $this->fields->filter(function ($field){
/** @var Field $field */
/** @var Client $client */
$client = $field->getClient();
if(!empty($client) && !$client->isRemove()) {
/** @var Company $company */
$company = $client->getCompany();
if (!empty($company) && !$company->isRemove()) {
/** @var CompanyGroup $companyGroup */
$companyGroup = $company->getCompanyGroup();
if (!empty($companyGroup) && !$companyGroup->isRemove()) {
return $field;
}
}
}
return null;
});
}
}
public function addField(Field $field): self
{
if (!$this->fields->contains($field)) {
$this->fields[] = $field;
$field->addEmployee($this);
}
return $this;
}
public function removeField(Field $field): self
{
if ($this->fields->removeElement($field)) {
$field->removeEmployee($this);
}
return $this;
}
/**
* @return Collection<int, MonitoringObjective>
*/
public function getMonitoringObjectives(): Collection
{
return $this->monitoringObjectives;
}
public function addMonitoringObjective(MonitoringObjective $monitoringObjective): self
{
if (!$this->monitoringObjectives->contains($monitoringObjective)) {
$this->monitoringObjectives[] = $monitoringObjective;
$monitoringObjective->setViewer($this);
}
return $this;
}
public function removeMonitoringObjective(MonitoringObjective $monitoringObjective): self
{
if ($this->monitoringObjectives->removeElement($monitoringObjective)) {
// set the owning side to null (unless already changed)
if ($monitoringObjective->getViewer() === $this) {
$monitoringObjective->setViewer(null);
}
}
return $this;
}
public function getCompanyGroupId()
{
if(!empty($this->getCompany())
&& !empty($this->getCompany()->getCompanyGroup())
){
return $this->getCompany()->getCompanyGroup()->getId();
}
return null;
}
public function getCreator(): ?User
{
return $this->creator;
}
public function setCreator(?User $creator): self
{
$this->creator = $creator;
return $this;
}
}