<?php
namespace App\Entity;
use App\Repository\ContractSeasonRepository;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Asserts;
#[ORM\Entity(repositoryClass: ContractSeasonRepository::class)]
class ContractSeason
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\ManyToOne(targetEntity: HotelContract::class, inversedBy: 'contractSeasons')]
#[ORM\JoinColumn(nullable: false)]
#[Asserts\NotBlank]
private ?HotelContract $hotelContract;
#[Asserts\NotBlank]
#[ORM\Column(type: 'datetime', nullable: false)]
private ?DateTimeInterface $startdate;
#[Asserts\NotBlank]
#[ORM\Column(type: 'datetime', nullable: false)]
private ?DateTimeInterface $enddate;
#[ORM\Column(type: 'string', length: 32, nullable: true)]
private ?string $commCalculType; // or margeCalculType depending on pricingType
#[ORM\Column(type: 'float', nullable: true)]
#[Asserts\PositiveOrZero]
private ?float $commValue; // or margeValue depending on pricingType
#[ORM\ManyToOne(targetEntity: Market::class)]
#[ORM\JoinColumn(nullable: false)]
private ?Market $market;
#[ORM\OneToMany(mappedBy: 'season', targetEntity: Pricing::class, cascade: ['persist'])]
#[ORM\JoinColumn(name: "season_id", referencedColumnName: "season_id", onDelete: "CASCADE")]
private $pricings;
#[ORM\Column(type: Types::SMALLINT)]
private ?int $pricingType = null;
#[ORM\ManyToOne]
private ?Arrangement $baseArrangement = null;
#[ORM\Column(nullable: true)]
private ?int $byPerson = null;
#[ORM\Column(nullable: true)]
private ?bool $NRF = null;
#[ORM\Column(nullable: true)]
private ?int $minimumStay = null;
public function __construct()
{
$this->pricings = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getHotelContract(): ?HotelContract
{
return $this->hotelContract;
}
public function setHotelContract(?HotelContract $hotelContract): self
{
$this->hotelContract = $hotelContract;
return $this;
}
public function getStartdate(): ?DateTimeInterface
{
return $this->startdate;
}
public function setStartdate(?DateTimeInterface $startdate): self
{
$this->startdate = $startdate;
return $this;
}
public function getEnddate(): ?DateTimeInterface
{
return $this->enddate;
}
public function setEnddate(?DateTimeInterface $enddate): self
{
$this->enddate = $enddate;
return $this;
}
public function getCommCalculType(): ?string
{
return $this->commCalculType;
}
public function setCommCalculType(?string $commCalculType): self
{
$this->commCalculType = $commCalculType;
return $this;
}
public function getCommValue(): ?float
{
return $this->commValue;
}
public function setCommValue(?float $commValue): self
{
$this->commValue = $commValue;
return $this;
}
public function getMarket(): ?Market
{
return $this->market;
}
public function setMarket(?Market $market): self
{
$this->market = $market;
return $this;
}
/**
* @return Collection<int, Pricing>
*/
public function getPricings(): Collection
{
return $this->pricings;
}
public function addPricing(Pricing $pricing): self
{
if (!$this->pricings->contains($pricing)) {
$this->pricings[] = $pricing;
$pricing->setSeason($this);
}
return $this;
}
public function removePricing(Pricing $pricing): self
{
if ($this->pricings->removeElement($pricing)) {
// set the owning side to null (unless already changed)
if ($pricing->getSeason() === $this) {
$pricing->setSeason(null);
}
}
return $this;
}
public function __clone(): void
{
if ($this->id) {
$this->id = null;
$saved_cloned_operations = [];
// cloning the relation pricings which is a OneToMany
$pricingsClonedCollection = new ArrayCollection();
foreach ($this->getPricings() as $pricing) {
$pricingClone = clone $pricing;
$pricingClone->setSeason(null);
foreach ($pricing->getOperations() as $operation){
if ( array_key_exists($operation->getId(), $saved_cloned_operations)){
$operationClone = $saved_cloned_operations[$operation->getId()];
}else
{
$operationClone = $operation->getCopy();
$saved_cloned_operations[$operation->getId()] = $operationClone;
}
$operationClone->addPricing($pricingClone);
$pricingClone->addOperation($operationClone);
//dd($pricingClone);
}
$pricingClone->setSeason($this);
$pricingsClonedCollection->add($pricingClone);
}
$this->pricings = $pricingsClonedCollection;
}
}
public function __toString(): string
{
return 'Season '.$this->startdate->format('d/m/Y')." - ".$this->enddate->format('d/m/Y');
}
#[Asserts\IsTrue(message:'already exist')]
function isExist(): bool
{
// TODO CHECK THIS FUCNTION isExist
return $this->getStartdate() && $this->getEnddate() && $this->getMarket();
}
public function getHotelRoomTypes() : array{
$hrt_array = [];
foreach ($this->pricings as $pricing){
$hrt = $pricing->getHotelRoomTypeArrangement()->getHotelRoomType();
if(!in_array($hrt, $hrt_array))
$hrt_array[] = $hrt;
}
return $hrt_array;
}
public function getArrangements() : ?array{
$arrangements = [];
foreach ($this->pricings as $pricing){
$arrangement = $pricing->getHotelRoomTypeArrangement()->getArrangement();
if(!in_array($arrangement, $arrangements))
$arrangements[] = $arrangement;
}
return $arrangements;
}
public function getPricingType(): ?int
{
return $this->pricingType;
}
public function setPricingType(int $pricingType): self
{
$this->pricingType = $pricingType;
return $this;
}
public function getBaseArrangement(): ?Arrangement
{
return $this->baseArrangement;
}
public function setBaseArrangement(?Arrangement $baseArrangement): self
{
$this->baseArrangement = $baseArrangement;
return $this;
}
public function overlap(ContractSeason $old_season): bool
{
// overlap means (StartA <= EndB) and (EndA >= StartB)
return
$this->startdate <= $old_season->enddate and
$this->enddate >= $old_season->startdate ;
}
public function isByPerson(): ?int
{
return $this->byPerson;
}
public function setByPerson(?int $byPerson): self
{
$this->byPerson = $byPerson;
return $this;
}
public function isNRF(): ?bool
{
return $this->NRF;
}
public function setNRF(?bool $NRF): self
{
$this->NRF = $NRF;
return $this;
}
public function getMinimumStay(): ?int
{
return $this->minimumStay;
}
public function setMinimumStay(?int $minimumStay): self
{
$this->minimumStay = $minimumStay;
return $this;
}
}