|
|
@@ -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; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |