<?php
namespace App\Entity;
use App\Repository\ClientOrderRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: ClientOrderRepository::class)]
class ClientOrder
{
/**
* Commande non complète
*/
const STATUS_DRAFT = 'draft';
/**
* Commande créée, en attente d'être envoyé à Nielsen (nielsenOrder = 0)
*/
const STATUS_TOSEND = 'tosend';
/**
* Commande envoyée à Nielsen (nielsenOrder > 0)
*/
const STATUS_SENT = 'sent';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(inversedBy: 'clientOrders')]
private ?Project $project = null;
#[ORM\OneToMany(mappedBy: 'clientOrder', targetEntity: OrderProduct::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
private Collection $orderProducts;
// Legacy relationships - keep for backward compatibility but marked as deprecated
/** @deprecated Use orderProducts collection instead */
#[ORM\OneToOne(mappedBy: 'clientOrder', cascade: ['persist', 'remove'])]
private ?Cadre $cadre = null;
/** @deprecated Use orderProducts collection instead */
#[ORM\OneToOne(mappedBy: 'clientOrder', cascade: ['persist', 'remove'])]
private ?PassePartout $passePartout = null;
/** @deprecated Use orderProducts collection instead */
#[ORM\OneToOne(mappedBy: 'clientOrder', cascade: ['persist', 'remove'])]
private ?CaisseAmericaine $caisseAmericaine = null;
/** @deprecated Use orderProducts collection instead */
#[ORM\OneToOne(mappedBy: 'clientOrder', cascade: ['persist', 'remove'])]
private ?Partial $partial = null;
/** @deprecated Use orderProducts collection instead */
#[ORM\OneToOne(mappedBy: 'clientOrder', cascade: ['persist', 'remove'])]
private ?Sujet $sujet = null;
#[ORM\Column(nullable: true)]
private ?float $price = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $created = null;
#[ORM\ManyToMany(targetEntity: NielsenOrder::class, mappedBy: 'clientOrders')]
private Collection $nielsenOrders;
#[ORM\Column(length: 255, nullable: true)]
private ?string $save_link = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $reference = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $client_reference = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $sellerName = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $refToSend = null;
#[ORM\ManyToOne(inversedBy: 'clientOrders', cascade: ["persist"])]
private ?Internaute $internaute = null;
#[ORM\OneToMany(mappedBy: 'clientOrder', targetEntity: Mail::class)]
private Collection $mails;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $comment = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $receivedByShopAt = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $clientContactedAt = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $productDeliveredToClientAt = null;
#[ORM\OneToMany(mappedBy: 'clientOrder', targetEntity: TinyUrl::class, cascade: ['persist', 'remove'])]
private Collection $tinyUrls;
#[ORM\Column(length: 255, nullable: true)]
private ?string $status = null;
#[ORM\Column(nullable: true)]
private ?float $priceNetRefToSend = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $orderToken = null;
#[ORM\Column(nullable: true)]
private ?bool $isArchived = null;
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $archivedAt = null;
/**
* Temporary property to store variants, not persisted to database
*/
public ?array $variants = null;
public function __construct()
{
$this->nielsenOrders = new ArrayCollection();
$this->created = new \DateTimeImmutable();
$this->mails = new ArrayCollection();
$this->tinyUrls = new ArrayCollection();
$this->orderProducts = new ArrayCollection();
$this->isArchived = false;
}
public function getAppConfig(): array
{
return ($this->getTinyUrls()->last() ?: null)
?->getAppConfig()
?? [];
}
public function getId(): ?int
{
return $this->id;
}
public function getProject(): ?Project
{
return $this->project;
}
public function getHauteur(): ?int
{
if ($this->orderProducts->count() > 0) {
$firstProduct = $this->orderProducts->first();
if ($firstProduct instanceof Cadre || $firstProduct instanceof CaisseAmericaine) {
return $firstProduct->getHauteur();
} elseif ($firstProduct instanceof PassePartout) {
return $firstProduct->getHauteurExterieur();
}
}
return $this->cadre?->getHauteur() ?? $this->caisseAmericaine?->getHauteur() ?? $this->passePartout?->getHauteurExterieur();
}
public function getLargeur(): ?int
{
if ($this->orderProducts->count() > 0) {
$firstProduct = $this->orderProducts->first();
if ($firstProduct instanceof Cadre || $firstProduct instanceof CaisseAmericaine) {
return $firstProduct->getLargeur();
} elseif ($firstProduct instanceof PassePartout) {
return $firstProduct->getLargeurExterieur();
}
}
return $this->cadre?->getLargeur() ?? $this->caisseAmericaine?->getLargeur() ?? $this->passePartout?->getLargeurExterieur();
}
public function setProject(?Project $project): static
{
$this->project = $project;
return $this;
}
/**
* @return Collection<int, OrderProduct>
*/
public function getOrderProducts(): Collection
{
return $this->orderProducts;
}
public function addOrderProduct(OrderProduct $orderProduct): static
{
if (!$this->orderProducts->contains($orderProduct)) {
$this->orderProducts->add($orderProduct);
$orderProduct->setClientOrder($this);
}
return $this;
}
public function removeOrderProduct(OrderProduct $orderProduct): static
{
if ($this->orderProducts->removeElement($orderProduct)) {
// set the owning side to null (unless already changed)
if ($orderProduct->getClientOrder() === $this) {
$orderProduct->setClientOrder(null);
}
}
return $this;
}
public function isMultiItemOrder(): bool
{
return $this->orderProducts->count() > 1;
}
public function getCalculatedPrice(): float
{
$total = 0;
foreach ($this->orderProducts as $product) {
$total += ($product->getPrice() ?? 0) * ($product->getQuantity() ?? 1);
}
return $total;
}
public function getCadres(): array
{
return array_values($this->orderProducts->filter(fn($product) => $product instanceof Cadre)->toArray());
}
public function getPassePartouts(): array
{
return array_values($this->orderProducts->filter(fn($product) => $product instanceof PassePartout)->toArray());
}
public function getCaisseAmericaines(): array
{
return array_values($this->orderProducts->filter(fn($product) => $product instanceof CaisseAmericaine)->toArray());
}
public function getSujets(): array
{
return array_values($this->orderProducts->filter(fn($product) => $product instanceof Sujet)->toArray());
}
public function getPartials(): array
{
return array_values($this->orderProducts->filter(fn($product) => $product instanceof Partial)->toArray());
}
public function getCadre(): ?Cadre
{
$cadres = $this->getCadres();
if (!empty($cadres)) {
return $cadres[0];
}
return $this->cadre;
}
public function setCadre(?Cadre $cadre): static
{
// unset the owning side of the relation if necessary
if ($cadre === null && $this->cadre !== null) {
$this->cadre->setClientOrder(null);
}
// set the owning side of the relation if necessary
if ($cadre !== null && $cadre->getClientOrder() !== $this) {
$cadre->setClientOrder($this);
}
$this->cadre = $cadre;
return $this;
}
public function getPassePartout(): ?PassePartout
{
$pp = $this->getPassePartouts();
if (!empty($pp)) {
return $pp[0];
}
return $this->passePartout;
}
public function setPassePartout(?PassePartout $passePartout): static
{
// unset the owning side of the relation if necessary
if ($passePartout === null && $this->passePartout !== null) {
$this->passePartout->setClientOrder(null);
}
// set the owning side of the relation if necessary
if ($passePartout !== null && $passePartout->getClientOrder() !== $this) {
$passePartout->setClientOrder($this);
}
$this->passePartout = $passePartout;
return $this;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(?float $price): static
{
$this->price = $price;
return $this;
}
public function getCreated(): ?\DateTimeInterface
{
return $this->created;
}
public function setCreated(\DateTimeInterface $created): static
{
$this->created = $created;
return $this;
}
/**
* @return Collection<int, NielsenOrder>
*/
public function getNielsenOrders(): Collection
{
return $this->nielsenOrders;
}
public function addNielsenOrder(NielsenOrder $nielsenOrder): static
{
if (!$this->nielsenOrders->contains($nielsenOrder)) {
$this->nielsenOrders->add($nielsenOrder);
$nielsenOrder->addClientOrder($this);
}
return $this;
}
public function removeNielsenOrder(NielsenOrder $nielsenOrder): static
{
if ($this->nielsenOrders->removeElement($nielsenOrder)) {
$nielsenOrder->removeClientOrder($this);
}
return $this;
}
public function getSujet(): ?Sujet
{
$sujets = $this->getSujets();
if (!empty($sujets)) {
return $sujets[0];
}
return $this->sujet;
}
public function setSujet(?Sujet $sujet): static
{
// unset the owning side of the relation if necessary
if ($sujet === null && $this->sujet !== null) {
$this->sujet->setClientOrder(null);
}
// set the owning side of the relation if necessary
if ($sujet !== null && $sujet->getClientOrder() !== $this) {
$sujet->setClientOrder($this);
}
$this->sujet = $sujet;
return $this;
}
public function getSaveCode(): string
{
$explodedSaveLink = explode("=", $this->save_link ?? "");
return end($explodedSaveLink) ?? "";
}
public function getSaveLink(): ?string
{
return $this->save_link;
}
public function setSaveLink(?string $save_link): static
{
$this->save_link = $save_link;
return $this;
}
public function getReference(): ?string
{
return $this->reference;
}
public function setReference(?string $reference): static
{
$this->reference = $reference;
return $this;
}
public function getClientReference(): ?string
{
return $this->client_reference;
}
public function setClientReference(?string $client_reference): static
{
$this->client_reference = $client_reference;
return $this;
}
public function getInternaute(): ?Internaute
{
return $this->internaute;
}
public function setInternaute(?Internaute $internaute): static
{
$this->internaute = $internaute;
return $this;
}
/**
* @return Collection<int, Mail>
*/
public function getMails(): Collection
{
return $this->mails;
}
public function addMail(Mail $mail): static
{
if (!$this->mails->contains($mail)) {
$this->mails->add($mail);
$mail->setClientOrder($this);
}
return $this;
}
public function removeMail(Mail $mail): static
{
if ($this->mails->removeElement($mail)) {
// set the owning side to null (unless already changed)
if ($mail->getClientOrder() === $this) {
$mail->setClientOrder(null);
}
}
return $this;
}
public function getComment(): ?string
{
return $this->comment;
}
public function setComment(?string $comment): static
{
$this->comment = $comment;
return $this;
}
public function getCaisseAmericaine(): ?CaisseAmericaine
{
$ca = $this->getCaisseAmericaines();
if (!empty($ca)) {
return $ca[0];
}
return $this->caisseAmericaine;
}
public function setCaisseAmericaine(?CaisseAmericaine $caisseAmericaine): static
{
// unset the owning side of the relation if necessary
if ($caisseAmericaine === null && $this->caisseAmericaine !== null) {
$this->caisseAmericaine->setClientOrder(null);
}
// set the owning side of the relation if necessary
if ($caisseAmericaine !== null && $caisseAmericaine->getClientOrder() !== $this) {
$caisseAmericaine->setClientOrder($this);
}
$this->caisseAmericaine = $caisseAmericaine;
return $this;
}
public function getPartial(): ?Partial
{
$partials = $this->getPartials();
if (!empty($partials)) {
return $partials[0];
}
return $this->partial;
}
public function setPartial(?Partial $partial): static
{
// unset the owning side of the relation if necessary
if ($partial === null && $this->partial !== null) {
$this->partial->setClientOrder(null);
}
// set the owning side of the relation if necessary
if ($partial !== null && $partial->getClientOrder() !== $this) {
$partial->setClientOrder($this);
}
$this->partial = $partial;
return $this;
}
public function getReceivedByShopAt(): ?\DateTimeInterface
{
return $this->receivedByShopAt;
}
public function setReceivedByShopAt(?\DateTimeInterface $receivedByShopAt): static
{
$this->receivedByShopAt = $receivedByShopAt;
return $this;
}
public function getClientContactedAt(): ?\DateTimeInterface
{
return $this->clientContactedAt;
}
public function setClientContactedAt(?\DateTimeInterface $clientContactedAt): static
{
$this->clientContactedAt = $clientContactedAt;
return $this;
}
public function getProductDeliveredToClientAt(): ?\DateTimeInterface
{
return $this->productDeliveredToClientAt;
}
public function setProductDeliveredToClientAt(?\DateTimeInterface $productDeliveredToClientAt): static
{
$this->productDeliveredToClientAt = $productDeliveredToClientAt;
return $this;
}
public function getSellerName(): ?string
{
return $this->sellerName;
}
public function setSellerName(?string $sellerName): self
{
$this->sellerName = $sellerName;
return $this;
}
/**
* @return Collection<int, TinyUrl>
*/
public function getTinyUrls(): Collection
{
return $this->tinyUrls;
}
public function addTinyUrl(TinyUrl $tinyUrl): static
{
if (!$this->tinyUrls->contains($tinyUrl)) {
$this->tinyUrls->add($tinyUrl);
$tinyUrl->setClientOrder($this);
}
return $this;
}
public function removeTinyUrl(TinyUrl $tinyUrl): static
{
if ($this->tinyUrls->removeElement($tinyUrl)) {
// set the owning side to null (unless already changed)
if ($tinyUrl->getClientOrder() === $this) {
$tinyUrl->setClientOrder(null);
}
}
return $this;
}
public function getRefToSend(): ?string
{
return $this->refToSend;
}
public function setRefToSend(?string $refToSend): self
{
$this->refToSend = $refToSend;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(?string $status): static
{
$this->status = $status;
return $this;
}
public function getPriceNetRefToSend(): ?float
{
return $this->priceNetRefToSend;
}
public function setPriceNetRefToSend(?float $priceNetRefToSend): static
{
$this->priceNetRefToSend = $priceNetRefToSend;
return $this;
}
public function getOrderToken(): ?string
{
return $this->orderToken;
}
public function setOrderToken(?string $orderToken): static
{
$this->orderToken = $orderToken;
return $this;
}
public function isIsArchived(): ?bool
{
return $this->isArchived;
}
public function setIsArchived(bool $isArchived): static
{
$this->isArchived = $isArchived;
return $this;
}
public function getArchivedAt(): ?\DateTimeImmutable
{
return $this->archivedAt;
}
public function setArchivedAt(?\DateTimeImmutable $archivedAt): static
{
$this->archivedAt = $archivedAt;
return $this;
}
public function getVariants(): ?array
{
return $this->variants;
}
public function setVariants(?array $variants): static
{
$this->variants = $variants;
return $this;
}
}