You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
6.0KB

  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,
  24. UserInterface $user = null,
  25. \DateTime $date = null,
  26. string $context = null
  27. ): bool {
  28. // Initialisation
  29. $this->messages = [];
  30. if (is_null($date)) {
  31. $date = new \DateTime();
  32. }
  33. // État des prise de commande (voir configuration de section)
  34. $orderState = $section->getSettingValue(SectionSettingDefinition::SETTING_ORDER_STATE);
  35. if ($orderState == SectionSettingDefinition::VALUE_ORDER_STATE_OPEN) {
  36. $this->addMessage('Les commandes sont ouvertes (configuration de la section).');
  37. return true;
  38. }
  39. if ($orderState == SectionSettingDefinition::VALUE_ORDER_STATE_CLOSED) {
  40. $this->addMessage('Les commandes sont fermées (configuration de la section).');
  41. return false;
  42. }
  43. // Nombre maximum de commandes par cycle (voir configuration de section)
  44. if($this->isMaximumOrderCycleAchieved($section)) {
  45. $this->addMessage('Le nombre maximum de commande a été atteint.');
  46. return false;
  47. }
  48. // Période de fermeture des commandes issue de la configuration de la section (congés)
  49. if($this->isClosingPeriod($section, $date)) {
  50. $this->addMessage(
  51. 'Les commandes sont fermées (période de fermeture des commandes dans la configuration de la section).'
  52. );
  53. return false;
  54. }
  55. // Période d'ouverture des commandes
  56. $openings = $section->getOpenings();
  57. foreach ($openings as $opening) {
  58. if(!$opening->getGroupUser() || ($opening->getGroupUser() && $user && $user->getGroupUsers()->contains($opening->getGroupUser()))) {
  59. if ($this->isDateMatchWithOpening($date, $opening)) {
  60. $this->addMessage('Les commandes sont ouvertes (périodes d\'ouverture classique des commandes).');
  61. return true;
  62. }
  63. }
  64. }
  65. $this->addMessage('Les commandes sont fermées (périodes d\'ouverture classique des commandes).');
  66. return false;
  67. }
  68. // isHolidays
  69. public function isClosingPeriod(SectionInterface $section, \DateTime $date)
  70. {
  71. $orderClosedStart = $section->getSettingValue(SectionSettingDefinition::SETTING_ORDER_CLOSED_START);
  72. $orderClosedEnd = $section->getSettingValue(SectionSettingDefinition::SETTING_ORDER_CLOSED_END);
  73. if ($orderClosedStart && $orderClosedEnd && $date >= $orderClosedStart && $date <= $orderClosedEnd) {
  74. return true;
  75. }
  76. return false;
  77. }
  78. // isMaximumOrderWeekAchieved
  79. public function isMaximumOrderCycleAchieved(SectionInterface $section)
  80. {
  81. $countOrderShopCycle = $this->orderShopStore->countValidByCurrentCycle();
  82. $orderMaximumPerCycle = $section->getSettingValue(SectionSettingDefinition::SETTING_ORDER_MAXIMUM_PER_CYCLE);
  83. if ($orderMaximumPerCycle && $countOrderShopCycle >= $orderMaximumPerCycle) {
  84. return true;
  85. }
  86. return false;
  87. }
  88. public function isDateMatchWithOpening(\DateTime $date, OpeningInterface $opening): bool
  89. {
  90. $day = $date->format('N');
  91. $dayOpening = $opening->getDay();
  92. if ($opening->getTimeStart()) {
  93. $dateOpeningStart = clone $date;
  94. $dateOpeningStart->setTime(
  95. $opening->getTimeStart()->format('H'),
  96. $opening->getTimeStart()->format('i')
  97. );
  98. }
  99. if ($opening->getTimeEnd()) {
  100. $dateOpeningEnd = clone $date;
  101. $dateOpeningEnd->setTime(
  102. $opening->getTimeEnd()->format('H'),
  103. $opening->getTimeEnd()->format('i')
  104. );
  105. }
  106. if ($day == $dayOpening) {
  107. // Commandes ouvertes toute la journée
  108. if (!$opening->getTimeStart() && !$opening->getTimeEnd()) {
  109. return true;
  110. } // Commandes ouvertes à partir de timeStart
  111. elseif ($opening->getTimeStart() && !$opening->getTimeEnd() && $date >= $dateOpeningStart) {
  112. return true;
  113. } // Commandes ouvertes jusqu'à timeEnd
  114. elseif (!$opening->getTimeStart() && $opening->getTimeEnd() && $date < $dateOpeningEnd) {
  115. return true;
  116. } // Commandes ouvertes de timeStart à timeEnd
  117. elseif ($opening->getTimeStart() && $opening->getTimeEnd()
  118. && $date >= $dateOpeningStart && $date < $dateOpeningEnd) {
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. public function getDateEndCurrentSale(SectionInterface $section, $formatDate = '', $delimiterDayTime = 'à')
  125. {
  126. // @TODO : à réécrire
  127. }
  128. public function getDateBeginNextSale(SectionInterface $section, $formatDate = '', $delimiterDayTime = 'à')
  129. {
  130. // @TODO : à réécrire
  131. }
  132. public function getMessages(): array
  133. {
  134. return $this->messages;
  135. }
  136. public function addMessage(string $message): void
  137. {
  138. $this->messages[] = $message;
  139. }
  140. }