ReportService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Services;
  3. use App\Enums\UserTypeEnum;
  4. use App\Models\User;
  5. use Carbon\Carbon;
  6. use Illuminate\Pagination\LengthAwarePaginator;
  7. use Illuminate\Database\Eloquent\Collection;
  8. class ReportService
  9. {
  10. public function getCounters(): array
  11. {
  12. $now = Carbon::now();
  13. return [
  14. 'novos_associados' => User::where('type', UserTypeEnum::ASSOCIADO)
  15. ->whereYear('created_at', $now->year)
  16. ->whereMonth('created_at', $now->month)
  17. ->count(),
  18. 'contatos' => User::where('type', UserTypeEnum::ASSOCIADO)->count(),
  19. 'exclusoes_mes' => User::where('type', UserTypeEnum::ASSOCIADO)
  20. ->whereNotNull('excluded_at')
  21. ->whereYear('excluded_at', $now->year)
  22. ->whereMonth('excluded_at', $now->month)
  23. ->count(),
  24. ];
  25. }
  26. public function getNovoAssociadosPaginated(array $filters = [], int $perPage = 10): LengthAwarePaginator
  27. {
  28. $now = Carbon::now();
  29. $query = User::where('type', UserTypeEnum::ASSOCIADO)
  30. ->whereYear('created_at', $now->year)
  31. ->whereMonth('created_at', $now->month)
  32. ->orderBy('created_at', 'desc');
  33. if (!empty($filters['search'])) {
  34. $term = '%' . mb_strtolower($filters['search']) . '%';
  35. $query->where(function ($q) use ($term, $filters) {
  36. $q->whereRaw('UNACCENT(LOWER(name)) LIKE UNACCENT(?)', [$term])
  37. ->orWhereRaw('UNACCENT(LOWER(cpf)) LIKE UNACCENT(?)', [$term])
  38. ->orWhereRaw("TO_CHAR(created_at, 'DD/MM/YYYY') LIKE ?", [$term]);
  39. });
  40. }
  41. return $query->paginate($perPage);
  42. }
  43. public function getContatosAssociadosPaginated(array $filters = [], int $perPage = 10): LengthAwarePaginator
  44. {
  45. $query = User::where('type', UserTypeEnum::ASSOCIADO)
  46. ->orderBy('name', 'asc');
  47. if (!empty($filters['search'])) {
  48. $term = '%' . mb_strtolower($filters['search']) . '%';
  49. $query->where(function ($q) use ($term) {
  50. $q->whereRaw('UNACCENT(LOWER(name)) LIKE UNACCENT(?)', [$term])
  51. ->orWhereRaw('UNACCENT(LOWER(email)) LIKE UNACCENT(?)', [$term])
  52. ->orWhereRaw('UNACCENT(LOWER(COALESCE(phone, \'\'))) LIKE UNACCENT(?)', [$term]);
  53. });
  54. }
  55. return $query->paginate($perPage);
  56. }
  57. public function getExclusoesMesPaginated(array $filters = [], int $perPage = 10): LengthAwarePaginator
  58. {
  59. $now = Carbon::now();
  60. $query = User::where('type', UserTypeEnum::ASSOCIADO)
  61. ->whereNotNull('excluded_at')
  62. ->whereYear('excluded_at', $now->year)
  63. ->whereMonth('excluded_at', $now->month)
  64. ->orderBy('excluded_at', 'desc');
  65. if (!empty($filters['search'])) {
  66. $term = '%' . mb_strtolower($filters['search']) . '%';
  67. $query->where(function ($q) use ($term) {
  68. $q->whereRaw('UNACCENT(LOWER(name)) LIKE UNACCENT(?)', [$term])
  69. ->orWhereRaw('UNACCENT(LOWER(cpf)) LIKE UNACCENT(?)', [$term])
  70. ->orWhereRaw("TO_CHAR(excluded_at, 'DD/MM/YYYY') LIKE ?", [$term]);
  71. });
  72. }
  73. return $query->paginate($perPage);
  74. }
  75. public function getAllNovoAssociados(): Collection
  76. {
  77. $now = Carbon::now();
  78. return User::where('type', UserTypeEnum::ASSOCIADO)
  79. ->whereYear('created_at', $now->year)
  80. ->whereMonth('created_at', $now->month)
  81. ->orderBy('created_at', 'desc')
  82. ->get();
  83. }
  84. public function getAllContatosAssociados(): Collection
  85. {
  86. return User::where('type', UserTypeEnum::ASSOCIADO)
  87. ->orderBy('name', 'asc')
  88. ->get();
  89. }
  90. public function getAllExclusoesMes(): Collection
  91. {
  92. $now = Carbon::now();
  93. return User::where('type', UserTypeEnum::ASSOCIADO)
  94. ->whereNotNull('excluded_at')
  95. ->whereYear('excluded_at', $now->year)
  96. ->whereMonth('excluded_at', $now->month)
  97. ->orderBy('excluded_at', 'desc')
  98. ->get();
  99. }
  100. }