|
- <?php
-
- namespace Lc\CaracoleBundle\Resolver;
-
- use App\Entity\Section\Section;
- use Lc\CaracoleBundle\Definition\SectionSettingDefinition;
- use Lc\CaracoleBundle\Model\Section\OpeningInterface;
- use Lc\CaracoleBundle\Model\Section\SectionInterface;
- use Lc\CaracoleBundle\Repository\Order\OrderShopStore;
- use Lc\SovBundle\Model\User\UserInterface;
- use Symfony\Component\Security\Core\Security;
-
- class OpeningResolver
- {
- protected array $messages = [];
- protected SectionResolver $sectionResolver;
- protected Security $security;
- protected OrderShopStore $orderShopStore;
-
- public function __construct(SectionResolver $sectionResolver, Security $security, OrderShopStore $orderShopStore)
- {
- $this->sectionResolver = $sectionResolver;
- $this->security = $security;
- $this->orderShopStore = $orderShopStore;
- }
-
- public function isOpenSale(
- SectionInterface $section,
- UserInterface $user = null,
- \DateTime $date = null,
- string $context = null
- ): bool {
- // Initialisation
- $this->messages = [];
-
- if (is_null($date)) {
- $date = new \DateTime();
- }
-
- // État des prise de commande (voir configuration de section)
- $orderState = $section->getSettingValue(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->isMaximumOrderCycleAchieved($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;
- }
-
- // isHolidays
- public function isClosingPeriod(SectionInterface $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)
- {
-
- $countOrderShopCycle = $this->orderShopStore->countValidByCurrentCycle();
- $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
- {
- $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;
- }
-
- public function getDateEndCurrentSale(SectionInterface $section, $formatDate = '', $delimiterDayTime = 'à')
- {
- // @TODO : à réécrire
- }
-
- public function getDateBeginNextSale(SectionInterface $section, $formatDate = '', $delimiterDayTime = 'à')
- {
- // @TODO : à réécrire
- }
-
- public function getMessages(): array
- {
- return $this->messages;
- }
-
- public function addMessage(string $message): void
- {
- $this->messages[] = $message;
- }
- }
|