Files
verifikator/app/Exports/PaguExport.php
T
2026-10-21 08:41:10 +07:00

165 lines
5.3 KiB
PHP

<?php
namespace App\Exports;
use App\Models\Verifikator\Pagu;
use App\Models\Verifikator\Rekapitulasi;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithTitle;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class PaguExport implements FromCollection, WithHeadings, WithMapping, WithColumnFormatting, WithStyles, ShouldAutoSize, WithTitle
{
protected $periode;
public function __construct($periode = null, protected ?int $facultyId = null)
{
$this->periode = $periode;
}
public function title(): string
{
return 'Data Pagu ' . ($this->periode ? $this->periode : 'Semua Periode');
}
public function collection()
{
$query = Pagu::with('unitKerja');
if ($this->facultyId !== null) {
$query->where('unit_pembayaran_id', $this->facultyId);
}
if ($this->periode) {
$query->where('tahun', $this->periode);
}
$query = $query->orderBy('unit_pembayaran_id', 'asc');
$data = $query->get();
// Map data with calculated columns
$mappedData = $data->map(function ($row) {
// Get rekapitulasi data for the same unit and year
$rekapitulasi = Rekapitulasi::where('tahun', $row->tahun)
->where('unit_kerja_nama', $row->unitKerja->nama ?? '')
->first();
$totalTerpakai = 0;
if ($rekapitulasi) {
$totalTerpakai = ($rekapitulasi->total_p1 ?? 0) +
($rekapitulasi->total_p2 ?? 0) +
($rekapitulasi->total_thr ?? 0) +
($rekapitulasi->total_r13 ?? 0) +
($rekapitulasi->total_publikasi ?? 0);
}
$sisaPagu = $row->pagu - $totalTerpakai;
return (object) [
'unit_kerja_nama' => $row->unitKerja ? $row->unitKerja->nama : 'Unit Kerja Tidak Ditemukan',
'tahun' => $row->tahun,
'pagu' => $row->pagu,
'total_terpakai' => $totalTerpakai,
'sisa_pagu' => $sisaPagu,
'persentase_terpakai' => $row->pagu > 0 ? ($totalTerpakai / $row->pagu) * 100 : 0,
];
});
// Add total row
if ($mappedData->count() > 0) {
$totalPagu = $mappedData->sum('pagu');
$totalTerpakai = $mappedData->sum('total_terpakai');
$totalSisa = $mappedData->sum('sisa_pagu');
$persentaseTerpakai = $totalPagu > 0 ? ($totalTerpakai / $totalPagu) * 100 : 0;
$mappedData->push((object) [
'unit_kerja_nama' => 'TOTAL',
'tahun' => '',
'pagu' => $totalPagu,
'total_terpakai' => $totalTerpakai,
'sisa_pagu' => $totalSisa,
'persentase_terpakai' => $persentaseTerpakai,
]);
}
return $mappedData;
}
public function headings(): array
{
return [
'Unit Kerja',
'Tahun',
'Pagu (Rp)',
'Jumlah Terpakai (Rp)',
'Sisa Pagu (Rp)',
'Persentase Terpakai (%)',
];
}
public function map($row): array
{
return [
$row->unit_kerja_nama,
$row->tahun,
$row->pagu,
$row->total_terpakai,
$row->sisa_pagu,
$row->persentase_terpakai,
];
}
public function columnFormats(): array
{
return [
'C' => '#,##0.00', // Pagu
'D' => '#,##0.00', // Jumlah Terpakai
'E' => '#,##0.00', // Sisa Pagu
'F' => '0.00', // Persentase
];
}
public function styles(Worksheet $sheet)
{
$dataCount = $this->collection()->count();
$totalRowNumber = $dataCount + 1; // +1 untuk header
$styles = [
// Header row styling
1 => [
'font' => ['bold' => true, 'color' => ['rgb' => 'FFFFFF']],
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'startColor' => ['rgb' => '4472C4']
],
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER
]
]
];
// Total row styling jika ada data
if ($dataCount > 1) { // > 1 karena ada row TOTAL
$styles[$totalRowNumber] = [
'font' => ['bold' => true, 'color' => ['rgb' => 'FFFFFF']],
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'startColor' => ['rgb' => '70AD47']
],
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,
'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER
]
];
}
return $styles;
}
}