Browse Source

Processus de commande

feature/export_comptable
Fab 4 years ago
parent
commit
ef7410c953
3 changed files with 113 additions and 100 deletions
  1. +7
    -5
      ShopBundle/Controller/Backend/OrderController.php
  2. +28
    -35
      ShopBundle/Model/Product.php
  3. +78
    -60
      ShopBundle/Services/OrderUtils.php

+ 7
- 5
ShopBundle/Controller/Backend/OrderController.php View File

} else if ($formAddProductToOrder->isSubmitted() && $formAddProductToOrder->isValid()) { } else if ($formAddProductToOrder->isSubmitted() && $formAddProductToOrder->isValid()) {
$orderProduct = new $orderProductClass->name; $orderProduct = new $orderProductClass->name;


$orderProduct->setQuantityOrder($formAddProductToOrder->get('quantity')->getData());
$orderProduct->setProduct($formAddProductToOrder->get('product')->getData());
$this->orderUtils->isProductAvailable($formAddProductToOrder->get('product')->getData(), $formAddProductToOrder->get('quantity')->getData() ){
$orderProduct->setQuantityOrder($formAddProductToOrder->get('quantity')->getData());
$orderProduct->setProduct($formAddProductToOrder->get('product')->getData());


$this->orderUtils->addOrderProduct($orderShop, $orderProduct);
$this->orderUtils->addOrderProduct($orderShop, $orderProduct);


$response['status'] = 'success';
$response['message'] = 'Le produit a bien été ajouté à la commande';
$response['status'] = 'success';
$response['message'] = 'Le produit a bien été ajouté à la commande';
}
} else { } else {
$response['status'] = 'error'; $response['status'] = 'error';
$response['message'] = 'Une erreur est survenue'; $response['message'] = 'Une erreur est survenue';

+ 28
- 35
ShopBundle/Model/Product.php View File



public function __construct() public function __construct()
{ {
$this->orderProducts = new ArrayCollection() ;
$this->orderProducts = new ArrayCollection();
} }


public function getBuyingPriceInherited() public function getBuyingPriceInherited()
{ {
if($this->getBuyingPrice()) {
if ($this->getBuyingPrice()) {
return $this->getBuyingPrice(); return $this->getBuyingPrice();
}
else {
} else {
return $this->getProductFamily()->getBuyingPrice(); return $this->getProductFamily()->getBuyingPrice();
} }
} }


public function getBuyingPriceByRefUnitInherited() public function getBuyingPriceByRefUnitInherited()
{ {
if($this->getBuyingPriceByRefUnit()) {
if ($this->getBuyingPriceByRefUnit()) {
return $this->getBuyingPriceByRefUnit(); return $this->getBuyingPriceByRefUnit();
}
else {
} else {
return $this->getProductFamily()->getBuyingPriceByRefUnit(); return $this->getProductFamily()->getBuyingPriceByRefUnit();
} }
} }


public function getPriceInherited() public function getPriceInherited()
{ {
if($this->getPrice()) {
if ($this->getPrice()) {
return $this->getPrice(); return $this->getPrice();
}
else {
} else {
return $this->getProductFamily()->getPrice(); return $this->getProductFamily()->getPrice();
} }
} }


public function getPriceByRefUnitInherited() public function getPriceByRefUnitInherited()
{ {
if($this->getPriceByRefUnit()) {
if ($this->getPriceByRefUnit()) {
return $this->getPriceByRefUnit(); return $this->getPriceByRefUnit();
}
else {
} else {
return $this->getProductFamily()->getPriceByRefUnit(); return $this->getProductFamily()->getPriceByRefUnit();
} }
} }


public function getBehaviorPriceInherited() public function getBehaviorPriceInherited()
{ {
return $this->getProductFamily()->getBehaviorPrice() ;
return $this->getProductFamily()->getBehaviorPrice();
} }


public function getReductionCatalogInherited() public function getReductionCatalogInherited()
{ {
return $this->getProductFamily()->getReductionCatalog() ;
return $this->getProductFamily()->getReductionCatalog();
} }


public function getUnitInherited() public function getUnitInherited()
{ {
if($this->getUnit()) {
if ($this->getUnit()) {
return $this->getUnit(); return $this->getUnit();
}
else {
} else {
return $this->getProductFamily()->getUnit(); return $this->getProductFamily()->getUnit();
} }
} }


public function getTitleInherited() public function getTitleInherited()
{ {
if($this->getTitle()){
if ($this->getTitle()) {
return $this->getTitle(); return $this->getTitle();
}
else{
} else {
return $this->getProductFamily()->getTitle(); return $this->getProductFamily()->getTitle();
} }
} }


public function getQuantityInherited() public function getQuantityInherited()
{ {
if($this->getQuantity()) {
if ($this->getQuantity()) {
return $this->getQuantity(); return $this->getQuantity();
}
else{
} else {
return $this->getProductFamily()->getQuantity(); return $this->getProductFamily()->getQuantity();
} }
} }


public function getQuantityLabelInherited() public function getQuantityLabelInherited()
{ {
$quantity = $this->getQuantityInherited() ;
$unit = $this->getUnitInherited() ;
return $quantity.$unit->getWordingShort() ;
$quantity = $this->getQuantityInherited();
$unit = $this->getUnitInherited();
return $quantity . $unit->getWordingShort();
} }


public function getQuantityTitle($productFamily) public function getQuantityTitle($productFamily)
{ {
$title = $this->getQuantityLabelInherited() ;
if($productFamily->hasProductsWithVariousWeight()) {
$title .= ', '.$this->getTitleInherited() ;
$title = $this->getQuantityLabelInherited();
if ($productFamily->hasProductsWithVariousWeight()) {
$title .= ', ' . $this->getTitleInherited();
} }
return $title ;
return $title;
} }


public function getAvailableQuantityInherited() public function getAvailableQuantityInherited()
{ {
if($this->getProductFamily()->getBehaviorCountStock()) {
return $this->getAvailableQuantity();
}
else {
if ($this->getProductFamily()->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY ||
$this->getProductFamily()->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
return $this->getProductFamily()->getAvailableQuantity(); return $this->getProductFamily()->getAvailableQuantity();
} else if ($this->getProductFamily()->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT) {
return $this->getAvailableQuantity();
} }
} }



+ 78
- 60
ShopBundle/Services/OrderUtils.php View File

use Lc\ShopBundle\Context\UserInterface; use Lc\ShopBundle\Context\UserInterface;
use Lc\ShopBundle\Model\Document; use Lc\ShopBundle\Model\Document;
use Lc\ShopBundle\Form\Backend\Order\OrderReductionCreditType; use Lc\ShopBundle\Form\Backend\Order\OrderReductionCreditType;
use Lc\ShopBundle\Model\Product;
use Lc\ShopBundle\Model\ProductFamily; use Lc\ShopBundle\Model\ProductFamily;
use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;
protected $orderShopRepo; protected $orderShopRepo;
protected $priceUtils; protected $priceUtils;
protected $productFamilyUtils; protected $productFamilyUtils;
protected $documentUtils ;
protected $session ;
protected $documentUtils;
protected $session;


public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils, public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils,
MerchantUtilsInterface $merchantUtils, PriceUtils $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils, MerchantUtilsInterface $merchantUtils, PriceUtils $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils,
DocumentUtils $documentUtils, SessionInterface $session)
DocumentUtils $documentUtils, SessionInterface $session)
{ {
$this->em = $em; $this->em = $em;
$this->security = $security; $this->security = $security;
$this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName()); $this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName());
$this->priceUtils = $priceUtils; $this->priceUtils = $priceUtils;
$this->productFamilyUtils = $productFamilyUtils; $this->productFamilyUtils = $productFamilyUtils;
$this->documentUtils = $documentUtils ;
$this->session = $session ;
$this->documentUtils = $documentUtils;
$this->session = $session;
} }






$orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor); $orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor);
$this->session->getFlashBag()->add('success', "Votre panier visiteur vient d'être fusionné avec votre panier client."); $this->session->getFlashBag()->add('success', "Votre panier visiteur vient d'être fusionné avec votre panier client.");
}
else {
} else {
$orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor; $orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor;
} }
// set user // set user
if($orderShop && $user && !$orderShop->getUser()) {
$orderShop->setUser($user) ;
$this->em->persist($orderShop) ;
$this->em->flush() ;
if ($orderShop && $user && !$orderShop->getUser()) {
$orderShop->setUser($user);
$this->em->persist($orderShop);
$this->em->flush();
} }
} }




public function addOrderProduct($orderShop, $orderProductAdd, $persist = true) public function addOrderProduct($orderShop, $orderProductAdd, $persist = true)
{ {
$return = false ;
$return = false;


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


if(!$orderShop) {
if (!$orderShop) {
$orderShop = $this->createOrderShop([ $orderShop = $this->createOrderShop([
'user' => $user, 'user' => $user,
'visitor' => $visitor, 'visitor' => $visitor,
} }


$updated = true; $updated = true;
$return = true ;
$return = true;


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


$return = true ;
$return = true;
} }
} }


return $return ;
return $return;
} }





public function countQuantities($orderShop) public function countQuantities($orderShop)
{ {
return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts()); return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
public function getOrderProductsByParentCategory($orderShop = null) public function getOrderProductsByParentCategory($orderShop = null)
{ {
$categoriesArray = []; $categoriesArray = [];
if($orderShop) {
if ($orderShop) {
foreach ($orderShop->getOrderProducts() as $orderProduct) { foreach ($orderShop->getOrderProducts() as $orderProduct) {
$productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories(); $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
$category = $productCategories[0]->getParentCategory(); $category = $productCategories[0]->getParentCategory();
} }







public function mergeOrderShops($orderShop1, $orderShop2) public function mergeOrderShops($orderShop1, $orderShop2)
{ {
if ($orderShop1 && $orderShop2) { if ($orderShop1 && $orderShop2) {


public function createDocumentInvoice(OrderShopInterface $orderShop) public function createDocumentInvoice(OrderShopInterface $orderShop)
{ {
$merchantAddress = $orderShop->getMerchant()->getAddress() ;
$buyerAddress = $orderShop->getInvoiceAddress() ;
$merchantAddress = $orderShop->getMerchant()->getAddress();
$buyerAddress = $orderShop->getInvoiceAddress();


$document = $this->documentUtils->createDocument([ $document = $this->documentUtils->createDocument([
'type' => Document::TYPE_INVOICE, 'type' => Document::TYPE_INVOICE,
'merchant_address' => $merchantAddress, 'merchant_address' => $merchantAddress,
'buyer_address' => $buyerAddress, 'buyer_address' => $buyerAddress,
'created_by' => $orderShop->getUser() 'created_by' => $orderShop->getUser()
]) ;
]);


return $document ;
return $document;
} }


public function groupOrderProductsByProductFamily($orderProducts) public function groupOrderProductsByProductFamily($orderProducts)
{ {
$orderProductsByProductFamily = [] ;
foreach($orderProducts as $orderProduct) {
if($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
$productFamily = $orderProduct->getProduct()->getProductFamily() ;
if(!isset($orderProductsByProductFamily[$productFamily->getId()])) {
$orderProductsByProductFamily = [];
foreach ($orderProducts as $orderProduct) {
if ($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
$productFamily = $orderProduct->getProduct()->getProductFamily();
if (!isset($orderProductsByProductFamily[$productFamily->getId()])) {
$orderProductsByProductFamily[$productFamily->getId()] = [ $orderProductsByProductFamily[$productFamily->getId()] = [
'order_products' => [], 'order_products' => [],
'total_quantity_weight' => 0, 'total_quantity_weight' => 0,
] ;
];
} }
$orderProductsByProductFamily[$productFamily->getId()]['order_products'][] = $orderProduct ;
$orderProductsByProductFamily[$productFamily->getId()]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()) * $orderProduct->getQuantityOrder() ;
$orderProductsByProductFamily[$productFamily->getId()]['order_products'][] = $orderProduct;
$orderProductsByProductFamily[$productFamily->getId()]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()) * $orderProduct->getQuantityOrder();
} }
} }


return $orderProductsByProductFamily ;
return $orderProductsByProductFamily;
} }


public function createOrderPayment($orderShop, $type, $amount, $reference = null, $comment = null, $paidAt = null) public function createOrderPayment($orderShop, $type, $amount, $reference = null, $comment = null, $paidAt = null)
{ {
$classOrderPayment = $this->em->getClassMetadata(OrderPaymentInterface::class)->getName() ;
$orderPayment = new $classOrderPayment ;

$orderPayment->setOrderShop($orderShop) ;
$orderPayment->setType($type) ;
$orderPayment->setAmount($amount) ;
$orderPayment->setReference($reference) ;
$orderPayment->setComment($comment) ;
if($paidAt) {
$orderPayment->setPaidAt($paidAt) ;
}
else {
$orderPayment->setPaidAt(new \DateTime('now')) ;
$classOrderPayment = $this->em->getClassMetadata(OrderPaymentInterface::class)->getName();
$orderPayment = new $classOrderPayment;

$orderPayment->setOrderShop($orderShop);
$orderPayment->setType($type);
$orderPayment->setAmount($amount);
$orderPayment->setReference($reference);
$orderPayment->setComment($comment);
if ($paidAt) {
$orderPayment->setPaidAt($paidAt);
} else {
$orderPayment->setPaidAt(new \DateTime('now'));
} }


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


public function isOrderPaid($orderShop){
if($this->getTotalOrderPayments($orderShop) >= $this->priceUtils->getTotalWithTaxAndReduction($orderShop)){
public function isOrderPaid($orderShop)
{
if ($this->getTotalOrderPayments($orderShop) >= $this->priceUtils->getTotalWithTaxAndReduction($orderShop)) {
return true; return true;
}else{
} else {
return false; return false;
} }
} }


public function getTotalOrderPayments($orderShop):float
public function getTotalOrderPayments($orderShop): float
{ {
$totalAmount = floatval(0); $totalAmount = floatval(0);
foreach ($orderShop->getOrderPayments() as $orderPayment){
foreach ($orderShop->getOrderPayments() as $orderPayment) {
$totalAmount = $orderPayment->getAmount() + $totalAmount; $totalAmount = $orderPayment->getAmount() + $totalAmount;
} }
return $totalAmount; return $totalAmount;
} }


public function deductAvailabilityProduct(\Lc\ShopBundle\Model\OrderShop $orderShop){
foreach ($orderShop->getOrderProducts() as $orderProduct){
public function deductAvailabilityProduct(\Lc\ShopBundle\Model\OrderShop $orderShop)
{
foreach ($orderShop->getOrderProducts() as $orderProduct) {
switch ($orderProduct->getProduct()->getProductFamily()->getBehaviorCountStock()) { switch ($orderProduct->getProduct()->getProductFamily()->getBehaviorCountStock()) {
case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE : case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE :


//Disponibilité par unité de référence
$oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
$newAvailability = $oldAvailability - ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient());
$orderProduct->getProduct()->getProductFamily()->setAvailableQuantity($newAvailability);


$this->em->persist($orderProduct->getProduct()->getProductFamily());


break; break;
case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY : case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :


$oldAvailability = $orderProduct->getProduct()->getProductFamily()->getAvailableQuantity();
$oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
$newAvailability = $oldAvailability - $orderProduct->getQuantityOrder(); $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
$orderProduct->getProduct()->getProductFamily()->setAvailableQuantity($newAvailability); $orderProduct->getProduct()->getProductFamily()->setAvailableQuantity($newAvailability);




break; break;
case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT : case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
$oldAvailability = $orderProduct->getProduct()->getAvailableQuantity();
$oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
$newAvailability = $oldAvailability - $orderProduct->getQuantityOrder(); $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
$orderProduct->getProduct()->setAvailableQuantity($newAvailability); $orderProduct->getProduct()->setAvailableQuantity($newAvailability);


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

public function isProductAvailable(Product $product, $quanityOrder)
{
$quanityAsked = $quanityOrder;

if($product->getProductFamily()->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
$quanityAsked = ($product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient()) * $quanityOrder;
}

if ($product->getAvailableQuantityInherited() >= $quanityAsked) {
return true;
} else {
return false;
}

}
} }

Loading…
Cancel
Save