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.

154 lines
5.4KB

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