<?php
namespace App\Entity;
use App\Repository\CommandeRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: CommandeRepository::class)]
class Commande
{
const COMMANDE_STATUS_NOT_DEFINED = 'non défini';
const COMMANDE_STATUS_SAISIE = 'saisie';
const COMMANDE_STATUS_DESACTIVER = 'desactiver';
const COMMANDE_STATUS_ATTRIBUER = 'attribuer';
const COMMANDE_STATUS_EDITER = 'editer';
const COMMANDE_STATUS_LIVRER = 'livrer';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne]
private ?Events $event = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: false)]
private ?Session $session = null;
#[ORM\ManyToOne]
private ?Cour $cour = null;
#[ORM\ManyToOne]
private ?Category $category = null;
#[ORM\Column(nullable: true)]
private ?int $nbr = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $groupement = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $statut = null;
#[ORM\Column(nullable: true)]
private ?float $prix_unitaire = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $note = null;
#[ORM\Column(nullable: true)]
private ?bool $option_commande = null;
#[ORM\ManyToOne(cascade: ['persist'], inversedBy: 'commandes')]
#[ORM\JoinColumn(nullable: false)]
private ?Client $client = null;
#[ORM\OneToMany(mappedBy: 'commande', targetEntity: Attribution::class, orphanRemoval: true)]
private $attributions;
#[ORM\OneToMany(mappedBy: 'commande', targetEntity: Spectateur::class, orphanRemoval: true)]
private Collection $spectateurs;
#[ORM\OneToMany(mappedBy: 'commande', targetEntity: CommandeActions::class, orphanRemoval: true)]
private Collection $commandeActions;
#[ORM\Column(nullable: true)]
private ?bool $isPapier = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $contactEmail = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $noteCommande = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $noteClient = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $noteSpectateur = null;
public function __construct()
{
$this->statut = self::COMMANDE_STATUS_SAISIE;
$this->attributions = new ArrayCollection();
$this->spectateurs = new ArrayCollection();
$this->commandeActions = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEvent(): ?Events
{
return $this->event;
}
public function setEvent(?Events $event): self
{
$this->event = $event;
return $this;
}
public function getSession(): ?Session
{
return $this->session;
}
public function setSession(?Session $session): self
{
$this->session = $session;
return $this;
}
public function getCour(): ?Cour
{
return $this->cour;
}
public function setCour(?Cour $cour): self
{
$this->cour = $cour;
return $this;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): self
{
$this->category = $category;
return $this;
}
public function getNbr(): ?int
{
return $this->nbr;
}
public function setNbr(?int $nbr): self
{
$this->nbr = $nbr;
return $this;
}
public function getGroupement(): ?string
{
return $this->groupement;
}
public function setGroupement(?string $groupement): self
{
$this->groupement = $groupement;
return $this;
}
public function getStatut(): ?string
{
return $this->statut;
}
public function setStatut(?string $statut): self
{
$this->statut = $statut;
return $this;
}
public function getPrixUnitaire(): ?float
{
return $this->prix_unitaire;
}
public function setPrixUnitaire(?float $prix_unitaire): self
{
$this->prix_unitaire = $prix_unitaire;
return $this;
}
public function getNote(): ?string
{
return $this->note;
}
public function setNote(?string $note): self
{
$this->note = $note;
return $this;
}
public function isOptionCommande(): ?bool
{
return $this->option_commande;
}
public function setOptionCommande(?bool $option_commande): self
{
$this->option_commande = $option_commande;
return $this;
}
public function getClient(): ?Client
{
return $this->client;
}
public function setClient(?Client $client): self
{
$this->client = $client;
return $this;
}
public function getTotal(): float
{
return $this->prix_unitaire * $this->nbr;
}
public function __toString(): string
{
// TODO: Implement __toString() method.
return $this->getSession()->getCodeSession();
}
/**
* @return Collection|Attribution[]
*/
public function getAttributions(): Collection
{
return $this->attributions;
}
public function addAttribution(Attribution $attribution): self
{
if (!$this->attributions->contains($attribution)) {
$this->attributions[] = $attribution;
$attribution->setCommande($this);
}
return $this;
}
public function removeAttribution(Attribution $attribution): self
{
if ($this->attributions->contains($attribution)) {
$this->attributions->removeElement($attribution);
// set the owning side to null (unless already changed)
if ($attribution->getCommande() === $this) {
$attribution->setCommande(null);
}
}
return $this;
}
/**
* @return Collection<int, Spectateur>
*/
public function getSpectateurs(): Collection
{
return $this->spectateurs;
}
public function addSpectateur(Spectateur $spectateur): self
{
if (!$this->spectateurs->contains($spectateur)) {
$this->spectateurs->add($spectateur);
$spectateur->setCommande($this);
}
return $this;
}
public function removeSpectateur(Spectateur $spectateur): self
{
if ($this->spectateurs->removeElement($spectateur)) {
// set the owning side to null (unless already changed)
if ($spectateur->getCommande() === $this) {
$spectateur->setCommande(null);
}
}
return $this;
}
/**
* @return Collection<int, CommandeActions>
*/
public function getCommandeActions(): array
{
return array_reverse($this->commandeActions->toArray());
}
public function addCommandeAction(CommandeActions $commandeAction): self
{
if (!$this->commandeActions->contains($commandeAction)) {
$this->commandeActions->add($commandeAction);
$commandeAction->setCommande($this);
}
return $this;
}
public function removeCommandeAction(CommandeActions $commandeAction): self
{
if ($this->commandeActions->removeElement($commandeAction)) {
// set the owning side to null (unless already changed)
if ($commandeAction->getCommande() === $this) {
$commandeAction->setCommande(null);
}
}
return $this;
}
public function isIsPapier(): ?bool
{
return $this->isPapier;
}
public function setIsPapier(?bool $isPapier): self
{
$this->isPapier = $isPapier;
return $this;
}
public function getContactEmail(): ?string
{
return $this->contactEmail;
}
public function setContactEmail(?string $contactEmail): self
{
$this->contactEmail = $contactEmail;
return $this;
}
public function getNoteCommande(): ?string
{
return $this->noteCommande;
}
public function setNoteCommande(?string $noteCommande): self
{
$this->noteCommande = $noteCommande;
return $this;
}
public function getNoteClient(): ?string
{
return $this->noteClient;
}
public function setNoteClient(?string $noteClient): self
{
$this->noteClient = $noteClient;
return $this;
}
public function getNoteSpectateur(): ?string
{
return $this->noteSpectateur;
}
public function setNoteSpectateur(?string $noteSpectateur): self
{
$this->noteSpectateur = $noteSpectateur;
return $this;
}
public function getFormSpectateurs(): array
{
$spectateurs = array_values(
array_filter(
array_map(
function ($spectateur) {
if ($spectateur->isIsActive())
return [
'nom' => $spectateur->getNom()
];
return null;
},
$this->getSpectateurs()->toArray()
),
function ($value) {
return $value !== null;
}
)
);
$result = [];
for ($i = 1; $i <= $this->nbr; $i++) {
if (count($spectateurs) >= $i) $result[] = $spectateurs[($i - 1)]['nom'];
else $result[$i] = '';
}
return $result;
}
public function getHistoriqueSpectateur(): array
{
return array_values(
array_filter(
array_map(
function ($spectateur) {
return [
'id' => $spectateur->getId(),
'nom' => $spectateur->getNom(),
'date' => $spectateur->getLocalizedDate()
];
},
$this->getSpectateurs()->toArray()
),
function ($value) {
return $value !== null;
}
)
);
}
public function getAllSpectateurToEmail(): ?string
{
$result = '<ul>';
/** @var Spectateur $spectateur */
foreach ($this->getAttributions() as $attribution) {
$spectateur = $attribution->getSpectateur();
$result .= '<li>' . $spectateur->getNom() . '</li>';
}
$result .= '</ul>';
return $result;
}
public function getGroupedSpectateur(): array
{
$resultat = [];
$nbrs = explode(',', $this->groupement);
$attributions = $this->getAttributions()->toArray();
$i = 0;
$limit = 0;
foreach ($nbrs as $index => $nbr) {
$limit += $nbr;
while ($i < $limit) {
if(isset($attributions[$i])) {
$resultat['groupe_' . $index][] = $attributions[$i];
}
$i++;
}
}
return $resultat;
}
public function getToAttribute(): ?Attribution
{
foreach ($this->getAttributions() as $attribution) {
if(is_null($attribution->getBillet())) return $attribution;
}
return null;
}
public function getNbNoms(): string
{
$saisie = 0;
$spectateurs = 0;
/** @var Spectateur $spectateur */
foreach ($this->getAttributions() as $attribution) {
$spectateur = $attribution->getSpectateur();
$spectateurs++;
if($spectateur->getNom() !== ' ' && $spectateur->getNom() !== '' && !is_null($spectateur->getNom())) $saisie ++;
}
return $saisie.' / '.$spectateurs;
}
public function getNbrBilletSameStatus(): int
{
$count = 0;
/** @var Attribution $attribution */
foreach ($this->attributions as $attribution) {
if($attribution->getStatut() == $this->statut) $count++;
}
return $count;
}
public function getNoms(): string
{
$spectateurs = $this->getSpectateurs();
$nb = 0;
foreach ($spectateurs as $spectateur) {
if($spectateur->getNom() != " " && $spectateur->getNom() != "") $nb++;
}
return $nb.' / '.count($spectateurs);
}
public function getStock(): string
{
$stock = 0;
foreach ($this->attributions as $attribution) {
if(is_null($attribution->getBillet())) $stock++;
}
return $stock.' / '.count($this->attributions);
}
}