Browse Source

Merge branch 'develop'

master
Guillaume 3 years ago
parent
commit
becbf17c60
2 changed files with 172 additions and 171 deletions
  1. +119
    -119
      Builder/Order/OrderShopBuilder.php
  2. +53
    -52
      Solver/Order/OrderShopSolver.php

+ 119
- 119
Builder/Order/OrderShopBuilder.php View File

use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Lc\CaracoleBundle\Builder\Credit\CreditHistoryBuilder; use Lc\CaracoleBundle\Builder\Credit\CreditHistoryBuilder;
use Lc\CaracoleBundle\Builder\File\DocumentBuilder; use Lc\CaracoleBundle\Builder\File\DocumentBuilder;
use Lc\CaracoleBundle\Event\Order\CartChangeEvent;
use Lc\CaracoleBundle\Event\Order\OrderShopChangeStatusEvent; use Lc\CaracoleBundle\Event\Order\OrderShopChangeStatusEvent;
use Lc\CaracoleBundle\Factory\Credit\CreditHistoryFactory; use Lc\CaracoleBundle\Factory\Credit\CreditHistoryFactory;
use Lc\CaracoleBundle\Factory\File\DocumentFactory; use Lc\CaracoleBundle\Factory\File\DocumentFactory;
protected FlashBagTranslator $flashBagTranslator; protected FlashBagTranslator $flashBagTranslator;


public function __construct( public function __construct(
EntityManagerInterface $entityManager,
OrderShopStore $orderShopStore,
OrderShopSolver $orderShopSolver,
OrderStatusStore $orderStatusStore,
OrderProductStore $orderProductStore,
ProductFamilyStore $productFamilyStore,
OrderProductBuilder $orderProductBuilder,
DocumentBuilder $documentBuilder,
PriceSolver $priceSolver,
EventDispatcherInterface $eventDispatcher,
FlashBagInterface $flashBag,
OpeningResolver $openingResolver,
ProductSolver $productSolver,
OrderShopResolver $orderShopResolver,
OrderProductReductionCatalogSolver $orderProductReductionCatalogSolver,
DistributionBuilder $distributionBuilder,
MerchantResolver $merchantResolver,
CreditHistoryBuilder $creditHistoryBuilder,
FlashBagTranslator $flashBagTranslator
)
{
EntityManagerInterface $entityManager,
OrderShopStore $orderShopStore,
OrderShopSolver $orderShopSolver,
OrderStatusStore $orderStatusStore,
OrderProductStore $orderProductStore,
ProductFamilyStore $productFamilyStore,
OrderProductBuilder $orderProductBuilder,
DocumentBuilder $documentBuilder,
PriceSolver $priceSolver,
EventDispatcherInterface $eventDispatcher,
FlashBagInterface $flashBag,
OpeningResolver $openingResolver,
ProductSolver $productSolver,
OrderShopResolver $orderShopResolver,
OrderProductReductionCatalogSolver $orderProductReductionCatalogSolver,
DistributionBuilder $distributionBuilder,
MerchantResolver $merchantResolver,
CreditHistoryBuilder $creditHistoryBuilder,
FlashBagTranslator $flashBagTranslator
) {
$this->entityManager = $entityManager; $this->entityManager = $entityManager;
$this->orderShopStore = $orderShopStore; $this->orderShopStore = $orderShopStore;
$this->orderShopSolver = $orderShopSolver; $this->orderShopSolver = $orderShopSolver;
} }


public function create( public function create(
SectionInterface $section,
UserInterface $user = null,
VisitorInterface $visitor = null
): OrderShopInterface
{
SectionInterface $section,
UserInterface $user = null,
VisitorInterface $visitor = null
): OrderShopInterface {
$orderShopFactory = new OrderShopFactory(); $orderShopFactory = new OrderShopFactory();
$orderShop = $orderShopFactory->create($section, $user, $visitor); $orderShop = $orderShopFactory->create($section, $user, $visitor);


protected array $cacheCartCurrentBySection = []; protected array $cacheCartCurrentBySection = [];


public function createIfNotExist( public function createIfNotExist(
SectionInterface $section,
UserInterface $user = null,
VisitorInterface $visitor = null,
bool $cache = false
): OrderShopInterface
{

SectionInterface $section,
UserInterface $user = null,
VisitorInterface $visitor = null,
bool $cache = false
): OrderShopInterface {
$cart = null; $cart = null;


// cache // cache
$cacheIdCartCurrent = 'cart_current_' . $section->getId(); $cacheIdCartCurrent = 'cart_current_' . $section->getId();
if ($cache if ($cache
&& isset($this->cacheCartCurrentBySection[$cacheIdCartCurrent])
&& $this->cacheCartCurrentBySection[$cacheIdCartCurrent]) {

&& isset($this->cacheCartCurrentBySection[$cacheIdCartCurrent])
&& $this->cacheCartCurrentBySection[$cacheIdCartCurrent]) {
return $this->cacheCartCurrentBySection[$cacheIdCartCurrent]; return $this->cacheCartCurrentBySection[$cacheIdCartCurrent];
} }


if ($cartUser) { if ($cartUser) {
$cart = $cartUser; $cart = $cartUser;
} elseif ($cartVisitor) { } elseif ($cartVisitor) {

if ($user && $cartVisitor && !$cartVisitor->getUser()) { if ($user && $cartVisitor && !$cartVisitor->getUser()) {
$cartVisitor->setUser($user); $cartVisitor->setUser($user);
$this->entityManager->update($cartVisitor); $this->entityManager->update($cartVisitor);
} }


public function setOrderStatus( public function setOrderStatus(
OrderShopInterface $orderShop,
string $alias,
bool $forceByAdmin = false
): OrderShopInterface
{
OrderShopInterface $orderShop,
string $alias,
bool $forceByAdmin = false
): OrderShopInterface {
$orderStatus = $this->orderStatusStore->getOneByAlias($alias); $orderStatus = $this->orderStatusStore->getOneByAlias($alias);


if ($orderStatus) { if ($orderStatus) {
if ($orderShop->getOrderStatus() === null if ($orderShop->getOrderStatus() === null
|| $orderShop->getOrderStatus()->getNextStatusAllowed()->contains($orderStatus)) {
|| $orderShop->getOrderStatus()->getNextStatusAllowed()->contains($orderStatus)) {
$this->applyChangeOrderStatus($orderShop, $orderStatus, $forceByAdmin); $this->applyChangeOrderStatus($orderShop, $orderStatus, $forceByAdmin);
} }
} else { } else {
} }


public function applyChangeOrderStatus( public function applyChangeOrderStatus(
OrderShopInterface $orderShop,
OrderStatusInterface $orderStatus,
bool $forceByAdmin = false
): void
{
OrderShopInterface $orderShop,
OrderStatusInterface $orderStatus,
bool $forceByAdmin = false
): void {
$this->eventDispatcher->dispatch( $this->eventDispatcher->dispatch(
new OrderShopChangeStatusEvent($orderShop, $orderStatus, $forceByAdmin),
OrderShopChangeStatusEvent::PRE_CHANGE_STATUS
new OrderShopChangeStatusEvent($orderShop, $orderStatus, $forceByAdmin),
OrderShopChangeStatusEvent::PRE_CHANGE_STATUS
); );


$orderShop->setOrderStatusProtected($orderStatus); $orderShop->setOrderStatusProtected($orderStatus);
$orderShop->addOrderStatusHistory($orderStatusHistory); $orderShop->addOrderStatusHistory($orderStatusHistory);


$this->eventDispatcher->dispatch( $this->eventDispatcher->dispatch(
new OrderShopChangeStatusEvent($orderShop, $orderStatus, $forceByAdmin),
OrderShopChangeStatusEvent::POST_CHANGE_STATUS
new OrderShopChangeStatusEvent($orderShop, $orderStatus, $forceByAdmin),
OrderShopChangeStatusEvent::POST_CHANGE_STATUS
); );
} }


public function addOrderProduct( public function addOrderProduct(
OrderShopInterface $orderShop,
OrderProductInterface $orderProductAdd,
bool $persist = true
): bool
{
OrderShopInterface $orderShop,
OrderProductInterface $orderProductAdd,
bool $persist = true
): bool {
$return = false; $return = false;


if ($this->orderShopSolver->isOrderProductAvailableAddCart($orderProductAdd, $orderShop)) { if ($this->orderShopSolver->isOrderProductAvailableAddCart($orderProductAdd, $orderShop)) {
$updated = false; $updated = false;
$this->orderProductBuilder->init($orderProductAdd); $this->orderProductBuilder->init($orderProductAdd);
$productFamily = $this->productFamilyStore->setSection($orderShop->getSection())->getOneBySlug( $productFamily = $this->productFamilyStore->setSection($orderShop->getSection())->getOneBySlug(
$orderProductAdd->getProduct()->getProductFamily()->getSlug()
$orderProductAdd->getProduct()->getProductFamily()->getSlug()
); );


if ($productFamily) { if ($productFamily) {
if ($reductionCatalog && $reductionCatalog->getStatus()) { if ($reductionCatalog && $reductionCatalog->getStatus()) {
$orderProductReductionCatalogFactory = new OrderProductReductionCatalogFactory(); $orderProductReductionCatalogFactory = new OrderProductReductionCatalogFactory();
$orderProductReductionCatalog = $orderProductReductionCatalogFactory->create( $orderProductReductionCatalog = $orderProductReductionCatalogFactory->create(
$reductionCatalog->getTitle(),
$reductionCatalog->getValue(),
$reductionCatalog->getUnit(),
$reductionCatalog->getBehaviorTaxRate()
$reductionCatalog->getTitle(),
$reductionCatalog->getValue(),
$reductionCatalog->getUnit(),
$reductionCatalog->getBehaviorTaxRate()
); );


$orderProductAdd->setOrderProductReductionCatalog($orderProductReductionCatalog); $orderProductAdd->setOrderProductReductionCatalog($orderProductReductionCatalog);


foreach ($orderShop->getOrderProducts() as $orderProduct) { foreach ($orderShop->getOrderProducts() as $orderProduct) {
if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId() if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId()
&& $orderProduct->getRedelivery() == $orderProductAdd->getRedelivery()
&& (string)$this->priceSolver->getPrice($orderProduct)
== (string)$this->priceSolver->getPrice($orderProductAdd)
&& $this->orderProductReductionCatalogSolver->compare(
$orderProduct->getOrderProductReductionCatalog(),
$orderProductAdd->getOrderProductReductionCatalog()
)) {
&& $orderProduct->getRedelivery() == $orderProductAdd->getRedelivery()
&& (string)$this->priceSolver->getPrice($orderProduct)
== (string)$this->priceSolver->getPrice($orderProductAdd)
&& $this->orderProductReductionCatalogSolver->compare(
$orderProduct->getOrderProductReductionCatalog(),
$orderProductAdd->getOrderProductReductionCatalog()
)) {
$orderProduct->setQuantityOrder( $orderProduct->setQuantityOrder(
$orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder()
$orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder()
); );


if ($persist) { if ($persist) {
if (isset($orderProductReductionCatalog)) { if (isset($orderProductReductionCatalog)) {
$this->entityManager->create($orderProductReductionCatalog); $this->entityManager->create($orderProductReductionCatalog);
} }
//TODO est-ce un update ou un create ???
// @TODO : update ou create ?
$this->entityManager->persist($orderProductAdd); $this->entityManager->persist($orderProductAdd);
$this->entityManager->update($orderShop); $this->entityManager->update($orderShop);
} }


if ($persist) { if ($persist) {
$this->entityManager->flush(); $this->entityManager->flush();
$this->eventDispatcher->dispatch(new CartChangeEvent($orderShop), CartChangeEvent::POST_UPDATE);
} }

// @TODO : dispatch event cart change
//$this->eventCartChange($orderShop);
} }
} else { } else {
// @TODO : retourner le message d'erreur et faire le addFlash dans le contrôleur // @TODO : retourner le message d'erreur et faire le addFlash dans le contrôleur
} }
} }
$this->utils->addFlash('error', $textError);*/ $this->utils->addFlash('error', $textError);*/

$return = false;
} }


return $return; return $return;
} }


public function merge( public function merge(
OrderShopInterface $orderShop1,
OrderShopInterface $orderShop2,
$persist = true
): OrderShopInterface
{
OrderShopInterface $orderShop1,
OrderShopInterface $orderShop2,
$persist = true
): OrderShopInterface {
//TODO essayer de comprendre prk on doit faire un refresh ici ??? //TODO essayer de comprendre prk on doit faire un refresh ici ???
$this->entityManager->refresh($orderShop1); $this->entityManager->refresh($orderShop1);
$this->entityManager->refresh($orderShop2); $this->entityManager->refresh($orderShop2);
if ($orderShop1 && $orderShop2) { if ($orderShop1 && $orderShop2) {
foreach ($orderShop2->getOrderProducts() as $orderProduct) { foreach ($orderShop2->getOrderProducts() as $orderProduct) {
$orderProductAlreadyInCart = $this->orderShopSolver->hasOrderProductAlreadyInCart($orderShop1, $orderProduct);
$orderProductAlreadyInCart = $this->orderShopSolver->hasOrderProductAlreadyInCart(
$orderShop1,
$orderProduct
);


if ($orderProductAlreadyInCart) { if ($orderProductAlreadyInCart) {
if ($orderProduct->getQuantityOrder() > $orderProductAlreadyInCart->getQuantityOrder()) { if ($orderProduct->getQuantityOrder() > $orderProductAlreadyInCart->getQuantityOrder()) {
$orderShop->addOrderPayment($orderPayment); $orderShop->addOrderPayment($orderPayment);


if ($meanPayment == OrderPaymentModel::MEAN_PAYMENT_CREDIT) { if ($meanPayment == OrderPaymentModel::MEAN_PAYMENT_CREDIT) {
$this->creditHistoryBuilder->create(CreditHistoryModel::TYPE_DEBIT, $this->merchantResolver->getUserMerchant(), [
'orderPayment' => $orderPayment
]);
$this->creditHistoryBuilder->create(
CreditHistoryModel::TYPE_DEBIT,
$this->merchantResolver->getUserMerchant(),
[
'orderPayment' => $orderPayment
]
);
} }


if ($this->orderShopResolver->isPaid($orderShop)) { if ($this->orderShopResolver->isPaid($orderShop)) {
$orderShop->setStatTotal($this->priceSolver->getTotal($orderShop)); $orderShop->setStatTotal($this->priceSolver->getTotal($orderShop));
$orderShop->setStatTotalWithTax($this->priceSolver->getTotalWithTax($orderShop)); $orderShop->setStatTotalWithTax($this->priceSolver->getTotalWithTax($orderShop));
$orderShop->setStatTotalOrderProductsWithReductions( $orderShop->setStatTotalOrderProductsWithReductions(
$this->priceSolver->getTotalOrderProductsWithReductions($orderShop)
$this->priceSolver->getTotalOrderProductsWithReductions($orderShop)
); );
$orderShop->setStatTotalOrderProductsWithTaxAndReductions( $orderShop->setStatTotalOrderProductsWithTaxAndReductions(
$this->priceSolver->getTotalOrderProductsWithTaxAndReductions($orderShop)
$this->priceSolver->getTotalOrderProductsWithTaxAndReductions($orderShop)
); );
$orderShop->setStatMarginOrderProductsWithReductions( $orderShop->setStatMarginOrderProductsWithReductions(
$this->priceSolver->getMarginOrderProductsWithReductions($orderShop)
$this->priceSolver->getMarginOrderProductsWithReductions($orderShop)
); );
$orderShop->setStatDeliveryPriceWithReduction($this->priceSolver->getDeliveryPriceWithReduction($orderShop)); $orderShop->setStatDeliveryPriceWithReduction($this->priceSolver->getDeliveryPriceWithReduction($orderShop));
$orderShop->setStatDeliveryPriceWithTaxAndReduction( $orderShop->setStatDeliveryPriceWithTaxAndReduction(
$this->priceSolver->getDeliveryPriceWithTaxAndReduction($orderShop)
$this->priceSolver->getDeliveryPriceWithTaxAndReduction($orderShop)
); );


$this->entityManager->persist($orderShop); $this->entityManager->persist($orderShop);
public function initDistribution(OrderShopInterface $orderShop): void public function initDistribution(OrderShopInterface $orderShop): void
{ {
$distribution = $this->distributionBuilder->guessDistributionByDeliveryDate( $distribution = $this->distributionBuilder->guessDistributionByDeliveryDate(
$orderShop->getDeliveryDate(),
$orderShop->getSection()
$orderShop->getDeliveryDate(),
$orderShop->getSection()
); );
$orderShop->setDistribution($distribution); $orderShop->setDistribution($distribution);
} }
} }


public function addReductionCart( public function addReductionCart(
OrderShopInterface $orderShop,
ReductionCartInterface $reductionCart
): ?OrderReductionCartInterface
{
OrderShopInterface $orderShop,
ReductionCartInterface $reductionCart
): ?OrderReductionCartInterface {
$orderReductionCartFactory = new OrderReductionCartFactory(); $orderReductionCartFactory = new OrderReductionCartFactory();
$orderReductionCart = $orderReductionCartFactory->create($orderShop, $reductionCart); $orderReductionCart = $orderReductionCartFactory->create($orderShop, $reductionCart);


$orderShop->addOrderReductionCart($orderReductionCart); $orderShop->addOrderReductionCart($orderReductionCart);


if ($this->orderShopResolver->isPositiveAmount($orderShop) if ($this->orderShopResolver->isPositiveAmount($orderShop)
&& $this->orderShopResolver->isPositiveAmountRemainingToBePaid($orderShop)) {
&& $this->orderShopResolver->isPositiveAmountRemainingToBePaid($orderShop)) {
$this->entityManager->create($orderReductionCart); $this->entityManager->create($orderReductionCart);
$this->entityManager->flush(); $this->entityManager->flush();




// createOrderReductionCredit // createOrderReductionCredit
public function addReductionCredit( public function addReductionCredit(
OrderShopInterface $orderShop,
ReductionCreditInterface $reductionCredit
): ?OrderReductionCreditInterface
{
OrderShopInterface $orderShop,
ReductionCreditInterface $reductionCredit
): ?OrderReductionCreditInterface {
$orderReductionCreditFactory = new OrderReductionCreditFactory(); $orderReductionCreditFactory = new OrderReductionCreditFactory();
$orderReductionCredit = $orderReductionCreditFactory->create($orderShop, $reductionCredit); $orderReductionCredit = $orderReductionCreditFactory->create($orderShop, $reductionCredit);


$orderShop->addOrderReductionCredit($orderReductionCredit); $orderShop->addOrderReductionCredit($orderReductionCredit);


if ($this->orderShopResolver->isPositiveAmount($orderShop) if ($this->orderShopResolver->isPositiveAmount($orderShop)
&& $this->orderShopResolver->isPositiveAmountRemainingToBePaid($orderShop)) {

&& $this->orderShopResolver->isPositiveAmountRemainingToBePaid($orderShop)) {
$this->entityManager->create($orderReductionCredit); $this->entityManager->create($orderReductionCredit);
$this->entityManager->flush(); $this->entityManager->flush();


} }


public function applyDeductAvailabilityProduct( public function applyDeductAvailabilityProduct(
OrderShopInterface $orderShop,
OrderProductInterface $orderProduct
): void
{
OrderShopInterface $orderShop,
OrderProductInterface $orderProduct
): void {
switch ($orderProduct->getProduct()->getProductFamily()->getBehaviorCountStock()) { switch ($orderProduct->getProduct()->getProductFamily()->getBehaviorCountStock()) {
case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE : case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE :


//Disponibilité par unité de référence //Disponibilité par unité de référence
$oldAvailability = $this->productSolver->getAvailableQuantityInherited($orderProduct->getProduct()); $oldAvailability = $this->productSolver->getAvailableQuantityInherited($orderProduct->getProduct());
$newAvailability = $oldAvailability - ($orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()));
$newAvailability = $oldAvailability - ($orderProduct->getQuantityOrder(
) * ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()));


$productFamily = $orderProduct->getProduct()->getProductFamily(); $productFamily = $orderProduct->getProduct()->getProductFamily();
$productFamily->setAvailableQuantity($newAvailability); $productFamily->setAvailableQuantity($newAvailability);


public function updatePriceByProductFamily(ProductFamilyInterface $productFamily) public function updatePriceByProductFamily(ProductFamilyInterface $productFamily)
{ {
$debug = '';

foreach ($this->merchantResolver->getCurrent()->getSections() as $section) { foreach ($this->merchantResolver->getCurrent()->getSections() as $section) {
// @TODO : faire la vérification isOpenSale depuis la méthode appelante // @TODO : faire la vérification isOpenSale depuis la méthode appelante
if (!$this->openingResolver->isOpenSale($section)) { if (!$this->openingResolver->isOpenSale($section)) {
$countOrderProductUpdated = 0; $countOrderProductUpdated = 0;


foreach ($productFamily->getProducts() as $product) { foreach ($productFamily->getProducts() as $product) {
$orderProducts = $this->orderProductStore->resetContext()->setSection($section)->getInCartsByProduct($product);
$orderProducts = $this->orderProductStore->resetContext()->setSection(
$section
)->getInCartsByProduct($product);


foreach ($orderProducts as $orderProduct) { foreach ($orderProducts as $orderProduct) {
$quantityOrder = $orderProduct->getQuantityOrder(); $quantityOrder = $orderProduct->getQuantityOrder();
$orderShop = $orderProduct->getOrderShop(); $orderShop = $orderProduct->getOrderShop();

$orderShop->removeOrderProduct($orderProduct); $orderShop->removeOrderProduct($orderProduct);
$this->entityManager->delete($orderProduct); $this->entityManager->delete($orderProduct);
$this->entityManager->flush(); $this->entityManager->flush();
} }
} }
if ($countOrderProductUpdated) { if ($countOrderProductUpdated) {
// @TODO : faire le add flash dans le controller
$this->flashBagTranslator->add( $this->flashBagTranslator->add(
'success',
'orderProductUpdated',
'OrderShop',
array('%count%' => $countOrderProductUpdated)
'success',
'orderProductUpdated',
'OrderShop',
//array('%count%' => $countOrderProductUpdated)
['%count' => $debug]
); );


$this->entityManager->flush(); $this->entityManager->flush();
$orderShop->setStatTotal($this->priceSolver->getTotal($orderShop)); $orderShop->setStatTotal($this->priceSolver->getTotal($orderShop));
$orderShop->setStatTotalWithTax($this->priceSolver->getTotalWithTax($orderShop)); $orderShop->setStatTotalWithTax($this->priceSolver->getTotalWithTax($orderShop));
$orderShop->setStatTotalOrderProductsWithReductions( $orderShop->setStatTotalOrderProductsWithReductions(
$this->priceSolver->getTotalOrderProductsWithReductions($orderShop)
$this->priceSolver->getTotalOrderProductsWithReductions($orderShop)
); );
$orderShop->setStatTotalOrderProductsWithTaxAndReductions( $orderShop->setStatTotalOrderProductsWithTaxAndReductions(
$this->priceSolver->getTotalOrderProductsWithTaxAndReductions($orderShop)
$this->priceSolver->getTotalOrderProductsWithTaxAndReductions($orderShop)
); );
$orderShop->setStatMarginOrderProductsWithReductions( $orderShop->setStatMarginOrderProductsWithReductions(
$this->priceSolver->getMarginOrderProductsWithReductions($orderShop)
$this->priceSolver->getMarginOrderProductsWithReductions($orderShop)
); );
$orderShop->setStatDeliveryPriceWithReduction($this->priceSolver->getDeliveryPriceWithReduction($orderShop)); $orderShop->setStatDeliveryPriceWithReduction($this->priceSolver->getDeliveryPriceWithReduction($orderShop));
$orderShop->setStatDeliveryPriceWithTaxAndReduction( $orderShop->setStatDeliveryPriceWithTaxAndReduction(
$this->priceSolver->getDeliveryPriceWithTaxAndReduction($orderShop)
$this->priceSolver->getDeliveryPriceWithTaxAndReduction($orderShop)
); );


$this->entityManager->update($orderShop); $this->entityManager->update($orderShop);
public function getProductsSalesStatistic(SectionInterface $section, $entity, $nbWeek = 2) public function getProductsSalesStatistic(SectionInterface $section, $entity, $nbWeek = 2)
{ {
$productsSalesStatistic = new ProductsSalesStatistic( $productsSalesStatistic = new ProductsSalesStatistic(
$this->entityManager,
$entity,
$nbWeek,
$this->productSolver
$this->entityManager,
$entity,
$nbWeek,
$this->productSolver
); );


$productsSalesStatistic->init($section, $this->distributionBuilder, $this->openingResolver); $productsSalesStatistic->init($section, $this->distributionBuilder, $this->openingResolver);

+ 53
- 52
Solver/Order/OrderShopSolver.php View File

use Lc\CaracoleBundle\Model\Product\ProductFamilyModel; use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
use Lc\CaracoleBundle\Model\Product\ProductInterface; use Lc\CaracoleBundle\Model\Product\ProductInterface;
use Lc\CaracoleBundle\Model\Reduction\ReductionCreditInterface; use Lc\CaracoleBundle\Model\Reduction\ReductionCreditInterface;
use Lc\CaracoleBundle\Model\Section\SectionInterface;
use Lc\CaracoleBundle\Solver\Product\ProductFamilySectionPropertySolver; use Lc\CaracoleBundle\Solver\Product\ProductFamilySectionPropertySolver;
use Lc\CaracoleBundle\Solver\Product\ProductSolver; use Lc\CaracoleBundle\Solver\Product\ProductSolver;


protected ProductSolver $productSolver; protected ProductSolver $productSolver;


public function __construct( public function __construct(
EntityManagerInterface $entityManager,
ProductSolver $productSolver
EntityManagerInterface $entityManager,
ProductSolver $productSolver
) { ) {
$this->entityManager = $entityManager; $this->entityManager = $entityManager;
$this->productSolver = $productSolver; $this->productSolver = $productSolver;
public static function getTypeDeliveryChoices() public static function getTypeDeliveryChoices()
{ {
return [ return [
OrderShopModel::DELIVERY_TYPE_HOME,
OrderShopModel::DELIVERY_TYPE_POINTSALE,
OrderShopModel::DELIVERY_TYPE_HOME,
OrderShopModel::DELIVERY_TYPE_POINTSALE,
]; ];
} }




// getOrderProductsByProductFamily // getOrderProductsByProductFamily
public function getOrderProductsByProductFamily( public function getOrderProductsByProductFamily(
OrderShopInterface $orderShop,
ProductFamilyInterface $productFamily
OrderShopInterface $orderShop,
ProductFamilyInterface $productFamily
): array { ): array {
$arrayOrderProducts = []; $arrayOrderProducts = [];


} }


public function getQuantityOrderByProduct( public function getQuantityOrderByProduct(
OrderShopInterface $orderShop,
ProductInterface $product,
$byWeight = false
OrderShopInterface $orderShop,
ProductInterface $product,
$byWeight = false
): int { ): int {
$quantity = 0; $quantity = 0;
$productFamily = $product->getProductFamily(); $productFamily = $product->getProductFamily();


foreach ($orderShop->getOrderProducts() as $orderProduct) { foreach ($orderShop->getOrderProducts() as $orderProduct) {
if ($orderProduct->getProduct()->getId() == $product->getId() if ($orderProduct->getProduct()->getId() == $product->getId()
|| (($behaviorCountStock == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY || $behaviorCountStock == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE)
&& $orderProduct->getProduct()->getProductFamily()->getId() == $productFamily->getId())) {
|| (($behaviorCountStock == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY || $behaviorCountStock == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE)
&& $orderProduct->getProduct()->getProductFamily()->getId() == $productFamily->getId())) {
if ($byWeight) { if ($byWeight) {
$quantity += $orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct( $quantity += $orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct(
) / $this->productSolver->getUnitInherited($orderProduct->getProduct())->getCoefficient());
) / $this->productSolver->getUnitInherited(
$orderProduct->getProduct()
)->getCoefficient());
} else { } else {
$quantity += $orderProduct->getQuantityOrder(); $quantity += $orderProduct->getQuantityOrder();
} }


// isProductAvailable // isProductAvailable
public function isProductAvailable( public function isProductAvailable(
ProductInterface $product,
$quantityOrder = 0,
$checkCart = false,
$orderShop
SectionInterface $section,
OrderShopInterface $orderShop,
ProductInterface $product,
int $quantityOrder = 0,
bool $checkCart = false
) { ) {
if ($product->getStatus() != 1 || $product->getProductFamily()->getStatus( if ($product->getStatus() != 1 || $product->getProductFamily()->getStatus(
) != 1 || !$this->productSolver->isProductSaleStatusOn($product)) {
) != 1 || !$this->productSolver->isProductSaleStatusOn($product)) {
return false; return false;
} }


if ($checkCart && !$orderShop) {
throw new \Exception("Attention : définir le orderShop à l'endroit où est appelé isAvailable");
}

$productFamily = $product->getProductFamily(); $productFamily = $product->getProductFamily();
$quantityAsked = $quantityOrder; $quantityAsked = $quantityOrder;


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


if ($checkCart) { if ($checkCart) {
} }


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

if (!$quantityOrder) { if (!$quantityOrder) {
$quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product); $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product);
} }
public function isOneProductAvailableAddCart(OrderShopInterface $orderShop, $products): bool public function isOneProductAvailableAddCart(OrderShopInterface $orderShop, $products): bool
{ {
foreach ($products as $product) { foreach ($products as $product) {
if ($this->isProductAvailable($product, 1, true, $orderShop)) {
if ($this->isProductAvailable($orderShop->getSection(), $orderShop, $product, 1, true)) {
return true; return true;
} }
} }
public function isOrderProductAvailableAddCart(OrderProductInterface $orderProduct, OrderShopInterface $orderShop) public function isOrderProductAvailableAddCart(OrderProductInterface $orderProduct, OrderShopInterface $orderShop)
{ {
return $this->isProductAvailable( return $this->isProductAvailable(
$orderProduct->getProduct(),
$orderProduct->getQuantityOrder(),
true,
$orderShop
$orderShop->getSection(),
$orderShop,
$orderProduct->getProduct(),
1,
true
); );
} }


} }


public function mergeComplentaryOrderShops( public function mergeComplentaryOrderShops(
OrderShopInterface $orderShop,
bool $combineProducts = true
OrderShopInterface $orderShop,
bool $combineProducts = true
): OrderShopInterface { ): OrderShopInterface {
$this->entityManager->refresh($orderShop); $this->entityManager->refresh($orderShop);


$updated = false; $updated = false;
foreach ($orderShop->getOrderProducts() as $orderProduct) { foreach ($orderShop->getOrderProducts() as $orderProduct) {
if ($combineProducts && $orderProduct->getProduct()->getId() == $orderProductAdd->getProduct( if ($combineProducts && $orderProduct->getProduct()->getId() == $orderProductAdd->getProduct(
)->getId()
&& (string)$orderProduct->getPrice() == (string)$orderProductAdd->getPrice()
)->getId()
&& (string)$orderProduct->getPrice() == (string)$orderProductAdd->getPrice()
) { ) {
$orderProduct->setUpdatedOnMergeComplementaryOrderShop(true); $orderProduct->setUpdatedOnMergeComplementaryOrderShop(true);
$orderProduct->setQuantityOrder( $orderProduct->setQuantityOrder(
$orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder()
$orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder()
); );


$updated = true; $updated = true;
} }


public function isReductionCreditAddedToOrder( public function isReductionCreditAddedToOrder(
OrderShopInterface $orderShop,
ReductionCreditInterface $reductionCredit
OrderShopInterface $orderShop,
ReductionCreditInterface $reductionCredit
) { ) {
foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) { foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
if ($orderReductionCredit->getReductionCredit() == $reductionCredit) { if ($orderReductionCredit->getReductionCredit() == $reductionCredit) {




public function hasOrderProductAlreadyInCart( public function hasOrderProductAlreadyInCart(
OrderShopInterface $orderShop,
OrderProductInterface $orderProductTest
OrderShopInterface $orderShop,
OrderProductInterface $orderProductTest
): ?OrderProductInterface { ): ?OrderProductInterface {
foreach ($orderShop->getOrderProducts() as $orderProduct) { foreach ($orderShop->getOrderProducts() as $orderProduct) {
if ($orderProduct->getProduct() == $orderProductTest->getProduct()) { if ($orderProduct->getProduct() == $orderProductTest->getProduct()) {
public function isValid(OrderShopInterface $orderShop): bool public function isValid(OrderShopInterface $orderShop): bool
{ {
if ($orderShop->getOrderStatus() && in_array( if ($orderShop->getOrderStatus() && in_array(
$orderShop->getOrderStatus()->getAlias(),
OrderStatusModel::$statusAliasAsValid
) > 0) {
$orderShop->getOrderStatus()->getAlias(),
OrderStatusModel::$statusAliasAsValid
) > 0) {
return true; return true;
} }


public function isCart(OrderShopInterface $orderShop): bool public function isCart(OrderShopInterface $orderShop): bool
{ {
if ($orderShop->getOrderStatus() && in_array( if ($orderShop->getOrderStatus() && in_array(
$orderShop->getOrderStatus()->getAlias(),
OrderStatusModel::$statusAliasAsCart
) > 0) {
$orderShop->getOrderStatus()->getAlias(),
OrderStatusModel::$statusAliasAsCart
) > 0) {
return true; return true;
} }




public function isDone(OrderShopInterface $orderShop): bool public function isDone(OrderShopInterface $orderShop): bool
{ {
if ($orderShop->getOrderStatus() && $orderShop->getOrderStatus()->getAlias() == OrderStatusModel::ALIAS_DONE ) {
if ($orderShop->getOrderStatus() && $orderShop->getOrderStatus()->getAlias() == OrderStatusModel::ALIAS_DONE) {
return true; return true;
} }
return false; return false;
} }


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



Loading…
Cancel
Save