|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?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() ?: (new \DateTime())->setTime(0, 0),
- $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;
- }
-
-
- }
|