<?php
namespace App\Entity\Gos;
use App\Entity\Gos\Uniqskills\Course;
use App\Enum\DocumentDistributionChannels;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* Cart
*
* @ORM\Entity(repositoryClass="App\Repository\ProductCartRepository")
* @ORM\HasLifecycleCallbacks
*/
class ProductCart
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
*
* @ORM\Column(name="quantity", type="integer")
*/
private $quantity;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Gos\Cart", inversedBy="productCart", cascade={"persist"})
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private $cart;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Gos\ProductVariant", inversedBy="productCart")
* @ORM\JoinColumn()
*/
private $productVariant;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Gos\ProductPack", inversedBy="productCart")
* @ORM\JoinColumn()
*/
private $productPack;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Gos\PaymentMethod", inversedBy="productCart")
* @ORM\JoinColumn()
*/
private $paymentMethod;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Gos\CartParticipant", mappedBy="productCart")
*/
private $cartParticipant;
/**
* @ORM\OneToOne(targetEntity="Gift", mappedBy="productCart")
*/
private $gift;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isGift;
/**
* @ORM\ManyToOne(targetEntity="Coupon", inversedBy="productCart", cascade={"persist"})
*/
private $coupon;
/**
* @ORM\ManyToMany(targetEntity="Coupon", mappedBy="productCarts", cascade={"persist"})
*/
private $coupons;
/**
* @ORM\ManyToOne(targetEntity="ProductAssociation", inversedBy="productCart")
* @ORM\JoinColumn()
*/
private $productAssociation;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Gos\ProductPack")
*/
private $cycleProductPack;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $lifetimeLanding;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $upsellingType;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $omitShippingCosts;
/**
* @ORM\ManyToOne(targetEntity=Course::class)
*/
private $course;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $selectedDiscount;
/**
* @ORM\ManyToOne(targetEntity=ProductCart::class, inversedBy="children")
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity="ProductCart", mappedBy="parent", cascade={"persist", "remove"})
*/
private $children;
/**
* @ORM\ManyToMany(targetEntity=Coupon::class, cascade={"persist"})
* @ORM\JoinTable(name="product_cart_not_combined_coupons")
*/
private $notCombinedCoupons;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $productDuplicationMessageDisplayed;
public function __clone()
{
if ($this->id)
{
$this->setId(null);
}
}
public function isActive(): bool
{
if ((!empty($this->getProductVariant()) && !$this->getProductVariant()->isActive())
|| (!empty($this->getProductPack() && !$this->getProductPack()->isActive())))
{
return false;
}
if (!empty($this->getProductPack()))
{
foreach ($this->getProductPack()->getProductPackItem() as $productPackItem)
{
/** @var ProductPackItem $productPackItem */
if (!$productPackItem->getProductVariant()->isActive())
{
return false;
}
}
}
return true;
}
public function countCartParticipant(): int
{
return $this->getCartParticipant()->count();
}
public function getCartParticipantByOrder($order)
{
if (is_int($order))
{
$cartParticipant = $this->getCartParticipant()->toArray();
asort($cartParticipant);
--$order;
if (isset($cartParticipant[$order]))
{
return $cartParticipant[$order];
}
}
return null;
}
public function hasEventProduct(): bool
{
if (
!empty($this->getProductVariant())
&& $this->getProductVariant()->isEvent()
)
{
return true;
}
if (!empty($this->getProductPack()))
{
foreach ($this->getProductPack()->getProductPackItem() as $productPackItem)
{
/** @var ProductPackItem $productPackItem */
if ($productPackItem->getProductVariant()->isEvent())
{
return true;
}
}
}
return false;
}
public function hasEventProductTaxFree(): bool
{
if (
!empty($this->getProductVariant())
&& $this->getProductVariant()->isEvent() && $this->getProductVariant()->getTaxFreeForBudgetUnit()
)
{
return true;
}
if (!empty($this->getProductPack()))
{
foreach ($this->getProductPack()->getProductPackItem() as $productPackItem)
{
/** @var ProductPackItem $productPackItem */
if ($productPackItem->getProductVariant()->isEvent() && $productPackItem->getProductVariant()->getTaxFreeForBudgetUnit())
{
return true;
}
}
}
return false;
}
public function hasOnlyEventProduct(): bool
{
if (
!empty($this->getProductVariant())
&& !$this->getProductVariant()->isEvent()
)
{
return false;
}
if (!empty($this->getProductPack()))
{
foreach ($this->getProductPack()->getProductPackItem() as $productPackItem)
{
/** @var ProductPackItem $productPackItem */
if (!$productPackItem->getProductVariant()->isEvent())
{
return false;
}
}
}
if (!empty($this->getCartParticipant()))
{
foreach ($this->getCartParticipant() as $cartParticipant)
{
if (!empty($cartParticipant->getProductVariantGroupItems()))
{
foreach ($cartParticipant->getProductVariantGroupItems() as $productVariantGroupItem)
{
if (!$productVariantGroupItem->getProductVariant()->isEvent())
{
return false;
}
}
}
}
}
return true;
}
public function hasProductPack(): bool
{
return !empty($this->getProductPack());
}
public function hasUniqskillsMultiUserCourse(): bool
{
return $this->getProductVariant() !== null
&& $this->getProductVariant()->getIsUniqskillsProduct()
&& $this->getProductVariant()->getMaxUsers() > 1
&& $this->getProductVariant()->getBuyMaxOne() === false
&& $this->getQuantity() > 1;
}
public function __construct()
{
$this->cartParticipant = new ArrayCollection();
$this->coupons = new ArrayCollection();
$this->children = new ArrayCollection();
$this->notCombinedCoupons = new ArrayCollection();
}
public function __toString()
{
return (string)$this->id;
}
/** @ORM\PrePersist() */
public function prePersist()
{
$this->createdAt = new \DateTime();
}
/** @ORM\PreUpdate() */
public function preUpdate()
{
$this->updatedAt = new \DateTime();
}
//------------------------------ setters & getters
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set id
*
* @return ProductCart
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Get quantity
*
* @return integer
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
*
* @return ProductCart
*/
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 ProductCart
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set cart
*
* @param \App\Entity\Gos\Cart $cart
*
* @return ProductCart
*/
public function setCart(\App\Entity\Gos\Cart $cart = null)
{
$this->cart = $cart;
return $this;
}
/**
* Get cart
*
* @return \App\Entity\Gos\Cart
*/
public function getCart()
{
return $this->cart;
}
/**
* Set paymentMethod
*
* @param \App\Entity\Gos\PaymentMethod $paymentMethod
*
* @return ProductCart
*/
public function setPaymentMethod(\App\Entity\Gos\PaymentMethod $paymentMethod = null)
{
$this->paymentMethod = $paymentMethod;
return $this;
}
/**
* Get paymentMethod
*
* @return \App\Entity\Gos\PaymentMethod
*/
public function getPaymentMethod()
{
return $this->paymentMethod;
}
public function getAvailablePaymentMethods()
{
$paymentMethods = [];
if ($this->getProductVariant())
{
$additionalOptionsByClientType = $this->getProductVariant()
->getAdditionalOptionsByClientType($this->getCart()->getClientType());
if ($additionalOptionsByClientType)
{
foreach ($additionalOptionsByClientType->getPaymentMethod() as $paymentMethod)
{
$paymentMethods[] = $paymentMethod;
}
}
}
elseif ($this->getProductPack())
{
/** @var ProductPackItem $productPackItem */
foreach ($this->getProductPack()->getProductPackItem() as $productPackItem)
{
$additionalOptionsByClientType = $productPackItem->getProductVariant()
->getAdditionalOptionsByClientType($this->getCart()->getClientType());
if ($additionalOptionsByClientType)
{
foreach ($additionalOptionsByClientType->getPaymentMethod() as $paymentMethod)
{
$paymentMethods[] = $paymentMethod;
}
}
}
}
return $paymentMethods;
}
public function getAvailableClientTypes()
{
$clientTypes = [];
$clientTypes['quantity'] = 0;
$clientTypes['types'] = [];
if ($this->getProductVariant())
{
$clientTypes['quantity'] = 1;
$this->getClientTypes($this->productVariant, $clientTypes);
}
elseif ($this->getProductPack())
{
/** @var ProductPackItem $productPackItem */
foreach ($this->getProductPack()->getProductPackItem() as $productPackItem)
{
if ($productPackItem->getProductVariant() !== null)
{
$this->getClientTypes($productPackItem->getProductVariant(), $clientTypes);
$clientTypes['quantity'] += 1;
}
}
}
return $clientTypes;
}
private function getClientTypes(ProductVariant $productVariant, &$clientTypes): void
{
foreach ($productVariant->getAdditionalOptionsByClientTypes() as $additionalOptionsByClientType)
{
if ($additionalOptionsByClientType->getIsActive() === true)
{
$clientTypes['types'][] = $additionalOptionsByClientType->getClientType();
}
}
}
public function setQuantity(int $quantity): self
{
$this->quantity = $quantity;
return $this;
}
public function getProductVariant(): ?ProductVariant
{
return $this->productVariant;
}
public function setProductVariant(?ProductVariant $productVariant): self
{
$this->productVariant = $productVariant;
return $this;
}
public function getRequiredPWZ(): ?bool
{
if ($this->getProductVariant() !== null)
{
return $this->getProductVariant()->getRequiredPWZ();
}
return false;
}
public function isNPWZRequired(): ?bool
{
if (!empty($this->productVariant)) {
return $this->getProductVariant()->getIsNPWZRequired() && $this->getProductVariant()->getRequiredPWZ();
}
if (!empty($this->productPack)) {
return $this->getProductPack()->isNPWZRequired();
}
return false;
}
public function getEmailInvoiceEnabled(): ?bool
{
if (
$this->getProductVariant() !== null
&&
$this->getProductVariant()->getDocumentDistributionChannel()
&&
$this->getProductVariant()->getDocumentDistributionChannel() != DocumentDistributionChannels::PAPER
)
{
return true;
}
if ($productPack = $this->getProductPack())
{
$productPackInvoiceE = true;
foreach ($productPack->getProductPackItem() as $productPackItem)
{
if ($productPackItem->getProductVariant()->getDocumentDistributionChannel() != DocumentDistributionChannels::EMAIL)
{
$productPackInvoiceE = false;
}
}
if ($productPackInvoiceE == true) return true;
}
return false;
}
public function getProductPack(): ?ProductPack
{
return $this->productPack;
}
public function setProductPack(?ProductPack $productPack): self
{
$this->productPack = $productPack;
return $this;
}
/**
* @return Collection|CartParticipant[]
*/
public function getCartParticipant(): Collection
{
return $this->cartParticipant;
}
public function addCartParticipant(CartParticipant $cartParticipant): self
{
if (!$this->cartParticipant->contains($cartParticipant)) {
$this->cartParticipant[] = $cartParticipant;
$cartParticipant->setProductCart($this);
}
return $this;
}
public function removeCartParticipant(CartParticipant $cartParticipant): self
{
if ($this->cartParticipant->contains($cartParticipant)) {
$this->cartParticipant->removeElement($cartParticipant);
// set the owning side to null (unless already changed)
if ($cartParticipant->getProductCart() === $this) {
$cartParticipant->setProductCart(null);
}
}
return $this;
}
public function removeAllCartParticipants(): self
{
$this->cartParticipant = new ArrayCollection();
return $this;
}
public function getGift(): ?Gift
{
return $this->gift;
}
public function setGift(?Gift $gift): self
{
$this->gift = $gift;
// set (or unset) the owning side of the relation if necessary
$newProductCart = $gift === null ? null : $this;
if ($newProductCart !== $gift->getProductCart()) {
$gift->setProductCart($newProductCart);
}
return $this;
}
public function getIsGift(): ?bool
{
return $this->isGift;
}
public function setIsGift(?bool $isGift): self
{
$this->isGift = $isGift;
return $this;
}
public function getName()
{
if ($this->getProductVariant() === null && $this->getProductPack() === null)
{
return '';
}
if ($this->getProductPack())
{
return $this->getProductPack()->getName();
}
if ($this->getCycleProductPack())
{
return $this->getCycleProductPack()->getLabel();
}
return $this->getProductVariant()->getAltTradeName() ?: $this->getProductVariant()->getTradeName();
}
public function getLabel()
{
if (!empty($this->productVariant))
{
return $this->getProductVariant()->getLabel();
}
elseif (!empty($this->productPack))
{
return $this->getProductPack()->getLabel();
}
return '';
}
public function getBuyMaxOne()
{
if (!empty($this->productVariant))
{
return $this->getProductVariant()->getBuyMaxOne();
}
elseif (!empty($this->productPack))
{
return $this->getProductPack()->getBuyMaxOne();
}
return false;
}
public function getIsEvent()
{
if (!empty($this->productVariant))
{
return $this->getProductVariant()->isEvent();
}
return false;
}
/**
* @deprecated Please use function getCoupons
*/
public function getCoupon(): ?Coupon
{
if ($this->coupons->isEmpty())
{
return $this->coupon;
}
return $this->coupons->first();
}
public function getCoupons(): Collection
{
return $this->coupons;
}
/**
* @deprecated Please use function addCoupon
*/
public function setCoupon(?Coupon $coupon): self
{
if ($this->coupon !== null && $this->coupons->contains($this->coupon))
{
$this->coupons->removeElement($this->coupon);
$this->coupon->removeProductCarts($this);
}
if ($coupon !== null && !$this->coupons->contains($coupon))
{
$this->coupons->add($coupon);
$coupon->addProductCarts($this);
}
$this->coupon = $coupon;
return $this;
}
public function setCoupons(Collection $coupons): self
{
if ($coupons->isEmpty())
{
$this->coupon = null;
}
/** @var Coupon $coupon */
foreach ($this->coupons as $coupon) {
$coupon->removeProductCarts($this);
}
$this->coupons = $coupons;
/** @var Coupon $coupon */
foreach ($coupons as $coupon) {
$coupon->addProductCarts($this);
}
return $this;
}
public function addCoupon(Coupon $coupon): self
{
if (!$this->coupons->contains($coupon))
{
$this->coupons->add($coupon);
$coupon->addProductCarts($this);
}
return $this;
}
public function removeCoupon(Coupon $coupon): self
{
if ($this->coupons->contains($coupon))
{
$this->coupons->removeElement($coupon);
$coupon->removeProductCarts($this);
}
if ($this->coupon === $coupon)
{
$this->coupon = null;
}
return $this;
}
public function getProductAssociation(): ?ProductAssociation
{
return $this->productAssociation;
}
public function setProductAssociation(?ProductAssociation $productAssociation): self
{
$this->productAssociation = $productAssociation;
return $this;
}
public function getCycleProductPack(): ?ProductPack
{
return $this->cycleProductPack;
}
public function setCycleProductPack(?ProductPack $cycleProductPack): self
{
$this->cycleProductPack = $cycleProductPack;
return $this;
}
public function findCyclePosition()
{
return $this->cycleProductPack->findCyclePosition($this->productVariant);
}
public function getLifetimeLanding(): ?bool
{
return $this->lifetimeLanding;
}
public function setLifetimeLanding(?bool $lifetimeLanding): self
{
$this->lifetimeLanding = $lifetimeLanding;
return $this;
}
public function askForPWZ(): bool
{
if (!empty($this->productVariant)) {
return (bool)$this->getProductVariant()->getRequiredPWZ();
}
if (!empty($this->productPack)) {
return $this->getProductPack()->askForPWZ();
}
return false;
}
public function getUpsellingType(): ?int
{
return $this->upsellingType;
}
public function setUpsellingType(?int $upsellingType): self
{
$this->upsellingType = $upsellingType;
return $this;
}
public function getOmitShippingCosts(): ?bool
{
return $this->omitShippingCosts;
}
public function setOmitShippingCosts(?bool $omitShippingCosts): self
{
$this->omitShippingCosts = $omitShippingCosts;
return $this;
}
public function getCourse(): ?Course
{
return $this->course;
}
public function setCourse(?Course $course): self
{
$this->course = $course;
return $this;
}
public function getSelectedDiscount(): ?int
{
return $this->selectedDiscount;
}
public function setSelectedDiscount(?int $selectedDiscount): self
{
$this->selectedDiscount = $selectedDiscount;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
public function getChildren(): Collection
{
return $this->children;
}
public function addChildren(ProductCart $productCart): self
{
if (!$this->children->contains($productCart)) {
$this->children[] = $productCart;
$productCart->setParent($this);
}
return $this;
}
public function removeChildren(ProductCart $productCart): self
{
if ($this->children->contains($productCart)) {
$this->children->removeElement($productCart);
// set the owning side to null (unless already changed)
if ($productCart->getChildren() === $this) {
$productCart->setParent(null);
}
}
return $this;
}
/**
* @return Collection|Coupon[]
*/
public function getNotCombinedCoupons(): Collection
{
return $this->notCombinedCoupons;
}
public function addNotCombinedCoupon(Coupon $notCombinedCoupon): self
{
if (!$this->notCombinedCoupons->contains($notCombinedCoupon)) {
$this->notCombinedCoupons[] = $notCombinedCoupon;
}
return $this;
}
public function removeNotCombinedCoupon(Coupon $notCombinedCoupon): self
{
if ($this->notCombinedCoupons->contains($notCombinedCoupon)) {
$this->notCombinedCoupons->removeElement($notCombinedCoupon);
}
return $this;
}
public function getProductDuplicationMessageDisplayed(): ?bool
{
return $this->productDuplicationMessageDisplayed;
}
public function setProductDuplicationMessageDisplayed(?bool $productDuplicationMessageDisplayed): self
{
$this->productDuplicationMessageDisplayed = $productDuplicationMessageDisplayed;
return $this;
}
}