<?php
namespace App\Entity\Gos;
use App\Entity\Gos\Uniqskills\OrderCycle;
use App\Entity\Gos\ProductPackMultiDiscount;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\Gos\ProductPackRepository")
* @ORM\HasLifecycleCallbacks
*/
class ProductPack
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(type="text", nullable=true)
*/
private $label;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isActive;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $showPrice;
/**
* @var bool
*
* @ORM\Column(type="boolean", nullable=true)
*/
private $buyMaxOne;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Gos\ProductPackItem", mappedBy="productPack", cascade={"persist"})
*/
private $productPackItem;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Gos\ProductCart", mappedBy="productPack")
*/
private $productCart;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $grossShow;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Gos\ProductPackType")
*/
private $type;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Gos\Uniqskills\OrderCycle", mappedBy="cyclePack")
*/
private $orderCycles;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isPubliclyAvailable;
/**
* @ORM\Column(type="date", nullable=true)
*/
private $publiclyAvailableDateFrom;
/**
* @ORM\Column(type="date", nullable=true)
*/
private $publiclyAvailableDateTo;
/**
* @ORM\OneToMany(targetEntity=ProductPackMultiDiscount::class, mappedBy="productPack", orphanRemoval=true, cascade={"persist"})
*/
private $multiDiscounts;
/**
* ProductPack constructor.
*/
public function __construct()
{
$this->productPackItem = new ArrayCollection();
$this->productCart = new ArrayCollection();
$this->orderCycles = new ArrayCollection();
$this->multiDiscounts = new ArrayCollection();
}
/** @ORM\PrePersist() */
public function prePersist()
{
$this->createdAt = new \DateTime();
}
/** @ORM\PreUpdate() */
public function preUpdate()
{
$this->updatedAt = new \DateTime();
}
public function __toString()
{
return $this->name;
}
public function isActive()
{
if (!$this->getIsActive())
{
return false;
}
if (!$this->getProductPackItem()->isEmpty())
{
foreach ($this->getProductPackItem() as $productPackItem)
{
/** @var ProductPackItem $productPackItem */
if (!$productPackItem->getProductVariant()->isActive())
{
return false;
}
}
}
return true;
}
/**
* Return productPackItem when has event product
* @return array
*/
public function getEventProductPackItem()
{
$productsPackItem = array();
if (!$this->getProductPackItem()->isEmpty())
{
foreach ($this->getProductPackItem() as $productPackItem)
{
/** @var ProductPackItem $productPackItem */
if ($productPackItem->getProductVariant()->isEvent())
{
$productsPackItem[] = $productPackItem;
}
}
}
return $productsPackItem;
}
public function getFirstNonVirtualItem(): ?ProductPackItem
{
foreach ($this->getProductPackItem() as $productPackItem)
{
if (!$productPackItem->getProductVariant()->getIsVirtual())
{
return $productPackItem;
}
}
return null;
}
public function hasPhysicalVariant(): bool
{
foreach ($this->getProductPackItem() as $productPackItem)
{
if ($productPackItem->getProductVariant()->isPhysical())
{
return true;
}
}
return false;
}
public function askForPWZ(): bool
{
if (!$this->getProductPackItem()->isEmpty())
{
foreach ($this->getProductPackItem() as $productPackItem)
{
/** @var ProductPackItem $productPackItem */
if ($productPackItem->getProductVariant()->getRequiredPWZ())
{
return true;
}
}
}
return false;
}
public function isNPWZRequired(): bool
{
if (!$this->getProductPackItem()->isEmpty())
{
foreach ($this->getProductPackItem() as $productPackItem)
{
/** @var ProductPackItem $productPackItem */
if ($productPackItem->getProductVariant()->getIsNPWZRequired() && $productPackItem->getProductVariant()->getRequiredPWZ())
{
return true;
}
}
}
return false;
}
public function getCourseProductPackItem(): array
{
$coursePackItems = array();
foreach ($this->getProductPackItem() as $productPackItem)
{
if ($productPackItem->getProductVariant() && $productPackItem->getProductVariant()->getCourses())
{
$coursePackItems[] = $productPackItem;
}
}
return $coursePackItems;
}
public function getTotalDiscountNet()
{
$totalDiscountNet = 0;
foreach ($this->getProductPackItem() as $productPackItem)
{
$totalDiscountNet += $productPackItem->getDiscountNet();
}
return $totalDiscountNet;
}
public function getTotalDiscountGross()
{
$totalDiscountGross = 0;
foreach ($this->getProductPackItem() as $productPackItem)
{
$totalDiscountGross += $productPackItem->getDiscountGross();
}
return $totalDiscountGross;
}
public function getTotalPriceBeforeDiscountNet()
{
$totalPriceNet = 0;
foreach ($this->getProductPackItem() as $productPackItem)
{
$totalPriceNet += $productPackItem->getQuantity() * $productPackItem->getProductVariant()->getFullPrice('net');
}
return $totalPriceNet;
}
public function getTotalPriceBeforeDiscountGross()
{
$totalPriceGross = 0;
foreach ($this->getProductPackItem() as $productPackItem)
{
$totalPriceGross += $productPackItem->getQuantity() * $productPackItem->getProductVariant()->getFullPrice('gross');
}
return $totalPriceGross;
}
public function getTotalPriceNet()
{
$totalPriceNet = 0;
foreach ($this->getProductPackItem() as $productPackItem)
{
$totalPriceNet += $productPackItem->getQuantity() * $productPackItem->getPriceNet();
}
return $totalPriceNet;
}
public function getTotalPriceGross()
{
$totalPriceGross = 0;
foreach ($this->getProductPackItem() as $productPackItem)
{
$totalPriceGross += $productPackItem->getQuantity() * $productPackItem->getPriceGross();
}
return $totalPriceGross;
}
//------------------------------ setters & getters
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 getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return Collection|ProductPackItem[]
*/
public function getProductPackItem(): Collection
{
return $this->productPackItem;
}
public function addProductPackItem(ProductPackItem $productPackItem): self
{
if (!$this->productPackItem->contains($productPackItem)) {
$this->productPackItem[] = $productPackItem;
$productPackItem->setProductPack($this);
}
return $this;
}
public function removeProductPackItem(ProductPackItem $productPackItem): self
{
if ($this->productPackItem->contains($productPackItem)) {
$this->productPackItem->removeElement($productPackItem);
// set the owning side to null (unless already changed)
if ($productPackItem->getProductPack() === $this) {
$productPackItem->setProductPack(null);
}
}
return $this;
}
/**
* @return Collection|ProductCart[]
*/
public function getProductCart(): Collection
{
return $this->productCart;
}
public function addProductCart(ProductCart $productCart): self
{
if (!$this->productCart->contains($productCart)) {
$this->productCart[] = $productCart;
$productCart->setProductPack($this);
}
return $this;
}
public function removeProductCart(ProductCart $productCart): self
{
if ($this->productCart->contains($productCart)) {
$this->productCart->removeElement($productCart);
// set the owning side to null (unless already changed)
if ($productCart->getProductPack() === $this) {
$productCart->setProductPack(null);
}
}
return $this;
}
public function getBuyMaxOne(): ?bool
{
return $this->buyMaxOne;
}
public function setBuyMaxOne(?bool $buyMaxOne): self
{
$this->buyMaxOne = $buyMaxOne;
return $this;
}
public function getIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(?bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function hasEventProduct()
{
if (!$this->getProductPackItem()->isEmpty())
{
foreach ($this->getProductPackItem() as $productPackItem)
{
/** @var ProductPack $productPackItem */
if ($productPackItem->getProductVariant()->isEvent())
{
return true;
}
}
}
return false;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(?string $label): self
{
$this->label = $label;
return $this;
}
public function getShowPrice(): ?bool
{
return $this->showPrice;
}
public function setShowPrice(?bool $showPrice): self
{
$this->showPrice = $showPrice;
return $this;
}
/**
* @param int|null $grossShow
* @return ProductPack
*/
public function setGrossShow(?int $grossShow): self
{
$this->grossShow = $grossShow;
return $this;
}
/**
* @return int|null
*/
public function getGrossShow(): ?int
{
return $this->grossShow;
}
public function getType(): ?ProductPackType
{
return $this->type;
}
public function setType(?ProductPackType $type): self
{
$this->type = $type;
return $this;
}
/**
* @return Collection|OrderCycle[]
*/
public function getOrderCycles(): Collection
{
return $this->orderCycles;
}
public function addOrderCycle(OrderCycle $orderCycle): self
{
if (!$this->orderCycles->contains($orderCycle)) {
$this->orderCycles[] = $orderCycle;
$orderCycle->setCyclePack($this);
}
return $this;
}
public function removeOrderCycle(OrderCycle $orderCycle): self
{
if ($this->orderCycles->contains($orderCycle)) {
$this->orderCycles->removeElement($orderCycle);
// set the owning side to null (unless already changed)
if ($orderCycle->getCyclePack() === $this) {
$orderCycle->setCyclePack(null);
}
}
return $this;
}
public function findCyclePosition(ProductVariant $productVariant)
{
$position = 0;
$course = $productVariant->getCourses()->first();
foreach ($this->getProductPackItem() as $item)
{
$position++;
if ($item->getProductVariant()->getCourses()->contains($course))
return $position;
}
}
public function getIsPubliclyAvailable(): ?bool
{
return $this->isPubliclyAvailable;
}
public function setIsPubliclyAvailable(?bool $isPubliclyAvailable): self
{
$this->isPubliclyAvailable = $isPubliclyAvailable;
return $this;
}
public function getPubliclyAvailableDateFrom(): ?\DateTimeInterface
{
return $this->publiclyAvailableDateFrom;
}
public function setPubliclyAvailableDateFrom(?\DateTimeInterface $publiclyAvailableDateFrom): self
{
$this->publiclyAvailableDateFrom = $publiclyAvailableDateFrom;
return $this;
}
public function getPubliclyAvailableDateTo(): ?\DateTimeInterface
{
return $this->publiclyAvailableDateTo;
}
public function setPubliclyAvailableDateTo(?\DateTimeInterface $publiclyAvailableDateTo): self
{
$this->publiclyAvailableDateTo = $publiclyAvailableDateTo;
return $this;
}
/**
* @return Collection|ProductPackMultiDiscount[]
*/
public function getMultiDiscounts(): Collection
{
return $this->multiDiscounts;
}
public function addMultiDiscount(ProductPackMultiDiscount $multiDiscount): self
{
if (!$this->multiDiscounts->contains($multiDiscount)) {
$this->multiDiscounts[] = $multiDiscount;
$multiDiscount->setProductPack($this);
}
return $this;
}
public function removeMultiDiscount(ProductPackMultiDiscount $multiDiscount): self
{
if ($this->multiDiscounts->contains($multiDiscount)) {
$this->multiDiscounts->removeElement($multiDiscount);
// set the owning side to null (unless already changed)
if ($multiDiscount->getProductPack() === $this) {
$multiDiscount->setProductPack(null);
}
}
return $this;
}
public function getPortals(): array
{
$portals = [];
/** @var ProductPackItem $productPackItem */
foreach ($this->getProductPackItem() as $productPackItem) {
foreach ($productPackItem->getProductVariant()->getPortalSettings() as $portalSetting) {
$portals[$portalSetting->getDomain()] = $portalSetting->getHash();
}
}
return $portals;
}
}