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.

177 lines
4.9KB

  1. <?php
  2. namespace domain\PointSale\PointSale;
  3. use domain\Distribution\Distribution\Distribution;
  4. use domain\Producer\Producer\Producer;
  5. use domain\User\User\User;
  6. use domain\_\AbstractRepository;
  7. class PointSaleRepository extends AbstractRepository
  8. {
  9. protected PointSaleRepositoryQuery $query;
  10. public function loadDependencies(): void
  11. {
  12. $this->loadQuery(PointSaleRepositoryQuery::class);
  13. }
  14. public function getDefaultOptionsSearch(): array
  15. {
  16. return [
  17. self::WITH => [],
  18. self::JOIN_WITH => [],
  19. self::ORDER_BY => 'is_bread_box ASC, name ASC',
  20. self::ATTRIBUTE_ID_PRODUCER => 'point_sale.id_producer'
  21. ];
  22. }
  23. public function findOnePointSaleById(int $id): ?PointSale
  24. {
  25. return $this->createDefaultQuery()
  26. ->filterById($id)
  27. ->findOne();
  28. }
  29. public function findOnePointSaleByIdUser(int $idUser): ?PointSale
  30. {
  31. return $this->createDefaultQuery()
  32. ->filterIsOnline()
  33. ->filterByIdUser($idUser)
  34. ->findOne();
  35. }
  36. public function findOnePointSaleByName(string $name): ?PointSale
  37. {
  38. return $this->createDefaultQuery()
  39. ->filterIsOnline()
  40. ->filterByName($name)
  41. ->findOne();
  42. }
  43. public function queryPointSalesByDistribution(Distribution $distribution)
  44. {
  45. return $this->createDefaultQuery()
  46. ->filterIsOnline()
  47. ->with(['pointSaleDistribution' => function ($q) use ($distribution) {
  48. $q->where(['id_distribution' => $distribution->id]);
  49. }]);
  50. }
  51. public function findPointSalesByDistribution(Distribution $distribution)
  52. {
  53. return $this->queryPointSalesByDistribution($distribution)->find();
  54. }
  55. public function findPointSalesByDistributionAsArray(Distribution $distribution)
  56. {
  57. return $this->queryPointSalesByDistribution($distribution)->asArray()->find();
  58. }
  59. public function findOnePointSaleDefaultByDistribution(Distribution $distribution): ?PointSale
  60. {
  61. $pointSalesArray = $this->findPointSalesByDistribution($distribution);
  62. foreach($pointSalesArray as $pointSale) {
  63. if($pointSale->default) {
  64. return $pointSale;
  65. }
  66. }
  67. return null;
  68. }
  69. /**
  70. * Retourne le nombre de points de vente pour le producteur courant.
  71. */
  72. public function countPointSales(): int
  73. {
  74. return $this->createDefaultQuery()
  75. ->filterIsOnline()
  76. ->count();
  77. }
  78. public function findPointSales()
  79. {
  80. return $this->createDefaultQuery()
  81. ->filterIsOnline()
  82. ->find();
  83. }
  84. public function findPointSalesByUserAccess(User $user)
  85. {
  86. return $this->createDefaultQuery()
  87. ->filterIsOnline()
  88. ->filterByUserAccess($user)
  89. ->find();
  90. }
  91. public function countPointSalesPublic()
  92. {
  93. return $this->queryPointSalesPublic($this->getProducerContext())->count();
  94. }
  95. public function queryPointSalesPublic(Producer $producer)
  96. {
  97. return $this->createDefaultQuery()
  98. ->filterIsOnline()
  99. ->filterByRestrictedAccess(0)
  100. ->orderBy('code ASC, restricted_access ASC, is_bread_box ASC, `default` DESC, name ASC')
  101. ->query();
  102. }
  103. public function populatePointSaleDropdownList(): array
  104. {
  105. $pointSalesArrayDropdown = ['' => '--'];
  106. $pointSalesArray = $this->findPointSales();
  107. foreach ($pointSalesArray as $pointSale) {
  108. $pointSalesArrayDropdown[$pointSale['id']] = $pointSale['name'];
  109. }
  110. return $pointSalesArrayDropdown;
  111. }
  112. public function findByBillingUser(User $user)
  113. {
  114. return $this->createDefaultQuery()
  115. ->filterIsOnline()
  116. ->filterByIdUser($user->id)
  117. ->orderBy('name ASC')
  118. ->find();
  119. }
  120. public function countPointSalesActiveLastThreeMonths()
  121. {
  122. $connection = \Yii::$app->getDb();
  123. $command = $connection->createCommand(
  124. '
  125. SELECT point_sale.id, COUNT(`order`.id) AS count_orders
  126. FROM `point_sale`
  127. LEFT JOIN `order` ON point_sale.id = `order`.id_point_sale AND `order`.`date` >= :date_limit
  128. GROUP BY point_sale.id
  129. HAVING count_orders > 0;',
  130. [
  131. ':date_limit' => date('Y-m-d', strtotime("-3 month")),
  132. ]
  133. );
  134. $results = $command->query();
  135. return count($results);
  136. }
  137. public function isDayOfWeekWithDelivery(string $dayWeek): bool
  138. {
  139. $activeDay = false;
  140. $pointsSaleArray = $this->findPointSales();
  141. $fieldDelivery = 'delivery_'.strtolower($dayWeek);
  142. foreach ($pointsSaleArray as $pointSale) {
  143. if ($pointSale->$fieldDelivery) {
  144. $activeDay = true;
  145. }
  146. }
  147. return $activeDay;
  148. }
  149. }