<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use App\Entity\Reference\ReferencePropertyDestination;
use App\Entity\Reference\ReferencePropertyType;
use App\Repository\PropertyVisitVoucherRepository;
use App\Traits\TimeStampTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Exception;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ApiResource(
* security= "is_granted('ROLE_AGENT') or is_granted('ROLE_MANAGER')",
* normalizationContext={"groups"={"property-visit:read"}, "swagger_definition_name"="Read"},
* denormalizationContext={"groups"={"property-visit:write"}, "swagger_definition_name"="Write"},
* itemOperations={
* "get" = {
* "path"="/property/visits/{id}",
* "security"="is_granted('GET', object)",
* },
* "put" = {
* "path"="/property/visits/{id}",
* "denormalization_context"={"groups"={"property-visit:put"}},
* "security"="is_granted('PUT', object)",
* },
* "delete" = {
* "path"="/property/visits/{id}",
* "security"="is_granted('DELETE', object)",
* },
* },
* collectionOperations={
* "get" = {
* "path"="/property/visits",
*
* },
* "post" = {
* "path"="/property/visits",
* "security_post_denormalize" = "is_granted('POST', object)"
* }
* },
* attributes={
* "pagination_items_per_page"=10,
* "formats"={"jsonld", "json", "html", "jsonhal", "csv"={"text/csv"} },
* "order"={"id":"DESC"}
* }
* )
*
* @ApiFilter(SearchFilter::class, properties={
* "transaction.id": "exact",
* "contact.id": "exact"
* })
* @ORM\Entity(repositoryClass=PropertyVisitVoucherRepository::class)
* @ORM\HasLifecycleCallbacks()
* @ORM\EntityListeners({"App\EventListener\VisitVoucherListener"})
*/
class PropertyVisitVoucher
{
use TimeStampTrait;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"property-visit:read", "contact:read", "property-report:read"})
*/
private $id;
/**
* @ORM\Column(type="integer")
* @Groups({"property-visit:read"})
*/
private ?int $createdBy;
/**
* @ORM\ManyToMany(targetEntity=Mandate::class, mappedBy="propertyVisitVouchers")
* @Groups({"property-visit:read","property-visit:write","property-visit:put","contact:read"})
*/
private $mandates;
/**
* @ORM\Column(type="float", nullable=true)
* @Groups({"property-visit:read","property-visit:write","property-visit:put"})
*/
private ?float $minPrice = 0;
/**
* @ORM\Column(type="float", nullable=true)
* @Groups({"property-visit:read","property-visit:write","property-visit:put"})
*/
private ?float $maxPrice = 0;
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups({"property-visit:read","property-visit:write","property-visit:put"})
*/
private ?int $minArea = 0;
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups({"property-visit:read","property-visit:write","property-visit:put"})
*/
private ?int $maxArea = 0;
/**
* @ORM\Column(type="string", nullable=true)
* @Groups({"property-visit:read","property-visit:write","property-visit:put"})
*/
private ?string $notes = "";
/**
* @ORM\ManyToOne(targetEntity=Contact::class, inversedBy="propertyVisitVouchers")
* @Groups({"property-visit:read","property-visit:write","property-visit:put"})
*/
private ?Contact $contact;
/**
* @ORM\Column(type="time", nullable=true)
* @Groups({"property-visit:read","property-visit:write","property-visit:put"})
*/
private ?\DateTimeInterface $startAt;
/**
* @ORM\Column(type="time", nullable=true)
* @Assert\GreaterThan(
* propertyPath="startAt",
* message="StartTime must be less than EndTime."
* )
* @Groups({"property-visit:read","property-visit:write","property-visit:put"})
*/
private ?\DateTimeInterface $endAt;
/**
* @ORM\Column(type="date", nullable=true)
* @Groups({"property-visit:read","property-visit:write","property-visit:put"})
*/
private ?\DateTimeInterface $visitDate;
/**
* @ORM\ManyToOne(targetEntity=ReferencePropertyDestination::class)
* @Groups({"property-visit:read","property-visit:write","property-visit:put"})
*/
private ?ReferencePropertyDestination $referencePropertyDestination = null;
/**
* @ORM\ManyToOne(targetEntity=ReferencePropertyType::class)
* @Groups({"property-visit:read","property-visit:write","property-visit:put"})
*/
private ?ReferencePropertyType $referencePropertyType = null;
/**
* @ORM\ManyToMany(targetEntity=City::class)
* @Groups({"property-visit:read","property-visit:write","property-visit:put","contact:read"})
*/
private $cities;
/**
* @ORM\ManyToMany(targetEntity=Neighborhood::class)
* @Groups({"property-visit:read","property-visit:write","property-visit:put","contact:read"})
*/
private $neighborhoods;
/**
* @ORM\OneToMany(targetEntity=PropertyVisitReport::class, mappedBy="visitVoucher", cascade={"remove"})
* @Groups({"property-visit:read"})
*/
private $propertyVisitReports;
/**
* @ORM\Column(type="float", nullable=true)
* @Groups({"property-visit:read","property-visit:write","property-visit:put"})
*/
private $fees = 0;
public function __construct()
{
$this->mandates = new ArrayCollection();
$this->cities = new ArrayCollection();
$this->neighborhoods = new ArrayCollection();
$this->propertyVisitReports = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getCreatedBy(): ?int
{
return $this->createdBy;
}
public function setCreatedBy(int $createdBy): self
{
$this->createdBy = $createdBy;
return $this;
}
/**
* @return Collection<int, Mandate>
*/
public function getMandates(): Collection
{
return $this->mandates;
}
public function addMandate(Mandate $mandate): self
{
if (!$this->mandates->contains($mandate)) {
$this->mandates[] = $mandate;
$mandate->addPropertyVisitVoucher($this);
}
return $this;
}
public function removeMandate(Mandate $mandate): self
{
if ($this->mandates->removeElement($mandate)) {
$mandate->removePropertyVisitVoucher($this);
}
return $this;
}
public function getContact(): ?Contact
{
return $this->contact;
}
public function setContact(?Contact $contact): self
{
$this->contact = $contact;
return $this;
}
public function getStartAt(): ?\DateTimeInterface
{
return $this->startAt;
}
public function setStartAt(?\DateTimeInterface $startAt): self
{
$this->startAt = $startAt;
return $this;
}
public function getEndAt(): ?\DateTimeInterface
{
return $this->endAt;
}
public function setEndAt(?\DateTimeInterface $endAt): self
{
$this->endAt = $endAt;
return $this;
}
public function getVisitDate(): ?\DateTimeInterface
{
return $this->visitDate;
}
public function setVisitDate(?\DateTimeInterface $visitDate): self
{
$this->visitDate = $visitDate;
return $this;
}
/**
* @return float|null
*/
public function getMinPrice(): ?float
{
return $this->minPrice;
}
/**
* @param float|null $minPrice
* @return PropertyVisitVoucher
*/
public function setMinPrice(?float $minPrice): self
{
$this->minPrice = $minPrice;
return $this;
}
/**
* @return float|null
*/
public function getMaxPrice(): ?float
{
return $this->maxPrice;
}
/**
* @param float|null $maxPrice
* @return PropertyVisitVoucher
*/
public function setMaxPrice(?float $maxPrice): self
{
$this->maxPrice = $maxPrice;
return $this;
}
/**
* @return int|null
*/
public function getMinArea(): ?int
{
return $this->minArea;
}
/**
* @param int|null $minArea
* @return PropertyVisitVoucher
*/
public function setMinArea(?int $minArea): self
{
$this->minArea = $minArea;
return $this;
}
/**
* @return int|null
*/
public function getMaxArea(): ?int
{
return $this->maxArea;
}
/**
* @param int|null $maxArea
* @return PropertyVisitVoucher
*/
public function setMaxArea(?int $maxArea): self
{
$this->maxArea = $maxArea;
return $this;
}
/**
* @return string|null
*/
public function getNotes(): ?string
{
return $this->notes;
}
/**
* @param string|null $notes
* @return PropertyVisitVoucher
*/
public function setNotes(?string $notes): self
{
$this->notes = $notes;
return $this;
}
/**
* @return ReferencePropertyDestination|null
*/
public function getReferencePropertyDestination(): ?ReferencePropertyDestination
{
return $this->referencePropertyDestination;
}
/**
* @param ReferencePropertyDestination|null $referencePropertyDestination
* @return PropertyVisitVoucher
*/
public function setReferencePropertyDestination(?ReferencePropertyDestination $referencePropertyDestination): self
{
$this->referencePropertyDestination = $referencePropertyDestination;
return $this;
}
/**
* @return ReferencePropertyType|null
*/
public function getReferencePropertyType(): ?ReferencePropertyType
{
return $this->referencePropertyType;
}
/**
* @param ReferencePropertyType|null $referencePropertyType
* @return PropertyVisitVoucher
*/
public function setReferencePropertyType(?ReferencePropertyType $referencePropertyType): self
{
$this->referencePropertyType = $referencePropertyType;
return $this;
}
/**
* @return Collection<int, City>
*/
public function getCities(): Collection
{
return $this->cities;
}
public function addCity(City $city): self
{
if (!$this->cities->contains($city)) {
$this->cities[] = $city;
}
return $this;
}
public function removeCity(City $city): self
{
$this->cities->removeElement($city);
return $this;
}
/**
* @return Collection<int, Neighborhood>
*/
public function getNeighborhoods(): Collection
{
return $this->neighborhoods;
}
public function addNeighborhood(Neighborhood $neighborhood): self
{
if (!$this->neighborhoods->contains($neighborhood)) {
$this->neighborhoods[] = $neighborhood;
}
return $this;
}
public function removeNeighborhood(Neighborhood $neighborhood): self
{
$this->neighborhoods->removeElement($neighborhood);
return $this;
}
/**
* @return Collection<int, PropertyVisitReport>
*/
public function getPropertyVisitReports(): Collection
{
return $this->propertyVisitReports;
}
public function addPropertyVisitReport(PropertyVisitReport $propertyVisitReport): self
{
if (!$this->propertyVisitReports->contains($propertyVisitReport)) {
$this->propertyVisitReports[] = $propertyVisitReport;
$propertyVisitReport->setPropertyVisitVoucher($this);
}
return $this;
}
public function removePropertyVisitReport(PropertyVisitReport $propertyVisitReport): self
{
if ($this->propertyVisitReports->removeElement($propertyVisitReport)) {
if ($propertyVisitReport->getPropertyVisitVoucher() === $this) {
$propertyVisitReport->setPropertyVisitVoucher(null);
}
}
return $this;
}
public function getFees(): ?float
{
return $this->fees;
}
public function setFees(?float $fees): self
{
$this->fees = $fees;
return $this;
}
/**
* @Groups({"property-visit:read"})
* @throws Exception
*/
public function getCanSendReport(): bool
{
if (empty($this->visitDate) || count($this->propertyVisitReports) > 0) {
return false;
}
$dueVisitDate = new \DateTime($this->visitDate->format('Y-m-d') . (!empty($this->endAt) ?
$this->endAt->format(' H:i:s') : ''));
return $dueVisitDate < (new \DateTime());
}
/**
* @Groups({"property-visit:read"})
*/
public function getIsEditable(): bool
{
/**
* @var Mandate $mandate
*/
foreach ($this->mandates as $mandate) {
if ($mandate->canGenerateTransaction()) {
return true;
}
}
return false;
}
}