<?php
namespace App\Entity;
use App\Repository\ProductRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\HttpFoundation\File\File;
use DateTimeImmutable;
#[ORM\Entity(repositoryClass: ProductRepository::class)]
#[Vich\Uploadable]
class Product
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;
#[ORM\Column(type: 'string', length: 255)]
private ?string $name = null;
#[ORM\Column(type: 'float')]
private ?float $price = null;
#[ORM\Column(type: 'text', nullable: true)]
private ?string $description = null;
#[ORM\ManyToMany(targetEntity: Attribute::class, mappedBy: 'products')]
#[ORM\JoinTable(name: 'attribute_product')]
private Collection $attributes;
#[Vich\UploadableField(mapping: 'product_image', fileNameProperty: 'imageName')]
private ?File $imageFile = null;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $imageName = null;
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
private ?DateTimeImmutable $updatedAt = null;
#[ORM\ManyToMany(targetEntity: Category::class, mappedBy: 'products')]
private Collection $categories;
#[ORM\OneToMany(targetEntity: ProductExtra::class, mappedBy: 'product', cascade: ['persist', 'remove'])]
private Collection $productExtras;
#[ORM\OneToMany(mappedBy: 'product', targetEntity: Gallery::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
private Collection $galleries;
#[ORM\Column(nullable: true)]
private ?float $fake_price = null;
#[ORM\Column(nullable: true)]
private ?int $stock = null;
#[ORM\OneToMany(mappedBy: 'product', targetEntity: OrderProduct::class)]
private Collection $orderProducts;
public function __construct()
{
$this->attributes = new ArrayCollection();
$this->categories = new ArrayCollection();
$this->productExtras = new ArrayCollection();
$this->galleries = new ArrayCollection();
$this->orderProducts = new ArrayCollection();
}
public function __toString(): string
{
return (string) $this->id . ' - ' . $this->name;
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getPrice(): ?float
{
return $this->price;
}
public function getRealPrice(): ?float
{
if($this->price){
return $this->price/100;
}
return $this->price;
}
public function getDiscound(): ?float
{
if($this->price && $this->fake_price){
return (($this->fake_price - $this->price) / $this->fake_price) * 100;
}
return 0;
}
public function setPrice(float $price): self
{
$this->price = $price;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
/**
* @return Collection<int, Attribute>
*/
public function getAttributes(): Collection
{
return $this->attributes;
}
public function addAttribute(Attribute $attribute): self
{
if (!$this->attributes->contains($attribute)) {
$this->attributes[] = $attribute;
$attribute->addProduct($this);
}
return $this;
}
public function removeAttribute(Attribute $attribute): self
{
if ($this->attributes->removeElement($attribute)) {
$attribute->removeProduct($this);
}
return $this;
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function setImageFile(?File $imageFile = null): self
{
$this->imageFile = $imageFile;
// Si la imagen ha cambiado, se debe actualizar la fecha de modificación.
if ($imageFile) {
$this->updatedAt = new \DateTimeImmutable();
}
return $this;
}
public function getImageName(): ?string
{
return $this->imageName;
}
public function setImageName(?string $imageName): self
{
$this->imageName = $imageName;
return $this;
}
public function getUpdatedAt(): ?DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(?DateTimeImmutable $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return Collection<int, Category>
*/
public function getCategories(): Collection
{
return $this->categories;
}
public function addCategory(Category $category): self
{
if (!$this->categories->contains($category)) {
$this->categories[] = $category;
$category->addProduct($this);
}
return $this;
}
public function removeCategory(Category $category): self
{
if ($this->categories->removeElement($category)) {
$category->removeProduct($this);
}
return $this;
}
/**
* @return Collection<int, ProductExtra>
*/
public function getProductExtras(): Collection
{
return $this->productExtras;
}
public function addProductExtra(ProductExtra $productExtra): self
{
if (!$this->productExtras->contains($productExtra)) {
$this->productExtras[] = $productExtra;
$productExtra->setProduct($this);
}
return $this;
}
public function removeProductExtra(ProductExtra $productExtra): self
{
if ($this->productExtras->removeElement($productExtra)) {
// set the owning side to null (unless already changed)
if ($productExtra->getProduct() === $this) {
$productExtra->setProduct(null);
}
}
return $this;
}
/**
* @return Collection<int, Gallery>
*/
public function getGalleries(): Collection
{
return $this->galleries;
}
public function addGallery(Gallery $gallery): static
{
if (!$this->galleries->contains($gallery)) {
$this->galleries->add($gallery);
$gallery->setProduct($this);
}
return $this;
}
public function removeGallery(Gallery $gallery): static
{
if ($this->galleries->removeElement($gallery)) {
// set the owning side to null (unless already changed)
if ($gallery->getProduct() === $this) {
$gallery->setProduct(null);
}
}
return $this;
}
public function getFakePrice(): ?float
{
return $this->fake_price;
}
public function setFakePrice(?float $fake_price): static
{
$this->fake_price = $fake_price;
return $this;
}
public function getRealFakePrice(): ?float
{
if($this->fake_price){
return $this->fake_price/100;
}
return $this->fake_price;
}
public function getStock(): ?int
{
return $this->stock;
}
public function setStock(?int $stock): static
{
$this->stock = $stock;
return $this;
}
/**
* @return Collection<int, OrderProduct>
*/
public function getOrderProducts(): Collection
{
return $this->orderProducts;
}
public function addOrderProduct(OrderProduct $orderProduct): static
{
if (!$this->orderProducts->contains($orderProduct)) {
$this->orderProducts->add($orderProduct);
$orderProduct->setProduct($this);
}
return $this;
}
public function removeOrderProduct(OrderProduct $orderProduct): static
{
if ($this->orderProducts->removeElement($orderProduct)) {
// set the owning side to null (unless already changed)
if ($orderProduct->getProduct() === $this) {
$orderProduct->setProduct(null);
}
}
return $this;
}
}