<?php
namespace App\Entity\ECommerce;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use App\Entity\ECommerce\Traits\DatesInterface;
use App\Entity\ECommerce\Traits\DatesTrait;
use Symfony\Component\Validator\Constraints;
/**
* CartItem
*
* @ORM\Table(name="cart_item")
* @ORM\Entity(repositoryClass="App\Repository\ECommerce\CartItemRepository")
*/
class CartItem implements DatesInterface
{
public const SHIPPED_STATUS = Invoice::SHIPPED_STATUS;
public const PARTIALLY_SHIPPED_STATUS = Invoice::PARTIALLY_SHIPPED_STATUS;
public const UNSHIPPED_STATUS = Invoice::UNSHIPPED_STATUS;
/**
* Money calculations rounding precision
*/
public const ROUNDING_PRECISION = Cart::ROUNDING_PRECISION;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $id;
/**
* @var float
*
* @ORM\Column(name="product_base_price", type="decimal", precision=18, scale=4, nullable=true, options={"comment": "product default price"})
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $productBasePrice;
/**
* @var float
*
* @ORM\Column(name="product_base_price_converted", type="decimal", precision=18, scale=4, nullable=true, options={"comment": "product default price converted in clients currency"})
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $productBasePriceConverted;
/**
* @var float
*
* @ORM\Column(name="product_price", type="decimal", precision=18, scale=4, nullable=true, options={"comment": "product discount price"})
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $productPrice;
/**
* @var float
*
* @ORM\Column(name="product_price_converted", type="decimal", precision=18, scale=4, nullable=true, options={"comment": "product discount price converted in clients currency"})
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $productPriceConverted;
/**
* @var float
*
* @ORM\Column(name="product_tax", type="decimal", precision=18, scale=4, nullable=true)
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $productTax;
/**
* @var float
*
* @ORM\Column(name="product_tax_converted", type="decimal", precision=18, scale=4, nullable=true)
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $productTaxConverted;
/**
* @var int
*
* @ORM\Column(name="quantity", type="integer")
*
* @Constraints\GreaterThanOrEqual(1)
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $quantity=1;
/**
* @var float
*
* @ORM\Column(name="rebate_percent", type="decimal", precision=5, scale=2, nullable=true)
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $rebatePercent;
/**
* @var float
*
* @ORM\Column(name="rebate", type="decimal", precision=18, scale=4, nullable=true, options={"comment": "discount"})
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $rebate;
/**
* @var float
*
* @ORM\Column(name="rebate_converted", type="decimal", precision=18, scale=4, nullable=true, options={"comment": "discount converted in clients currency"})
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $rebateConverted;
/**
* @var float
*
* @ORM\Column(name="sum", type="decimal", precision=18, scale=4, nullable=true, options={"comment": "product price with rebate multiplied with quantity"})
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $sum;
/**
* @var float
*
* @ORM\Column(name="sum_converted", type="decimal", precision=18, scale=4, nullable=true, options={"comment": "sum value converted in clients currency"})
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $sumConverted;
/**
* @var float
*
* @ORM\Column(name="tax_percent", type="decimal", precision=7, scale=4, nullable=true)
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $taxPercent;
/**
* @var float
*
* @ORM\Column(name="tax", type="decimal", precision=18, scale=4, nullable=true)
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $tax;
/**
* @var float
*
* @ORM\Column(name="tax_converted", type="decimal", precision=18, scale=4, nullable=true)
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $taxConverted;
/**
* @var float
*
* @ORM\Column(name="total", type="decimal", precision=18, scale=4, nullable=true)
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $total;
/**
* @var float
*
* @ORM\Column(name="total_converted", type="decimal", precision=18, scale=4, nullable=true)
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $totalConverted;
/**
* @var \DateTime
*
* @ORM\Column(name="added_at", type="datetime", nullable=false)
*
* @JMS\Groups(groups={"cart_private", "view_cart_item"})
*/
private $addedAt;
/**
* @var \DateTime
*
* @ORM\Column(name="deliver_date", type="datetime", nullable=true)
*
* @Constraints\GreaterThanOrEqual(propertyPath="minDeliveryDate", groups={"CheckOut"})
* @Constraints\NotNull(groups={"CheckOut"})
*/
private $deliverDate;
/**
* @var \DateTime
*/
protected $minDeliveryDate;
/**
* @var Cart
*
* @ORM\ManyToOne(targetEntity="App\Entity\ECommerce\Cart", inversedBy="items")
*/
private $cart;
/**
* @var Depot
*
* @ORM\ManyToOne(targetEntity="App\Entity\ECommerce\Depot")
* @ORM\JoinColumn(name="depot", referencedColumnName="id", nullable=true)
*/
private $depot;
/**
* @var Product
*
* @ORM\ManyToOne(targetEntity="App\Entity\ECommerce\Product")
* @ORM\JoinColumn(name="product", referencedColumnName="id")
*
* @JMS\Groups(groups={})
*/
private $product;
/**
* @deprecated use \App\Repository\ECommerce\DepotProductRepository::getByDepotAndProduct(Depot $depot, Product $product) method
*/
private $depotProduct = null;
/**
* @var Collection
*
* @ORM\OneToMany(targetEntity="App\Entity\ECommerce\BillItem", mappedBy="cartItem")
*/
private $billItems;
use DatesTrait {
DatesTrait::__construct as __DTConstruct;
}
/**
* CartItem constructor.
*/
public function __construct(Cart $cart, Product $product, Depot $depot)
{
$this->billItems = new ArrayCollection();
$this->setCart($cart)
->setProduct($product)
->setDepot($depot)
->setAddedAt();
$this->__DTConstruct();
}
/**
* Get id
*
* @return int
*/
public function getId(): ?int
{
return $this->id;
}
/**
* Get id
*/
public function setId(int $id): self
{
$this->id = $id;
return $this;
}
/**
* Set quantity
*
*
*/
public function setQuantity(int $quantity): CartItem
{
$this->quantity = max($quantity, 1);
return $this;
}
/**
* Get quantity
*
* @return int
*/
public function getQuantity(): ?int
{
return $this->quantity;
}
/**
* Set product
*
*
*/
public function setProduct(?Product $product = null): CartItem
{
$this->product = $product;
return $this;
}
/**
* Get product
*/
public function getProduct(): Product
{
return $this->product;
}
/**
* Set productPrice
*
* @param float $productPrice
*/
public function setProductPrice($productPrice): CartItem
{
$this->productPrice = $productPrice;
return $this;
}
/**
* Get productPrice
*
* @return float
*/
public function getProductPrice(): ?float
{
return $this->productPrice;
}
/**
* Set productPriceConverted
*
* @param string $productPriceConverted
*/
public function setProductPriceConverted($productPriceConverted): CartItem
{
$this->productPriceConverted = $productPriceConverted;
return $this;
}
/**
* Get productPriceConverted
*
* @return float
*/
public function getProductPriceConverted(): ?float
{
return $this->productPriceConverted;
}
/**
* Set productTax
*
* @param float $productTax
*/
public function setProductTax($productTax): CartItem
{
$this->productTax = $productTax;
return $this;
}
/**
* Get productTax
*
* @return float
*/
public function getProductTax(): ?float
{
return $this->productTax;
}
/**
* Set productTaxConverted
*
* @param string $productTaxConverted
*/
public function setProductTaxConverted($productTaxConverted): CartItem
{
$this->productTaxConverted = $productTaxConverted;
return $this;
}
/**
* Get productTaxConverted
*
* @return float
*/
public function getProductTaxConverted(): ?float
{
return $this->productTaxConverted;
}
/**
* Set tax
*
* @param float $tax
*/
public function setTax($tax): CartItem
{
$this->tax = $tax;
return $this;
}
/**
* Get tax
*
* @return float
*/
public function getTax(): ?float
{
return $this->tax;
}
/**
* Set taxConverted
*
* @param string $taxConverted
*/
public function setTaxConverted($taxConverted): CartItem
{
$this->taxConverted = $taxConverted;
return $this;
}
/**
* Get taxConverted
*
* @return float
*/
public function getTaxConverted(): ?float
{
return $this->taxConverted;
}
/**
* Set sum
*
* @param float $sum
*/
public function setSum($sum): CartItem
{
$this->sum = $sum;
return $this;
}
/**
* Get sum
*
* @return float
*/
public function getSum(): ?float
{
return $this->sum;
}
/**
* Set sumConverted
*
* @param string $sumConverted
*/
public function setSumConverted($sumConverted): CartItem
{
$this->sumConverted = $sumConverted;
return $this;
}
/**
* Get sumConverted
*
* @return float
*/
public function getSumConverted(): ?float
{
return $this->sumConverted;
}
/**
* Set total
*
* @param string $total
*/
public function setTotal($total): CartItem
{
$this->total = $total;
return $this;
}
/**
* Get total
*
* @return float
*/
public function getTotal(): ?float
{
return $this->total;
}
/**
* Set totalConverted
*
* @param string $totalConverted
*/
public function setTotalConverted($totalConverted): CartItem
{
$this->totalConverted = $totalConverted;
return $this;
}
/**
* Get totalConverted
*
* @return float
*/
public function getTotalConverted(): ?float
{
return $this->totalConverted;
}
/**
* Set addedAt
*
* @param \DateTime $addedAt
*/
public function setAddedAt($addedAt=null): CartItem
{
$this->addedAt = $addedAt ?? new \DateTime();
return $this;
}
/**
* Get addedAt
*
* @return \DateTime
*/
public function getAddedAt(): ?\DateTime
{
return $this->addedAt;
}
/**
* Set cart
*
*
*/
public function setCart(Cart $cart = null): CartItem
{
$this->cart = $cart;
return $this;
}
/**
* Get cart
*/
public function getCart(): Cart
{
return $this->cart;
}
/**
* Set depot
*
*
*/
public function setDepot(Depot $depot = null): CartItem
{
$this->depot = $depot;
return $this;
}
/**
* Get depot
*/
public function getDepot(): Depot
{
return $this->depot;
}
/**
* Set rebatePercent
*
* @param string $rebatePercent
*/
public function setRebatePercent($rebatePercent): CartItem
{
$this->rebatePercent = $rebatePercent;
return $this;
}
/**
* Get rebatePercent
*
* @return float
*/
public function getRebatePercent(): ?float
{
return $this->rebatePercent;
}
/**
* Set rebate
*
* @param string $rebate
*/
public function setRebate($rebate): CartItem
{
$this->rebate = $rebate;
return $this;
}
/**
* Get rebate
*
* @return float
*/
public function getRebate(): ?float
{
return $this->rebate;
}
/**
* Set rebateConverted
*
* @param string $rebateConverted
*/
public function setRebateConverted($rebateConverted): CartItem
{
$this->rebateConverted = $rebateConverted;
return $this;
}
/**
* Get rebateConverted
*
* @return float
*/
public function getRebateConverted(): ?float
{
return $this->rebateConverted;
}
/**
* Set taxPercent
*
* @param string $taxPercent
*/
public function setTaxPercent($taxPercent): CartItem
{
$this->taxPercent = $taxPercent;
return $this;
}
/**
* Get taxPercent
*
* @return float
*/
public function getTaxPercent(): ?float
{
return $this->taxPercent;
}
/**
* Set deliverDate
*
* @param \DateTime $deliverDate
*/
public function setDeliverDate($deliverDate): CartItem
{
$this->deliverDate = $deliverDate;
return $this;
}
/**
* Get deliverDate
*
* @return \DateTime
*/
public function getDeliverDate(): ?\DateTime
{
return $this->deliverDate;
}
public function getMinDeliveryDate(): \DateTime
{
$deliveryAt = $this->getDepot()->getDeliverAt();
$this->minDeliveryDate = new \DateTime($deliveryAt);
return $this->minDeliveryDate;
}
public function setMinDeliveryDate(\DateTime $minDeliveryDate): CartItem
{
$this->minDeliveryDate = $minDeliveryDate;
return $this;
}
/**
* Add billItem.
*
*
*/
public function addBillItem(BillItem $billItem): CartItem
{
$this->billItems[] = $billItem;
return $this;
}
/**
* Remove billItem.
*
*
* @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeBillItem(BillItem $billItem): bool
{
return $this->billItems->removeElement($billItem);
}
/**
* Get billItems.
*/
public function getBillItems(): Collection
{
return $this->billItems;
}
public function getStatus(): string
{
$statuses = $this->billItems->map(fn(BillItem $billItem): int => $billItem->getBill()->getShipped() ? $billItem->getQuantity() : 0)->toArray();
$sum =array_sum($statuses);
if ($sum === 0) {
return self::UNSHIPPED_STATUS;
}
if ($this->quantity === $sum) {
return self::SHIPPED_STATUS;
}
return self::PARTIALLY_SHIPPED_STATUS;
}
/**
* Set productBasePrice.
*
* @param string|null $productBasePrice
*/
public function setProductBasePrice($productBasePrice = null): CartItem
{
$this->productBasePrice = $productBasePrice;
return $this;
}
/**
* Get productBasePrice.
*
* @return float
*/
public function getProductBasePrice(): ?float
{
return $this->productBasePrice;
}
/**
* Set productBasePriceConverted.
*
* @param string|null $productBasePriceConverted
*/
public function setProductBasePriceConverted($productBasePriceConverted = null): CartItem
{
$this->productBasePriceConverted = $productBasePriceConverted;
return $this;
}
/**
* Get productBasePriceConverted.
*
* @return float
*/
public function getProductBasePriceConverted(): ?float
{
return $this->productBasePriceConverted;
}
}