<?php
namespace App\Entity;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Exception;
/**
* Class Cart
*
* @ORM\Entity
* @ORM\Table(name="malys_cart")
* @ORM\HasLifecycleCallbacks()
* @package App\Entity
* @ORM\Entity(repositoryClass="App\Repository\CartRepository")
*/
class Cart
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var Customer
*
* Many carts have one customer
* @ORM\ManyToOne(targetEntity="Customer")
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
*/
protected $customer;
/**
* @var User
*
* Many carts have one user
*
* @ORM\ManyToOne(targetEntity="User", inversedBy="carts")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="CartItem", mappedBy="cart", cascade={"persist", "remove"})
*/
protected $items;
/**
* @var DateTime
*
* @ORM\Column(name="createdAt", type="datetime", nullable=false)
*/
protected $createdAt;
/**
* @var DateTime
*
* @ORM\Column(name="updatedAt", type="datetime", nullable=true)
*/
protected $updatedAt;
/**
* Order is word reserved in mysql
* @ORM\ManyToOne(targetEntity="App\Entity\Order")
* @ORM\JoinColumn(name="orderToModify", referencedColumnName="id", nullable=true)
*/
protected $orderToModify;
/**
* @ORM\Column(name="comments" ,type="array", nullable=true)
*/
protected $comments = [];
/**
* @ORM\Column(name="requested_delivery_dates" ,type="array", nullable=true)
*/
protected $requestedDeliveryDates = [];
/******************************* FIELDS FOR VIEW ******************************/
/**
* @var boolean
*/
protected $calculated;
/**
* @var float
*/
protected $totalPrice;
/**
* Cart constructor.
*/
public function __construct()
{
$this->items = new ArrayCollection();
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return Customer
*/
public function getCustomer()
{
return $this->customer;
}
/**
* @param Customer $customer
*/
public function setCustomer($customer)
{
$this->customer = $customer;
}
/**
* @return User
*/
public function getUser()
{
return $this->user;
}
/**
* @param User $user
*/
public function setUser($user)
{
$this->user = $user;
}
// /**
// * @return SupplierCustomer
// */
// public function getSupplierCustomer()
// {
// return $this->supplierCustomer;
// }
//
// /**
// * @return SupplierCustomer|null
// */
// public function getEnabledSupplierCustomer()
// {
// if ($this->supplierCustomer instanceof SupplierCustomer
// && $this->supplierCustomer->getSupplier() instanceof Supplier
// && $this->supplierCustomer->getSupplier()->isEnabled()) {
// return $this->supplierCustomer;
// }
// return null;
// }
//
// /**
// * @param SupplierCustomer $supplierCustomer
// */
// public function setSupplierCustomer($supplierCustomer)
// {
// $this->supplierCustomer = $supplierCustomer;
// }
/**
* @return ArrayCollection
*/
public function getItems()
{
return $this->items;
}
/**
* @return ArrayCollection|\Doctrine\Common\Collections\Collection
*/
// public function getActiveItems()
// {
// return $this->items->filter(function($entry) {
// /** @var $entry CartItem */
// if($entry->getProduct() instanceof Product && $entry->getProduct()->isEnabled()) {
// return true;
// }
// return false;
// });
// }
/**
* @param ArrayCollection $items
*/
public function setItems($items)
{
$this->items = $items;
}
/**
* @param CartItem $item
*/
public function addItem($item)
{
$this->items->add($item);
}
/**
* @param CartItem $item
*/
public function removeItem($item)
{
if ($this->items->contains($item)) {
$this->items->removeElement($item);
$item->setCart(null);
}
}
/**
* @return DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set createdAt
*
* @return Cart
* @ORM\PrePersist
*/
public function setCreatedAt()
{
$this->createdAt = new DateTime();
$this->updatedAt = new DateTime();
return $this;
}
/**
* @return DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set updatedAt
*
* @return Cart
* @ORM\PreUpdate
*/
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é AB');
}
}
/**
* @param float $totalPrice
*/
public function setTotalPrice($totalPrice)
{
$this->totalPrice = $totalPrice;
}
/**
* @return mixed
*/
public function getOrderToModify()
{
return $this->orderToModify;
}
/**
* @param mixed $orderToModify
*/
public function setOrderToModify($orderToModify)
{
$this->orderToModify = $orderToModify;
}
/**
* @return mixed
*/
public function getComments()
{
return $this->comments;
}
/**
* @param mixed $comments
*/
public function setComments($comments)
{
$this->comments = $comments;
}
/**
* @return mixed
*/
public function getRequestedDeliveryDates()
{
return $this->requestedDeliveryDates;
}
/**
* @param mixed $requestedDeliveryDates
*/
public function setRequestedDeliveryDates($requestedDeliveryDates)
{
$this->requestedDeliveryDates = $requestedDeliveryDates;
}
}