<?php
namespace App\Entity;
use App\Constant\ObjectTypeConstant;
use App\Constant\ReportConstant;
use App\Entity\Report\Report;
use App\Entity\Report\TaskList;
use App\Entity\Task\Objective;
use App\Entity\Techcard\Techcard;
use App\Model\CacheRemover;
use App\Repository\FieldRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use function Clue\StreamFilter\fun;
/**
* @ORM\Entity(repositoryClass=FieldRepository::class)
*/
class Field
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToOne(targetEntity=Season::class)
*/
private $season;
/**
* @ORM\ManyToOne(targetEntity=Culture::class)
*/
private $culture;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $sort;
/**
* @ORM\ManyToOne(targetEntity=Reproduct::class)
*/
private $reproduct;
/**
* @ORM\Column(type="json", nullable=true)
*/
private $params = [];
/**
* @ORM\Column(type="json")
*/
private $coordinates = [];
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $size;
/**
* @ORM\ManyToOne(targetEntity=Client::class, inversedBy="fields")
*/
private $client;
/**
* @ORM\ManyToOne(targetEntity=Techcard::class, inversedBy="fields")
*/
private $techcard;
/**
* @ORM\Column(type="string", length=255, nullable=true, options={"default":"#000000"})
*/
private $color;
/**
* @ORM\OneToMany(targetEntity=TaskList::class, mappedBy="field")
*/
private $taskLists;
/**
* @ORM\OneToMany(targetEntity=Report::class, mappedBy="field")
*/
private $reports;
/**
* @ORM\ManyToMany(targetEntity=Employee::class, inversedBy="fields")
*/
private $employees;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default":false})
*/
private $removed;
/**
* @ORM\ManyToMany(targetEntity=Techcard::class, inversedBy="fieldsAll")
*/
private $techcards;
/**
* @ORM\OneToMany(targetEntity=Objective::class, mappedBy="field")
*/
private $objectives;
/**
* @ORM\Column(type="string", length=255, nullable=true, options={"default":"field"})
*/
private $objectType;
/**
* @ORM\ManyToOne(targetEntity=ObjectGroup::class, inversedBy="fields")
*/
private $objectGroup;
/**
* @ORM\ManyToOne(targetEntity=Location::class, inversedBy="fields")
*/
private $location;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $objectStatus;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $hash;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default":true})
*/
private $defaultCultureColor;
/**
* @ORM\OneToMany(targetEntity=FieldParams::class, mappedBy="field")
*/
private $fieldParams;
/**
* @ORM\OneToMany(targetEntity=Recommendation::class, mappedBy="field")
*/
private $recommendations;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $cropWiseFieldId;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default":false})
*/
private $archive;
public function __construct()
{
if ($this->id == null) {
$this->removed = false;
$this->color = '#000000';
$this->objectType = ObjectTypeConstant::OBJECT_TYPE_FIELD;
$this->defaultCultureColor = 1;
$this->archive = 0;
}
$this->taskLists = new ArrayCollection();
$this->employees = new ArrayCollection();
$this->reports = new ArrayCollection();
$this->techcards = new ArrayCollection();
$this->objectives = new ArrayCollection();
$this->fieldParams = new ArrayCollection();
$this->setHash();
$this->recommendations = 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 getSeason(): ?Season
{
return $this->season;
}
public function setSeason(?Season $season): self
{
$this->season = $season;
return $this;
}
public function getCulture(): ?Culture
{
return $this->culture;
}
public function setCulture(?Culture $culture): self
{
$this->culture = $culture;
return $this;
}
public function getSort(): ?string
{
return $this->sort;
}
public function setSort(?string $sort): self
{
$this->sort = $sort;
return $this;
}
public function getReproduct(): ?Reproduct
{
return $this->reproduct;
}
public function setReproduct(?Reproduct $reproduct): self
{
$this->reproduct = $reproduct;
return $this;
}
public function getParams(): ?array
{
return $this->params;
}
public function setParams(?array $params): self
{
$this->params = $params;
return $this;
}
public function getCoordinates(): ?array
{
$coordinate = [];
if (!empty($this->coordinates)) {
foreach ($this->coordinates as $item) {
if (isset($item['lat'])) {
$coordinate[] = [
'lat' => (float)$item['lat'],
'lng' => (float)$item['lng'],
];
}
}
}
return $coordinate;
}
public function setCoordinates($coordinates): self
{
if (is_string($coordinates)) {
$this->coordinates = json_decode($coordinates, 1);
} else {
$this->coordinates = $coordinates;
}
return $this;
}
public function getSizeNumber(): ?int
{
return (float)$this->size;
}
public function getSize(): ?string
{
return number_format((float)$this->size, 1, '.', ' ');
}
public function setSize(?string $size): self
{
$this->size = $size;
return $this;
}
public function getClient(): ?Client
{
return $this->client;
}
public function setClient(?Client $client): self
{
$this->client = $client;
return $this;
}
public function getTechcard(): ?Techcard
{
return $this->techcard;
}
public function setTechcard(?Techcard $techcard): self
{
$this->techcard = $techcard;
return $this;
}
public function __toString()
{
return $this->name;
}
public function getColor(): ?string
{
if ($this->isDefaultCultureColor()) {
if (!empty($this->getCulture())) {
$color = $this->getCulture()->getColor();
if (!is_null($color)) {
return $color;
} else {
return '#000000';
}
}
}
return $this->color;
}
public function setColor(?string $color): self
{
$this->color = $color;
return $this;
}
public function getOperations()
{
$result = [];
$techcard = $this->getTechcard();
if (!empty($techcard)) {
$operations = $techcard->getOperations();
if (!empty($operations)) {
$result['techcard']['name'] = $techcard->getName();
$result['techcard']['id'] = $techcard->getId();
$result['techcard']['list'] = [];
foreach ($operations as $operation) {
$result['techcard']['list'][] = [
'name' => $operation->getName(),
'id' => $operation->getId()
];
}
}
}
return $result;
}
public function getEnableOperations()
{
$result = [];
$techcard = $this->getTechcard();
if (!empty($techcard)) {
$operations = $techcard->getOperations();
if (!empty($operations)) {
$result['techcard']['name'] = $techcard->getName();
$result['techcard']['id'] = $techcard->getId();
$result['techcard']['list'] = [];
foreach ($operations as $operation) {
if (!$operation->isRemoved()) {
$result['techcard']['list'][] = [
'name' => $operation->getName(),
'id' => $operation->getId()
];
}
}
}
}
return $result;
}
public function getTodayWorks()
{
$result = [
ReportConstant::AGRO_OPERATION_TYPE => 0,
ReportConstant::MONITORING_TYPE => 0
];
foreach ($this->getTaskLists() as $taskList) {
if (!empty($taskList->getSeason()) && $taskList->getSeason()->getName() == date("Y")) {
foreach ($taskList->getTasks() as $task) {
$dateFrom = $task->getDateFrom();
$dateTo = $task->getDateTo();
$dateNow = new \DateTime(date("Y-m-d"));
if (
$dateNow == $dateFrom ||
$dateNow == $dateTo ||
($dateFrom < $dateNow && $dateNow <= $dateTo)
) {
$key = ReportConstant::AGRO_OPERATION_TYPE;
if (!empty($task->getMonitoring())) {
$key = ReportConstant::MONITORING_TYPE;
}
if (isset($result[$key])) {
$result[$key]++;
} else {
$result[$key] = 1;
}
}
}
}
}
return $result;
}
public function getMonitorings()
{
$result = [];
$techcard = $this->getTechcard();
if (!empty($techcard)) {
$monitorings = $techcard->getMonitorings();
if (!empty($monitorings)) {
$result['techcard']['name'] = $techcard->getName();
$result['techcard']['id'] = $techcard->getId();
$result['techcard']['list'] = [];
foreach ($monitorings as $monitoring) {
$result['techcard']['list'][] = [
'name' => $monitoring->getName(),
'id' => $monitoring->getId()
];
}
}
}
return $result;
}
public function getEnableMonitorings()
{
$result = [];
$techcard = $this->getTechcard();
if (!empty($techcard)) {
$monitorings = $techcard->getMonitorings();
if (!empty($monitorings)) {
$result['techcard']['name'] = $techcard->getName();
$result['techcard']['id'] = $techcard->getId();
$result['techcard']['list'] = [];
foreach ($monitorings as $monitoring) {
if (!$monitoring->isRemoved()) {
$result['techcard']['list'][] = [
'name' => $monitoring->getName(),
'id' => $monitoring->getId()
];
}
}
}
}
return $result;
}
/**
* @return Collection|TaskList[]
*/
public function getTaskLists(): Collection
{
return $this->taskLists;
}
public function addTaskList(TaskList $taskList): self
{
if (!$this->taskLists->contains($taskList)) {
$this->taskLists[] = $taskList;
$taskList->setField($this);
}
return $this;
}
public function removeTaskList(TaskList $taskList): self
{
if ($this->taskLists->removeElement($taskList)) {
// set the owning side to null (unless already changed)
if ($taskList->getField() === $this) {
$taskList->setField(null);
}
}
return $this;
}
public function removeAllTaskList(): self
{
foreach ($this->getTaskLists() as $taskList) {
$this->removeTaskList($taskList);
}
return $this;
}
public function getTaskListBySeason(Season $season)
{
foreach ($this->taskLists as $taskList) {
if ($taskList->getSeason()->getId() == $season->getId()) {
return $taskList;
}
}
return null;
}
/**
* @return Collection|Employee[]
*/
public function getEmployees(): Collection
{
return $this->employees;
}
public function addEmployee(Employee $employee): self
{
if (!$this->employees->contains($employee)) {
$this->employees[] = $employee;
}
return $this;
}
public function removeEmployee(Employee $employee): self
{
$this->employees->removeElement($employee);
return $this;
}
/**
* @return Collection|Report[]
*/
public function getReports(): Collection
{
return $this->reports;
}
/**
* @return Collection|Report[]
*/
public function getObjectiveReports(): Collection
{
return $this->reports->filter(function ($field) {
if (!empty($field->getObjective())) {
return $field;
}
return null;
});
}
public function addReport(Report $report): self
{
if (!$this->reports->contains($report)) {
$this->reports[] = $report;
$report->setField($this);
}
return $this;
}
public function removeReport(Report $report): self
{
if ($this->reports->removeElement($report)) {
// set the owning side to null (unless already changed)
if ($report->getField() === $this) {
$report->setField(null);
}
}
return $this;
}
public function isRemoved(): ?bool
{
return $this->removed;
}
public function setRemoved(?bool $removed): self
{
$this->removed = $removed;
return $this;
}
/**
* @return Collection|Techcard[]
*/
public function getTechcards(): Collection
{
return $this->techcards;
}
public function addTechcard(Techcard $techcard): self
{
if (!$this->techcards->contains($techcard)) {
/** @var Techcard $techcardInList */
foreach ($this->techcards as $techcardInList) {
if (!empty($techcard->getSeason()) && !empty($techcardInList->getSeason()) && $techcard->getSeason()->getId() == $techcardInList->getSeason()->getId() && $techcard->getObjectType() == ObjectTypeConstant::OBJECT_TYPE_FIELD) {
$techcardInList->removeField($this);
$this->removeTechcard($techcardInList);
}
}
$this->techcards[] = $techcard;
$this->techcard = $techcard;
$this->season = $techcard->getSeason();
$this->culture = $techcard->getCulture();
$techcard->addField($this);
}
return $this;
}
public function removeTechcard(Techcard $techcard): self
{
$this->techcards->removeElement($techcard);
return $this;
}
/**
* @return Collection<int, Objective>
*/
public function getObjectives(): Collection
{
return $this->objectives;
}
public function addObjective(Objective $objective): self
{
if (!$this->objectives->contains($objective)) {
$this->objectives[] = $objective;
$objective->setField($this);
}
return $this;
}
public function removeObjective(Objective $objective): self
{
if ($this->objectives->removeElement($objective)) {
// set the owning side to null (unless already changed)
if ($objective->getField() === $this) {
$objective->setField(null);
}
}
return $this;
}
public function getObjectType(): ?string
{
return $this->objectType;
}
public function setObjectType(?string $objectType): self
{
$this->objectType = $objectType;
return $this;
}
public function getObjectGroup(): ?ObjectGroup
{
return $this->objectGroup;
}
public function setObjectGroup(?ObjectGroup $objectGroup): self
{
$this->objectGroup = $objectGroup;
return $this;
}
public function getLocation(): ?Location
{
return $this->location;
}
public function setLocation(?Location $location): self
{
$this->location = $location;
return $this;
}
public function getObjectStatus(): ?string
{
return $this->objectStatus;
}
public function setObjectStatus(string $objectStatus): self
{
$this->objectStatus = $objectStatus;
return $this;
}
public function getHash(): ?string
{
return $this->hash;
}
public function setHash(): self
{
$this->hash = md5(date('YmdHis') . microtime(true) . rand(0, 999999));
return $this;
}
public function removeFieldForAllEmployee()
{
$employees = $this->getEmployees();
if ($employees->count() > 0) {
$cacheRemover = new CacheRemover();
foreach ($employees as $employee) {
$this->removeEmployee($employee);
$cacheRemover->removeCache($employee);
}
}
return $this;
}
public function getCompanyGroupId()
{
if (!empty($this->getClient())
&& !empty($this->getClient()->getCompany())
&& !empty($this->getClient()->getCompany()->getCompanyGroup())
) {
return $this->getClient()->getCompany()->getCompanyGroup()->getId();
}
return null;
}
/**
* @return mixed
*/
public function isDefaultCultureColor()
{
return $this->defaultCultureColor;
}
/**
* @param mixed $defaultCultureColor
*/
public function setDefaultCultureColor(bool $defaultCultureColor): void
{
$this->defaultCultureColor = $defaultCultureColor;
}
/**
* @return Collection<int, FieldParams>
*/
public function getFieldParams(): Collection
{
return $this->fieldParams;
}
public function getFieldParam(string $key): FieldParams
{
$params = $this->fieldParams;
/** @var FieldParams $item */
foreach ($params as $item) {
if ($item->getParamKey() == $key) {
return $item;
}
}
$newParam = new FieldParams();
$newParam->setField($this);
$newParam->setParamKey($key);
$this->addFieldParam($newParam);
return $newParam;
}
public function addFieldParam(FieldParams $fieldParam): self
{
if (!$this->fieldParams->contains($fieldParam)) {
$this->fieldParams[] = $fieldParam;
$fieldParam->setField($this);
}
return $this;
}
public function removeFieldParam(FieldParams $fieldParam): self
{
if ($this->fieldParams->removeElement($fieldParam)) {
// set the owning side to null (unless already changed)
if ($fieldParam->getField() === $this) {
$fieldParam->setField(null);
}
}
return $this;
}
public function getFieldParamsToArray(): array
{
$response = [];
$params = $this->getFieldParams();
foreach ($params as $param) {
$response[$param->getParamKey()] = $param->getValue();
}
return $response;
}
public function checkCultureExists(int $cultureId): bool
{
$status = false;
if (!empty($this->getCulture()) && $this->getCulture()->getId() == $cultureId) {
return true;
}
foreach ($this->getTechcards() as $techcard) {
if (!empty($techcard->getCulture()) && $techcard->getCulture()->getId() == $cultureId) {
return true;
}
}
return $status;
}
/**
* @return Collection<int, Recommendation>
*/
public function getRecommendations(): Collection
{
return $this->recommendations;
}
public function addRecommendation(Recommendation $recommendation): self
{
if (!$this->recommendations->contains($recommendation)) {
$this->recommendations[] = $recommendation;
$recommendation->setField($this);
}
return $this;
}
public function removeRecommendation(Recommendation $recommendation): self
{
if ($this->recommendations->removeElement($recommendation)) {
// set the owning side to null (unless already changed)
if ($recommendation->getField() === $this) {
$recommendation->setField(null);
}
}
return $this;
}
public function getCropWiseFieldId(): ?int
{
return $this->cropWiseFieldId;
}
public function setCropWiseFieldId(?int $cropWiseFieldId): self
{
$this->cropWiseFieldId = $cropWiseFieldId;
return $this;
}
public function isArchive(): ?bool
{
return $this->archive;
}
public function setArchive(?bool $archive): self
{
$this->archive = $archive;
return $this;
}
}