src/Entity/Task/MonitoringObjective.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Task;
  3. use App\Constant\StatusConstant;
  4. use App\Entity\Agro\Operation;
  5. use App\Entity\Client;
  6. use App\Entity\Culture;
  7. use App\Entity\Employee;
  8. use App\Entity\Season;
  9. use App\Entity\Techcard\Techcard;
  10. use App\Entity\User;
  11. use App\Model\CacheRemover;
  12. use App\Repository\Task\MonitoringObjectiveRepository;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Doctrine\Common\Collections\Collection;
  15. use Doctrine\ORM\Mapping as ORM;
  16. /**
  17.  * @ORM\Entity(repositoryClass=MonitoringObjectiveRepository::class)
  18.  */
  19. class MonitoringObjective
  20. {
  21.     /**
  22.      * @ORM\Id
  23.      * @ORM\GeneratedValue
  24.      * @ORM\Column(type="integer")
  25.      */
  26.     private $id;
  27.     /**
  28.      * @ORM\ManyToOne(targetEntity=Client::class, inversedBy="monitoringObjectives")
  29.      */
  30.     private $client;
  31.     /**
  32.      * @ORM\ManyToOne(targetEntity=Culture::class, inversedBy="monitoringObjectives")
  33.      */
  34.     private $culture;
  35.     /**
  36.      * @ORM\ManyToOne(targetEntity=Techcard::class, inversedBy="monitoringObjectives")
  37.      */
  38.     private $techcard;
  39.     /**
  40.      * @ORM\ManyToOne(targetEntity=Operation::class, inversedBy="monitoringObjectives")
  41.      */
  42.     private $operation;
  43.     /**
  44.      * @ORM\Column(type="date")
  45.      */
  46.     private $dateStart;
  47.     /**
  48.      * @ORM\Column(type="datetime")
  49.      */
  50.     private $dateEnd;
  51.     /**
  52.      * @ORM\ManyToOne(targetEntity=Employee::class, inversedBy="monitoringObjectives")
  53.      */
  54.     private $viewer;
  55.     /**
  56.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="monitoringObjectives")
  57.      */
  58.     private $creator;
  59.     /**
  60.      * @ORM\OneToMany(targetEntity=Objective::class, mappedBy="monitoringObjective")
  61.      */
  62.     private $objectives;
  63.     /**
  64.      * @ORM\Column(type="string", length=255, nullable=true)
  65.      */
  66.     private $name;
  67.     /**
  68.      * @ORM\Column(type="boolean", nullable=true, options={"default" : false})
  69.      */
  70.     private $close;
  71.     /**
  72.      * @ORM\Column(type="datetime", nullable=true)
  73.      */
  74.     private $updateAt;
  75.     /**
  76.      * @ORM\Column(type="boolean", nullable=true)
  77.      */
  78.     private $complete;
  79.     /**
  80.      * @ORM\ManyToOne(targetEntity=Season::class, inversedBy="monitoringObjectives")
  81.      */
  82.     private $season;
  83.     /**
  84.      * @ORM\Column(type="string", length=255, nullable=true)
  85.      */
  86.     private $hash;
  87.     public function __construct()
  88.     {
  89.         if(empty($this->id)){
  90.             $this->name 'Новый мониторинг';
  91.             $this->close false;
  92.             $this->complete false;
  93.             $this->setHash();
  94.         }
  95.         $this->objectives = new ArrayCollection();
  96.         $this->updateAt = new \DateTime();
  97.     }
  98.     public function getId(): ?int
  99.     {
  100.         return $this->id;
  101.     }
  102.     public function getClient(): ?Client
  103.     {
  104.         return $this->client;
  105.     }
  106.     public function setClient(?Client $client): self
  107.     {
  108.         $this->client $client;
  109.         return $this;
  110.     }
  111.     public function getCulture(): ?Culture
  112.     {
  113.         return $this->culture;
  114.     }
  115.     public function setCulture(?Culture $culture): self
  116.     {
  117.         $this->culture $culture;
  118.         return $this;
  119.     }
  120.     public function getTechcard(): ?Techcard
  121.     {
  122.         return $this->techcard;
  123.     }
  124.     public function setTechcard(?Techcard $techcard): self
  125.     {
  126.         $this->techcard $techcard;
  127.         return $this;
  128.     }
  129.     public function getOperation(): ?Operation
  130.     {
  131.         return $this->operation;
  132.     }
  133.     public function setOperation(?Operation $operation): self
  134.     {
  135.         $this->operation $operation;
  136.         return $this;
  137.     }
  138.     public function getDateStart(): ?\DateTimeInterface
  139.     {
  140.         return $this->dateStart;
  141.     }
  142.     public function setDateStart(\DateTimeInterface $dateStart): self
  143.     {
  144.         $this->dateStart $dateStart;
  145.         return $this;
  146.     }
  147.     public function getDateEnd(): ?\DateTimeInterface
  148.     {
  149.         return $this->dateEnd;
  150.     }
  151.     public function setDateEnd(\DateTimeInterface $dateEnd): self
  152.     {
  153.         $this->dateEnd $dateEnd;
  154.         return $this;
  155.     }
  156.     public function getViewer(): ?Employee
  157.     {
  158.         return $this->viewer;
  159.     }
  160.     public function setViewer(?Employee $viewer): self
  161.     {
  162.         $this->viewer $viewer;
  163.         return $this;
  164.     }
  165.     public function getCreator(): ?User
  166.     {
  167.         return $this->creator;
  168.     }
  169.     public function setCreator(?User $creator): self
  170.     {
  171.         $this->creator $creator;
  172.         return $this;
  173.     }
  174.     /**
  175.      * @return Collection<int, Objective>
  176.      */
  177.     public function getObjectives(): Collection
  178.     {
  179.         return $this->objectives;
  180.     }
  181.     public function addObjective(Objective $objective): self
  182.     {
  183.         if (!$this->objectives->contains($objective)) {
  184.             $this->objectives[] = $objective;
  185.             $objective->setMonitoringObjective($this);
  186.         }
  187.         return $this;
  188.     }
  189.     public function removeObjective(Objective $objective): self
  190.     {
  191.         if ($this->objectives->removeElement($objective)) {
  192.             // set the owning side to null (unless already changed)
  193.             if ($objective->getMonitoringObjective() === $this) {
  194.                 $objective->setMonitoringObjective(null);
  195.             }
  196.         }
  197.         return $this;
  198.     }
  199.     public function getStatus(): string
  200.     {
  201.         $status StatusConstant::NEW;
  202.         if($this->getObjectives()->count() > 0){
  203.             $cancelCount 0;
  204.             foreach ($this->getObjectives() as $objective){
  205.                 if(in_array($objective->getStatus(),[StatusConstant::IN_PROGRESS,StatusConstant::COMPLETE])){
  206.                     $status StatusConstant::IN_PROGRESS;
  207.                     break;
  208.                 }
  209.                 if(in_array($objective->getStatus(),[StatusConstant::CANCEL])){
  210.                     $cancelCount++;
  211.                 }
  212.             }
  213.             if($this->getObjectives()->count() == $cancelCount){
  214.                 $status StatusConstant::CANCEL;
  215.             }
  216.         }
  217.         if($this->close){
  218.             $status StatusConstant::CANCEL;
  219.         }
  220.         $analitics $this->getObjectivesAnalytics();
  221.         if($this->complete || (isset($analitics[StatusConstant::COMPLETE]) && ($this->getObjectives()->count() == $analitics[StatusConstant::COMPLETE]))){
  222.             $status StatusConstant::COMPLETE;
  223.             $this->complete true;
  224.         }
  225.        return  $status;
  226.     }
  227.     public function getName(): ?string
  228.     {
  229.         if(!empty($this->name)) {
  230.             return $this->name;
  231.         } else {
  232.             if(!empty($this->getOperation())){
  233.                 return $this->getOperation()->getName();
  234.             }
  235.         }
  236.         return '';
  237.     }
  238.     public function setName(?string $name): self
  239.     {
  240.         $this->name $name;
  241.         return $this;
  242.     }
  243.     public function getObjectivesAnalytics(): ?array
  244.     {
  245.        $analitics null;
  246.        if($this->getObjectives()->count() > 0){
  247.            $analitics = [];
  248.            foreach ($this->getObjectives() as $objective) {
  249.                $status $objective->getStatus();
  250.                if($status == StatusConstant::NEW){
  251.                    $status StatusConstant::FREE;
  252.                } elseif($status == StatusConstant::CANCEL && $objective->getReports()->count() > 0){
  253.                    $status StatusConstant::COMPLETE;
  254.                }
  255.                if(isset($analitics[$status])){
  256.                    $analitics[$status] = $analitics[$status] + 1;
  257.                } else {
  258.                    $analitics[$status] = 1;
  259.                }
  260.            }
  261.        }
  262.        return $analitics;
  263.     }
  264.     public function isClose(): ?bool
  265.     {
  266.         return $this->close;
  267.     }
  268.     public function setClose(?bool $close): self
  269.     {
  270.         $this->close $close;
  271.         return $this;
  272.     }
  273.     public function getUpdateAt(): ?\DateTimeInterface
  274.     {
  275.         return $this->updateAt;
  276.     }
  277.     public function setUpdateAt(?\DateTimeInterface $updateAt): self
  278.     {
  279.         $this->updateAt $updateAt;
  280.         return $this;
  281.     }
  282.     public function isComplete(): ?bool
  283.     {
  284.         return $this->complete;
  285.     }
  286.     public function setComplete(bool $complete): self
  287.     {
  288.         $this->complete $complete;
  289.         return $this;
  290.     }
  291.     public function getSeason(): ?Season
  292.     {
  293.         return $this->season;
  294.     }
  295.     public function setSeason(?Season $season): self
  296.     {
  297.         $this->season $season;
  298.         return $this;
  299.     }
  300.     public function getHash(): ?string
  301.     {
  302.         return $this->hash;
  303.     }
  304.     public function setHash(): self
  305.     {
  306.         $this->hash md5(date('YmdHis').microtime(true).rand(0,999999));
  307.         return $this;
  308.     }
  309.     public function checkCompleteStatus()
  310.     {
  311.         $countAll $this->getObjectives()->count();
  312.         $cancel 0;
  313.         foreach ($this->getObjectives() as $objective) {
  314.             if(in_array($objective->getStatus(),[StatusConstant::CANCEL,StatusConstant::COMPLETE])){
  315.                 $cancel++;
  316.             }
  317.         }
  318.         if($cancel == $countAll && $cancel !== 0){
  319.             $this->setComplete(1);
  320.             $this->setHash();
  321.         }
  322.     }
  323. }