ExclusoesMesExport.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 ExclusoesMesExport 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. ->whereNotNull('excluded_at')
  23. ->whereYear('excluded_at', $now->year)
  24. ->whereMonth('excluded_at', $now->month)
  25. ->orderBy('excluded_at', 'desc');
  26. if ($this->search) {
  27. $term = '%' . mb_strtolower($this->search) . '%';
  28. $query->where(function ($q) use ($term) {
  29. $q->whereRaw('UNACCENT(LOWER(name)) LIKE UNACCENT(?)', [$term])
  30. ->orWhereRaw('UNACCENT(LOWER(cpf)) LIKE UNACCENT(?)', [$term])
  31. ->orWhereRaw("TO_CHAR(excluded_at, 'DD/MM/YYYY') LIKE ?", [$term]);
  32. });
  33. }
  34. return $query->get()->map(fn($u) => [
  35. $u->name,
  36. $u->cpf ?? '',
  37. $u->excluded_at?->format('d/m/Y') ?? '',
  38. ]);
  39. }
  40. public function headings(): array
  41. {
  42. return ['Nome', 'CPF', 'Data de Exclusão'];
  43. }
  44. public function title(): string
  45. {
  46. return 'Exclusões do Mês';
  47. }
  48. public function columnWidths(): array
  49. {
  50. return ['A' => 40, 'B' => 20, 'C' => 20];
  51. }
  52. public function styles(Worksheet $sheet): array
  53. {
  54. return [
  55. 1 => [
  56. 'font' => ['bold' => true, 'color' => ['argb' => 'FFFFFFFF']],
  57. 'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['argb' => 'FF661D75']],
  58. 'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER],
  59. ],
  60. ];
  61. }
  62. }