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->flashBag = $flashBag; $this->parameterBag = $parameterBag; $this->router = $router; $this->orderShopSolver = $orderShopSolver; $this->reductionCartStore = $reductionCartStore; } 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->joinOrderProduct(true); return $query; } // getOrderShopsOfWeek public function getByCurrentCycle($params = [], $query = null) { return $this->getBy( array_merge( [ 'cycleNumber' => $this->orderShopSolver->getCycleNumberCurrentOrder($this->section), 'isValid' => true, ], $params ), $query ); } // getOrderShopsOfWeekByUser public function getByCurrentCycleAndUser(UserInterface $user = null, array $params = [], $query = null) { return $this->getByCurrentCycle( array_merge( [ 'user' => $user, 'cycleNumber' => $this->orderShopSolver->getCycleNumberCurrentOrder($this->section), 'excludeComplementaryOrderShops' => true ], $params ), $query ); } //public $countOrderShopsOfWeek = null; // public function countByCurrentCycle(bool $excludeComplementaryOrderShops = true, $query = null) public function countByCurrentCycle(array $params, $query = null) { return $this->countBy( array_merge( [ 'cycleNumber' => $this->orderShopSolver->getCycleNumberCurrentOrder($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(int $cycleNumber, $query = null): int { $lastOrder = $this->getOneLastValidByCycle($cycleNumber, $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 ); } public function countValidByCurrentCycle($query = null): int { return $this->countBy( [ 'cycleNumber' => $this->orderShopSolver->getCycleNumberCurrentOrder($this->section), 'isValid' => true, 'excludeComplementaryOrderShops' => true ], $query ); } // countValidOrderWithReductionCredit public function countValidWithReductionCredit( ReductionCreditInterface $reductionCredit, UserInterface $user = null, $query = null ): int { $query = $this->createDefaultQuery($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->createDefaultQuery($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 { $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 public function getOneLastValidByCycle(int $cycleNumber, $query = null): ?OrderShopInterface { $query = $this->createDefaultQuery($query); $query ->filterByCycleNumber($cycleNumber) ->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'])) { 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['cycleNumber'])) { $query->filterByCycleNumber($params['cycleNumber']); } 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['mergeComplementaryOrderShops'])) { $query ->joinComplementaryOrderShops(); } if (isset($params['excludeComplementaryOrderShops']) || isset($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->getByTypeAndUser(ReductionCreditModel::TYPE_CREDIT, $user); $reductionCreditsArray = []; foreach ($reductionCredits as $reductionCredit) { if (!$this->countValidWithReductionCredit($reductionCredit, $user) && ($reductionCredit->getSection()->getMerchant() == $this->merchant)) { $reductionCreditsArray[] = $reductionCredit; } } return $reductionCreditsArray; } public function getReductionGiftsAvailableByUser($user): array { $reductionGifts = $this->reductionCreditStore->getByTypeAndUser(ReductionCreditModel::TYPE_GIFT, $user); $reductionGiftsArray = []; foreach ($reductionGifts as $reductionGift) { if (!$this->countValidWithReductionCredit($reductionGift)) { $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->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->getSection()->getMerchant() == $this->merchant)) { $reductionCartsArray[] = $reductionCart; } } return $reductionCartsArray; } public function countValidOrderProductsOfCyclesByProducts( array $cycleNumbers, array $products, $query = null ): array { $query = $this->createDefaultQuery($query); $query ->filterByAlias(OrderStatusModel::$statusAliasAsValid) ->filterByCycleNumbers($cycleNumbers) ->filterByProducts($products) ->selectSum() ->groupBy('.cycleNumber, product.id'); return $query->find(); } public function countValidOrderProductsOfCycleByProduct(int $cycleNumber, int $productId, $query = null): ?string { $query = $this->createDefaultQuery($query); $query ->filterByAlias(OrderStatusModel::$statusAliasAsValid) ->filterByCycleNumber($cycleNumber) ->filterByProduct($productId) ->selectSumQuantityOrder() ->groupBy('.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->flashBag->add('error', 'error.reductionCredit.userNotAllow'); 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->flashBah->add('error', 'error.reductionCredit.alreadyUse'); return false; } } else { if ($this->countValidWithReductionCredit($reductionCredit) > 0) { // @TODO : déplacer la gestion du flash message //$this->flashBah->add('error', 'error.reductionCredit.alreadyUse'); return false; } } return true; } }