<?php
namespace App\Service;
use App\Entity\BasketItem;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class BasketService
{
private $session;
private $entityManager;
public function __construct(SessionInterface $session, EntityManagerInterface $entityManager)
{
$this->session = $session;
$this->entityManager = $entityManager;
}
public function getBasket(){
return $this->session->get("basket", []);
}
public function getRefreshedBasket(){
$basket = $this->getBasket();
foreach($basket as $basketItem){
$basketItem->refresh($this->entityManager);
}
$this->setBasket($basket);
return $basket;
}
public function hasItem(): ?bool
{
return count($this->getBasket()) > 0;
}
public function setBasket($basket){
$this->session->set("basket", $basket);
}
public function add(BasketItem $basketItem){
$basket = $this->getBasket();
$index = $this->indexOf($basketItem->getProduct()->getId());
if($index!=-1){
$qty = $basket[$index]->getQuantity() + $basketItem->getQuantity();
$basketItem->setQuantity($qty);
$this->update($basketItem->getProduct()->getId(), $basketItem);
} else{
$this->checkBasketItem($basketItem);
$basket[] = $basketItem;
$this->setBasket($basket);
}
$this->refreshTotalCost();
}
public function refreshTotalCost(){
$totalCost = $this->getTotalCost();
$basket = $this->getBasket();
$totalCostWithTVA = $this->getTotalCostAndTVAWithBasket($basket);
$this->session->set("basketTotalCost", $totalCost);
$this->session->set("totalCostWithTVA", $totalCostWithTVA);
}
public function indexOf($productId){
$basket = $this->getBasket();
$index = -1;
for($i=0; $i<count($basket) ; $i++){
if($basket[$i]->getProduct()->getId()==$productId) {
$index = $i;
break;
}
}
return $index;
}
public function getTotalCost(){
$basket = $this->getBasket();
return $this->getTotalCostWithBasket($basket);
}
public function getTotalCostWithBasket($basket){
$cost = 0;
for($i=0; $i<count($basket) ; $i++){
$cost += $basket[$i]->getCost();
}
return $cost;
}
public function getTotalCostAndTVAWithBasket($basket){
$cost = 0;
$tva = 0;
for($i=0; $i<count($basket) ; $i++){
$cost += $basket[$i]->getCost();
$tva += ProductService::calculateTvaFromTTC($basket[$i]->getCost(), $basket[$i]->getProduct()->getTva());
}
$ht = $cost - $tva;
return [
"cost" => $cost,
"tva" => $tva,
"ht" => $ht,
];
}
public function checkBasketItem(BasketItem $basketItem){
$basketItem->refresh($this->entityManager);
$basketItem->getProduct()->checkQty($basketItem->getQuantity());
return true;
}
public function update($productId, $basketItem){
$this->checkBasketItem($basketItem);
$basket = $this->getBasket();
$index = $this->indexOf($productId);
if($index!=-1) array_splice($basket, $index, 1);
$basket[] = $basketItem;
$this->setBasket($basket);
$this->refreshTotalCost();
}
public function remove($productId){
$basket = $this->getBasket();
$index = $this->indexOf($productId);
if($index!=-1) array_splice($basket, $index, 1);
$this->setBasket($basket);
$this->refreshTotalCost();
}
public function removeBasket(){
$this->session->remove("basket");
$this->refreshTotalCost();
}
}