<?php
namespace App\Entity\Task;
use App\Constant\StatusConstant;
use App\Entity\Agro\Operation;
use App\Entity\Client;
use App\Entity\Culture;
use App\Entity\Employee;
use App\Entity\Season;
use App\Entity\Techcard\Techcard;
use App\Entity\User;
use App\Model\CacheRemover;
use App\Repository\Task\MonitoringObjectiveRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=MonitoringObjectiveRepository::class)
*/
class MonitoringObjective
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Client::class, inversedBy="monitoringObjectives")
*/
private $client;
/**
* @ORM\ManyToOne(targetEntity=Culture::class, inversedBy="monitoringObjectives")
*/
private $culture;
/**
* @ORM\ManyToOne(targetEntity=Techcard::class, inversedBy="monitoringObjectives")
*/
private $techcard;
/**
* @ORM\ManyToOne(targetEntity=Operation::class, inversedBy="monitoringObjectives")
*/
private $operation;
/**
* @ORM\Column(type="date")
*/
private $dateStart;
/**
* @ORM\Column(type="datetime")
*/
private $dateEnd;
/**
* @ORM\ManyToOne(targetEntity=Employee::class, inversedBy="monitoringObjectives")
*/
private $viewer;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="monitoringObjectives")
*/
private $creator;
/**
* @ORM\OneToMany(targetEntity=Objective::class, mappedBy="monitoringObjective")
*/
private $objectives;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $name;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default" : false})
*/
private $close;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $updateAt;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $complete;
/**
* @ORM\ManyToOne(targetEntity=Season::class, inversedBy="monitoringObjectives")
*/
private $season;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $hash;
public function __construct()
{
if(empty($this->id)){
$this->name = 'Новый мониторинг';
$this->close = false;
$this->complete = false;
$this->setHash();
}
$this->objectives = new ArrayCollection();
$this->updateAt = new \DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function getClient(): ?Client
{
return $this->client;
}
public function setClient(?Client $client): self
{
$this->client = $client;
return $this;
}
public function getCulture(): ?Culture
{
return $this->culture;
}
public function setCulture(?Culture $culture): self
{
$this->culture = $culture;
return $this;
}
public function getTechcard(): ?Techcard
{
return $this->techcard;
}
public function setTechcard(?Techcard $techcard): self
{
$this->techcard = $techcard;
return $this;
}
public function getOperation(): ?Operation
{
return $this->operation;
}
public function setOperation(?Operation $operation): self
{
$this->operation = $operation;
return $this;
}
public function getDateStart(): ?\DateTimeInterface
{
return $this->dateStart;
}
public function setDateStart(\DateTimeInterface $dateStart): self
{
$this->dateStart = $dateStart;
return $this;
}
public function getDateEnd(): ?\DateTimeInterface
{
return $this->dateEnd;
}
public function setDateEnd(\DateTimeInterface $dateEnd): self
{
$this->dateEnd = $dateEnd;
return $this;
}
public function getViewer(): ?Employee
{
return $this->viewer;
}
public function setViewer(?Employee $viewer): self
{
$this->viewer = $viewer;
return $this;
}
public function getCreator(): ?User
{
return $this->creator;
}
public function setCreator(?User $creator): self
{
$this->creator = $creator;
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->setMonitoringObjective($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->getMonitoringObjective() === $this) {
$objective->setMonitoringObjective(null);
}
}
return $this;
}
public function getStatus(): string
{
$status = StatusConstant::NEW;
if($this->getObjectives()->count() > 0){
$cancelCount = 0;
foreach ($this->getObjectives() as $objective){
if(in_array($objective->getStatus(),[StatusConstant::IN_PROGRESS,StatusConstant::COMPLETE])){
$status = StatusConstant::IN_PROGRESS;
break;
}
if(in_array($objective->getStatus(),[StatusConstant::CANCEL])){
$cancelCount++;
}
}
if($this->getObjectives()->count() == $cancelCount){
$status = StatusConstant::CANCEL;
}
}
if($this->close){
$status = StatusConstant::CANCEL;
}
$analitics = $this->getObjectivesAnalytics();
if($this->complete || (isset($analitics[StatusConstant::COMPLETE]) && ($this->getObjectives()->count() == $analitics[StatusConstant::COMPLETE]))){
$status = StatusConstant::COMPLETE;
$this->complete = true;
}
return $status;
}
public function getName(): ?string
{
if(!empty($this->name)) {
return $this->name;
} else {
if(!empty($this->getOperation())){
return $this->getOperation()->getName();
}
}
return '';
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getObjectivesAnalytics(): ?array
{
$analitics = null;
if($this->getObjectives()->count() > 0){
$analitics = [];
foreach ($this->getObjectives() as $objective) {
$status = $objective->getStatus();
if($status == StatusConstant::NEW){
$status = StatusConstant::FREE;
} elseif($status == StatusConstant::CANCEL && $objective->getReports()->count() > 0){
$status = StatusConstant::COMPLETE;
}
if(isset($analitics[$status])){
$analitics[$status] = $analitics[$status] + 1;
} else {
$analitics[$status] = 1;
}
}
}
return $analitics;
}
public function isClose(): ?bool
{
return $this->close;
}
public function setClose(?bool $close): self
{
$this->close = $close;
return $this;
}
public function getUpdateAt(): ?\DateTimeInterface
{
return $this->updateAt;
}
public function setUpdateAt(?\DateTimeInterface $updateAt): self
{
$this->updateAt = $updateAt;
return $this;
}
public function isComplete(): ?bool
{
return $this->complete;
}
public function setComplete(bool $complete): self
{
$this->complete = $complete;
return $this;
}
public function getSeason(): ?Season
{
return $this->season;
}
public function setSeason(?Season $season): self
{
$this->season = $season;
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 checkCompleteStatus()
{
$countAll = $this->getObjectives()->count();
$cancel = 0;
foreach ($this->getObjectives() as $objective) {
if(in_array($objective->getStatus(),[StatusConstant::CANCEL,StatusConstant::COMPLETE])){
$cancel++;
}
}
if($cancel == $countAll && $cancel !== 0){
$this->setComplete(1);
$this->setHash();
}
}
}