<?php
namespace App\Entity\Gos;
use App\Entity\ContactSegmentationableInterface;
use App\Enum\Product\ProductSourceSystem;
use App\Utils\BC\Import\Mapping\ProductVariant\Creator\Spec\ProductVariantNumbersSpec;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* Product
*
* @ORM\Table(name="product")
* @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
* @ORM\HasLifecycleCallbacks
* @Vich\Uploadable
*/
class Product implements ContactSegmentationableInterface
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @ORM\Column(enumType="App\Enum\Product\ProductSourceSystem", length=20)
*/
private ProductSourceSystem $sourceSystem = ProductSourceSystem::INTERNAL;
/**
* @var bool
*
* @ORM\Column(type="boolean", options={"default":0})
*/
private $isVirtual = false;
/**
* @var bool
*
* @ORM\Column(type="boolean", options={"default":0}, nullable=true)
*/
private $isEvent = false;
/**
* @var bool
*
* @ORM\Column(type="boolean", options={"default":0})
*/
private $isProductPremium = false;
/**
* @var bool
*
* @ORM\Column(type="boolean", options={"default":0})
*/
private $separateProduct = false;
/**
* @var bool
*
* @ORM\Column(type="boolean", options={"default":0})
*/
private $manualVerificationUser = false;
/**
* @var string
*
* @ORM\Column(type="string", length=255)
*/
private $description;
/**
* @var int
*
* @ORM\Column(type="string", length=100, nullable=true)
*/
private $productNo;
/**
* @var string
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $department;
/**
* @var string
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $productState;
/**
* @var string
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $vatRate;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $discountOnline;
/**
* @ORM\Column(type="float", precision=16, scale=4, nullable=true)
*/
private $discountValueOnline;
/**
* @var bool
*
* @ORM\Column(type="boolean", nullable=true)
*/
private $stateIsAvailible;
/**
* @var bool
*
* @ORM\Column(type="boolean", nullable=true)
*/
private $stateIsForcedReservation;
/**
* @var bool
*
* @ORM\Column(type="boolean", nullable=true)
*/
private $stateIsProductTest;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Gos\ProductVariant", mappedBy="product")
*/
private $productVariant;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Gos\ProductCategory", inversedBy="products")
* @ORM\JoinColumn(onDelete="SET NULL")
*/
private $productCategory;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Gos\ProductGroup", inversedBy="products")
* @ORM\JoinColumn()
* @JMS\Groups({"PortalSettings"})
*/
private $productGroups;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Gos\ProductType", inversedBy="products")
* @ORM\JoinColumn()
* @JMS\Groups({"PortalSettings"})
*/
private $productType;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Gos\ProductGosType", inversedBy="products")
* @ORM\JoinColumn()
*/
private $productGosType;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Gos\ProductVariant", mappedBy="masterProduct")
*/
private $masterProductVariant;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Gos\Term", mappedBy="products")
*/
private $terms;
/**
* @ORM\ManyToOne(targetEntity="EventType", inversedBy="products")
* @ORM\JoinColumn()
*/
private $eventType;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @ORM\OneToOne(targetEntity="GoogleMerchantFeed", mappedBy="product")
*/
private $googleMerchantFeed;
/**
* @var string
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $eventPlaceName;
/**
* @var string
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $eventPlaceDesc;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Gos\ProductAssociation", mappedBy="products", fetch="EAGER")
*/
private $productAssociation;
/**
* @ORM\ManyToMany(targetEntity=ContactSegmentation::class)
*/
private $contactSegmentations;
/** @ORM\PrePersist() */
public function prePersist()
{
$this->createdAt = new \DateTime();
}
/** @ORM\PreUpdate() */
public function preUpdate()
{
$this->updatedAt = new \DateTime();
}
public function __toString()
{
return empty($this->productNo) ? $this->description : (string)$this->productNo;
}
public function labelForSelect2()
{
$type = empty($this->getProductGosType()) ? 'NULL' : $this->getProductGosType()->getName();
return (string)$this->productNo . ' (' . $this->description . ') [Type: ' . $type . ']';
}
public function getSourceSystem(): ProductSourceSystem
{
return $this->sourceSystem;
}
public function setSourceSystem(ProductSourceSystem $sourceSystem): void
{
$this->sourceSystem = $sourceSystem;
}
public function getIsEvent(): ?bool
{
if (
!empty($this->getProductGosType())
&& $this->getProductGosType()->getName() == 'event'
)
{
return true;
}
return false;
}
public function getEventStartDate()
{
$date = null;
foreach ($this->getProductVariant() as $productVariant)
{
if (empty($productVariant->getEventDate())) continue;
if ($productVariant->getEventDate() < $date || $date === null)
{
$date = $productVariant->getEventDate();
}
}
return $date;
}
public function getEventDate($return = 'text')
{
$date = null;
/** @var ProductVariant $productVariant */
foreach ($this->getProductVariant() as $productVariant)
{
if (empty($productVariant->getEventDate()))
{
continue;
}
$eventDate = clone $productVariant->getEventDate();
$eventDate->setTime(0,0);
if (empty($date) || $date !== $eventDate)
{
if (is_array($date) && in_array($eventDate, $date))
{
continue;
}
$date[] = $eventDate;
}
}
if ($return == 'text')
{
if (is_array($date))
{
usort($date, function ($a, $b){
if ($a == $b) { return 0; }
return $a < $b ? -1 : 1;
});
if (count($date) == 1)
{
$date = $date[0]->format('d.m.Y');
}
else
{
$date = $date[0]->format('d') . '-' . $date[count($date) - 1]->format('d.m.Y');
}
}
}
elseif ($return = 'json')
{
usort($date, function ($a, $b){
if ($a == $b) { return 0; }
return $a < $b ? -1 : 1;
});
$date = array($date[0]->format('Y-m-d'), $date[count($date) - 1]->format('Y-m-d'));
$date = json_encode($date);
}
return $date;
}
public function getVariant($productVariantNoComplete)
{
foreach ($this->getProductVariant() as $productVariant)
{
if ($productVariant->getProductVariantNoComplete() == $productVariantNoComplete)
return $productVariant;
}
return null;
}
public function getActiveVariants()
{
$activeVariants = new ArrayCollection();
foreach ($this->getProductVariant() as $productVariant)
{
if ($productVariant->isActive())
{
$activeVariants->add($productVariant);
}
}
return $activeVariants;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set description
*
* @param string $description
*
* @return Product
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set productNo
*
* @param integer $productNo
*
* @return Product
*/
public function setProductNo($productNo)
{
$this->productNo = $productNo;
return $this;
}
/**
* Get productNo
*
* @return integer
*/
public function getProductNo()
{
return $this->productNo;
}
/**
* Set department
*
* @param string $department
*
* @return Product
*/
public function setDepartment($department)
{
$this->department = $department;
return $this;
}
/**
* Get department
*
* @return string
*/
public function getDepartment()
{
return $this->department;
}
/**
* Set productState
*
* @param string $productState
*
* @return Product
*/
public function setProductState($productState)
{
$this->productState = $productState;
return $this;
}
/**
* Get productState
*
* @return string
*/
public function getProductState()
{
return $this->productState;
}
/**
* Set vatRate
*
* @param integer $vatRate
*
* @return Product
*/
public function setVatRate($vatRate)
{
$this->vatRate = $vatRate;
return $this;
}
/**
* Get vatRate
*
* @return integer
*/
public function getVatRate()
{
return $this->vatRate;
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
*
* @return Product
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* @param \DateTime $updatedAt
*
* @return Product
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Add productVariant
*
* @param \App\Entity\Gos\ProductVariant $productVariant
*
* @return Product
*/
public function addProductVariant(\App\Entity\Gos\ProductVariant $productVariant)
{
if (!$this->productVariant->contains($productVariant))
{
$this->productVariant[] = $productVariant;
}
return $this;
}
/**
* Remove productVariant
*
* @param \App\Entity\Gos\ProductVariant $productVariant
*/
public function removeProductVariant(\App\Entity\Gos\ProductVariant $productVariant)
{
$this->productVariant->removeElement($productVariant);
}
/**
* Get productVariant
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getProductVariant()
{
return $this->productVariant;
}
/**
* Set productCategory
*
* @param \App\Entity\Gos\ProductCategory $productCategory
*
* @return Product
*/
public function setProductCategory(\App\Entity\Gos\ProductCategory $productCategory = null)
{
$this->productCategory = $productCategory;
return $this;
}
/**
* Get productCategory
*
* @return \App\Entity\Gos\ProductCategory
*/
public function getProductCategory()
{
return $this->productCategory;
}
/**
* Set productGroups
*
* @param \App\Entity\Gos\ProductGroup $productGroups
*
* @return Product
*/
public function setProductGroups(\App\Entity\Gos\ProductGroup $productGroups = null)
{
$this->productGroups = $productGroups;
return $this;
}
/**
* Get productGroups
*
* @return \App\Entity\Gos\ProductGroup
*/
public function getProductGroups()
{
return $this->productGroups;
}
/**
* Set isVirtual
*
* @param boolean $isVirtual
*
* @return Product
*/
public function setIsVirtual($isVirtual)
{
$this->isVirtual = $isVirtual;
return $this;
}
/**
* Get isVirtual
*
* @return boolean
*/
public function getIsVirtual()
{
return $this->isVirtual;
}
/**
* Set isProductPremium
*
* @param boolean $isProductPremium
*
* @return Product
*/
public function setIsProductPremium($isProductPremium)
{
$this->isProductPremium = $isProductPremium;
return $this;
}
/**
* Get isProductPremium
*
* @return boolean
*/
public function getIsProductPremium()
{
return $this->isProductPremium;
}
/**
* Set productType
*
* @param \App\Entity\Gos\ProductType $productType
*
* @return Product
*/
public function setProductType(\App\Entity\Gos\ProductType $productType = null)
{
$this->productType = $productType;
return $this;
}
/**
* Get productType
*
* @return \App\Entity\Gos\ProductType
*/
public function getProductType()
{
return $this->productType;
}
/**
* Set discountOnline
*
* @param boolean $discountOnline
*
* @return Product
*/
public function setDiscountOnline($discountOnline)
{
$this->discountOnline = $discountOnline;
return $this;
}
/**
* Get discountOnline
*
* @return boolean
*/
public function getDiscountOnline()
{
return $this->discountOnline;
}
/**
* Set discountValueOnline
*
* @param float $discountValueOnline
*
* @return Product
*/
public function setDiscountValueOnline($discountValueOnline)
{
$this->discountValueOnline = $discountValueOnline;
return $this;
}
/**
* Get discountValueOnline
*
* @return float
*/
public function getDiscountValueOnline()
{
return $this->discountValueOnline;
}
/**
* Add masterProductVariant
*
* @param \App\Entity\Gos\ProductVariant $masterProductVariant
*
* @return Product
*/
public function addMasterProductVariant(\App\Entity\Gos\ProductVariant $masterProductVariant)
{
$masterProductVariant->setMasterProduct($this);
$this->masterProductVariant[] = $masterProductVariant;
return $this;
}
/**
* Remove masterProductVariant
*
* @param \App\Entity\Gos\ProductVariant $masterProductVariant
*/
public function removeMasterProductVariant(\App\Entity\Gos\ProductVariant $masterProductVariant)
{
$this->masterProductVariant->removeElement($masterProductVariant);
}
/**
* Get masterProductVariant
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getMasterProductVariant()
{
return $this->masterProductVariant;
}
/**
* Set stateIsAvailible
*
* @param boolean $stateIsAvailible
*
* @return Product
*/
public function setStateIsAvailible($stateIsAvailible)
{
$this->stateIsAvailible = $stateIsAvailible;
return $this;
}
/**
* Get stateIsAvailible
*
* @return boolean
*/
public function getStateIsAvailible()
{
return $this->stateIsAvailible;
}
/**
* Set stateIsForcedReservation
*
* @param boolean $stateIsForcedReservation
*
* @return Product
*/
public function setStateIsForcedReservation($stateIsForcedReservation)
{
$this->stateIsForcedReservation = $stateIsForcedReservation;
return $this;
}
/**
* Get stateIsForcedReservation
*
* @return boolean
*/
public function getStateIsForcedReservation()
{
return $this->stateIsForcedReservation;
}
/**
* Set stateIsProductTest
*
* @param boolean $stateIsProductTest
*
* @return Product
*/
public function setStateIsProductTest($stateIsProductTest)
{
$this->stateIsProductTest = $stateIsProductTest;
return $this;
}
/**
* Get stateIsProductTest
*
* @return boolean
*/
public function getStateIsProductTest()
{
return $this->stateIsProductTest;
}
/**
* Add term
*
* @param \App\Entity\Gos\Term $term
*
* @return Product
*/
public function addTerm(\App\Entity\Gos\Term $term)
{
$term->addProduct($this);
$this->terms[] = $term;
return $this;
}
/**
* Remove term
*
* @param \App\Entity\Gos\Term $term
*/
public function removeTerm(\App\Entity\Gos\Term $term)
{
$term->removeProduct($this);
$this->terms->removeElement($term);
}
/**
* Get terms
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTerms()
{
return $this->terms;
}
/**
* Set separateProduct
*
* @param boolean $separateProduct
*
* @return Product
*/
public function setSeparateProduct($separateProduct)
{
$this->separateProduct = $separateProduct;
return $this;
}
/**
* Get separateProduct
*
* @return boolean
*/
public function getSeparateProduct()
{
return $this->separateProduct;
}
/**
* Set manualVerificationUser
*
* @param boolean $manualVerificationUser
*
* @return Product
*/
public function setManualVerificationUser($manualVerificationUser)
{
$this->manualVerificationUser = $manualVerificationUser;
return $this;
}
/**
* Get manualVerificationUser
*
* @return boolean
*/
public function getManualVerificationUser()
{
return $this->manualVerificationUser;
}
/**
* Set googleMerchantFeed
*
* @param \App\Entity\Gos\GoogleMerchantFeed $googleMerchantFeed
*
* @return ProductVariant
*/
public function setGoogleMerchantFeed(\App\Entity\Gos\GoogleMerchantFeed $googleMerchantFeed = null)
{
$this->googleMerchantFeed = $googleMerchantFeed;
return $this;
}
/**
* Get googleMerchantFeed
*
* @return \App\Entity\Gos\GoogleMerchantFeed
*/
public function getGoogleMerchantFeed()
{
return $this->googleMerchantFeed;
}
/**
* Constructor
*/
public function __construct()
{
$this->productVariant = new ArrayCollection();
$this->masterProductVariant = new ArrayCollection();
$this->terms = new ArrayCollection();
$this->productAssociation = new ArrayCollection();
$this->contactSegmentations = new ArrayCollection();
}
public function getEventType(): ?EventType
{
return $this->eventType;
}
public function setEventType(?EventType $eventType): self
{
$this->eventType = $eventType;
return $this;
}
public function getEventPlaceName(): ?string
{
return $this->eventPlaceName;
}
public function setEventPlaceName(?string $eventPlaceName): self
{
$this->eventPlaceName = $eventPlaceName;
return $this;
}
public function getEventPlaceDesc(): ?string
{
return $this->eventPlaceDesc;
}
public function setEventPlaceDesc(?string $eventPlaceDesc): self
{
$this->eventPlaceDesc = $eventPlaceDesc;
return $this;
}
public function getProductAssociation($id = null)
{
if ($id)
{
return $this->productAssociation->filter(function($productAssociation) use ($id) {
if ($productAssociation->getId() == $id) return $productAssociation;
})->first();
}
return $this->productAssociation;
}
public function getSingleProductAssociation()
{
return $this->productAssociation->first();
}
/**
* @param ProductAssociation $productAssociation
* @return Product
*/
public function addProductAssociation(ProductAssociation $productAssociation): self
{
if (!$this->productAssociation->contains($productAssociation)) {
$this->productAssociation[] = $productAssociation;
$productAssociation->addProduct($this);
}
return $this;
}
/**
* @param ProductAssociation $productAssociation
* @return Product
*/
public function removeProductAssociation(ProductAssociation $productAssociation): self
{
if ($this->productAssociation->contains($productAssociation)) {
$this->productAssociation->removeElement($productAssociation);
$productAssociation->removeProduct($this);
}
return $this;
}
public function getProductGosType(): ?ProductGosType
{
return $this->productGosType;
}
public function setProductGosType(?ProductGosType $productGosType): self
{
$this->productGosType = $productGosType;
return $this;
}
public function setIsEvent(?bool $isEvent): self
{
$this->isEvent = $isEvent;
return $this;
}
/**
* @return Collection|ContactSegmentation[]
*/
public function getContactSegmentations(): Collection
{
return $this->contactSegmentations;
}
public function addContactSegmentation(ContactSegmentation $contactSegmentation): self
{
if (!$this->contactSegmentations->contains($contactSegmentation)) {
$this->contactSegmentations[] = $contactSegmentation;
}
return $this;
}
public function removeContactSegmentation(ContactSegmentation $contactSegmentation): self
{
if ($this->contactSegmentations->contains($contactSegmentation)) {
$this->contactSegmentations->removeElement($contactSegmentation);
}
return $this;
}
public function getSalesmanagoTags(bool $funneled = false): array
{
return array_values(
$this->contactSegmentations
->map(function (ContactSegmentation $seg) use ($funneled) {
$tag = $seg->getSalesmanagoTag();
if ($funneled && $tag && strncmp($tag, 'SEG1_', 5) === 0) {
return substr($tag, 5);
}
return $tag;
})
->filter(function (?string $tag) {
return $tag !== null && $tag !== '';
})
->toArray()
);
}
public function getSalesManagoSourceTag(): ?string
{
return null;
}
public function findProductVariantByPositionType(string $positionType): ?ProductVariant
{
foreach ($this->getProductVariant() as $productVariant) {
$productVariantPositionType = ProductVariantNumbersSpec::extract(
$productVariant->getProductVariantNoComplete(), 'positionType'
);
if ($productVariantPositionType === $positionType) {
return $productVariant;
}
}
return null;
}
}