Переглянути джерело

Processus de commande

feature/export_comptable
Fab 4 роки тому
джерело
коміт
ef7410c953
3 змінених файлів з 113 додано та 100 видалено
  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 Переглянути файл

@@ -137,13 +137,15 @@ class OrderController extends AdminController
} else if ($formAddProductToOrder->isSubmitted() && $formAddProductToOrder->isValid()) {
$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 {
$response['status'] = 'error';
$response['message'] = 'Une erreur est survenue';

+ 28
- 35
ShopBundle/Model/Product.php Переглянути файл

@@ -34,112 +34,105 @@ abstract class Product extends AbstractEntity implements SortableInterface, Prod

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

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

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

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

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

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

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

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

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

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

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)
{
$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()
{
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();
} else if ($this->getProductFamily()->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT) {
return $this->getAvailableQuantity();
}
}


+ 78
- 60
ShopBundle/Services/OrderUtils.php Переглянути файл

@@ -20,6 +20,7 @@ use Lc\ShopBundle\Context\ReductionCreditInterface;
use Lc\ShopBundle\Context\UserInterface;
use Lc\ShopBundle\Model\Document;
use Lc\ShopBundle\Form\Backend\Order\OrderReductionCreditType;
use Lc\ShopBundle\Model\Product;
use Lc\ShopBundle\Model\ProductFamily;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Security;
@@ -35,12 +36,12 @@ class OrderUtils
protected $orderShopRepo;
protected $priceUtils;
protected $productFamilyUtils;
protected $documentUtils ;
protected $session ;
protected $documentUtils;
protected $session;

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


@@ -85,15 +86,14 @@ class OrderUtils

$orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor);
$this->session->getFlashBag()->add('success', "Votre panier visiteur vient d'être fusionné avec votre panier client.");
}
else {
} else {
$orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor;
}
// 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();
}
}

@@ -131,12 +131,12 @@ class OrderUtils

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([
'user' => $user,
'visitor' => $visitor,
@@ -177,7 +177,7 @@ class OrderUtils
}

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

break;
}
@@ -203,15 +203,14 @@ class OrderUtils
$this->em->flush();
}

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

return $return ;
return $return;
}



public function countQuantities($orderShop)
{
return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
@@ -231,7 +230,7 @@ class OrderUtils
public function getOrderProductsByParentCategory($orderShop = null)
{
$categoriesArray = [];
if($orderShop) {
if ($orderShop) {
foreach ($orderShop->getOrderProducts() as $orderProduct) {
$productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
$category = $productCategories[0]->getParentCategory();
@@ -307,9 +306,6 @@ class OrderUtils
}





public function mergeOrderShops($orderShop1, $orderShop2)
{
if ($orderShop1 && $orderShop2) {
@@ -329,8 +325,8 @@ class OrderUtils

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

$document = $this->documentUtils->createDocument([
'type' => Document::TYPE_INVOICE,
@@ -340,80 +336,86 @@ class OrderUtils
'merchant_address' => $merchantAddress,
'buyer_address' => $buyerAddress,
'created_by' => $orderShop->getUser()
]) ;
]);

return $document ;
return $document;
}

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()] = [
'order_products' => [],
'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)
{
$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;
}else{
} else {
return false;
}
}

public function getTotalOrderPayments($orderShop):float
public function getTotalOrderPayments($orderShop): float
{
$totalAmount = floatval(0);
foreach ($orderShop->getOrderPayments() as $orderPayment){
foreach ($orderShop->getOrderPayments() as $orderPayment) {
$totalAmount = $orderPayment->getAmount() + $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()) {
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;
case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :

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

@@ -421,7 +423,7 @@ class OrderUtils

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

@@ -433,4 +435,20 @@ class OrderUtils
$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;
}

}
}

Завантаження…
Відмінити
Зберегти