<?php
namespace App\Entity;
use App\Repository\CartItemRepository;
use App\Traits\DateTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=CartItemRepository::class)
*/
class CartItem
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* SKU : Identifiant d'unité de stock
* @ORM\Column(type="string", length=255)
*/
private $sku;
/**
* @ORM\Column(type="integer")
*/
private $quantity;
/**
* @ORM\ManyToOne(targetEntity=Cart::class, inversedBy="items")
* @ORM\JoinColumn(nullable=false, referencedColumnName="id")
*/
private $cart;
/**
* @ORM\OneToMany(targetEntity=CartItemOption::class, mappedBy="item",cascade={ "remove"})
*/
private $options;
/**
* @ORM\OneToMany(targetEntity=CartItemShipping::class, mappedBy="item", cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=true)
*/
private $shipments;
/**
* @ORM\OneToMany(targetEntity=CartItemParticipant::class, mappedBy="cartItem", cascade={"persist","remove"})
* @ORM\JoinColumn(nullable=true)
*/
private $participants;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $wishDateEvasion;
/**
* provenance du produit
* @ORM\Column(type="string", length=255)
*/
private ?string $catalogue;
use DateTrait;
public function __construct()
{
$this->options = new ArrayCollection();
$this->shipments = new ArrayCollection();
$this->participants = new ArrayCollection();
}
/*
* ============================================================================================
* =============================== FONCTIONS CUSTOM ===========================================
* ============================================================================================
*/
/**
* @Assert\IsFalse(message = "Les quantités sont incorrectes, merci de rectifier")
* @return boolean
*/
public function isQuantityByItemShipping(): bool
{
$quantity = 0;
foreach ( $this->getShipments() as $shipment ) {
if ( $shipment instanceof CartItemShipping ) {
$quantity += $shipment->getQuantity();
}
}
if ( $quantity <= $this->getQuantity() ) {
return FALSE;
}
return TRUE;
}
/**
* @return Collection|CartItemShipping[]
*/
public function getShipments(): Collection
{
return $this->shipments;
}
public function getQuantity(): ?int
{
return $this->quantity;
}
/*
* ============================================================================================
* ============================== FIN FONCTIONS CUSTOM ========================================
* ============================================================================================
*/
public function setQuantity( int $quantity ): self
{
$this->quantity = $quantity;
return $this;
}
public function getInitItemShipping()
{
foreach ( $this->getShipments() as $shipment ) {
if ( $shipment instanceof CartItemShipping && $shipment->getInit() ) {
return $shipment;
}
}
return NULL;
}
public function getItemsINotShipping(): bool
{
foreach ( $this->getShipments() as $shipment ) {
if ( $shipment instanceof CartItemShipping && NULL === $shipment->getShippingAddress() ) {
return TRUE;
}
}
return FALSE;
}
public function getId(): ?int
{
return $this->id;
}
public function getCatalogue(): ?string
{
return $this->catalogue;
}
public function setCatalogue( string $catalogue ): self
{
$this->catalogue = $catalogue;
return $this;
}
public function getSku(): ?string
{
return $this->sku;
}
public function setSku( string $sku ): self
{
$this->sku = $sku;
return $this;
}
public function getCart(): ?Cart
{
return $this->cart;
}
public function setCart( ?Cart $cart ): self
{
$this->cart = $cart;
return $this;
}
/**
* @return Collection|CartItemOption[]
*/
public function getOptions(): Collection
{
return $this->options;
}
public function addOption( CartItemOption $option ): self
{
if ( !$this->options->contains( $option ) ) {
$this->options[] = $option;
$option->setItem( $this );
}
return $this;
}
public function removeOption( CartItemOption $option ): self
{
if ( $this->options->removeElement( $option ) ) {
// set the owning side to null (unless already changed)
if ( $option->getItem() === $this ) {
$option->setItem( NULL );
}
}
return $this;
}
public function addQuantity( $quantity ): CartItem
{
if ( is_null( $this->quantity ) ) {
$this->quantity = 0;
}
$this->quantity += $quantity;
return $this;
}
public function editQuantity( $quantity ): CartItem
{
$this->quantity = $quantity;
return $this;
}
public function removeQuantity( $quantity ): CartItem
{
$this->quantity -= $quantity;
if ( $this->quantity < 0 ) {
$this->quantity = 0;
}
return $this;
}
public function addShipment( CartItemShipping $shipment ): self
{
if ( !$this->shipments->contains( $shipment ) ) {
$this->shipments[] = $shipment;
$shipment->setItem( $this );
}
return $this;
}
public function removeShipment( CartItemShipping $shipment ): self
{
if ( $this->shipments->removeElement( $shipment ) ) {
// set the owning side to null (unless already changed)
if ( $shipment->getItem() === $this ) {
$shipment->setItem( NULL );
}
}
return $this;
}
/**
* @return Collection|CartItemParticipant[]
*/
public function getParticipants(): Collection
{
return $this->participants;
}
public function addParticipant( CartItemParticipant $participant ): self
{
if ( !$this->participants->contains( $participant ) ) {
$this->participants[] = $participant;
$participant->setCartItem( $this );
}
return $this;
}
public function removeParticipant( CartItemParticipant $participant ): self
{
if ( $this->participants->removeElement( $participant ) ) {
// set the owning side to null (unless already changed)
if ( $participant->getCartItem() === $this ) {
$participant->setCartItem( NULL );
}
}
return $this;
}
public function getWishDateEvasion()
{
return $this->wishDateEvasion;
}
public function setWishDateEvasion($wishDateEvasion)
{
$this->wishDateEvasion = $wishDateEvasion;
}
}