<?php
namespace App\Entity;
use App\Repository\BasketItemRepository;
use App\Service\ProductService;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
#[ORM\Entity(repositoryClass: BasketItemRepository::class)]
class BasketItem implements JsonSerializable
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\ManyToOne(targetEntity: 'Product', inversedBy: 'basketItems')]
#[ORM\JoinColumn(name: 'id_product', referencedColumnName: 'id')]
private $product;
#[ORM\Column(type: 'integer')]
private $quantity;
public function getId(): ?int
{
return $this->id;
}
public function getQuantity(): ?int
{
return $this->quantity;
}
public function setQuantity(int $quantity): self
{
$this->quantity = $quantity;
return $this;
}
public function getCost()
{
return $this->getProduct()->getCost() * $this->getQuantity();
}
public function getTva(){
$cost = $this->getCost();
return $this->getTvaWithCost($cost);
}
public function getTvaWithCost($cost){
return ProductService::calculateTvaFromTTC($cost, $this->getProduct()->getTva());
}
public function getProduct(): ?Product
{
return $this->product;
}
public function setProduct(Product $product): self
{
$this->product = $product;
return $this;
}
public function __construct($product, int $quantity){
$this->product = $product;
$this->quantity = $quantity;
}
public function jsonSerialize()
{
$vars = get_object_vars($this);
return $vars;
}
public function refresh(EntityManagerInterface $entityManager){
$productRepository = $entityManager->getRepository(Product::class);
$product = $productRepository->find($this->getProduct()->getId());
$this->setProduct($product);
}
public function getQtyPreOrder(){
$qtePreCmd = 0;
if($this->getProduct()->isPrecommande()){
$qtePreCmd = $this->getQuantity() - $this->getProduct()->getProduitQteStock()->getQteStock();
if( $qtePreCmd <= 0) $qtePreCmd = 0;
}
return $qtePreCmd;
}
}