src/Entity/ProductVariant.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ProductVariantRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\DBAL\Types\Types;
  7. use Doctrine\ORM\Mapping as ORM;
  8. #[ORM\Entity(repositoryClassProductVariantRepository::class)]
  9. #[ORM\Table(name'product_variants')]
  10. class ProductVariant
  11. {
  12.     #[ORM\Id]
  13.     #[ORM\GeneratedValue]
  14.     #[ORM\Column]
  15.     private ?int $id null;
  16.     #[ORM\ManyToOne(inversedBy'variants')]
  17.     #[ORM\JoinColumn(nullablefalseonDelete'CASCADE')]
  18.     private ?Product $product null;
  19.     #[ORM\Column(length255)]
  20.     private ?string $sku null;
  21.     #[ORM\Column(length255nullabletrue)]
  22.     private ?string $barcode null;
  23.     #[ORM\Column]
  24.     private ?float $price null;
  25.     #[ORM\Column(nullabletrue)]
  26.     private ?float $compareAtPrice null;
  27.     #[ORM\Column]
  28.     private ?int $stock 0;
  29.     #[ORM\Column]
  30.     private ?int $minStockAlert 0;
  31.     #[ORM\Column]
  32.     private ?bool $manageStock true;
  33.     #[ORM\Column]
  34.     private ?bool $allowBackorders false;
  35.     #[ORM\Column(length20)]
  36.     private ?string $stockStatus 'in_stock';
  37.     #[ORM\Column(nullabletrue)]
  38.     private ?float $weight null;
  39.     #[ORM\Column(nullabletrue)]
  40.     private ?float $length null;
  41.     #[ORM\Column(nullabletrue)]
  42.     private ?float $width null;
  43.     #[ORM\Column(nullabletrue)]
  44.     private ?float $height null;
  45.     #[ORM\Column]
  46.     private array $images = []; // Images spécifiques à cette variante
  47.     #[ORM\Column]
  48.     private ?bool $isActive true;
  49.     #[ORM\Column]
  50.     private ?bool $isDefault false// Variante par défaut
  51.     #[ORM\Column]
  52.     private ?int $sortOrder 0;
  53.     #[ORM\Column]
  54.     private ?\DateTimeImmutable $createdAt null;
  55.     #[ORM\Column]
  56.     private ?\DateTimeImmutable $updatedAt null;
  57.     #[ORM\ManyToMany(targetEntityProductAttributeValue::class, inversedBy'variants')]
  58.     #[ORM\JoinTable(name'product_variant_attribute_values')]
  59.     private Collection $attributeValues;
  60.     public function __construct()
  61.     {
  62.         $this->attributeValues = new ArrayCollection();
  63.         $this->createdAt = new \DateTimeImmutable();
  64.         $this->updatedAt = new \DateTimeImmutable();
  65.     }
  66.     public function getId(): ?int
  67.     {
  68.         return $this->id;
  69.     }
  70.     public function getProduct(): ?Product
  71.     {
  72.         return $this->product;
  73.     }
  74.     public function setProduct(?Product $product): static
  75.     {
  76.         $this->product $product;
  77.         return $this;
  78.     }
  79.     public function getSku(): ?string
  80.     {
  81.         return $this->sku;
  82.     }
  83.     public function setSku(string $sku): static
  84.     {
  85.         $this->sku $sku;
  86.         return $this;
  87.     }
  88.     public function getBarcode(): ?string
  89.     {
  90.         return $this->barcode;
  91.     }
  92.     public function setBarcode(?string $barcode): static
  93.     {
  94.         $this->barcode $barcode;
  95.         return $this;
  96.     }
  97.     public function getPrice(): ?float
  98.     {
  99.         return $this->price;
  100.     }
  101.     public function setPrice(float $price): static
  102.     {
  103.         $this->price $price;
  104.         return $this;
  105.     }
  106.     public function getCompareAtPrice(): ?float
  107.     {
  108.         return $this->compareAtPrice;
  109.     }
  110.     public function setCompareAtPrice(?float $compareAtPrice): static
  111.     {
  112.         $this->compareAtPrice $compareAtPrice;
  113.         return $this;
  114.     }
  115.     public function getStock(): ?int
  116.     {
  117.         return $this->stock;
  118.     }
  119.     public function setStock(int $stock): static
  120.     {
  121.         $this->stock $stock;
  122.         return $this;
  123.     }
  124.     public function getMinStockAlert(): ?int
  125.     {
  126.         return $this->minStockAlert;
  127.     }
  128.     public function setMinStockAlert(int $minStockAlert): static
  129.     {
  130.         $this->minStockAlert $minStockAlert;
  131.         return $this;
  132.     }
  133.     public function isManageStock(): ?bool
  134.     {
  135.         return $this->manageStock;
  136.     }
  137.     public function setManageStock(bool $manageStock): static
  138.     {
  139.         $this->manageStock $manageStock;
  140.         return $this;
  141.     }
  142.     public function isAllowBackorders(): ?bool
  143.     {
  144.         return $this->allowBackorders;
  145.     }
  146.     public function setAllowBackorders(bool $allowBackorders): static
  147.     {
  148.         $this->allowBackorders $allowBackorders;
  149.         return $this;
  150.     }
  151.     public function getStockStatus(): ?string
  152.     {
  153.         return $this->stockStatus;
  154.     }
  155.     public function setStockStatus(string $stockStatus): static
  156.     {
  157.         $this->stockStatus $stockStatus;
  158.         return $this;
  159.     }
  160.     public function getWeight(): ?float
  161.     {
  162.         return $this->weight;
  163.     }
  164.     public function setWeight(?float $weight): static
  165.     {
  166.         $this->weight $weight;
  167.         return $this;
  168.     }
  169.     public function getLength(): ?float
  170.     {
  171.         return $this->length;
  172.     }
  173.     public function setLength(?float $length): static
  174.     {
  175.         $this->length $length;
  176.         return $this;
  177.     }
  178.     public function getWidth(): ?float
  179.     {
  180.         return $this->width;
  181.     }
  182.     public function setWidth(?float $width): static
  183.     {
  184.         $this->width $width;
  185.         return $this;
  186.     }
  187.     public function getHeight(): ?float
  188.     {
  189.         return $this->height;
  190.     }
  191.     public function setHeight(?float $height): static
  192.     {
  193.         $this->height $height;
  194.         return $this;
  195.     }
  196.     public function getImages(): array
  197.     {
  198.         return $this->images;
  199.     }
  200.     public function setImages(array $images): static
  201.     {
  202.         $this->images $images;
  203.         return $this;
  204.     }
  205.     public function isIsActive(): ?bool
  206.     {
  207.         return $this->isActive;
  208.     }
  209.     public function setIsActive(bool $isActive): static
  210.     {
  211.         $this->isActive $isActive;
  212.         return $this;
  213.     }
  214.     public function isIsDefault(): ?bool
  215.     {
  216.         return $this->isDefault;
  217.     }
  218.     public function setIsDefault(bool $isDefault): static
  219.     {
  220.         $this->isDefault $isDefault;
  221.         return $this;
  222.     }
  223.     public function getSortOrder(): ?int
  224.     {
  225.         return $this->sortOrder;
  226.     }
  227.     public function setSortOrder(int $sortOrder): static
  228.     {
  229.         $this->sortOrder $sortOrder;
  230.         return $this;
  231.     }
  232.     public function getCreatedAt(): ?\DateTimeImmutable
  233.     {
  234.         return $this->createdAt;
  235.     }
  236.     public function setCreatedAt(\DateTimeImmutable $createdAt): static
  237.     {
  238.         $this->createdAt $createdAt;
  239.         return $this;
  240.     }
  241.     public function getUpdatedAt(): ?\DateTimeImmutable
  242.     {
  243.         return $this->updatedAt;
  244.     }
  245.     public function setUpdatedAt(\DateTimeImmutable $updatedAt): static
  246.     {
  247.         $this->updatedAt $updatedAt;
  248.         return $this;
  249.     }
  250.     /**
  251.      * @return Collection<int, ProductAttributeValue>
  252.      */
  253.     public function getAttributeValues(): Collection
  254.     {
  255.         return $this->attributeValues;
  256.     }
  257.     public function addAttributeValue(ProductAttributeValue $attributeValue): static
  258.     {
  259.         if (!$this->attributeValues->contains($attributeValue)) {
  260.             $this->attributeValues->add($attributeValue);
  261.         }
  262.         return $this;
  263.     }
  264.     public function removeAttributeValue(ProductAttributeValue $attributeValue): static
  265.     {
  266.         $this->attributeValues->removeElement($attributeValue);
  267.         return $this;
  268.     }
  269.     /**
  270.      * Obtenir une représentation textuelle de la variante (ex: "Rouge - XL")
  271.      */
  272.     public function getDisplayName(): string
  273.     {
  274.         $parts = [];
  275.         foreach ($this->attributeValues as $value) {
  276.             $parts[] = $value->getValue();
  277.         }
  278.         return implode(' - '$parts);
  279.     }
  280.     /**
  281.      * Vérifier si la variante est en stock
  282.      */
  283.     public function isInStock(): bool
  284.     {
  285.         if (!$this->isActive) {
  286.             return false;
  287.         }
  288.         if ($this->stockStatus === 'out_of_stock') {
  289.             return false;
  290.         }
  291.         if ($this->manageStock && $this->stock <= && !$this->allowBackorders) {
  292.             return false;
  293.         }
  294.         return true;
  295.     }
  296. }