<?phpnamespace App\Entity;use App\Repository\ProductVariantRepository;use Doctrine\Common\Collections\ArrayCollection;use Doctrine\Common\Collections\Collection;use Doctrine\DBAL\Types\Types;use Doctrine\ORM\Mapping as ORM;#[ORM\Entity(repositoryClass: ProductVariantRepository::class)]#[ORM\Table(name: 'product_variants')]class ProductVariant{ #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; #[ORM\ManyToOne(inversedBy: 'variants')] #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')] private ?Product $product = null; #[ORM\Column(length: 255)] private ?string $sku = null; #[ORM\Column(length: 255, nullable: true)] private ?string $barcode = null; #[ORM\Column] private ?float $price = null; #[ORM\Column(nullable: true)] private ?float $compareAtPrice = null; #[ORM\Column] private ?int $stock = 0; #[ORM\Column] private ?int $minStockAlert = 0; #[ORM\Column] private ?bool $manageStock = true; #[ORM\Column] private ?bool $allowBackorders = false; #[ORM\Column(length: 20)] private ?string $stockStatus = 'in_stock'; #[ORM\Column(nullable: true)] private ?float $weight = null; #[ORM\Column(nullable: true)] private ?float $length = null; #[ORM\Column(nullable: true)] private ?float $width = null; #[ORM\Column(nullable: true)] private ?float $height = null; #[ORM\Column] private array $images = []; // Images spécifiques à cette variante #[ORM\Column] private ?bool $isActive = true; #[ORM\Column] private ?bool $isDefault = false; // Variante par défaut #[ORM\Column] private ?int $sortOrder = 0; #[ORM\Column] private ?\DateTimeImmutable $createdAt = null; #[ORM\Column] private ?\DateTimeImmutable $updatedAt = null; #[ORM\ManyToMany(targetEntity: ProductAttributeValue::class, inversedBy: 'variants')] #[ORM\JoinTable(name: 'product_variant_attribute_values')] private Collection $attributeValues; public function __construct() { $this->attributeValues = new ArrayCollection(); $this->createdAt = new \DateTimeImmutable(); $this->updatedAt = new \DateTimeImmutable(); } public function getId(): ?int { return $this->id; } public function getProduct(): ?Product { return $this->product; } public function setProduct(?Product $product): static { $this->product = $product; return $this; } public function getSku(): ?string { return $this->sku; } public function setSku(string $sku): static { $this->sku = $sku; return $this; } public function getBarcode(): ?string { return $this->barcode; } public function setBarcode(?string $barcode): static { $this->barcode = $barcode; return $this; } public function getPrice(): ?float { return $this->price; } public function setPrice(float $price): static { $this->price = $price; return $this; } public function getCompareAtPrice(): ?float { return $this->compareAtPrice; } public function setCompareAtPrice(?float $compareAtPrice): static { $this->compareAtPrice = $compareAtPrice; return $this; } public function getStock(): ?int { return $this->stock; } public function setStock(int $stock): static { $this->stock = $stock; return $this; } public function getMinStockAlert(): ?int { return $this->minStockAlert; } public function setMinStockAlert(int $minStockAlert): static { $this->minStockAlert = $minStockAlert; return $this; } public function isManageStock(): ?bool { return $this->manageStock; } public function setManageStock(bool $manageStock): static { $this->manageStock = $manageStock; return $this; } public function isAllowBackorders(): ?bool { return $this->allowBackorders; } public function setAllowBackorders(bool $allowBackorders): static { $this->allowBackorders = $allowBackorders; return $this; } public function getStockStatus(): ?string { return $this->stockStatus; } public function setStockStatus(string $stockStatus): static { $this->stockStatus = $stockStatus; return $this; } public function getWeight(): ?float { return $this->weight; } public function setWeight(?float $weight): static { $this->weight = $weight; return $this; } public function getLength(): ?float { return $this->length; } public function setLength(?float $length): static { $this->length = $length; return $this; } public function getWidth(): ?float { return $this->width; } public function setWidth(?float $width): static { $this->width = $width; return $this; } public function getHeight(): ?float { return $this->height; } public function setHeight(?float $height): static { $this->height = $height; return $this; } public function getImages(): array { return $this->images; } public function setImages(array $images): static { $this->images = $images; return $this; } public function isIsActive(): ?bool { return $this->isActive; } public function setIsActive(bool $isActive): static { $this->isActive = $isActive; return $this; } public function isIsDefault(): ?bool { return $this->isDefault; } public function setIsDefault(bool $isDefault): static { $this->isDefault = $isDefault; return $this; } public function getSortOrder(): ?int { return $this->sortOrder; } public function setSortOrder(int $sortOrder): static { $this->sortOrder = $sortOrder; return $this; } public function getCreatedAt(): ?\DateTimeImmutable { return $this->createdAt; } public function setCreatedAt(\DateTimeImmutable $createdAt): static { $this->createdAt = $createdAt; return $this; } public function getUpdatedAt(): ?\DateTimeImmutable { return $this->updatedAt; } public function setUpdatedAt(\DateTimeImmutable $updatedAt): static { $this->updatedAt = $updatedAt; return $this; } /** * @return Collection<int, ProductAttributeValue> */ public function getAttributeValues(): Collection { return $this->attributeValues; } public function addAttributeValue(ProductAttributeValue $attributeValue): static { if (!$this->attributeValues->contains($attributeValue)) { $this->attributeValues->add($attributeValue); } return $this; } public function removeAttributeValue(ProductAttributeValue $attributeValue): static { $this->attributeValues->removeElement($attributeValue); return $this; } /** * Obtenir une représentation textuelle de la variante (ex: "Rouge - XL") */ public function getDisplayName(): string { $parts = []; foreach ($this->attributeValues as $value) { $parts[] = $value->getValue(); } return implode(' - ', $parts); } /** * Vérifier si la variante est en stock */ public function isInStock(): bool { if (!$this->isActive) { return false; } if ($this->stockStatus === 'out_of_stock') { return false; } if ($this->manageStock && $this->stock <= 0 && !$this->allowBackorders) { return false; } return true; }}