<?php
namespace App\Entity;
use App\Entity\Product\Product;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Exception;
/**
* Class Cart Item
*
* @ORM\Entity
* @ORM\Table(name="malys_cart_item")
* @ORM\HasLifecycleCallbacks()
* @package App\Entity
* @ORM\Entity(repositoryClass="App\Repository\CartItemRepository")
*/
class CartItem
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var Cart
*
* @ORM\ManyToOne(targetEntity="Cart", inversedBy="items")
* @ORM\JoinColumn(name="cart_id", referencedColumnName="id")
*/
protected $cart;
/**
* @var integer
*
* @ORM\Column(name="quantity", type="integer", nullable=false)
*/
protected $quantity = 1;
/**
* @var float $bundling
* @ORM\Column(name="bundling", type="float", nullable=true)
*/
protected $bundling;
/**
* @var Product
*
* @ORM\ManyToOne(targetEntity="App\Entity\Product\Product")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
*/
protected $product;
/**
*
* @ORM\ManyToOne(targetEntity="ProductVariation")
* @ORM\JoinColumn(name="product_variation_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
*/
protected $productVariation;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\ProductService")
* @ORM\JoinTable(name="malys_cart_item_services")
*/
protected $services;
/**
* @var DateTime
*
* @ORM\Column(name="createdAt", type="datetime", nullable=false)
*/
protected $createdAt;
/**
* @var DateTime
*
* @ORM\Column(name="updatedAt", type="datetime", nullable=true)
*/
protected $updatedAt;
/******************************* FIELDS FOR VIEW ******************************/
/**
* @var boolean
*/
protected $calculated;
/**
* @var float
*/
protected $totalPrice;
/**
* CartItem constructor.
*/
public function __construct()
{
$this->services = new ArrayCollection();
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return Cart
*/
public function getCart()
{
return $this->cart;
}
/**
* @param Cart $cart
*/
public function setCart($cart)
{
$this->cart = $cart;
}
/**
* @return int
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* @param int $quantity
*/
public function setQuantity($quantity)
{
$this->quantity = $quantity;
}
/**
* @return Product
*/
public function getProduct()
{
return $this->product;
}
/**
* @param Product $product
*/
public function setProduct($product)
{
$this->product = $product;
}
/**
* @return DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set createdAt
*
* @return CartItem
* @ORM\PrePersist
* @throws Exception
*/
public function setCreatedAt()
{
$this->createdAt = new DateTime();
$this->updatedAt = new DateTime();
return $this;
}
/**
* @return DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set updatedAt
*
* @return CartItem
* @ORM\PreUpdate
* @throws Exception
*/
public function setUpdatedAt()
{
$this->updatedAt = new DateTime();
return $this;
}
/**
* @return bool
*/
public function isCalculated()
{
return $this->calculated;
}
/**
* @param bool $calculated
*/
public function setCalculated($calculated)
{
$this->calculated = $calculated;
}
/**
* @return float
* @throws Exception
*/
public function getTotalPrice()
{
if ($this->isCalculated()) {
return $this->totalPrice;
} else {
throw new Exception('Le montant n\'a pas été calculé AA');
}
}
/**
* @param float $totalPrice
*/
public function setTotalPrice($totalPrice)
{
$this->totalPrice = $totalPrice;
}
/**
* @return float
*/
public function getBundling()
{
return $this->bundling;
}
/**
* @param float $bundling
*/
public function setBundling($bundling)
{
$this->bundling = $bundling;
}
public function getPositiveBundling()
{
if (!empty($this->bundling) && $this->bundling > 0) {
$bundling = $this->bundling;
} else {
$bundling = 1;
}
return $bundling;
}
/**
* @return mixed
*/
public function getProductVariation()
{
return $this->productVariation;
}
/**
* @param mixed $productVariation
*/
public function setProductVariation($productVariation)
{
$this->productVariation = $productVariation;
}
/**
* @return mixed
*/
public function getServices()
{
return $this->services;
}
/**
* @param mixed $services
*/
public function setServices($services)
{
$this->services = $services;
}
public function addService(ProductService $productService)
{
if (!$this->services->contains($productService)) {
$this->services->add($productService);
}
return $this;
}
public function removeService(ProductService $productService)
{
$this->services->removeElement($productService);
return $this;
}
}