<?php
namespace App\AtechNet\CoreBundle\Entity;
use App\AtechNet\CoreBundle\Helper\Furbify\CartHelper;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\Table as Table;
use Symfony\Component\Validator\Constraints as Assert;
/**
* BasketItem
*
* @Table(name="baskets__items")
* @Entity
* @HasLifecycleCallbacks
*/
class BasketItem extends AbstractEntity
{
/**
* @var Basket
*
* @Assert\NotNull
*
* @ManyToOne(targetEntity="Basket", inversedBy="basketItems")
* @JoinColumn(name="basket_id", referencedColumnName="id")
**/
private $basket;
/**
* @var Product
*
* @Assert\NotNull
*
* @ManyToOne(targetEntity="Product")
* @JoinColumn(name="product_id", referencedColumnName="id")
**/
private $product;
/**
* @var int
*
* @Column(name="quantity", type="integer")
**/
private $quantity;
/**
* @var ArrayCollection
*
* @OneToMany(targetEntity="BasketItemAttributeOption", mappedBy="basketItem", orphanRemoval=true, cascade={"persist", "remove"})
**/
private $itemAttributes;
/**
* @return Basket
*/
public function getBasket()
{
return $this->basket;
}
/**
* @param Basket $basket
*/
public function setBasket($basket)
{
$this->basket = $basket;
}
/**
* @return Product
*/
public function getProduct()
{
return $this->product;
}
/**
* @param Product $product
*/
public function setProduct($product)
{
$this->product = $product;
}
/**
* Set quantity
*
* @param int $quantity
*/
public function setQuantity($quantity)
{
$this->quantity = $quantity;
}
/**
* Get quantity
*
* @return int
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* @return ArrayCollection
*/
public function getItemAttributes()
{
return $this->itemAttributes;
}
/**
* Returns attributeOption Ids for current BasketItem
*
* @return int|string
*/
public function getItemAttributeIDs()
{
$itemAttributeOptions = $this->itemAttributes;
$ids = 'none';
if ($itemAttributeOptions->count() > 0) {
/**
* @var BasketItemAttributeOption $iao
*/
foreach ($itemAttributeOptions as $iao) {
if ($ids == 'none') {
$ids = $iao->getAttributeOption()->getId();
} else {
$ids .= ',' . $iao->getAttributeOption()->getId();
}
}
}
return $ids;
}
/**
* @param Domain $domain
* @return float
*/
public function getPriceWithAttributesForDomain(Domain $domain)
{
// Customer
$customer = $this->basket->getCustomer();
// Use Zero VAT
$useZeroVat = (!is_null($customer) && $customer->useZeroVat($customer->getVatId()));
// Price excluding VAT
if ($useZeroVat) {
// Discounted Price
if ($this->product->getDomainMeta($domain)->isDiscounted()) {
$productPrice = $this->product->getDomainMeta($domain)->getDomainDiscount();
}
// Original Price
else {
$productPrice = $this->product->getDomainMeta($domain)->getDomainPrice();
}
}
// Price including VAT
else {
// Discounted Price
if ($this->product->getDomainMeta($domain)->isDiscounted()) {
$productPrice = $this->product->getDomainMeta($domain)->getDomainVatDiscount();
}
// Original Price
else {
$productPrice = $this->product->getDomainMeta($domain)->getDomainVatPrice();
}
}
// Price
$price = $productPrice;
// Attributes
$basketItemAttributeOptions = $this->getItemAttributes();
/**
* @var BasketItemAttributeOption $biao
*/
if ($basketItemAttributeOptions->count() > 0) {
// Cross-sell Attribute Options
$crossSellAttributeOptions = CartHelper::getCrossSellAttributeOptionsByProductTypeId($this->product->getType()->getId(), $domain->getId());
foreach ($basketItemAttributeOptions as $biao) {
$attributeOption = $biao->getAttributeOption();
$attributeOptionMeta = $attributeOption->getDomainMeta($domain);
// Attribute Option Price excluding VAT
if ($useZeroVat) {
$_price = $attributeOptionMeta->getDomainPrice();
}
// Attribute Option Price including VAT
else {
$_price = $attributeOptionMeta->getDomainVatPrice();
}
// Cross-sell Attribute Options Calculated Price
if (isset($crossSellAttributeOptions[$attributeOption->getId()])) {
if (isset($crossSellAttributeOptions[$attributeOption->getId()]['percentPrice'])) {
$_price = $domain->createVatPrice((($this->getPriceWithAttributesForDomainWithException($domain, $attributeOption) * $crossSellAttributeOptions[$attributeOption->getId()]['percentPrice']) / 100), 1.);
}
}
$price += $_price;
}
}
return $price;
}
/**
* @param Domain $domain
* @param AttributeOption $attributeOption
* @return float
*/
public function getPriceWithAttributesForDomainWithException(Domain $domain, AttributeOption $attributeOption): float
{
// Customer
$customer = $this->basket->getCustomer();
// Use Zero VAT
$useZeroVat = (!is_null($customer) && $customer->useZeroVat($customer->getVatId()));
// Price excluding VAT
if ($useZeroVat) {
// Discounted Price
if ($this->product->getDomainMeta($domain)->isDiscounted()) {
$productPrice = $this->product->getDomainMeta($domain)->getDomainDiscount();
}
// Original Price
else {
$productPrice = $this->product->getDomainMeta($domain)->getDomainPrice();
}
}
// Price including VAT
else {
// Discounted Price
if ($this->product->getDomainMeta($domain)->isDiscounted()) {
$productPrice = $this->product->getDomainMeta($domain)->getDomainVatDiscount();
}
// Original Price
else {
$productPrice = $this->product->getDomainMeta($domain)->getDomainVatPrice();
}
}
// Price
$price = $productPrice;
// Attributes
$basketItemAttributeOptions = $this->getItemAttributes();
/**
* @var BasketItemAttributeOption $biao
*/
if (!is_null($basketItemAttributeOptions) && $basketItemAttributeOptions->count() > 0) {
// Cross-sell Attribute Options
$crossSellAttributeOptions = CartHelper::getCrossSellAttributeOptionsByProductTypeId($this->product->getType()->getId(), $domain->getId());
foreach ($basketItemAttributeOptions as $biao) {
$basketItemAttributeOption = $biao->getAttributeOption();
if ($attributeOption->getId() !== $basketItemAttributeOption->getId()) {
$attributeOptionMeta = $basketItemAttributeOption->getDomainMeta($domain);
// Attribute Option Price excluding VAT
if ($useZeroVat) {
$_price = $attributeOptionMeta->getDomainPrice();
}
// Attribute Option Price including VAT
else {
$_price = $attributeOptionMeta->getDomainVatPrice();
}
// Cross-sell Attribute Options Calculated Price
if (isset($crossSellAttributeOptions[$basketItemAttributeOption->getId()])) {
if (isset($crossSellAttributeOptions[$basketItemAttributeOption->getId()]['percentPrice'])) {
$_price = $domain->createVatPrice((($this->getPriceWithAttributesForDomainWithException($domain, $basketItemAttributeOption) * $crossSellAttributeOptions[$basketItemAttributeOption->getId()]['percentPrice']) / 100), 1.);
} else {
$_price = 0;
}
}
$price += $_price;
}
}
}
return $price;
}
}