<?php
declare(strict_types=1);
namespace App\Entity\Product;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Core\Model\ProductVariant as BaseProductVariant;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Webmozart\Assert\Assert;
use Sylius\Component\Attribute\Model\AttributeValueInterface;
use App\Entity\Order\OrderItem;
class ProductVariant extends BaseProductVariant implements ProductVariantInterface
{
public const COATING_TYPE_VALUES = [
'coating_mat',
'coating_brillant',
'coating_inner',
'fabric_heat_sealed'
];
public const GIFT_FOR_VALUES = [
// 'him',
// 'her'
'7cce7202-a23b-11ed-ab1e-bae0dddde718',
'7cce7cb6-a23b-11ed-9292-bae0dddde718'
];
public const MAIN_MATERIAL_VALUES = [
'linen',
'cotton',
'gauze',
'velvet',
'coated_cotton',
'cotton_voile',
'jersey',
'pleather',
'suede_fabric',
'hand_painted'
];
// Selectionne les produits de base (en dur)
// Grande besace : BASE.BES.CAR.G
// Petite besace : BASE.BES.P
// Sac compagnon : BASE.SAC.COM
// --
// Rabat grande besace : RABA.BES.CAR.G
// Rabat petite besace : RABA.BES.P
// Rabat besace petit nuage : RABA.BES.PET.
// Rabat sac compagnon : RABA.SAC.COM
// --
// Petite Lanière : LANI.P
// Grande lanière : LANI.G
public const SACZIP_FORMES = [
'base' => [
'grand_besace' => 'BASE.BES.CAR.G',
'petite_besace' => 'BASE.BES.P',
'sac_compagnon' => 'BASE.SAC.COM'
],
'rabat' => [
'rabat_grande_besace' => 'RABA.BES.CAR.G',
'rabat_petite_besace' => 'RABA.BES.P',
'rabat_besace_petite_nuage' => 'RABA.BES.PET.',
'rabat_sac_compagnon' => 'RABA.SAC.COM'
],
'laniere' => [
'petite_laniere' => 'LANI.P',
'grande_laniere' => 'LANI.G'
]
];
protected $id;
protected $incomingStock;
protected $dateIncomingStock;
protected $attributes;
protected $coatingType;
protected $coatingVegetal;
protected $mainMaterial;
protected $giftFor;
protected $badge;
protected $tags;
protected $pricePerTwo;
protected $oldUrl;
protected $itemMin=5;
protected $isBlocked;
protected $taxons;
public function __construct()
{
parent::__construct();
$this->attributes = new ArrayCollection();
}
public function setId(int $id): void
{
$this->id = $id;
}
public function getIncomingStock(): ?int
{
return $this->incomingStock;
}
public function setIncomingStock(?int $incomingStock): void
{
$this->incomingStock = $incomingStock;
}
public function getDateIncomingStock()
{
return $this->dateIncomingStock;
}
public function setDateIncomingStock($dateIncomingStock): void
{
$this->dateIncomingStock = $dateIncomingStock;
}
public function getCoatingType(): ?string
{
return $this->coatingType;
}
public function setCoatingType(?string $coatingType): void
{
$this->coatingType = $coatingType;
}
public function getCoatingVegetal(): ?bool
{
return $this->coatingVegetal;
}
public function setCoatingVegetal(?bool $coatingVegetal): void
{
$this->coatingVegetal = $coatingVegetal;
}
public function getMainMaterial(): ?string
{
return $this->mainMaterial;
}
public function setMainMaterial(?string $mainMaterial): void
{
$this->mainMaterial = $mainMaterial;
}
public function getGiftFor(): ?string
{
return $this->giftFor;
}
public function setGiftFor(?string $giftFor): void
{
$this->giftFor = $giftFor;
}
public function getBadge(): ?string
{
return $this->badge;
}
public function setBadge(?string $badge): void
{
$this->badge = $badge;
}
public function getPricePerTwo(): ?string
{
return $this->pricePerTwo;
}
public function setPricePerTwo(?string $pricePerTwo): void
{
$this->pricePerTwo = $pricePerTwo;
}
public function getOldUrl(): ?string
{
return $this->oldUrl;
}
public function setOldUrl(?string $oldUrl): void
{
$this->oldUrl = $oldUrl;
}
public function getIsBlocked(): ?bool
{
return $this->isBlocked;
}
public function setIsBlocked(?bool $isBlocked): void
{
$this->isBlocked = $isBlocked;
}
public function getSlug(): ?string
{
return $this->getTranslation()->getSlug();
}
public function setSlug(?string $slug): void
{
$this->getTranslation()->setSlug($slug);
}
public function getDescription(): ?string
{
return $this->getTranslation()->getDescription();
}
public function setDescription(?string $description): void
{
$this->getTranslation()->setDescription($description);
}
public function getAdvantages(): ?string
{
return $this->getTranslation()->getAdvantages();
}
public function setAdvantages(?string $advantages): void
{
$this->getTranslation()->setAdvantages($advantages);
}
public function getMaintenance(): ?string
{
return $this->getTranslation()->getMaintenance();
}
public function setMaintenance(?string $maintenance): void
{
$this->getTranslation()->setMaintenance($maintenance);
}
public function getAttributes(): Collection
{
// dump('coucou');
// dump($this->attributes);
// exit();
return $this->attributes;
}
public function getAttributesByLocale(
string $localeCode,
string $fallbackLocaleCode,
?string $baseLocaleCode = null
): Collection {
if (null === $baseLocaleCode || $baseLocaleCode === $fallbackLocaleCode) {
$baseLocaleCode = $fallbackLocaleCode;
$fallbackLocaleCode = null;
}
$attributes = $this->attributes->filter(
function (ProductVariantAttributeValueInterface $attribute) use ($baseLocaleCode) {
return $attribute->getLocaleCode() === $baseLocaleCode || null === $attribute->getLocaleCode();
}
);
$attributesWithFallback = [];
foreach ($attributes as $attribute) {
$attributesWithFallback[] = $this->getAttributeInDifferentLocale($attribute, $localeCode, $fallbackLocaleCode);
}
return new ArrayCollection($attributesWithFallback);
}
protected function getAttributeInDifferentLocale(
ProductVariantAttributeValueInterface $attributeValue,
string $localeCode,
?string $fallbackLocaleCode = null
) {
if (!$this->hasNotEmptyAttributeByCodeAndLocale($attributeValue->getCode(), $localeCode)) {
if (
null !== $fallbackLocaleCode &&
$this->hasNotEmptyAttributeByCodeAndLocale($attributeValue->getCode(), $fallbackLocaleCode)
) {
return $this->getAttributeByCodeAndLocale($attributeValue->getCode(), $fallbackLocaleCode);
}
return $attributeValue;
}
return $this->getAttributeByCodeAndLocale($attributeValue->getCode(), $localeCode);
}
protected function hasNotEmptyAttributeByCodeAndLocale(string $attributeCode, string $localeCode): bool
{
$attributeValue = $this->getAttributeByCodeAndLocale($attributeCode, $localeCode);
if (null === $attributeValue) {
return false;
}
$value = $attributeValue->getValue();
if ('' === $value || null === $value || [] === $value) {
return false;
}
return true;
}
public function addAttribute(?ProductVariantAttributeValueInterface $attribute): void
{
/** @var ProductVariantAttributeValueInterface $attribute */
Assert::isInstanceOf(
$attribute,
ProductVariantAttributeValueInterface::class,
'Attribute objects added to a Product object have to implement ProductVariantAttributeValueInterface'
);
if (!$this->hasAttribute($attribute)) {
$attribute->setProductVariant($this);
// dump( $attribute);
// exit();
$this->attributes->add($attribute);
}
}
public function removeAttribute(?ProductVariantAttributeValueInterface $attribute): void
{
/** @var ProductVariantAttributeValueInterface $attribute */
Assert::isInstanceOf(
$attribute,
ProductVariantAttributeValueInterface::class,
'Attribute objects removed from a Product object have to implement ProductVariantAttributeValueInterface'
);
if ($this->hasAttribute($attribute)) {
$this->attributes->removeElement($attribute);
$attribute->setProductVariant(null);
}
}
public function hasAttribute(ProductVariantAttributeValueInterface $attribute): bool
{
return $this->attributes->contains($attribute);
}
public function hasAttributeByCodeAndLocale(string $attributeCode, ?string $localeCode = null): bool
{
$localeCode = $localeCode ?: $this->getTranslation()->getLocale();
foreach ($this->attributes as $attribute) {
if ($attribute->getAttribute()->getCode() === $attributeCode
&& ($attribute->getLocaleCode() === $localeCode || null === $attribute->getLocaleCode())) {
return true;
}
}
return false;
}
public function getAttributeByCodeAndLocale(string $attributeCode, ?string $localeCode = null): ?ProductVariantAttributeValueInterface
{
if (null === $localeCode) {
$localeCode = $this->getTranslation()->getLocale();
}
foreach ($this->attributes as $attribute) {
if ($attribute->getAttribute()->getCode() === $attributeCode &&
($attribute->getLocaleCode() === $localeCode || null === $attribute->getLocaleCode())) {
return $attribute;
}
}
return null;
}
public function setItemMin(int $itemMin)
{
$this->itemMin = $itemMin;
}
public function getItemMin(): int
{
return $this->itemMin;
}
public function canAddItem(): bool
{
// echo 'canAddItem - '.OrderItem::itemMin.' -- '.$this->itemMin;
// if( ($this->getOnHand() - $this->getOnHold() - OrderItem::itemMin /*- $this->getQuantity()*/ ) > 0 && $this->hasImages()){
// return true;
// }
if($this->getIsBlocked()) {
return false;
}
if($this->getId() == 41360) {
dump($this);
}
if( ($this->getOnHand() - $this->getOnHold() - $this->itemMin /*- $this->getQuantity()*/ ) > 0 && $this->hasImages()){
return true;
}
return false;
}
public function countItemStock(): int
{
// return $this->getOnHand() - $this->getOnHold() - OrderItem::itemMin ;
return $this->getOnHand() - $this->getOnHold() - $this->itemMin ;
}
public function countItemStockTissu(): int
{
// return $this->getOnHand() - $this->getOnHold() - OrderItem::itemMin ;
return $this->getOnHand() - $this->getOnHold() - $this->itemMin ;
}
public function getTaxons()
{
$taxons = [];
foreach($this->taxons as $productVariantTaxon) {
$taxons[] = $productVariantTaxon->getTaxon();
}
return $taxons;
}
public function setTaxons($taxons): void
{
$this->taxons = $taxons;
}
public function getTags()
{
if(empty($this->tags)) {
return [];
}
return explode(',', $this->tags);
}
public function setTags($tags): void
{
$this->tags = implode(',', $tags);
}
}