NovoAssociadosExport.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 collection(): Collection
  18. {
  19. $now = Carbon::now();
  20. return User::where('type', UserTypeEnum::ASSOCIADO)
  21. ->whereYear('created_at', $now->year)
  22. ->whereMonth('created_at', $now->month)
  23. ->orderBy('created_at', 'desc')
  24. ->get()
  25. ->map(fn($u) => [
  26. $u->name,
  27. $u->cpf ?? '',
  28. $u->created_at?->format('d/m/Y') ?? '',
  29. ]);
  30. }
  31. public function headings(): array
  32. {
  33. return ['Nome', 'CPF', 'Data de Cadastro'];
  34. }
  35. public function title(): string
  36. {
  37. return 'Novos Associados';
  38. }
  39. public function columnWidths(): array
  40. {
  41. return ['A' => 40, 'B' => 20, 'C' => 20];
  42. }
  43. public function styles(Worksheet $sheet): array
  44. {
  45. return [
  46. 1 => [
  47. 'font' => ['bold' => true, 'color' => ['argb' => 'FFFFFFFF']],
  48. 'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['argb' => 'FF661D75']],
  49. 'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER],
  50. ],
  51. ];
  52. }
  53. }