src/Flexy/ShopBundle/Entity/Product/AttributValue.php line 19

  1. <?php
  2. namespace App\Flexy\ShopBundle\Entity\Product;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Flexy\ShopBundle\Repository\Product\AttributValueRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Serializer\Annotation\Groups;
  9. #[ApiResource(
  10.     normalizationContext: ['groups' => ['read']],
  11.     denormalizationContext: ['groups' => ['write']],
  12.     
  13.     )]
  14. #[ORM\Entity(repositoryClassAttributValueRepository::class)]
  15. #[ORM\Cache(usage:"NONSTRICT_READ_WRITE",region:"append_depend")]
  16. class AttributValue implements \Stringable
  17. {
  18.     #[ORM\Id]
  19.     #[ORM\GeneratedValue]
  20.     #[ORM\Column(type'integer')]
  21.     #[Groups(['read''write'])]
  22.     private $id;
  23.     #[Groups(['read''write'])]
  24.     #[ORM\Column(type'text'nullabletrue)]
  25.     private ?string $description null;
  26.     #[ORM\Column(type'datetime_immutable'nullabletrue)]
  27.     private ?\DateTimeImmutable $createdAt null;
  28.     #[ORM\ManyToOne(targetEntityAttribut::class, inversedBy'attributValues',cascade:[ "persist" ])]
  29.     #[Groups(['read''write'])]
  30.     private ?\App\Flexy\ShopBundle\Entity\Product\Attribut $attribut null;
  31.     #[ORM\Column(type'string'length255,nullable:true)]
  32.     private ?string $value null;
  33.     #[ORM\ManyToOne(targetEntityProduct::class, inversedBy'attributValues'cascade: ['persist'])]
  34.     private ?\App\Flexy\ShopBundle\Entity\Product\Product $product null;
  35.     #[ORM\Column(type'float'nullabletrue)]
  36.     #[Groups(['read''write'])]
  37.     private ?float $price null;
  38.     #[ORM\Column(type'integer'nullabletrue)]
  39.     #[Groups(['read''write'])]
  40.     private ?int $quantity null;
  41.     #[ORM\ManyToMany(targetEntityProductVariant::class, mappedBy'attributeValues')]
  42.     private \Doctrine\Common\Collections\Collection|array $productVariants;
  43.     #[ORM\Column(length255nullabletrue)]
  44.     private ?string $image null;
  45.     #[ORM\Column(length255nullabletrue)]
  46.     private ?string $skuCode null;
  47.   
  48.     #[ORM\Column(nullabletrue)]
  49.     private ?float $costPrice null;
  50.     public function __construct()
  51.     {
  52.         $this->productVariants = new ArrayCollection();
  53.     }
  54.     public function __toString(): string
  55.     {
  56.         return $this->attribut ." : "$this->value;
  57.     }
  58.     public function getId(): ?int
  59.     {
  60.         return $this->id;
  61.     }
  62.     public function getDescription(): ?string
  63.     {
  64.         return $this->description;
  65.     }
  66.     public function setDescription(?string $description): self
  67.     {
  68.         $this->description $description;
  69.         return $this;
  70.     }
  71.     public function getCreatedAt(): ?\DateTimeImmutable
  72.     {
  73.         return $this->createdAt;
  74.     }
  75.     public function setCreatedAt(?\DateTimeImmutable $createdAt): self
  76.     {
  77.         $this->createdAt $createdAt;
  78.         return $this;
  79.     }
  80.     public function getAttribut(): ?Attribut
  81.     {
  82.         return $this->attribut;
  83.     }
  84.     public function setAttribut(?Attribut $attribut): self
  85.     {
  86.         $this->attribut $attribut;
  87.         return $this;
  88.     }
  89.     public function getValue(): ?string
  90.     {
  91.         return $this->value;
  92.     }
  93.     public function setValue(string $value): self
  94.     {
  95.         $this->value $value;
  96.         return $this;
  97.     }
  98.     public function getProduct(): ?Product
  99.     {
  100.         return $this->product;
  101.     }
  102.     public function setProduct(?Product $product): self
  103.     {
  104.         $this->product $product;
  105.         return $this;
  106.     }
  107.     public function getPrice(): ?float
  108.     {
  109.         return $this->price;
  110.     }
  111.     public function setPrice(?float $price): self
  112.     {
  113.         $this->price $price;
  114.         return $this;
  115.     }
  116.     public function getQuantity(): ?int
  117.     {
  118.         return $this->quantity;
  119.     }
  120.     public function setQuantity(?int $quantity): self
  121.     {
  122.         $this->quantity $quantity;
  123.         return $this;
  124.     }
  125.     /**
  126.      * @return Collection|ProductVariant[]
  127.      */
  128.     public function getProductVariants(): Collection
  129.     {
  130.         return $this->productVariants;
  131.     }
  132.     public function addProductVariant(ProductVariant $productVariant): self
  133.     {
  134.         if (!$this->productVariants->contains($productVariant)) {
  135.             $this->productVariants[] = $productVariant;
  136.             $productVariant->addAttributeValue($this);
  137.         }
  138.         return $this;
  139.     }
  140.     public function removeProductVariant(ProductVariant $productVariant): self
  141.     {
  142.         if ($this->productVariants->removeElement($productVariant)) {
  143.             $productVariant->removeAttributeValue($this);
  144.         }
  145.         return $this;
  146.     }
  147.     #[Groups(['read''write'])]
  148.     public function getAttributName(){
  149.         if($this->getAttribut()){
  150.             return $this->getAttribut()->getName();
  151.         }
  152.         return "";
  153.     }
  154.     public function getImage(): ?string
  155.     {
  156.         return $this->image;
  157.     }
  158.     public function setImage(?string $image): static
  159.     {
  160.         $this->image $image;
  161.         return $this;
  162.     }
  163.     public function getSkuCode(): ?string
  164.     {
  165.         return $this->skuCode;
  166.     }
  167.     public function setSkuCode(?string $skuCode): static
  168.     {
  169.         $this->skuCode $skuCode;
  170.         return $this;
  171.     }
  172.     public function getFinalPrice(){
  173.         
  174.         $price 0;
  175.         if($this->getProduct()){
  176.             if($this->getProduct()->getPromotion()){
  177.             
  178.                 if($this->getProduct()->getPromotion()->getType()=="percent"){
  179.                     
  180.                     $reduction = ($this->getPrice() / 100) * $this->getProduct()->getPromotion()->getValue();
  181.                     $finalPrice $this->getPrice() - $reduction;
  182.                     
  183.                 }else{
  184.                     
  185.                     $finalPrice $this->getPrice() - $this->getProduct()->getPromotion()->getValue();
  186.                     
  187.                 }
  188.                 
  189.                 return $finalPrice;
  190.                 
  191.                 
  192.             }
  193.         }
  194.         return $this->getPrice();
  195.        
  196.         
  197.     }
  198.     public function getCostPrice(): ?float
  199.     {
  200.         return $this->costPrice;
  201.     }
  202.     public function setCostPrice(?float $costPrice): static
  203.     {
  204.         $this->costPrice $costPrice;
  205.         return $this;
  206.     }
  207. }