Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

181 lines
6.2KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Resolver;
  3. use App\Entity\Section\Section;
  4. use Lc\CaracoleBundle\Definition\SectionSettingDefinition;
  5. use Lc\CaracoleBundle\Model\Section\OpeningInterface;
  6. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  7. use Lc\CaracoleBundle\Repository\Order\OrderShopStore;
  8. use Lc\SovBundle\Model\User\UserInterface;
  9. use Symfony\Component\Security\Core\Security;
  10. class OpeningResolver
  11. {
  12. protected array $messages = [];
  13. protected SectionResolver $sectionResolver;
  14. protected Security $security;
  15. //protected OrderShopStore $orderShopStore;
  16. public function __construct(SectionResolver $sectionResolver, Security $security /*, OrderShopStore $orderShopStore*/)
  17. {
  18. $this->sectionResolver = $sectionResolver;
  19. $this->security = $security;
  20. //$this->orderShopStore = $orderShopStore;
  21. }
  22. public function isOpenSale(
  23. SectionInterface $section = null,
  24. UserInterface $user = null,
  25. \DateTime $date = null,
  26. string $context = null
  27. ): bool {
  28. // Initialisation
  29. $this->messages = [];
  30. if (is_null($section)) {
  31. $section = $this->sectionResolver->getCurrent();
  32. }
  33. if (is_null($user)) {
  34. $user = $this->security->getUser();
  35. }
  36. if (is_null($date)) {
  37. $date = new \DateTime();
  38. }
  39. // État des prise de commande (voir configuration de section)
  40. $orderState = $section->getSettingValue(SectionSettingDefinition::SETTING_ORDER_STATE);
  41. if ($orderState == SectionSettingDefinition::VALUE_ORDER_STATE_OPEN) {
  42. $this->addMessage('Les commandes sont ouvertes (configuration de la section).');
  43. return true;
  44. }
  45. if ($orderState == SectionSettingDefinition::VALUE_ORDER_STATE_CLOSED) {
  46. $this->addMessage('Les commandes sont fermées (configuration de la section).');
  47. return false;
  48. }
  49. // Nombre maximum de commandes par cycle (voir configuration de section)
  50. if($this->isMaximumOrderCycleAchieved($section)) {
  51. $this->addMessage('Le nombre maximum de commande a été atteint.');
  52. return false;
  53. }
  54. // Période de fermeture des commandes issue de la configuration de la section (congés)
  55. if($this->isClosingPeriod($section)) {
  56. $this->addMessage(
  57. 'Les commandes sont fermées (période de fermeture des commandes dans la configuration de la section).'
  58. );
  59. return false;
  60. }
  61. // Période d'ouverture des commandes
  62. $openings = $section->getOpenings();
  63. foreach ($openings as $opening) {
  64. if(!$opening->getGroupUser() || ($opening->getGroupUser() && $user && $user->getGroupUsers()->contains($opening->getGroupUser()))) {
  65. if ($this->isDateMatchWithOpening($date, $opening)) {
  66. $this->addMessage('Les commandes sont ouvertes (périodes d\'ouverture classique des commandes).');
  67. return true;
  68. }
  69. }
  70. }
  71. $this->addMessage('Les commandes sont fermées (périodes d\'ouverture classique des commandes).');
  72. return false;
  73. }
  74. // isHolidays
  75. public function isClosingPeriod(Section $section, \DateTime $date)
  76. {
  77. $orderClosedStart = $section->getSettingValue(SectionSettingDefinition::SETTING_ORDER_CLOSED_START);
  78. $orderClosedEnd = $section->getSettingValue(SectionSettingDefinition::SETTING_ORDER_CLOSED_END);
  79. if ($orderClosedStart && $orderClosedEnd && $date >= $orderClosedStart && $date <= $orderClosedEnd) {
  80. return true;
  81. }
  82. return false;
  83. }
  84. // isMaximumOrderWeekAchieved
  85. public function isMaximumOrderCycleAchieved(SectionInterface $section)
  86. {
  87. // @TODO : countValidByCycle
  88. //$countOrderShopCycle = $this->orderShopStore->countValidByCycle($section);
  89. $countOrderShopCycle = 0 ;
  90. $orderMaximumPerCycle = $section->getSettingValue(SectionSettingDefinition::SETTING_ORDER_MAXIMUM_PER_CYCLE);
  91. if ($orderMaximumPerCycle && $countOrderShopCycle >= $orderMaximumPerCycle) {
  92. return true;
  93. }
  94. return false;
  95. }
  96. public function isDateMatchWithOpening(\DateTime $date, OpeningInterface $opening): bool
  97. {
  98. $day = $date->format('N');
  99. $dayOpening = $opening->getDay();
  100. if ($opening->getTimeStart()) {
  101. $dateOpeningStart = clone $date;
  102. $dateOpeningStart->setTime(
  103. $opening->getTimeStart()->format('H'),
  104. $opening->getTimeStart()->format('i')
  105. );
  106. }
  107. if ($opening->getTimeEnd()) {
  108. $dateOpeningEnd = clone $date;
  109. $dateOpeningEnd->setTime(
  110. $opening->getTimeEnd()->format('H'),
  111. $opening->getTimeEnd()->format('i')
  112. );
  113. }
  114. if ($day == $dayOpening) {
  115. // Commandes ouvertes toute la journée
  116. if (!$opening->getTimeStart() && !$opening->getTimeEnd()) {
  117. return true;
  118. } // Commandes ouvertes à partir de timeStart
  119. elseif ($opening->getTimeStart() && !$opening->getTimeEnd() && $date >= $dateOpeningStart) {
  120. return true;
  121. } // Commandes ouvertes jusqu'à timeEnd
  122. elseif (!$opening->getTimeStart() && $opening->getTimeEnd() && $date < $dateOpeningEnd) {
  123. return true;
  124. } // Commandes ouvertes de timeStart à timeEnd
  125. elseif ($opening->getTimeStart() && $opening->getTimeEnd()
  126. && $date >= $dateOpeningStart && $date < $dateOpeningEnd) {
  127. return true;
  128. }
  129. }
  130. return false;
  131. }
  132. public function getDateEndCurrentSale(SectionInterface $section, $formatDate = '', $delimiterDayTime = 'à')
  133. {
  134. // @TODO : à réécrire
  135. }
  136. public function getDateBeginNextSale(SectionInterface $section, $formatDate = '', $delimiterDayTime = 'à')
  137. {
  138. // @TODO : à réécrire
  139. }
  140. public function getMessages(): array
  141. {
  142. return $this->messages;
  143. }
  144. public function addMessage(string $message): void
  145. {
  146. $this->messages[] = $message;
  147. }
  148. }