query = $query; $this->entityManager = $entityManager; $this->priceSolver = $priceSolver; $this->documentBuilder = $documentBuilder; $this->reductionCreditStore = $reductionCreditStore; $this->reductionCartSolver = $reductionCartSolver; $this->sectionStore = $sectionStore; $this->orderProductStore = $orderProductStore; $this->merchantStore = $merchantStore; $this->flashBagTranslator = $flashBagTranslator; $this->parameterBag = $parameterBag; $this->router = $router; $this->orderShopSolver = $orderShopSolver; $this->reductionCartStore = $reductionCartStore; $this->distributionBuilder = $distributionBuilder; $this->settingSolver = $settingSolver; } public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface { //$query->orderBy('id', 'DESC'); return $query; } public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface { $this ->addFilterBySectionOptionnal($query) ->addFilterByMerchantViaSectionOptionnal($query); return $query; } public function relationsDefault(RepositoryQueryInterface $query): RepositoryQueryInterface { $query->joinOrderProducts(true); return $query; } // getOrderShopsOfWeek //getByCurrentCycle public function getByCurrentDistribution($params = [], $query = null) { return $this->getBy( array_merge( [ 'distribution' => $this->distributionBuilder->guessCurrentDistributionOrder( $this->section ), 'isValid' => true, ], $params ), $query ); } // getOrderShopsOfWeekByUser //getByCurrentCycleAndUser public function getByCurrentDistributionAndUser(UserInterface $user = null, array $params = [], $query = null) { return $this->getByCurrentDistribution( array_merge( [ 'user' => $user, 'excludeComplementaryOrderShops' => true ], $params ), $query ); } //public $countOrderShopsOfWeek = null; // public function countByCurrentCycle(bool $excludeComplementaryOrderShops = true, $query = null) //countByCurrentCycle public function countByCurrentDistribution(array $params, $query = null) { return $this->countBy( array_merge( [ 'distribution' => $this->distributionBuilder->guessCurrentDistributionOrder( $this->section ), 'excludeComplementaryOrderShops' => isset($params['excludeComplementaryOrderShops']) ?? true, ], $params ), $query ); // @TODO : optimisation à remettre en place /*if (is_null($this->countOrderShopsOfWeek)) { $this->countOrderShopsOfWeek = $this->getByCurrentCycle( $section, [ 'count' => true, 'excludeComplementaryOrderShops' => $excludeComplementaryOrderShops ] ); } return $this->countOrderShopsOfWeek;*/ } // getNextWeekId public function getNextCycleId(Distribution $distribution, $query = null): int { $lastOrder = $this->getOneLastValidByDistribution($distribution, $query); if ($lastOrder) { return intval($lastOrder->getCycleId() + 1); } else { return 1; } } public function getNextIdValidOrder($query = null): int { $lastOrder = $this->getOneLastValid($query); if ($lastOrder) { return intval($lastOrder->getIdValidOrder() + 1); } else { return 1; } } // countValidOrderShopByUserAllMerchant public function countValidByUserAllMerchant($user, $query = null): int { return $this->countBy( [ 'user' => $user, 'isValid' => true, // @TODO : à tester 'isMerchantOnline' => true, 'excludeComplementaryOrderShops' => true ], $query ); } public function countValidByUser(UserInterface $user = null, $query = null): int { return $this->countBy( [ 'user' => $user, 'isValid' => true, 'excludeComplementaryOrderShops' => true ], $query ); } //countValidByCurrentCycle public function countValidByCurrentDistribution($query = null): int { return $this->countBy( [ 'distribution' => $this->distributionBuilder->guessCurrentDistributionOrder($this->section), 'isValid' => true, 'excludeComplementaryOrderShops' => true ], $query ); } // countValidOrderWithReductionCredit public function countValidWithReductionCredit( ReductionCreditInterface $reductionCredit, UserInterface $user = null, $query = null ): int { //TODO vérifier que ne pas utiliser createDefaultQuery est pertinent $query = $this->createQuery($query); if ($user) { $query->filterByUser($user); } $query ->selectCount() ->filterByReductionCredit($reductionCredit) ->filterByStatus(OrderStatusModel::$statusAliasAsValid); return $query->count(); } // countValidOrderWithReductionCart public function countValidWithReductionCart( ReductionCartInterface $reductionCart, $query = null ): int { $query = $this->createQuery($query); $query ->selectCount() ->filterByReductionCart($reductionCart) ->filterByStatus(OrderStatusModel::$statusAliasAsValid); return $query->count(); } // countValidOrderWithReductionCartPerUser public function countValidWithReductionCartByUser( ReductionCartInterface $reductionCart, UserInterface $user, $query = null ): int { $query = $this->createDefaultQuery($query); $query ->selectCount() ->filterByUser($user) ->filterByReductionCart($reductionCart) ->filterByStatus(OrderStatusModel::$statusAliasAsValid); return $query->count(); } // findCartCurrent public function getOneCartCurrent( UserInterface $user = null, VisitorInterface $visitor = null, $query = null ): ?OrderShopInterface { if (is_null($user) && is_null($visitor)) { return null; } $query = $this->createDefaultQuery($query); if (!is_null($user)) { $query->filterByUser($user); } else { if (!is_null($visitor)) { $query->filterByVisitor($visitor); } } $query ->selectOrderReductionCarts() ->filterByStatus(OrderStatusModel::$statusAliasAsCart); return $query->findOne(); } // findLastOrderValidOfWeek //getOneLastValidByCycle public function getOneLastValidByDistribution( DistributionInterface $distribution, $query = null ): ?OrderShopInterface { $query = $this->createDefaultQuery($query); $query ->filterByDistribution($distribution) ->filterByStatus(OrderStatusModel::$statusAliasAsValid) ->filterIsNotComplementaryOrderShop() ->orderBy('.cycleId', 'DESC'); return $query->findOne(); } //findLastOrderValid public function getOneLastValid($query = null): ?OrderShopInterface { $query = $this->createDefaultQuery($query); $query ->filterByStatus(OrderStatusModel::$statusAliasAsValid) ->filterIsNotComplementaryOrderShop() ->orderBy('.idValidOrder', 'DESC'); return $query->findOne(); } public function countBy(array $params = [], $query = null) { $query = $this->createDefaultQuery($query); $query->selectCount(); $this->applyGetByFilters($params, $query); return $query->count(); } public function getBy(array $params = [], $query = null): array { $query = $this->createDefaultQuery($query); $this->applyGetByFilters($params, $query); $orderShops = $query->find(); if (isset($params['mergeComplementaryOrderShops']) && $params['mergeComplementaryOrderShops'] == true) { foreach ($orderShops as $orderShop) { $this->orderShopSolver->mergeComplentaryOrderShops($orderShop); } } return $orderShops; } protected function applyGetByFilters(array $params, $query) { if (isset($params['isMerchantOnline'])) { $query->filterIsMerchantOnline(); } 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['distribution'])) { $query->filterByDistribution($params['distribution']); } if (isset($params['distributions'])) { $query->filterByDistributions($params['distributions']); } if (isset($params['isCart'])) { $query->filterByStatus(OrderStatusModel::$statusAliasAsCart); } if (isset($params['isValid'])) { $query->filterByStatus(OrderStatusModel::$statusAliasAsValid); } if (isset($params['isWaitingDelivery'])) { $query->filterByStatus(OrderStatusModel::$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['minimumTomorrowDelivery'])) { $query->filterMinimumTomorrowDelivery(); } if (isset($params['mergeComplementaryOrderShops']) && $params['mergeComplementaryOrderShops']) { $query ->joinComplementaryOrderShops(); } if ((isset($params['excludeComplementaryOrderShops']) && $params['excludeComplementaryOrderShops']) || (isset($params['mergeComplementaryOrderShops']) && $params['mergeComplementaryOrderShops'])) { $query->filterIsNullMainOrderShop(); } if (isset($params['joinProductFamily'])) { $query->joinProductFamily(true); } 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']); } return $query; } public function isReductionGiftUsed(ReductionCreditInterface $reductionGift, $query = null) { if ($this->countValidWithReductionCredit($reductionGift, null, $query)) { return true; } else { return false; } } public function isReductionCreditUsed( ReductionCreditInterface $reductionCredit, UserInterface $user = null, $query = null ) { if ($this->countValidWithReductionCredit($reductionCredit, $user, $query)) { return true; } else { return false; } } public function getReductionCreditsAvailableByUser(UserInterface $user): array { $reductionCredits = $this->reductionCreditStore ->setMerchant($this->merchant) ->getByTypeAndUser(ReductionCreditModel::TYPE_CREDIT, $user); $reductionCreditsArray = []; foreach ($reductionCredits as $reductionCredit) { if (!$this->countValidWithReductionCredit($reductionCredit, $user)) { $reductionCreditsArray[] = $reductionCredit; } } return $reductionCreditsArray; } public function getReductionGiftsAvailableByUser(UserInterface $user): array { $reductionGifts = $this->reductionCreditStore ->setMerchant($this->merchant) ->getByTypeAndUser(ReductionCreditModel::TYPE_GIFT, $user); $reductionGiftsArray = []; foreach ($reductionGifts as $reductionGift) { if (!$this->countValidWithReductionCredit($reductionGift, $user)) { $reductionGiftsArray[] = $reductionGift; } } return $reductionGiftsArray; } // getReductionCartRemainingQuantity public function getReductionCartRemainingQuantity(ReductionCartInterface $reductionCart): float { return $reductionCart->getAvailableQuantity() - $this->countValidWithReductionCart( $reductionCart ); } // getReductionCartUsedQuantityPerUser public function getReductionCartUsedQuantityByUser( ReductionCartInterface $reductionCart, UserInterface $user ): float { return $this->countValidWithReductionCartByUser($reductionCart, $user); } // getReductionCartUsedQuantity public function getReductionCartUsedQuantity(ReductionCartInterface $reductionCart): float { return $this->countValidWithReductionCart($reductionCart); } // getReductionCartRemainingQuantityPerUser public function getReductionCartRemainingQuantityByUser( ReductionCartInterface $reductionCart, UserInterface $user ): float { if ($reductionCart->getAvailableQuantityPerUser()) { return $reductionCart->getAvailableQuantityPerUser() - $this->countValidWithReductionCartByUser( $reductionCart, $user ); } return false; } // findAllAvailableForUser / getReductionCartsAvailableByUser public function getReductionCartAvailableByUser(UserInterface $user, $query = null) { $reductionCarts = $this->reductionCartStore ->setMerchant($this->merchant) ->getOnline(); $reductionCartsArray = []; foreach ($reductionCarts as $reductionCart) { if ($this->reductionCartSolver->matchWithUser($reductionCart, $user) && $this->reductionCartSolver->matchWithGroupUser($reductionCart, $user) && $this->getReductionCartRemainingQuantityByUser($reductionCart, $user) && ($reductionCart->getUsers()->count() > 0 || $reductionCart->getGroupUsers()->count() > 0) && (!$this->merchant || $reductionCart->getMerchant() == $this->merchant)) { $reductionCartsArray[] = $reductionCart; } } return $reductionCartsArray; } //countValidOrderProductsOfCyclesByProducts public function countValidOrderProductsOfDistributionsByProducts( array $distributions, array $products, $query = null ): array { $query = $this->createDefaultQuery($query); $query ->filterByAlias(OrderStatusModel::$statusAliasAsValid) ->filterByDistributions($distributions) ->filterByProducts($products) ->selectSum() ->groupBy('distribution.cycleNumber, product.id'); return $query->find(); } //countValidOrderProductsOfCycleByProduct public function countValidOrderProductsOfDistributionByProduct( DistributionInterface $distribution, ProductInterface $product, $query = null ): ?string { //TODO attention à vérifier $query = $this->createQuery($query); $query ->filterByAlias(OrderStatusModel::$statusAliasAsValid) ->filterByDistribution($distribution) ->filterByProduct($product) ->selectSumQuantityOrder() ->joinDistribution() ->groupBy('distribution.cycleNumber, product.id'); $result = $query->findOne(); if ($result) { return $result['quantity']; } return null; } public function isReductionCreditAllowAddToOrder( OrderShopInterface $orderShop, ReductionCreditInterface $reductionCredit ) { $user = $orderShop->getUser(); // appartient à l'utilisateur if (!$reductionCredit->getUsers()->contains($user)) { // @TODO : déplacer la gestion du flash message $this->flashBagTranslator->add('error', 'userNotAllow', 'ReductionCredit'); return false; } // n'a pas été utilisé if ($reductionCredit->getType() == ReductionCreditModel::TYPE_CREDIT) { if ($this->countValidWithReductionCredit($reductionCredit, $user) > 0) { // @TODO : déplacer la gestion du flash message $this->flashBagTranslator->add('error', 'alreadyUse', 'ReductionCredit'); return false; } } else { if ($this->countValidWithReductionCredit($reductionCredit) > 0) { // @TODO : déplacer la gestion du flash message $this->flashBagTranslator->add('error', 'alreadyUse', 'ReductionCredit'); return false; } } return true; } }