Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

203 lines
6.8KB

  1. <?php
  2. namespace domain\Distribution\Distribution;
  3. use domain\Producer\Producer\ProducerSolver;
  4. use domain\Subscription\Subscription\Subscription;
  5. use domain\Subscription\Subscription\SubscriptionSolver;
  6. use domain\_\AbstractRepository;
  7. class DistributionRepository extends AbstractRepository
  8. {
  9. protected DistributionRepositoryQuery $query;
  10. protected DistributionSolver $distributionSolver;
  11. protected SubscriptionSolver $subscriptionSolver;
  12. protected ProducerSolver $producerSolver;
  13. public function loadDependencies(): void
  14. {
  15. $this->loadQuery(DistributionRepositoryQuery::class);
  16. $this->distributionSolver = $this->loadService(DistributionSolver::class);
  17. $this->subscriptionSolver = $this->loadService(SubscriptionSolver::class);
  18. $this->producerSolver = $this->loadService(ProducerSolver::class);
  19. }
  20. public function getDefaultOptionsSearch(): array
  21. {
  22. return [
  23. self::WITH => [],
  24. self::JOIN_WITH => [],
  25. self::ORDER_BY => 'date ASC',
  26. self::ATTRIBUTE_ID_PRODUCER => 'distribution.id_producer'
  27. ];
  28. }
  29. public function findOneDistributionById(int $id): ?Distribution
  30. {
  31. return $this->createQuery()
  32. ->filterById($id)
  33. ->findOne();
  34. }
  35. public function findOneDistribution(string $date, bool $active = null): ?Distribution
  36. {
  37. $query = $this->createDefaultQuery();
  38. $query->filterByDate($date);
  39. if(!is_null($active)) {
  40. $query->filterByActive($active);
  41. }
  42. return $query->findOne();
  43. }
  44. public function findOneFirstDistribution(): ?Distribution
  45. {
  46. return $this->createDefaultQuery()
  47. ->orderBy('date ASC')
  48. ->findOne();
  49. }
  50. public function findOneLastDistribution(): ?Distribution
  51. {
  52. return $this->createDefaultQuery()
  53. ->orderBy('date DESC')
  54. ->findOne();
  55. }
  56. public function findDistributionsActive(): array
  57. {
  58. return $this->createDefaultQuery()
  59. ->filterByActive(true)
  60. ->orderBy('date ASC')
  61. ->find();
  62. }
  63. public function findDistributionsWithOrders()
  64. {
  65. return $this->createDefaultQuery()
  66. ->filterHasOrders()
  67. ->orderBy('date ASC')
  68. ->find();
  69. }
  70. /**
  71. * Récupère les distributions futures.
  72. */
  73. public function findDistributionsIncoming($ignoreActive = false): array
  74. {
  75. $query = $this->createDefaultQuery();
  76. if(!$ignoreActive) {
  77. $query->filterByActive(true);
  78. }
  79. $query
  80. ->filterByDateBetterThanToday()
  81. ->orderBy('date ASC');
  82. return $query->find();
  83. }
  84. /**
  85. * Recherche les distributions futures où l'abonnement peut s'appliquer.
  86. */
  87. public function findDistributionsIncomingMatchWithSubscrtiption(Subscription $subscription, bool $filterByOrderDeadline = false): array
  88. {
  89. $incomingDistributionsArray = $this->createDefaultQuery()
  90. ->filterByDateBetterThanToday()
  91. ->filterBySubscriptionDates($subscription)
  92. ->find();
  93. if($filterByOrderDeadline) {
  94. $incomingDistributionsArray = $this->distributionSolver
  95. ->filterDistributionsByDateDelay($incomingDistributionsArray);
  96. }
  97. $matchedIncomingDistributionsArray = [];
  98. foreach ($incomingDistributionsArray as $incomingDistribution) {
  99. if ($this->subscriptionSolver->isSubscriptionMatchWith($subscription, $incomingDistribution->date)) {
  100. $matchedIncomingDistributionsArray[] = $incomingDistribution;
  101. }
  102. }
  103. return $matchedIncomingDistributionsArray;
  104. }
  105. public function isDistributionDateAvailable(string $date = null): bool
  106. {
  107. if($date && strlen($date)) {
  108. $distribution = $this->findOneDistribution($date);
  109. if($distribution && $this->distributionSolver->isDistributionAvailable($distribution)) {
  110. return true;
  111. }
  112. }
  113. return false;
  114. }
  115. public function isOneDistributionWeekActive(string $date): bool
  116. {
  117. $oneDistributionWeekActive = false;
  118. $week = sprintf('%02d', date('W', strtotime($date)));
  119. $start = strtotime(date('Y', strtotime($date)) . 'W' . $week);
  120. $dateMonday = date('Y-m-d', strtotime('Monday', $start));
  121. $dateTuesday = date('Y-m-d', strtotime('Tuesday', $start));
  122. $dateWednesday = date('Y-m-d', strtotime('Wednesday', $start));
  123. $dateThursday = date('Y-m-d', strtotime('Thursday', $start));
  124. $dateFriday = date('Y-m-d', strtotime('Friday', $start));
  125. $dateSaturday = date('Y-m-d', strtotime('Saturday', $start));
  126. $dateSunday = date('Y-m-d', strtotime('Sunday', $start));
  127. $weekDistribution = Distribution::find()
  128. ->andWhere([
  129. 'id_producer' => $this->getProducerContextId(),
  130. 'active' => 1,
  131. ])
  132. ->andWhere([
  133. 'or',
  134. ['date' => $dateMonday],
  135. ['date' => $dateTuesday],
  136. ['date' => $dateWednesday],
  137. ['date' => $dateThursday],
  138. ['date' => $dateFriday],
  139. ['date' => $dateSaturday],
  140. ['date' => $dateSunday],
  141. ])
  142. ->one();
  143. if ($weekDistribution) {
  144. $oneDistributionWeekActive = true;
  145. }
  146. return $oneDistributionWeekActive;
  147. }
  148. public function findDistributionsDashboard(): array
  149. {
  150. $optionDashboardNumberDistributions = $this->producerSolver->getConfig('option_dashboard_number_distributions');
  151. $optionDashboardDateStart = $this->producerSolver->getConfig('option_dashboard_date_start');
  152. $optionDashboardDateEnd = $this->producerSolver->getConfig('option_dashboard_date_end');
  153. $dashboardNumberDistributions = $optionDashboardNumberDistributions ?: 3;
  154. $queryDistributions = Distribution::find()->with('order');
  155. if ($optionDashboardDateStart || $optionDashboardDateEnd) {
  156. if ($optionDashboardDateStart) {
  157. $queryDistributions->andWhere(['>=', 'distribution.date', $optionDashboardDateStart]);
  158. }
  159. if ($optionDashboardDateEnd) {
  160. $queryDistributions->andWhere(['<=', 'distribution.date', $optionDashboardDateEnd]);
  161. }
  162. } else {
  163. $queryDistributions->andWhere(['>=', 'distribution.date', date('Y-m-d')]);
  164. }
  165. $distributionsArray = $queryDistributions->andWhere([
  166. 'distribution.id_producer' => $this->getProducerContextId(),
  167. 'distribution.active' => 1
  168. ])
  169. ->orderBy('date ASC')
  170. ->limit($dashboardNumberDistributions)
  171. ->all();
  172. return $distributionsArray;
  173. }
  174. }