src/Entity/Field.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Constant\ObjectTypeConstant;
  4. use App\Constant\ReportConstant;
  5. use App\Entity\Report\Report;
  6. use App\Entity\Report\TaskList;
  7. use App\Entity\Task\Objective;
  8. use App\Entity\Techcard\Techcard;
  9. use App\Model\CacheRemover;
  10. use App\Repository\FieldRepository;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use function Clue\StreamFilter\fun;
  15. /**
  16.  * @ORM\Entity(repositoryClass=FieldRepository::class)
  17.  */
  18. class Field
  19. {
  20.     /**
  21.      * @ORM\Id
  22.      * @ORM\GeneratedValue
  23.      * @ORM\Column(type="integer")
  24.      */
  25.     private $id;
  26.     /**
  27.      * @ORM\Column(type="string", length=255)
  28.      */
  29.     private $name;
  30.     /**
  31.      * @ORM\ManyToOne(targetEntity=Season::class)
  32.      */
  33.     private $season;
  34.     /**
  35.      * @ORM\ManyToOne(targetEntity=Culture::class)
  36.      */
  37.     private $culture;
  38.     /**
  39.      * @ORM\Column(type="string", length=255, nullable=true)
  40.      */
  41.     private $sort;
  42.     /**
  43.      * @ORM\ManyToOne(targetEntity=Reproduct::class)
  44.      */
  45.     private $reproduct;
  46.     /**
  47.      * @ORM\Column(type="json", nullable=true)
  48.      */
  49.     private $params = [];
  50.     /**
  51.      * @ORM\Column(type="json")
  52.      */
  53.     private $coordinates = [];
  54.     /**
  55.      * @ORM\Column(type="string", length=255, nullable=true)
  56.      */
  57.     private $size;
  58.     /**
  59.      * @ORM\ManyToOne(targetEntity=Client::class, inversedBy="fields")
  60.      */
  61.     private $client;
  62.     /**
  63.      * @ORM\ManyToOne(targetEntity=Techcard::class, inversedBy="fields")
  64.      */
  65.     private $techcard;
  66.     /**
  67.      * @ORM\Column(type="string", length=255, nullable=true, options={"default":"#000000"})
  68.      */
  69.     private $color;
  70.     /**
  71.      * @ORM\OneToMany(targetEntity=TaskList::class, mappedBy="field")
  72.      */
  73.     private $taskLists;
  74.     /**
  75.      * @ORM\OneToMany(targetEntity=Report::class, mappedBy="field")
  76.      */
  77.     private $reports;
  78.     /**
  79.      * @ORM\ManyToMany(targetEntity=Employee::class, inversedBy="fields")
  80.      */
  81.     private $employees;
  82.     /**
  83.      * @ORM\Column(type="boolean", nullable=true, options={"default":false})
  84.      */
  85.     private $removed;
  86.     /**
  87.      * @ORM\ManyToMany(targetEntity=Techcard::class, inversedBy="fieldsAll")
  88.      */
  89.     private $techcards;
  90.     /**
  91.      * @ORM\OneToMany(targetEntity=Objective::class, mappedBy="field")
  92.      */
  93.     private $objectives;
  94.     /**
  95.      * @ORM\Column(type="string", length=255, nullable=true, options={"default":"field"})
  96.      */
  97.     private $objectType;
  98.     /**
  99.      * @ORM\ManyToOne(targetEntity=ObjectGroup::class, inversedBy="fields")
  100.      */
  101.     private $objectGroup;
  102.     /**
  103.      * @ORM\ManyToOne(targetEntity=Location::class, inversedBy="fields")
  104.      */
  105.     private $location;
  106.     /**
  107.      * @ORM\Column(type="string", length=255, nullable=true)
  108.      */
  109.     private $objectStatus;
  110.     /**
  111.      * @ORM\Column(type="string", length=255, nullable=true)
  112.      */
  113.     private $hash;
  114.     /**
  115.      * @ORM\Column(type="boolean", nullable=true, options={"default":true})
  116.      */
  117.     private $defaultCultureColor;
  118.     /**
  119.      * @ORM\OneToMany(targetEntity=FieldParams::class, mappedBy="field")
  120.      */
  121.     private $fieldParams;
  122.     /**
  123.      * @ORM\OneToMany(targetEntity=Recommendation::class, mappedBy="field")
  124.      */
  125.     private $recommendations;
  126.     /**
  127.      * @ORM\Column(type="integer", nullable=true)
  128.      */
  129.     private $cropWiseFieldId;
  130.     /**
  131.      * @ORM\Column(type="boolean", nullable=true, options={"default":false})
  132.      */
  133.     private $archive;
  134.     public function __construct()
  135.     {
  136.         if ($this->id == null) {
  137.             $this->removed false;
  138.             $this->color '#000000';
  139.             $this->objectType ObjectTypeConstant::OBJECT_TYPE_FIELD;
  140.             $this->defaultCultureColor 1;
  141.             $this->archive 0;
  142.         }
  143.         $this->taskLists = new ArrayCollection();
  144.         $this->employees = new ArrayCollection();
  145.         $this->reports = new ArrayCollection();
  146.         $this->techcards = new ArrayCollection();
  147.         $this->objectives = new ArrayCollection();
  148.         $this->fieldParams = new ArrayCollection();
  149.         $this->setHash();
  150.         $this->recommendations = new ArrayCollection();
  151.     }
  152.     public function getId(): ?int
  153.     {
  154.         return $this->id;
  155.     }
  156.     public function getName(): ?string
  157.     {
  158.         return $this->name;
  159.     }
  160.     public function setName(string $name): self
  161.     {
  162.         $this->name $name;
  163.         return $this;
  164.     }
  165.     public function getSeason(): ?Season
  166.     {
  167.         return $this->season;
  168.     }
  169.     public function setSeason(?Season $season): self
  170.     {
  171.         $this->season $season;
  172.         return $this;
  173.     }
  174.     public function getCulture(): ?Culture
  175.     {
  176.         return $this->culture;
  177.     }
  178.     public function setCulture(?Culture $culture): self
  179.     {
  180.         $this->culture $culture;
  181.         return $this;
  182.     }
  183.     public function getSort(): ?string
  184.     {
  185.         return $this->sort;
  186.     }
  187.     public function setSort(?string $sort): self
  188.     {
  189.         $this->sort $sort;
  190.         return $this;
  191.     }
  192.     public function getReproduct(): ?Reproduct
  193.     {
  194.         return $this->reproduct;
  195.     }
  196.     public function setReproduct(?Reproduct $reproduct): self
  197.     {
  198.         $this->reproduct $reproduct;
  199.         return $this;
  200.     }
  201.     public function getParams(): ?array
  202.     {
  203.         return $this->params;
  204.     }
  205.     public function setParams(?array $params): self
  206.     {
  207.         $this->params $params;
  208.         return $this;
  209.     }
  210.     public function getCoordinates(): ?array
  211.     {
  212.         $coordinate = [];
  213.         if (!empty($this->coordinates)) {
  214.             foreach ($this->coordinates as $item) {
  215.                 if (isset($item['lat'])) {
  216.                     $coordinate[] = [
  217.                         'lat' => (float)$item['lat'],
  218.                         'lng' => (float)$item['lng'],
  219.                     ];
  220.                 }
  221.             }
  222.         }
  223.         return $coordinate;
  224.     }
  225.     public function setCoordinates($coordinates): self
  226.     {
  227.         if (is_string($coordinates)) {
  228.             $this->coordinates json_decode($coordinates1);
  229.         } else {
  230.             $this->coordinates $coordinates;
  231.         }
  232.         return $this;
  233.     }
  234.     public function getSizeNumber(): ?int
  235.     {
  236.         return (float)$this->size;
  237.     }
  238.     public function getSize(): ?string
  239.     {
  240.         return number_format((float)$this->size1'.'' ');
  241.     }
  242.     public function setSize(?string $size): self
  243.     {
  244.         $this->size $size;
  245.         return $this;
  246.     }
  247.     public function getClient(): ?Client
  248.     {
  249.         return $this->client;
  250.     }
  251.     public function setClient(?Client $client): self
  252.     {
  253.         $this->client $client;
  254.         return $this;
  255.     }
  256.     public function getTechcard(): ?Techcard
  257.     {
  258.         return $this->techcard;
  259.     }
  260.     public function setTechcard(?Techcard $techcard): self
  261.     {
  262.         $this->techcard $techcard;
  263.         return $this;
  264.     }
  265.     public function __toString()
  266.     {
  267.         return $this->name;
  268.     }
  269.     public function getColor(): ?string
  270.     {
  271.         if ($this->isDefaultCultureColor()) {
  272.             if (!empty($this->getCulture())) {
  273.                 $color $this->getCulture()->getColor();
  274.                 if (!is_null($color)) {
  275.                     return $color;
  276.                 } else {
  277.                    return '#000000';
  278.                 }
  279.             }
  280.         }
  281.         return $this->color;
  282.     }
  283.     public function setColor(?string $color): self
  284.     {
  285.         $this->color $color;
  286.         return $this;
  287.     }
  288.     public function getOperations()
  289.     {
  290.         $result = [];
  291.         $techcard $this->getTechcard();
  292.         if (!empty($techcard)) {
  293.             $operations $techcard->getOperations();
  294.             if (!empty($operations)) {
  295.                 $result['techcard']['name'] = $techcard->getName();
  296.                 $result['techcard']['id'] = $techcard->getId();
  297.                 $result['techcard']['list'] = [];
  298.                 foreach ($operations as $operation) {
  299.                     $result['techcard']['list'][] = [
  300.                         'name' => $operation->getName(),
  301.                         'id' => $operation->getId()
  302.                     ];
  303.                 }
  304.             }
  305.         }
  306.         return $result;
  307.     }
  308.     public function getEnableOperations()
  309.     {
  310.         $result = [];
  311.         $techcard $this->getTechcard();
  312.         if (!empty($techcard)) {
  313.             $operations $techcard->getOperations();
  314.             if (!empty($operations)) {
  315.                 $result['techcard']['name'] = $techcard->getName();
  316.                 $result['techcard']['id'] = $techcard->getId();
  317.                 $result['techcard']['list'] = [];
  318.                 foreach ($operations as $operation) {
  319.                     if (!$operation->isRemoved()) {
  320.                         $result['techcard']['list'][] = [
  321.                             'name' => $operation->getName(),
  322.                             'id' => $operation->getId()
  323.                         ];
  324.                     }
  325.                 }
  326.             }
  327.         }
  328.         return $result;
  329.     }
  330.     public function getTodayWorks()
  331.     {
  332.         $result = [
  333.             ReportConstant::AGRO_OPERATION_TYPE => 0,
  334.             ReportConstant::MONITORING_TYPE => 0
  335.         ];
  336.         foreach ($this->getTaskLists() as $taskList) {
  337.             if (!empty($taskList->getSeason()) && $taskList->getSeason()->getName() == date("Y")) {
  338.                 foreach ($taskList->getTasks() as $task) {
  339.                     $dateFrom $task->getDateFrom();
  340.                     $dateTo $task->getDateTo();
  341.                     $dateNow = new \DateTime(date("Y-m-d"));
  342.                     if (
  343.                         $dateNow == $dateFrom ||
  344.                         $dateNow == $dateTo ||
  345.                         ($dateFrom $dateNow && $dateNow <= $dateTo)
  346.                     ) {
  347.                         $key ReportConstant::AGRO_OPERATION_TYPE;
  348.                         if (!empty($task->getMonitoring())) {
  349.                             $key ReportConstant::MONITORING_TYPE;
  350.                         }
  351.                         if (isset($result[$key])) {
  352.                             $result[$key]++;
  353.                         } else {
  354.                             $result[$key] = 1;
  355.                         }
  356.                     }
  357.                 }
  358.             }
  359.         }
  360.         return $result;
  361.     }
  362.     public function getMonitorings()
  363.     {
  364.         $result = [];
  365.         $techcard $this->getTechcard();
  366.         if (!empty($techcard)) {
  367.             $monitorings $techcard->getMonitorings();
  368.             if (!empty($monitorings)) {
  369.                 $result['techcard']['name'] = $techcard->getName();
  370.                 $result['techcard']['id'] = $techcard->getId();
  371.                 $result['techcard']['list'] = [];
  372.                 foreach ($monitorings as $monitoring) {
  373.                     $result['techcard']['list'][] = [
  374.                         'name' => $monitoring->getName(),
  375.                         'id' => $monitoring->getId()
  376.                     ];
  377.                 }
  378.             }
  379.         }
  380.         return $result;
  381.     }
  382.     public function getEnableMonitorings()
  383.     {
  384.         $result = [];
  385.         $techcard $this->getTechcard();
  386.         if (!empty($techcard)) {
  387.             $monitorings $techcard->getMonitorings();
  388.             if (!empty($monitorings)) {
  389.                 $result['techcard']['name'] = $techcard->getName();
  390.                 $result['techcard']['id'] = $techcard->getId();
  391.                 $result['techcard']['list'] = [];
  392.                 foreach ($monitorings as $monitoring) {
  393.                     if (!$monitoring->isRemoved()) {
  394.                         $result['techcard']['list'][] = [
  395.                             'name' => $monitoring->getName(),
  396.                             'id' => $monitoring->getId()
  397.                         ];
  398.                     }
  399.                 }
  400.             }
  401.         }
  402.         return $result;
  403.     }
  404.     /**
  405.      * @return Collection|TaskList[]
  406.      */
  407.     public function getTaskLists(): Collection
  408.     {
  409.         return $this->taskLists;
  410.     }
  411.     public function addTaskList(TaskList $taskList): self
  412.     {
  413.         if (!$this->taskLists->contains($taskList)) {
  414.             $this->taskLists[] = $taskList;
  415.             $taskList->setField($this);
  416.         }
  417.         return $this;
  418.     }
  419.     public function removeTaskList(TaskList $taskList): self
  420.     {
  421.         if ($this->taskLists->removeElement($taskList)) {
  422.             // set the owning side to null (unless already changed)
  423.             if ($taskList->getField() === $this) {
  424.                 $taskList->setField(null);
  425.             }
  426.         }
  427.         return $this;
  428.     }
  429.     public function removeAllTaskList(): self
  430.     {
  431.         foreach ($this->getTaskLists() as $taskList) {
  432.             $this->removeTaskList($taskList);
  433.         }
  434.         return $this;
  435.     }
  436.     public function getTaskListBySeason(Season $season)
  437.     {
  438.         foreach ($this->taskLists as $taskList) {
  439.             if ($taskList->getSeason()->getId() == $season->getId()) {
  440.                 return $taskList;
  441.             }
  442.         }
  443.         return null;
  444.     }
  445.     /**
  446.      * @return Collection|Employee[]
  447.      */
  448.     public function getEmployees(): Collection
  449.     {
  450.         return $this->employees;
  451.     }
  452.     public function addEmployee(Employee $employee): self
  453.     {
  454.         if (!$this->employees->contains($employee)) {
  455.             $this->employees[] = $employee;
  456.         }
  457.         return $this;
  458.     }
  459.     public function removeEmployee(Employee $employee): self
  460.     {
  461.         $this->employees->removeElement($employee);
  462.         return $this;
  463.     }
  464.     /**
  465.      * @return Collection|Report[]
  466.      */
  467.     public function getReports(): Collection
  468.     {
  469.         return $this->reports;
  470.     }
  471.     /**
  472.      * @return Collection|Report[]
  473.      */
  474.     public function getObjectiveReports(): Collection
  475.     {
  476.         return $this->reports->filter(function ($field) {
  477.             if (!empty($field->getObjective())) {
  478.                 return $field;
  479.             }
  480.             return null;
  481.         });
  482.     }
  483.     public function addReport(Report $report): self
  484.     {
  485.         if (!$this->reports->contains($report)) {
  486.             $this->reports[] = $report;
  487.             $report->setField($this);
  488.         }
  489.         return $this;
  490.     }
  491.     public function removeReport(Report $report): self
  492.     {
  493.         if ($this->reports->removeElement($report)) {
  494.             // set the owning side to null (unless already changed)
  495.             if ($report->getField() === $this) {
  496.                 $report->setField(null);
  497.             }
  498.         }
  499.         return $this;
  500.     }
  501.     public function isRemoved(): ?bool
  502.     {
  503.         return $this->removed;
  504.     }
  505.     public function setRemoved(?bool $removed): self
  506.     {
  507.         $this->removed $removed;
  508.         return $this;
  509.     }
  510.     /**
  511.      * @return Collection|Techcard[]
  512.      */
  513.     public function getTechcards(): Collection
  514.     {
  515.         return $this->techcards;
  516.     }
  517.     public function addTechcard(Techcard $techcard): self
  518.     {
  519.         if (!$this->techcards->contains($techcard)) {
  520.             /** @var Techcard $techcardInList */
  521.             foreach ($this->techcards as $techcardInList) {
  522.                 if (!empty($techcard->getSeason()) && !empty($techcardInList->getSeason()) && $techcard->getSeason()->getId() == $techcardInList->getSeason()->getId() && $techcard->getObjectType() == ObjectTypeConstant::OBJECT_TYPE_FIELD) {
  523.                     $techcardInList->removeField($this);
  524.                     $this->removeTechcard($techcardInList);
  525.                 }
  526.             }
  527.             $this->techcards[] = $techcard;
  528.             $this->techcard $techcard;
  529.             $this->season $techcard->getSeason();
  530.             $this->culture $techcard->getCulture();
  531.             $techcard->addField($this);
  532.         }
  533.         return $this;
  534.     }
  535.     public function removeTechcard(Techcard $techcard): self
  536.     {
  537.         $this->techcards->removeElement($techcard);
  538.         return $this;
  539.     }
  540.     /**
  541.      * @return Collection<int, Objective>
  542.      */
  543.     public function getObjectives(): Collection
  544.     {
  545.         return $this->objectives;
  546.     }
  547.     public function addObjective(Objective $objective): self
  548.     {
  549.         if (!$this->objectives->contains($objective)) {
  550.             $this->objectives[] = $objective;
  551.             $objective->setField($this);
  552.         }
  553.         return $this;
  554.     }
  555.     public function removeObjective(Objective $objective): self
  556.     {
  557.         if ($this->objectives->removeElement($objective)) {
  558.             // set the owning side to null (unless already changed)
  559.             if ($objective->getField() === $this) {
  560.                 $objective->setField(null);
  561.             }
  562.         }
  563.         return $this;
  564.     }
  565.     public function getObjectType(): ?string
  566.     {
  567.         return $this->objectType;
  568.     }
  569.     public function setObjectType(?string $objectType): self
  570.     {
  571.         $this->objectType $objectType;
  572.         return $this;
  573.     }
  574.     public function getObjectGroup(): ?ObjectGroup
  575.     {
  576.         return $this->objectGroup;
  577.     }
  578.     public function setObjectGroup(?ObjectGroup $objectGroup): self
  579.     {
  580.         $this->objectGroup $objectGroup;
  581.         return $this;
  582.     }
  583.     public function getLocation(): ?Location
  584.     {
  585.         return $this->location;
  586.     }
  587.     public function setLocation(?Location $location): self
  588.     {
  589.         $this->location $location;
  590.         return $this;
  591.     }
  592.     public function getObjectStatus(): ?string
  593.     {
  594.         return $this->objectStatus;
  595.     }
  596.     public function setObjectStatus(string $objectStatus): self
  597.     {
  598.         $this->objectStatus $objectStatus;
  599.         return $this;
  600.     }
  601.     public function getHash(): ?string
  602.     {
  603.         return $this->hash;
  604.     }
  605.     public function setHash(): self
  606.     {
  607.         $this->hash md5(date('YmdHis') . microtime(true) . rand(0999999));
  608.         return $this;
  609.     }
  610.     public function removeFieldForAllEmployee()
  611.     {
  612.         $employees $this->getEmployees();
  613.         if ($employees->count() > 0) {
  614.             $cacheRemover = new CacheRemover();
  615.             foreach ($employees as $employee) {
  616.                 $this->removeEmployee($employee);
  617.                 $cacheRemover->removeCache($employee);
  618.             }
  619.         }
  620.         return $this;
  621.     }
  622.     public function getCompanyGroupId()
  623.     {
  624.         if (!empty($this->getClient())
  625.             && !empty($this->getClient()->getCompany())
  626.             && !empty($this->getClient()->getCompany()->getCompanyGroup())
  627.         ) {
  628.             return $this->getClient()->getCompany()->getCompanyGroup()->getId();
  629.         }
  630.         return null;
  631.     }
  632.     /**
  633.      * @return mixed
  634.      */
  635.     public function isDefaultCultureColor()
  636.     {
  637.         return $this->defaultCultureColor;
  638.     }
  639.     /**
  640.      * @param mixed $defaultCultureColor
  641.      */
  642.     public function setDefaultCultureColor(bool $defaultCultureColor): void
  643.     {
  644.         $this->defaultCultureColor $defaultCultureColor;
  645.     }
  646.     /**
  647.      * @return Collection<int, FieldParams>
  648.      */
  649.     public function getFieldParams(): Collection
  650.     {
  651.         return $this->fieldParams;
  652.     }
  653.     public function getFieldParam(string $key): FieldParams
  654.     {
  655.         $params $this->fieldParams;
  656.         /** @var FieldParams $item */
  657.         foreach ($params as $item) {
  658.             if ($item->getParamKey() == $key) {
  659.                 return $item;
  660.             }
  661.         }
  662.         $newParam = new FieldParams();
  663.         $newParam->setField($this);
  664.         $newParam->setParamKey($key);
  665.         $this->addFieldParam($newParam);
  666.         return $newParam;
  667.     }
  668.     public function addFieldParam(FieldParams $fieldParam): self
  669.     {
  670.         if (!$this->fieldParams->contains($fieldParam)) {
  671.             $this->fieldParams[] = $fieldParam;
  672.             $fieldParam->setField($this);
  673.         }
  674.         return $this;
  675.     }
  676.     public function removeFieldParam(FieldParams $fieldParam): self
  677.     {
  678.         if ($this->fieldParams->removeElement($fieldParam)) {
  679.             // set the owning side to null (unless already changed)
  680.             if ($fieldParam->getField() === $this) {
  681.                 $fieldParam->setField(null);
  682.             }
  683.         }
  684.         return $this;
  685.     }
  686.     public function getFieldParamsToArray(): array
  687.     {
  688.         $response = [];
  689.         $params $this->getFieldParams();
  690.         foreach ($params as $param) {
  691.             $response[$param->getParamKey()] = $param->getValue();
  692.         }
  693.         return $response;
  694.     }
  695.     public function checkCultureExists(int $cultureId): bool
  696.     {
  697.         $status false;
  698.         if (!empty($this->getCulture()) && $this->getCulture()->getId() == $cultureId) {
  699.             return true;
  700.         }
  701.         foreach ($this->getTechcards() as $techcard) {
  702.             if (!empty($techcard->getCulture()) && $techcard->getCulture()->getId() == $cultureId) {
  703.                 return true;
  704.             }
  705.         }
  706.         return $status;
  707.     }
  708.     /**
  709.      * @return Collection<int, Recommendation>
  710.      */
  711.     public function getRecommendations(): Collection
  712.     {
  713.         return $this->recommendations;
  714.     }
  715.     public function addRecommendation(Recommendation $recommendation): self
  716.     {
  717.         if (!$this->recommendations->contains($recommendation)) {
  718.             $this->recommendations[] = $recommendation;
  719.             $recommendation->setField($this);
  720.         }
  721.         return $this;
  722.     }
  723.     public function removeRecommendation(Recommendation $recommendation): self
  724.     {
  725.         if ($this->recommendations->removeElement($recommendation)) {
  726.             // set the owning side to null (unless already changed)
  727.             if ($recommendation->getField() === $this) {
  728.                 $recommendation->setField(null);
  729.             }
  730.         }
  731.         return $this;
  732.     }
  733.     public function getCropWiseFieldId(): ?int
  734.     {
  735.         return $this->cropWiseFieldId;
  736.     }
  737.     public function setCropWiseFieldId(?int $cropWiseFieldId): self
  738.     {
  739.         $this->cropWiseFieldId $cropWiseFieldId;
  740.         return $this;
  741.     }
  742.     public function isArchive(): ?bool
  743.     {
  744.         return $this->archive;
  745.     }
  746.     public function setArchive(?bool $archive): self
  747.     {
  748.         $this->archive $archive;
  749.         return $this;
  750.     }
  751. }