sectionResolver = $sectionResolver; $this->security = $security; $this->orderShopStore = $orderShopStore; $this->settingSolver = $settingSolver; $this->openingStore = $openingStore; $this->openingSolver = $openingSolver; } public function isOpenSale( SectionInterface $section = null, UserInterface $user = null, string $context = null ): bool { // Initialisation $this->messages = []; if (is_null($section)) { $section = $this->sectionResolver->getCurrent(true); } if (is_null($user)) { $user = $this->security->getUser(); } $date = new \DateTime(); // État des prise de commande (voir configuration de section) $orderState = $this->settingSolver->getSettingValue($section, SectionSettingDefinition::SETTING_ORDER_STATE); if ($orderState == SectionSettingDefinition::VALUE_ORDER_STATE_OPEN) { $this->addMessage('Les commandes sont ouvertes (configuration de la section).'); return true; } if ($orderState == SectionSettingDefinition::VALUE_ORDER_STATE_CLOSED) { $this->addMessage('Les commandes sont fermées (configuration de la section).'); return false; } // Nombre maximum de commandes par cycle (voir configuration de section) if ($this->isMaximumOrderDistributionAchieved($section)) { $this->addMessage('Le nombre maximum de commande a été atteint.'); return false; } // Période de fermeture des commandes issue de la configuration de la section (congés) if ($this->isClosingPeriod($section, $date)) { $this->addMessage( 'Les commandes sont fermées (période de fermeture des commandes dans la configuration de la section).' ); return false; } // Période d'ouverture des commandes $openings = $section->getOpenings(); foreach ($openings as $opening) { if (!$opening->getGroupUser() || ($opening->getGroupUser() && $user && $user->getGroupUsers()->contains( $opening->getGroupUser() ))) { if ($this->isDateMatchWithOpening($date, $opening)) { $this->addMessage('Les commandes sont ouvertes (périodes d\'ouverture classique des commandes).'); return true; } } } $this->addMessage('Les commandes sont fermées (périodes d\'ouverture classique des commandes).'); return false; } public function isOpenFullTime(SectionInterface $section = null) { if (is_null($section)) { $section = $this->sectionResolver->getCurrent(); } $orderState = $this->settingSolver->getSettingValue($section, SectionSettingDefinition::SETTING_ORDER_STATE); if ($orderState == SectionSettingDefinition::VALUE_ORDER_STATE_OPEN) { return true; } return false; } // isHolidays public function isClosingPeriod(SectionInterface $section = null, \DateTime $date = null) { if (is_null($section)) { $section = $this->sectionResolver->getCurrent(); } if(is_null($date)) { $date = new \DateTime(); } $orderClosedStart = $this->settingSolver->getSettingValue( $section, SectionSettingDefinition::SETTING_ORDER_CLOSED_START ); $orderClosedEnd = $this->settingSolver->getSettingValue( $section, SectionSettingDefinition::SETTING_ORDER_CLOSED_END ); if ($orderClosedStart && $orderClosedEnd && $date >= $orderClosedStart && $date <= $orderClosedEnd) { return true; } return false; } // isMaximumOrderWeekAchieved //isMaximumOrderCycleAchieved public function isMaximumOrderDistributionAchieved(SectionInterface $section) { $countOrderShopCycle = $this->orderShopStore ->setSection($section) ->countValidByCurrentDistribution(); $orderMaximumPerCycle = $this->settingSolver->getSettingValue( $section, SectionSettingDefinition::SETTING_ORDER_MAXIMUM_PER_CYCLE ); if ($orderMaximumPerCycle && $countOrderShopCycle >= $orderMaximumPerCycle) { return true; } return false; } public function isDateMatchWithOpening(\DateTime $date, OpeningInterface $opening): bool { $day = $date->format('N'); $dayOpening = $opening->getDay(); if ($opening->getTimeStart()) { $dateOpeningStart = clone $date; $dateOpeningStart->setTime( $opening->getTimeStart()->format('H'), $opening->getTimeStart()->format('i') ); } if ($opening->getTimeEnd()) { $dateOpeningEnd = clone $date; $dateOpeningEnd->setTime( $opening->getTimeEnd()->format('H'), $opening->getTimeEnd()->format('i') ); } if ($day == $dayOpening) { // Commandes ouvertes toute la journée if (!$opening->getTimeStart() && !$opening->getTimeEnd()) { return true; } // Commandes ouvertes à partir de timeStart elseif ($opening->getTimeStart() && !$opening->getTimeEnd() && $date >= $dateOpeningStart) { return true; } // Commandes ouvertes jusqu'à timeEnd elseif (!$opening->getTimeStart() && $opening->getTimeEnd() && $date < $dateOpeningEnd) { return true; } // Commandes ouvertes de timeStart à timeEnd elseif ($opening->getTimeStart() && $opening->getTimeEnd() && $date >= $dateOpeningStart && $date < $dateOpeningEnd) { return true; } } return false; } // getDateEndCurrentSale public function getFormatedDateClosingCurrentSale( SectionInterface $section = null, $formatDate = '', $delimiterDayTime = 'à' ) { if (is_null($section)) { $section = $this->sectionResolver->getCurrent(); } $date = new \DateTime(); $openingArray = $this->openingStore ->setSection($section) ->get(); if ($this->isOpenSale($section)) { $openingClosing = $this->openingSolver->getNextOpeningOfClosing($date, $openingArray); if ($openingClosing) { return $this->openingSolver->getFormatedDateByFormatAndDelimiterDayTime( $date, $openingClosing->getTimeEnd(), $formatDate, $delimiterDayTime ); } } return ''; } // getDateBeginNextSale public function getFormatedDateOpeningNextSale( SectionInterface $section = null, $formatDate = '', $delimiterDayTime = 'à' ) { if (is_null($section)) { $section = $this->sectionResolver->getCurrent(); } $date = new \DateTime(); if($this->isClosingPeriod($section)) { $date = $this->settingSolver->getSettingValue( $section, SectionSettingDefinition::SETTING_ORDER_CLOSED_END ); } $openingArray = $this->openingStore ->setSection($section) ->get(); if (!$this->isOpenSale($section)) { $opening = $this->openingSolver->getNextOpeningOfOpening($date, $openingArray); if ($opening) { return $this->openingSolver->getFormatedDateByFormatAndDelimiterDayTime( $date, $opening->getTimeStart() ?: (new \DateTime())->setTime(0, 0), $formatDate, $delimiterDayTime ); } } return ''; } public function isOpenSaleOnlyComplementaryOrders(SectionInterface $section = null, UserInterface $user = null) { if (is_null($section)) { $section = $this->sectionResolver->getCurrent(); } if (is_null($user)) { $user = $this->security->getUser(); } // @TODO : ajouter une option dans les sections (permettre les commandes complémentaires ou non) $orderShopsUser = $this->orderShopStore->setSection($section)->getByCurrentDistributionAndUser($user); return $this->isOpenSale($section, $user) && $this->isMaximumOrderDistributionAchieved($section) && count($orderShopsUser) > 0; } public function getMessageOpeningNextSale(SectionInterface $section): ?string { $openingTextClosed = $this->settingSolver->getSettingValue( $section, SectionSettingDefinition::SETTING_OPENING_TEXT_CLOSED ); if ($openingTextClosed && strlen($openingTextClosed) > 0) { return $openingTextClosed ; } $dateOpeningNextSale = $this->getFormatedDateOpeningNextSale($section); if ($dateOpeningNextSale && strlen($dateOpeningNextSale) > 0) { return 'Réouverture aux commandes le '.$dateOpeningNextSale.'' ; } return null; } public function getMessages(): array { return $this->messages; } public function addMessage(string $message): void { $this->messages[] = $message; } }