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.

OpeningResolver.php 6.9KB

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