<?php
namespace Roothirsch\Tuer24Bundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Timestampable\Traits\TimestampableEntity;
#[ORM\Entity(repositoryClass: "Roothirsch\Tuer24Bundle\Repository\Tuer24ProductRepository")]
#[ORM\Table(name: 'roothirsch_tuer24_product')]
#[Assert\Callback(['Roothirsch\Tuer24Bundle\Validator\TranslationValidator', 'validateProductTranslations'])]
class Tuer24Product
{
use TimestampableEntity;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 100, unique: true)]
#[Assert\NotBlank]
#[Assert\Length(max: 100)]
private $sku;
#[ORM\Column(type: 'string', length: 255)]
#[Assert\Length(max: 255)]
private $title = '';
#[ORM\Column(type: 'text', nullable: true)]
private $shortDescription;
#[ORM\Column(type: 'text', nullable: true)]
private $description;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $imagePath;
#[ORM\Column(type: 'decimal', precision: 15, scale: 2)]
#[Assert\NotBlank]
#[Assert\PositiveOrZero]
private $price;
#[ORM\Column(type: 'decimal', precision: 15, scale: 2)]
#[Assert\NotBlank]
#[Assert\PositiveOrZero]
private $basePrice;
#[ORM\Column(type: 'decimal', precision: 5, scale: 2)]
#[Assert\NotBlank]
#[Assert\Range(min: 0, max: 100)]
private $taxRate;
#[ORM\Column(type: 'string', length: 20, nullable: true)]
#[Assert\Length(max: 20)]
private $unit;
#[ORM\Column(type: 'decimal', precision: 10, scale: 2, nullable: true)]
#[Assert\PositiveOrZero]
private $weight;
#[ORM\Column(type: 'boolean')]
private $enabled = true;
#[ORM\OneToMany(targetEntity: Tuer24ProductTranslation::class, mappedBy: 'product', cascade: ['persist', 'remove'], orphanRemoval: true)]
private $translations;
#[ORM\ManyToMany(targetEntity: Tuer24Category::class, inversedBy: 'products')]
#[ORM\JoinTable(name: 'tuer24_product_category')]
private $categories;
#[ORM\OneToMany(targetEntity: Tuer24ProductPrice::class, mappedBy: 'product', cascade: ['persist', 'remove'], orphanRemoval: true)]
private $prices;
#[ORM\OneToOne(targetEntity: Tuer24Stock::class, mappedBy: 'product', cascade: ['persist', 'remove'], orphanRemoval: true)]
private $stock;
/**
* Constructor
*/
public function __construct()
{
$this->translations = new ArrayCollection();
$this->categories = new ArrayCollection();
$this->prices = new ArrayCollection();
$this->basePrice = 0.0; // Set default value for basePrice
$this->price = 0.0; // Set default value for price
$this->taxRate = 7.7; // Default Swiss VAT rate
$this->weight = 0.0; // Set default value for weight
}
/**
* Get id
*
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* Get sku
*
* @return string|null
*/
public function getSku(): ?string
{
return $this->sku;
}
/**
* Set sku
*
* @param string $sku
* @return $this
*/
public function setSku(string $sku): self
{
$this->sku = $sku;
return $this;
}
/**
* Get title
*
* @return string|null
*/
public function getTitle(): ?string
{
return $this->title;
}
/**
* Set title
*
* @param string $title
* @return $this
*/
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
/**
* Get short description
*
* @return string|null
*/
public function getShortDescription(): ?string
{
return $this->shortDescription;
}
/**
* Set short description
*
* @param string|null $shortDescription
* @return $this
*/
public function setShortDescription(?string $shortDescription): self
{
$this->shortDescription = $shortDescription;
return $this;
}
/**
* Get description
*
* @return string|null
*/
public function getDescription(): ?string
{
return $this->description;
}
/**
* Set description
*
* @param string|null $description
* @return $this
*/
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
/**
* Get price
*
* @return float|null
*/
public function getPrice(): ?float
{
return $this->price;
}
/**
* Set price
*
* @param float $price
* @return $this
*/
public function setPrice(float $price): self
{
$this->price = $price;
return $this;
}
/**
* Get base price
*
* @return float|null
*/
public function getBasePrice(): ?float
{
return $this->basePrice;
}
/**
* Set base price
*
* @param float $basePrice
* @return $this
*/
public function setBasePrice(float $basePrice): self
{
$this->basePrice = $basePrice;
return $this;
}
/**
* Get tax rate
*
* @return float|null
*/
public function getTaxRate(): ?float
{
return $this->taxRate;
}
/**
* Set tax rate
*
* @param float $taxRate
* @return $this
*/
public function setTaxRate(float $taxRate): self
{
$this->taxRate = $taxRate;
return $this;
}
/**
* Get unit
*
* @return string|null
*/
public function getUnit(): ?string
{
return $this->unit;
}
/**
* Set unit
*
* @param string|null $unit
* @return $this
*/
public function setUnit(?string $unit): self
{
$this->unit = $unit;
return $this;
}
/**
* Get weight
*
* @return float|null
*/
public function getWeight(): ?float
{
return $this->weight;
}
/**
* Set weight
*
* @param float|null $weight
* @return $this
*/
public function setWeight(?float $weight): self
{
$this->weight = $weight;
return $this;
}
/**
* Is enabled
*
* @return bool|null
*/
public function isEnabled(): ?bool
{
return $this->enabled;
}
/**
* Set enabled
*
* @param bool $enabled
* @return $this
*/
public function setEnabled(bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
/**
* Get translations
*
* @return Collection|Tuer24ProductTranslation[]
*/
public function getTranslations(): Collection
{
return $this->translations;
}
/**
* Add translation
*
* @param Tuer24ProductTranslation $translation
* @return $this
*/
public function addTranslation(Tuer24ProductTranslation $translation): self
{
if (!$this->translations->contains($translation)) {
$this->translations[] = $translation;
$translation->setProduct($this);
}
return $this;
}
/**
* Remove translation
*
* @param Tuer24ProductTranslation $translation
* @return $this
*/
public function removeTranslation(Tuer24ProductTranslation $translation): self
{
if ($this->translations->removeElement($translation)) {
// set the owning side to null (unless already changed)
if ($translation->getProduct() === $this) {
$translation->setProduct(null);
}
}
return $this;
}
/**
* Get categories
*
* @return Collection|Tuer24Category[]
*/
public function getCategories(): Collection
{
return $this->categories;
}
/**
* Add category
*
* @param Tuer24Category $category
* @return $this
*/
public function addCategory(Tuer24Category $category): self
{
if (!$this->categories->contains($category)) {
$this->categories[] = $category;
$category->addProduct($this);
}
return $this;
}
/**
* Remove category
*
* @param Tuer24Category $category
* @return $this
*/
public function removeCategory(Tuer24Category $category): self
{
if ($this->categories->removeElement($category)) {
$category->removeProduct($this);
}
return $this;
}
/**
* Get prices
*
* @return Collection|Tuer24ProductPrice[]
*/
public function getPrices(): Collection
{
return $this->prices;
}
/**
* Add price
*
* @param Tuer24ProductPrice $price
* @return $this
*/
public function addPrice(Tuer24ProductPrice $price): self
{
if (!$this->prices->contains($price)) {
$this->prices[] = $price;
$price->setProduct($this);
}
return $this;
}
/**
* Remove price
*
* @param Tuer24ProductPrice $price
* @return $this
*/
public function removePrice(Tuer24ProductPrice $price): self
{
if ($this->prices->removeElement($price)) {
// set the owning side to null (unless already changed)
if ($price->getProduct() === $this) {
$price->setProduct(null);
}
}
return $this;
}
/**
* Get stock
*
* @return Tuer24Stock|null
*/
public function getStock(): ?Tuer24Stock
{
return $this->stock;
}
/**
* Set stock
*
* @param Tuer24Stock|null $stock
* @return $this
*/
public function setStock(?Tuer24Stock $stock): self
{
// unset the owning side of the relation if necessary
if ($stock === null && $this->stock !== null) {
$this->stock->setProduct(null);
}
// set the owning side of the relation if necessary
if ($stock !== null && $stock->getProduct() !== $this) {
$stock->setProduct($this);
}
$this->stock = $stock;
return $this;
}
/**
* Get translation by locale
*
* @param string $locale
* @return Tuer24ProductTranslation|null
*/
public function getTranslation(string $locale): ?Tuer24ProductTranslation
{
foreach ($this->translations as $translation) {
if ($translation->getLocale() === $locale) {
return $translation;
}
}
return null;
}
/**
* Get price for company
*
* @param int $companyId
* @return Tuer24ProductPrice|null
*/
public function getPriceForCompany(int $companyId): ?Tuer24ProductPrice
{
foreach ($this->prices as $price) {
if ($price->getCompany()->getId() === $companyId) {
return $price;
}
}
return null;
}
/**
* Get image path
*
* @return string|null
*/
public function getImagePath(): ?string
{
return $this->imagePath;
}
/**
* Set image path
*
* @param string|null $imagePath
* @return $this
*/
public function setImagePath(?string $imagePath): self
{
$this->imagePath = $imagePath;
return $this;
}
}