diff --git a/app/Exports/P1PerbandinganExport.php b/app/Exports/P1PerbandinganExport.php new file mode 100644 index 00000000..3c88ca35 --- /dev/null +++ b/app/Exports/P1PerbandinganExport.php @@ -0,0 +1,337 @@ +jenisPegawai = $jenisPegawai; + $this->tahun = $tahun; + $this->bulan = $bulan; + $this->unit = $unit; + $this->arah = $arah; + } + + public function sheets(): array + { + $sheets = []; + $months = $this->bulan === null ? range(1, Carbon::now()->month) : [$this->bulan]; + + foreach ($months as $month) { + $sheets[] = new P1PerbandinganMonthlySheet($this->jenisPegawai, $this->tahun, $month, $this->unit, $this->arah); + } + + return $sheets; + } +} + +class P1PerbandinganMonthlySheet implements FromCollection, ShouldAutoSize, WithColumnFormatting, WithHeadings, WithMapping, WithStyles, WithTitle +{ + protected $jenisPegawai; + + protected $tahun; + + protected $bulan; + + protected $unit; + + protected $arah; + + private $cachedCollection; + + private array $totals = []; + + protected $bulanNames = [ + 1 => 'Januari', + 2 => 'Februari', + 3 => 'Maret', + 4 => 'April', + 5 => 'Mei', + 6 => 'Juni', + 7 => 'Juli', + 8 => 'Agustus', + 9 => 'September', + 10 => 'Oktober', + 11 => 'November', + 12 => 'Desember', + ]; + + public function __construct($jenisPegawai, $tahun, $bulan, $unit, $arah = '') + { + $this->jenisPegawai = $jenisPegawai; + $this->tahun = $tahun; + $this->bulan = $bulan; + $this->unit = $unit; + $this->arah = $arah; + } + + public function title(): string + { + return $this->bulanNames[$this->bulan]; + } + + public function collection() + { + if ($this->cachedCollection !== null) { + return $this->cachedCollection; + } + + return $this->cachedCollection = $this->buildCollection(); + } + + private function baseQuery(int $queryYear, int $queryMonth) + { + return P1::query() + ->where('tahun', $queryYear) + ->where('bulan', $queryMonth) + ->when($this->unit !== null && $this->unit !== '', fn ($query) => $query->where('id_unit_kerja', $this->unit)) + ->when($this->jenisPegawai === 'dosen' || $this->jenisPegawai === 'tendik', fn ($query) => $query->whereRaw('LOWER(jenis_pegawai) = ?', [$this->jenisPegawai])) + ->get() + ->keyBy('nip'); + } + + private function keteranganLabel(float $selisihNetto): string + { + return match (true) { + $selisihNetto > 0 => 'Naik', + $selisihNetto < 0 => 'Turun', + default => 'Tetap', + }; + } + + private function buildCollection() + { + $previousMonth = $this->bulan === 1 ? 12 : $this->bulan - 1; + $previousYear = $this->bulan === 1 ? $this->tahun - 1 : $this->tahun; + + $current = $this->baseQuery($this->tahun, $this->bulan); + $previous = $this->baseQuery($previousYear, $previousMonth); + $nips = $current->keys()->merge($previous->keys())->unique(); + + $comparison = $nips->map(function ($nip) use ($current, $previous) { + $currentRow = $current->get($nip); + $previousRow = $previous->get($nip); + + $currentBruto = (float) ($currentRow->gaji_yang_dibayarkan ?? 0); + $previousBruto = (float) ($previousRow->gaji_yang_dibayarkan ?? 0); + $currentPajak = (float) ($currentRow->pph21 ?? 0); + $previousPajak = (float) ($previousRow->pph21 ?? 0); + $currentNetto = (float) ($currentRow->gaji_bersih ?? 0); + $previousNetto = (float) ($previousRow->gaji_bersih ?? 0); + + $selisihBruto = $currentBruto - $previousBruto; + $selisihPajak = $currentPajak - $previousPajak; + $selisihNetto = $currentNetto - $previousNetto; + + $acuan = $currentRow ?? $previousRow; + $cleanNip = ltrim((string) $nip, "'"); + + return (object) [ + 'no' => 0, + 'nip' => "'".$cleanNip, + 'nama_lengkap' => $acuan->nama ?? '-', + 'pangkat' => $acuan->pangkat ?? '-', + 'golongan' => $acuan->golongan ?? '-', + 'nama_jafung' => $acuan->nama_jabatan_fungsional ?? '-', + 'grade_jafung' => $acuan->grade_jabatan_fungsional ?? '-', + 'harga_jafung' => $acuan->gaji_jabatan_fungsional ?? 0, + 'nama_jtt' => $acuan->nama_jabatan_tugas_tambahan ?: '-', + 'grade_jtt' => $acuan->grade_jabatan_tugas_tambahan ?? '-', + 'harga_jtt' => $acuan->gaji_jabatan_tugas_tambahan ?? 0, + 'jenis_pegawai' => strtolower((string) ($acuan->jenis_pegawai ?? '')), + 'selisih_bruto' => $selisihBruto, + 'selisih_pajak' => $selisihPajak, + 'selisih_netto' => $selisihNetto, + 'keterangan' => $this->keteranganLabel($selisihNetto), + 'npwp' => $acuan->npwp ? "'".str_replace(['-', '.'], '', $acuan->npwp) : '', + 'nomor_rekening' => $acuan->nomor_rekening ? "'".$acuan->nomor_rekening : '', + 'nama_unit' => $acuan->nama_unit_kerja ?? '-', + ]; + })->filter(function ($row) { + $values = [$row->selisih_bruto, $row->selisih_pajak, $row->selisih_netto]; + + return match ($this->arah) { + 'diatas' => collect($values)->contains(fn ($value) => $value > 0), + 'dibawah' => collect($values)->contains(fn ($value) => $value < 0), + default => collect($values)->contains(fn ($value) => $value != 0), + }; + })->values(); + + $sortedData = $comparison->sortBy('nama_lengkap')->values(); + + $sortedData = $sortedData->map(function ($item, $index) { + $item->no = $index + 1; + + return $item; + }); + + $this->totals = [ + 'total_kenaikan_netto' => $sortedData->filter(fn ($row) => $row->selisih_netto > 0)->sum('selisih_netto'), + 'total_penurunan_netto' => $sortedData->filter(fn ($row) => $row->selisih_netto < 0)->sum('selisih_netto'), + 'total_kenaikan_bruto' => $sortedData->filter(fn ($row) => $row->selisih_bruto > 0)->sum('selisih_bruto'), + 'total_penurunan_bruto' => $sortedData->filter(fn ($row) => $row->selisih_bruto < 0)->sum('selisih_bruto'), + ]; + + if ($sortedData->count() > 0) { + for ($spacer = 0; $spacer < 3; $spacer++) { + $sortedData->push((object) ['is_spacer' => true]); + } + + $summary = [ + 'Total Kenaikan P1' => 'total_kenaikan_bruto', + 'Total Penurunan P1' => 'total_penurunan_bruto', + 'Total Kenaikan P1 Setelah Pajak' => 'total_kenaikan_netto', + 'Total Penurunan P1 Setelah Pajak' => 'total_penurunan_netto', + ]; + + foreach ($summary as $label => $key) { + $sortedData->push((object) [ + 'is_summary' => true, + 'nama_lengkap' => $label, + 'summary_value' => $this->totals[$key] ?? 0, + ]); + } + } + + return $sortedData; + } + + public function headings(): array + { + return [ + 'No.', + 'Nama', + 'NIP', + 'Pangkat', + 'Golongan', + 'Jabatan Fungsional', + 'Grade Jabatan Fungsional', + 'Harga Jabatan Fungsional', + 'Jabatan Tugas Tambahan', + 'Grade Jabatan Tugas Tambahan', + 'Harga Jabatan Tugas Tambahan', + 'Jenis Pegawai', + 'Selisih Bruto', + 'Selisih Pajak', + 'Selisih Netto', + 'Keterangan', + 'NPWP', + 'Nomor Rekening', + 'Unit Kerja', + ]; + } + + public function map($row): array + { + if ($row->is_spacer ?? false) { + return array_fill(0, 19, ''); + } + + if ($row->is_summary ?? false) { + return [ + '', + $row->nama_lengkap.':', + $row->summary_value, + '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', + ]; + } + + return [ + $row->no, + $row->nama_lengkap, + $row->nip, + $row->pangkat, + $row->golongan, + $row->nama_jafung, + $row->grade_jafung, + $row->harga_jafung, + $row->nama_jtt, + $row->grade_jtt, + $row->harga_jtt, + $row->jenis_pegawai == 'dosen' ? 'Dosen' : ($row->jenis_pegawai == 'tendik' ? 'Tendik' : '-'), + $row->selisih_bruto ?: 0, + $row->selisih_pajak ?: 0, + $row->selisih_netto ?: 0, + $row->keterangan ?: '-', + $row->npwp, + $row->nomor_rekening, + $row->nama_unit, + ]; + } + + public function columnFormats(): array + { + $accountingFormat = '_("Rp"\ * #,##0.00_);_("Rp"\ * \-#,##0.00_);_("Rp"\ * "-"??_);_(@_)'; + + return [ + 'C' => '@', // NIP as text + 'H' => $accountingFormat, // Harga Jabatan Fungsional + 'K' => $accountingFormat, // Harga Jabatan Tugas Tambahan + 'M' => $accountingFormat, // Selisih Bruto + 'N' => $accountingFormat, // Selisih Pajak + 'O' => $accountingFormat, // Selisih Netto + 'Q' => '@', // NPWP as text + 'R' => '@', // Nomor Rekening as text + ]; + } + + public function styles(Worksheet $sheet) + { + $dataCount = $this->collection()->count(); + $summaryStartRow = $dataCount - 2; + $accountingFormat = '_("Rp"\ * #,##0.00_);_("Rp"\ * \-#,##0.00_);_("Rp"\ * "-"??_);_(@_)'; + + $styles = [ + 1 => [ + 'font' => ['bold' => true, 'color' => ['rgb' => 'FFFFFF']], + 'fill' => [ + 'fillType' => Fill::FILL_SOLID, + 'startColor' => ['rgb' => '4472C4'], + ], + 'alignment' => [ + 'horizontal' => Alignment::HORIZONTAL_CENTER, + 'vertical' => Alignment::VERTICAL_CENTER, + ], + ], + ]; + + if ($dataCount > 0) { + for ($rowNumber = $summaryStartRow; $rowNumber <= $summaryStartRow + 3; $rowNumber++) { + $styles['B'.$rowNumber] = [ + 'font' => ['bold' => true], + ]; + $styles['C'.$rowNumber] = [ + 'font' => ['bold' => true], + 'numberFormat' => ['formatCode' => $accountingFormat], + ]; + } + } + + return $styles; + } +} diff --git a/app/Http/Controllers/Perhitungan/Remunerasi/P1Controller.php b/app/Http/Controllers/Perhitungan/Remunerasi/P1Controller.php index a1fa6f2e..5516ad19 100644 --- a/app/Http/Controllers/Perhitungan/Remunerasi/P1Controller.php +++ b/app/Http/Controllers/Perhitungan/Remunerasi/P1Controller.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Perhitungan\Remunerasi; use App\Exports\P1Export; +use App\Exports\P1PerbandinganExport; use App\Exports\P1SelisihExport; use App\Http\Controllers\Controller; use App\Models\Rekapitulasi\P1; @@ -536,6 +537,58 @@ class P1Controller extends Controller return Excel::download(new P1SelisihExport($jenisPegawai, $tahun, $bulan, $unit, $includePppk2025), $filename); } + public function exportPerbandingan(Request $request): BinaryFileResponse + { + set_time_limit(300); + + $datenow = Carbon::now(); + $bulanNow = $datenow->format('m'); + $tahunNow = $datenow->format('Y'); + $team = Team::find(session('team_id')); + + if ($request->has('unit_kerja') && $request->unit_kerja !== null) { + $unit = $request->unit_kerja; + } elseif ($team && $team->unit_kerja_id !== null && $team->unit_kerja_id !== 0) { + $unit = $team->unit_kerja_id; + } else { + $unit = $request->unit_kerja; + } + + $jenisPegawai = $request->jenis_pegawai ?? ''; + $tahun = (int) ($request->tahun ?? $tahunNow); + $bulan = $request->bulan ?? null; + $arah = $request->arah ?? ''; + + $unitIdForModel = ($unit == 99) ? 0 : $unit; + + $bulanNames = [ + 1 => 'Januari', 2 => 'Februari', 3 => 'Maret', 4 => 'April', 5 => 'Mei', 6 => 'Juni', + 7 => 'Juli', 8 => 'Agustus', 9 => 'September', 10 => 'Oktober', 11 => 'November', 12 => 'Desember', + ]; + + $unitName = ''; + if ($unit) { + $unitKerja = UnitKerja::find($unitIdForModel); + if ($unitKerja) { + $unitName = '_'.str_replace(['/', ' ', '.'], '_', $unitKerja->nama); + } + } else { + $unitName = '_Semua_Unit_Kerja'; + } + + if (! $bulan) { + $namaBulanSekarang = $bulanNames[(int) $bulanNow]; + $filename = "P1_Perbandingan_Januari_sampai_{$namaBulanSekarang}_{$tahun}{$unitName}.xlsx"; + + return Excel::download(new P1PerbandinganExport($jenisPegawai, $tahun, null, $unitIdForModel, $arah), $filename); + } + + $bulanName = $bulanNames[(int) $bulan]; + $filename = "P1_Perbandingan_{$bulanName}_{$tahun}{$unitName}.xlsx"; + + return Excel::download(new P1PerbandinganExport($jenisPegawai, $tahun, (int) $bulan, $unitIdForModel, $arah), $filename); + } + public function logs(Request $request): JsonResponse { Log::info('Export P1 Log:', $request->all()); diff --git a/resources/views/perhitungan/remunerasi/p1/index.blade.php b/resources/views/perhitungan/remunerasi/p1/index.blade.php index 6f7c2ec2..824eb8ee 100644 --- a/resources/views/perhitungan/remunerasi/p1/index.blade.php +++ b/resources/views/perhitungan/remunerasi/p1/index.blade.php @@ -148,6 +148,7 @@ 'actionButton' => ' Export Excel Export Excel + Export Excel ' ]) @@ -240,6 +241,7 @@