<?php
namespace App\Entity;
use App\Repository\ProducerRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Blameable\Traits\BlameableEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: ProducerRepository::class)]
class Producer implements TranslatableInterface
{
use BlameableEntity; //Hook blameable behaviour. Updates createdBy, updatedBy fields
use TimestampableEntity; //Hook timestampable behaviour. Updates createdAt, updatedAt fields
use TranslatableTrait;
#[ORM\Id]
#[ORM\GeneratedValue(strategy: "IDENTITY")]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Assert\NotNull()]
#[Assert\Length(max: 255)]
private ?string $name = null;
#[ORM\OneToMany(mappedBy: 'producer', targetEntity: Product::class)]
private Collection $products;
#[ORM\OneToMany(mappedBy: 'producer', targetEntity: Media::class, cascade: ["persist", "remove"])]
#[ORM\OrderBy(["position" => "ASC"])]
private Collection $media;
#[ORM\Column(length: 25, nullable: true)]
#[Assert\Length(max: 25)]
private ?string $cif = null;
#[ORM\ManyToOne(inversedBy: 'producers', cascade:["persist"])]
#[Assert\Valid()]
private ?Address $address = null;
#[ORM\Column(length: 100, nullable: true)]
#[Assert\Length(max: 100)]
private ?string $contactName = null;
#[ORM\Column(length: 50, nullable: true)]
#[Assert\Length(max: 50)]
private ?string $contactPhone = null;
#[ORM\Column(length: 255, nullable: true)]
#[Assert\Length(max: 255)]
#[Assert\Email()]
private ?string $contactEmail = null;
#[ORM\Column(length: 255, nullable: true)]
#[Assert\Length(max: 255)]
private ?string $paymentMethod = null;
#[ORM\Column(length: 255, nullable: true)]
#[Assert\Length(max: 255)]
private ?string $paymentTerms = null;
#[ORM\Column(length: 500, nullable: true)]
private ?string $holdedId = null;
#[ORM\Column(length: 255, nullable: true)]
#[Assert\Length(max: 255)]
private ?string $socialReason = null;
#[ORM\Column(type: 'boolean', options: ['default' => false])]
private ?bool $selfShip = false;
#[ORM\OneToMany(mappedBy: 'Producer', targetEntity: ProductUnique::class)]
private Collection $productUniques;
#[ORM\Column(type: 'integer', nullable: true)]
private int $ordersMinimumRequired;
public function __call($method, $arguments)
{
return $this->proxyCurrentLocaleTranslation($method, $arguments);
}
public function __construct()
{
$this->products = new ArrayCollection();
$this->media = new ArrayCollection();
$this->productUniques = new ArrayCollection();
}
public function __toString()
{
return $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;
}
/**
* @return Collection<int, Product>
*/
public function getProducts(): Collection
{
return $this->products;
}
public function addProduct(Product $product): self
{
if (!$this->products->contains($product)) {
$this->products->add($product);
$product->setProducer($this);
}
return $this;
}
public function removeProduct(Product $product): self
{
if ($this->products->removeElement($product)) {
// set the owning side to null (unless already changed)
if ($product->getProducer() === $this) {
$product->setProducer(null);
}
}
return $this;
}
/**
* Main image is the one that have position 0
* @return Media|null
*/
public function getMainProducerImage(): ?Media
{
if ($this->media) {
$filteredProducerImages = $this->media->filter(function (Media $producerImage) {
return $producerImage->getPosition() == 0;
});
return (count($filteredProducerImages) > 0 ) ? $filteredProducerImages->first() : null;
} else {
return null;
}
}
/**
* @return Collection<int, Media>
*/
public function getMedia(): Collection
{
return $this->media;
}
public function addMedium(Media $medium): self
{
if (!$this->media->contains($medium)) {
$medium->setDirectory('producer');
$this->media->add($medium);
$medium->setProducer($this);
}
return $this;
}
public function removeMedium(Media $medium): self
{
if ($this->media->removeElement($medium)) {
// set the owning side to null (unless already changed)
if ($medium->getProducer() === $this) {
$medium->setProducer(null);
}
}
return $this;
}
public function getCif(): ?string
{
return $this->cif;
}
public function setCif(?string $cif): self
{
$this->cif = $cif;
return $this;
}
public function getAddress(): ?Address
{
return $this->address;
}
public function setAddress(?Address $address): self
{
$this->address = $address;
return $this;
}
public function getContactName(): ?string
{
return $this->contactName;
}
public function setContactName(?string $contactName): self
{
$this->contactName = $contactName;
return $this;
}
public function getContactPhone(): ?string
{
return $this->contactPhone;
}
public function setContactPhone(?string $contactPhone): self
{
$this->contactPhone = $contactPhone;
return $this;
}
public function getContactEmail(): ?string
{
return $this->contactEmail;
}
public function setContactEmail(?string $contactEmail): self
{
$this->contactEmail = $contactEmail;
return $this;
}
public function getPaymentMethod(): ?string
{
return $this->paymentMethod;
}
public function setPaymentMethod(?string $paymentMethod): self
{
$this->paymentMethod = $paymentMethod;
return $this;
}
public function getPaymentTerms(): ?string
{
return $this->paymentTerms;
}
public function setPaymentTerms(?string $paymentTerms): self
{
$this->paymentTerms = $paymentTerms;
return $this;
}
public function getHoldedId(): ?string
{
return $this->holdedId;
}
public function setHoldedId(?string $holdedId): self
{
$this->holdedId = $holdedId;
return $this;
}
public function getSocialReason(): ?string
{
return $this->socialReason;
}
public function setSocialReason(?string $socialReason): self
{
$this->socialReason = $socialReason;
return $this;
}
public function setSelfShip(bool $IsSelfShip): self
{
$this->selfShip = $IsSelfShip;
return $this;
}
public function getSelfShip(): bool
{
return $this->selfShip;
}
/**
* @return Collection<int, ProductUnique>
*/
public function getProductUniques(): Collection
{
return $this->productUniques;
}
public function addProductUnique(ProductUnique $productUnique): self
{
if (!$this->productUniques->contains($productUnique)) {
$this->productUniques->add($productUnique);
$productUnique->setProducer($this);
}
return $this;
}
public function removeProductUnique(ProductUnique $productUnique): self
{
if ($this->productUniques->removeElement($productUnique)) {
// set the owning side to null (unless already changed)
if ($productUnique->getProducer() === $this) {
$productUnique->setProducer(null);
}
}
return $this;
}
public function getOrdersMinimumRequired(): int
{
return $this->ordersMinimumRequired ?? 0;
}
public function setOrdersMinimumRequired(int $ordersMinimumRequired): self
{
$this->ordersMinimumRequired = $ordersMinimumRequired;
return $this;
}
}