No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

103 líneas
3.2KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Repositories;
  4. use Core\Repository;
  5. class HouseholderNameRepository extends Repository
  6. {
  7. protected string $table = 'householder_names';
  8. protected string $primaryKey = 'id';
  9. public function countAll(string $search = '', string $householdId = ''): int
  10. {
  11. [$sql, $params] = $this->buildFilterQuery(
  12. 'SELECT COUNT(*) AS n FROM householder_names hn
  13. JOIN households h ON h.id = hn.household_id',
  14. $search, $householdId
  15. );
  16. $row = $this->database->first($sql, $params);
  17. return (int) ($row['n'] ?? 0);
  18. }
  19. /** @return list<array<string,mixed>> */
  20. public function findPaged(int $page, int $perPage, string $search = '', string $householdId = ''): array
  21. {
  22. $offset = ($page - 1) * $perPage;
  23. [$where, $params] = $this->buildFilterQuery(
  24. 'SELECT hn.*, h.address AS household_address, t.name AS territory_name
  25. FROM householder_names hn
  26. JOIN households h ON h.id = hn.household_id
  27. JOIN territories t ON t.id = h.territory_id',
  28. $search, $householdId
  29. );
  30. return $this->database->query(
  31. $where . ' ORDER BY h.address ASC, hn.name ASC LIMIT :limit OFFSET :offset',
  32. array_merge($params, ['limit' => $perPage, 'offset' => $offset])
  33. );
  34. }
  35. /** @return list<array<string,mixed>> */
  36. public function findAllByHousehold(int|string $householdId): array
  37. {
  38. return $this->database->query(
  39. 'SELECT * FROM householder_names WHERE household_id = :id ORDER BY name ASC',
  40. ['id' => $householdId]
  41. );
  42. }
  43. public function findWithHousehold(int|string $id): ?array
  44. {
  45. return $this->database->first(
  46. 'SELECT hn.*, h.address AS household_address, h.territory_id,
  47. t.name AS territory_name
  48. FROM householder_names hn
  49. JOIN households h ON h.id = hn.household_id
  50. JOIN territories t ON t.id = h.territory_id
  51. WHERE hn.id = :id',
  52. ['id' => $id]
  53. );
  54. }
  55. public function toggleLetterReturned(int|string $id): bool
  56. {
  57. return $this->database->execute(
  58. 'UPDATE householder_names
  59. SET letter_returned = CASE WHEN letter_returned = 1 THEN 0 ELSE 1 END,
  60. return_date = CASE WHEN letter_returned = 0 THEN datetime(\'now\') ELSE NULL END,
  61. updated_at = datetime(\'now\')
  62. WHERE id = :id',
  63. ['id' => $id]
  64. );
  65. }
  66. /** @return array{string, array<string,mixed>} */
  67. private function buildFilterQuery(string $base, string $search, string $householdId): array
  68. {
  69. $conditions = [];
  70. $params = [];
  71. if ($search !== '') {
  72. $conditions[] = 'hn.name LIKE :s';
  73. $params['s'] = '%' . $search . '%';
  74. }
  75. if ($householdId !== '') {
  76. $conditions[] = 'hn.household_id = :hid';
  77. $params['hid'] = $householdId;
  78. }
  79. $sql = $base;
  80. if (!empty($conditions)) {
  81. $sql .= ' WHERE ' . implode(' AND ', $conditions);
  82. }
  83. return [$sql, $params];
  84. }
  85. }

Powered by TurnKey Linux.