|
- <?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\Model\Section\SectionModel;
- use Lc\CaracoleBundle\Repository\Order\OrderShopStore;
- use Lc\CaracoleBundle\Repository\Section\OpeningStore;
- use Lc\CaracoleBundle\Solver\Section\OpeningSolver;
- use Lc\SovBundle\Model\User\UserInterface;
- use Lc\SovBundle\Solver\Setting\SettingSolver;
- use Symfony\Component\Security\Core\Security;
-
- class OpeningResolver
- {
- const OPENING_CONTEXT_FRONTEND = 'frontend';
- const OPENING_CONTEXT_BACKEND = 'backend';
-
- protected array $messages = [];
- protected SectionResolver $sectionResolver;
- protected Security $security;
- protected OrderShopStore $orderShopStore;
- protected SettingSolver $settingSolver;
- protected OpeningStore $openingStore;
- protected OpeningSolver $openingSolver;
-
- public function __construct(
- SectionResolver $sectionResolver,
- Security $security,
- OrderShopStore $orderShopStore,
- SettingSolver $settingSolver,
- OpeningStore $openingStore,
- OpeningSolver $openingSolver
- ) {
- $this->sectionResolver = $sectionResolver;
- $this->security = $security;
- $this->orderShopStore = $orderShopStore;
- $this->settingSolver = $settingSolver;
- $this->openingStore = $openingStore;
- $this->openingSolver = $openingSolver;
- }
-
- public function isOpenSale(
- SectionInterface $section = null,
- UserInterface $user = null,
- string $context = null
- ): bool {
- // Initialisation
- $this->messages = [];
-
- if (is_null($section)) {
- $section = $this->sectionResolver->getCurrent();
- }
-
- if (is_null($user)) {
- $user = $this->security->getUser();
- }
-
- $date = new \DateTime();
-
- // État des prise de commande (voir configuration de section)
- $orderState = $this->settingSolver->getSettingValue($section, 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->isMaximumOrderDistributionAchieved($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;
- }
-
- public function isOpenFullTime(SectionInterface $section = null)
- {
- if (is_null($section)) {
- $section = $this->sectionResolver->getCurrent();
- }
-
- $orderState = $this->settingSolver->getSettingValue($section, SectionSettingDefinition::SETTING_ORDER_STATE);
-
- if ($orderState == SectionSettingDefinition::VALUE_ORDER_STATE_OPEN) {
- return true;
- }
-
- return false;
- }
-
- // isHolidays
- public function isClosingPeriod(SectionInterface $section = null)
- {
- if (is_null($section)) {
- $section = $this->sectionResolver->getCurrent();
- }
-
- $date = new \DateTime();
-
- $orderClosedStart = $this->settingSolver->getSettingValue(
- $section,
- SectionSettingDefinition::SETTING_ORDER_CLOSED_START
- );
- $orderClosedEnd = $this->settingSolver->getSettingValue(
- $section,
- SectionSettingDefinition::SETTING_ORDER_CLOSED_END
- );
-
- if ($orderClosedStart && $orderClosedEnd && $date >= $orderClosedStart && $date <= $orderClosedEnd) {
- return true;
- }
-
- return false;
- }
-
- // isMaximumOrderWeekAchieved
- //isMaximumOrderCycleAchieved
- public function isMaximumOrderDistributionAchieved(SectionInterface $section)
- {
- $countOrderShopCycle = $this->orderShopStore
- ->setSection($section)
- ->countValidByCurrentDistribution();
-
- $orderMaximumPerCycle = $this->settingSolver->getSettingValue(
- $section,
- 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;
- }
-
- // getDateEndCurrentSale
- public function getFormatedDateClosingCurrentSale(
- SectionInterface $section = null,
- $formatDate = '',
- $delimiterDayTime = 'à'
- ) {
- if (is_null($section)) {
- $section = $this->sectionResolver->getCurrent();
- }
-
- $date = new \DateTime();
- $openingArray = $this->openingStore
- ->setSection($section)
- ->get();
-
- if ($this->isOpenSale($section)) {
- $openingClosing = $this->openingSolver->getNextOpeningOfClosing($date, $openingArray);
- if ($openingClosing) {
- return $this->openingSolver->getFormatedDateByFormatAndDelimiterDayTime(
- $date,
- $openingClosing->getTimeEnd(),
- $formatDate,
- $delimiterDayTime
- );
- }
- }
-
- return '';
- }
-
- // getDateBeginNextSale
- public function getFormatedDateOpeningNextSale(
- SectionInterface $section = null,
- $formatDate = '',
- $delimiterDayTime = 'à'
- ) {
- if (is_null($section)) {
- $section = $this->sectionResolver->getCurrent();
- }
-
- $date = new \DateTime();
- $openingArray = $this->openingStore
- ->setSection($section)
- ->get();
-
- if (!$this->isOpenSale($section)) {
- $opening = $this->openingSolver->getNextOpeningOfOpening($date, $openingArray);
- if ($opening) {
- return $this->openingSolver->getFormatedDateByFormatAndDelimiterDayTime(
- $date,
- $opening->getTimeStart(),
- $formatDate,
- $delimiterDayTime
- );
- }
- }
-
- return '';
- }
-
- public function isOpenSaleOnlyComplementaryOrders(Section $section = null, UserInterface $user = null)
- {
- if (is_null($section)) {
- $section = $this->sectionResolver->getCurrent();
- }
-
- if (is_null($user)) {
- $user = $this->security->getUser();
- }
-
- // @TODO : ajouter une option dans les sections (permettre les commandes complémentaires ou non)
- $orderShopsUser = $this->orderShopStore->setSection($section)->getByCurrentDistributionAndUser($user);
-
- return $this->isOpenSale($section, $user)
- && $this->isMaximumOrderDistributionAchieved($section)
- && count($orderShopsUser) > 0;
- }
-
- public function getMessages(): array
- {
- return $this->messages;
- }
-
- public function addMessage(string $message): void
- {
- $this->messages[] = $message;
- }
-
-
- }
|