|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- <?php
-
- namespace Lc\CaracoleBundle\Repository\Order;
-
- use App\Entity\Delivery\DeliveryAvailabilityPointSale;
- use App\Entity\Delivery\DeliveryAvailabilityZone;
- use App\Entity\Order\OrderStatus;
- use App\Entity\Section\Section;
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\CaracoleBundle\Builder\File\DocumentBuilder;
- use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
- use Lc\CaracoleBundle\Model\Order\OrderReductionCartInterface;
- use Lc\CaracoleBundle\Model\Order\OrderReductionCreditInterface;
- 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\Repository\SectionStoreTrait;
- use Lc\CaracoleBundle\Resolver\Price\PriceResolver;
- use Lc\CaracoleBundle\Solver\Price\PriceSolver;
- use Lc\SovBundle\Model\User\UserInterface;
- 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
- {
- use SectionStoreTrait;
-
- protected OrderShopRepositoryQuery $query;
- protected EntityManagerInterface $entityManager;
- protected PriceSolver $priceSolver;
- 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(
- OrderShopRepositoryQuery $query,
- EntityManagerInterface $entityManager,
- PriceSolver $priceSolver,
- DocumentBuilder $documentBuilder,
- ReductionCreditStore $reductionCreditStore,
- SectionStore $sectionStore,
- OrderProductStore $orderProductStore,
- MerchantStore $merchantStore,
- FlashBagInterface $flashBag,
- OpeningResolver $openingResolver,
- ParameterBagInterface $parameterBag,
- UrlGeneratorInterface $router
- ) {
- $this->query = $query;
- $this->entityManager = $entityManager;
- $this->priceSolver = $priceSolver;
- $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;
- }
-
- // getOrderShopsOfWeek
- public function getByCycle(SectionInterface $section, $params = [])
- {
- $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->getOneLastValid($section);
-
- if ($lastOrder) {
- return intval($lastOrder->getIdValidOrder() + 1);
- } else {
- return 1;
- }
- }
-
- // 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
- ]
- );
- }
-
- // countValidOrderWithReductionCredit
- public function countValidWithReductionCredit(
- OrderReductionCreditInterface $reductionCredit,
- UserInterface $user = null
- ): string {
- $query = $this->query->create();
-
- if ($user) {
- $query->filterByUser($user);
- }
- $query
- ->selectCount()
- ->filterByReductionCredit($reductionCredit)
- ->filterByStatus(OrderStatus::$statusAliasAsValid)
- ->filterBySection($this->section);
-
- return $query->count();
- }
-
- // countValidOrderWithReductionCart
- public function countValidWithReductionCart(
- OrderReductionCartInterface $reductionCart
- ): int {
- $query = $this->query->create();
- $query
- ->selectCount()
- ->filterByReductionCart($reductionCart)
- ->filterByStatus(OrderStatus::$statusAliasAsValid)
- ->filterBySection($this->section);
-
- return $query->count();
- }
-
- // countValidOrderWithReductionCartPerUser
- public function countValidWithReductionCartPerUser(
- OrderReductionCartInterface $reductionCart,
- UserInterface $user
- ): string {
- $query = $this->query->create();
-
- $query
- ->selectCount()
- ->filterByUser($user)
- ->filterByReductionCart($reductionCart)
- ->filterByStatus(OrderStatus::$statusAliasAsValid)
- ->filterBySection($this->section);
-
- return $query->count();
- }
-
- // findCartCurrent
- public function getCartCurrent(array $params): ?OrderShopInterface
- {
- $query = $this->query->create();
-
- if (isset($params['user'])) {
- $query
- ->filterByUser($params['user']);
- }
- if (isset($params['visitor'])) {
- $query
- ->filterByVisitor($params['visitor']);
- }
-
- $query
- ->selectOrderReductionCarts()
- ->filterByStatus(OrderStatus::$statusAliasAsValid)
- ->filterBySection($this->section);
-
- $results = $query->find();
-
- if ($results) {
- return $results[0];
- }
-
- return null;
- }
-
- // findLastOrderValidOfWeek
- public function getOneLastValidOfCycle(int $cycleNumber): ?OrderShopInterface
- {
- $query = $this->query->create();
-
- $query
- ->filterByCycleNumber($cycleNumber)
- ->filterByStatus(OrderStatus::$statusAliasAsValid)
- ->filterIsNotMainOrderShop()
- ->orderBy('.weekId', 'DESC')
- ->filterBySection($this->section);
-
- return $query->findOne();
- }
-
- //findLastOrderValid
- public function getOneLastValid(): ?OrderShopInterface
- {
- $query = $this->query->create();
-
- $query
- ->filterByStatus(OrderStatus::$statusAliasAsValid)
- ->filterIsNotMainOrderShop()
- ->orderBy('.idValidOrder', 'DESC')
- ->filterBySection($this->section);
-
- return $query->findOne();
- }
-
- // @TODO : Fonction à tester
-
- // findAllBy
- public function getBy(array $params = [])
- {
- $query = $this->query->create();
-
- if (isset($params['section'])) {
- $query->filterBySection($params['section']);
- } else {
- $query->filterBySection($this->section);
- }
-
- if (isset($params['count']) && $params['count']) {
- $query->selectCount();
- } else {
- if (isset($params['select'])) {
- $query->selectParam($params['select']);
- }
- }
-
- if (isset($params['dateStart']) || isset($params['dateEnd'])) {
- $params['dateField'] = isset($params['dateField']) ? $params['dateField'] : 'validationDate';
- }
-
- if (isset($params['dateStart'])) {
- $query->filterByDateStart($params['dateField'], $params['dateStart']);
- }
-
- if (isset($params['dateEnd'])) {
- $query->filterByDateEnd($params['dateField'], $params['dateEnd']);
- }
-
- if (isset($params['cycleNumber'])) {
- $query->filterByCycleNumber($params['cycleNumber']);
- }
-
- if (isset($params['isCart'])) {
- $query->filterByStatus(OrderStatus::$statusAliasAsCart);
- }
-
- if (isset($params['isValid'])) {
- $query->filterByStatus(OrderStatus::$statusAliasAsValid);
- }
-
- if (isset($params['isWaitingDelivery'])) {
- $query->filterByStatus(OrderStatus::$statusAliasWaitingDelivery);
- }
-
- if (isset($params['orderStatus'])) {
- $query->filterByStatus($params['orderStatus']);
- }
-
- if (isset($params['user'])) {
- $query->filterByUser($params['user']);
- }
-
- if (isset($params['address'])) {
- $query->filterByAddress($params['address']);
- }
-
- if (isset($params['weekDeliveryTrucks'])) {
- $query->filterByWeekDeliveryTruck($params['weekDeliveryTrucks']);
- }
-
- if (isset($params['estimatedDeliveryDateTime'])) {
- $date = clone $params['estimatedDeliveryDateTime'];
- $query
- ->filterByEstimatedDeliveryDateStart($date->format('Y-m-d 00:00:00'))
- ->filterByEstimatedDeliveryDateEnd($date->modify('+1 day')->format('Y-m-d 00:00:00'));
- }
-
- if (isset($params['deliveryDate'])) {
- $date = clone $params['deliveryDate'];
- $query
- ->filterByDeliveryDateStart($date->format('Y-m-d 00:00:00'))
- ->filterByDeliveryDateEnd($date->modify('+1 day')->format('Y-m-d 00:00:00'));
- }
-
- if (isset($params['mergeComplementaryOrderShops'])) {
- $query
- ->joinComplementaryOrderShops();
- }
-
- if (isset($params['excludeComplementaryOrderShops']) || isset($params['mergeComplementaryOrderShops'])) {
- $query->filterIsNullMainOrderShop();
- }
-
- if (isset($params['isCircuit'])) {
- $query->filterIsNullDeliveryPointSale();
- }
-
- if (isset($params['isDepository'])) {
- $query->filterIsNotNullDeliveryPointSale();
- }
-
- if (isset($params['isOffCircuit'])) {
- $query->filterIsPointSale('devAliasHorsTournee');
- }
-
- if (isset($params['isGiftVoucher'])) {
- $query->filterIsPointSale('devAliasGiftVoucher');
- }
-
- if (isset($params['deliveryAvailability'])) {
- $deliveryAvailability = $params['deliveryAvailability'];
- $deliveryAvailabilityZone = ($deliveryAvailability instanceof DeliveryAvailabilityZone) ? $deliveryAvailability : false;
- $deliveryAvailabilityPointSale = ($deliveryAvailability instanceof DeliveryAvailabilityPointSale) ? $deliveryAvailability : false;
-
- if ($deliveryAvailabilityZone) {
- $query->filterByAvailabilityPointZone($deliveryAvailabilityZone);
- }
-
- if ($deliveryAvailabilityPointSale) {
- $query->filterByAvailabilityPointZone($deliveryAvailabilityPointSale);
- }
- } else {
- $query->joinDeliverySlotZone();
- $query->joinDeliverySlotPointSale();
- }
-
- if (isset($params['orderBy'])) {
- $sort = isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC';
- $query->orderBy($params['orderBy'], $sort);
- } else {
- $query->orderBy('.id', 'DESC');
- }
-
- if (isset($params['groupBy'])) {
- $query->groupBy($params['groupBy']);
- }
-
- if (isset($params['count']) && $params['count']) {
- return $query->count();
- }
-
- return $query->find();
- }
-
- /*
- public function getCartCurrent(SectionInterface $section, UserInterface $user = null, VisitorInterface $visitor = null)
- {
- $paramsSearchOrderShop = [];
-
- $user = $this->security->getUser();
- $visitor = $this->userUtils->getVisitorCurrent();
-
- $orderShop = null;
- $orderShopUser = null;
- $orderShopVisitor = null;
-
- if ($user) {
- $orderShopUser = $this->orderShopRepo->findCartCurrent(
- [
- 'user' => $user
- ]
- );
- }
-
- if ($visitor) {
- $orderShopVisitor = $this->orderShopRepo->findCartCurrent(
- [
- 'visitor' => $visitor
- ]
- );
- }
-
- if ($orderShopUser || $orderShopVisitor) {
- // merge
- if ($orderShopUser && $orderShopVisitor && $orderShopUser != $orderShopVisitor
- && $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts())
- && $orderShopUser->getOrderStatus()->getAlias() == OrderStatus::ALIAS_CART) {
- $orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor);
- $this->utils->addFlash(
- 'success',
- "Votre panier visiteur vient d'être fusionné avec votre panier client."
- );
- } else {
- $orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor;
- }
- // set user
- if ($orderShop && $user && !$orderShop->getUser()) {
- $orderShop->setUser($user);
- $orderShop->setVisitor(null);
- $this->em->persist($orderShop);
- $this->em->flush();
- }
- }
-
- return $orderShop;
- }*/
- }
|