NovoAssociadosExport.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Exports;
  3. use App\Enums\UserTypeEnum;
  4. use App\Models\User;
  5. use Carbon\Carbon;
  6. use Maatwebsite\Excel\Concerns\FromCollection;
  7. use Maatwebsite\Excel\Concerns\WithHeadings;
  8. use Maatwebsite\Excel\Concerns\WithStyles;
  9. use Maatwebsite\Excel\Concerns\WithColumnWidths;
  10. use Maatwebsite\Excel\Concerns\WithTitle;
  11. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  12. use PhpOffice\PhpSpreadsheet\Style\Fill;
  13. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  14. use Illuminate\Support\Collection;
  15. class NovoAssociadosExport implements FromCollection, WithHeadings, WithStyles, WithColumnWidths, WithTitle
  16. {
  17. public function __construct(private readonly ?string $search = null) {}
  18. public function collection(): Collection
  19. {
  20. $now = Carbon::now();
  21. $query = User::where('type', UserTypeEnum::ASSOCIADO)
  22. ->whereYear('created_at', $now->year)
  23. ->whereMonth('created_at', $now->month)
  24. ->orderBy('created_at', 'desc');
  25. if ($this->search) {
  26. $term = '%' . mb_strtolower($this->search) . '%';
  27. $query->where(function ($q) use ($term) {
  28. $q->whereRaw('UNACCENT(LOWER(name)) LIKE UNACCENT(?)', [$term])
  29. ->orWhereRaw('UNACCENT(LOWER(cpf)) LIKE UNACCENT(?)', [$term])
  30. ->orWhereRaw("TO_CHAR(created_at, 'DD/MM/YYYY') LIKE ?", [$term]);
  31. });
  32. }
  33. return $query->get()->map(fn($u) => [
  34. $u->name,
  35. $u->cpf ?? '',
  36. $u->created_at?->format('d/m/Y') ?? '',
  37. ]);
  38. }
  39. public function headings(): array
  40. {
  41. return ['Nome', 'CPF', 'Data de Cadastro'];
  42. }
  43. public function title(): string
  44. {
  45. return 'Novos Associados';
  46. }
  47. public function columnWidths(): array
  48. {
  49. return ['A' => 40, 'B' => 20, 'C' => 20];
  50. }
  51. public function styles(Worksheet $sheet): array
  52. {
  53. return [
  54. 1 => [
  55. 'font' => ['bold' => true, 'color' => ['argb' => 'FFFFFFFF']],
  56. 'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['argb' => 'FF661D75']],
  57. 'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER],
  58. ],
  59. ];
  60. }
  61. }