89 lines
3.3 KiB
PHP
89 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
|
use Maatwebsite\Excel\Concerns\WithHeadings;
|
|
use Maatwebsite\Excel\Concerns\WithMapping;
|
|
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
|
use Maatwebsite\Excel\Concerns\WithEvents;
|
|
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
|
|
use Maatwebsite\Excel\Events\AfterSheet;
|
|
use Carbon\Carbon;
|
|
|
|
class EmployeesExport implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize, WithEvents, WithCustomStartCell
|
|
{
|
|
protected $employees;
|
|
|
|
// Menangkap data hasil filter dari Controller
|
|
public function __construct($employees)
|
|
{
|
|
$this->employees = $employees;
|
|
}
|
|
|
|
public function collection()
|
|
{
|
|
return $this->employees;
|
|
}
|
|
|
|
// Mulai tabel data di baris ke-6 (Baris 1-4 untuk informasi header)
|
|
public function startCell(): string
|
|
{
|
|
return 'A6';
|
|
}
|
|
|
|
public function headings(): array
|
|
{
|
|
return ['NIK', 'No. KTP', 'Inisial', 'Nama Depan', 'Nama Belakang', 'Email', 'No. HP', 'Jenis Kelamin', 'Tgl Lahir', 'Tgl Masuk', 'Departemen', 'Jabatan', 'Status', 'Hak Akses'];
|
|
}
|
|
|
|
public function map($user): array
|
|
{
|
|
return [
|
|
$user->nik,
|
|
$user->identity_number ?? '-',
|
|
strtoupper($user->initial ?? '-'),
|
|
$user->first_name,
|
|
$user->last_name,
|
|
$user->email,
|
|
$user->phone ?? '-',
|
|
$user->gender == 'L' ? 'Laki-laki' : 'Perempuan',
|
|
$user->date_of_birth ? Carbon::parse($user->date_of_birth)->format('Y-m-d') : '-',
|
|
$user->join_date ? Carbon::parse($user->join_date)->format('Y-m-d') : '-',
|
|
$user->department->name ?? '-',
|
|
$user->position->name ?? '-',
|
|
$user->is_active ? 'Aktif' : 'Non-Aktif',
|
|
$user->roles->pluck('name')->implode(', '),
|
|
];
|
|
}
|
|
|
|
// Event untuk menyuntikkan Teks Header di atas tabel dan memberi warna
|
|
public function registerEvents(): array
|
|
{
|
|
return [
|
|
AfterSheet::class => function(AfterSheet $event) {
|
|
$sheet = $event->sheet->getDelegate();
|
|
$count = $this->employees->count();
|
|
$user = auth()->user()->first_name . ' ' . auth()->user()->last_name;
|
|
$date = Carbon::now()->translatedFormat('d F Y - H:i:s');
|
|
|
|
// Menyuntikkan Informasi di baris atas
|
|
$sheet->setCellValue('A1', 'LAPORAN DATA KARYAWAN');
|
|
$sheet->setCellValue('A2', 'Total Data (Sesuai Filter) : ' . $count . ' Karyawan');
|
|
$sheet->setCellValue('A3', 'Dicetak Oleh : ' . $user);
|
|
$sheet->setCellValue('A4', 'Waktu Cetak : ' . $date);
|
|
|
|
// Styling (Merge judul & warna header tabel)
|
|
$sheet->mergeCells('A1:N1');
|
|
$sheet->getStyle('A1')->getFont()->setBold(true)->setSize(14);
|
|
$sheet->getStyle('A2:A4')->getFont()->setBold(true);
|
|
$sheet->getStyle('A6:N6')->getFont()->setBold(true);
|
|
|
|
// Memberi warna latar abu-abu pada header tabel (Baris 6)
|
|
$sheet->getStyle('A6:N6')->getFill()
|
|
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
|
|
->getStartColor()->setARGB('FFE2E8F0');
|
|
},
|
|
];
|
|
}
|
|
} |