<?php
namespace App\Entity;
use App\Entity\Privatisation;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use App\Repository\ExtraReservationRepository;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity(repositoryClass=ExtraReservationRepository::class)
*/
class ExtraReservation
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $quantity;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $commentary;
/**
* @ORM\ManyToOne(targetEntity=Extra::class, inversedBy="extraReservations")
* @ORM\JoinColumn(nullable=false)
*/
private $extra;
/**
* @ORM\ManyToOne(targetEntity=Privatisation::class, inversedBy="extraReservations")
* @ORM\JoinColumn(nullable=true)
*/
private $privatisation;
/**
* @ORM\OneToMany(targetEntity=OptionQuantityReservation::class, mappedBy="extraReservation", orphanRemoval=true, cascade={"persist"})
*/
private $optionQuantityReservations;
/**
* @ORM\OneToMany(targetEntity=OptionSelectReservation::class, mappedBy="extraReservation", orphanRemoval=true, cascade={"persist"})
*/
private $optionSelectReservations;
/**
* @ORM\ManyToOne(targetEntity=Token::class, inversedBy="extraReservations")
* @ORM\JoinColumn(nullable=true)
*/
private $token;
/**
* @ORM\ManyToOne(targetEntity=Reservation::class, inversedBy="extraReservations")
* @ORM\JoinColumn(nullable=true)
*/
private $reservation;
public function __construct()
{
$this->optionQuantityReservations = new ArrayCollection();
$this->optionSelectReservations = new ArrayCollection();
}
public function __clone()
{
$optionQuantityReservations = new ArrayCollection();
foreach($this->getOptionQuantityReservations() as $optionQuantityReservation) {
$newOptionQuantityReservation = clone $optionQuantityReservation;
$newOptionQuantityReservation->setExtraReservation($this);
$optionQuantityReservations->add($newOptionQuantityReservation);
}
$this->optionQuantityReservations = $optionQuantityReservations;
$optionSelectReservations = new ArrayCollection();
foreach($this->getOptionSelectReservations() as $optionSelectReservation) {
$newOptionSelectReservation = clone $optionSelectReservation;
$newOptionSelectReservation->setExtraReservation($this);
$optionSelectReservations->add($newOptionSelectReservation);
}
$this->optionSelectReservations = $optionSelectReservations;
}
public function getId(): ?int
{
return $this->id;
}
public function getQuantity(): ?int
{
return $this->quantity;
}
public function setQuantity(int $quantity): self
{
$this->quantity = $quantity;
return $this;
}
public function getCommentary(): ?string
{
return $this->commentary;
}
public function setCommentary(?string $commentary): self
{
$this->commentary = $commentary;
return $this;
}
public function getExtra(): ?Extra
{
return $this->extra;
}
public function setExtra(?Extra $extra): self
{
$this->extra = $extra;
return $this;
}
public function getPrivatisation(): ?Privatisation
{
return $this->privatisation;
}
public function setPrivatisation(?Privatisation $privatisation): self
{
$this->privatisation = $privatisation;
return $this;
}
/**
* @return Collection<int, OptionQuantityReservation>
*/
public function getOptionQuantityReservations(): Collection
{
$reservations = $this->optionQuantityReservations->toArray();
usort($reservations, function ($a, $b) {
return $a->getOptionQuantity()->getId() <=> $b->getOptionQuantity()->getId();
});
return new ArrayCollection($reservations);
}
public function addOptionQuantityReservation(OptionQuantityReservation $optionQuantityReservation): self
{
if (!$this->optionQuantityReservations->contains($optionQuantityReservation)) {
$this->optionQuantityReservations[] = $optionQuantityReservation;
$optionQuantityReservation->setExtraReservation($this);
}
return $this;
}
public function removeOptionQuantityReservation(OptionQuantityReservation $optionQuantityReservation): self
{
if ($this->optionQuantityReservations->removeElement($optionQuantityReservation)) {
// set the owning side to null (unless already changed)
if ($optionQuantityReservation->getExtraReservation() === $this) {
$optionQuantityReservation->setExtraReservation(null);
}
}
return $this;
}
/**
* @return Collection<int, OptionSelectReservation>
*/
public function getOptionSelectReservations(): Collection
{
return $this->optionSelectReservations;
}
public function addOptionSelectReservation(OptionSelectReservation $optionSelectReservation): self
{
if (!$this->optionSelectReservations->contains($optionSelectReservation)) {
$this->optionSelectReservations[] = $optionSelectReservation;
$optionSelectReservation->setExtraReservation($this);
}
return $this;
}
public function removeOptionSelectReservation(OptionSelectReservation $optionSelectReservation): self
{
if ($this->optionSelectReservations->removeElement($optionSelectReservation)) {
// set the owning side to null (unless already changed)
if ($optionSelectReservation->getExtraReservation() === $this) {
$optionSelectReservation->setExtraReservation(null);
}
}
return $this;
}
public function isEmpty()
{
$isEmpty = true;
foreach($this->getOptionQuantityReservations() as $optionQuantityReservation) {
if($optionQuantityReservation->getQuantity() > 0) {
$isEmpty = false;
}
}
foreach($this->getOptionSelectReservations() as $optionSelectReservation) {
if($optionSelectReservation->getOptionSelectItem()) {
$isEmpty = false;
}
}
return $isEmpty;
}
public function getToken(): ?Token
{
return $this->token;
}
public function setToken(?Token $token): self
{
$this->token = $token;
return $this;
}
public function getReservation(): ?Reservation
{
return $this->reservation;
}
public function setReservation(?Reservation $reservation): self
{
$this->reservation = $reservation;
return $this;
}
public function getDetails(): string
{
$details = $this->getExtra()->getLabel();
$options = [];
foreach ($this->getOptionQuantityReservations() as $optionQuantityReservation) {
if($optionQuantityReservation->getQuantity() > 0) {
$option = $optionQuantityReservation->getOptionQuantity()->getLabel() . " x " . $optionQuantityReservation->getQuantity();
if($optionQuantityReservation->getCommentary()) {
$option .= " " . $optionQuantityReservation->getCommentary();
}
$options[] = $option;
}
}
foreach($this->getOptionSelectReservations() as $optionSelectReservation) {
$option = $optionSelectReservation->getOptionSelect()->getLabel() . " : ";
$option .= $optionSelectReservation->getOptionSelectItem()->getLabel();
if($optionSelectReservation->getCommentary()) {
$option .= " " . $optionQuantityReservation->getCommentary();
}
$options[] = $option;
}
if($options) {
$details .= "(s) : " . implode(", ", $options) . "";
}
return $details;
}
public function getDetailsAsArray($showCategory = true) : array
{
$extraCategory = $this->getExtra()->getLabel();
$options = [];
foreach ($this->getOptionQuantityReservations() as $optionQuantityReservation) {
if($optionQuantityReservation->getQuantity() > 0) {
if($showCategory) {
$option = $optionQuantityReservation->getQuantity() ." ". $extraCategory ." ". $optionQuantityReservation->getOptionQuantity()->getLabel();
} else {
$option = $optionQuantityReservation->getQuantity() ." ". $optionQuantityReservation->getOptionQuantity()->getLabel();
}
if($optionQuantityReservation->getCommentary()) {
$option .= " " . $optionQuantityReservation->getCommentary();
}
$options[] = strtolower($option);
}
}
// foreach($this->getOptionSelectReservations() as $optionSelectReservation) {
// $option = $optionSelectReservation->getOptionSelect()->getLabel() . " : ";
// $option .= $optionSelectReservation->getOptionSelectItem()->getLabel();
// if($optionSelectReservation->getCommentary()) {
// $option .= " " . $optionQuantityReservation->getCommentary();
// }
// $options[] = $option;
// }
return $options;
}
}