<?php
namespace App\Entity;
use App\Entity\Techcard\Techcard;
use App\Repository\CompanyRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=CompanyRepository::class)
*/
class Company
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $email;
/**
* @ORM\Column(type="string", length=255, options={"default": "ROLE_ADMIN"})
*/
private $role;
/**
* @ORM\OneToMany(targetEntity=Client::class, mappedBy="company")
*/
private $clients;
/**
* @ORM\OneToMany(targetEntity=User::class, mappedBy="company")
*/
private $user;
/**
* @ORM\OneToMany(targetEntity=Techcard::class, mappedBy="company")
*/
private $techcards;
/**
* @ORM\OneToMany(targetEntity=Employee::class, mappedBy="company")
*/
private $employees;
/**
* @ORM\ManyToOne(targetEntity=CompanyGroup::class, inversedBy="companies")
*/
private $companyGroup;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default": false})
*/
private $remove;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $cropWiseToken;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default": false})
*/
private $downloadCropWiseReport;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default": false})
*/
private $uploadCereraReport;
public function __construct()
{
if(is_null($this->id)){
$this->role = 'ROLE_ADMIN';
$this->remove = false;
}
$this->clients = new ArrayCollection();
$this->user = new ArrayCollection();
$this->techcards = new ArrayCollection();
$this->employees = 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 getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* @return mixed
*/
public function getRole()
{
return $this->role;
}
/**
* @param mixed $role
*/
public function setRole($role): void
{
$this->role = $role;
}
/**
* @return Collection|Client[]
*/
public function getClients($displayRemoved = false): Collection
{
if(!$displayRemoved) {
return $this->clients->filter(function ($item) {
if(!$item->isRemove()){
return $item;
}
return null;
});
} else {
return $this->clients;
}
}
public function addClient(Client $client): self
{
if (!$this->clients->contains($client)) {
$this->clients[] = $client;
$client->setCompany($this);
}
return $this;
}
public function removeClient(Client $client): self
{
if ($this->clients->removeElement($client)) {
// set the owning side to null (unless already changed)
if ($client->getCompany() === $this) {
$client->setCompany(null);
}
}
return $this;
}
public function removeAllClient(): self
{
$clients = $this->getClients();
if($clients){
foreach ($clients as $client){
$this->removeClient($client);
}
}
return $this;
}
public function removeAllEmployee()
{
$employees = $this->getAllEmployees();
if($employees->count()) {
foreach ($employees as $employee) {
$this->removeEmployee($employee);
}
}
}
/**
* @return Collection|User[]
*/
public function getUser(): Collection
{
return $this->user;
}
public function addUser(User $user): self
{
if (!$this->user->contains($user)) {
$this->user[] = $user;
$user->setCompany($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->user->removeElement($user)) {
// set the owning side to null (unless already changed)
if ($user->getCompany() === $this) {
$user->setCompany(null);
}
}
return $this;
}
public function removeAllUser()
{
$users = $this->getUser();
if($users) {
foreach ($users as $user) {
$this->removeUser($user);
}
}
}
/**
* @return mixed
*/
public function getDownloadCropWiseReport()
{
return $this->downloadCropWiseReport;
}
/**
* @param mixed $downloadCropWiseReport
*/
public function setDownloadCropWiseReport($downloadCropWiseReport): void
{
$this->downloadCropWiseReport = $downloadCropWiseReport;
}
/**
* @return mixed
*/
public function getUploadCereraReport()
{
return $this->uploadCereraReport;
}
/**
* @param mixed $uploadCereraReport
*/
public function setUploadCereraReport($uploadCereraReport): void
{
$this->uploadCereraReport = $uploadCereraReport;
}
public function getFields(): ArrayCollection
{
$fields = [];
$clients = $this->getClients()->toArray();
array_walk($clients,function ($item) use (&$fields){
$fields = array_merge($fields,$item->getFields()->toArray());
});
return new ArrayCollection($fields);
}
/**
* @return Collection|Techcard[]
*/
public function getTechcards(): Collection
{
return $this->techcards;
}
public function addTechcard(Techcard $techcard): self
{
if (!$this->techcards->contains($techcard)) {
$this->techcards[] = $techcard;
$techcard->setCompany($this);
}
return $this;
}
public function removeTechcard(Techcard $techcard): self
{
if ($this->techcards->removeElement($techcard)) {
// set the owning side to null (unless already changed)
if ($techcard->getCompany() === $this) {
$techcard->setCompany(null);
}
}
return $this;
}
public function removeAllTechcard()
{
$techcards = $this->getTechcards();
if($techcards) {
foreach ($techcards as $techcard) {
$this->removeTechcard($techcard);
}
}
}
public function getEmployees()
{
$response = new ArrayCollection();
if($this->getClients()->count()){
$this->getClients()->filter(function ($client) use (&$response){
if($client->getFields()->count()){
$client->getFields()->filter(function ($field) use (&$response){
/** @var Field $field */
if($field->getEmployees()->count()){
$field->getEmployees()->filter(function ($employee) use (&$response){
if(!$response->contains($employee)){
$response->add($employee);
}
});
}
});
}
});
}
return $response;
}
public function __toString()
{
return $this->name;
}
public function getAllEmployees()
{
return $this->employees;
}
public function addEmployee(Employee $employee): self
{
if (!$this->employees->contains($employee)) {
$this->employees[] = $employee;
$employee->setCompancompany($this);
}
return $this;
}
public function removeEmployee(Employee $employee): self
{
if ($this->employees->removeElement($employee)) {
// set the owning side to null (unless already changed)
if ($employee->getCompancompany() === $this) {
$employee->setCompancompany(null);
}
}
return $this;
}
public function getCompanyGroup(): ?CompanyGroup
{
return $this->companyGroup;
}
public function setCompanyGroup(?CompanyGroup $companyGroup): self
{
$this->companyGroup = $companyGroup;
return $this;
}
public function isRemove(): ?bool
{
return $this->remove;
}
public function setRemove(?bool $remove): self
{
$this->remove = $remove;
return $this;
}
public function getCropWiseToken(): ?string
{
return $this->cropWiseToken;
}
public function setCropWiseToken(?string $cropWiseToken): self
{
$this->cropWiseToken = $cropWiseToken;
return $this;
}
}