@@ -2,11 +2,13 @@ | |||
namespace Lc\ShopBundle\Controller\Frontend ; | |||
use App\Entity\ReductionCredit; | |||
use App\Form\Frontend\OrderProductsType; | |||
use Doctrine\ORM\EntityManagerInterface; | |||
use Lc\ShopBundle\Context\MerchantUtilsInterface; | |||
use Lc\ShopBundle\Context\OrderProductInterface; | |||
use Lc\ShopBundle\Context\OrderReductionCartInterface; | |||
use Lc\ShopBundle\Context\OrderReductionCreditInterface; | |||
use Lc\ShopBundle\Context\OrderUtilsInterface; | |||
use Lc\ShopBundle\Context\ProductFamilyInterface; | |||
use Lc\ShopBundle\Context\ReductionCartInterface; | |||
@@ -81,6 +83,17 @@ class CartController extends BaseController | |||
return new JsonResponse($return) ; | |||
} | |||
public function redirectToReferer($request) { | |||
$referer = $request->headers->get('referer'); | |||
if($referer) { | |||
return $this->redirect($referer); | |||
} | |||
else { | |||
return $this->redirectToRoute('frontend_order_cart'); | |||
} | |||
} | |||
public function deleteReductionCart(Request $request) | |||
{ | |||
$id = $request->get('id') ; | |||
@@ -100,6 +113,51 @@ class CartController extends BaseController | |||
$this->addFlash('error', 'Une erreur est survenue lors de la suppression de la réduction. ') ; | |||
} | |||
return $this->redirectToReferer($request) ; | |||
} | |||
public function addReductionCredit(Request $request) | |||
{ | |||
$id = $request->get('id') ; | |||
$orderShop = $this->orderUtils->getCartCurrent() ; | |||
$user = $this->security->getUser() ; | |||
$reductionCreditRepository = $this->em->getRepository(ReductionCredit::class) ; | |||
$reductionCredit = $reductionCreditRepository->find($id) ; | |||
if($orderShop && $user && $reductionCredit | |||
&& $this->orderUtils->isReductionCreditAllowAddToOrder($orderShop, $reductionCredit) | |||
&& !$this->orderUtils->isReductionCreditAddedToOrder($orderShop, $reductionCredit)) { | |||
$this->orderUtils->createOrderReductionCredit($orderShop, $reductionCredit) ; | |||
$this->addFlash('success', 'Votre avoir a bien été ajouté à votre panier.') ; | |||
} | |||
else { | |||
$this->addFlash('error', "Impossible d'effectuer cette action"); | |||
} | |||
return $this->redirectToReferer($request) ; | |||
} | |||
public function deleteReductionCredit(Request $request) | |||
{ | |||
$id = $request->get('id') ; | |||
$orderReductionCreditRepository = $this->em->getRepository($this->em->getClassMetadata(OrderReductionCreditInterface::class)->getName()) ; | |||
$orderReductionCredit = $orderReductionCreditRepository->findOneById((int) $id) ; | |||
$orderShop = $this->orderUtils->getCartCurrent() ; | |||
if($orderReductionCredit && $orderShop->getOrderReductionCredits() && $orderShop->getOrderReductionCredits()->contains($orderReductionCredit)) { | |||
$this->em->remove($orderReductionCredit) ; | |||
$this->em->flush() ; | |||
$this->addFlash('success', 'Votre avoir a bien été supprimé de votre panier.') ; | |||
} | |||
else { | |||
$this->addFlash('error', 'Une erreur est survenue lors de la suppression de votre avoir. ') ; | |||
} | |||
$referer = $request->headers->get('referer'); | |||
if($referer) { |
@@ -31,7 +31,7 @@ class OrderShopRepository extends BaseRepository implements DefaultRepositoryInt | |||
$query = $this->filterOrderValid($query); | |||
$query->select('count(e.id)'); | |||
$query->andWhere('e.user = :user'); | |||
$query->join('e.orderReductionCredits', 'orc'); | |||
$query->innerJoin('e.orderReductionCredits', 'orc'); | |||
$query->andWhere('orc.reductionCredit = :reductionCredit'); | |||
$query->setParameter('reductionCredit', $reductionCredit); | |||
$query->setParameter('user', $user); |
@@ -35,6 +35,7 @@ class OrderUtils | |||
protected $merchantUtils; | |||
protected $orderShopRepo; | |||
protected $reductionCreditRepo ; | |||
protected $orderReductionCreditRepo ; | |||
protected $priceUtils; | |||
protected $productFamilyUtils; | |||
protected $documentUtils; | |||
@@ -50,6 +51,7 @@ class OrderUtils | |||
$this->merchantUtils = $merchantUtils; | |||
$this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName()); | |||
$this->reductionCreditRepo = $this->em->getRepository($this->em->getClassMetadata(ReductionCreditInterface::class)->getName()); | |||
$this->orderReductionCreditRepo = $this->em->getRepository($this->em->getClassMetadata(OrderReductionCreditInterface::class)->getName()); | |||
$this->priceUtils = $priceUtils; | |||
$this->productFamilyUtils = $productFamilyUtils; | |||
$this->documentUtils = $documentUtils; | |||
@@ -473,4 +475,14 @@ class OrderUtils | |||
return $reductionCreditsArray ; | |||
} | |||
public function isReductionCreditAddedToOrder($orderShop, $reductionCredit) | |||
{ | |||
foreach($orderShop->getOrderReductionCredits() as $orderReductionCredit) { | |||
if($orderReductionCredit->getReductionCredit() == $reductionCredit) { | |||
return true ; | |||
} | |||
} | |||
return false ; | |||
} | |||
} |
@@ -79,12 +79,19 @@ trait OrderUtilsReductionTrait | |||
public function isReductionCreditAllowAddToOrder($orderShop, $reductionCredit) | |||
{ | |||
$user = $orderShop->getUser() ; | |||
if ($this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $orderShop->getUser()) > 0) { | |||
// appartient à l'utilisateur | |||
if(!$reductionCredit->getUsers()->contains($user)) { | |||
return false ; | |||
} | |||
// n'a pas été utilisé | |||
if ($this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user) > 0) { | |||
return false; | |||
} else { | |||
return true; | |||
} | |||
return true; | |||
} | |||
public function getReductionCartRemainingQuantity($reductionCart) :float | |||
@@ -111,6 +118,9 @@ trait OrderUtilsReductionTrait | |||
$orderReductionCredit->setUnit($reductionCredit->getUnit()); | |||
$orderReductionCredit->setBehaviorTaxRate($reductionCredit->getBehaviorTaxRate()); | |||
$this->em->persist($orderReductionCredit); | |||
$this->em->flush(); | |||
return $orderReductionCredit; | |||
} | |||
@@ -165,10 +165,10 @@ class PriceUtils | |||
{ | |||
if ($withTax) { | |||
$priceToReturn = $this->getTotalOrderProductsWithTaxAndReductionCatalog(); | |||
$priceToReturn = $this->getTotalOrderProductsWithTaxAndReductionCatalog($order); | |||
} | |||
else { | |||
$priceToReturn = $this->getTotalOrderProductsWithReductionCatalog(); | |||
$priceToReturn = $this->getTotalOrderProductsWithReductionCatalog($order); | |||
} | |||
foreach ($order->getOrderReductionCarts() as $orderReductionCart) { | |||
if ($orderReductionCart->getUnit() == 'percent') { | |||
@@ -200,6 +200,16 @@ class PriceUtils | |||
return $this->getSumOrderProductsDispatch($entity, true); | |||
} | |||
public function getTotalOrderProductsWithReduction($entity) | |||
{ | |||
return $this->getTotalOrderProductsWithReductionCatalog($entity); | |||
} | |||
public function getTotalOrderProductsWithTaxAndReduction($entity) | |||
{ | |||
return $this->getTotalOrderProductsWithTaxAndReductionCatalog($entity); | |||
} | |||
public function getTotalOrderProductsWithReductionCatalog($entity) | |||
{ | |||
return $this->getSumOrderProductsDispatch($entity, false, true); |