src/Entity/Rent.php line 17

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\RentRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\DBAL\Types\Types;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Uid\Uuid;
  9. use Symfony\Component\HttpFoundation\File\File;
  10. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. #[ORM\Entity(repositoryClassRentRepository::class)]
  13. #[Vich\Uploadable]
  14. class Rent implements \JsonSerializable
  15. {
  16.     #[ORM\Id]
  17.     #[ORM\Column(type'uuid'uniquetrue)]
  18.     #[ORM\GeneratedValue(strategy'CUSTOM')]
  19.     #[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
  20.     private $id;
  21.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  22.     private ?\DateTimeInterface $startAt null;
  23.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  24.     private ?\DateTimeInterface $finishAt null;
  25.     #[ORM\ManyToOne(inversedBy'rents')]
  26.     private ?Company $company null;
  27.     #[ORM\OneToMany(mappedBy'rent'targetEntitySpot::class, cascade: ['persist''remove'])]
  28.     #[ORM\OrderBy(['height' => 'ASC'])]
  29.     private Collection $spots;
  30.     #[ORM\ManyToOne(inversedBy'rents')]
  31.     #[ORM\JoinColumn(nullablefalse)]
  32.     private ?Tower $tower null;
  33.     #[Assert\File(mimeTypes: ["application/pdf""application/x-pdf"])]
  34.     #[Vich\UploadableField(mapping'contract'fileNameProperty'contractName'size'contractSize')]
  35.     private ?File $contractFile null;
  36.     #[ORM\Column(length255nullabletrue)]
  37.     private ?string $contractName null;
  38.     #[ORM\Column(nullabletrue)]
  39.     private ?int $contractSize null;
  40.     #[ORM\Column]
  41.     private ?\DateTimeImmutable $createdAt null;
  42.     #[ORM\Column(typeTypes::DATETIME_MUTABLE)]
  43.     private ?\DateTime $updatedAt null;
  44.     #[Vich\UploadableField(mapping'photo'fileNameProperty'photoName'size'photoSize')]
  45.     private ?File $photoFile null;
  46.     #[ORM\Column(length255nullabletrue)]
  47.     private ?string $photoName null;
  48.     #[ORM\Column(nullabletrue)]
  49.     private ?int $photoSize null;
  50.     private ?string $link null;
  51.     #[ORM\Column(length255nullabletrue)]
  52.     private ?string $price null;
  53.     public ?int $maxHeight 0;
  54.     public function getLink(): ?string
  55.     {
  56.         return $this->link;
  57.     }
  58.     public function setLink(string $link)
  59.     {
  60.         $this->link $link;
  61.     }
  62.     public function __construct()
  63.     {
  64.         $this->spots = new ArrayCollection();
  65.         $this->createdAt = new \DateTimeImmutable();
  66.         $this->updatedAt = new \DateTime();
  67.     }
  68.     public function getId(): ?Uuid
  69.     {
  70.         return $this->id;
  71.     }
  72.     public function getStartAt(): ?\DateTimeInterface
  73.     {
  74.         return $this->startAt;
  75.     }
  76.     public function setStartAt(?\DateTimeInterface $startAt): self
  77.     {
  78.         $this->startAt $startAt;
  79.         return $this;
  80.     }
  81.     public function getFinishAt(): ?\DateTimeInterface
  82.     {
  83.         return $this->finishAt;
  84.     }
  85.     public function setFinishAt(?\DateTimeInterface $finishAt): self
  86.     {
  87.         $this->finishAt $finishAt;
  88.         return $this;
  89.     }
  90.     public function getCompany(): ?Company
  91.     {
  92.         return $this->company;
  93.     }
  94.     public function setCompany(?Company $company): self
  95.     {
  96.         $this->company $company;
  97.         return $this;
  98.     }
  99.     /**
  100.      * @return Collection<int, Spot>
  101.      */
  102.     public function getSpots(): Collection
  103.     {
  104.         return $this->spots;
  105.     }
  106.     public function addSpot(Spot $spot): self
  107.     {
  108.         if (!$this->spots->contains($spot)) {
  109.             $this->spots->add($spot);
  110.             $spot->setRent($this);
  111.         }
  112.         return $this;
  113.     }
  114.     public function removeSpot(Spot $spot): self
  115.     {
  116.         if ($this->spots->removeElement($spot)) {
  117.             // set the owning side to null (unless already changed)
  118.             if ($spot->getRent() === $this) {
  119.                 $spot->setRent(null);
  120.             }
  121.         }
  122.         return $this;
  123.     }
  124.     public function getSpotsAsString(): string
  125.     {
  126.         $spots $this->getSpots();
  127.         $lines = [];
  128.         foreach ($spots as $spot) {
  129.             $lines[] = $spot;
  130.         }
  131.         return implode('<br>'$lines);
  132.     }
  133.     public function getTower(): ?Tower
  134.     {
  135.         return $this->tower;
  136.     }
  137.     public function setTower(?Tower $tower): self
  138.     {
  139.         $this->tower $tower;
  140.         return $this;
  141.     }
  142.     public function jsonSerialize()
  143.     {
  144.         return [
  145.             'id' => $this->getId(),
  146.             'startAt' => $this->getStartAt()->format('d/m/Y'),
  147.             'finishAt' => $this->getFinishAt()->format('d/m/Y'),
  148.             'company' => $this->getCompany()->getName(),
  149.             'link' => $this->getLink(),
  150.         ];
  151.     }
  152.     public function setContractFile(?File $contractFile null): void
  153.     {
  154.         $this->contractFile $contractFile;
  155.         if (null !== $contractFile) {
  156.             $this->updatedAt = new \DateTimeImmutable();
  157.         }
  158.     }
  159.     public function getContractFile(): ?File
  160.     {
  161.         return $this->contractFile;
  162.     }
  163.     public function getContractNameWithFolder(): ?string
  164.     {
  165.         if(empty($this->contractName))
  166.             return $this->contractName;
  167.         $folder "/contracts/".$this->getCreatedAt()->format('Y/m/');
  168.         return $folder $this->contractName;
  169.     }
  170.     public function getContractName(): ?string
  171.     {
  172.         return $this->contractName;
  173.     }
  174.     public function setContractName(?string $contractName): self
  175.     {
  176.         $this->contractName $contractName;
  177.         return $this;
  178.     }
  179.     public function getContractSize(): ?int
  180.     {
  181.         return $this->contractSize;
  182.     }
  183.     public function setContractSize(?int $contractSize): self
  184.     {
  185.         $this->contractSize $contractSize;
  186.         return $this;
  187.     }
  188.     public function getCreatedAt(): ?\DateTimeImmutable
  189.     {
  190.         return $this->createdAt;
  191.     }
  192.     public function setCreatedAt(\DateTimeImmutable $createdAt): self
  193.     {
  194.         $this->createdAt $createdAt;
  195.         return $this;
  196.     }
  197.     public function getUpdatedAt(): ?\DateTimeInterface
  198.     {
  199.         return $this->updatedAt;
  200.     }
  201.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  202.     {
  203.         $this->updatedAt $updatedAt;
  204.         return $this;
  205.     }
  206.     public function setPhotoFile(?File $photoFile null): void
  207.     {
  208.         $this->photoFile $photoFile;
  209.         if (null !== $photoFile) {
  210.             $this->updatedAt = new \DateTimeImmutable();
  211.         }
  212.     }
  213.     public function getPhotoFile(): ?File
  214.     {
  215.         return $this->photoFile;
  216.     }
  217.     public function getPhotoNameWithFolder(): ?string
  218.     {
  219.         if(empty($this->photoName))
  220.             return $this->photoName;
  221.         $folder "/photos/".$this->getCreatedAt()->format('Y/m/');
  222.         return $folder $this->photoName;
  223.     }
  224.     public function getPhotoName(): ?string
  225.     {
  226.         return $this->photoName;
  227.     }
  228.     public function setPhotoName(string $photoName): self
  229.     {
  230.         $this->photoName $photoName;
  231.         return $this;
  232.     }
  233.     public function getPhotoSize(): ?int
  234.     {
  235.         return $this->photoSize;
  236.     }
  237.     public function setPhotoSize(?int $photoSize): self
  238.     {
  239.         $this->photoSize $photoSize;
  240.         return $this;
  241.     }
  242.     public function getRentValue(): ?string
  243.     {
  244.         $totalSpots $this->getSpots()->count();
  245.         $price = empty($this->getPrice()) ? $this->getTower()->getPrice() : $this->getPrice();
  246.         return ($price $totalSpots) ?? 0;
  247.     }
  248.     public function getPrice(): ?string
  249.     {
  250.         return $this->price ?? 0;
  251.     }
  252.     public function setPrice(string $price): self
  253.     {
  254.         $this->price $price;
  255.         return $this;
  256.     }
  257. }