<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use App\Helpers\Encoder;
/**
* CustomersBasket
*
* @ORM\Table(name="carts_items")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
*/
class CartItem
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \App\Entity\Cart
*
* @ORM\ManyToOne(targetEntity="App\Entity\Cart", inversedBy="items")
* @ORM\JoinColumn(name="cart_id", referencedColumnName="id", onDelete="cascade")
*/
private $cart;
/**
* @var \App\Entity\Product
*
* @ORM\ManyToOne(targetEntity="App\Entity\Product")
* @ORM\JoinColumn(name="product_id", referencedColumnName="products_id", onDelete="cascade")
*/
private $product;
/**
* @var int
*
* @ORM\Column(name="quantity", type="integer", nullable=false)
*/
private $quantity = '0';
/**
* @var string
*
* @ORM\Column(name="final_price", type="decimal", precision=15, scale=4, nullable=false, options={"default"="0.0000"})
*/
private $unitPrice = '0.0000';
/**
* @var string
*
* @ORM\Column(name="total_price", type="decimal", precision=15, scale=4, nullable=false, options={"default"="0.0000"})
*/
private $totalPrice = '0.0000';
/**
* @var ?MarketingRule
*/
private $marketingRule = null;
/**
* @var ?string
*/
private $discountType = null;
/**
* @var \DateTime|null
*
* @ORM\Column(name="created_at", type="datetime", nullable=true)
*/
private $createdAt;
/**
* @var \DateTime|null
*
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
*/
private $updatedAt;
private $quantityOffered = 0;
public function __construct(Product $product, $qty) {
$this->product = $product;
$this->quantity = $qty;
}
public function getId() {
return $this->id;
}
public function getCart(): \App\Entity\Cart {
return $this->cart;
}
public function getQuantity() {
return $this->quantity;
}
public function getUnitPrice($withEcotax = true) {
if($withEcotax === false){
return $this->unitPrice - $this->product->getEcotax();
}
return $this->unitPrice;
}
public function getPrice($withEcotax = true) {
return $this->getUnitPrice($withEcotax, $this->getCart()->getPriceGroup()) * $this->getQuantity();
}
public function getNormalPrice($withEcotax = true) {
return $this->getProduct()->getPrice($withEcotax, $this->getCart()->getPriceGroup()) * $this->getQuantity();
}
public function getCreatedAt() {
return $this->createdAt;
}
public function getUpdatedAt(): ?\DateTime {
return $this->updatedAt;
}
public function getProduct() {
return $this->product;
}
public function getReminded() {
return $this->reminded;
}
public function hasDiscount() {
return (Encoder::priceFormat($this->getPrice(true, $this->getCart()->getPriceGroup())) != Encoder::priceFormat($this->getTotalPrice())) ||
(Encoder::priceFormat($this->getUnitPrice(false)) != Encoder::priceFormat($this->getProduct()->getPrice(false, $this->getCart()->getPriceGroup())));
}
public function getDiscount() {
return $this->getUnitPrice(false) - $this->getProduct()->getPrice(false, $this->getCart()->getPriceGroup());
}
public function getPercentDiscount() {
return round($this->getDiscount(false) / $this->getProduct()->getPrice(false, $this->getCart()->getPriceGroup()),2)*100;
}
public function getTotalPrice() {
return $this->totalPrice;
}
public function getMarketingRule(): ?MarketingRule {
return $this->marketingRule;
}
public function getDiscountType(): ?string {
return $this->discountType;
}
public function getQuantityOffered() {
return $this->quantityOffered;
}
public function setId($id) {
$this->id = $id;
}
public function setCart(\App\Entity\Cart $cart): void {
$this->cart = $cart;
}
public function setQuantity($quantity) {
$this->quantity = $quantity;
}
public function setUnitPrice($finalPrice) {
$this->unitPrice = $finalPrice;
}
public function setCreatedAt($createdAt) {
$this->createdAt = $createdAt;
}
public function setUpdatedAt(?\DateTime $updatedAt): void {
$this->updatedAt = $updatedAt;
}
public function setProduct(\App\Entity\Product $product) {
$this->product = $product;
}
public function setReminded(\DateTime $date) {
$this->reminded = $date;
}
public function setTotalPrice($totalPrice): void {
$this->totalPrice = $totalPrice;
}
public function setMarketingRule(?MarketingRule $marketingRule): void {
$this->marketingRule = $marketingRule;
}
public function setDiscountType(?string $discountType): void {
if($discountType == 'soleil'){
$this->discountType = $this->product->isNonSoleil() ? 'non-soleil' : $discountType;
}else{
$this->discountType = $discountType;
}
}
public function setQuantityOffered($quantityOffered): void {
$this->quantityOffered = $quantityOffered;
}
public function toArray()
{
return [
'id'=>$this->getProduct()->getId(),
'name'=>$this->getProduct()->getName(),
'reference'=>$this->getProduct()->getModel(),
'unitPrice'=>$this->getUnitPrice(),
'normalUnitPrice'=>$this->getProduct()->getPrice(false, $this->getCart()->getPriceGroup()),
'price'=>$this->getPrice(false, $this->getCart()->getPriceGroup()),
'normalPrice'=>$this->getNormalPrice(),
'totalPrice'=>$this->getTotalPrice(),
'quantity'=>$this->getQuantity(),
'product'=>$this->getProduct()->toArray(),
];
}
/**
* @ORM\PostLoad
*/
public function postLoad(\Doctrine\ORM\Event\LifecycleEventArgs $arg) {
}
/**
* @ORM\PrePersist()
*/
public function prePersist(\Doctrine\ORM\Event\LifecycleEventArgs $args) {
$this->setCreatedAt(new \DateTime());
}
/**
* @ORM\PreUpdate()
*/
public function preUpdate(\Doctrine\ORM\Event\LifecycleEventArgs $args) {
$this->setUpdatedAt(new \DateTime());
}
}