| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace App\Exports;
- use App\Enums\UserTypeEnum;
- use App\Models\User;
- use Carbon\Carbon;
- use Maatwebsite\Excel\Concerns\FromCollection;
- use Maatwebsite\Excel\Concerns\WithHeadings;
- use Maatwebsite\Excel\Concerns\WithStyles;
- use Maatwebsite\Excel\Concerns\WithColumnWidths;
- use Maatwebsite\Excel\Concerns\WithTitle;
- use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
- use PhpOffice\PhpSpreadsheet\Style\Fill;
- use PhpOffice\PhpSpreadsheet\Style\Alignment;
- use Illuminate\Support\Collection;
- class NovoAssociadosExport implements FromCollection, WithHeadings, WithStyles, WithColumnWidths, WithTitle
- {
- public function __construct(private readonly ?string $search = null) {}
- public function collection(): Collection
- {
- $now = Carbon::now();
- $query = User::where('type', UserTypeEnum::ASSOCIADO)
- ->whereYear('created_at', $now->year)
- ->whereMonth('created_at', $now->month)
- ->orderBy('created_at', 'desc');
- if ($this->search) {
- $term = '%' . mb_strtolower($this->search) . '%';
- $query->where(function ($q) use ($term) {
- $q->whereRaw('UNACCENT(LOWER(name)) LIKE UNACCENT(?)', [$term])
- ->orWhereRaw('UNACCENT(LOWER(cpf)) LIKE UNACCENT(?)', [$term])
- ->orWhereRaw("TO_CHAR(created_at, 'DD/MM/YYYY') LIKE ?", [$term]);
- });
- }
- return $query->get()->map(fn($u) => [
- $u->name,
- $u->cpf ?? '',
- $u->created_at?->format('d/m/Y') ?? '',
- ]);
- }
- public function headings(): array
- {
- return ['Nome', 'CPF', 'Data de Cadastro'];
- }
- public function title(): string
- {
- return 'Novos Associados';
- }
- public function columnWidths(): array
- {
- return ['A' => 40, 'B' => 20, 'C' => 20];
- }
- public function styles(Worksheet $sheet): array
- {
- return [
- 1 => [
- 'font' => ['bold' => true, 'color' => ['argb' => 'FFFFFFFF']],
- 'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['argb' => 'FF661D75']],
- 'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER],
- ],
- ];
- }
- }
|