src/Entity/Product.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ProductRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  8. use Symfony\Component\HttpFoundation\File\File;
  9. use DateTimeImmutable;
  10. #[ORM\Entity(repositoryClassProductRepository::class)]
  11. #[Vich\Uploadable]
  12. class Product
  13. {
  14.     #[ORM\Id]
  15.     #[ORM\GeneratedValue]
  16.     #[ORM\Column(type'integer')]
  17.     private ?int $id null;
  18.     #[ORM\Column(type'string'length255)]
  19.     private ?string $name null;
  20.     #[ORM\Column(type'float')]
  21.     private ?float $price null;
  22.     #[ORM\Column(type'text'nullabletrue)]
  23.     private ?string $description null;
  24.     #[ORM\ManyToMany(targetEntityAttribute::class, mappedBy'products')]
  25.     #[ORM\JoinTable(name'attribute_product')]
  26.     private Collection $attributes;
  27.     #[Vich\UploadableField(mapping'product_image'fileNameProperty'imageName')]
  28.     private ?File $imageFile null;
  29.     #[ORM\Column(type'string'length255nullabletrue)]
  30.     private ?string $imageName null;
  31.     #[ORM\Column(type'datetime_immutable'nullabletrue)]
  32.     private ?DateTimeImmutable $updatedAt null;
  33.     #[ORM\ManyToMany(targetEntityCategory::class, mappedBy'products')]
  34.     private Collection $categories;
  35.     #[ORM\OneToMany(targetEntityProductExtra::class, mappedBy'product'cascade: ['persist''remove'])]
  36.     private Collection $productExtras;
  37.     #[ORM\OneToMany(mappedBy'product'targetEntityGallery::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  38.     private Collection $galleries;
  39.     #[ORM\Column(nullabletrue)]
  40.     private ?float $fake_price null;
  41.     #[ORM\Column(nullabletrue)]
  42.     private ?int $stock null;
  43.     #[ORM\OneToMany(mappedBy'product'targetEntityOrderProduct::class)]
  44.     private Collection $orderProducts;
  45.     public function __construct()
  46.     {
  47.         $this->attributes = new ArrayCollection();
  48.         $this->categories = new ArrayCollection();
  49.         $this->productExtras = new ArrayCollection();
  50.         $this->galleries = new ArrayCollection();
  51.         $this->orderProducts = new ArrayCollection();
  52.     }
  53.     public function __toString(): string
  54.     {
  55.         return (string) $this->id ' - ' $this->name;
  56.     }
  57.     public function getId(): ?int
  58.     {
  59.         return $this->id;
  60.     }
  61.     public function getName(): ?string
  62.     {
  63.         return $this->name;
  64.     }
  65.     public function setName(string $name): self
  66.     {
  67.         $this->name $name;
  68.         return $this;
  69.     }
  70.     public function getPrice(): ?float
  71.     {
  72.         return $this->price;
  73.     }
  74.     
  75.     public function getRealPrice(): ?float
  76.     {
  77.         if($this->price){
  78.             return $this->price/100;
  79.         }
  80.         return $this->price;
  81.     }
  82.     public function getDiscound(): ?float
  83.     {
  84.         if($this->price && $this->fake_price){
  85.             return (($this->fake_price $this->price) / $this->fake_price) * 100;
  86.         }
  87.         return 0;
  88.     }
  89.     public function setPrice(float $price): self
  90.     {
  91.         $this->price $price;
  92.         return $this;
  93.     }
  94.     public function getDescription(): ?string
  95.     {
  96.         return $this->description;
  97.     }
  98.     public function setDescription(?string $description): self
  99.     {
  100.         $this->description $description;
  101.         return $this;
  102.     }
  103.     /**
  104.      * @return Collection<int, Attribute>
  105.      */
  106.     public function getAttributes(): Collection
  107.     {
  108.         return $this->attributes;
  109.     }
  110.     public function addAttribute(Attribute $attribute): self
  111.     {
  112.         if (!$this->attributes->contains($attribute)) {
  113.             $this->attributes[] = $attribute;
  114.             $attribute->addProduct($this);
  115.         }
  116.         return $this;
  117.     }
  118.     public function removeAttribute(Attribute $attribute): self
  119.     {
  120.         if ($this->attributes->removeElement($attribute)) {
  121.             $attribute->removeProduct($this);
  122.         }
  123.         return $this;
  124.     }
  125.     public function getImageFile(): ?File
  126.     {
  127.         return $this->imageFile;
  128.     }
  129.     public function setImageFile(?File $imageFile null): self
  130.     {
  131.         $this->imageFile $imageFile;
  132.         // Si la imagen ha cambiado, se debe actualizar la fecha de modificación.
  133.         if ($imageFile) {
  134.             $this->updatedAt = new \DateTimeImmutable();
  135.         }
  136.         return $this;
  137.     }
  138.     public function getImageName(): ?string
  139.     {
  140.         return $this->imageName;
  141.     }
  142.     public function setImageName(?string $imageName): self
  143.     {
  144.         $this->imageName $imageName;
  145.         return $this;
  146.     }
  147.     public function getUpdatedAt(): ?DateTimeImmutable
  148.     {
  149.         return $this->updatedAt;
  150.     }
  151.     public function setUpdatedAt(?DateTimeImmutable $updatedAt): self
  152.     {
  153.         $this->updatedAt $updatedAt;
  154.         return $this;
  155.     }
  156.     /**
  157.      * @return Collection<int, Category>
  158.      */
  159.     public function getCategories(): Collection
  160.     {
  161.         return $this->categories;
  162.     }
  163.     public function addCategory(Category $category): self
  164.     {
  165.         if (!$this->categories->contains($category)) {
  166.             $this->categories[] = $category;
  167.             $category->addProduct($this);
  168.         }
  169.         return $this;
  170.     }
  171.     public function removeCategory(Category $category): self
  172.     {
  173.         if ($this->categories->removeElement($category)) {
  174.             $category->removeProduct($this);
  175.         }
  176.         return $this;
  177.     }
  178.     /**
  179.      * @return Collection<int, ProductExtra>
  180.      */
  181.     public function getProductExtras(): Collection
  182.     {
  183.         return $this->productExtras;
  184.     }
  185.     public function addProductExtra(ProductExtra $productExtra): self
  186.     {
  187.         if (!$this->productExtras->contains($productExtra)) {
  188.             $this->productExtras[] = $productExtra;
  189.             $productExtra->setProduct($this);
  190.         }
  191.         return $this;
  192.     }
  193.     public function removeProductExtra(ProductExtra $productExtra): self
  194.     {
  195.         if ($this->productExtras->removeElement($productExtra)) {
  196.             // set the owning side to null (unless already changed)
  197.             if ($productExtra->getProduct() === $this) {
  198.                 $productExtra->setProduct(null);
  199.             }
  200.         }
  201.         return $this;
  202.     }
  203.     /**
  204.      * @return Collection<int, Gallery>
  205.      */
  206.     public function getGalleries(): Collection
  207.     {
  208.         return $this->galleries;
  209.     }
  210.     public function addGallery(Gallery $gallery): static
  211.     {
  212.         if (!$this->galleries->contains($gallery)) {
  213.             $this->galleries->add($gallery);
  214.             $gallery->setProduct($this);
  215.         }
  216.         return $this;
  217.     }
  218.     public function removeGallery(Gallery $gallery): static
  219.     {
  220.         if ($this->galleries->removeElement($gallery)) {
  221.             // set the owning side to null (unless already changed)
  222.             if ($gallery->getProduct() === $this) {
  223.                 $gallery->setProduct(null);
  224.             }
  225.         }
  226.         return $this;
  227.     }
  228.     public function getFakePrice(): ?float
  229.     {
  230.         return $this->fake_price;
  231.     }
  232.     public function setFakePrice(?float $fake_price): static
  233.     {
  234.         $this->fake_price $fake_price;
  235.         return $this;
  236.     }
  237.     public function getRealFakePrice(): ?float
  238.     {
  239.         if($this->fake_price){
  240.             return $this->fake_price/100;
  241.         }
  242.         return $this->fake_price;
  243.     }
  244.     public function getStock(): ?int
  245.     {
  246.         return $this->stock;
  247.     }
  248.     public function setStock(?int $stock): static
  249.     {
  250.         $this->stock $stock;
  251.         return $this;
  252.     }
  253.     /**
  254.      * @return Collection<int, OrderProduct>
  255.      */
  256.     public function getOrderProducts(): Collection
  257.     {
  258.         return $this->orderProducts;
  259.     }
  260.     public function addOrderProduct(OrderProduct $orderProduct): static
  261.     {
  262.         if (!$this->orderProducts->contains($orderProduct)) {
  263.             $this->orderProducts->add($orderProduct);
  264.             $orderProduct->setProduct($this);
  265.         }
  266.         return $this;
  267.     }
  268.     public function removeOrderProduct(OrderProduct $orderProduct): static
  269.     {
  270.         if ($this->orderProducts->removeElement($orderProduct)) {
  271.             // set the owning side to null (unless already changed)
  272.             if ($orderProduct->getProduct() === $this) {
  273.                 $orderProduct->setProduct(null);
  274.             }
  275.         }
  276.         return $this;
  277.     }
  278. }