<?php
namespace App\Entity;
use App\Repository\HotelContractRepository;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: HotelContractRepository::class)]
class HotelContract extends Product
{
#[ORM\ManyToOne(targetEntity: Hotel::class, cascade: ['persist'], inversedBy: 'hotelContracts')]
#[ORM\JoinColumn(nullable: false)]
private Hotel $hotel;
#[ORM\OneToMany(mappedBy: 'hotelContract', targetEntity: ContractSeason::class)]
private Collection $contractSeasons;
#[ORM\OneToMany(mappedBy: 'hotelContract', targetEntity: Stopsale::class, orphanRemoval: true)]
private Collection $stopsales;
#[ORM\OneToMany(mappedBy: 'hotelContract', targetEntity: Promo::class, orphanRemoval: true)]
private Collection $promos;
#[ORM\OneToMany(mappedBy: 'hotelContract', targetEntity: CancellationPolicy::class, orphanRemoval: true)]
private Collection $cancellationPolicies;
public function __construct()
{
parent::__construct();
$this->contractSeasons = new ArrayCollection();
$this->stopsales = new ArrayCollection();
$this->promos = new ArrayCollection();
$this->cancellationPolicies = new ArrayCollection();
}
public function getHotel(): ?Hotel
{
return $this->hotel;
}
public function setHotel(?Hotel $hotel): self
{
$this->hotel = $hotel;
return $this;
}
/**
* @return Collection<int, ContractSeason>
*/
public function getContractSeasons(): Collection
{
return $this->contractSeasons;
}
public function addContractSeason(ContractSeason $contractSeason): self
{
if (!$this->contractSeasons->contains($contractSeason)) {
$this->contractSeasons[] = $contractSeason;
$contractSeason->setHotelContract($this);
}
return $this;
}
public function removeContractSeason(ContractSeason $contractSeason): self
{
if ($this->contractSeasons->removeElement($contractSeason)) {
// set the owning side to null (unless already changed)
if ($contractSeason->getHotelContract() === $this) {
$contractSeason->setHotelContract(null);
}
}
return $this;
}
public function __toString(): string
{
$hotel_name = $this->getHotel()->getName();
$provider_name = $this->getProvider()->getName();
if($hotel_name === $provider_name){
return $hotel_name;
}
return $hotel_name." (".$provider_name.")";
}
/**
* @return Collection<int, Stopsale>
*/
public function getStopsales(): Collection
{
return $this->stopsales;
}
public function addStopsale(Stopsale $stopsale): self
{
if (!$this->stopsales->contains($stopsale)) {
$this->stopsales->add($stopsale);
$stopsale->setHotelContract($this);
}
return $this;
}
public function removeStopsale(Stopsale $stopsale): self
{
if ($this->stopsales->removeElement($stopsale)) {
// set the owning side to null (unless already changed)
if ($stopsale->getHotelContract() === $this) {
$stopsale->setHotelContract(null);
}
}
return $this;
}
/**
* @return Collection<int, Promo>
*/
public function getPromos(): Collection
{
return $this->promos;
}
public function addPromo(Promo $promo): self
{
if (!$this->promos->contains($promo)) {
$this->promos->add($promo);
$promo->setHotelContract($this);
}
return $this;
}
public function removePromo(Promo $promo): self
{
if ($this->promos->removeElement($promo)) {
// set the owning side to null (unless already changed)
if ($promo->getHotelContract() === $this) {
$promo->setHotelContract(null);
}
}
return $this;
}
/**
* @return Collection<int, CancellationPolicy>
*/
public function getCancellationPolicies(): Collection
{
return $this->cancellationPolicies;
}
public function addCancellationPolicy(CancellationPolicy $cancellationPolicy): self
{
if (!$this->cancellationPolicies->contains($cancellationPolicy)) {
$this->cancellationPolicies->add($cancellationPolicy);
$cancellationPolicy->setHotelContract($this);
}
return $this;
}
public function removeCancellationPolicy(CancellationPolicy $cancellationPolicy): self
{
if ($this->cancellationPolicies->removeElement($cancellationPolicy)) {
// set the owning side to null (unless already changed)
if ($cancellationPolicy->getHotelContract() === $this) {
$cancellationPolicy->setHotelContract(null);
}
}
return $this;
}
public function getActiveSeasonsByMarket() : array{
$activeSeasons = [];
foreach ($this->contractSeasons as $season) {
$endDate = $season->getEndDate();
if ($endDate >= new DateTime()) {
$marketName = $season->getMarket()->getName();
$activeSeasons[$marketName][] = $season;
}
}
return $activeSeasons;
}
public function getClass() : string {
return "HotelContract";
}
public function getIcon() : string{
return "fas fa-hotel";
}
}