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