src/AtechNet/CoreBundle/Entity/Basket.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\AtechNet\CoreBundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\ORM\Mapping\Column;
  5. use Doctrine\ORM\Mapping\Entity;
  6. use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
  7. use Doctrine\ORM\Mapping\JoinColumn;
  8. use Doctrine\ORM\Mapping\ManyToOne;
  9. use Doctrine\ORM\Mapping\OneToMany;
  10. use Doctrine\ORM\Mapping\OrderBy;
  11. use Doctrine\ORM\Mapping\Table as Table;
  12. /**
  13.  * Basket
  14.  *
  15.  * @Table(name="baskets")
  16.  * @Entity
  17.  * @HasLifecycleCallbacks
  18.  */
  19. class Basket extends AbstractEntity
  20. {
  21.     /**
  22.      * @var Domain
  23.      *
  24.      * @ManyToOne(targetEntity="Domain")
  25.      * @JoinColumn(name="domain_id", referencedColumnName="id")
  26.      */
  27.     private $domain;
  28.     /**
  29.      * @var Customer
  30.      *
  31.      * @ManyToOne(targetEntity="Customer", inversedBy="baskets")
  32.      * @JoinColumn(name="customer_id", referencedColumnName="id", nullable=true)
  33.      */
  34.     private $customer;
  35.     /**
  36.      * @var CustomerAddress
  37.      *
  38.      * @ManyToOne(targetEntity="CustomerAddress")
  39.      * @JoinColumn(name="billing_address_id", referencedColumnName="id", nullable=true)
  40.      */
  41.     private $billingAddress;
  42.     /**
  43.      * @var CustomerAddress
  44.      *
  45.      * @ManyToOne(targetEntity="CustomerAddress")
  46.      * @JoinColumn(name="shipping_address_id", referencedColumnName="id", nullable=true)
  47.      */
  48.     private $shippingAddress;
  49.     /**
  50.      * @var integer
  51.      *
  52.      * @Column(name="payment_method", type="integer")
  53.      */
  54.     private $paymentMethod;
  55.     /**
  56.      * @var integer
  57.      *
  58.      * @Column(name="shipping_method", type="integer")
  59.      */
  60.     private $shippingMethod;
  61.     /**
  62.      * @var bool
  63.      *
  64.      * @Column(name="useBillingAddressAsShippingAddress", type="boolean")
  65.      */
  66.     private $useBillingAddressAsShippingAddress;
  67.     /**
  68.      * @var string
  69.      *
  70.      * @Column(name="message", type="text", nullable=true)
  71.      */
  72.     private $message;
  73.     /**
  74.      * @var string
  75.      *
  76.      * @Column(name="coupon_code", type="string", length=255, nullable=true)
  77.      */
  78.     private $couponCode;
  79.     /**
  80.      * @var bool
  81.      *
  82.      * @Column(name="active", type="boolean")
  83.      */
  84.     private $active = true;
  85.     /**
  86.      * @var ArrayCollection
  87.      *
  88.      * @OneToMany(targetEntity="BasketItem", mappedBy="basket", orphanRemoval=true, cascade={"persist", "remove"})
  89.      * @OrderBy({"id" = "ASC"})
  90.      **/
  91.     private $basketItems;
  92.     /**
  93.      * @var string
  94.      *
  95.      * @Column(name="abandoned_cart_email", type="string", nullable=true)
  96.      */
  97.     private $abandonedEmail;
  98.     /**
  99.      * @var string
  100.      */
  101.     private $email;
  102.     public function __construct()
  103.     {
  104.         parent::__construct();
  105.         $this->basketItems = new ArrayCollection();
  106.     }
  107.     /**
  108.      * @return Domain
  109.      */
  110.     public function getDomain()
  111.     {
  112.         return $this->domain;
  113.     }
  114.     /**
  115.      * @param Domain $domain
  116.      */
  117.     public function setDomain($domain)
  118.     {
  119.         $this->domain = $domain;
  120.     }
  121.     /**
  122.      * @return Customer
  123.      */
  124.     public function getCustomer()
  125.     {
  126.         return $this->customer;
  127.     }
  128.     /**
  129.      * @param Customer $customer
  130.      */
  131.     public function setCustomer($customer)
  132.     {
  133.         $this->customer = $customer;
  134.     }
  135.     /**
  136.      * Set billingAddress
  137.      *
  138.      * @param CustomerAddress $billingAddress
  139.      * @return Basket
  140.      */
  141.     public function setBillingAddress($billingAddress)
  142.     {
  143.         $this->billingAddress = $billingAddress;
  144.         return $this;
  145.     }
  146.     /**
  147.      * Get billingAddress
  148.      *
  149.      * @return CustomerAddress
  150.      */
  151.     public function getBillingAddress()
  152.     {
  153.         return $this->billingAddress;
  154.     }
  155.     /**
  156.      * Set paymentMethod
  157.      *
  158.      * @param integer $paymentMethod
  159.      * @return Basket
  160.      */
  161.     public function setPaymentMethod($paymentMethod)
  162.     {
  163.         $this->paymentMethod = $paymentMethod;
  164.         return $this;
  165.     }
  166.     /**
  167.      * Get paymentMethod
  168.      *
  169.      * @return integer
  170.      */
  171.     public function getPaymentMethod()
  172.     {
  173.         return $this->paymentMethod;
  174.     }
  175.     /**
  176.      * @return int
  177.      */
  178.     public function getShippingMethod()
  179.     {
  180.         return $this->shippingMethod;
  181.     }
  182.     /**
  183.      * @param int $shippingMethod
  184.      */
  185.     public function setShippingMethod($shippingMethod)
  186.     {
  187.         $this->shippingMethod = $shippingMethod;
  188.     }
  189.     /**
  190.      * @return CustomerAddress
  191.      */
  192.     public function getShippingAddress()
  193.     {
  194.         return $this->shippingAddress;
  195.     }
  196.     /**
  197.      * @param CustomerAddress $shippingAddress
  198.      */
  199.     public function setShippingAddress($shippingAddress)
  200.     {
  201.         $this->shippingAddress = $shippingAddress;
  202.     }
  203.     /**
  204.      * @return boolean
  205.      */
  206.     public function useBillingAddressAsShippingAddress()
  207.     {
  208.         return $this->useBillingAddressAsShippingAddress;
  209.     }
  210.     /**
  211.      * @param boolean $useBillingAddressAsShippingAddress
  212.      */
  213.     public function setUseBillingAddressAsShippingAddress($useBillingAddressAsShippingAddress)
  214.     {
  215.         $this->useBillingAddressAsShippingAddress = $useBillingAddressAsShippingAddress;
  216.     }
  217.     /**
  218.      * @return string
  219.      */
  220.     public function getMessage()
  221.     {
  222.         return $this->message;
  223.     }
  224.     /**
  225.      * @param string $message
  226.      */
  227.     public function setMessage($message)
  228.     {
  229.         $this->message = $message;
  230.     }
  231.     /**
  232.      * @return string
  233.      */
  234.     public function getCouponCode()
  235.     {
  236.         return $this->couponCode;
  237.     }
  238.     /**
  239.      * @param string $couponCode
  240.      */
  241.     public function setCouponCode($couponCode)
  242.     {
  243.         $this->couponCode = $couponCode;
  244.     }
  245.     /**
  246.      * @return boolean
  247.      */
  248.     public function isActive()
  249.     {
  250.         return $this->active;
  251.     }
  252.     /**
  253.      * @param boolean $active
  254.      */
  255.     public function setActive($active)
  256.     {
  257.         $this->active = $active;
  258.     }
  259.     /**
  260.      * Get BasketItems
  261.      * @return ArrayCollection
  262.      */
  263.     public function getBasketItems()
  264.     {
  265.         return $this->basketItems;
  266.     }
  267.     /**
  268.      * @param BasketItem $BasketItem
  269.      */
  270.     public function addBasketItem(BasketItem $BasketItem)
  271.     {
  272.         $this->basketItems->add($BasketItem);
  273.     }
  274.     /**
  275.      * Returns number of all items in basket
  276.      *
  277.      * @return int
  278.      */
  279.     public function getFullItemQuantity()
  280.     {
  281.         $itemQuantity = 0;
  282.         /**
  283.          * @var BasketItem $basketItem
  284.          */
  285.         foreach ($this->basketItems as $basketItem) {
  286.             $itemQuantity = $itemQuantity + $basketItem->getQuantity();
  287.         }
  288.         return $itemQuantity;
  289.     }
  290.     /**
  291.      * Get basket weight in kg
  292.      *
  293.      * @return int
  294.      */
  295.     public function getFullWeight()
  296.     {
  297.         $itemsWeight = 0;
  298.         /**
  299.          * @var BasketItem $basketItem
  300.          */
  301.         foreach ($this->basketItems as $basketItem) {
  302.             $itemsWeight = $itemsWeight + (($basketItem->getProduct()->getWeight() * $basketItem->getQuantity()) / 1000);
  303.         }
  304.         return $itemsWeight;
  305.     }
  306.     /**
  307.      * Get BasketItems
  308.      * @return integer
  309.      */
  310.     public function getSubTotalPrice()
  311.     {
  312.         $subTotal = 0;
  313.         /**
  314.          * @var BasketItem $basketItem
  315.          */
  316.         foreach ($this->basketItems as $basketItem) {
  317.             $subTotal = $subTotal + ($basketItem->getPriceWithAttributesForDomain($this->getDomain()) * $basketItem->getQuantity());
  318.         }
  319.         return $subTotal;
  320.     }
  321.     /**
  322.      * Get BasketItems
  323.      * @return integer
  324.      */
  325.     public function getTaxFromSubTotalPrice()
  326.     {
  327.         // TODO check if VAT ID is SET
  328.         $subTotal = $this->getSubTotalPrice();
  329.         $domainTaxPercentage = $this->getVat();
  330.         $tax = ($subTotal / (100 + $domainTaxPercentage)) * $domainTaxPercentage;
  331.         return $tax;
  332.     }
  333.     /**
  334.      * Get BasketItems
  335.      * @return integer
  336.      */
  337.     public function getTotalPrice()
  338.     {
  339.         $subTotalPrice = $this->getSubTotalPrice();
  340.         $shippingData = $this->getShippingTitleAndPrice();
  341.         $paymentData = $this->getPaymentTitleAndPrice();
  342.         $totalPrice = $subTotalPrice + $shippingData['price'] + $paymentData['price'];
  343.         return $totalPrice;
  344.     }
  345.     /**
  346.      * Check if everything is set for the basket to be converted into order
  347.      * THIS CHECK SHOULD BE USED FOR FINAL SAFETY CHECK ONLY, DOESNT RETURN ERRORS, JUST TRUE OR FALSE
  348.      *
  349.      * @return bool
  350.      */
  351.     public function canConvertBasketToOrder()
  352.     {
  353.         if ($this->getDomain() != null
  354.             && $this->getCustomer() != null
  355.             && $this->getBillingAddress() != null
  356.             && ($this->useBillingAddressAsShippingAddress() || $this->getShippingAddress() != null)
  357.             && $this->getPaymentMethod() != null && $this->getPaymentMethod() != 0
  358.             && $this->getShippingMethod() != null && $this->getShippingMethod() != 0
  359.             && $this->getBasketItems()->count() > 0) {
  360.             /**
  361.              * Check addresses for required fields
  362.              */
  363.             if (!$this->getBillingAddress()->checkRequiredFields()) {
  364.                 return false;
  365.             }
  366.             if (!$this->useBillingAddressAsShippingAddress()) {
  367.                 if (!$this->getShippingAddress()->checkRequiredFields()) {
  368.                     return false;
  369.                 }
  370.             }
  371.             return true;
  372.         } else {
  373.             return false;
  374.         }
  375.     }
  376.     public function getPaymentTitleAndPrice()
  377.     {
  378.         $domainPaymentTypes = $this->getDomain()->getDomainPaymentTypes();
  379.         $data = null;
  380.         /**
  381.          * @var DomainPaymentType $paymentType
  382.          */
  383.         if ($domainPaymentTypes->count() > 0) {
  384.             foreach ($domainPaymentTypes as $paymentType) {
  385.                 if ($paymentType->getType() == $this->getPaymentMethod()) {
  386.                     $data = [
  387.                         'title' => $paymentType->getTitle(),
  388.                         'price' => $paymentType->getPrice(),
  389.                     ];
  390.                 }
  391.             }
  392.         }
  393.         return $data;
  394.     }
  395.     public function getShippingTitleAndPrice()
  396.     {
  397.         $domainShippingTypes = $this->getDomain()->getDomainShippingTypes();
  398.         $data = null;
  399.         /**
  400.          * @var DomainShippingType $shippingType
  401.          */
  402.         if ($domainShippingTypes->count() > 0) {
  403.             foreach ($domainShippingTypes as $shippingType) {
  404.                 if ($shippingType->getType() == $this->getShippingMethod()) {
  405.                     $data = [
  406.                         'title' => $shippingType->getTitle(),
  407.                         'price' => $shippingType->getPrice(),
  408.                     ];
  409.                 }
  410.             }
  411.         }
  412.         return $data;
  413.     }
  414.     /**
  415.      * @return int
  416.      */
  417.     public function getVat() // TODO tz orderVat
  418.     {
  419.         $dateStr = $this->getCreated()->format('YmdHi');
  420.         $vatId = 'not set';
  421.         if ($this->customer && $this->customer->getVatId() != null) {
  422.             $vatId = $this->customer->getVatId();
  423.         }
  424.         return $this->customer && $this->customer->useZeroVat($vatId, $dateStr) ? 0 : $this->getDomain()->getVat($dateStr);
  425.     }
  426.     /**
  427.      * Validate Pickup Point availability
  428.      *
  429.      * @return bool
  430.      */
  431.     public function isPickupPointAvailable(): bool
  432.     {
  433.         /**
  434.          * @var BasketItem $basketItem
  435.          */
  436.         foreach ($this->basketItems as $basketItem) {
  437.             // All In One, Monitor, PC, PC Set
  438.             if (in_array($basketItem->getProduct()->getType()->getId(), [ProductType::PRODUCT_TYPE_ID_ALL_IN_ONE, ProductType::PRODUCT_TYPE_ID_MONITOR, ProductType::PRODUCT_TYPE_ID_PC, ProductType::PRODUCT_TYPE_ID_PC_SET])) {
  439.                 return false;
  440.             }
  441.         }
  442.         return true;
  443.     }
  444.     /**
  445.      * @return string|null
  446.      */
  447.     public function getEmail(): ?string
  448.     {
  449.         return $this->email;
  450.     }
  451.     /**
  452.      * @param string|null $email
  453.      */
  454.     public function setEmail(?string $email)
  455.     {
  456.         $this->email = $email;
  457.     }
  458.     /**
  459.      * @return string
  460.      */
  461.     public function getAbandonedEmail()
  462.     {
  463.         return $this->abandonedEmail;
  464.     }
  465.     /**
  466.      * @param string $abandonedEmail
  467.      */
  468.     public function setAbandonedEmail($abandonedEmail)
  469.     {
  470.         $this->abandonedEmail = $abandonedEmail;
  471.     }
  472. }