Bladeren bron

Refactor OrderUtils

feature/export_comptable
Fab 4 jaren geleden
bovenliggende
commit
bd9f92572a
7 gewijzigde bestanden met toevoegingen van 320 en 271 verwijderingen
  1. +1
    -1
      ShopBundle/Model/OrderAmountMin.php
  2. +4
    -270
      ShopBundle/Services/Order/OrderUtils.php
  3. +55
    -0
      ShopBundle/Services/Order/OrderUtilsCartTrait.php
  4. +31
    -0
      ShopBundle/Services/Order/OrderUtilsDocumentTrait.php
  5. +52
    -0
      ShopBundle/Services/Order/OrderUtilsPaymentTrait.php
  6. +23
    -0
      ShopBundle/Services/Order/OrderUtilsReductionTrait.php
  7. +154
    -0
      ShopBundle/Services/Order/OrderUtilsStockTrait.php

+ 1
- 1
ShopBundle/Model/OrderAmountMin.php Bestand weergeven

@@ -23,4 +23,4 @@ trait OrderAmountMin
return $this;
}

}
}

+ 4
- 270
ShopBundle/Services/Order/OrderUtils.php Bestand weergeven

@@ -34,6 +34,10 @@ use Symfony\Component\Security\Core\Security;
class OrderUtils
{
use OrderUtilsReductionTrait;
use OrderUtilsStockTrait;
use OrderUtilsPaymentTrait;
use OrderUtilsDocumentTrait;
use OrderUtilsCartTrait;

protected $em;
protected $security;
@@ -67,48 +71,6 @@ class OrderUtils
}


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

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

$orderShop = null;
$orderShopUser = null;
$orderShopVisitor = null;

if ($user) {
$orderShopUser = $this->orderShopRepo->findCartCurrent([
'user' => $user
]);
}

if ($visitor) {
$orderShopVisitor = $this->orderShopRepo->findCartCurrent([
'visitor' => $visitor
]);
}

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

return $orderShop;
}

public function createOrderShop($params)
{
@@ -348,24 +310,6 @@ class OrderUtils
}
}

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

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

return $document;
}

public function groupOrderProductsByProductFamily($orderProducts)
{
@@ -387,217 +331,7 @@ class OrderUtils
return $orderProductsByProductFamily;
}

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

$orderPayment->setOrderShop($orderShop);
$orderPayment->setMeanPayment($meanPayment);
$orderPayment->setAmount($amount);
$orderPayment->setReference($reference);
$orderPayment->setComment($comment);
$orderPayment->setEditable(false);

if ($paidAt) {
$orderPayment->setPaidAt($paidAt);
} else {
$orderPayment->setPaidAt(new \DateTime('now'));
}

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

return $orderPayment ;
}

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

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

public function deductAvailabilityProduct(\Lc\ShopBundle\Model\OrderShop $orderShop)
{
//TODO ne pas déduire des stocks les orderProduct marqué en relivraison

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()->getAvailableQuantityInherited();
$newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
$orderProduct->getProduct()->getProductFamily()->setAvailableQuantity($newAvailability);

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

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

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

break;
}

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

public function isCartAllowToBeOrder($order){
return true;
}

public function getReductionCreditsAvailableByUser($user)
{
$reductionCredits = $this->reductionCreditRepo->findReductionCreditsByUser($user) ;

$reductionCreditsArray = [] ;
foreach($reductionCredits as $reductionCredit) {
if(!$this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user)) {
$reductionCreditsArray[] = $reductionCredit ;
}
}

return $reductionCreditsArray ;
}

public function isReductionCreditAddedToOrder($orderShop, $reductionCredit)
{
foreach($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
if($orderReductionCredit->getReductionCredit() == $reductionCredit) {
return true ;
}
}

return false ;
}

public function isProductAvailable(Product $product, $quantityOrder = 0, $checkCart = false, $orderShop = null)
{
if(!$orderShop) {
$orderShop = $this->getCartCurrent() ;
}
$productFamily = $product->getProductFamily() ;
$quantityAsked = $quantityOrder;

if($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
if(!$quantityOrder) {
$quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product, true) ;
}
else {
$quantityAsked = ($product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient()) * $quantityOrder;
}

if($checkCart) {
$quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product, true) ;
}
}

if(($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY
|| $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT)) {

if(!$quantityOrder) {
$quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product) ;
}

if($checkCart) {
$quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product) ;
}
}

if ($product->getAvailableQuantityInherited() >= $quantityAsked
|| $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_UNLIMITED) {
return true;
}
else {
return false;
}
}

public function isOneProductAvailableAddCart($products): bool
{
$orderShop = $this->getCartCurrent() ;

foreach($products as $product) {
if($this->isProductAvailable($product, 1, true)) {
return true ;
}
}

return false ;
}

public function isOrderProductAvailable(OrderProductInterface $orderProduct)
{
return $this->isProductAvailable($orderProduct->getProduct(), $orderProduct->getQuantityOrder()) ;
}

public function isOrderProductAvailableAddCart(OrderProductInterface $orderProduct, $orderShop = null)
{
$product = $orderProduct->getProduct() ;
return $this->isProductAvailable($product, $orderProduct->getQuantityOrder(), true, $orderShop);
}

public function getQuantityOrderByProduct($orderShop, $product, $byWeight = false)
{
$quantity = 0 ;
$productFamily = $product->getProductFamily() ;
$behaviorCountStock = $productFamily->getBehaviorCountStock() ;

if($orderShop) {
foreach($orderShop->getOrderProducts() as $orderProduct) {
if($orderProduct->getProduct()->getId() == $product->getId()
|| ( ($behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY || $behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE)
&& $orderProduct->getProduct()->getProductFamily()->getId() == $productFamily->getId())) {

if($byWeight) {
$quantity += $orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct() / $product->getUnitInherited()->getCoefficient()) ;
}
else {
$quantity += $orderProduct->getQuantityOrder() ;
}
}
}
}

return $quantity ;
}

public function getProductQuantityMaxAddCart($product)
{
$orderShop = $this->getCartCurrent() ;
$productFamily = $product->getProductFamily() ;

$byWeight = false ;
if($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
$byWeight = true ;
}

return $product->getAvailableQuantityInherited() - $this->getQuantityOrderByProduct($orderShop, $product, $byWeight) ;
}
}

+ 55
- 0
ShopBundle/Services/Order/OrderUtilsCartTrait.php Bestand weergeven

@@ -0,0 +1,55 @@
<?php

namespace Lc\ShopBundle\Services\Order;


trait OrderUtilsCartTrait
{
public function getCartCurrent()
{
$paramsSearchOrderShop = [];

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

$orderShop = null;
$orderShopUser = null;
$orderShopVisitor = null;

if ($user) {
$orderShopUser = $this->orderShopRepo->findCartCurrent([
'user' => $user
]);
}

if ($visitor) {
$orderShopVisitor = $this->orderShopRepo->findCartCurrent([
'visitor' => $visitor
]);
}

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

return $orderShop;
}


public function isCartAllowToBeOrder($order){
return true;
}
}

+ 31
- 0
ShopBundle/Services/Order/OrderUtilsDocumentTrait.php Bestand weergeven

@@ -0,0 +1,31 @@
<?php

namespace Lc\ShopBundle\Services\Order;


use Lc\ShopBundle\Context\OrderShopInterface;
use Lc\ShopBundle\Model\Document;

trait OrderUtilsDocumentTrait
{

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

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

return $document;
}

}

+ 52
- 0
ShopBundle/Services/Order/OrderUtilsPaymentTrait.php Bestand weergeven

@@ -0,0 +1,52 @@
<?php

namespace Lc\ShopBundle\Services\Order;


use Lc\ShopBundle\Context\OrderPaymentInterface;

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

$orderPayment->setOrderShop($orderShop);
$orderPayment->setMeanPayment($meanPayment);
$orderPayment->setAmount($amount);
$orderPayment->setReference($reference);
$orderPayment->setComment($comment);
$orderPayment->setEditable(false);

if ($paidAt) {
$orderPayment->setPaidAt($paidAt);
} else {
$orderPayment->setPaidAt(new \DateTime('now'));
}

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

return $orderPayment ;
}

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

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

}

+ 23
- 0
ShopBundle/Services/Order/OrderUtilsReductionTrait.php Bestand weergeven

@@ -120,6 +120,29 @@ trait OrderUtilsReductionTrait

}
}*/
public function getReductionCreditsAvailableByUser($user)
{
$reductionCredits = $this->reductionCreditRepo->findReductionCreditsByUser($user) ;

$reductionCreditsArray = [] ;
foreach($reductionCredits as $reductionCredit) {
if(!$this->orderShopRepo->countValidOrderWithReductionCredit($reductionCredit, $user)) {
$reductionCreditsArray[] = $reductionCredit ;
}
}

return $reductionCreditsArray ;
}

public function isReductionCreditAddedToOrder($orderShop, $reductionCredit)
{
foreach($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
if($orderReductionCredit->getReductionCredit() == $reductionCredit) {
return true ;
}
}

return false ;
}

}

+ 154
- 0
ShopBundle/Services/Order/OrderUtilsStockTrait.php Bestand weergeven

@@ -0,0 +1,154 @@
<?php

namespace Lc\ShopBundle\Services\Order;


use Lc\ShopBundle\Context\OrderProductInterface;
use Lc\ShopBundle\Model\Product;
use Lc\ShopBundle\Model\ProductFamily;

trait OrderUtilsStockTrait
{
public function deductAvailabilityProduct(\Lc\ShopBundle\Model\OrderShop $orderShop)
{
//TODO ne pas déduire des stocks les orderProduct marqué en relivraison

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()->getAvailableQuantityInherited();
$newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
$orderProduct->getProduct()->getProductFamily()->setAvailableQuantity($newAvailability);

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

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

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

break;
}

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

public function isProductAvailable(Product $product, $quantityOrder = 0, $checkCart = false, $orderShop = null)
{
if(!$orderShop) {
$orderShop = $this->getCartCurrent() ;
}
$productFamily = $product->getProductFamily() ;
$quantityAsked = $quantityOrder;

if($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
if(!$quantityOrder) {
$quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product, true) ;
}
else {
$quantityAsked = ($product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient()) * $quantityOrder;
}

if($checkCart) {
$quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product, true) ;
}
}

if(($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY
|| $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT)) {

if(!$quantityOrder) {
$quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product) ;
}

if($checkCart) {
$quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product) ;
}
}

if ($product->getAvailableQuantityInherited() >= $quantityAsked
|| $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_UNLIMITED) {
return true;
}
else {
return false;
}
}

public function isOneProductAvailableAddCart($products): bool
{
foreach($products as $product) {
if($this->isProductAvailable($product, 1, true)) {
return true ;
}
}

return false ;
}

public function isOrderProductAvailable(OrderProductInterface $orderProduct)
{
return $this->isProductAvailable($orderProduct->getProduct(), $orderProduct->getQuantityOrder()) ;
}

public function isOrderProductAvailableAddCart(OrderProductInterface $orderProduct, $orderShop = null)
{
$product = $orderProduct->getProduct() ;
return $this->isProductAvailable($product, $orderProduct->getQuantityOrder(), true, $orderShop);
}

public function getQuantityOrderByProduct($orderShop, $product, $byWeight = false)
{
$quantity = 0 ;
$productFamily = $product->getProductFamily() ;
$behaviorCountStock = $productFamily->getBehaviorCountStock() ;

if($orderShop) {
foreach($orderShop->getOrderProducts() as $orderProduct) {
if($orderProduct->getProduct()->getId() == $product->getId()
|| ( ($behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY || $behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE)
&& $orderProduct->getProduct()->getProductFamily()->getId() == $productFamily->getId())) {

if($byWeight) {
$quantity += $orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct() / $product->getUnitInherited()->getCoefficient()) ;
}
else {
$quantity += $orderProduct->getQuantityOrder() ;
}
}
}
}

return $quantity ;
}

public function getProductQuantityMaxAddCart($product)
{
$orderShop = $this->getCartCurrent() ;
$productFamily = $product->getProductFamily() ;

$byWeight = false ;
if($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
$byWeight = true ;
}

return $product->getAvailableQuantityInherited() - $this->getQuantityOrderByProduct($orderShop, $product, $byWeight) ;
}


}

Laden…
Annuleren
Opslaan