src/AtechNet/CoreBundle/Entity/BasketItem.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\AtechNet\CoreBundle\Entity;
  3. use App\AtechNet\CoreBundle\Helper\Furbify\CartHelper;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\ORM\Mapping\Column;
  6. use Doctrine\ORM\Mapping\Entity;
  7. use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
  8. use Doctrine\ORM\Mapping\JoinColumn;
  9. use Doctrine\ORM\Mapping\ManyToOne;
  10. use Doctrine\ORM\Mapping\OneToMany;
  11. use Doctrine\ORM\Mapping\Table as Table;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. /**
  14.  * BasketItem
  15.  *
  16.  * @Table(name="baskets__items")
  17.  * @Entity
  18.  * @HasLifecycleCallbacks
  19.  */
  20. class BasketItem extends AbstractEntity
  21. {
  22.     /**
  23.      * @var Basket
  24.      *
  25.      * @Assert\NotNull
  26.      *
  27.      * @ManyToOne(targetEntity="Basket", inversedBy="basketItems")
  28.      * @JoinColumn(name="basket_id", referencedColumnName="id")
  29.      **/
  30.     private $basket;
  31.     /**
  32.      * @var Product
  33.      *
  34.      * @Assert\NotNull
  35.      *
  36.      * @ManyToOne(targetEntity="Product")
  37.      * @JoinColumn(name="product_id", referencedColumnName="id")
  38.      **/
  39.     private $product;
  40.     /**
  41.      * @var int
  42.      *
  43.      * @Column(name="quantity", type="integer")
  44.      **/
  45.     private $quantity;
  46.     /**
  47.      * @var ArrayCollection
  48.      *
  49.      * @OneToMany(targetEntity="BasketItemAttributeOption", mappedBy="basketItem", orphanRemoval=true, cascade={"persist", "remove"})
  50.      **/
  51.     private $itemAttributes;
  52.     /**
  53.      * @return Basket
  54.      */
  55.     public function getBasket()
  56.     {
  57.         return $this->basket;
  58.     }
  59.     /**
  60.      * @param Basket $basket
  61.      */
  62.     public function setBasket($basket)
  63.     {
  64.         $this->basket = $basket;
  65.     }
  66.     /**
  67.      * @return Product
  68.      */
  69.     public function getProduct()
  70.     {
  71.         return $this->product;
  72.     }
  73.     /**
  74.      * @param Product $product
  75.      */
  76.     public function setProduct($product)
  77.     {
  78.         $this->product = $product;
  79.     }
  80.     /**
  81.      * Set quantity
  82.      *
  83.      * @param int $quantity
  84.      */
  85.     public function setQuantity($quantity)
  86.     {
  87.         $this->quantity = $quantity;
  88.     }
  89.     /**
  90.      * Get quantity
  91.      *
  92.      * @return int
  93.      */
  94.     public function getQuantity()
  95.     {
  96.         return $this->quantity;
  97.     }
  98.     /**
  99.      * @return ArrayCollection
  100.      */
  101.     public function getItemAttributes()
  102.     {
  103.         return $this->itemAttributes;
  104.     }
  105.     /**
  106.      * Returns attributeOption Ids for current BasketItem
  107.      *
  108.      * @return int|string
  109.      */
  110.     public function getItemAttributeIDs()
  111.     {
  112.         $itemAttributeOptions = $this->itemAttributes;
  113.         $ids = 'none';
  114.         if ($itemAttributeOptions->count() > 0) {
  115.             /**
  116.              * @var BasketItemAttributeOption $iao
  117.              */
  118.             foreach ($itemAttributeOptions as $iao) {
  119.                 if ($ids == 'none') {
  120.                     $ids = $iao->getAttributeOption()->getId();
  121.                 } else {
  122.                     $ids .= ',' . $iao->getAttributeOption()->getId();
  123.                 }
  124.             }
  125.         }
  126.         return $ids;
  127.     }
  128.     /**
  129.      * @param Domain $domain
  130.      * @return float
  131.      */
  132.     public function getPriceWithAttributesForDomain(Domain $domain)
  133.     {
  134.         // Customer
  135.         $customer = $this->basket->getCustomer();
  136.         // Use Zero VAT
  137.         $useZeroVat = (!is_null($customer) && $customer->useZeroVat($customer->getVatId()));
  138.         // Price excluding VAT
  139.         if ($useZeroVat) {
  140.             // Discounted Price
  141.             if ($this->product->getDomainMeta($domain)->isDiscounted()) {
  142.                 $productPrice = $this->product->getDomainMeta($domain)->getDomainDiscount();
  143.             }
  144.             // Original Price
  145.             else {
  146.                 $productPrice = $this->product->getDomainMeta($domain)->getDomainPrice();
  147.             }
  148.         }
  149.         // Price including VAT
  150.         else {
  151.             // Discounted Price
  152.             if ($this->product->getDomainMeta($domain)->isDiscounted()) {
  153.                 $productPrice = $this->product->getDomainMeta($domain)->getDomainVatDiscount();
  154.             }
  155.             // Original Price
  156.             else {
  157.                 $productPrice = $this->product->getDomainMeta($domain)->getDomainVatPrice();
  158.             }
  159.         }
  160.         // Price
  161.         $price = $productPrice;
  162.         // Attributes
  163.         $basketItemAttributeOptions = $this->getItemAttributes();
  164.         /**
  165.          * @var BasketItemAttributeOption $biao
  166.          */
  167.         if ($basketItemAttributeOptions->count() > 0) {
  168.             // Cross-sell Attribute Options
  169.             $crossSellAttributeOptions = CartHelper::getCrossSellAttributeOptionsByProductTypeId($this->product->getType()->getId(), $domain->getId());
  170.             foreach ($basketItemAttributeOptions as $biao) {
  171.                 $attributeOption = $biao->getAttributeOption();
  172.                 $attributeOptionMeta = $attributeOption->getDomainMeta($domain);
  173.                 // Attribute Option Price excluding VAT
  174.                 if ($useZeroVat) {
  175.                     $_price = $attributeOptionMeta->getDomainPrice();
  176.                 }
  177.                 // Attribute Option Price including VAT
  178.                 else {
  179.                     $_price = $attributeOptionMeta->getDomainVatPrice();
  180.                 }
  181.                 // Cross-sell Attribute Options Calculated Price
  182.                 if (isset($crossSellAttributeOptions[$attributeOption->getId()])) {
  183.                     if (isset($crossSellAttributeOptions[$attributeOption->getId()]['percentPrice'])) {
  184.                         $_price = $domain->createVatPrice((($this->getPriceWithAttributesForDomainWithException($domain, $attributeOption) * $crossSellAttributeOptions[$attributeOption->getId()]['percentPrice']) / 100), 1.);
  185.                     }
  186.                 }
  187.                 $price += $_price;
  188.             }
  189.         }
  190.         return $price;
  191.     }
  192.     /**
  193.      * @param Domain $domain
  194.      * @param AttributeOption $attributeOption
  195.      * @return float
  196.      */
  197.     public function getPriceWithAttributesForDomainWithException(Domain $domain, AttributeOption $attributeOption): float
  198.     {
  199.         // Customer
  200.         $customer = $this->basket->getCustomer();
  201.         // Use Zero VAT
  202.         $useZeroVat = (!is_null($customer) && $customer->useZeroVat($customer->getVatId()));
  203.         // Price excluding VAT
  204.         if ($useZeroVat) {
  205.             // Discounted Price
  206.             if ($this->product->getDomainMeta($domain)->isDiscounted()) {
  207.                 $productPrice = $this->product->getDomainMeta($domain)->getDomainDiscount();
  208.             }
  209.             // Original Price
  210.             else {
  211.                 $productPrice = $this->product->getDomainMeta($domain)->getDomainPrice();
  212.             }
  213.         }
  214.         // Price including VAT
  215.         else {
  216.             // Discounted Price
  217.             if ($this->product->getDomainMeta($domain)->isDiscounted()) {
  218.                 $productPrice = $this->product->getDomainMeta($domain)->getDomainVatDiscount();
  219.             }
  220.             // Original Price
  221.             else {
  222.                 $productPrice = $this->product->getDomainMeta($domain)->getDomainVatPrice();
  223.             }
  224.         }
  225.         // Price
  226.         $price = $productPrice;
  227.         // Attributes
  228.         $basketItemAttributeOptions = $this->getItemAttributes();
  229.         /**
  230.          * @var BasketItemAttributeOption $biao
  231.          */
  232.         if (!is_null($basketItemAttributeOptions) && $basketItemAttributeOptions->count() > 0) {
  233.             // Cross-sell Attribute Options
  234.             $crossSellAttributeOptions = CartHelper::getCrossSellAttributeOptionsByProductTypeId($this->product->getType()->getId(), $domain->getId());
  235.             foreach ($basketItemAttributeOptions as $biao) {
  236.                 $basketItemAttributeOption = $biao->getAttributeOption();
  237.                 if ($attributeOption->getId() !== $basketItemAttributeOption->getId()) {
  238.                     $attributeOptionMeta = $basketItemAttributeOption->getDomainMeta($domain);
  239.                     // Attribute Option Price excluding VAT
  240.                     if ($useZeroVat) {
  241.                         $_price = $attributeOptionMeta->getDomainPrice();
  242.                     }
  243.                     // Attribute Option Price including VAT
  244.                     else {
  245.                         $_price = $attributeOptionMeta->getDomainVatPrice();
  246.                     }
  247.                     // Cross-sell Attribute Options Calculated Price
  248.                     if (isset($crossSellAttributeOptions[$basketItemAttributeOption->getId()])) {
  249.                         if (isset($crossSellAttributeOptions[$basketItemAttributeOption->getId()]['percentPrice'])) {
  250.                             $_price = $domain->createVatPrice((($this->getPriceWithAttributesForDomainWithException($domain, $basketItemAttributeOption) * $crossSellAttributeOptions[$basketItemAttributeOption->getId()]['percentPrice']) / 100), 1.);
  251.                         } else {
  252.                             $_price = 0;
  253.                         }
  254.                     }
  255.                     $price += $_price;
  256.                 }
  257.             }
  258.         }
  259.         return $price;
  260.     }
  261. }