return $this; | return $this; | ||||
} | } | ||||
} | |||||
} |
class OrderUtils | class OrderUtils | ||||
{ | { | ||||
use OrderUtilsReductionTrait; | use OrderUtilsReductionTrait; | ||||
use OrderUtilsStockTrait; | |||||
use OrderUtilsPaymentTrait; | |||||
use OrderUtilsDocumentTrait; | |||||
use OrderUtilsCartTrait; | |||||
protected $em; | protected $em; | ||||
protected $security; | protected $security; | ||||
} | } | ||||
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) | public function createOrderShop($params) | ||||
{ | { | ||||
} | } | ||||
} | } | ||||
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) | public function groupOrderProductsByProductFamily($orderProducts) | ||||
{ | { | ||||
return $orderProductsByProductFamily; | 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) ; | |||||
} | |||||
} | } |
<?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; | |||||
} | |||||
} |
<?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; | |||||
} | |||||
} |
<?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; | |||||
} | |||||
} |
} | } | ||||
}*/ | }*/ | ||||
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 ; | |||||
} | |||||
} | } |
<?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) ; | |||||
} | |||||
} |