src/Entity/PropertyVisitVoucher.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  6. use App\Entity\Reference\ReferencePropertyDestination;
  7. use App\Entity\Reference\ReferencePropertyType;
  8. use App\Repository\PropertyVisitVoucherRepository;
  9. use App\Traits\TimeStampTrait;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Exception;
  14. use Symfony\Component\Serializer\Annotation\Groups;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. /**
  17.  * @ApiResource(
  18.  *     security= "is_granted('ROLE_AGENT') or is_granted('ROLE_MANAGER')",
  19.  *     normalizationContext={"groups"={"property-visit:read"}, "swagger_definition_name"="Read"},
  20.  *     denormalizationContext={"groups"={"property-visit:write"}, "swagger_definition_name"="Write"},
  21.  *     itemOperations={
  22.  *          "get" = {
  23.  *                     "path"="/property/visits/{id}",
  24.  *                      "security"="is_granted('GET', object)",
  25.  *                  },
  26.  *          "put" = {
  27.  *               "path"="/property/visits/{id}",
  28.  *               "denormalization_context"={"groups"={"property-visit:put"}},
  29.  *               "security"="is_granted('PUT', object)",
  30.  *              },
  31.  *          "delete" = {
  32.  *                 "path"="/property/visits/{id}",
  33.  *                 "security"="is_granted('DELETE', object)",
  34.  *              },
  35.  *     },
  36.  *     collectionOperations={
  37.  *          "get" = {
  38.  *                  "path"="/property/visits",
  39.  *
  40.  *                 },
  41.  *          "post" = {
  42.  *                "path"="/property/visits",
  43.  *                 "security_post_denormalize" = "is_granted('POST', object)"
  44.  *              }
  45.  *     },
  46.  *     attributes={
  47.  *          "pagination_items_per_page"=10,
  48.  *          "formats"={"jsonld", "json", "html", "jsonhal", "csv"={"text/csv"} },
  49.  *          "order"={"id":"DESC"}
  50.  *     }
  51.  * )
  52.  *
  53.  *  @ApiFilter(SearchFilter::class, properties={
  54.  *          "transaction.id": "exact",
  55.  *          "contact.id": "exact"
  56.  *  })
  57.  * @ORM\Entity(repositoryClass=PropertyVisitVoucherRepository::class)
  58.  * @ORM\HasLifecycleCallbacks()
  59.  * @ORM\EntityListeners({"App\EventListener\VisitVoucherListener"})
  60.  */
  61. class PropertyVisitVoucher
  62. {
  63.     use TimeStampTrait;
  64.     /**
  65.      * @ORM\Id
  66.      * @ORM\GeneratedValue
  67.      * @ORM\Column(type="integer")
  68.      * @Groups({"property-visit:read", "contact:read", "property-report:read"})
  69.      */
  70.     private $id;
  71.     /**
  72.      * @ORM\Column(type="integer")
  73.      * @Groups({"property-visit:read"})
  74.      */
  75.     private ?int $createdBy;
  76.     /**
  77.      * @ORM\ManyToMany(targetEntity=Mandate::class, mappedBy="propertyVisitVouchers")
  78.      * @Groups({"property-visit:read","property-visit:write","property-visit:put","contact:read"})
  79.      */
  80.     private $mandates;
  81.     /**
  82.      * @ORM\Column(type="float", nullable=true)
  83.      * @Groups({"property-visit:read","property-visit:write","property-visit:put"})
  84.      */
  85.     private ?float $minPrice 0;
  86.     /**
  87.      * @ORM\Column(type="float", nullable=true)
  88.      * @Groups({"property-visit:read","property-visit:write","property-visit:put"})
  89.      */
  90.     private ?float $maxPrice 0;
  91.     /**
  92.      * @ORM\Column(type="integer", nullable=true)
  93.      * @Groups({"property-visit:read","property-visit:write","property-visit:put"})
  94.      */
  95.     private ?int $minArea 0;
  96.     /**
  97.      * @ORM\Column(type="integer", nullable=true)
  98.      * @Groups({"property-visit:read","property-visit:write","property-visit:put"})
  99.      */
  100.     private ?int $maxArea 0;
  101.     /**
  102.      * @ORM\Column(type="string", nullable=true)
  103.      * @Groups({"property-visit:read","property-visit:write","property-visit:put"})
  104.      */
  105.     private ?string $notes "";
  106.     /**
  107.      * @ORM\ManyToOne(targetEntity=Contact::class, inversedBy="propertyVisitVouchers")
  108.      * @Groups({"property-visit:read","property-visit:write","property-visit:put"})
  109.      */
  110.     private ?Contact $contact;
  111.     /**
  112.      * @ORM\Column(type="time", nullable=true)
  113.      * @Groups({"property-visit:read","property-visit:write","property-visit:put"})
  114.      */
  115.     private ?\DateTimeInterface $startAt;
  116.     /**
  117.      * @ORM\Column(type="time", nullable=true)
  118.      * @Assert\GreaterThan(
  119.      *     propertyPath="startAt",
  120.      *     message="StartTime must be less than EndTime."
  121.      * )
  122.      * @Groups({"property-visit:read","property-visit:write","property-visit:put"})
  123.      */
  124.     private ?\DateTimeInterface $endAt;
  125.     /**
  126.      * @ORM\Column(type="date", nullable=true)
  127.      * @Groups({"property-visit:read","property-visit:write","property-visit:put"})
  128.      */
  129.     private ?\DateTimeInterface $visitDate;
  130.     /**
  131.      * @ORM\ManyToOne(targetEntity=ReferencePropertyDestination::class)
  132.      * @Groups({"property-visit:read","property-visit:write","property-visit:put"})
  133.      */
  134.     private ?ReferencePropertyDestination $referencePropertyDestination null;
  135.     /**
  136.      * @ORM\ManyToOne(targetEntity=ReferencePropertyType::class)
  137.      * @Groups({"property-visit:read","property-visit:write","property-visit:put"})
  138.      */
  139.     private ?ReferencePropertyType $referencePropertyType null;
  140.     /**
  141.      * @ORM\ManyToMany(targetEntity=City::class)
  142.      * @Groups({"property-visit:read","property-visit:write","property-visit:put","contact:read"})
  143.      */
  144.     private $cities;
  145.     /**
  146.      * @ORM\ManyToMany(targetEntity=Neighborhood::class)
  147.      * @Groups({"property-visit:read","property-visit:write","property-visit:put","contact:read"})
  148.      */
  149.     private $neighborhoods;
  150.     /**
  151.      * @ORM\OneToMany(targetEntity=PropertyVisitReport::class, mappedBy="visitVoucher", cascade={"remove"})
  152.      * @Groups({"property-visit:read"})
  153.      */
  154.     private $propertyVisitReports;
  155.     /**
  156.      * @ORM\Column(type="float", nullable=true)
  157.      * @Groups({"property-visit:read","property-visit:write","property-visit:put"})
  158.      */
  159.     private $fees 0;
  160.     public function __construct()
  161.     {
  162.         $this->mandates = new ArrayCollection();
  163.         $this->cities = new ArrayCollection();
  164.         $this->neighborhoods = new ArrayCollection();
  165.         $this->propertyVisitReports = new ArrayCollection();
  166.     }
  167.     public function getId(): ?int
  168.     {
  169.         return $this->id;
  170.     }
  171.     public function getCreatedBy(): ?int
  172.     {
  173.         return $this->createdBy;
  174.     }
  175.     public function setCreatedBy(int $createdBy): self
  176.     {
  177.         $this->createdBy $createdBy;
  178.         return $this;
  179.     }
  180.     /**
  181.      * @return Collection<int, Mandate>
  182.      */
  183.     public function getMandates(): Collection
  184.     {
  185.         return $this->mandates;
  186.     }
  187.     public function addMandate(Mandate $mandate): self
  188.     {
  189.         if (!$this->mandates->contains($mandate)) {
  190.             $this->mandates[] = $mandate;
  191.             $mandate->addPropertyVisitVoucher($this);
  192.         }
  193.         return $this;
  194.     }
  195.     public function removeMandate(Mandate $mandate): self
  196.     {
  197.         if ($this->mandates->removeElement($mandate)) {
  198.             $mandate->removePropertyVisitVoucher($this);
  199.         }
  200.         return $this;
  201.     }
  202.     public function getContact(): ?Contact
  203.     {
  204.         return $this->contact;
  205.     }
  206.     public function setContact(?Contact $contact): self
  207.     {
  208.         $this->contact $contact;
  209.         return $this;
  210.     }
  211.     public function getStartAt(): ?\DateTimeInterface
  212.     {
  213.         return $this->startAt;
  214.     }
  215.     public function setStartAt(?\DateTimeInterface $startAt): self
  216.     {
  217.         $this->startAt $startAt;
  218.         return $this;
  219.     }
  220.     public function getEndAt(): ?\DateTimeInterface
  221.     {
  222.         return $this->endAt;
  223.     }
  224.     public function setEndAt(?\DateTimeInterface $endAt): self
  225.     {
  226.         $this->endAt $endAt;
  227.         return $this;
  228.     }
  229.     public function getVisitDate(): ?\DateTimeInterface
  230.     {
  231.         return $this->visitDate;
  232.     }
  233.     public function setVisitDate(?\DateTimeInterface $visitDate): self
  234.     {
  235.         $this->visitDate $visitDate;
  236.         return $this;
  237.     }
  238.     /**
  239.      * @return float|null
  240.      */
  241.     public function getMinPrice(): ?float
  242.     {
  243.         return $this->minPrice;
  244.     }
  245.     /**
  246.      * @param float|null $minPrice
  247.      * @return PropertyVisitVoucher
  248.      */
  249.     public function setMinPrice(?float $minPrice): self
  250.     {
  251.         $this->minPrice $minPrice;
  252.         return $this;
  253.     }
  254.     /**
  255.      * @return float|null
  256.      */
  257.     public function getMaxPrice(): ?float
  258.     {
  259.         return $this->maxPrice;
  260.     }
  261.     /**
  262.      * @param float|null $maxPrice
  263.      * @return PropertyVisitVoucher
  264.      */
  265.     public function setMaxPrice(?float $maxPrice): self
  266.     {
  267.         $this->maxPrice $maxPrice;
  268.         return $this;
  269.     }
  270.     /**
  271.      * @return int|null
  272.      */
  273.     public function getMinArea(): ?int
  274.     {
  275.         return $this->minArea;
  276.     }
  277.     /**
  278.      * @param int|null $minArea
  279.      * @return PropertyVisitVoucher
  280.      */
  281.     public function setMinArea(?int $minArea): self
  282.     {
  283.         $this->minArea $minArea;
  284.         return $this;
  285.     }
  286.     /**
  287.      * @return int|null
  288.      */
  289.     public function getMaxArea(): ?int
  290.     {
  291.         return $this->maxArea;
  292.     }
  293.     /**
  294.      * @param int|null $maxArea
  295.      * @return PropertyVisitVoucher
  296.      */
  297.     public function setMaxArea(?int $maxArea): self
  298.     {
  299.         $this->maxArea $maxArea;
  300.         return $this;
  301.     }
  302.     /**
  303.      * @return string|null
  304.      */
  305.     public function getNotes(): ?string
  306.     {
  307.         return $this->notes;
  308.     }
  309.     /**
  310.      * @param string|null $notes
  311.      * @return PropertyVisitVoucher
  312.      */
  313.     public function setNotes(?string $notes): self
  314.     {
  315.         $this->notes $notes;
  316.         return $this;
  317.     }
  318.     /**
  319.      * @return ReferencePropertyDestination|null
  320.      */
  321.     public function getReferencePropertyDestination(): ?ReferencePropertyDestination
  322.     {
  323.         return $this->referencePropertyDestination;
  324.     }
  325.     /**
  326.      * @param ReferencePropertyDestination|null $referencePropertyDestination
  327.      * @return PropertyVisitVoucher
  328.      */
  329.     public function setReferencePropertyDestination(?ReferencePropertyDestination $referencePropertyDestination): self
  330.     {
  331.         $this->referencePropertyDestination $referencePropertyDestination;
  332.         return $this;
  333.     }
  334.     /**
  335.      * @return ReferencePropertyType|null
  336.      */
  337.     public function getReferencePropertyType(): ?ReferencePropertyType
  338.     {
  339.         return $this->referencePropertyType;
  340.     }
  341.     /**
  342.      * @param ReferencePropertyType|null $referencePropertyType
  343.      * @return PropertyVisitVoucher
  344.      */
  345.     public function setReferencePropertyType(?ReferencePropertyType $referencePropertyType): self
  346.     {
  347.         $this->referencePropertyType $referencePropertyType;
  348.         return $this;
  349.     }
  350.     /**
  351.      * @return Collection<int, City>
  352.      */
  353.     public function getCities(): Collection
  354.     {
  355.         return $this->cities;
  356.     }
  357.     public function addCity(City $city): self
  358.     {
  359.         if (!$this->cities->contains($city)) {
  360.             $this->cities[] = $city;
  361.         }
  362.         return $this;
  363.     }
  364.     public function removeCity(City $city): self
  365.     {
  366.         $this->cities->removeElement($city);
  367.         return $this;
  368.     }
  369.     /**
  370.      * @return Collection<int, Neighborhood>
  371.      */
  372.     public function getNeighborhoods(): Collection
  373.     {
  374.         return $this->neighborhoods;
  375.     }
  376.     public function addNeighborhood(Neighborhood $neighborhood): self
  377.     {
  378.         if (!$this->neighborhoods->contains($neighborhood)) {
  379.             $this->neighborhoods[] = $neighborhood;
  380.         }
  381.         return $this;
  382.     }
  383.     public function removeNeighborhood(Neighborhood $neighborhood): self
  384.     {
  385.         $this->neighborhoods->removeElement($neighborhood);
  386.         return $this;
  387.     }
  388.     /**
  389.      * @return Collection<int, PropertyVisitReport>
  390.      */
  391.     public function getPropertyVisitReports(): Collection
  392.     {
  393.         return $this->propertyVisitReports;
  394.     }
  395.     public function addPropertyVisitReport(PropertyVisitReport $propertyVisitReport): self
  396.     {
  397.         if (!$this->propertyVisitReports->contains($propertyVisitReport)) {
  398.             $this->propertyVisitReports[] = $propertyVisitReport;
  399.             $propertyVisitReport->setPropertyVisitVoucher($this);
  400.         }
  401.         return $this;
  402.     }
  403.     public function removePropertyVisitReport(PropertyVisitReport $propertyVisitReport): self
  404.     {
  405.         if ($this->propertyVisitReports->removeElement($propertyVisitReport)) {
  406.             if ($propertyVisitReport->getPropertyVisitVoucher() === $this) {
  407.                 $propertyVisitReport->setPropertyVisitVoucher(null);
  408.             }
  409.         }
  410.         return $this;
  411.     }
  412.     public function getFees(): ?float
  413.     {
  414.         return $this->fees;
  415.     }
  416.     public function setFees(?float $fees): self
  417.     {
  418.         $this->fees $fees;
  419.         return $this;
  420.     }
  421.     /**
  422.      * @Groups({"property-visit:read"})
  423.      * @throws Exception
  424.      */
  425.     public function getCanSendReport(): bool
  426.     {
  427.         if (empty($this->visitDate) || count($this->propertyVisitReports) > 0) {
  428.             return false;
  429.         }
  430.         $dueVisitDate = new \DateTime($this->visitDate->format('Y-m-d') . (!empty($this->endAt) ?
  431.                         $this->endAt->format(' H:i:s') : ''));
  432.         return $dueVisitDate < (new \DateTime());
  433.     }
  434.     /**
  435.      * @Groups({"property-visit:read"})
  436.      */
  437.     public function getIsEditable(): bool
  438.     {
  439.         /**
  440.          * @var Mandate $mandate
  441.          */
  442.         foreach ($this->mandates as $mandate) {
  443.             if ($mandate->canGenerateTransaction()) {
  444.                 return true;
  445.             }
  446.         }
  447.         return false;
  448.     }
  449. }