Fab 4 years ago
parent
commit
a69e7fcd3d
4 changed files with 172 additions and 43 deletions
  1. +2
    -20
      ShopBundle/Controller/Frontend/CartController.php
  2. +3
    -3
      ShopBundle/Repository/ProductFamilyRepository.php
  3. +0
    -20
      ShopBundle/Resources/config/shop_routes.yaml
  4. +167
    -0
      ShopBundle/Services/OrderUtils.php

+ 2
- 20
ShopBundle/Controller/Frontend/CartController.php View File

@@ -3,7 +3,6 @@
namespace Lc\ShopBundle\Controller\Frontend ;

use App\Form\Frontend\OrderProductsType;
use App\Services\OrderUtils;
use Doctrine\ORM\EntityManagerInterface;
use Lc\ShopBundle\Context\MerchantUtilsInterface;
use Lc\ShopBundle\Context\OrderProductInterface;
@@ -17,6 +16,7 @@ class CartController extends BaseController
{
protected $orderUtils ;
protected $productFamilyRepository ;
protected $orderProductRepository ;

protected $productFamily ;
protected $orderProducts = [] ;
@@ -27,6 +27,7 @@ class CartController extends BaseController
parent::__construct($em, $security, $merchantUtils);
$this->orderUtils = $orderUtils ;
$this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ;
$this->orderProductRepository = $this->em->getRepository($this->em->getClassMetaData(OrderProductInterface::class)->getName()) ;
}

public function addProductFamily(Request $request)
@@ -58,23 +59,4 @@ class CartController extends BaseController
return new JsonResponse($return) ;
}

public function addProduct()
{

}

public function editProduct()
{

}

public function deleteProduct()
{

}

public function summary()
{

}
}

+ 3
- 3
ShopBundle/Repository/ProductFamilyRepository.php View File

@@ -28,7 +28,7 @@ class ProductFamilyRepository extends BaseRepository implements DefaultRepositor

public function findNoveltiesByParentCategories()
{
return $this->_productsByParentCategories($this->findNovelties()) ;
return $this->productsByParentCategories($this->findNovelties()) ;
}

public function findOrganics()
@@ -40,10 +40,10 @@ class ProductFamilyRepository extends BaseRepository implements DefaultRepositor

public function findOrganicsByParentCategories()
{
return $this->_productsByParentCategories($this->findOrganics()) ;
return $this->productsByParentCategories($this->findOrganics()) ;
}

public function _productsByParentCategories($products)
public function productsByParentCategories($products)
{
$categoriesArray = [] ;
foreach($products as $product) {

+ 0
- 20
ShopBundle/Resources/config/shop_routes.yaml View File

@@ -7,26 +7,6 @@ lc_api:
path: /api/{entity}/{id}
controller: Lc\ShopBundle\Controller\ApiController::getEntity

lc_frontend_cart_add_product_family:
path: /lc/cart/add/product-family
controller: Lc\ShopBundle\Controller\Frontend\CartController::addProductFamily

lc_frontend_cart_add_product:
path: /lc/cart/add/product
controller: Lc\ShopBundle\Controller\Frontend\CartController::addProduct

lc_frontend_cart_edit_product:
path: /lc/cart/edit/product
controller: Lc\ShopBundle\Controller\Frontend\CartController::editProduct

lc_frontend_cart_delete_product:
path: /lc/cart/delete/product
controller: Lc\ShopBundle\Controller\Frontend\CartController::deleteProduct

lc_frontend_cart_summary:
path: /lc/cart/summary
controller: Lc\ShopBundle\Controller\Frontend\CartController::summary

lc_frontend_favorite_toggle:
path: /lc/favorite/toggle
controller: Lc\ShopBundle\Controller\Frontend\FavoriteController::toggle

+ 167
- 0
ShopBundle/Services/OrderUtils.php View File

@@ -2,7 +2,174 @@

namespace Lc\ShopBundle\Services ;

use App\Entity\OrderShop;
use Doctrine\ORM\EntityManagerInterface;
use Lc\ShopBundle\Context\MerchantUtilsInterface;
use Symfony\Component\Security\Core\Security;

class OrderUtils
{
protected $em;
protected $security;
protected $userUtils;
protected $merchantUtils;
private $orderShopRepo;

public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils, MerchantUtilsInterface $merchantUtils)
{
$this->em = $em;
$this->security = $security;
$this->userUtils = $userUtils;
$this->merchantUtils = $merchantUtils;
$this->orderShopRepo = $this->em->getRepository(OrderShop::class);
}

public function getOrderShopCurrent()
{
$paramsSearchOrderShop = [];

$user = $this->security->getUser();
$visitor = $this->userUtils->getVisitorCurrent();

if ($user) {
$paramsSearchOrderShop['user'] = $user;
}

if ($visitor) {
$paramsSearchOrderShop['visitor'] = $visitor;
}

$orderShop = $this->orderShopRepo->findOneBy($paramsSearchOrderShop);

if (!$orderShop) {
$orderShop = $this->createOrderShop([
'user' => $user,
'visitor' => $visitor,
'merchant' => $this->merchantUtils->getMerchantCurrent()
]);
}

return $orderShop;
}

public function createOrderShop($params)
{
$orderShop = new OrderShop();

$orderShopBelongTo = false;
if (isset($params['user']) && $params['user']) {
$orderShopBelongTo = true;
$orderShop->setUser($params['user']);
}
if (isset($params['visitor']) && $params['visitor']) {
$orderShopBelongTo = true;
$orderShop->setVisitor($params['visitor']);
}

if (!$orderShopBelongTo) {
throw new \ErrorException('La commande doit être liée à un utilisateur ou à un visiteur.');
}

if (isset($params['merchant']) && $params['merchant']) {
$orderShop->setMerchant($params['merchant']);
} else {
throw new \ErrorException('La commande doit être liée à un merchant.');
}

$this->em->persist($orderShop);
$this->em->flush();

return $orderShop;
}

public function addOrderProduct($orderShop, $orderProductAdd)
{
if ($orderProductAdd->getQuantity() > 0) {
$updated = false;

$orderProductAdd->setTitle($orderProductAdd->getTitleOrderShop());
$orderProductAdd->setPrice($orderProductAdd->getProduct()->getPriceInherited());
$orderProductAdd->setUnit($orderProductAdd->getProduct()->getUnitInherited());
$orderProductAdd->setTaxRate($orderProductAdd->getProduct()->getTaxRateInherited());

foreach ($orderShop->getOrderProducts() as $orderProduct) {
if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId()) {
$updated = true;
$orderProduct->setQuantity($orderProduct->getQuantity() + $orderProductAdd->getQuantity());
$this->em->persist($orderProduct);
}
}

if (!$updated) {
$orderShop->addOrderProduct($orderProductAdd);
$this->em->persist($orderProductAdd);
$this->em->persist($orderShop);
}

$this->em->flush();
}
}

public function countQuantities($orderShop)
{
return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
}

public function countQuantitiesByOrderProducts($orderProducts = [])
{
$count = 0;

foreach ($orderProducts as $orderProduct) {
$count += $orderProduct->getQuantity();
}

return $count;
}

public function calculateTotalWithTax($orderShop)
{
return $this->calculateTotalWithTaxByOrderProducts($orderShop->getOrderProducts());
}

public function calculateTotalWithTaxByOrderProducts($orderProducts = [])
{
$totalWithTax = 0;

foreach ($orderProducts as $orderProduct) {
$totalWithTax += $orderProduct->getPriceWithTax() * $orderProduct->getQuantity();
}

return $totalWithTax;
}

public function orderProductsByParentCategory($orderShop = null)
{
$categoriesArray = [];
foreach ($orderShop->getOrderProducts() as $orderProduct) {
$productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
$category = $productCategories[0]->getParentCategory();
$labelCategory = $category->getTitle() ;
if (!isset($categoriesArray[$labelCategory])) {
$categoriesArray[$labelCategory] = [] ;
}
$categoriesArray[$labelCategory][] = $orderProduct;
}

return $categoriesArray;
}

public function getDatasSummary($orderShop = null)
{
if(!$orderShop) {
$orderShop = $this->getOrderShopCurrent() ;
}

$data = [] ;
$data['count'] = $this->countQuantities($orderShop) ;
$data['total_with_tax'] = $this->calculateTotalWithTax($orderShop) ;
$data['categories'] = $this->orderProductsByParentCategory($orderShop) ;

return $data ;
}

}

Loading…
Cancel
Save