src/Entity/Gos/Cart.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Gos;
  3. use App\Entity\Gos\Uniqskills\Course;
  4. use App\Enum\Product\ProductSourceSystem;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. /**
  9.  * Cart
  10.  *
  11.  * @ORM\Table(name="cart")
  12.  * @ORM\Entity(repositoryClass="App\Repository\CartRepository")
  13.  * @ORM\HasLifecycleCallbacks
  14.  */
  15. class Cart
  16. {
  17.     /**
  18.      * @var int
  19.      *
  20.      * @ORM\Column(name="id", type="integer")
  21.      * @ORM\Id
  22.      * @ORM\GeneratedValue(strategy="AUTO")
  23.      */
  24.     private $id;
  25.     /**
  26.      * @var string
  27.      *
  28.      * @ORM\Column(name="hash", type="string", length=255, unique=true)
  29.      */
  30.     private $hash;
  31.     /**
  32.      * @var bool
  33.      *
  34.      * @ORM\Column(type="boolean", nullable=true)
  35.      */
  36.     private $model;
  37.     /**
  38.      * @ORM\OneToMany(targetEntity="App\Entity\Gos\CouponPending", mappedBy="cart", cascade={"persist"})
  39.      */
  40.     private $couponPending;
  41.     /**
  42.      * @ORM\ManyToOne(targetEntity="App\Entity\Gos\Coupon", inversedBy="cart", cascade={"persist"})
  43.      * @ORM\JoinColumn()
  44.      *
  45.      * TODO: DEV-171 Zostawilem to pole dla pewnosci zeby nic sie nie wysypalo
  46.      * Jesli wszystko zacznie uzywac multi kuponow bedzie mozna usunac to pole
  47.      */
  48.     private $coupon;
  49.     /**
  50.      * @ORM\ManyToMany(targetEntity="App\Entity\Gos\Coupon", inversedBy="carts", cascade={"persist"})
  51.      * @ORM\JoinTable(name="carts_coupons")
  52.      */
  53.     private $coupons;
  54.     /**
  55.      * @ORM\OneToMany(targetEntity="App\Entity\Gos\ProductCart", mappedBy="cart", cascade={"persist"})
  56.      */
  57.     private $productCart;
  58.     /**
  59.      * @ORM\ManyToOne(targetEntity="App\Entity\Gos\Address", inversedBy="cart", cascade={"persist"})
  60.      * @ORM\JoinColumn()
  61.      */
  62.     private $address;
  63.     /**
  64.      * @ORM\OneToOne(targetEntity="App\Entity\Gos\User", inversedBy="cart")
  65.      * @ORM\JoinColumn(onDelete="SET NULL")
  66.      */
  67.     private $user;
  68.     /**
  69.      * @ORM\OneToOne(targetEntity="App\Entity\Gos\UserTemporary", inversedBy="cart", cascade={"persist"})
  70.      * @ORM\JoinColumn(onDelete="SET NULL")
  71.      */
  72.     private $userTemporary;
  73.     /**
  74.      * @ORM\Column(type="boolean", nullable=true)
  75.      */
  76.     private $hasMultiDiscount;
  77.     /**
  78.      * @ORM\Column(type="integer", nullable=true)
  79.      */
  80.     private $selectedDiscount;
  81.     /**
  82.      * @ORM\ManyToOne(targetEntity="Country", inversedBy="carts")
  83.      * @ORM\JoinColumn()
  84.      */
  85.     private $country;
  86.     /**
  87.      * @ORM\Column(type="integer", options={"default": 0})
  88.      */
  89.     private $extendedPaymentTime 0;
  90.     /**
  91.      * @ORM\ManyToOne(targetEntity="PaymentMethod", inversedBy="cart")
  92.      * @ORM\JoinColumn()
  93.      */
  94.     private $paymentMethod;
  95.     /**
  96.      * @ORM\ManyToOne(targetEntity="PaymentSystem", inversedBy="cart")
  97.      * @ORM\JoinColumn()
  98.      */
  99.     private $paymentSystem;
  100.     /**
  101.      * @ORM\Column(type="datetime")
  102.      */
  103.     private $createdAt;
  104.     /**
  105.      * @ORM\Column(type="datetime", nullable=true)
  106.      */
  107.     private $updatedAt;
  108.     /**
  109.      * @ORM\ManyToOne(targetEntity=PortalSettings::class, inversedBy="carts")
  110.      */
  111.     private $portalSettings;
  112.     /**
  113.      * @ORM\ManyToOne(targetEntity=ShippingType::class)
  114.      */
  115.     private $shippingType;
  116.     /**
  117.      * @ORM\Column(type="boolean", options={"default": false})
  118.      */
  119.     private $sentToSm false;
  120.     /**
  121.      * @ORM\Column(type="string", length=255, nullable=true)
  122.      */
  123.     private $burAssignedSupportId;
  124.     /**
  125.      * @ORM\Column(type="string", length=255, nullable=true)
  126.      */
  127.     private $salesManago2EventId;
  128.     public function isActive()
  129.     {
  130.         foreach ($this->getProductCart() as $productCart)
  131.         {
  132.             /** @var ProductCart $productCart */
  133.             if (!$productCart->isActive())
  134.             {
  135.                 return false;
  136.             }
  137.         }
  138.         return true;
  139.     }
  140.     public function __clone()
  141.     {
  142.         if ($this->id)
  143.         {
  144.             $this->setId(null);
  145.         }
  146.     }
  147.     /** @ORM\PrePersist() */
  148.     public function prePersist()
  149.     {
  150.         $this->createdAt = new \DateTime();
  151.     }
  152.     /** @ORM\PreUpdate() */
  153.     public function preUpdate()
  154.     {
  155.         $this->updatedAt = new \DateTime();
  156.     }
  157.     public function __toString()
  158.     {
  159.         return (string)$this->hash;
  160.     }
  161.     public function getCourse(): ?Course
  162.     {
  163.         /** @var ProductCart $productCart */
  164.         foreach ($this->getProductCart() as $productCart)
  165.         {
  166.             if ($productCart->getCourse())
  167.             {
  168.                 return $productCart->getCourse();
  169.             }
  170.         }
  171.         return null;
  172.     }
  173.     public function showButtonAdditionalAddress(): bool
  174.     {
  175.         if (!$this->getProductCart()->isEmpty())
  176.         {
  177.             /** @var ProductCart $productCart */
  178.             foreach ($this->getProductCart() as $productCart)
  179.             {
  180.                 if ($productCart->getProductPack())
  181.                 {
  182.                     foreach ($productCart->getProductPack()->getProductPackItem() as $item)
  183.                     {
  184.                         if ($item->getProductVariant()->getShowButtonAdditionalAddress() === true) return true;
  185.                     }
  186.                 }
  187.                 else if ($productCart->getProductVariant() && $productCart->getProductVariant()->getShowButtonAdditionalAddress() === true)
  188.                 {
  189.                     return true;
  190.                 }
  191.             }
  192.         }
  193.         return false;
  194.     }
  195.     public function hasProductVariant(ProductVariant $productVariant): bool
  196.     {
  197.         /** @var ProductCart $productCart */
  198.         foreach ($this->productCart as $productCart)
  199.         {
  200.             if ($productCart->getProductVariant() === $productVariant)
  201.             {
  202.                 return true;
  203.             }
  204.         }
  205.         return false;
  206.     }
  207.     public function hasOnlyDigitalProducts(): bool
  208.     {
  209.         if (!$this->getProductCart()->isEmpty())
  210.         {
  211.             /** @var ProductCart $productCart */
  212.             foreach ($this->getProductCart() as $productCart)
  213.             {
  214.                 if ($productCart->getProductPack())
  215.                 {
  216.                     foreach ($productCart->getProductPack()->getProductPackItem() as $item)
  217.                     {
  218.                         if (!$item->getProductVariant()->getIsDigital())
  219.                         {
  220.                             return false;
  221.                         }
  222.                     }
  223.                 }
  224.                 else if ($productCart->getProductVariant() && !$productCart->getProductVariant()->getIsDigital())
  225.                 {
  226.                     return false;
  227.                 }
  228.             }
  229.         }
  230.         return true;
  231.     }
  232.     public function hasEventProduct(): bool
  233.     {
  234.         if (!$this->getProductCart()->isEmpty())
  235.         {
  236.             foreach ($this->getProductCart() as $productCart)
  237.             {
  238.                 /** @var ProductCart $productCart */
  239.                 if ($productCart->hasEventProduct())
  240.                 {
  241.                     return true;
  242.                 }
  243.             }
  244.         }
  245.         return false;
  246.     }
  247.     public function hasEventProductTaxFree(): bool
  248.     {
  249.         if (!$this->getProductCart()->isEmpty())
  250.         {
  251.             foreach ($this->getProductCart() as $productCart)
  252.             {
  253.                 /** @var ProductCart $productCart */
  254.                 if ($productCart->hasEventProductTaxFree())
  255.                 {
  256.                     return true;
  257.                 }
  258.             }
  259.         }
  260.         return false;
  261.     }
  262.     public function hasOnlyEventProduct(): bool
  263.     {
  264.         if (!$this->getProductCart()->isEmpty())
  265.         {
  266.             foreach ($this->getProductCart() as $productCart)
  267.             {
  268.                 /** @var ProductCart $productCart */
  269.                 if (!$productCart->hasOnlyEventProduct())
  270.                 {
  271.                     return false;
  272.                 }
  273.             }
  274.         }
  275.         return true;
  276.     }
  277.     /**
  278.      * Return prodcutCart when has event product
  279.      * @return array
  280.      */
  281.     public function getEventProductCart(): array
  282.     {
  283.         $productsCart = array();
  284.         if (!$this->getProductCart()->isEmpty())
  285.         {
  286.             foreach ($this->getProductCart() as $productCart)
  287.             {
  288.                 /** @var ProductCart $productCart */
  289.                 if ($productCart->hasEventProduct())
  290.                 {
  291.                     $productsCart[] = $productCart;
  292.                 }
  293.             }
  294.         }
  295.         return $productsCart;
  296.     }
  297.     public function hasUniqskillsMultiUserCourse()
  298.     {
  299.         return !empty(array_filter($this->getProductCart()->toArray(), function($productCart)
  300.         {
  301.             return $productCart->getProductVariant() !== null
  302.                 && $productCart->getProductVariant()->getIsUniqskillsProduct()
  303.                 && $productCart->getProductVariant()->getMaxUsers() > 1
  304.                 && $productCart->getProductVariant()->getBuyMaxOne() === false
  305.                 && $productCart->getQuantity() > 1;
  306.         }));
  307.     }
  308.     public function getUniqskillsMultiUserCourse()
  309.     {
  310.         return array_filter($this->getProductCart()->toArray(), function($productCart)
  311.         {
  312.             return $productCart->getProductVariant() !== null
  313.                 && $productCart->getProductVariant()->getIsUniqskillsProduct()
  314.                 && $productCart->getProductVariant()->getMaxUsers() > 1
  315.                 && $productCart->getProductVariant()->getBuyMaxOne() === false;
  316.         });
  317.     }
  318.     //------------------------------ setters & getters
  319.     /**
  320.      * Set id
  321.      *
  322.      * @return Cart
  323.      */
  324.     public function setId($id)
  325.     {
  326.         $this->id $id;
  327.         return $this;
  328.     }
  329.     /**
  330.      * Constructor
  331.      */
  332.     public function __construct()
  333.     {
  334.         $this->productCart      = new ArrayCollection();
  335.         $this->couponPending    = new ArrayCollection();
  336.         $this->coupons          = new ArrayCollection();
  337.     }
  338.     /**
  339.      * Get id
  340.      *
  341.      * @return integer
  342.      */
  343.     public function getId()
  344.     {
  345.         return $this->id;
  346.     }
  347.     /**
  348.      * Set hash
  349.      *
  350.      * @param string $hash
  351.      *
  352.      * @return Cart
  353.      */
  354.     public function setHash($hash)
  355.     {
  356.         $this->hash $hash;
  357.         return $this;
  358.     }
  359.     /**
  360.      * Get hash
  361.      *
  362.      * @return string
  363.      */
  364.     public function getHash()
  365.     {
  366.         return $this->hash;
  367.     }
  368.     /**
  369.      * Set model
  370.      *
  371.      * @param boolean $model
  372.      *
  373.      * @return Cart
  374.      */
  375.     public function setModel($model)
  376.     {
  377.         $this->model $model;
  378.         return $this;
  379.     }
  380.     /**
  381.      * Get model
  382.      *
  383.      * @return boolean
  384.      */
  385.     public function getModel()
  386.     {
  387.         return $this->model;
  388.     }
  389.     /**
  390.      * Set createdAt
  391.      *
  392.      * @param \DateTime $createdAt
  393.      *
  394.      * @return Cart
  395.      */
  396.     public function setCreatedAt($createdAt)
  397.     {
  398.         $this->createdAt $createdAt;
  399.         return $this;
  400.     }
  401.     /**
  402.      * Get createdAt
  403.      *
  404.      * @return \DateTime
  405.      */
  406.     public function getCreatedAt()
  407.     {
  408.         return $this->createdAt;
  409.     }
  410.     /**
  411.      * Set updatedAt
  412.      *
  413.      * @param \DateTime $updatedAt
  414.      *
  415.      * @return Cart
  416.      */
  417.     public function setUpdatedAt($updatedAt)
  418.     {
  419.         $this->updatedAt $updatedAt;
  420.         return $this;
  421.     }
  422.     /**
  423.      * Get updatedAt
  424.      *
  425.      * @return \DateTime
  426.      */
  427.     public function getUpdatedAt()
  428.     {
  429.         return $this->updatedAt;
  430.     }
  431.     /**
  432.      * Set couponPending
  433.      * @deprecated Please use addCouponPending
  434.      *
  435.      * @param CouponPending $couponPending
  436.      *
  437.      * @return Cart
  438.      */
  439.     public function setCouponPending(CouponPending $couponPending null): Cart
  440.     {
  441.         if ($couponPending) {
  442.             return $this->addCouponPending($couponPending);
  443.         }
  444.         return $this;
  445.     }
  446.     /**
  447.      * Add couponPending
  448.      *
  449.      * @param CouponPending $couponPending
  450.      *
  451.      * @return $this
  452.      */
  453.     public function addCouponPending(CouponPending $couponPending): Cart
  454.     {
  455.         if (!$this->couponPending->contains($couponPending))
  456.         {
  457.             $this->couponPending->add($couponPending);
  458.             $couponPending->setCart($this);
  459.         }
  460.         return $this;
  461.     }
  462.     /**
  463.      * Remove couponPending
  464.      *
  465.      * @param CouponPending $couponPending
  466.      *
  467.      * @return $this
  468.      */
  469.     public function removeCouponPending(CouponPending $couponPending): Cart
  470.     {
  471.         if ($this->couponPending->contains($couponPending))
  472.         {
  473.             $this->couponPending->removeElement($couponPending);
  474.             $couponPending->setCart(null);
  475.         }
  476.         return $this;
  477.     }
  478.     /**
  479.      * Get couponPending
  480.      *
  481.      * @return Collection
  482.      */
  483.     public function getCouponPending()
  484.     {
  485.         return $this->couponPending;
  486.     }
  487.     /**
  488.      * Set coupon
  489.      *
  490.      * @param Coupon|null $coupon
  491.      * @deprecated Please use addCupon instead
  492.      *
  493.      * TODO: DEV-171 Zostawielem ta funkcje dla pewnosci zeby nic sie nie wysypalo
  494.      * Jesli wszystko zacznie uzywac multi kuponow bedzie mozna ja usunac
  495.      *
  496.      * @return Cart
  497.      */
  498.     public function setCoupon(Coupon $coupon null)
  499.     {
  500.         if ($this->coupon !== null && $this->coupons->contains($this->coupon))
  501.         {
  502.             $this->coupons->removeElement($this->coupon);
  503.         }
  504.         if ($coupon !== null && !$this->coupons->contains($coupon))
  505.         {
  506.             $this->coupons->add($coupon);
  507.         }
  508.         $this->coupon $coupon;
  509.         return $this;
  510.     }
  511.     /**
  512.      * Set coupons
  513.      *
  514.      * @param Collection $coupons
  515.      *
  516.      * @return Cart
  517.      */
  518.     public function setCoupons(Collection $coupons)
  519.     {
  520.         if ($coupons->isEmpty())
  521.         {
  522.             $this->coupon null;
  523.         }
  524.         $this->coupons $coupons;
  525.         /** @var Coupon $coupon */
  526.         foreach ($coupons as $coupon) {
  527.             $coupon->addCarts($this);
  528.         }
  529.         return $this;
  530.     }
  531.     /**
  532.      * Add coupon
  533.      *
  534.      * @param Coupon $coupon
  535.      *
  536.      * @return Cart
  537.      */
  538.     public function addCoupon(Coupon $coupon)
  539.     {
  540.         if(!$this->coupons->contains($coupon))
  541.         {
  542.             $this->coupons->add($coupon);
  543.             $coupon->addCarts($this);
  544.         }
  545.         return $this;
  546.     }
  547.     /**
  548.      * Remove coupon
  549.      *
  550.      * @param Coupon $coupon
  551.      *
  552.      * @return Cart
  553.      */
  554.     public function removeCoupon(Coupon $coupon)
  555.     {
  556.         if($this->coupons->contains($coupon))
  557.         {
  558.             $this->coupons->removeElement($coupon);
  559.             $coupon->removeCarts($this);
  560.         }
  561.         if ($this->coupon === $coupon)
  562.         {
  563.             $this->coupon null;
  564.         }
  565.         return $this;
  566.     }
  567.     /**
  568.      * Get coupon
  569.      * @deprecated Please use getCoupons instead
  570.      *
  571.      * TODO: DEV-171 Zostawielem ta funkcje dla pewnosci zeby nic sie nie wysypalo
  572.      * Jesli wszystko zacznie uzywac multi kuponow bedzie mozna usunac ta funkcje
  573.      *
  574.      * @return Coupon
  575.      */
  576.     public function getCoupon()
  577.     {
  578.         if ($this->coupons->isEmpty())
  579.         {
  580.             return $this->coupon;
  581.         }
  582.         return $this->coupons->first();
  583.     }
  584.     /**
  585.      * Get coupons
  586.      *
  587.      * @return Collection
  588.      */
  589.     public function getCoupons()
  590.     {
  591.         return $this->coupons;
  592.     }
  593.     /**
  594.      * Add productCart
  595.      *
  596.      * @param \App\Entity\Gos\ProductCart $productCart
  597.      *
  598.      * @return Cart
  599.      */
  600.     public function addProductCart(\App\Entity\Gos\ProductCart $productCart)
  601.     {
  602.         $this->productCart[] = $productCart;
  603.         $this->getPaymentSystems();
  604.         return $this;
  605.     }
  606.     /**
  607.      * Remove productCart
  608.      *
  609.      * @param \App\Entity\Gos\ProductCart $productCart
  610.      */
  611.     public function removeProductCart(\App\Entity\Gos\ProductCart $productCart)
  612.     {
  613.         $this->productCart->removeElement($productCart);
  614.     }
  615.     /**
  616.      * Get productCart
  617.      *
  618.      * @return \Doctrine\Common\Collections\Collection
  619.      */
  620.     public function getProductCart()
  621.     {
  622.         return $this->productCart;
  623.     }
  624.     public function hasProducts()
  625.     {
  626.         return $this->productCart->first();
  627.     }
  628.     /**
  629.      * @return Collection|ProductVariant[]
  630.      */
  631.     public function getAllProductVariants(): Collection
  632.     {
  633.         $productVariants = new ArrayCollection();
  634.         foreach ($this->productCart as $productCart)
  635.         {
  636.             /** @var ProductCart $productCart */
  637.             if ($productCart->getProductVariant())
  638.             {
  639.                 $productVariants->add($productCart->getProductVariant());
  640.             }
  641.             elseif ($productCart->getProductPack())
  642.             {
  643.                 foreach ($productCart->getProductPack()->getProductPackItem() as $productPackItem)
  644.                 {
  645.                     if ($productPackItem->getProductVariant())
  646.                     {
  647.                         $productVariants->add($productPackItem->getProductVariant());
  648.                     }
  649.                 }
  650.             }
  651.         }
  652.         return $productVariants;
  653.     }
  654.     public function checkCartHasOnlyProductPact(): bool
  655.     {
  656.         if ($this->getProductCart()->isEmpty())
  657.         {
  658.             return false;
  659.         }
  660.         /** @var ProductCart $productCart */
  661.         foreach ($this->getProductCart() as $productCart)
  662.         {
  663.             if (is_null($productCart->getProductPack()))
  664.             {
  665.                 return false;
  666.             }
  667.         }
  668.         return true;
  669.     }
  670.     public function omittedShippingCostsInAllProducts(): bool
  671.     {
  672.         foreach ($this->productCart as $productCart)
  673.         {
  674.             /** @var ProductCart $productCart */
  675.             if (!$productCart->getOmitShippingCosts())
  676.             {
  677.                 return false;
  678.             }
  679.         }
  680.         return true;
  681.     }
  682.     public function checkProlongationVariantInCart()
  683.     {
  684.         foreach ($this->productCart as $productCart)
  685.         {
  686.             /** @var ProductCart $productCart */
  687.             if ($productVariant $productCart->getProductVariant())
  688.             {
  689.                 if (substr($productVariant->getProductVariantNoComplete(), 01) == '2')
  690.                 {
  691.                     return true;
  692.                 }
  693.             }
  694.         }
  695.         return false;
  696.     }
  697.     /**
  698.      * Set address
  699.      *
  700.      * @param \App\Entity\Gos\Address $address
  701.      *
  702.      * @return Cart
  703.      */
  704.     public function setAddress(\App\Entity\Gos\Address $address null)
  705.     {
  706.         $this->address $address;
  707.         return $this;
  708.     }
  709.     /**
  710.      * Get address
  711.      *
  712.      * @return \App\Entity\Gos\Address
  713.      */
  714.     public function getAddress()
  715.     {
  716.         return $this->address;
  717.     }
  718.     /**
  719.      * Set user
  720.      *
  721.      * @param \App\Entity\Gos\User $user
  722.      *
  723.      * @return Cart
  724.      */
  725.     public function setUser(\App\Entity\Gos\User $user null)
  726.     {
  727.         $this->user $user;
  728.         return $this;
  729.     }
  730.     /**
  731.      * Get user
  732.      *
  733.      * @return \App\Entity\Gos\User
  734.      */
  735.     public function getUser()
  736.     {
  737.         return $this->user;
  738.     }
  739.     public function getUserTemporary(): ?UserTemporary
  740.     {
  741.         return $this->userTemporary;
  742.     }
  743.     public function setUserTemporary(?UserTemporary $userTemporary): self
  744.     {
  745.         $this->userTemporary $userTemporary;
  746.         return $this;
  747.     }
  748.     public function getExtendedPaymentTime(): ?int
  749.     {
  750.         return $this->extendedPaymentTime;
  751.     }
  752.     public function setExtendedPaymentTime(int $extendedPaymentTime): self
  753.     {
  754.         $this->extendedPaymentTime $extendedPaymentTime;
  755.         return $this;
  756.     }
  757.     public function getHasMultiDiscount(): ?bool
  758.     {
  759.         return $this->hasMultiDiscount;
  760.     }
  761.     public function setHasMultiDiscount(?bool $hasMultiDiscount): self
  762.     {
  763.         $this->hasMultiDiscount $hasMultiDiscount;
  764.         return $this;
  765.     }
  766.     public function getSelectedDiscount(): ?int
  767.     {
  768.         return $this->selectedDiscount;
  769.     }
  770.     public function setSelectedDiscount(?int $selectedDiscount): self
  771.     {
  772.         $this->selectedDiscount $selectedDiscount;
  773.         return $this;
  774.     }
  775.     public function getClientType(): ?ClientType
  776.     {
  777.         if (empty($this->address) || empty($this->getAddress()->getClientType()))
  778.         {
  779.             return null;
  780.         }
  781.         return $this->getAddress()->getClientType();
  782.     }
  783.     public function getClientTypeName(): ?string
  784.     {
  785.         return $this->getClientType() ? $this->getClientType()->getName() : null;
  786.     }
  787.     public function hasProductCartToGift(): bool
  788.     {
  789.         if (!empty($this->productCart))
  790.         {
  791.             foreach ($this->productCart as $productCart)
  792.             {
  793.                 /** @var ProductCart $productCart */
  794.                 if (!empty($productCart->getGift()) || $productCart->getIsGift())
  795.                 {
  796.                     return true;
  797.                 }
  798.             }
  799.         }
  800.         return false;
  801.     }
  802.     public function getProductsCartToGift()
  803.     {
  804.         if (!empty($this->productCart))
  805.         {
  806.             return $this->getProductCart()->filter(
  807.                 function ($productCart) {
  808.                     return $productCart->getGift();
  809.                 }
  810.             );
  811.         }
  812.         return [];
  813.     }
  814.     public function getCountry(): ?Country
  815.     {
  816.         return $this->country;
  817.     }
  818.     public function setCountry(?Country $country): self
  819.     {
  820.         $this->country $country;
  821.         return $this;
  822.     }
  823.     public function shouldShowGrossPrice(): bool
  824.     {
  825.         return $this->getPortalSettings() ? $this->getPortalSettings()->getShowGrossPrice() : false;
  826.     }
  827.     public function hasOnlyUniqskillsProducts(): bool
  828.     {
  829.         foreach ($this->getAllProductVariants() as $productVariant)
  830.         {
  831.             if ($productVariant->getCourses()->isEmpty())
  832.             {
  833.                 return false;
  834.             }
  835.         }
  836.         return true;
  837.     }
  838.     public function getPaymentSystems(): array
  839.     {
  840.         $paymentSystems = [];
  841. //        if ($this->hasOnlyUniqskillsProducts())
  842. //        {
  843.             if (empty($this->getAllProductVariants()))
  844.             {
  845.                 $this->setPaymentSystem(null);
  846.                 return $paymentSystems;
  847.             }
  848.             foreach ($this->getAllProductVariants() as $index => $productVariant)
  849.             {
  850.                 if ($index === 0)
  851.                 {
  852.                     foreach ($productVariant->getPaymentSystem() as $paymentSystem)
  853.                     {
  854.                         if ($paymentSystem->getIsActive() === false)
  855.                         {
  856.                             continue;
  857.                         }
  858.                         if ($this->hasProductCartToGift() && $paymentSystem->getSlug() === 'proforma-invoice')
  859.                         {
  860.                             continue;
  861.                         }
  862.                         $paymentSystems[$paymentSystem->getSlug()] = $paymentSystem;
  863.                     }
  864.                 }
  865.                 else
  866.                 {
  867.                     foreach ($paymentSystems as $key => $paymentSystem)
  868.                     {
  869.                         if (!in_array($paymentSystem$productVariant->getPaymentSystem()->toArray()))
  870.                         {
  871.                             unset($paymentSystems[$key]);
  872.                         }
  873.                     }
  874.                 }
  875.             }
  876.             uasort($paymentSystems, function (PaymentSystem $paymentSystemAPaymentSystem $paymentSystemB) {
  877.                 return $paymentSystemA->getPosition($this->getCountry(), $this->portalSettings) <=> $paymentSystemB->getPosition($this->getCountry(), $this->portalSettings);
  878.             });
  879.             if (!empty($paymentSystems) && empty($this->getPaymentSystem()))
  880.             {
  881.                 $this->setPaymentSystem(reset($paymentSystems));
  882.             }
  883. //        }
  884. //        elseif (empty($this->getCountry()) || $this->getCountry()->getSlug() == 'poland')
  885. //        {
  886. //            $paymentSystems[] = (new PaymentSystem())
  887. //                ->setName($this->getInvoicePaymentTitle())
  888. //                ->setSlug('invoice')
  889. //                ->setIsActive(true);
  890. //
  891. //            $this->setPaymentSystem(null);
  892. //        }
  893.         return $paymentSystems;
  894.     }
  895.     public function getPaymentMethod(): ?PaymentMethod
  896.     {
  897.         return $this->paymentMethod;
  898.     }
  899.     public function setPaymentMethod(?PaymentMethod $paymentMethod): self
  900.     {
  901.         $this->paymentMethod $paymentMethod;
  902.         return $this;
  903.     }
  904.     public function getPaymentSystem(): ?PaymentSystem
  905.     {
  906.         return $this->paymentSystem;
  907.     }
  908.     public function setPaymentSystem(?PaymentSystem $paymentSystem): self
  909.     {
  910.         $this->paymentSystem $paymentSystem;
  911.         return $this;
  912.     }
  913.     public function canBePaidForWithOnline()
  914.     {
  915.         /** @var ProductCart $productCart */
  916.         foreach ($this->productCart as $productCart)
  917.         {
  918.             $hasOnline array_filter($productCart->getAvailablePaymentMethods(),
  919.                 function($val) {return $val->getSlug() == 'online';});
  920.             if (empty($hasOnline))
  921.             {
  922.                 return false;
  923.             }
  924.         }
  925.         return true;
  926.     }
  927.     public function mustBeHiddenInSB()
  928.     {
  929.         $clientType $this->getAddress() ? $this->getAddress()->getClientType() : null;
  930.         /** @var ProductCart $productCart */
  931.         foreach ($this->productCart as $productCart)
  932.         {
  933.             if ($productCart->getProductVariant() !== null) {
  934.                 $additionalOptionsByClientType $productCart->getProductVariant()->getAdditionalOptionsByClientType($clientType);
  935.                 if ($additionalOptionsByClientType && $additionalOptionsByClientType->getHiddenInSBTillPaid() === true) {
  936.                     return true;
  937.                 }
  938.             }
  939.             if ($productCart->getProductPack() !== null) {
  940.                 foreach ($productCart->getProductPack()->getProductPackItem() as $productPackItem) {
  941.                     $additionalOptionsByClientType $productPackItem->getProductVariant()->getAdditionalOptionsByClientType($clientType);
  942.                     if ($additionalOptionsByClientType && $additionalOptionsByClientType->getHiddenInSBTillPaid() === true) {
  943.                         return true;
  944.                     }
  945.                 }
  946.             }
  947.         }
  948.         return false;
  949.     }
  950.     public function getProductsWithDelayedProformaPayment()
  951.     {
  952.         $products = array();
  953.         /** @var ProductCart $productCart */
  954.         foreach ($this->productCart as $productCart)
  955.         {
  956.             if ($productCart->getProductPack())
  957.             {
  958.                 if (count($productCart->getProductPack()->getProductPackItem()))
  959.                 {
  960.                     foreach ($productCart->getProductPack()->getProductPackItem() as $productPackItem)
  961.                     {
  962.                         if ($productPackItem->getProductVariant()
  963.                             ->checkIfProductVariantHavePaymentMethod($this->getClientType(), 'delayed-proforma'))
  964.                         {
  965.                             $products[] = $productPackItem->getProductVariant();
  966.                         }
  967.                     }
  968.                 }
  969.             }
  970.             else
  971.             {
  972.                 if ($productCart->getProductVariant())
  973.                 {
  974.                     if ($productCart->getProductVariant()
  975.                         ->checkIfProductVariantHavePaymentMethod($this->getClientType(), 'delayed-proforma'))
  976.                     {
  977.                         $products[] = $productCart->getProductVariant();
  978.                     }
  979.                 }
  980.             }
  981.         }
  982.         return $products;
  983.     }
  984.     public function getProductsWithInvoicePayment()
  985.     {
  986.         $products = array();
  987.         /** @var ProductCart $productCart */
  988.         foreach ($this->productCart as $productCart)
  989.         {
  990.             foreach ($productCart->getAvailablePaymentMethods() as $paymentMethod)
  991.             {
  992.                 if ($paymentMethod->getSlug() == 'invoice')
  993.                 {
  994.                     $products[] = $productCart;
  995.                 }
  996.             }
  997.         }
  998.         return $products;
  999.     }
  1000.     public function getProductsWithProformaPayment()
  1001.     {
  1002.         $products = array();
  1003.         /** @var ProductCart $productCart */
  1004.         foreach ($this->productCart as $productCart)
  1005.         {
  1006.             foreach ($productCart->getAvailablePaymentMethods() as $paymentMethod)
  1007.             {
  1008.                 if ($paymentMethod->getSlug() == 'proforma-invoice')
  1009.                 {
  1010.                     $products[] = $productCart;
  1011.                 }
  1012.             }
  1013.         }
  1014.         return $products;
  1015.     }
  1016.     public function hasProductsWithRecurringPayment(): bool
  1017.     {
  1018.         /** @var ProductCart $productCart */
  1019.         foreach ($this->productCart as $productCart)
  1020.         {
  1021.             if ($productCart->getProductVariant())
  1022.             {
  1023.                 if ($productCart->getProductVariant()->getIsRecurringSubscription() === true)
  1024.                 {
  1025.                     return true;
  1026.                 }
  1027.             }
  1028.         }
  1029.         return false;
  1030.     }
  1031.     public function getInvoicePaymentTitle()
  1032.     {
  1033.         if (!empty($this->getProductsWithDelayedProformaPayment()))
  1034.         {
  1035.             return 'Faktura pro forma';
  1036.         }
  1037.         if (!empty($this->getProductsWithInvoicePayment()) && !empty($this->getProductsWithProformaPayment()))
  1038.         {
  1039.             $title 'Faktury: VAT oraz pro-forma';
  1040.         }
  1041.         elseif (!empty($this->getProductsWithProformaPayment()))
  1042.         {
  1043.             $title 'Faktura pro-forma';
  1044.         }
  1045.         else
  1046.         {
  1047.             $title 'Faktura VAT';
  1048.         }
  1049.         if ($this->canBePaidForWithOnline())
  1050.         {
  1051.             if ($this->mustBeHiddenInSB())
  1052.                 $title 'Płatność online';
  1053.             else
  1054.                 $title .= ' z możliwością opłacenia online';
  1055.         }
  1056.         return $title;
  1057.     }
  1058.     public function hasCycleProducts(): bool
  1059.     {
  1060.         /** @var ProductCart $productCart */
  1061.         foreach ($this->getProductCart() as $productCart)
  1062.         {
  1063.             if ($productCart->getCycleProductPack())
  1064.             {
  1065.                 return true;
  1066.             }
  1067.         }
  1068.         return false;
  1069.     }
  1070.     public function getCycleProducts(): array
  1071.     {
  1072.         $cycleProducts = array();
  1073.         /** @var ProductCart $productCart */
  1074.         foreach ($this->getProductCart() as $productCart)
  1075.         {
  1076.             if ($productCart->getCycleProductPack())
  1077.             {
  1078.                 $cycleProducts[] = $productCart->getProductVariant();
  1079.             }
  1080.         }
  1081.         return $cycleProducts;
  1082.     }
  1083.     public function getAvailableClientTypes()
  1084.     {
  1085.         $clientTypes = [];
  1086.         $quantity 0;
  1087.         /** @var ProductCart $productCart */
  1088.         foreach ($this->productCart as $productCart)
  1089.         {
  1090.             $clientTypes array_merge($clientTypes$productCart->getAvailableClientTypes()['types']);
  1091.             $quantity += $productCart->getAvailableClientTypes()['quantity'];
  1092.         }
  1093.         $count array_count_values(array_map(function ($clientType) {
  1094.             return $clientType->getId();
  1095.         }, $clientTypes));
  1096.         $arr = [];
  1097.         foreach ($count as $key => $item)
  1098.         {
  1099.             // if all product variants have the same clientType
  1100.             if($quantity == $item$arr[] = $this->findClientTypeById($key$clientTypes);
  1101.         }
  1102.         return $arr;
  1103.     }
  1104.     private function findClientTypeById($id$clientTypes)
  1105.     {
  1106.         foreach ($clientTypes as $clientType)
  1107.         {
  1108.             if($clientType->getId() == $id)
  1109.             {
  1110.                 return $clientType;
  1111.             }
  1112.         }
  1113.         return false;
  1114.     }
  1115.     public function getPortalSettings(): ?PortalSettings
  1116.     {
  1117.         return $this->portalSettings;
  1118.     }
  1119.     public function setPortalSettings(?PortalSettings $portalSettings): self
  1120.     {
  1121.         $this->portalSettings $portalSettings;
  1122.         return $this;
  1123.     }
  1124.     public function isEmailInvoiceEnabled(): bool
  1125.     {
  1126.         $emailInvoiceEnabled true;
  1127.         /** @var ProductCart $cartProduct */
  1128.         foreach ($this->getProductCart() as $cartProduct)
  1129.         {
  1130.             if (!$cartProduct->getEmailInvoiceEnabled())
  1131.             {
  1132.                 $emailInvoiceEnabled false;
  1133.                 break;
  1134.             }
  1135.         }
  1136.         return $emailInvoiceEnabled;
  1137.     }
  1138.     public function askForPWZ(): bool
  1139.     {
  1140.         /** @var ProductCart $productCart */
  1141.         foreach ($this->getProductCart() as $productCart)
  1142.         {
  1143.             if ($productCart->askForPWZ())
  1144.             {
  1145.                 return true;
  1146.             }
  1147.         }
  1148.         return false;
  1149.     }
  1150.     public function isNPWZRequired(): bool
  1151.     {
  1152.         /** @var ProductCart $productCart */
  1153.         foreach ($this->getProductCart() as $productCart)
  1154.         {
  1155.             if ($productCart->isNPWZRequired())
  1156.             {
  1157.                 return true;
  1158.             }
  1159.         }
  1160.         return false;
  1161.     }
  1162.     public function getShippingType(): ?ShippingType
  1163.     {
  1164.         return $this->shippingType;
  1165.     }
  1166.     public function setShippingType(?ShippingType $shippingType): self
  1167.     {
  1168.         $this->shippingType $shippingType;
  1169.         return $this;
  1170.     }
  1171.     public function isSentToSm(): ?bool
  1172.     {
  1173.         return $this->sentToSm;
  1174.     }
  1175.     public function setSentToSm(bool $sentToSm): self
  1176.     {
  1177.         $this->sentToSm $sentToSm;
  1178.         return $this;
  1179.     }
  1180.     public function validateCoupons(): void
  1181.     {
  1182.         /** @var Coupon $coupon */
  1183.         foreach ($this->getCoupons() as $coupon) {
  1184.             if ($coupon->isValid() === false) {
  1185.                 $this->removeCoupon($coupon);
  1186.             }
  1187.         }
  1188.         /** @var ProductCart $productCart */
  1189.         foreach ($this->getProductCart() as $productCart) {
  1190.             foreach ($productCart->getCoupons() as $coupon) {
  1191.                 if ($coupon->isValid() === false) {
  1192.                     $productCart->removeCoupon($coupon);
  1193.                 }
  1194.             }
  1195.         }
  1196.     }
  1197.     public function getBurAssignedSupportId(): ?string
  1198.     {
  1199.         return $this->burAssignedSupportId;
  1200.     }
  1201.     public function setBurAssignedSupportId(?string $burAssignedSupportId): self
  1202.     {
  1203.         $this->burAssignedSupportId $burAssignedSupportId;
  1204.         return $this;
  1205.     }
  1206.     public function getSalesManago2EventId(): ?string
  1207.     {
  1208.         return $this->salesManago2EventId;
  1209.     }
  1210.     public function setSalesManago2EventId(string $salesManago2EventId): self
  1211.     {
  1212.         $this->salesManago2EventId $salesManago2EventId;
  1213.         return $this;
  1214.     }
  1215.     public function hasAtLeastOneBCProduct(): bool
  1216.     {
  1217.         /** @var ProductCart $productCart */
  1218.         foreach ($this->productCart as $productCart) {
  1219.             if ($productCart->getProductVariant() && $productCart->getProductVariant()->getMasterProduct()->getSourceSystem() === ProductSourceSystem::BC) {
  1220.                 return true;
  1221.             }
  1222.             if ($productCart->getProductPack()) {
  1223.                 foreach ($productCart->getProductPack()->getProductPackItem() as $productPackItem) {
  1224.                     if ($productPackItem->getProductVariant() && $productPackItem->getProductVariant()->getMasterProduct()->getSourceSystem() === ProductSourceSystem::BC) {
  1225.                         return true;
  1226.                     }
  1227.                 }
  1228.             }
  1229.         }
  1230.         return false;
  1231.     }
  1232. }