<?php
namespace App\Entity;
use Knp\DoctrineBehaviors\Contract\Entity\SoftDeletableInterface;
use Knp\DoctrineBehaviors\Model\SoftDeletable\SoftDeletableTrait;
use Knp\DoctrineBehaviors\Model\Blameable\BlameableTrait;
use Knp\DoctrineBehaviors\Contract\Entity\BlameableInterface;
use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity
* @ORM\Table(name="cart")
*/
class Cart implements BlameableInterface, TimestampableInterface, SoftDeletableInterface {
use BlameableTrait;
use TimestampableTrait;
use SoftDeletableTrait;
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
*/
private $user;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\DeliveryCountry")
*/
private $deliveryCountry;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Language")
*/
private $language;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\DeliveryMethod")
*/
private $deliveryMethod;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\PaymentMethod")
*/
private $paymentMethod;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\RebateCode")
*/
private $rebateCode;
/**
* @Doctrine\ORM\Mapping\Column(type="decimal", precision=10, scale=2, nullable=true)
*/
protected $shippingCost;
/**
* @var string
*
* @ORM\Column(name="session", type="string", length=40, nullable=true)
*/
private $session;
/**
* @var string
*
* @ORM\Column(name="ip", type="string", length=40, nullable=true)
*/
private $ip;
/**
* @var string
*
* @ORM\Column(name="user_agent", type="text", nullable=true)
*/
private $userAgent;
/**
* @var string
*
* @ORM\Column(name="rebate_code_content", type="string", length=255, nullable=true)
*/
private $rebateCodeContent;
/**
* @ORM\OneToMany(targetEntity="App\Entity\CartProduct", mappedBy="cart", cascade={"all"})
* @Doctrine\ORM\Mapping\OrderBy({"id" = "DESC"})
*/
protected $products;
/**
* @Doctrine\ORM\Mapping\Column(name="removed_products_notiication", type="boolean", nullable=true, options={"default"=0})
*/
protected $removedProductsNotification = 0;
/**
* Constructor
*/
public function __construct()
{
$this->products = new \Doctrine\Common\Collections\ArrayCollection();
$this->removedProductsNotification = 0;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set shippingCost
*
* @param string $shippingCost
*
* @return Cart
*/
public function setShippingCost($shippingCost)
{
$this->shippingCost = $shippingCost;
return $this;
}
/**
* Get shippingCost
*
* @return string
*/
public function getShippingCost()
{
return $this->shippingCost;
}
/**
* Set session
*
* @param string $session
*
* @return Cart
*/
public function setSession($session)
{
$this->session = $session;
return $this;
}
/**
* Get session
*
* @return string
*/
public function getSession()
{
return $this->session;
}
/**
* Set ip
*
* @param string $ip
*
* @return Cart
*/
public function setIp($ip)
{
$this->ip = $ip;
return $this;
}
/**
* Get ip
*
* @return string
*/
public function getIp()
{
return $this->ip;
}
/**
* Set userAgent
*
* @param string $userAgent
*
* @return Cart
*/
public function setUserAgent($userAgent)
{
$this->userAgent = $userAgent;
return $this;
}
/**
* Get userAgent
*
* @return string
*/
public function getUserAgent()
{
return $this->userAgent;
}
/**
* Set rebateCodeContent
*
* @param string $rebateCodeContent
*
* @return Cart
*/
public function setRebateCodeContent($rebateCodeContent)
{
$this->rebateCodeContent = $rebateCodeContent;
return $this;
}
/**
* Get rebateCodeContent
*
* @return string
*/
public function getRebateCodeContent()
{
return $this->rebateCodeContent;
}
/**
* Set user
*
* @param \App\Entity\User $user
*
* @return Cart
*/
public function setUser(\App\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \App\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set deliveryCountry
*
* @param \App\Entity\DeliveryCountry $deliveryCountry
*
* @return Cart
*/
public function setDeliveryCountry(\App\Entity\DeliveryCountry $deliveryCountry = null)
{
$this->deliveryCountry = $deliveryCountry;
return $this;
}
/**
* Get deliveryCountry
*
* @return \App\Entity\DeliveryCountry
*/
public function getDeliveryCountry()
{
return $this->deliveryCountry;
}
/**
* Set deliveryMethod
*
* @param \App\Entity\DeliveryMethod $deliveryMethod
*
* @return Cart
*/
public function setDeliveryMethod(\App\Entity\DeliveryMethod $deliveryMethod = null)
{
$this->deliveryMethod = $deliveryMethod;
return $this;
}
/**
* Get deliveryMethod
*
* @return \App\Entity\DeliveryMethod
*/
public function getDeliveryMethod()
{
return $this->deliveryMethod;
}
/**
* Set paymentMethod
*
* @param \App\Entity\PaymentMethod $paymentMethod
*
* @return Cart
*/
public function setPaymentMethod(\App\Entity\PaymentMethod $paymentMethod = null)
{
$this->paymentMethod = $paymentMethod;
return $this;
}
/**
* Get paymentMethod
*
* @return \App\Entity\PaymentMethod
*/
public function getPaymentMethod()
{
return $this->paymentMethod;
}
/**
* Set rebateCode
*
* @param \App\Entity\RebateCode $rebateCode
*
* @return Cart
*/
public function setRebateCode(\App\Entity\RebateCode $rebateCode = null)
{
$this->rebateCode = $rebateCode;
return $this;
}
/**
* Get rebateCode
*
* @return \App\Entity\RebateCode
*/
public function getRebateCode()
{
return $this->rebateCode;
}
/**
* Any of the products in the cart applicable for discount?
* @return bool
*/
public function isCartApplicableForAmountDiscount() {
foreach ($this->getProducts() as $product) {
if ($this->isProductApplicableForRebate($product->getProduct())) {
return true;
}
}
return false;
}
/**
* Check if product is applicable for rebate
* @param Product $product
* @return bool
*/
public function isProductApplicableForRebate(Product $product) {
if (!$this->getRebateCode()) {
return true;
}
$lang = $this->getLanguage();
if ($lang) {
$langParam = $product->getLangParamByLocale($lang->getLocale());
/**
* Jeśli produkt ma cenę skreśloną, a jego kategoria nie jest Outlet to nie uwzględniamy kodu rabatowego
*/
if ($langParam && $langParam->getPriceCrossed() && $product->getMainCategory() && $product->getMainCategory()->getId() !== Category::TYPE_OUTLET) {
return false;
}
}
if (count($this->getRebateCode()->getLandingPages()) or count($this->getRebateCode()->getProductProducers())) {
/** @var $landingPage LandingPage */
foreach ($this->getRebateCode()->getLandingPages() as $landingPage) {
foreach ($landingPage->getProducts() as $landingPageProduct) {
if ($landingPageProduct->getId() == $product->getId()) {
return true;
}
}
}
$locale = $this->getLanguage()->getLocaleShort();
foreach ($this->getRebateCode()->getLandingPages() as $landingPage) {
if ($product->getProductProducer($locale) and $landingPage->getProductProducer() and $landingPage->getProductProducer()->getId() == $product->getProductProducer($locale)->getId()) {
return true;
}
}
foreach ($this->getRebateCode()->getProductProducers() as $productProducer) {
if ($product->getProductProducer($locale) and $productProducer->getId() == $product->getProductProducer($locale)->getId()) {
return true;
}
}
if ($this->getRebateCode()->getCode() === 'BTS24') {
//return true;
}
return false;
}
return true;
}
/**
* Add product
*
* @param \App\Entity\CartProduct $product
*
* @return Cart
*/
public function addProduct(\App\Entity\CartProduct $product)
{
$this->products[] = $product;
return $this;
}
/**
* Remove product
*
* @param \App\Entity\CartProduct $product
*/
public function removeProduct(\App\Entity\CartProduct $product)
{
$this->products->removeElement($product);
}
/**
* Get products
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getProducts()
{
return $this->products;
}
public function getLanguage(): ?Language
{
return $this->language;
}
public function setLanguage(?Language $language): self
{
$this->language = $language;
return $this;
}
/**
* @return int
*/
public function getRemovedProductsNotification(): int
{
return $this->removedProductsNotification;
}
/**
* @param int $removedProductsNotification
*/
public function setRemovedProductsNotification(int $removedProductsNotification): void
{
$this->removedProductsNotification = $removedProductsNotification;
}
}