<?php
namespace App\Entity;
use App\_Interface\LocaleInterface;
use App\Repository\ProductRepository;
use App\_Trait\LocaleTrait;
use Cocur\Slugify\Slugify;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\productCategory;
use App\Model\AppConstant;
use Exception;
use JsonSerializable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
#[ORM\Entity(repositoryClass: ProductRepository::class)]
class Product implements JsonSerializable, LocaleInterface
{
const EXCEPTION_CODE = [
"INSUFFICIENT_STOCK" => 4040
];
use LocaleTrait;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $name;
#[ORM\Column(type: 'text', nullable: true)]
private $description;
#[ORM\Column(type: 'decimal', precision: 10, scale: 3)]
private $cost;
#[ORM\ManyToOne(targetEntity: 'ProductCategory', inversedBy: 'products')]
#[ORM\JoinColumn(name: 'id_product_category', referencedColumnName: 'id')]
private $productCategory;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $image;
#[ORM\OneToMany(targetEntity: 'FavoriteProduct', mappedBy: 'product')]
private $favoriteProducts;
#[ORM\OneToMany(targetEntity: ProductImages::class, mappedBy: 'product', fetch: 'EAGER', orphanRemoval: true)]
private $productImages;
#[ORM\OneToOne(targetEntity: ViewProduitQteStock::class, mappedBy: 'produit', cascade: ['persist', 'remove'])]
private $produitQteStock;
#[ORM\Column(type: 'boolean', nullable: true)]
private $precommande;
#[ORM\Column(type: 'text', nullable: true)]
private $apropos;
#[ORM\Column(type: 'text', nullable: true)]
private $avertissement;
#[ORM\Column(type: 'text', nullable: true)]
private $precautionsEmploi;
#[ORM\Column(type: 'text', nullable: true)]
private $breveDescription;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $volume;
#[ORM\OneToMany(targetEntity: ProductTracabilite::class, mappedBy: 'product')]
private $tracabilites;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $legende_image;
#[ORM\Column(length: 255, nullable: true)]
private ?string $metatitle = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $metadescription = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $name_english = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $breveDescription_english = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $apropos_english = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $description_english = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $avertissement_english = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $precautionsEmploi_english = null;
#[ORM\Column(nullable: true, options:['default' => 0])]
private ?int $langue = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $legalNotice = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $legalNotice_english = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $originAndCurrentProduction = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $originAndCurrentProduction_english = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $usageTips = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $usageTips_english = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $compositionAndPrecautionsForUse = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $compositionAndPrecautionsForUse_english = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $precautionsGenerales = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $precautionsGenerales_english = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $referenceSKU = null;
#[ORM\Column(nullable: true)]
private ?bool $featured = null;
#[ORM\Column(type: Types::DECIMAL, precision: 5, scale: 2, nullable: true)]
private ?string $tva = AppConstant::DEFAULT_TVA_STR;
#[ORM\Column(nullable: true)]
private ?bool $isMonthProduct = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $majDate = null;
#[ORM\Column(nullable: true)]
private ?int $status = null;
/**
* @return Collection<int, FavoriteProduct>
*/
public function getFavoriteProducts(): Collection
{
return $this->favoriteProducts;
}
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 getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getCost(): ?string
{
return $this->cost;
}
public function setCost(string $cost): self
{
$this->cost = $cost;
return $this;
}
public function getProductCategory(): ?ProductCategory
{
return $this->productCategory;
}
public function setProductCategory(ProductCategory $productCategory): self
{
$this->productCategory = $productCategory;
return $this;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
public function __construct(){
$this->favoriteProducts = new ArrayCollection();
$this->productImages = new ArrayCollection();
$this->tracabilites = new ArrayCollection();
}
// public function __construct(string $name, string $description, string $cost, productCategory $productCategory, string $image){
// $this->setname($name);
// $this->setDescription($description);
// $this->setcost($cost);
// $this->setproductCategory($productCategory);
// $this->setimage($image);
// }
public function jsonSerialize()
{
$vars = get_object_vars($this);
unset($vars["tracabilites"]);
$vars["available"] = $this->isAvailable();
$vars["preOrder"] = $this->isPreOrder();
return $vars;
}
/**
* @return Collection<int, ProductImages>
*/
public function getProductImages(): Collection
{
return $this->productImages;
}
public function addProductImage(ProductImages $productImage): self
{
if (!$this->productImages->contains($productImage)) {
$this->productImages[] = $productImage;
$productImage->setProduct($this);
}
return $this;
}
public function removeProductImage(ProductImages $productImage): self
{
if ($this->productImages->removeElement($productImage)) {
// set the owning side to null (unless already changed)
if ($productImage->getProduct() === $this) {
$productImage->setProduct(null);
}
}
return $this;
}
public function getProduitQteStock(): ?ViewProduitQteStock
{
return $this->produitQteStock;
}
public function setProduitQteStock(ViewProduitQteStock $produitQteStock): self
{
// set the owning side of the relation if necessary
if ($produitQteStock->getProduit() !== $this) {
$produitQteStock->setProduit($this);
}
$this->produitQteStock = $produitQteStock;
return $this;
}
public function isPrecommande(): ?bool
{
return $this->precommande;
}
public function setPrecommande(?bool $precommande): self
{
$this->precommande = $precommande;
return $this;
}
public function isAvailable(): bool{
return $this->getProduitQteStock()?->getQteStock() > 0 || $this->isPrecommande();
}
public function isPreOrder(int $qty = 1): bool{
return $this->isPrecommande() && ($this->getProduitQteStock()?->getQteStock() - $qty) <= 0;
}
public function checkQty(float $qty, bool $considerPreOrder = true){
if(!(($considerPreOrder && $this->isPrecommande()) || $this->getProduitQteStock()->getQteStock() >= $qty)){
throw new Exception("Stock insuffisant pour le produit <<".$this->getName(), self::EXCEPTION_CODE['INSUFFICIENT_STOCK']);
}
return true;
}
public function getApropos(): ?string
{
return $this->apropos;
}
public function setApropos(?string $apropos): self
{
$this->apropos = $apropos;
return $this;
}
public function getAvertissement(): ?string
{
return $this->avertissement;
}
public function setAvertissement(?string $avertissement): self
{
$this->avertissement = $avertissement;
return $this;
}
public function getPrecautionsEmploi(): ?string
{
return $this->precautionsEmploi;
}
public function setPrecautionsEmploi(?string $precautionsEmploi): self
{
$this->precautionsEmploi = $precautionsEmploi;
return $this;
}
public function getBreveDescription(): ?string
{
return $this->breveDescription;
}
public function setBreveDescription(?string $breveDescription): self
{
$this->breveDescription = $breveDescription;
return $this;
}
public function getVolume(): ?string
{
return $this->volume;
}
public function setVolume(?string $volume): self
{
$this->volume = $volume;
return $this;
}
/**
* @return Collection<int, ProductTracabilite>
*/
public function getTracabilites(): Collection
{
return $this->tracabilites;
}
public function addTracabilite(ProductTracabilite $tracabilite): self
{
if (!$this->tracabilites->contains($tracabilite)) {
$this->tracabilites[] = $tracabilite;
$tracabilite->setProduct($this);
}
return $this;
}
public function removeTracabilite(ProductTracabilite $tracabilite): self
{
if ($this->tracabilites->removeElement($tracabilite)) {
// set the owning side to null (unless already changed)
if ($tracabilite->getProduct() === $this) {
$tracabilite->setProduct(null);
}
}
return $this;
}
/**
* Set the value of tracabilites
*
* @return self
*/
public function setTracabilites($tracabilites)
{
$this->tracabilites = $tracabilites;
return $this;
}
public function getSlugCatProduct()
{
return (new Slugify())->slugify($this->productCategory->getName()) ;
}
public function getSlugProduct()
{
return (new Slugify())->slugify($this->getNameByDefaultLocale()) ;
}
public function getLegendeImage(): ?string
{
return $this->legende_image;
}
public function setLegendeImage(?string $legende_image): self
{
$this->legende_image = $legende_image;
return $this;
}
public function getMetatitle(): ?string
{
return $this->metatitle;
}
public function setMetatitle(?string $metatitle): self
{
$this->metatitle = $metatitle;
return $this;
}
public function getMetadescription(): ?string
{
return $this->metadescription;
}
public function setMetadescription(?string $metadescription): self
{
$this->metadescription = $metadescription;
return $this;
}
public function getNameEnglish(): ?string
{
return $this->name_english;
}
public function setNameEnglish(?string $name_english): self
{
$this->name_english = $name_english;
return $this;
}
public function getBreveDescriptionEnglish(): ?string
{
return $this->breveDescription_english;
}
public function setBreveDescriptionEnglish(?string $breveDescription_english): self
{
$this->breveDescription_english = $breveDescription_english;
return $this;
}
public function getAproposEnglish(): ?string
{
return $this->apropos_english;
}
public function setAproposEnglish(?string $apropos_english): self
{
$this->apropos_english = $apropos_english;
return $this;
}
public function getDescriptionEnglish(): ?string
{
return $this->description_english;
}
public function setDescriptionEnglish(?string $description_english): self
{
$this->description_english = $description_english;
return $this;
}
public function getAvertissementEnglish(): ?string
{
return $this->avertissement_english;
}
public function setAvertissementEnglish(?string $avertissement_english): self
{
$this->avertissement_english = $avertissement_english;
return $this;
}
public function getPrecautionsEmploiEnglish(): ?string
{
return $this->precautionsEmploi_english;
}
public function setPrecautionsEmploiEnglish(?string $precautionsEmploi_english): self
{
$this->precautionsEmploi_english = $precautionsEmploi_english;
return $this;
}
/** Permet de récuperer le nom du produit en fonction de la version (langue) */
public function getNameByDefaultLocale()
{
$name = '';
if ($GLOBALS['app_default_locale'] === 'fr' ) {
$name = $this->name;
}else if($GLOBALS['app_default_locale'] === 'en'){
$name = ($this->name_english === '' or is_null($this->name_english)) ? $this->name : $this->name_english ;
}
return $name;
}
public function getBreveDescriptionByDefaultLocale()
{
$breveDescription = '';
if ($GLOBALS['app_default_locale'] === 'fr') {
$breveDescription = $this->breveDescription;
}else if($GLOBALS['app_default_locale'] === 'en'){
$breveDescription = ($this->breveDescription_english === '' or is_null($this->breveDescription_english)) ? '' : $this->breveDescription_english ;
}
return $breveDescription;
}
public function getAproposByDefaultLocale()
{
$apropos = '';
if ($GLOBALS['app_default_locale'] === 'fr') {
$apropos = $this->apropos;
}else if($GLOBALS['app_default_locale'] === 'en'){
$apropos = ($this->apropos_english === '' or is_null($this->apropos_english)) ? '' : $this->apropos_english ;
}
return $apropos;
}
public function getDescriptionByDefaultLocale()
{
$description = '';
if ($GLOBALS['app_default_locale'] === 'fr') {
$description = $this->description;
}else if($GLOBALS['app_default_locale'] === 'en'){
$description = ($this->description === '' or is_null($this->description_english)) ? '' : $this->description_english ;
}
return $description;
}
public function getAvertissementByDefaultLocale()
{
$avertissement = '';
if ($GLOBALS['app_default_locale'] === 'fr') {
$avertissement = $this->avertissement;
}else if($GLOBALS['app_default_locale'] === 'en'){
$avertissement = ($this->avertissement_english === '' or is_null($this->avertissement_english)) ? '' : $this->avertissement_english ;
}
return $avertissement;
}
public function getPrecautionsEmploiByDefaultLocale()
{
$precautionsEmploi = '';
if ($GLOBALS['app_default_locale'] === 'fr') {
$precautionsEmploi = $this->precautionsEmploi;
}else if($GLOBALS['app_default_locale'] === 'en'){
$precautionsEmploi = ($this->precautionsEmploi_english === '' or is_null($this->precautionsEmploi_english)) ? '' : $this->precautionsEmploi_english ;
}
return $precautionsEmploi;
}
public function getLangue(): ?int
{
return $this->langue;
}
public function setLangue(?int $langue): self
{
$this->langue = $langue;
return $this;
}
public function getLegalNotice(): ?string
{
return $this->legalNotice;
}
public function setLegalNotice(?string $legalNotice): self
{
$this->legalNotice = $legalNotice;
return $this;
}
public function getLegalNoticeEnglish(): ?string
{
return $this->legalNotice_english;
}
public function setLegalNoticeEnglish(?string $legalNotice_english): self
{
$this->legalNotice_english = $legalNotice_english;
return $this;
}
public function getOriginAndCurrentProduction(): ?string
{
return $this->originAndCurrentProduction;
}
public function setOriginAndCurrentProduction(?string $originAndCurrentProduction): self
{
$this->originAndCurrentProduction = $originAndCurrentProduction;
return $this;
}
public function getOriginAndCurrentProductionEnglish(): ?string
{
return $this->originAndCurrentProduction_english;
}
public function setOriginAndCurrentProductionEnglish(?string $originAndCurrentProduction_english): self
{
$this->originAndCurrentProduction_english = $originAndCurrentProduction_english;
return $this;
}
public function getUsageTips(): ?string
{
return $this->usageTips;
}
public function setUsageTips(?string $usageTips): self
{
$this->usageTips = $usageTips;
return $this;
}
public function getUsageTipsEnglish(): ?string
{
return $this->usageTips_english;
}
public function setUsageTipsEnglish(?string $usageTips_english): self
{
$this->usageTips_english = $usageTips_english;
return $this;
}
public function getCompositionAndPrecautionsForUse(): ?string
{
return $this->compositionAndPrecautionsForUse;
}
public function setCompositionAndPrecautionsForUse(?string $compositionAndPrecautionsForUse): self
{
$this->compositionAndPrecautionsForUse = $compositionAndPrecautionsForUse;
return $this;
}
public function getCompositionAndPrecautionsForUseEnglish(): ?string
{
return $this->compositionAndPrecautionsForUse_english;
}
public function setCompositionAndPrecautionsForUseEnglish(?string $compositionAndPrecautionsForUse_english): self
{
$this->compositionAndPrecautionsForUse_english = $compositionAndPrecautionsForUse_english;
return $this;
}
public function getLegalNoticeByDefaultLocale()
{
$legalNotice = '';
if ($GLOBALS['app_default_locale'] === 'fr') {
$legalNotice = $this->legalNotice;
}else if($GLOBALS['app_default_locale'] === 'en'){
$legalNotice = ($this->legalNotice_english === '' or is_null($this->legalNotice_english)) ? '' : $this->legalNotice_english ;
}
return $legalNotice;
}
public function getOriginAndCurrentProductionByDefaultLocale()
{
$origin_CProduct = '';
if ($GLOBALS['app_default_locale'] === 'fr') {
$origin_CProduct = $this->originAndCurrentProduction;
}else if($GLOBALS['app_default_locale'] === 'en'){
$origin_CProduct = ($this->originAndCurrentProduction_english === '' or is_null($this->originAndCurrentProduction_english)) ? '' : $this->originAndCurrentProduction_english ;
}
return $origin_CProduct;
}
public function getUsageTipsByDefaultLocale()
{
$usageTips = '';
if ($GLOBALS['app_default_locale'] === 'fr') {
$usageTips = $this->usageTips;
}else if($GLOBALS['app_default_locale'] === 'en'){
$usageTips = ($this->usageTips_english === '' or is_null($this->usageTips_english)) ? '' : $this->usageTips_english ;
}
return $usageTips;
}
public function getCompositionAndPrecautionsForUseByDefaultLocale()
{
$compAndPreForUse = '';
if ($GLOBALS['app_default_locale'] === 'fr') {
$compAndPreForUse = $this->compositionAndPrecautionsForUse;
}else if($GLOBALS['app_default_locale'] === 'en'){
$compAndPreForUse = ($this->compositionAndPrecautionsForUse_english === '' or is_null($this->compositionAndPrecautionsForUse_english)) ? '' : $this->compositionAndPrecautionsForUse_english ;
}
return $compAndPreForUse;
}
public function getGeneralPrecautionByDefaultLocale()
{
$generalPrecautions = '';
if ($GLOBALS['app_default_locale'] === 'fr') {
$generalPrecautions = $this->precautionsGenerales;
}else if($GLOBALS['app_default_locale'] === 'en'){
$generalPrecautions = ($this->precautionsGenerales_english === '' or is_null($this->precautionsGenerales_english)) ? '' : $this->precautionsGenerales_english ;
}
return $generalPrecautions;
}
public function getPrecautionsGenerales(): ?string
{
return $this->precautionsGenerales;
}
public function setPrecautionsGenerales(?string $precautionsGenerales): self
{
$this->precautionsGenerales = $precautionsGenerales;
return $this;
}
public function getPrecautionsGeneralesEnglish(): ?string
{
return $this->precautionsGenerales_english;
}
public function setPrecautionsGeneralesEnglish(?string $precautionsGenerales_english): self
{
$this->precautionsGenerales_english = $precautionsGenerales_english;
return $this;
}
public function getReferenceSKU(): ?string
{
return $this->referenceSKU;
}
public function setReferenceSKU(?string $referenceSKU): self
{
$this->referenceSKU = $referenceSKU;
return $this;
}
public function isFeatured(): ?bool
{
return $this->featured;
}
public function setFeatured(?bool $featured): self
{
$this->featured = $featured;
return $this;
}
public function getTva(): ?string
{
return $this->tva;
}
public function setTva(?string $tva): self
{
$this->tva = $tva;
return $this;
}
public function isIsMonthProduct(): ?bool
{
return $this->isMonthProduct;
}
public function setIsMonthProduct(?bool $isMonthProduct): self
{
$this->isMonthProduct = $isMonthProduct;
return $this;
}
public function getMajDate(): ?\DateTimeInterface
{
return $this->majDate;
}
public function setMajDate(?\DateTimeInterface $majDate): self
{
$this->majDate = $majDate;
return $this;
}
public function getStatus(): ?int
{
return $this->status;
}
public function setStatus(?int $status): self
{
$this->status = $status;
return $this;
}
}