691 lines
28 KiB
PHP
691 lines
28 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use App\Models\Verifikator\JabatanFungsionalRiwayat;
|
|
use App\Models\Verifikator\JabatanTugasTambahanRiwayat;
|
|
use App\Models\Verifikator\Periode;
|
|
use App\Models\Verifikator\Rasio;
|
|
use App\Models\Verifikator\View\Pegawai;
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
|
use PhpOffice\PhpSpreadsheet\Style\Border;
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
|
class RasioP2Export
|
|
{
|
|
protected $unit;
|
|
|
|
protected $filterRasio;
|
|
|
|
protected $filterNominal;
|
|
|
|
protected $format;
|
|
|
|
protected $periode;
|
|
|
|
public function __construct($unit = null, $filterRasio = null, $filterNominal = null, $format = 'multiple', $periode = null)
|
|
{
|
|
$this->unit = $unit;
|
|
$this->filterRasio = $filterRasio;
|
|
$this->filterNominal = $filterNominal;
|
|
$this->format = $format;
|
|
$this->periode = $periode ?? Periode::where('status', true)->first()->id_periode;
|
|
}
|
|
|
|
public function export()
|
|
{
|
|
// Increase time limit and memory for large exports
|
|
set_time_limit(900); // 15 minutes
|
|
ini_set('memory_limit', '1024M'); // 1GB
|
|
ini_set('max_execution_time', '900');
|
|
|
|
$spreadsheet = new Spreadsheet;
|
|
|
|
// Set document properties
|
|
$spreadsheet->getProperties()
|
|
->setCreator('Verifikator System')
|
|
->setTitle('Data Rasio P2')
|
|
->setSubject('Export Rasio P2')
|
|
->setDescription('Data Rasio P2 yang diekspor dari sistem verifikator');
|
|
|
|
// Jika unit null, cek format export
|
|
if ($this->unit === null) {
|
|
if ($this->format === 'single') {
|
|
// Format single: semua unit kerja dalam satu sheet
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->setTitle('Semua Unit Pembayaran');
|
|
$this->fillSheetDataAllUnits($sheet);
|
|
} else {
|
|
// Format multiple: sheet per unit kerja (default)
|
|
$unitKerjaMapping = $this->getUnitKerjaMapping();
|
|
|
|
// Loop untuk membuat sheet per unit kerja
|
|
foreach ($unitKerjaMapping as $unitId => $unitNama) {
|
|
// Buat sheet baru atau gunakan yang sudah ada
|
|
if ($unitId === 0) {
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->setTitle($this->sanitizeSheetName($unitNama));
|
|
} else {
|
|
$sheet = $spreadsheet->createSheet();
|
|
$sheet->setTitle($this->sanitizeSheetName($unitNama));
|
|
}
|
|
|
|
// Isi data untuk unit ini
|
|
$this->fillSheetData($sheet, $unitId);
|
|
}
|
|
}
|
|
} else {
|
|
// Jika unit dipilih, buat satu sheet saja
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$unitKerjaMapping = $this->getUnitKerjaMapping();
|
|
$sheetTitle = $unitKerjaMapping[$this->unit] ?? 'Unit '.$this->unit;
|
|
$sheet->setTitle($this->sanitizeSheetName($sheetTitle));
|
|
$this->fillSheetData($sheet, $this->unit);
|
|
}
|
|
|
|
return $spreadsheet;
|
|
}
|
|
|
|
protected function fillSheetDataAllUnits($sheet)
|
|
{
|
|
// Set headers
|
|
$headers = [
|
|
'A1' => 'No.',
|
|
'B1' => 'Nama',
|
|
'C1' => 'NIP',
|
|
'D1' => 'Jenis Pegawai',
|
|
'E1' => 'Pangkat',
|
|
'F1' => 'Golongan',
|
|
'G1' => 'Jabatan Fungsional',
|
|
'H1' => 'Grade Jabatan Fungsional',
|
|
'I1' => 'Harga Jabatan Fungsional',
|
|
'J1' => 'Jabatan Tugas Tambahan',
|
|
'K1' => 'Grade Jabatan Tugas Tambahan',
|
|
'L1' => 'Harga Jabatan Tugas Tambahan',
|
|
'M1' => 'Gaji P2 yang dibayarkan',
|
|
'N1' => 'PPH 21',
|
|
'O1' => 'Gaji P2 Bersih',
|
|
'P1' => 'NPWP',
|
|
'Q1' => 'Nomor Rekening',
|
|
'R1' => 'Unit Kerja',
|
|
'S1' => 'Unit Pembayaran',
|
|
];
|
|
|
|
foreach ($headers as $cell => $header) {
|
|
$sheet->setCellValue($cell, $header);
|
|
}
|
|
|
|
// Style headers
|
|
$headerRange = 'A1:S1';
|
|
$sheet->getStyle($headerRange)->applyFromArray([
|
|
'font' => [
|
|
'bold' => true,
|
|
'color' => ['rgb' => 'FFFFFF'],
|
|
],
|
|
'fill' => [
|
|
'fillType' => Fill::FILL_SOLID,
|
|
'startColor' => ['rgb' => '4472C4'],
|
|
],
|
|
'alignment' => [
|
|
'horizontal' => Alignment::HORIZONTAL_CENTER,
|
|
'vertical' => Alignment::VERTICAL_CENTER,
|
|
],
|
|
'borders' => [
|
|
'allBorders' => [
|
|
'borderStyle' => Border::BORDER_THIN,
|
|
'color' => ['rgb' => '000000'],
|
|
],
|
|
],
|
|
]);
|
|
|
|
// Get data untuk semua unit kerja (0-9)
|
|
$allData = collect();
|
|
$unitKerjaMapping = $this->getUnitKerjaMapping();
|
|
|
|
foreach ($unitKerjaMapping as $unitId => $unitNama) {
|
|
$unitData = $this->getDataForUnit($unitId);
|
|
// Add unit pembayaran name to each item
|
|
$unitData = $unitData->map(function ($item) use ($unitNama) {
|
|
$item['unit_pembayaran_nama'] = $unitNama;
|
|
|
|
return $item;
|
|
});
|
|
$allData = $allData->merge($unitData);
|
|
}
|
|
|
|
// Fill data
|
|
$row = 2;
|
|
$no = 1;
|
|
foreach ($allData as $item) {
|
|
$sheet->setCellValue('A'.$row, $no);
|
|
$sheet->setCellValue('B'.$row, $item['nama']);
|
|
$sheet->setCellValueExplicit('C'.$row, $item['nip'], DataType::TYPE_STRING);
|
|
$sheet->setCellValue('D'.$row, $item['jenis_pegawai']);
|
|
$sheet->setCellValue('E'.$row, $item['pangkat']);
|
|
$sheet->setCellValue('F'.$row, $item['golongan']);
|
|
$sheet->setCellValue('G'.$row, $item['jabatan_fungsional']);
|
|
$sheet->setCellValue('H'.$row, $item['grade_jafung']);
|
|
$sheet->setCellValue('I'.$row, $item['harga_jafung']);
|
|
$sheet->setCellValue('J'.$row, $item['jabatan_tambahan']);
|
|
$sheet->setCellValue('K'.$row, $item['grade_jtt']);
|
|
$sheet->setCellValue('L'.$row, $item['harga_jtt']);
|
|
$sheet->setCellValue('M'.$row, $item['p2_numeric']);
|
|
$sheet->setCellValue('N'.$row, $item['pajak_numeric']);
|
|
$sheet->setCellValue('O'.$row, $item['p2_setelah_pajak_numeric']);
|
|
$sheet->setCellValueExplicit('P'.$row, $item['npwp'], DataType::TYPE_STRING);
|
|
$sheet->setCellValueExplicit('Q'.$row, $item['nomor_rekening'], DataType::TYPE_STRING);
|
|
$sheet->setCellValue('R'.$row, $item['unit_kerja']);
|
|
$sheet->setCellValue('S'.$row, $item['unit_pembayaran_nama']);
|
|
$row++;
|
|
$no++;
|
|
}
|
|
|
|
// Style data rows
|
|
if ($row > 2) {
|
|
$dataRange = 'A2:S'.($row - 1);
|
|
$sheet->getStyle($dataRange)->applyFromArray([
|
|
'borders' => [
|
|
'allBorders' => [
|
|
'borderStyle' => Border::BORDER_THIN,
|
|
'color' => ['rgb' => '000000'],
|
|
],
|
|
],
|
|
'alignment' => [
|
|
'vertical' => Alignment::VERTICAL_CENTER,
|
|
],
|
|
]);
|
|
|
|
// Format number columns
|
|
$sheet->getStyle('I2:I'.($row - 1))->getNumberFormat()->setFormatCode('#,##0');
|
|
$sheet->getStyle('L2:L'.($row - 1))->getNumberFormat()->setFormatCode('#,##0');
|
|
$sheet->getStyle('M2:O'.($row - 1))->getNumberFormat()->setFormatCode('#,##0');
|
|
|
|
// Format text columns
|
|
$sheet->getStyle('C2:C'.($row - 1))->getNumberFormat()->setFormatCode('@'); // NIP
|
|
$sheet->getStyle('P2:P'.($row - 1))->getNumberFormat()->setFormatCode('@'); // NPWP
|
|
$sheet->getStyle('Q2:Q'.($row - 1))->getNumberFormat()->setFormatCode('@'); // Nomor Rekening
|
|
}
|
|
|
|
// Auto-size columns
|
|
foreach (range('A', 'S') as $column) {
|
|
$sheet->getColumnDimension($column)->setAutoSize(true);
|
|
}
|
|
|
|
// Add summary row
|
|
if (! empty($allData->toArray())) {
|
|
$summaryRow = $row + 1;
|
|
$sheet->setCellValue('L'.$summaryRow, 'TOTAL');
|
|
$sheet->setCellValue('M'.$summaryRow, '=SUM(M2:M'.($row - 1).')');
|
|
$sheet->setCellValue('N'.$summaryRow, '=SUM(N2:N'.($row - 1).')');
|
|
$sheet->setCellValue('O'.$summaryRow, '=SUM(O2:O'.($row - 1).')');
|
|
|
|
$sheet->getStyle('L'.$summaryRow.':O'.$summaryRow)->applyFromArray([
|
|
'font' => ['bold' => true],
|
|
'fill' => [
|
|
'fillType' => Fill::FILL_SOLID,
|
|
'startColor' => ['rgb' => 'E7E6E6'],
|
|
],
|
|
'borders' => [
|
|
'allBorders' => [
|
|
'borderStyle' => Border::BORDER_THIN,
|
|
'color' => ['rgb' => '000000'],
|
|
],
|
|
],
|
|
]);
|
|
|
|
$sheet->getStyle('M'.$summaryRow.':O'.$summaryRow)->getNumberFormat()->setFormatCode('#,##0');
|
|
|
|
// Add dosen/tendik count rows (unique by NIP)
|
|
$uniqueData = $allData->unique('nip');
|
|
$jumlahDosen = $uniqueData->filter(fn ($item) => strtolower($item['jenis_pegawai']) === 'dosen')->count();
|
|
$jumlahTendik = $uniqueData->filter(fn ($item) => strtolower($item['jenis_pegawai']) === 'tendik')->count();
|
|
|
|
$dosenRow = $summaryRow + 2;
|
|
$tendikRow = $dosenRow + 1;
|
|
|
|
$sheet->setCellValue('L'.$dosenRow, 'Jumlah Dosen');
|
|
$sheet->setCellValue('M'.$dosenRow, $jumlahDosen);
|
|
$sheet->setCellValue('L'.$tendikRow, 'Jumlah Tendik');
|
|
$sheet->setCellValue('M'.$tendikRow, $jumlahTendik);
|
|
|
|
$countStyle = [
|
|
'font' => ['bold' => true],
|
|
'borders' => [
|
|
'allBorders' => [
|
|
'borderStyle' => Border::BORDER_THIN,
|
|
'color' => ['rgb' => '000000'],
|
|
],
|
|
],
|
|
];
|
|
|
|
$sheet->getStyle('L'.$dosenRow.':M'.$dosenRow)->applyFromArray($countStyle);
|
|
$sheet->getStyle('L'.$tendikRow.':M'.$tendikRow)->applyFromArray($countStyle);
|
|
}
|
|
}
|
|
|
|
protected function fillSheetData($sheet, $unit)
|
|
{
|
|
// Set headers
|
|
$headers = [
|
|
'A1' => 'No.',
|
|
'B1' => 'Nama',
|
|
'C1' => 'NIP',
|
|
'D1' => 'Jenis Pegawai',
|
|
'E1' => 'Pangkat',
|
|
'F1' => 'Golongan',
|
|
'G1' => 'Jabatan Fungsional',
|
|
'H1' => 'Grade Jabatan Fungsional',
|
|
'I1' => 'Harga Jabatan Fungsional',
|
|
'J1' => 'Jabatan Tugas Tambahan',
|
|
'K1' => 'Grade Jabatan Tugas Tambahan',
|
|
'L1' => 'Harga Jabatan Tugas Tambahan',
|
|
'M1' => 'Gaji P2 yang dibayarkan',
|
|
'N1' => 'PPH 21',
|
|
'O1' => 'Gaji P2 Bersih',
|
|
'P1' => 'NPWP',
|
|
'Q1' => 'Nomor Rekening',
|
|
'R1' => 'Unit Kerja',
|
|
];
|
|
|
|
foreach ($headers as $cell => $header) {
|
|
$sheet->setCellValue($cell, $header);
|
|
}
|
|
|
|
// Style headers
|
|
$headerRange = 'A1:R1';
|
|
$sheet->getStyle($headerRange)->applyFromArray([
|
|
'font' => [
|
|
'bold' => true,
|
|
'color' => ['rgb' => 'FFFFFF'],
|
|
],
|
|
'fill' => [
|
|
'fillType' => Fill::FILL_SOLID,
|
|
'startColor' => ['rgb' => '4472C4'],
|
|
],
|
|
'alignment' => [
|
|
'horizontal' => Alignment::HORIZONTAL_CENTER,
|
|
'vertical' => Alignment::VERTICAL_CENTER,
|
|
],
|
|
'borders' => [
|
|
'allBorders' => [
|
|
'borderStyle' => Border::BORDER_THIN,
|
|
'color' => ['rgb' => '000000'],
|
|
],
|
|
],
|
|
]);
|
|
|
|
// Get data untuk unit tertentu
|
|
$data = $this->getDataForUnit($unit);
|
|
|
|
// Fill data
|
|
$row = 2;
|
|
$no = 1;
|
|
foreach ($data as $item) {
|
|
$sheet->setCellValue('A'.$row, $no);
|
|
$sheet->setCellValue('B'.$row, $item['nama']);
|
|
$sheet->setCellValueExplicit('C'.$row, $item['nip'], DataType::TYPE_STRING);
|
|
$sheet->setCellValue('D'.$row, $item['jenis_pegawai']);
|
|
$sheet->setCellValue('E'.$row, $item['pangkat']);
|
|
$sheet->setCellValue('F'.$row, $item['golongan']);
|
|
$sheet->setCellValue('G'.$row, $item['jabatan_fungsional']);
|
|
$sheet->setCellValue('H'.$row, $item['grade_jafung']);
|
|
$sheet->setCellValue('I'.$row, $item['harga_jafung']);
|
|
$sheet->setCellValue('J'.$row, $item['jabatan_tambahan']);
|
|
$sheet->setCellValue('K'.$row, $item['grade_jtt']);
|
|
$sheet->setCellValue('L'.$row, $item['harga_jtt']);
|
|
$sheet->setCellValue('M'.$row, $item['p2_numeric']);
|
|
$sheet->setCellValue('N'.$row, $item['pajak_numeric']);
|
|
$sheet->setCellValue('O'.$row, $item['p2_setelah_pajak_numeric']);
|
|
$sheet->setCellValueExplicit('P'.$row, $item['npwp'], DataType::TYPE_STRING);
|
|
$sheet->setCellValueExplicit('Q'.$row, $item['nomor_rekening'], DataType::TYPE_STRING);
|
|
$sheet->setCellValue('R'.$row, $item['unit_kerja']);
|
|
$row++;
|
|
$no++;
|
|
}
|
|
|
|
// Style data rows
|
|
if ($row > 2) {
|
|
$dataRange = 'A2:R'.($row - 1);
|
|
$sheet->getStyle($dataRange)->applyFromArray([
|
|
'borders' => [
|
|
'allBorders' => [
|
|
'borderStyle' => Border::BORDER_THIN,
|
|
'color' => ['rgb' => '000000'],
|
|
],
|
|
],
|
|
'alignment' => [
|
|
'vertical' => Alignment::VERTICAL_CENTER,
|
|
],
|
|
]);
|
|
|
|
// Format number columns
|
|
$sheet->getStyle('I2:I'.($row - 1))->getNumberFormat()->setFormatCode('#,##0');
|
|
$sheet->getStyle('L2:L'.($row - 1))->getNumberFormat()->setFormatCode('#,##0');
|
|
$sheet->getStyle('M2:O'.($row - 1))->getNumberFormat()->setFormatCode('#,##0');
|
|
|
|
// Format text columns
|
|
$sheet->getStyle('C2:C'.($row - 1))->getNumberFormat()->setFormatCode('@'); // NIP
|
|
$sheet->getStyle('P2:P'.($row - 1))->getNumberFormat()->setFormatCode('@'); // NPWP
|
|
$sheet->getStyle('Q2:Q'.($row - 1))->getNumberFormat()->setFormatCode('@'); // Nomor Rekening
|
|
}
|
|
|
|
// Auto-size columns
|
|
foreach (range('A', 'R') as $column) {
|
|
$sheet->getColumnDimension($column)->setAutoSize(true);
|
|
}
|
|
|
|
// Add summary row
|
|
if (! empty($data->toArray())) {
|
|
$summaryRow = $row + 1;
|
|
$sheet->setCellValue('L'.$summaryRow, 'TOTAL');
|
|
$sheet->setCellValue('M'.$summaryRow, '=SUM(M2:M'.($row - 1).')');
|
|
$sheet->setCellValue('N'.$summaryRow, '=SUM(N2:N'.($row - 1).')');
|
|
$sheet->setCellValue('O'.$summaryRow, '=SUM(O2:O'.($row - 1).')');
|
|
|
|
$sheet->getStyle('L'.$summaryRow.':O'.$summaryRow)->applyFromArray([
|
|
'font' => ['bold' => true],
|
|
'fill' => [
|
|
'fillType' => Fill::FILL_SOLID,
|
|
'startColor' => ['rgb' => 'E7E6E6'],
|
|
],
|
|
'borders' => [
|
|
'allBorders' => [
|
|
'borderStyle' => Border::BORDER_THIN,
|
|
'color' => ['rgb' => '000000'],
|
|
],
|
|
],
|
|
]);
|
|
|
|
$sheet->getStyle('M'.$summaryRow.':O'.$summaryRow)->getNumberFormat()->setFormatCode('#,##0');
|
|
|
|
// Add dosen/tendik count rows (unique by NIP)
|
|
$uniqueData = $data->unique('nip');
|
|
$jumlahDosen = $uniqueData->filter(fn ($item) => strtolower($item['jenis_pegawai']) === 'dosen')->count();
|
|
$jumlahTendik = $uniqueData->filter(fn ($item) => strtolower($item['jenis_pegawai']) === 'tendik')->count();
|
|
|
|
$dosenRow = $summaryRow + 2;
|
|
$tendikRow = $dosenRow + 1;
|
|
|
|
$sheet->setCellValue('L'.$dosenRow, 'Jumlah Dosen');
|
|
$sheet->setCellValue('M'.$dosenRow, $jumlahDosen);
|
|
$sheet->setCellValue('L'.$tendikRow, 'Jumlah Tendik');
|
|
$sheet->setCellValue('M'.$tendikRow, $jumlahTendik);
|
|
|
|
$countStyle = [
|
|
'font' => ['bold' => true],
|
|
'borders' => [
|
|
'allBorders' => [
|
|
'borderStyle' => Border::BORDER_THIN,
|
|
'color' => ['rgb' => '000000'],
|
|
],
|
|
],
|
|
];
|
|
|
|
$sheet->getStyle('L'.$dosenRow.':M'.$dosenRow)->applyFromArray($countStyle);
|
|
$sheet->getStyle('L'.$tendikRow.':M'.$tendikRow)->applyFromArray($countStyle);
|
|
}
|
|
}
|
|
|
|
protected function getUnitKerjaMapping()
|
|
{
|
|
return [
|
|
0 => 'Universitas',
|
|
1 => 'Fakultas Hukum',
|
|
2 => 'Fakultas Ekonomi dan Bisnis',
|
|
3 => 'Fakultas Pertanian',
|
|
4 => 'Fakultas Teknik',
|
|
5 => 'Fakultas Ilmu Sosial dan Ilmu Politik',
|
|
6 => 'Fakultas Keguruan dan Ilmu Pendidikan',
|
|
7 => 'Fakultas Kehutanan',
|
|
8 => 'Fakultas Matematika dan Ilmu Pengetahuan Alam',
|
|
9 => 'Fakultas Kedokteran',
|
|
];
|
|
}
|
|
|
|
protected function sanitizeSheetName($name)
|
|
{
|
|
// Excel sheet name tidak boleh lebih dari 31 karakter dan tidak boleh mengandung: \ / ? * [ ]
|
|
$name = str_replace(['\\', '/', '?', '*', '[', ']'], '', $name);
|
|
|
|
return mb_substr($name, 0, 31);
|
|
}
|
|
|
|
protected function getDataForUnit($unit)
|
|
{
|
|
// Query dengan optimalisasi
|
|
$pegawaisQuery = Pegawai::withoutGlobalScope('aktif')->select('nip', 'nip_siakad', 'nama', 'jenis_pegawai', 'unit_kerja_nama', 'jabatan_fungsional_id', 'jabatan_dtt_id', 'npwp', 'nomor_rekening')
|
|
->whereHas('jabatan', function ($query) use ($unit) {
|
|
$query
|
|
->where('periode', $this->periode)
|
|
->where('active', true)
|
|
->whereHas('detail.remunerasi', function ($query) use ($unit) {
|
|
$query->where('unit_pembayaran_id', $unit)
|
|
->whereNull('deleted_at');
|
|
});
|
|
});
|
|
|
|
// Load data dengan pagination
|
|
$pegawais = $pegawaisQuery
|
|
->with([
|
|
'jabatan' => function ($query) use ($unit) {
|
|
$query
|
|
->select('id', 'nip', 'tahun', 'periode', 'active')
|
|
->where('periode', $this->periode)
|
|
->where('active', true)
|
|
->with(['detail' => function ($query) use ($unit) {
|
|
$query
|
|
->select('id', 'jabatan_pegawai_id', 'bulan', 'golongan', 'status_pns', 'jabatan_fungsional_id', 'jabatan_tambahan_id')
|
|
->orderBy('bulan', 'desc') // Order by bulan descending to get latest
|
|
->with([
|
|
'fungsional:id,nama',
|
|
'tambahan:id,nama',
|
|
'pangkat',
|
|
'remunerasi' => function ($query) use ($unit) {
|
|
$query->select('id', 'jabatan_pegawai_detail_id', 'p2_bruto', 'p2_rasio', 'p2_tax', 'p2_netto', 'poin_remunerasi', 'poin_p2', 'unit_pembayaran_id')
|
|
->where('unit_pembayaran_id', $unit)
|
|
->whereNull('deleted_at');
|
|
},
|
|
]);
|
|
}]);
|
|
},
|
|
])
|
|
->get();
|
|
|
|
// Format data dari database menggunakan data asli
|
|
$data = collect($pegawais)->map(function ($pegawai) use ($unit) {
|
|
// Hitung total P2 bruto untuk pegawai ini menggunakan formula yang diberikan
|
|
$totalP2Bruto = 0;
|
|
$totalPajak = 0;
|
|
$jabatanFungsional = '-';
|
|
$jabatanTambahan = '-';
|
|
$pangkat = '-';
|
|
$golongan = '-';
|
|
$gradeJafung = '-';
|
|
$hargaJafung = 0;
|
|
$gradeJtt = '-';
|
|
$hargaJtt = 0;
|
|
|
|
if ($pegawai->jabatan && $pegawai->jabatan->detail->isNotEmpty()) {
|
|
// Hitung total P2 bruto dari semua detail jabatan dan remunerasi
|
|
$totalP2Bruto = $pegawai->jabatan->detail->sum(function ($detail) {
|
|
return $detail->remunerasi->sum('p2_bruto');
|
|
});
|
|
|
|
// Hitung total pajak dari semua detail jabatan dan remunerasi
|
|
$totalPajak = $pegawai->jabatan->detail->sum(function ($detail) {
|
|
return $detail->remunerasi->sum('p2_tax');
|
|
});
|
|
|
|
// Get data dari detail bulan terbaru (first karena sudah di-order by bulan desc)
|
|
$latestDetail = $pegawai->jabatan->detail->first();
|
|
|
|
if ($latestDetail) {
|
|
// Get pangkat dan golongan
|
|
if ($latestDetail->pangkat) {
|
|
$pangkat = $latestDetail->pangkat->pangkat;
|
|
}
|
|
$golongan = $latestDetail->golongan ?? '-';
|
|
|
|
// Get jabatan fungsional, grade, dan harga
|
|
if ($latestDetail->fungsional) {
|
|
$jabatanFungsional = $latestDetail->fungsional->nama;
|
|
|
|
// Query JabatanFungsionalRiwayat berdasarkan jabatan_fungsional_id
|
|
if ($latestDetail->jabatan_fungsional_id) {
|
|
$riwayatJafung = JabatanFungsionalRiwayat::where('jabatan_fungsional_id', $latestDetail->jabatan_fungsional_id)
|
|
->orderBy('periode', 'desc')
|
|
->first();
|
|
|
|
if ($riwayatJafung) {
|
|
$gradeJafung = $riwayatJafung->grade ?? '-';
|
|
$hargaJafung = $riwayatJafung->harga ?? 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Get jabatan tambahan, grade, dan harga
|
|
if ($latestDetail->tambahan) {
|
|
$jabatanTambahan = $latestDetail->tambahan->nama;
|
|
|
|
// Query JabatanTugasTambahanRiwayat berdasarkan jabatan_tambahan_id
|
|
if ($latestDetail->jabatan_tambahan_id) {
|
|
$riwayatJtt = JabatanTugasTambahanRiwayat::where('jtt_id', $latestDetail->jabatan_tambahan_id)
|
|
->orderBy('periode', 'desc')
|
|
->first();
|
|
|
|
if ($riwayatJtt) {
|
|
$gradeJtt = $riwayatJtt->grade ?? '-';
|
|
$hargaJtt = $riwayatJtt->harga ?? 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$npwp = $pegawai->npwp ? str_replace(['-', '.'], '', $pegawai->npwp) : '';
|
|
|
|
$nomorRekening = $pegawai->nomor_rekening ?? '';
|
|
|
|
return [
|
|
'nip' => $pegawai->nip,
|
|
'nama' => $pegawai->nama,
|
|
'jenis_pegawai' => ucfirst($pegawai->jenis_pegawai),
|
|
'unit_kerja' => $pegawai->unit_kerja_nama,
|
|
'unit_pembayaran_id' => $unit,
|
|
'jabatan_fungsional' => $jabatanFungsional,
|
|
'jabatan_tambahan' => $jabatanTambahan,
|
|
'pangkat' => $pangkat,
|
|
'golongan' => $golongan,
|
|
'grade_jafung' => $gradeJafung,
|
|
'harga_jafung' => $hargaJafung,
|
|
'grade_jtt' => $gradeJtt,
|
|
'harga_jtt' => $hargaJtt,
|
|
'npwp' => $npwp,
|
|
'nomor_rekening' => $nomorRekening,
|
|
'p2_sebelum_rasio' => number_format($totalP2Bruto, 0, ',', '.'),
|
|
'p2' => number_format($totalP2Bruto, 0, ',', '.'),
|
|
'rasio' => '-',
|
|
'rasio_display' => '-',
|
|
'rasio_numeric' => 0,
|
|
'p2_sebelum_rasio_numeric' => $totalP2Bruto,
|
|
'p2_numeric' => $totalP2Bruto,
|
|
'pajak_numeric' => $totalPajak,
|
|
'p2_setelah_pajak_numeric' => $totalP2Bruto - $totalPajak,
|
|
];
|
|
})->filter(function ($item) {
|
|
// Filter hanya data dengan P2 sebelum rasio tidak 0
|
|
return $item['p2_sebelum_rasio_numeric'] > 0;
|
|
});
|
|
|
|
// Hitung total P2 normal (sebelum rasio) menggunakan formula yang diberikan
|
|
$totalP2Normal = $pegawais->sum(function ($pegawai) {
|
|
return $pegawai->jabatan->detail->sum(function ($detail) {
|
|
return $detail->remunerasi->sum('p2_bruto');
|
|
});
|
|
});
|
|
|
|
// Ambil rasio default dari database berdasarkan periode
|
|
$defaultRasio = Rasio::where('unit_kerja_id', $unit)
|
|
->where('periode', $this->periode)
|
|
->first();
|
|
|
|
// Apply rasio atau nominal filter
|
|
$calculatedRasio = null;
|
|
|
|
// Jika filter nominal diisi, hitung rasio berdasarkan nominal
|
|
if ($this->filterNominal !== null && $this->filterNominal !== '') {
|
|
$calculatedRasio = ($this->filterNominal / $totalP2Normal) * 100;
|
|
}
|
|
// Jika filter rasio diisi, gunakan rasio tersebut
|
|
elseif ($this->filterRasio !== null && $this->filterRasio !== '') {
|
|
$calculatedRasio = $this->filterRasio;
|
|
}
|
|
// Jika tidak ada filter, gunakan rasio default dari database
|
|
elseif ($defaultRasio) {
|
|
$calculatedRasio = $defaultRasio->rasio * 100; // Convert dari desimal ke persentase
|
|
}
|
|
|
|
// Apply rasio ke semua data jika ada
|
|
if ($calculatedRasio !== null) {
|
|
$data = $data->map(function ($item) use ($calculatedRasio, $pegawais) {
|
|
// Hitung P2 setelah rasio
|
|
$p2SetelahRasio = ($item['p2_sebelum_rasio_numeric'] * $calculatedRasio) / 100;
|
|
|
|
// Cari pegawai yang sesuai untuk mendapatkan data pajak
|
|
$pegawai = $pegawais->firstWhere('nip', $item['nip']);
|
|
$totalPajak = 0;
|
|
|
|
if ($pegawai && $pegawai->jabatan && $pegawai->jabatan->detail->isNotEmpty()) {
|
|
// Hitung pajak berdasarkan rasio yang sama
|
|
$totalPajakSebelumRasio = $pegawai->jabatan->detail->sum(function ($detail) {
|
|
return $detail->remunerasi->sum('p2_tax');
|
|
});
|
|
$totalPajak = ($totalPajakSebelumRasio * $calculatedRasio) / 100;
|
|
}
|
|
|
|
// Hitung P2 setelah pajak
|
|
$p2SetelahPajak = $p2SetelahRasio - $totalPajak;
|
|
|
|
$item['p2'] = number_format($p2SetelahRasio, 0, ',', '.');
|
|
$item['rasio'] = number_format($calculatedRasio, 1);
|
|
$item['rasio_display'] = number_format($calculatedRasio, 1).'%';
|
|
$item['rasio_numeric'] = $calculatedRasio;
|
|
$item['p2_numeric'] = $p2SetelahRasio;
|
|
$item['pajak_numeric'] = $totalPajak;
|
|
$item['p2_setelah_pajak_numeric'] = $p2SetelahPajak;
|
|
|
|
return $item;
|
|
});
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function download($filename = null)
|
|
{
|
|
// Increase time limit and memory for large exports
|
|
set_time_limit(900); // 15 minutes
|
|
ini_set('memory_limit', '1024M'); // 1GB
|
|
ini_set('max_execution_time', '900');
|
|
|
|
$spreadsheet = $this->export();
|
|
|
|
if (! $filename) {
|
|
$filename = 'rasio-p2-'.date('Y-m-d-H-i-s').'.xlsx';
|
|
}
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
|
|
// Set headers for download
|
|
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
header('Content-Disposition: attachment;filename="'.$filename.'"');
|
|
header('Cache-Control: max-age=0');
|
|
|
|
$writer->save('php://output');
|
|
exit;
|
|
}
|
|
}
|