<?php
/**
* @author Leo BANNHOLTZER (contact@scaledev.fr)
* @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
* @license commercial
*/
declare(strict_types=1);
namespace Bluue\ProductsBundle\Entity;
use App\Entity\Context;
use App\Entity\TaxRule;
use App\Entity\Currency;
use App\Entity\UnitMeasure;
use Symfony\Component\Uid\Uuid;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use App\Entity\Traits\ContextServiceEntity;
use Doctrine\Common\Collections\Collection;
use Bluue\ProductsBundle\Entity\ProductType;
use Bluue\ProductsBundle\Entity\ProductContext;
use Bluue\ProductsBundle\Entity\RelatedProduct;
use Bluue\ProductsBundle\Entity\PackProduct;
use Doctrine\Common\Collections\ArrayCollection;
use Bluue\ProductsBundle\Entity\ProductAttachment;
use Bluue\ProductsBundle\Repository\ProductRepository;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
use Bluue\ProductsBundle\Validator\UniqueProductReferenceBrand;
use App\DoctrineExtensions\Timestampable\Traits\UserTimestampableEntity;
use App\DoctrineExtensions\SoftDeleteable\Traits\UserSoftDeleteableEntity;
use App\Entity\User;
use DateTime;
use Bluue\ProductsBundle\Entity\ProductTag;
/**
* @ORM\Entity(repositoryClass=ProductRepository::class)
* @ORM\Table(name="products_bundle__product", indexes={
* @ORM\Index(name="reference_brand", columns={"reference_brand"}),
* @ORM\Index(name="serial_number", columns={"serial_number"}),
* @ORM\Index(name="deleted_at", columns={"deleted_at"}),
* @ORM\Index(name="created_at", columns={"created_at"}),
* @ORM\Index(name="updated_at", columns={"updated_at"}),
* @ORM\Index(name="energy_code", columns={"energy_code"}),
* },
* )
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=false)
* @UniqueProductReferenceBrand
*/
class Product
{
use UserTimestampableEntity;
use UserSoftDeleteableEntity;
use ContextServiceEntity;
/**
* @ORM\Id
* @ORM\Column(type="uuid")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
*/
private ?Uuid $id = null;
/**
* @ORM\ManyToOne(targetEntity=ProductType::class, inversedBy="products", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
private ProductType $product_type;
/**
* @ORM\ManyToOne(targetEntity=UnitMeasure::class)
*/
private ?UnitMeasure $unit_measure = null;
/**
* @ORM\ManyToOne(targetEntity=Brand::class, inversedBy="products")
*/
private ?Brand $brand = null;
/**
* @ORM\ManyToOne(targetEntity=Product::class, inversedBy="childProducts")
*/
private ?Product $parent = null;
/**
* @ORM\OneToMany(targetEntity=Product::class, mappedBy="parent", cascade={"remove"}, fetch="EXTRA_LAZY")
*/
private Collection $childProducts;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $reference_brand = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $serialNumber = null;
/**
* @ORM\OneToMany(
* targetEntity=Ean13::class,
* mappedBy="product",
* cascade={"persist", "remove"},
* fetch="EXTRA_LAZY")
*/
private Collection $ean13s;
/**
* @ORM\Column(type="decimal", precision=20, scale=6, nullable=true)
*/
private ?string $initial_unit = null;
/**
* @ORM\Column(type="decimal", precision=20, scale=6, nullable=true)
*/
private ?string $weight;
/**
* @ORM\Column(type="decimal", precision=20, scale=6, nullable=true)
*/
private ?string $width;
/**
* @ORM\Column(type="decimal", precision=20, scale=6, nullable=true)
*/
private ?string $height;
/**
* @ORM\Column(type="decimal", precision=20, scale=6, nullable=true)
*/
private ?string $depth;
/**
* @ORM\Column(type="string", length=10, nullable=true)
*/
private ?string $customsCode = null;
/**
* @ORM\Column(type="boolean", options={"default" : "0"})
*/
private bool $manufacturedPack = false;
/**
* @ORM\Column(type="boolean", options={"default" : "0"})
*/
private bool $sellPriceFromChildren = false;
/**
* @ORM\Column(type="boolean", options={"default" : "0"})
*/
private bool $retrieveInfoFromFirstChildOfPack = false;
/**
* @ORM\Column(type="string", nullable=true)
*/
private ?string $energyCode = null;
/**
* @ORM\OneToMany(targetEntity=Declination::class, mappedBy="product", cascade={"remove"}, fetch="EXTRA_LAZY")
*/
private Collection $declinations;
/**
* @ORM\OneToMany(targetEntity=ProductContext::class, mappedBy="product", cascade={"remove"}, fetch="EXTRA_LAZY")
*/
private Collection $productContexts;
/**
* @ORM\OneToMany(targetEntity=RelatedProduct::class, mappedBy="parent", cascade={"remove"}, fetch="EXTRA_LAZY")
* @ORM\OrderBy({"position" = "ASC"})
*/
private Collection $relatedProducts;
/**
* @ORM\OneToMany(targetEntity=PackProduct::class, mappedBy="parent", cascade={"remove"}, fetch="EXTRA_LAZY")
* @ORM\OrderBy({"position" = "ASC"})
*/
private Collection $packProducts;
/**
* @ORM\OneToMany(
* targetEntity=ProductAttachment::class,
* mappedBy="product",
* cascade={"persist", "remove"},
* fetch="EXTRA_LAZY"
* )
*/
private Collection $productAttachments;
/**
* @ORM\Column(type="boolean", options={"default" : "0"})
*/
private bool $noCustomerDiscount = false;
/**
* @ORM\Column(type="json", nullable="true")
*/
private array $options = [];
private ?Context $context = null;
/**
* @ORM\OneToMany(
* targetEntity="ProductTag",
* mappedBy="product",
* fetch="EXTRA_LAZY",
* cascade={"persist", "remove"}
* )
*/
private Collection $productTags;
/**
* @ORM\ManyToMany(targetEntity=User::class, fetch="EXTRA_LAZY")
* @ORM\JoinTable(
* name="products_bundle__product_user",
* joinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}
* )
*/
private Collection $users;
// For stock bundle
private Collection $stockProducts;
private Collection $stockProductRoots;
public $stockProductConfiguration = null;
public ?object $stockProductRoot = null;
public $quantity_real = null;
public $quantity_virtual = null;
public $product_suppliers = null;
public $giftCards = null;
public $applyWholesalePriceToAllContexts = null;
public $applySellPriceToAllContexts = null;
public function __construct()
{
$this->childProducts = new ArrayCollection();
$this->declinations = new ArrayCollection();
$this->productContexts = new ArrayCollection();
$this->relatedProducts = new ArrayCollection();
$this->packProducts = new ArrayCollection();
$this->productAttachments = new ArrayCollection();
$this->productTags = new ArrayCollection();
$this->users = new ArrayCollection();
// For stock bundle.
$this->stockProducts = new ArrayCollection();
$this->stockProductRoots = new ArrayCollection();
$this->ean13s = new ArrayCollection();
$this->product_suppliers = new ArrayCollection();
$this->giftCards = new ArrayCollection();
}
/**
* @return Uuid|null
*/
public function getId(): ?Uuid
{
return $this->id;
}
/**
* @return ProductType|null
*/
public function getProductType(): ?ProductType
{
return $this->product_type;
}
/**
* @param ProductType|null $product_type
* @return $this
*/
public function setProductType(?ProductType $product_type): self
{
$this->product_type = $product_type;
return $this;
}
/**
* @return UnitMeasure|null
*/
public function getUnitMeasure(): ?UnitMeasure
{
if ($this->unit_measure) {
return $this->unit_measure;
} elseif ($this->getParent() && $this->getParent()->getUnitMeasure()) {
return $this->getParent()->getUnitMeasure();
}
return null;
}
/**
* @param UnitMeasure|null $unit_measure
* @return $this
*/
public function setUnitMeasure(?UnitMeasure $unit_measure): self
{
$this->unit_measure = $unit_measure;
return $this;
}
/**
* @return Brand|null
*/
public function getBrand(): ?Brand
{
return $this->brand;
}
/**
* @param Brand|null $brand
* @return $this
*/
public function setBrand(?Brand $brand): self
{
$this->brand = $brand;
return $this;
}
/**
* @return self|null
*/
public function getParent(): ?self
{
return $this->parent;
}
/**
* @param self|null $parent
* @return $this
*/
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection|self[]
*/
public function getChildProducts(): Collection
{
return $this->childProducts;
}
/**
* @param self $childProduct
* @return $this
*/
public function addChildProduct(self $childProduct): self
{
if (!$this->childProducts->contains($childProduct)) {
$this->childProducts[] = $childProduct;
$childProduct->setParent($this);
}
return $this;
}
/**
* @param self $childProduct
* @return $this
*/
public function removeChildProduct(self $childProduct): self
{
if ($this->childProducts->removeElement($childProduct)) {
if ($childProduct->getParent() === $this) {
$childProduct->setParent(null);
}
}
return $this;
}
/**
* @return string|null
*/
public function getReferenceBrand(): ?string
{
return $this->reference_brand;
}
/**
* @param string|null $reference_brand
* @return $this
*/
public function setReferenceBrand(?string $reference_brand): self
{
$this->reference_brand = $reference_brand;
return $this;
}
/**
* @return string|null
*/
public function getSerialNumber(): ?string
{
return $this->serialNumber;
}
/**
* @param string|null $serialNumber
* @return $this
*/
public function setSerialNumber(?string $serialNumber): self
{
$this->serialNumber = $serialNumber;
return $this;
}
/**
* @return Collection|Ean13[]
*/
public function getEan13s(): Collection
{
return $this->ean13s;
}
/**
* @param Ean13 $ean13
* @return $this
*/
public function addEan13(Ean13 $ean13): self
{
if (!$this->ean13s->contains($ean13)) {
$this->ean13s[] = $ean13;
$ean13->setProduct($this);
}
return $this;
}
/**
* @param Ean13 $ean13
* @return $this
*/
public function removeEan13(Ean13 $ean13): self
{
$this->ean13s->removeElement($ean13);
return $this;
}
/**
* @return $this
*/
public function removeAllEan13(): self
{
$this->ean13s->slice(0);
return $this;
}
public function getDefaultEan13()
{
$ean13 = $this->ean13s->filter(function ($item) {
return $item->getIsDefault();
});
return count($ean13) ? $ean13->first() : false;
}
public function showDefaultEan13(): string
{
$default_ean13 = $this->getDefaultEan13();
return $default_ean13 && !empty($default_ean13->getValue()) ? $default_ean13->getValue() : '';
}
/**
* @return string|null
*/
public function getInitialUnit(): ?string
{
return $this->initial_unit;
}
/**
* @param string|null $initial_unit
* @return $this
*/
public function setInitialUnit(?string $initial_unit): self
{
$this->initial_unit = $initial_unit;
return $this;
}
/**
* @return string|null
*/
public function getWeight(): ?string
{
return $this->weight;
}
/**
* @param string|null $weight
* @return $this
*/
public function setWeight(?string $weight): self
{
$this->weight = $weight;
return $this;
}
/**
* @return string|null
*/
public function getWidth(): ?string
{
return $this->width;
}
/**
* @param string|null $width
* @return $this
*/
public function setWidth(?string $width): self
{
$this->width = $width;
return $this;
}
/**
* @return string|null
*/
public function getHeight(): ?string
{
return $this->height;
}
/**
* @param string|null $height
* @return $this
*/
public function setHeight(?string $height): self
{
$this->height = $height;
return $this;
}
/**
* @return string|null
*/
public function getDepth(): ?string
{
return $this->depth;
}
/**
* @param string|null $depth
* @return $this
*/
public function setDepth(?string $depth): self
{
$this->depth = $depth;
return $this;
}
/**
* https://www.tarifdouanier.eu/
* @return string|null
*/
public function getCustomsCode(): ?string
{
return $this->customsCode;
}
/**
* @param string|null $customsCode
* @return $this
*/
public function setCustomsCode(?string $customsCode): self
{
$this->customsCode = $customsCode;
return $this;
}
/**
* @return bool
*/
public function isManufacturedPack(): bool
{
return $this->manufacturedPack;
}
/**
* @param bool $manufacturedPack
* @return Product
*/
public function setManufacturedPack(bool $manufacturedPack): Product
{
$this->manufacturedPack = $manufacturedPack;
return $this;
}
/**
* @return bool
*/
public function isSellPriceFromChildren(): bool
{
return $this->sellPriceFromChildren;
}
/**
* @param bool $sellPriceFromChildren
* @return Product
*/
public function setSellPriceFromChildren(bool $sellPriceFromChildren): Product
{
$this->sellPriceFromChildren = $sellPriceFromChildren;
return $this;
}
/**
* @return bool
*/
public function getRetrieveInfoFromFirstChildOfPack(): bool
{
return $this->retrieveInfoFromFirstChildOfPack;
}
/**
* @param bool $retrieveInfoFromFirstChildOfPack
* @return Product
*/
public function setRetrieveInfoFromFirstChildOfPack(bool $retrieveInfoFromFirstChildOfPack): Product
{
$this->retrieveInfoFromFirstChildOfPack = $retrieveInfoFromFirstChildOfPack;
return $this;
}
/**
* @return string|null
*/
public function getEnergyCode(): ?string
{
return $this->energyCode;
}
/**
* @param string|null $energyCode
* @return $this
*/
public function setEnergyCode(?string $energyCode): self
{
$this->energyCode = $energyCode;
return $this;
}
/**
* @return Collection|Declination[]
*/
public function getDeclinations(): Collection
{
$declinations = $this->declinations->toArray();
usort($declinations, function ($a, $b) {
return $a->getName() <=> $b->getName();
});
return new ArrayCollection($declinations);
}
/**
* @param Declination $declination
* @return $this
*/
public function addDeclination(Declination $declination): self
{
if (!$this->declinations->contains($declination)) {
$this->declinations[] = $declination;
$declination->setProduct($this);
}
return $this;
}
/**
* @param Declination $declination
* @return $this
*/
public function removeDeclination(Declination $declination): self
{
if ($this->declinations->removeElement($declination)) {
if ($declination->getProduct() === $this) {
$declination->setProduct(null);
}
}
return $this;
}
/**
* @return Collection|ProductContext[]
*/
public function getProductContexts(): Collection
{
return $this->productContexts;
}
/**
* @param ProductContext $productContext
* @return $this
*/
public function addProductContext(ProductContext $productContext): self
{
if (!$this->productContexts->contains($productContext)) {
$this->productContexts[] = $productContext;
$productContext->setProduct($this);
}
return $this;
}
/**
* @param ProductContext $productContext
* @return $this
*/
public function removeProductContext(ProductContext $productContext): self
{
if ($this->productContexts->removeElement($productContext)) {
if ($productContext->getProduct() === $this) {
$productContext->setProduct(null);
}
}
return $this;
}
/**
* @param ?Context $context
* @return ProductContext|null
*/
public function getProductContext(?Context $context = null): ?ProductContext
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc;
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return ProductImage|null
*/
public function getCoverImageOfProductContext(?Context $context = null): ?ProductImage
{
$productContext = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$productContext = $pc;
}
}
}
$image = null;
if ($productContext) {
foreach ($productContext->getProductImages() as $pi) {
if ($pi->getIsCover()) {
$image = $pi;
}
}
}
return $image;
}
/**
* @return Collection|RelatedProduct[]
*/
public function getRelatedProducts(): Collection
{
return $this->relatedProducts;
}
/**
* @param RelatedProduct $relatedProduct
* @return $this
*/
public function addRelatedProduct(RelatedProduct $relatedProduct): self
{
if (!$this->relatedProducts->contains($relatedProduct)) {
$this->relatedProducts[] = $relatedProduct;
$relatedProduct->setParent($this);
}
return $this;
}
/**
* @return Collection|PackProduct[]
*/
public function getPackProducts(): Collection
{
return $this->packProducts;
}
/**
* @param PackProduct $packProduct
* @return $this
*/
public function addPackProduct(PackProduct $packProduct): self
{
if (!$this->packProducts->contains($packProduct)) {
$this->packProducts[] = $packProduct;
$packProduct->setParent($this);
}
return $this;
}
/**
* @return Collection|ProductAttachment[]
*/
public function getProductAttachments(): Collection
{
return $this->productAttachments;
}
/**
* @param ProductAttachment $productAttachment
* @return $this
*/
public function addProductAttachment(ProductAttachment $productAttachment): self
{
if (!$this->productAttachments->contains($productAttachment)) {
$this->productAttachments[] = $productAttachment;
$productAttachment->setProduct($this);
}
return $this;
}
/**
* @param bool|null $noCustomerDiscount
* @return $this
*/
public function setNoCustomerDiscount(?bool $noCustomerDiscount): self
{
$this->noCustomerDiscount = $noCustomerDiscount;
return $this;
}
/**
* @return bool|null
*/
public function getNoCustomerDiscount(): ?bool
{
return $this->noCustomerDiscount;
}
/**
* @return array
*/
public function getOptions(): array
{
return $this->options;
}
/**
* @param array $options
* @return $this
*/
public function setOptions(array $options): self
{
$this->options = $options;
return $this;
}
/**
* @param array $options
* @return $this
*/
public function addOptions(array $options): self
{
return $this->setOptions(array_merge($this->options, $options));
}
/**
* @return UnitMeasure|null
*/
public function getRootUnitMeasure(): ?UnitMeasure
{
$rootUnitMeasure = null;
if ($this->getChildProducts()->count()) {
foreach ($this->getChildProducts() as $childProduct) {
if ($childProduct->getUnitMeasure()) {
if ($childProduct->getUnitMeasure()->getParent()) {
$rootUnitMeasure = $childProduct->getUnitMeasure()->getParent();
} else {
$rootUnitMeasure = $childProduct->getUnitMeasure();
}
break;
}
}
} elseif ($this->getDeclinations()->count()) {
foreach ($this->getDeclinations() as $declination) {
if ($declination->getUnitMeasure()) {
if ($declination->getUnitMeasure()->getParent()) {
$rootUnitMeasure = $declination->getUnitMeasure()->getParent();
} else {
$rootUnitMeasure = $declination->getUnitMeasure();
}
break;
}
}
} elseif ($this->getParent() && $this->getParent()->getUnitMeasure()) {
if ($this->getParent()->getUnitMeasure()->getParent()) {
$rootUnitMeasure = $this->getParent()->getUnitMeasure()->getParent();
} else {
$rootUnitMeasure = $this->getParent()->getUnitMeasure();
}
}
return $rootUnitMeasure;
}
/**
* @param ?Context $context
* @return string|null
*/
public function getName(?Context $context = null): ?string
{
$value = null;
if ($this->getId()) {
if (
$this->getProductContexts()->count() == 1 &&
$this->getProductContexts()->first()->getContext() != $context
) {
$context = $this->getProductContexts()->first()->getContext();
}
if (
!$context
&& !$this->getProductContexts()->isEmpty()
&& !$this->getProductContext($this->context ?: $this->contextService->getActualOrDefault())
) {
$context = $this->getProductContexts()->first()->getContext();
}
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getName();
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return string|null
*/
public function getMetaTagName(?Context $context = null): ?string
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getMetaTagName();
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return string|null
*/
public function getDescription(?Context $context = null): ?string
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getDescription();
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return string|null
*/
public function getMetaTagDescription(?Context $context = null): ?string
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getMetaTagDescription();
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return string|null
*/
public function getSummary(?Context $context = null): ?string
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getSummary();
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return string|null
*/
public function getSource(?Context $context = null): ?string
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getSource();
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return EcoPart|null
*/
public function getEcoPart(?Context $context = null): ?EcoPart
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getEcoPart();
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return string|null
*/
public function getEcoPartAmount(?Context $context = null): ?string
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getEcoPartAmount();
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return TaxRule|null
*/
public function getTaxRule(?Context $context = null): ?TaxRule
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getTaxRule();
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return Currency|null
*/
public function getCurrency(?Context $context = null): ?Currency
{
$value = null;
if ($this->getId()) {
if (
$this->getProductContexts()->count() == 1 &&
$this->getProductContexts()->first()->getContext() != $context
) {
$context = $this->getProductContexts()->first()->getContext();
}
if (
!$context
&& !$this->getProductContexts()->isEmpty()
&& !$this->getProductContext($this->context ?: $this->contextService->getActualOrDefault())
) {
$context = $this->getProductContexts()->first()->getContext();
}
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getCurrency();
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return string|null
*/
public function getLinkRewrite(?Context $context = null): ?string
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getLinkRewrite();
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return bool|null
*/
public function getIsActive(?Context $context = null): ?bool
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getIsActive();
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return bool|null
*/
public function getAvailableForOrder(?Context $context = null): ?bool
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getAvailableForOrder();
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return string|null
*/
public function getReference(?Context $context = null): ?string
{
$value = null;
if ($this->getId()) {
if (
$this->getProductContexts()->count() == 1 &&
$this->getProductContexts()->first()->getContext() != $context
) {
$context = $this->getProductContexts()->first()->getContext();
}
if (
!$context
&& !$this->getProductContexts()->isEmpty()
&& !$this->getProductContext($this->context ?: $this->contextService->getActualOrDefault())
) {
$context = $this->getProductContexts()->first()->getContext();
}
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getReference();
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return string|null
*/
public function getWholesalePrice(?Context $context = null): ?string
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getWholesalePrice();
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return DateTime|null
*/
public function getCreatedAtWithContext(?Context $context = null): ?DateTime
{
$value = $this->getCreatedAt();
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getCreatedAt();
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return DateTime|null
*/
public function getUpdatedAtWithContext(?Context $context = null): ?DateTime
{
$value = $this->getUpdatedAt();
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getUpdatedAt();
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return string|null
*/
public function getSellPrice(?Context $context = null): ?string
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getSellPrice();
}
}
}
return $value;
}
/**
* @param ?Context $context
* @return bool|null
*/
public function isChangeableTaxRule(?Context $context = null): ?bool
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->isChangeableTaxRule();
}
}
}
return $value;
}
/**
* @param ?Context $context
*/
public function getTranslations(?Context $context = null)
{
$collection = [];
if ($this->getId()) {
if (
$this->getProductContexts()->count() == 1 &&
$this->getProductContexts()->first()->getContext() != $context
) {
$context = $this->getProductContexts()->first()->getContext();
}
if (
!$context
&& !$this->getProductContexts()->isEmpty()
&& !$this->getProductContext($this->context ?: $this->contextService->getActualOrDefault())
) {
$context = $this->getProductContexts()->first()->getContext();
}
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$collection = $pc->getTranslations();
}
}
}
return $collection;
}
/**
* @param Context|null $context
* @return string
*/
public function getChoiceLabel(?Context $context = null): string
{
if (
$this->getProductContexts()->count() == 1 &&
$this->getProductContexts()->first()->getContext() != $context
) {
$context = $this->getProductContexts()->first()->getContext();
}
if (
!$context
&& !$this->getProductContexts()->isEmpty()
&& !$this->getProductContext($this->context ?: $this->contextService->getActualOrDefault())
) {
$context = $this->getProductContexts()->first()->getContext();
}
$return = '';
if ($ref = $this->getReference($context)) {
$return = '[' . $ref . '] ';
}
return $return . $this->getName($context);
}
/**
* @return bool
*/
public function getIsPack(): bool
{
return $this->getId() && $this->getProductType()->getNumber() == 3;
}
// For stock bundle
/**
* @param Uuid|null $warehouseId
* @param Context|null $context
* @return Collection
*/
public function getStockProducts(?Uuid $warehouseId = null, ?Context $context = null): Collection
{
$stockProducts = $this->stockProducts->filter(function ($stockProduct) use ($context) {
if ($context) {
return !$stockProduct->getDeclination() &&
$stockProduct->getStockProductRootContexts()->filter(
function ($stockProductRootContext) use ($context) {
return $stockProductRootContext->getContext() == $context;
}
)->count() > 0;
}
return !$stockProduct->getDeclination();
});
if ($warehouseId) {
$stockProducts = $stockProducts->filter(function ($stockProduct) use ($warehouseId) {
return $stockProduct->getWarehouse()->getId() == $warehouseId;
});
}
return $stockProducts;
}
/**
* @return Collection
*/
public function getStockProductRoots(): Collection
{
return $this->stockProductRoots;
}
/**
* @return object|null
*/
public function getStockProductRoot(): ?object
{
$stockProductRoots = $this->stockProductRoots->filter(function ($item) {
return !$item->getDeclination();
});
return count($stockProductRoots) ? $stockProductRoots->first() : null;
}
/**
* @param $stockProductRoot
* @return $this
*/
public function addStockProductRoot($stockProductRoot): self
{
if (!$this->stockProductRoots->contains($stockProductRoot)) {
$this->stockProductRoots[] = $stockProductRoot;
$stockProductRoot->setProduct($this);
}
return $this;
}
/**
* @param $stockProductRoot
* @return $this
*/
public function removeStockProductRoot($stockProductRoot): self
{
if ($this->stockProductRoots->removeElement($stockProductRoot)) {
if ($stockProductRoot->getProduct() === $this) {
$stockProductRoot->setProduct(null);
}
}
return $this;
}
/**
* @param Uuid|null $warehouseId
* @return int
*/
public function getStockProductsFinalQuantity(?Uuid $warehouseId = null): int
{
$quantity = 0;
if ($warehouseId) {
foreach ($this->getStockProducts($warehouseId) as $stock_product) {
$quantity += $stock_product->getQuantityAvailable();
}
} else {
$quantity += $this->getStockProductRoot() ? $this->getStockProductRoot()->getQuantityAvailable() : 0;
}
return $quantity;
}
/**
* @param $stockProducts
* @return Product
*/
public function setStockProducts($stockProducts): self
{
$this->stockProducts = $stockProducts;
return $this;
}
/**
* @return Product
*/
public function setStockProductRoot($stockProductRoot): self
{
$this->stockProductRoot = $stockProductRoot;
return $this;
}
/**
* @param Context|null $context
* @return object|null
*/
public function getStockProductRootContext(?Context $context = null): ?object
{
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
return $this->getStockProductRoot() ?
$this->getStockProductRoot()->getStockProductRootContext($this->context) : null
;
}
public function getDefaultProductSupplier()
{
if ($this->product_suppliers) {
if (!$this->context) {
$this->context = $this->contextService->getActualOrDefault();
}
foreach ($this->product_suppliers as $product_supplier) {
foreach ($product_supplier->getProductSupplierContexts() as $psContext) {
if ($psContext->getContext()->getId() == $this->context->getId() && $psContext->getIsDefault()) {
return $product_supplier;
}
}
}
}
return false;
}
/**
* @return Collection|ProductTag[]
*/
public function getProductTags(): Collection
{
return $this->productTags;
}
/**
* @param ProductTag $productTag
* @return $this
*/
public function addProductTag(?ProductTag $productTag): self
{
if (!$this->productTags->contains($productTag)) {
$this->productTags[] = $productTag;
$productTag->setProduct($this);
}
return $this;
}
/**
* @param ProductTag $productTag
* @return $this
*/
public function removeProductTag(ProductTag $productTag): self
{
$this->productTags->removeElement($productTag);
return $this;
}
public function getUsers(): Collection
{
return $this->users;
}
public function getGiftCards()
{
return $this->giftCards;
}
public function getIsGiftCard()
{
return $this->getId() && $this->getProductType()->getNumber() == 10 ? true : false;
}
/**
* @param Context $context
* @return int|null
*/
public function getQuantityMin(?Context $context = null)
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getQuantityMin();
}
}
}
return $value;
}
/**
* @return string
*/
public function getVisibility(?Context $context): ?string
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getVisibility();
}
}
}
return $value;
}
/**
* @param Context|null $context
* @return string|null
*/
public function getRedirectType(?Context $context = null): ?string
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getRedirectType();
}
}
}
return $value;
}
/**
* @param Context|null $context
* @return Product|null
*/
public function getProductRedirect(?Context $context = null): ?Product
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getProductRedirect();
}
}
}
return $value;
}
/**
* @param Context|null $context
* @return object|null
*/
public function getCategoryRedirect(?Context $context = null): ?object
{
$value = null;
if ($this->getId()) {
if (!$this->context) {
$this->context = $context ?: $this->contextService->getActualOrDefault();
} elseif ($context && $context != $this->context) {
$this->context = $context;
}
foreach ($this->getProductContexts() as $pc) {
if ($pc->getContext() == $this->context) {
$value = $pc->getCategoryRedirect();
}
}
}
return $value;
}
}