namespace Lc\ShopBundle\Controller\Frontend ; | namespace Lc\ShopBundle\Controller\Frontend ; | ||||
use App\Entity\ReductionCredit; | |||||
use App\Form\Frontend\OrderProductsType; | use App\Form\Frontend\OrderProductsType; | ||||
use Doctrine\ORM\EntityManagerInterface; | use Doctrine\ORM\EntityManagerInterface; | ||||
use Lc\ShopBundle\Context\MerchantUtilsInterface; | use Lc\ShopBundle\Context\MerchantUtilsInterface; | ||||
use Lc\ShopBundle\Context\OrderProductInterface; | use Lc\ShopBundle\Context\OrderProductInterface; | ||||
use Lc\ShopBundle\Context\OrderReductionCartInterface; | use Lc\ShopBundle\Context\OrderReductionCartInterface; | ||||
use Lc\ShopBundle\Context\OrderReductionCreditInterface; | |||||
use Lc\ShopBundle\Context\OrderUtilsInterface; | use Lc\ShopBundle\Context\OrderUtilsInterface; | ||||
use Lc\ShopBundle\Context\ProductFamilyInterface; | use Lc\ShopBundle\Context\ProductFamilyInterface; | ||||
use Lc\ShopBundle\Context\ReductionCartInterface; | use Lc\ShopBundle\Context\ReductionCartInterface; | ||||
return new JsonResponse($return) ; | 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) | public function deleteReductionCart(Request $request) | ||||
{ | { | ||||
$id = $request->get('id') ; | $id = $request->get('id') ; | ||||
$this->addFlash('error', 'Une erreur est survenue lors de la suppression de la réduction. ') ; | $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'); | $referer = $request->headers->get('referer'); | ||||
if($referer) { | if($referer) { |
$query = $this->filterOrderValid($query); | $query = $this->filterOrderValid($query); | ||||
$query->select('count(e.id)'); | $query->select('count(e.id)'); | ||||
$query->andWhere('e.user = :user'); | $query->andWhere('e.user = :user'); | ||||
$query->join('e.orderReductionCredits', 'orc'); | |||||
$query->innerJoin('e.orderReductionCredits', 'orc'); | |||||
$query->andWhere('orc.reductionCredit = :reductionCredit'); | $query->andWhere('orc.reductionCredit = :reductionCredit'); | ||||
$query->setParameter('reductionCredit', $reductionCredit); | $query->setParameter('reductionCredit', $reductionCredit); | ||||
$query->setParameter('user', $user); | $query->setParameter('user', $user); |
protected $merchantUtils; | protected $merchantUtils; | ||||
protected $orderShopRepo; | protected $orderShopRepo; | ||||
protected $reductionCreditRepo ; | protected $reductionCreditRepo ; | ||||
protected $orderReductionCreditRepo ; | |||||
protected $priceUtils; | protected $priceUtils; | ||||
protected $productFamilyUtils; | protected $productFamilyUtils; | ||||
protected $documentUtils; | protected $documentUtils; | ||||
$this->merchantUtils = $merchantUtils; | $this->merchantUtils = $merchantUtils; | ||||
$this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName()); | $this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName()); | ||||
$this->reductionCreditRepo = $this->em->getRepository($this->em->getClassMetadata(ReductionCreditInterface::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->priceUtils = $priceUtils; | ||||
$this->productFamilyUtils = $productFamilyUtils; | $this->productFamilyUtils = $productFamilyUtils; | ||||
$this->documentUtils = $documentUtils; | $this->documentUtils = $documentUtils; | ||||
return $reductionCreditsArray ; | return $reductionCreditsArray ; | ||||
} | } | ||||
public function isReductionCreditAddedToOrder($orderShop, $reductionCredit) | |||||
{ | |||||
foreach($orderShop->getOrderReductionCredits() as $orderReductionCredit) { | |||||
if($orderReductionCredit->getReductionCredit() == $reductionCredit) { | |||||
return true ; | |||||
} | |||||
} | |||||
return false ; | |||||
} | |||||
} | } |
public function isReductionCreditAllowAddToOrder($orderShop, $reductionCredit) | 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; | return false; | ||||
} else { | |||||
return true; | |||||
} | } | ||||
return true; | |||||
} | } | ||||
public function getReductionCartRemainingQuantity($reductionCart) :float | public function getReductionCartRemainingQuantity($reductionCart) :float | ||||
$orderReductionCredit->setUnit($reductionCredit->getUnit()); | $orderReductionCredit->setUnit($reductionCredit->getUnit()); | ||||
$orderReductionCredit->setBehaviorTaxRate($reductionCredit->getBehaviorTaxRate()); | $orderReductionCredit->setBehaviorTaxRate($reductionCredit->getBehaviorTaxRate()); | ||||
$this->em->persist($orderReductionCredit); | |||||
$this->em->flush(); | |||||
return $orderReductionCredit; | return $orderReductionCredit; | ||||
} | } | ||||
{ | { | ||||
if ($withTax) { | if ($withTax) { | ||||
$priceToReturn = $this->getTotalOrderProductsWithTaxAndReductionCatalog(); | |||||
$priceToReturn = $this->getTotalOrderProductsWithTaxAndReductionCatalog($order); | |||||
} | } | ||||
else { | else { | ||||
$priceToReturn = $this->getTotalOrderProductsWithReductionCatalog(); | |||||
$priceToReturn = $this->getTotalOrderProductsWithReductionCatalog($order); | |||||
} | } | ||||
foreach ($order->getOrderReductionCarts() as $orderReductionCart) { | foreach ($order->getOrderReductionCarts() as $orderReductionCart) { | ||||
if ($orderReductionCart->getUnit() == 'percent') { | if ($orderReductionCart->getUnit() == 'percent') { | ||||
return $this->getSumOrderProductsDispatch($entity, true); | 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) | public function getTotalOrderProductsWithReductionCatalog($entity) | ||||
{ | { | ||||
return $this->getSumOrderProductsDispatch($entity, false, true); | return $this->getSumOrderProductsDispatch($entity, false, true); |