src/Entity/Cart.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Knp\DoctrineBehaviors\Contract\Entity\SoftDeletableInterface;
  4. use Knp\DoctrineBehaviors\Model\SoftDeletable\SoftDeletableTrait;
  5. use Knp\DoctrineBehaviors\Model\Blameable\BlameableTrait;
  6. use Knp\DoctrineBehaviors\Contract\Entity\BlameableInterface;
  7. use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
  8. use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. use Symfony\Component\HttpFoundation\File\UploadedFile;
  14. use Symfony\Component\HttpFoundation\File\File;
  15. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  16. /**
  17.  * @ORM\Entity
  18.  * @ORM\Table(name="cart")
  19.  */
  20. class Cart implements BlameableInterfaceTimestampableInterfaceSoftDeletableInterface  {
  21.     use BlameableTrait;
  22.     use TimestampableTrait;
  23.     
  24.     use SoftDeletableTrait;
  25.     
  26. /**
  27.      * @ORM\Id
  28.      * @ORM\Column(type="integer")
  29.      * @ORM\GeneratedValue(strategy="AUTO")
  30.      */
  31.     protected $id;
  32.     /**
  33.      * @ORM\ManyToOne(targetEntity="App\Entity\User")
  34.      */
  35.     private $user;
  36.     /**
  37.      * @ORM\ManyToOne(targetEntity="App\Entity\DeliveryCountry")
  38.      */
  39.     private $deliveryCountry;
  40.     /**
  41.      * @ORM\ManyToOne(targetEntity="App\Entity\Language")
  42.      */
  43.     private $language;
  44.     /**
  45.      * @ORM\ManyToOne(targetEntity="App\Entity\DeliveryMethod")
  46.      */
  47.     private $deliveryMethod;
  48.     /**
  49.      * @ORM\ManyToOne(targetEntity="App\Entity\PaymentMethod")
  50.      */
  51.     private $paymentMethod;
  52.     /**
  53.      * @ORM\ManyToOne(targetEntity="App\Entity\RebateCode")
  54.      */
  55.     private $rebateCode;
  56.     /**
  57.      * @Doctrine\ORM\Mapping\Column(type="decimal", precision=10, scale=2, nullable=true)
  58.      */
  59.     protected $shippingCost;
  60.     /**
  61.      * @var string
  62.      *
  63.      * @ORM\Column(name="session", type="string", length=40, nullable=true)
  64.      */
  65.     private $session;
  66.     /**
  67.      * @var string
  68.      *
  69.      * @ORM\Column(name="ip", type="string", length=40, nullable=true)
  70.      */
  71.     private $ip;
  72.     /**
  73.      * @var string
  74.      *
  75.      * @ORM\Column(name="user_agent", type="text", nullable=true)
  76.      */
  77.     private $userAgent;
  78.     /**
  79.      * @var string
  80.      *
  81.      * @ORM\Column(name="rebate_code_content", type="string", length=255, nullable=true)
  82.      */
  83.     private $rebateCodeContent;
  84.     /**
  85.      * @ORM\OneToMany(targetEntity="App\Entity\CartProduct", mappedBy="cart", cascade={"all"})
  86.      * @Doctrine\ORM\Mapping\OrderBy({"id" = "DESC"})
  87.      */
  88.     protected $products;
  89.     /**
  90.      * @Doctrine\ORM\Mapping\Column(name="removed_products_notiication", type="boolean", nullable=true, options={"default"=0})
  91.      */
  92.     protected $removedProductsNotification 0;
  93.     /**
  94.      * Constructor
  95.      */
  96.     public function __construct()
  97.     {
  98.         $this->products = new \Doctrine\Common\Collections\ArrayCollection();
  99.         $this->removedProductsNotification 0;
  100.     }
  101.     /**
  102.      * Get id
  103.      *
  104.      * @return integer
  105.      */
  106.     public function getId()
  107.     {
  108.         return $this->id;
  109.     }
  110.     /**
  111.      * Set shippingCost
  112.      *
  113.      * @param string $shippingCost
  114.      *
  115.      * @return Cart
  116.      */
  117.     public function setShippingCost($shippingCost)
  118.     {
  119.         $this->shippingCost $shippingCost;
  120.         return $this;
  121.     }
  122.     /**
  123.      * Get shippingCost
  124.      *
  125.      * @return string
  126.      */
  127.     public function getShippingCost()
  128.     {
  129.         return $this->shippingCost;
  130.     }
  131.     /**
  132.      * Set session
  133.      *
  134.      * @param string $session
  135.      *
  136.      * @return Cart
  137.      */
  138.     public function setSession($session)
  139.     {
  140.         $this->session $session;
  141.         return $this;
  142.     }
  143.     /**
  144.      * Get session
  145.      *
  146.      * @return string
  147.      */
  148.     public function getSession()
  149.     {
  150.         return $this->session;
  151.     }
  152.     /**
  153.      * Set ip
  154.      *
  155.      * @param string $ip
  156.      *
  157.      * @return Cart
  158.      */
  159.     public function setIp($ip)
  160.     {
  161.         $this->ip $ip;
  162.         return $this;
  163.     }
  164.     /**
  165.      * Get ip
  166.      *
  167.      * @return string
  168.      */
  169.     public function getIp()
  170.     {
  171.         return $this->ip;
  172.     }
  173.     /**
  174.      * Set userAgent
  175.      *
  176.      * @param string $userAgent
  177.      *
  178.      * @return Cart
  179.      */
  180.     public function setUserAgent($userAgent)
  181.     {
  182.         $this->userAgent $userAgent;
  183.         return $this;
  184.     }
  185.     /**
  186.      * Get userAgent
  187.      *
  188.      * @return string
  189.      */
  190.     public function getUserAgent()
  191.     {
  192.         return $this->userAgent;
  193.     }
  194.     /**
  195.      * Set rebateCodeContent
  196.      *
  197.      * @param string $rebateCodeContent
  198.      *
  199.      * @return Cart
  200.      */
  201.     public function setRebateCodeContent($rebateCodeContent)
  202.     {
  203.         $this->rebateCodeContent $rebateCodeContent;
  204.         return $this;
  205.     }
  206.     /**
  207.      * Get rebateCodeContent
  208.      *
  209.      * @return string
  210.      */
  211.     public function getRebateCodeContent()
  212.     {
  213.         return $this->rebateCodeContent;
  214.     }
  215.     /**
  216.      * Set user
  217.      *
  218.      * @param \App\Entity\User $user
  219.      *
  220.      * @return Cart
  221.      */
  222.     public function setUser(\App\Entity\User $user null)
  223.     {
  224.         $this->user $user;
  225.         return $this;
  226.     }
  227.     /**
  228.      * Get user
  229.      *
  230.      * @return \App\Entity\User
  231.      */
  232.     public function getUser()
  233.     {
  234.         return $this->user;
  235.     }
  236.     /**
  237.      * Set deliveryCountry
  238.      *
  239.      * @param \App\Entity\DeliveryCountry $deliveryCountry
  240.      *
  241.      * @return Cart
  242.      */
  243.     public function setDeliveryCountry(\App\Entity\DeliveryCountry $deliveryCountry null)
  244.     {
  245.         $this->deliveryCountry $deliveryCountry;
  246.         return $this;
  247.     }
  248.     /**
  249.      * Get deliveryCountry
  250.      *
  251.      * @return \App\Entity\DeliveryCountry
  252.      */
  253.     public function getDeliveryCountry()
  254.     {
  255.         return $this->deliveryCountry;
  256.     }
  257.     /**
  258.      * Set deliveryMethod
  259.      *
  260.      * @param \App\Entity\DeliveryMethod $deliveryMethod
  261.      *
  262.      * @return Cart
  263.      */
  264.     public function setDeliveryMethod(\App\Entity\DeliveryMethod $deliveryMethod null)
  265.     {
  266.         $this->deliveryMethod $deliveryMethod;
  267.         return $this;
  268.     }
  269.     /**
  270.      * Get deliveryMethod
  271.      *
  272.      * @return \App\Entity\DeliveryMethod
  273.      */
  274.     public function getDeliveryMethod()
  275.     {
  276.         return $this->deliveryMethod;
  277.     }
  278.     /**
  279.      * Set paymentMethod
  280.      *
  281.      * @param \App\Entity\PaymentMethod $paymentMethod
  282.      *
  283.      * @return Cart
  284.      */
  285.     public function setPaymentMethod(\App\Entity\PaymentMethod $paymentMethod null)
  286.     {
  287.         $this->paymentMethod $paymentMethod;
  288.         return $this;
  289.     }
  290.     /**
  291.      * Get paymentMethod
  292.      *
  293.      * @return \App\Entity\PaymentMethod
  294.      */
  295.     public function getPaymentMethod()
  296.     {
  297.         return $this->paymentMethod;
  298.     }
  299.     /**
  300.      * Set rebateCode
  301.      *
  302.      * @param \App\Entity\RebateCode $rebateCode
  303.      *
  304.      * @return Cart
  305.      */
  306.     public function setRebateCode(\App\Entity\RebateCode $rebateCode null)
  307.     {
  308.         $this->rebateCode $rebateCode;
  309.         return $this;
  310.     }
  311.     /**
  312.      * Get rebateCode
  313.      *
  314.      * @return \App\Entity\RebateCode
  315.      */
  316.     public function getRebateCode()
  317.     {
  318.         return $this->rebateCode;
  319.     }
  320.     /**
  321.      * Any of the products in the cart applicable for discount?
  322.      * @return bool
  323.      */
  324.     public function isCartApplicableForAmountDiscount() {
  325.         foreach ($this->getProducts() as $product) {
  326.             if ($this->isProductApplicableForRebate($product->getProduct())) {
  327.                 return true;
  328.             }
  329.         }
  330.         return false;
  331.     }
  332.     /**
  333.      * Check if product is applicable for rebate
  334.      * @param Product $product
  335.      * @return bool
  336.      */
  337.     public function isProductApplicableForRebate(Product $product) {
  338.         if (!$this->getRebateCode()) {
  339.             return true;
  340.         }
  341.         $lang $this->getLanguage();
  342.         if ($lang) {
  343.             $langParam $product->getLangParamByLocale($lang->getLocale());
  344.             /**
  345.              * Jeśli produkt ma cenę skreśloną, a jego kategoria nie jest Outlet to nie uwzględniamy kodu rabatowego
  346.              */
  347.             if ($langParam && $langParam->getPriceCrossed() && $product->getMainCategory() && $product->getMainCategory()->getId() !== Category::TYPE_OUTLET) {
  348.                 return false;
  349.             }
  350.         }
  351.         if (count($this->getRebateCode()->getLandingPages()) or count($this->getRebateCode()->getProductProducers())) {
  352.             /** @var $landingPage LandingPage */
  353.             foreach ($this->getRebateCode()->getLandingPages() as $landingPage) {
  354.                 foreach ($landingPage->getProducts() as $landingPageProduct) {
  355.                     if ($landingPageProduct->getId() == $product->getId()) {
  356.                         return true;
  357.                     }
  358.                 }
  359.             }
  360.             $locale $this->getLanguage()->getLocaleShort();
  361.             foreach ($this->getRebateCode()->getLandingPages() as $landingPage) {
  362.                 if ($product->getProductProducer($locale) and $landingPage->getProductProducer() and $landingPage->getProductProducer()->getId() == $product->getProductProducer($locale)->getId()) {
  363.                     return true;
  364.                 }
  365.             }
  366.             foreach ($this->getRebateCode()->getProductProducers() as $productProducer) {
  367.                 if ($product->getProductProducer($locale) and $productProducer->getId() == $product->getProductProducer($locale)->getId()) {
  368.                     return true;
  369.                 }
  370.             }
  371.             if ($this->getRebateCode()->getCode() === 'BTS24') {
  372.                 //return true;
  373.             }
  374.             return false;
  375.         }
  376.         return true;
  377.     }
  378.     /**
  379.      * Add product
  380.      *
  381.      * @param \App\Entity\CartProduct $product
  382.      *
  383.      * @return Cart
  384.      */
  385.     public function addProduct(\App\Entity\CartProduct $product)
  386.     {
  387.         $this->products[] = $product;
  388.         return $this;
  389.     }
  390.     /**
  391.      * Remove product
  392.      *
  393.      * @param \App\Entity\CartProduct $product
  394.      */
  395.     public function removeProduct(\App\Entity\CartProduct $product)
  396.     {
  397.         $this->products->removeElement($product);
  398.     }
  399.     /**
  400.      * Get products
  401.      *
  402.      * @return \Doctrine\Common\Collections\Collection
  403.      */
  404.     public function getProducts()
  405.     {
  406.         return $this->products;
  407.     }
  408.     public function getLanguage(): ?Language
  409.     {
  410.         return $this->language;
  411.     }
  412.     public function setLanguage(?Language $language): self
  413.     {
  414.         $this->language $language;
  415.         return $this;
  416.     }
  417.     /**
  418.      * @return int
  419.      */
  420.     public function getRemovedProductsNotification(): int
  421.     {
  422.         return $this->removedProductsNotification;
  423.     }
  424.     /**
  425.      * @param int $removedProductsNotification
  426.      */
  427.     public function setRemovedProductsNotification(int $removedProductsNotification): void
  428.     {
  429.         $this->removedProductsNotification $removedProductsNotification;
  430.     }
  431. }