<?php
namespace App\Entity;
use App\Repository\OrderProductRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: OrderProductRepository::class)]
#[ORM\InheritanceType('SINGLE_TABLE')]
#[ORM\DiscriminatorColumn(name: 'discriminator', type: 'string')]
#[ORM\DiscriminatorMap([
'cadre' => Cadre::class,
'passe-partout' => PassePartout::class,
'caisse-americaine' => CaisseAmericaine::class,
'sujet' => Sujet::class,
'partial' => Partial::class,
])]
class OrderProduct
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
protected ?int $id = null;
#[ORM\Column(nullable: true)]
protected ?int $quantity = null;
#[ORM\Column(nullable: true)]
protected ?float $price = null;
#[ORM\Column(length: 255, nullable: true)]
protected ?string $imgUrl = null;
#[ORM\ManyToOne(targetEntity: ClientOrder::class, inversedBy: 'orderProducts')]
#[ORM\JoinColumn(nullable: false)]
protected ?ClientOrder $clientOrder = null;
#[ORM\Column(length: 255, nullable: true)]
protected ?string $refToSend = null;
public function __construct()
{
$this->quantity = 1;
}
public function getId(): ?int
{
return $this->id;
}
public function getQuantity(): ?int
{
return $this->quantity;
}
public function setQuantity(?int $quantity): static
{
$this->quantity = $quantity;
return $this;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(?float $price): static
{
$this->price = $price;
return $this;
}
public function getImgUrl(): ?string
{
return $this->imgUrl;
}
public function setImgUrl(?string $imgUrl): static
{
$this->imgUrl = $imgUrl;
return $this;
}
public function getClientOrder(): ?ClientOrder
{
return $this->clientOrder;
}
public function setClientOrder(?ClientOrder $clientOrder): static
{
$this->clientOrder = $clientOrder;
return $this;
}
public function getRefToSend(): ?string
{
return $this->refToSend;
}
public function setRefToSend(?string $refToSend): static
{
$this->refToSend = $refToSend;
return $this;
}
/**
* Extract reference from product name (fallback method)
*/
public function getReferenceNielsen(): ?string
{
$name = null;
if ($this instanceof Cadre || $this instanceof CaisseAmericaine || $this instanceof PassePartout || $this instanceof Partial) {
$name = $this->getName();
}
if ($name) {
preg_match('#\((.*?)\)#', $name, $variant);
if (!empty($variant)) {
return str_replace(["(", ")"], "", $variant[0]);
}
return $name;
}
return null;
}
/**
* Get the product type for cart system compatibility
*/
public function getProductType(): string
{
$discriminator = [
Cadre::class => 'cadre',
PassePartout::class => 'passe-partout',
CaisseAmericaine::class => 'caisse-americaine',
Sujet::class => 'sujet',
Partial::class => 'partial',
];
return $discriminator[static::class] ?? 'unknown';
}
}