Browse Source

Refactoring services

packProduct
Guillaume 3 years ago
parent
commit
214ea1a16d
16 changed files with 565 additions and 120 deletions
  1. +5
    -5
      Builder/File/DocumentBuilder.php
  2. +11
    -2
      Builder/Order/OrderProductBuilder.php
  3. +56
    -29
      Builder/Order/OrderShopBuilder.php
  4. +25
    -0
      Event/Order/CartChangeEvent.php
  5. +30
    -21
      Event/Order/OrderShopChangeStatusEvent.php
  6. +5
    -1
      Factory/Order/OrderProductFactory.php
  7. +2
    -2
      Generator/Reference/DocumentReferenceGenerator.php
  8. +2
    -2
      Generator/Reference/OrderReferenceGenerator.php
  9. +1
    -0
      Model/Address/AddressModel.php
  10. +29
    -0
      Repository/Order/OrderProductStore.php
  11. +150
    -12
      Repository/Order/OrderShopStore.php
  12. +14
    -4
      Repository/Reduction/ReductionCartStore.php
  13. +1
    -32
      Repository/Reduction/ReductionCreditStore.php
  14. +7
    -0
      Repository/Section/SectionStore.php
  15. +37
    -10
      Resolver/OpeningResolver.php
  16. +190
    -0
      Statistic/Statistic.php

+ 5
- 5
Builder/File/DocumentBuilder.php View File



use Lc\CaracoleBundle\Model\File\DocumentInterface; use Lc\CaracoleBundle\Model\File\DocumentInterface;
use Lc\CaracoleBundle\Model\Order\OrderShopInterface; use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
use Lc\CaracoleBundle\Resolver\Reference\DocumentReferenceResolver;
use Lc\CaracoleBundle\Generator\Reference\DocumentReferenceGenerator;


class DocumentBuilder class DocumentBuilder
{ {
protected DocumentReferenceResolver $documentReferenceResolver;
protected DocumentReferenceGenerator $documentReferenceGenerator;


public function __construct(DocumentReferenceResolver $documentReferenceResolver)
public function __construct(DocumentReferenceGenerator $documentReferenceGenerator)
{ {
$this->documentReferenceResolver = $documentReferenceResolver;
$this->documentReferenceGenerator = $documentReferenceGenerator;
} }


public function initFromOrderShop(DocumentInterface $document, OrderShopInterface $orderShop) public function initFromOrderShop(DocumentInterface $document, OrderShopInterface $orderShop)
$merchantAddress = $orderShop->getMerchant()->getAddress(); $merchantAddress = $orderShop->getMerchant()->getAddress();
$buyerAddress = $orderShop->getInvoiceAddress(); $buyerAddress = $orderShop->getInvoiceAddress();


$document->setReference($this->documentReferenceResolver->buildReference($orderShop->getMerchant())) ;
$document->setReference($this->documentReferenceGenerator->buildReference($orderShop->getMerchant())) ;
$document->setMerchantAddress($merchantAddress); $document->setMerchantAddress($merchantAddress);
$document->setBuyerAddress($buyerAddress); $document->setBuyerAddress($buyerAddress);
$document->setMerchantAddressText($merchantAddress->getSummary()); $document->setMerchantAddressText($merchantAddress->getSummary());

+ 11
- 2
Builder/Order/OrderProductBuilder.php View File



namespace Lc\CaracoleBundle\Builder\Order; namespace Lc\CaracoleBundle\Builder\Order;


use Doctrine\ORM\EntityManagerInterface;
use Lc\CaracoleBundle\Model\Order\OrderProductInterface; use Lc\CaracoleBundle\Model\Order\OrderProductInterface;
use Lc\CaracoleBundle\Repository\Order\OrderProductStore;
use Lc\CaracoleBundle\Resolver\Price\PriceResolver; use Lc\CaracoleBundle\Resolver\Price\PriceResolver;


class OrderProductBuilder class OrderProductBuilder
{ {
protected EntityManagerInterface $entityManager;
protected PriceResolver $priceResolver; protected PriceResolver $priceResolver;
protected OrderProductStore $orderProductStore;


public function __construct(PriceResolver $priceResolver)
{
public function __construct(
EntityManagerInterface $entityManager,
PriceResolver $priceResolver,
OrderProductStore $orderProductStore
) {
$this->entityManager = $entityManager;
$this->priceResolver = $priceResolver; $this->priceResolver = $priceResolver;
$this->orderProductStore = $orderProductStore;
} }


public function init(OrderProductInterface $orderProduct) public function init(OrderProductInterface $orderProduct)

+ 56
- 29
Builder/Order/OrderShopBuilder.php View File

use Lc\CaracoleBundle\Model\File\DocumentModel; use Lc\CaracoleBundle\Model\File\DocumentModel;
use Lc\CaracoleBundle\Model\Order\OrderProductInterface; use Lc\CaracoleBundle\Model\Order\OrderProductInterface;
use Lc\CaracoleBundle\Model\Order\OrderShopInterface; use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
use Lc\CaracoleBundle\Model\Order\OrderStatusInterface;
use Lc\CaracoleBundle\Model\Order\OrderStatusModel; use Lc\CaracoleBundle\Model\Order\OrderStatusModel;
use Lc\CaracoleBundle\Model\Product\ProductFamilyModel; use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
use Lc\CaracoleBundle\Model\Reduction\ReductionCartInterface; use Lc\CaracoleBundle\Model\Reduction\ReductionCartInterface;
use Lc\CaracoleBundle\Repository\Product\ProductFamilyStore; use Lc\CaracoleBundle\Repository\Product\ProductFamilyStore;
use Lc\CaracoleBundle\Resolver\Price\PriceResolver; use Lc\CaracoleBundle\Resolver\Price\PriceResolver;
use Lc\SovBundle\Model\User\UserInterface; use Lc\SovBundle\Model\User\UserInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;


class OrderShopBuilder class OrderShopBuilder
{ {
protected PriceResolver $priceResolver; protected PriceResolver $priceResolver;
protected OrderProductBuilder $orderProductBuilder; protected OrderProductBuilder $orderProductBuilder;
protected DocumentBuilder $documentBuilder; protected DocumentBuilder $documentBuilder;
protected EventDispatcherInterface $eventDispatcher;
protected FlashBagInterface $flashBag;


public function __construct( public function __construct(
EntityManagerInterface $entityManager, EntityManagerInterface $entityManager,
ProductFamilyStore $productFamilyStore, ProductFamilyStore $productFamilyStore,
OrderProductBuilder $orderProductBuilder, OrderProductBuilder $orderProductBuilder,
DocumentBuilder $documentBuilder, DocumentBuilder $documentBuilder,
PriceResolver $priceResolver
PriceResolver $priceResolver,
EventDispatcherInterface $eventDispatcher,
FlashBagInterface $flashBag
) { ) {
$this->entityManager = $entityManager; $this->entityManager = $entityManager;
$this->orderShopStore = $orderShopStore; $this->orderShopStore = $orderShopStore;
$this->orderProductBuilder = $orderProductBuilder; $this->orderProductBuilder = $orderProductBuilder;
$this->documentBuilder = $documentBuilder; $this->documentBuilder = $documentBuilder;
$this->priceResolver = $priceResolver; $this->priceResolver = $priceResolver;
$this->eventDispatcher = $eventDispatcher;
$this->flashBag = $flashBag;
} }


public function create( public function create(
return $cart; return $cart;
} }


public function changeOrderStatus(OrderShopInterface $orderShop, string $alias)
public function changeOrderStatus(OrderShopInterface $orderShop, string $alias, bool $forceByAdmin = false)
{ {
$orderStatus = $this->orderStatusStore->getRepositoryQuery()->findOneByAlias($alias); $orderStatus = $this->orderStatusStore->getRepositoryQuery()->findOneByAlias($alias);


if ($orderShop->getOrderStatus() === null
|| $orderShop->getOrderStatus()->getNextStatusAllowed()->contains($orderStatus)) {
$this->eventDispatcher->dispatch(
new OrderShopChangeStatusEvent($orderShop, $orderStatus),
OrderShopChangeStatusEvent::PRE_CHANGE_STATUS
);
if ($orderStatus) {
if ($orderShop->getOrderStatus() === null
|| $orderShop->getOrderStatus()->getNextStatusAllowed()->contains($orderStatus)) {


$orderShop->setOrderStatusProtected($orderStatus);

$orderStatusHistoryFactory = new OrderStatusHistoryFactory();
$orderStatusHistory = $orderStatusHistoryFactory->create($orderShop, $orderStatus);

$orderShop->addOrderStatusHistory($orderStatusHistory);
$this->applyChangeOrderStatus($orderShop, $orderStatus, $forceByAdmin);
}
} else {
throw new \ErrorException('La statut demandé n\'existe pas.');
} }


return $orderShop; return $orderShop;
} }


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

$orderShop->setOrderStatusProtected($orderStatus);

$orderStatusHistoryFactory = new OrderStatusHistoryFactory();
$orderStatusHistory = $orderStatusHistoryFactory->create($orderShop, $orderStatus);

$orderShop->addOrderStatusHistory($orderStatusHistory);

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

public function addOrderProduct( public function addOrderProduct(
OrderShopInterface $orderShop, OrderShopInterface $orderShop,
OrderProductInterface $orderProductAdd, OrderProductInterface $orderProductAdd,
$orderReductionCartFactory = new OrderReductionCartFactory(); $orderReductionCartFactory = new OrderReductionCartFactory();
$orderReductionCart = $orderReductionCartFactory->create($orderShop, $reductionCart); $orderReductionCart = $orderReductionCartFactory->create($orderShop, $reductionCart);


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


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

$this->entityManager->persist($orderReductionCart); $this->entityManager->persist($orderReductionCart);
$this->entityManager->flush(); $this->entityManager->flush();


return $orderReductionCart ;
}
else {
$orderShop->removeOrderReductionCart($orderReductionCart) ;
return $orderReductionCart;
} else {
$orderShop->removeOrderReductionCart($orderReductionCart);
return false; return false;
} }
} }
$orderReductionCreditFactory = new OrderReductionCreditFactory(); $orderReductionCreditFactory = new OrderReductionCreditFactory();
$orderReductionCredit = $orderReductionCreditFactory->create($orderShop, $reductionCredit); $orderReductionCredit = $orderReductionCreditFactory->create($orderShop, $reductionCredit);


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


if($this->isOrderShopPositiveAmount($orderShop)
if ($this->isOrderShopPositiveAmount($orderShop)
&& $this->isOrderShopPositiveAmountRemainingToBePaid($orderShop)) { && $this->isOrderShopPositiveAmountRemainingToBePaid($orderShop)) {

$this->entityManager->persist($orderReductionCredit); $this->entityManager->persist($orderReductionCredit);
$this->entityManager->flush(); $this->entityManager->flush();


return $orderReductionCredit; return $orderReductionCredit;
}
else {
$orderShop->removeOrderReductionCredit($orderReductionCredit) ;
} else {
$orderShop->removeOrderReductionCredit($orderReductionCredit);


return false; return false;
} }
// @TODO : à refactorer en plaçant dans src tout ce qui est spécifique à PDL // @TODO : à refactorer en plaçant dans src tout ce qui est spécifique à PDL
foreach ($orderShop->getOrderProducts() as $orderProduct) { foreach ($orderShop->getOrderProducts() as $orderProduct) {
//Si ce n'esrt pas une relivraison OU si c'est une relivraison + relivraison + ce n'est pas une erruer producteur //Si ce n'esrt pas une relivraison OU si c'est une relivraison + relivraison + ce n'est pas une erruer producteur
if (!$orderProduct->isRedelivery() || ($orderProduct->isRedelivery() && $orderProduct->isRedeliverySupplierOrder() && !$orderProduct->isRedeliverySupplierMistake())) {
if (!$orderProduct->isRedelivery() || ($orderProduct->isRedelivery(
) && $orderProduct->isRedeliverySupplierOrder(
) && !$orderProduct->isRedeliverySupplierMistake())) {
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 = $orderProduct->getProduct()->getAvailableQuantityInherited(); $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
$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);

+ 25
- 0
Event/Order/CartChangeEvent.php View File

<?php

namespace Lc\CaracoleBundle\Event\Order;

use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
use Lc\CaracoleBundle\Model\Order\OrderStatusInterface;
use Symfony\Contracts\EventDispatcher\Event;

class CartChangeEvent extends Event
{
const PRE_UPDATE = 'cart_event.pre_update';
const POST_UPDATE = 'cart_event.post_update';

protected OrderShopInterface $orderShop;

public function __construct(OrderShopInterface $orderShop, OrderStatusInterface $orderStatus, bool $forceByAdmin)
{
$this->orderShop = $orderShop;
}

public function getOrderShop()
{
return $this->orderShop;
}
}

+ 30
- 21
Event/Order/OrderShopChangeStatusEvent.php View File



namespace Lc\CaracoleBundle\Event\Order; namespace Lc\CaracoleBundle\Event\Order;


use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
use Lc\CaracoleBundle\Model\Order\OrderStatusInterface;
use Symfony\Contracts\EventDispatcher\Event; use Symfony\Contracts\EventDispatcher\Event;


class OrderShopChangeStatusEvent extends Event class OrderShopChangeStatusEvent extends Event
{ {
const PRE_CHANGE_STATUS = 'order_shop_event.pre_change_status';
const POST_CHANGE_STATUS = 'order_shop_event.post_change_status';

protected $orderShop;
protected $orderStatus;

public function __construct($orderShop, $orderStatus)
{
$this->orderShop = $orderShop;
$this->orderStatus = $orderStatus;
}

public function getOrderShop()
{
return $this->orderShop;
}

public function getOrderStatus()
{
return $this->orderStatus;
}
const PRE_CHANGE_STATUS = 'order_shop_event.pre_change_status';
const POST_CHANGE_STATUS = 'order_shop_event.post_change_status';

protected OrderShopInterface $orderShop;
protected OrderStatusInterface $orderStatus;
protected bool $forceByAdmin;

public function __construct(OrderShopInterface $orderShop, OrderStatusInterface $orderStatus, bool $forceByAdmin)
{
$this->orderShop = $orderShop;
$this->orderStatus = $orderStatus;
$this->forceByAdmin = $forceByAdmin;
}

public function getOrderShop()
{
return $this->orderShop;
}

public function getOrderStatus()
{
return $this->orderStatus;
}

public function getForceByAdmin()
{
return $this->forceByAdmin;
}
} }

+ 5
- 1
Factory/Order/OrderProductFactory.php View File



use App\Entity\Order\OrderProduct; use App\Entity\Order\OrderProduct;
use Lc\CaracoleBundle\Model\Order\OrderProductInterface; use Lc\CaracoleBundle\Model\Order\OrderProductInterface;
use Lc\CaracoleBundle\Model\Product\ProductInterface;
use Lc\SovBundle\Factory\AbstractFactory; use Lc\SovBundle\Factory\AbstractFactory;


class OrderProductFactory extends AbstractFactory class OrderProductFactory extends AbstractFactory
{ {
public function create(): OrderProductInterface
public function create(ProductInterface $product, int $quantityOrder): OrderProductInterface
{ {
$orderProduct = new OrderProduct(); $orderProduct = new OrderProduct();


$orderProduct->setProduct($product);
$orderProduct->setQuantityOrder($quantityOrder);

return $orderProduct; return $orderProduct;
} }



Resolver/Reference/DocumentReferenceResolver.php → Generator/Reference/DocumentReferenceGenerator.php View File

<?php <?php


namespace Lc\CaracoleBundle\Resolver\Reference;
namespace Lc\CaracoleBundle\Generator\Reference;


use Lc\CaracoleBundle\Definition\SectionSettingDefinition; use Lc\CaracoleBundle\Definition\SectionSettingDefinition;
use Lc\CaracoleBundle\Model\File\DocumentModel; use Lc\CaracoleBundle\Model\File\DocumentModel;
use Lc\CaracoleBundle\Model\Section\SectionModel; use Lc\CaracoleBundle\Model\Section\SectionModel;
use Lc\CaracoleBundle\Repository\File\DocumentStore; use Lc\CaracoleBundle\Repository\File\DocumentStore;


class DocumentReferenceResolver
class DocumentReferenceGenerator
{ {


protected DocumentStore $documentStore; protected DocumentStore $documentStore;

Resolver/Reference/OrderReferenceResolver.php → Generator/Reference/OrderReferenceGenerator.php View File

<?php <?php


namespace Lc\CaracoleBundle\Resolver\Reference;
namespace Lc\CaracoleBundle\Generator\Reference;


use Lc\CaracoleBundle\Definition\SectionSettingDefinition; use Lc\CaracoleBundle\Definition\SectionSettingDefinition;
use Lc\CaracoleBundle\Model\Order\OrderShopInterface; use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
use Lc\CaracoleBundle\Model\Section\SectionModel; use Lc\CaracoleBundle\Model\Section\SectionModel;


class OrderReferenceResolver
class OrderReferenceGenerator
{ {


public function buildReference(OrderShopInterface $orderShop, \DateTime $distributionDate = null): string public function buildReference(OrderShopInterface $orderShop, \DateTime $distributionDate = null): string

+ 1
- 0
Model/Address/AddressModel.php View File

return $this->getTitle() . ' - ' . $this->getZip() . ' ' . $this->getCity(); return $this->getTitle() . ' - ' . $this->getZip() . ' ' . $this->getCity();
} }


// getAddressSummary
public function getSummaryShort() public function getSummaryShort()
{ {
return $this->getAddress() . ' - ' . $this->getZip() . ' ' . $this->getCity(); return $this->getAddress() . ' - ' . $this->getZip() . ' ' . $this->getCity();

+ 29
- 0
Repository/Order/OrderProductStore.php View File

{ {
$this->query = $query; $this->query = $query;
} }

// getOrderProductsFormOrderShopsGroupByStorageOrder
public function getByOrderShopsGroupByStorageOrder(array $orderShops): array
{
$orderProductsByStorageOrder = [];

foreach ($orderShops as $orderShop) {
foreach ($orderShop->getOrderProducts() as $orderProduct) {

if ($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {

$storageOrder = 0;
if ($orderProduct->getProduct()->getProductFamily()->getStorageOrder()) $storageOrder = $productFamily->getStorageOrder();

if (!isset($orderProductsByStorageOrder[$storageOrder])) {
$orderProductsByStorageOrder[$productFamily->getId()] = [
'order_products' => [],
'total_quantity_weight' => 0,
];
}

$orderProductsByStorageOrder[$storageOrder]['order_products'][] = $orderProduct;
$orderProductsByStorageOrder[$storageOrder]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()) * $orderProduct->getQuantityOrder();
}
}
}

return $orderProductsByStorageOrder;
}
} }

+ 150
- 12
Repository/Order/OrderShopStore.php View File



namespace Lc\CaracoleBundle\Repository\Order; namespace Lc\CaracoleBundle\Repository\Order;


use App\Entity\Order\OrderProduct;
use App\Entity\Section\Section;
use Doctrine\ORM\EntityManagerInterface;
use Lc\CaracoleBundle\Builder\File\DocumentBuilder; use Lc\CaracoleBundle\Builder\File\DocumentBuilder;
use Lc\CaracoleBundle\Factory\File\DocumentFactory;
use Lc\CaracoleBundle\Model\File\DocumentModel;
use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
use Lc\CaracoleBundle\Model\Order\OrderShopInterface; use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
use Lc\CaracoleBundle\Model\Section\SectionInterface;
use Lc\CaracoleBundle\Repository\Merchant\MerchantStore;
use Lc\CaracoleBundle\Repository\Reduction\ReductionCreditStore;
use Lc\CaracoleBundle\Repository\Section\SectionStore;
use Lc\CaracoleBundle\Resolver\OpeningResolver;
use Lc\CaracoleBundle\Resolver\Price\PriceResolver; use Lc\CaracoleBundle\Resolver\Price\PriceResolver;
use Lc\CaracoleBundle\Resolver\Reference\DocumentReferenceResolver;
use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Repository\AbstractStore; use Lc\SovBundle\Repository\AbstractStore;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;


class OrderShopStore extends AbstractStore class OrderShopStore extends AbstractStore
{ {
protected OrderShopRepositoryQuery $query; protected OrderShopRepositoryQuery $query;
protected EntityManagerInterface $entityManager;
protected PriceResolver $priceResolver; protected PriceResolver $priceResolver;
protected DocumentReferenceResolver $documentReferenceResolver;
protected DocumentBuilder $documentBuilder; protected DocumentBuilder $documentBuilder;
protected ReductionCreditStore $reductionCreditStore;
protected SectionStore $sectionStore;
protected OrderProductStore $orderProductStore;
protected MerchantStore $merchantStore;
protected FlashBagInterface $flashBag;
protected OpeningResolver $openingResolver;
protected ParameterBagInterface $parameterBag;
protected UrlGeneratorInterface $router;


public function __construct( public function __construct(
OrderShopRepositoryQuery $query, OrderShopRepositoryQuery $query,
EntityManagerInterface $entityManager,
PriceResolver $priceResolver, PriceResolver $priceResolver,
DocumentReferenceResolver $documentReferenceResolver,
DocumentBuilder $documentBuilder
DocumentBuilder $documentBuilder,
ReductionCreditStore $reductionCreditStore,
SectionStore $sectionStore,
OrderProductStore $orderProductStore,
MerchantStore $merchantStore,
FlashBagInterface $flashBag,
OpeningResolver $openingResolver,
ParameterBagInterface $parameterBag,
UrlGeneratorInterface $router
) { ) {
$this->query = $query; $this->query = $query;
$this->entityManager = $entityManager;
$this->priceResolver = $priceResolver; $this->priceResolver = $priceResolver;
$this->documentReferenceResolver = $documentReferenceResolver;
$this->documentBuilder = $documentBuilder; $this->documentBuilder = $documentBuilder;
$this->reductionCreditStore = $reductionCreditStore;
$this->sectionStore = $sectionStore;
$this->orderProductStore = $orderProductStore;
$this->merchantStore = $merchantStore;
$this->flashBag = $flashBag;
$this->openingResolver = $openingResolver;
$this->parameterBag = $parameterBag;
$this->router = $router;
} }


public function getDatas(OrderShopInterface $orderShop = null): array
// getOrderShopsOfWeek
public function getByCycle(SectionInterface $section, $params = [])
{ {
$data = [];
$orderShops = $this->getBy(
array_merge(
[
'section' => $section,
'cycleNumber' => $this->getCycleNumberCurrentOrder($section),
'isValid' => true,
],
$params
)
);

return $orderShops;
}

// getOrderShopsOfWeekByUser
public function getByCycleAndUser(SectionInterface $section, UserInterface $user, array $params = [])
{
return $this->getByCycle(
$section,
array_merge(
[
'user' => $user,
'weekNumber' => $this->getCycleNumberCurrentOrder($section),
'excludeComplementaryOrderShops' => true
],
$params
)
);
}

//public $countOrderShopsOfWeek = null;

public function countByCycle(SectionInterface $section, bool $excludeComplementaryOrderShops = true)
{
return $this->getByCycle(
$section,
[
'count' => true,
'excludeComplementaryOrderShops' => $excludeComplementaryOrderShops

]
);

// @TODO : optimisation à remettre en place
/*if (is_null($this->countOrderShopsOfWeek)) {
$this->countOrderShopsOfWeek = $this->getByCycle(
$section,
[
'count' => true,
'excludeComplementaryOrderShops' => $excludeComplementaryOrderShops

]
);
}
return $this->countOrderShopsOfWeek;*/
}

// getNextWeekId
public function getNextCycleId(SectionInterface $section, int $cycleNumber): int
{
$lastOrder = $this->getOneLastOrderValidOfCycle($section, $cycleNumber);
if ($lastOrder) {
return intval($lastOrder->getCycleId() + 1);
} else {
return 1;
}
}

public function getNextIdValidOrder(Section $section)
{
$lastOrder = $this->getOneLastOrderValid($section);


if (is_null($orderShop)) {
$orderShop = $this->getCartCurrent();
if ($lastOrder) {
return intval($lastOrder->getIdValidOrder() + 1);
} else {
return 1;
} }
}

// getOrderDatas
public function getDatas(OrderShopInterface $orderShop, UserInterface $user = null): array
{
$data = [];


$data['order'] = $orderShop; $data['order'] = $orderShop;


return $newOrderShop; return $newOrderShop;
} }


public function isCartAllowToBeOrder(OrderShopInterface $orderShop)
public function isCartAllowToBeOrder(OrderShopInterface $orderShop, bool $forceByAdmin = false)
{ {
return true; return true;
} }


// countValidOrderShopByUserAllMerchant
public function countValidByUserAllMerchant($user)
{
$totalOrder = 0;

foreach ($this->merchantStore->getRepositoryQuery()->findAll() as $merchant) {
$totalOrder += $this->countValidByUser($user, $merchant);
}

return $totalOrder;
}

public function countValidByUser(UserInterface $user, MerchantInterface $merchant = null)
{
return $this->getBy(
[
'user' => $user,
'isValid' => true,
'merchant' => $merchant,
'excludeComplementaryOrderShops' => true,
'count' => true
]
);
}

/* /*
public function getCartCurrent(SectionInterface $section, UserInterface $user = null, VisitorInterface $visitor = null) public function getCartCurrent(SectionInterface $section, UserInterface $user = null, VisitorInterface $visitor = null)
{ {

+ 14
- 4
Repository/Reduction/ReductionCartStore.php View File



namespace Lc\CaracoleBundle\Repository\Reduction; namespace Lc\CaracoleBundle\Repository\Reduction;


use App\Entity\Order\OrderShop;
use App\Entity\Reduction\ReductionCart;
use App\Resolver\Price\PriceResolver;
use Lc\CaracoleBundle\Model\Reduction\ReductionCartInterface; use Lc\CaracoleBundle\Model\Reduction\ReductionCartInterface;
use Lc\CaracoleBundle\Repository\Order\OrderShopStore; use Lc\CaracoleBundle\Repository\Order\OrderShopStore;
use Lc\SovBundle\Model\User\UserInterface; use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Repository\AbstractStore; use Lc\SovBundle\Repository\AbstractStore;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;


class ReductionCartStore extends AbstractStore class ReductionCartStore extends AbstractStore
{ {
protected ReductionCartRepositoryQuery $query; protected ReductionCartRepositoryQuery $query;
protected OrderShopStore $orderShopStore; protected OrderShopStore $orderShopStore;
protected PriceResolver $priceResolver;
protected FlashBagInterface $flashBag;


public function __construct( public function __construct(
ReductionCartRepositoryQuery $query, ReductionCartRepositoryQuery $query,
OrderShopStore $orderShopStore
OrderShopStore $orderShopStore,
PriceResolver $priceResolver,
FlashBagInterface $flashBag
) { ) {
$this->query = $query; $this->query = $query;
$this->orderShopStore = $orderShopStore; $this->orderShopStore = $orderShopStore;
$this->priceResolver = $priceResolver;
$this->flashBag = $flashBag;
} }


// getReductionCartByCode // getReductionCartByCode
// getReductionCartUsedQuantityPerUser // getReductionCartUsedQuantityPerUser
public function getUsedQuantityByUser(ReductionCartInterface $reductionCart, UserInterface $user): float public function getUsedQuantityByUser(ReductionCartInterface $reductionCart, UserInterface $user): float
{ {
return $this->orderShopStore->countValidOrderWithReductionCartPerUser($reductionCart, $user);
return $this->orderShopStore->countValidWithReductionCartPerUser($reductionCart, $user);
} }


// getReductionCartUsedQuantity // getReductionCartUsedQuantity
public function getUsedQuantity(ReductionCartInterface $reductionCart): float public function getUsedQuantity(ReductionCartInterface $reductionCart): float
{ {
return $this->orderShopStore->countValidOrderWithReductionCart($reductionCart);
return $this->orderShopStore->countValidWithReductionCartPerUser($reductionCart);
} }


// getReductionCartRemainingQuantityPerUser // getReductionCartRemainingQuantityPerUser
{ {
if ($reductionCart->getAvailableQuantityPerUser()) { if ($reductionCart->getAvailableQuantityPerUser()) {
return $reductionCart->getAvailableQuantityPerUser( return $reductionCart->getAvailableQuantityPerUser(
) - $this->orderShopRepo->countValidOrderWithReductionCartPerUser($reductionCart, $user);
) - $this->orderShopStore->countValidWithReductionCartPerUser($reductionCart, $user);
} }


return false; return false;

+ 1
- 32
Repository/Reduction/ReductionCreditStore.php View File

class ReductionCreditStore extends AbstractStore class ReductionCreditStore extends AbstractStore
{ {
protected ReductionCreditRepositoryQuery $query; protected ReductionCreditRepositoryQuery $query;
protected OrderShopStore $orderShopStore;


public function __construct( public function __construct(
ReductionCreditRepositoryQuery $query,
OrderShopStore $orderShopStore
ReductionCreditRepositoryQuery $query
) { ) {
$this->query = $query; $this->query = $query;
$this->orderShopStore = $orderShopStore;
}

// isReductionCreditAllowAddToOrder
public function isAllowAddToOrder(OrderShopInterface $orderShop, ReductionCreditInterface $reductionCredit)
{
$user = $orderShop->getUser();

// appartient à l'utilisateur
if (!$reductionCredit->getUsers()->contains($user)) {
// @TODO : gérer les addFlash dans le controleur
//$this->utils->addFlash('error', 'error.reductionCredit.userNotAllow');
return false;
}

// n'a pas été utilisé
if ($reductionCredit->getType() == ReductionCredit::TYPE_CREDIT) {
if ($this->orderShopStore->countValidOrderWithReductionCredit($reductionCredit, $user) > 0) {
//$this->utils->addFlash('error', 'error.reductionCredit.alreadyUse');
return false;
}
} else {
if ($this->orderShopStore->countValidOrderWithReductionCredit($reductionCredit) > 0) {
//$this->utils->addFlash('error', 'error.reductionCredit.alreadyUse');
return false;
}
}

return true;
} }


// findReductionCreditsByUser // findReductionCreditsByUser

+ 7
- 0
Repository/Section/SectionStore.php View File



use Lc\CaracoleBundle\Model\Section\SectionInterface; use Lc\CaracoleBundle\Model\Section\SectionInterface;
use Lc\CaracoleBundle\Repository\MerchantStoreTrait; use Lc\CaracoleBundle\Repository\MerchantStoreTrait;
use Lc\CaracoleBundle\Repository\Order\OrderShopStore;
use Lc\SovBundle\Repository\AbstractStore; use Lc\SovBundle\Repository\AbstractStore;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;


use MerchantStoreTrait; use MerchantStoreTrait;


protected SectionRepositoryQuery $query; protected SectionRepositoryQuery $query;
protected OrderShopStore $orderShopStore;


public function __construct(SectionRepositoryQuery $query) public function __construct(SectionRepositoryQuery $query)
{ {


return $section; return $section;
} }

public function countOpenDays(SectionInterface $section): int
{
// @TODO : à implémenter avec le nouveau système d'ouverture des commandes
}
} }

+ 37
- 10
Resolver/OpeningResolver.php View File



namespace Lc\CaracoleBundle\Resolver; namespace Lc\CaracoleBundle\Resolver;


use App\Entity\Section\Section;
use Lc\CaracoleBundle\Definition\SectionSettingDefinition; use Lc\CaracoleBundle\Definition\SectionSettingDefinition;
use Lc\CaracoleBundle\Model\Section\OpeningInterface; use Lc\CaracoleBundle\Model\Section\OpeningInterface;
use Lc\CaracoleBundle\Model\Section\SectionInterface; use Lc\CaracoleBundle\Model\Section\SectionInterface;
use Lc\CaracoleBundle\Repository\Order\OrderShopStore;
use Lc\SovBundle\Model\User\UserInterface; use Lc\SovBundle\Model\User\UserInterface;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;


protected array $messages = []; protected array $messages = [];
protected SectionResolver $sectionResolver; protected SectionResolver $sectionResolver;
protected Security $security; protected Security $security;
//protected OrderShopStore $orderShopStore;


public function __construct(SectionResolver $sectionResolver, Security $security)
public function __construct(SectionResolver $sectionResolver, Security $security /*, OrderShopStore $orderShopStore*/)
{ {
$this->sectionResolver = $sectionResolver; $this->sectionResolver = $sectionResolver;
$this->security = $security; $this->security = $security;
//$this->orderShopStore = $orderShopStore;
} }


public function isOpenSale( public function isOpenSale(
SectionInterface $section = null, SectionInterface $section = null,
UserInterface $user = null, UserInterface $user = null,
\DateTime $date = null
\DateTime $date = null,
string $context = null
): bool { ): bool {
// Initialisation // Initialisation
$this->messages = []; $this->messages = [];
} }


// Nombre maximum de commandes par cycle (voir configuration de section) // Nombre maximum de commandes par cycle (voir configuration de section)
$countOrderShopCycle = 0;
$orderMaximumPerCycle = $section->getSettingValue(SectionSettingDefinition::SETTING_ORDER_MAXIMUM_PER_CYCLE);
if ($orderMaximumPerCycle && $countOrderShopCycle >= $orderMaximumPerCycle) {
if($this->isMaximumOrderCycleAchieved($section)) {
$this->addMessage('Le nombre maximum de commande a été atteint.'); $this->addMessage('Le nombre maximum de commande a été atteint.');
return false; return false;
} }


// Période de fermeture des commandes issue de la configuration de la section (congés) // Période de fermeture des commandes issue de la configuration de la section (congés)
$orderClosedStart = $section->getSettingValue(SectionSettingDefinition::SETTING_ORDER_CLOSED_START);
$orderClosedEnd = $section->getSettingValue(SectionSettingDefinition::SETTING_ORDER_CLOSED_END);

if ($orderClosedStart && $orderClosedEnd && $date >= $orderClosedStart && $date <= $orderClosedEnd) {
if($this->isClosingPeriod($section)) {
$this->addMessage( $this->addMessage(
'Les commandes sont fermées (période de fermeture des commandes dans la configuration de la section).' 'Les commandes sont fermées (période de fermeture des commandes dans la configuration de la section).'
); );
return false; return false;
} }


// isHolidays
public function isClosingPeriod(Section $section, \DateTime $date)
{
$orderClosedStart = $section->getSettingValue(SectionSettingDefinition::SETTING_ORDER_CLOSED_START);
$orderClosedEnd = $section->getSettingValue(SectionSettingDefinition::SETTING_ORDER_CLOSED_END);

if ($orderClosedStart && $orderClosedEnd && $date >= $orderClosedStart && $date <= $orderClosedEnd) {
return true;
}

return false;
}


// isMaximumOrderWeekAchieved
public function isMaximumOrderCycleAchieved(SectionInterface $section)
{
// @TODO : countValidByCycle
//$countOrderShopCycle = $this->orderShopStore->countValidByCycle($section);
$countOrderShopCycle = 0 ;
$orderMaximumPerCycle = $section->getSettingValue(SectionSettingDefinition::SETTING_ORDER_MAXIMUM_PER_CYCLE);
if ($orderMaximumPerCycle && $countOrderShopCycle >= $orderMaximumPerCycle) {
return true;
}

return false;
}

public function isDateMatchWithOpening(\DateTime $date, OpeningInterface $opening): bool public function isDateMatchWithOpening(\DateTime $date, OpeningInterface $opening): bool
{ {
$day = $date->format('N'); $day = $date->format('N');
return false; return false;
} }



public function getDateEndCurrentSale(SectionInterface $section, $formatDate = '', $delimiterDayTime = 'à') public function getDateEndCurrentSale(SectionInterface $section, $formatDate = '', $delimiterDayTime = 'à')
{ {
// @TODO : à réécrire // @TODO : à réécrire

+ 190
- 0
Statistic/Statistic.php View File

<?php


namespace Lc\CaracoleBundle\Statistic;

use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class Statistic
{
protected $properties = array();
protected $averageProperties = array();
protected $labels;
protected $dateStart;
protected $dateEnd;
protected $interval;
protected $em;
protected $resultsSort;

public function __construct(
EntityManagerInterface $entityManager,
?\DateTime $dateStart = null,
?\DateTime $dateEnd = null,
?string $interval = null
) {
$this->em = $entityManager;
$this->dateStart = $dateStart;
$this->dateEnd = $dateEnd;
$this->interval = $interval;
}

public function addProperty(string $propertyName, array $options)
{
$resolver = new OptionsResolver();
$this->configurePropertyOptions($resolver);
$this->properties[$propertyName] = $resolver->resolve($options);
$this->resultsSort[$propertyName] = $propertyName;
}

public function addAverageProperty(string $propertyName, array $options)
{
$resolver = new OptionsResolver();
$this->configurePropertyOptions($resolver);
$this->averageProperties[$propertyName] = $resolver->resolve($options);
$this->resultsSort[$propertyName] = $propertyName;
}


public function setAverageData(string $propertyName, string $key, $value)
{
if (isset($this->averageProperties[$propertyName])) {
$this->averageProperties[$propertyName]['data'][$key] += number_format($value, 2);
} else {
throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas ');
}
}

public function setData(string $propertyName, string $key, $value)
{
if (isset($this->properties[$propertyName])) {
$this->properties[$propertyName]['data'][$key] += number_format($value, 2);
$this->properties[$propertyName]['total_period'] += number_format($value, 2);
} else {
throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas ');
}
}

public function setAveragePropertiesData()
{
foreach ($this->getLabels() as $key => $label) {
foreach ($this->getAverageProperties() as $averagePropertyName => $averageProperty) {
if ($this->getData($averageProperty['divider'], $key)) {
$this->setAverageData(
$averagePropertyName,
$key,
$this->getData($averageProperty['dividend'], $key) / $this->getData(
$averageProperty['divider'],
$key
)
);
}

if ($this->getTotalPeriod($averageProperty['divider'])) {
$this->averageProperties[$averagePropertyName]['total_period'] = $this->getTotalPeriod(
$averageProperty['dividend']
) / $this->getTotalPeriod($averageProperty['divider']);
}
}
}
foreach ($this->getProperties() as $propertyName => $property) {
$this->properties[$propertyName]['average_period'] = number_format(
$this->properties[$propertyName]['total_period'] / count($this->getLabels()),
2
);
}
foreach ($this->getAverageProperties() as $averagePropertyName => $averageProperty) {
$this->averageProperties[$averagePropertyName]['average_period'] = number_format(
$this->averageProperties[$averagePropertyName]['total_period'] / count($this->getLabels()),
2
);
}
}


public function getLabels()
{
return $this->labels;
}

public function getProperties()
{
return $this->properties;
}

public function getData($propertyName, $key)
{
if (!isset($this->properties[$propertyName])) {
throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas ');
}
return $this->properties[$propertyName]['data'][$key];
}

public function getTotalPeriod($propertyName)
{
if (!isset($this->properties[$propertyName])) {
throw new \Exception('La proprieté "' . $propertyName . '" n\'existe pas ');
}
return $this->properties[$propertyName]['total_period'];
}

public function getAverageProperties()
{
return $this->averageProperties;
}


public function getResults()
{
$results = array_replace($this->resultsSort, $this->properties, $this->averageProperties);
return $results;
}

public function getAsArray()
{
return array(
'label' => $this->getLabels(),
'data' => $this->getResults(),
);
}

public function getDateRange()
{
return new \DatePeriod(
$this->getDateStart(),
new \DateInterval('P1' . $this->getInterval()),
$this->getDateEnd()
);
}

public function getDateStart()
{
return $this->dateStart;
}

public function getDateEnd()
{
return $this->dateEnd;
}

public function getInterval()
{
return $this->interval;
}

public function configurePropertyOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
[
'unit' => '',
'label' => 'Chiffre affaire produit',
'label_short' => 'CA produit',
'data' => array(),
'total_period' => 0,
'average_period' => 0,
'dividend' => null,
'divider' => null,
]
);
}
}

Loading…
Cancel
Save