411 lines
16 KiB
PHP
411 lines
16 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Perhitungan\Remunerasi;
|
|
|
|
use App\Exports\P1Export;
|
|
use App\Exports\P1SelisihExport;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Verifikator\Team;
|
|
use App\Models\Verifikator\UnitKerja;
|
|
use App\Services\DawaiService;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\View\View;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class P1Controller extends Controller
|
|
{
|
|
public function index(): View
|
|
{
|
|
$title = 'P1';
|
|
$team = Team::find(session('team_id'));
|
|
|
|
if (! $team || ! isset($team->unit_kerja_id) || $team->unit_kerja_id === null || $team->unit_kerja_id === 0) {
|
|
$units = UnitKerja::get();
|
|
|
|
return view('perhitungan.remunerasi.p1.index', compact('units', 'team', 'title'));
|
|
}
|
|
|
|
return view('perhitungan.remunerasi.p1.index', compact('team', 'title'));
|
|
}
|
|
|
|
public function data(Request $request): JsonResponse
|
|
{
|
|
$datenow = Carbon::now();
|
|
$bulanNow = $datenow->format('m');
|
|
$tahunNow = $datenow->format('Y');
|
|
$team = Team::find(session('team_id'));
|
|
|
|
if (! $team) {
|
|
return DataTables::of(collect([]))->make();
|
|
}
|
|
|
|
if (! isset($team->unit_kerja_id) || $team->unit_kerja_id === null || $team->unit_kerja_id === 0) {
|
|
$unit = $request->unit;
|
|
} else {
|
|
$unit = $team->unit_kerja_id;
|
|
}
|
|
|
|
$jenisPegawai = $request->jenis_pegawai ?? '';
|
|
$tahun = $request->tahun ?? $tahunNow;
|
|
$bulan = $request->bulan ?? $bulanNow;
|
|
|
|
$cacheKey = "p1_data_{$jenisPegawai}_{$tahun}_{$bulan}_{$unit}";
|
|
|
|
$data = Cache::remember($cacheKey, now()->addDay(), function () use ($jenisPegawai, $tahun, $bulan, $unit) {
|
|
$dawaiService = new DawaiService;
|
|
|
|
return $dawaiService->getDataP1($jenisPegawai, $tahun, $bulan, $unit);
|
|
});
|
|
|
|
$includePppk2025 = $request->boolean('include_pppk_2025');
|
|
|
|
$filteredData = collect($data)
|
|
->filter(fn ($item) => isset($item['harga_p1']) && $item['harga_p1'] > 0)
|
|
->filter(fn ($item) => $includePppk2025 || ! str_contains($item['nip'] ?? '', '202521'));
|
|
|
|
return DataTables::of($filteredData)
|
|
->addIndexColumn()
|
|
->editColumn('nip', function ($row) {
|
|
return ltrim($row['nip'] ?? '', "'");
|
|
})
|
|
->editColumn('nama_jtt', function ($row) {
|
|
return $row['nama_jtt'] ?: '-';
|
|
})
|
|
->editColumn('jenis_pegawai', function ($row) {
|
|
return $row['jenis_pegawai'] == 'dosen' ? 'Dosen' : ($row['jenis_pegawai'] == 'tendik' ? 'Tendik' : '-');
|
|
})
|
|
->editColumn('harga_p1', function ($row) {
|
|
return 'Rp. '.number_format($row['harga_p1'] ?: 0, 2, ',', '.');
|
|
})
|
|
->editColumn('pajak_p1', function ($row) {
|
|
return 'Rp. '.number_format($row['pajak_p1'] ?: 0, 2, ',', '.');
|
|
})
|
|
->editColumn('harga_p1_setelah_pajak', function ($row) {
|
|
return 'Rp. '.number_format($row['harga_p1_setelah_pajak'] ?: 0, 2, ',', '.');
|
|
})
|
|
->make();
|
|
}
|
|
|
|
public function dataSelisih(Request $request): JsonResponse
|
|
{
|
|
$datenow = Carbon::now();
|
|
$bulanNow = $datenow->format('m');
|
|
$tahunNow = $datenow->format('Y');
|
|
$team = Team::find(session('team_id'));
|
|
|
|
if (! $team) {
|
|
return DataTables::of(collect([]))->make();
|
|
}
|
|
|
|
if (! isset($team->unit_kerja_id) || $team->unit_kerja_id === null || $team->unit_kerja_id === 0) {
|
|
$unit = $request->unit;
|
|
} else {
|
|
$unit = $team->unit_kerja_id;
|
|
}
|
|
|
|
$jenisPegawai = $request->jenis_pegawai ?? '';
|
|
$tahun = $request->tahun ?? $tahunNow;
|
|
$bulan = $request->bulan ?? $bulanNow;
|
|
$jenisSelisih = $request->jenis_selisih ?? '';
|
|
$includePppk2025 = $request->boolean('include_pppk_2025');
|
|
|
|
$cacheKey = "p1_selisih_data_{$jenisPegawai}_{$tahun}_{$bulan}_{$unit}";
|
|
|
|
$data = Cache::remember($cacheKey, now()->addDay(), function () use ($jenisPegawai, $tahun, $bulan, $unit) {
|
|
$dawaiService = new DawaiService;
|
|
|
|
return $dawaiService->getDataP1Selisih($jenisPegawai, $tahun, $bulan, $unit);
|
|
});
|
|
|
|
$filteredData = collect($data)->filter(function ($item) use ($jenisSelisih, $includePppk2025) {
|
|
if (! isset($item['harga_p1']) || $item['harga_p1'] <= 0) {
|
|
return false;
|
|
}
|
|
|
|
if ($jenisSelisih !== '' && $this->resolveJenisSelisih($item) !== $jenisSelisih) {
|
|
return false;
|
|
}
|
|
|
|
if (! $includePppk2025 && str_contains($item['nip'] ?? '', '202521')) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
});
|
|
|
|
return DataTables::of($filteredData)
|
|
->addIndexColumn()
|
|
->editColumn('nip', function ($row) {
|
|
return ltrim($row['nip'] ?? '', "'");
|
|
})
|
|
->editColumn('jenis_pegawai', function ($row) {
|
|
return $row['jenis_pegawai'] == 'dosen' ? 'Dosen' : ($row['jenis_pegawai'] == 'tendik' ? 'Tendik' : '-');
|
|
})
|
|
->addColumn('jenis_selisih', function ($row) {
|
|
return $this->jenisSelisihLabel($this->resolveJenisSelisih($row));
|
|
})
|
|
->addColumn('selisih_bruto', function ($row) {
|
|
$value = $row['selisih_harga_p1'] ?? 0;
|
|
|
|
return 'Rp. '.number_format($value, 2, ',', '.');
|
|
})
|
|
->addColumn('selisih_pajak', function ($row) {
|
|
$value = ($row['selisih_harga_p1'] ?? 0) - ($row['selisih_harga_p1_setelah_pajak'] ?? 0);
|
|
|
|
return 'Rp. '.number_format($value, 2, ',', '.');
|
|
})
|
|
->addColumn('selisih_netto', function ($row) {
|
|
$value = $row['selisih_harga_p1_setelah_pajak'] ?? 0;
|
|
|
|
return 'Rp. '.number_format($value, 2, ',', '.');
|
|
})
|
|
->addColumn('detail', function ($row) {
|
|
return json_encode([
|
|
'riwayat' => $this->buildRiwayatSelisihDetail($row),
|
|
]);
|
|
})
|
|
->rawColumns(['detail'])
|
|
->make();
|
|
}
|
|
|
|
/**
|
|
* Bangun rincian per riwayat pembayaran untuk modal Detail Selisih: tiap entri riwayat
|
|
* (P1 yang pernah dibayarkan) dibandingkan dengan P1 saat ini (P1 yang seharusnya
|
|
* dibayarkan), supaya histori pembayaran dan selisihnya tampil bersamaan per index.
|
|
*/
|
|
private function buildRiwayatSelisihDetail(array $row): array
|
|
{
|
|
$brutoBaru = $row['harga_p1'] ?? 0;
|
|
$nettoBaru = $row['harga_p1_setelah_pajak'] ?? 0;
|
|
|
|
$p1Baru = [
|
|
'jafung' => $row['nama_jafung'] ?? '-',
|
|
'jtt' => $row['nama_jtt'] ?: '-',
|
|
'status_pegawai' => $row['status_pegawai'] ?? '-',
|
|
'golongan' => $row['golongan'] ?? '-',
|
|
'bruto' => 'Rp. '.number_format($brutoBaru, 2, ',', '.'),
|
|
'pajak' => 'Rp. '.number_format($brutoBaru - $nettoBaru, 2, ',', '.'),
|
|
'netto' => 'Rp. '.number_format($nettoBaru, 2, ',', '.'),
|
|
];
|
|
|
|
return collect($row['riwayat_pembayaran'] ?? [])->map(function ($r) use ($p1Baru, $brutoBaru, $nettoBaru) {
|
|
$brutoLama = $r['harga_p1'] ?? 0;
|
|
$nettoLama = $r['harga_p1_setelah_pajak'] ?? 0;
|
|
|
|
// Ikuti konvensi selisih milik API Dawai sendiri (selisih_harga_p1 / selisih_harga_p1_setelah_pajak
|
|
// di level row = lama - baru): positif = Kelebihan (dulu dibayar lebih besar dari yang seharusnya
|
|
// sekarang), negatif = Kekurangan. Netto (bukan bruto) dipakai untuk menentukan Keterangan karena
|
|
// bruto bisa saja tidak berubah walau netto berubah akibat golongan/tarif pajak berubah.
|
|
$selisihBruto = $brutoLama - $brutoBaru;
|
|
$selisihNetto = $nettoLama - $nettoBaru;
|
|
|
|
$golonganLama = $r['golongan'] ?? null;
|
|
$golonganBaru = $row['golongan'] ?? null;
|
|
|
|
// Bedakan sumber selisih: kalau bruto (nominal P1-nya sendiri, mis. akibat jafung/jtt berubah)
|
|
// ikut berbeda, sebut itu selisih P1. Kalau bruto sama tapi netto beda, berarti selisihnya
|
|
// murni dari pajak (biasanya karena golongan berubah sehingga tarif pajaknya ikut berubah).
|
|
$keterangan = match (true) {
|
|
$selisihBruto != 0 => ($selisihBruto < 0 ? 'Kekurangan' : 'Kelebihan').' P1',
|
|
$selisihNetto != 0 => ($selisihNetto < 0 ? 'Kekurangan' : 'Kelebihan').' Pajak'
|
|
.($golonganLama !== null && $golonganBaru !== null && $golonganLama !== $golonganBaru
|
|
? ' (Perubahan Golongan)'
|
|
: ''),
|
|
default => '-',
|
|
};
|
|
|
|
return [
|
|
'p1_lama' => [
|
|
'jafung' => $r['nama_jafung'] ?? '-',
|
|
'jtt' => $r['nama_jtt'] ?: '-',
|
|
'status_pegawai' => $r['status_pegawai'] ?? '-',
|
|
'golongan' => $r['golongan'] ?? '-',
|
|
'bruto' => 'Rp. '.number_format($brutoLama, 2, ',', '.'),
|
|
'pajak' => 'Rp. '.number_format($brutoLama - $nettoLama, 2, ',', '.'),
|
|
'netto' => 'Rp. '.number_format($nettoLama, 2, ',', '.'),
|
|
],
|
|
'p1_baru' => $p1Baru,
|
|
'keterangan' => $keterangan,
|
|
'selisih_bruto' => 'Rp. '.number_format($selisihBruto, 2, ',', '.'),
|
|
'selisih_pajak' => 'Rp. '.number_format($selisihBruto - $selisihNetto, 2, ',', '.'),
|
|
'selisih_netto' => 'Rp. '.number_format($selisihNetto, 2, ',', '.'),
|
|
];
|
|
})->values()->all();
|
|
}
|
|
|
|
/**
|
|
* Tentukan jenis selisih ('kekurangan'/'kelebihan') berdasarkan field status_selisih_harga_p1
|
|
* yang dikirim langsung oleh API Dawai (nilainya: 'Kekurangan', 'Kelebihan', atau 'Tidak Ada').
|
|
*/
|
|
private function resolveJenisSelisih(array $item): ?string
|
|
{
|
|
$statusBruto = $item['status_selisih_harga_p1'] ?? null;
|
|
$statusNetto = $item['status_selisih_harga_p1_setelah_pajak'] ?? null;
|
|
|
|
// Bruto (nominal P1-nya sendiri) diutamakan. Kalau bruto tidak ada bedanya tapi netto
|
|
// ada (mis. karena golongan berubah sehingga tarif pajak ikut berubah), pakai status netto
|
|
// supaya kasus selisih murni pajak tetap kelihatan di kolom/filter Keterangan.
|
|
$status = in_array($statusBruto, ['Kekurangan', 'Kelebihan'], true) ? $statusBruto : $statusNetto;
|
|
|
|
return match ($status) {
|
|
'Kekurangan' => 'kekurangan',
|
|
'Kelebihan' => 'kelebihan',
|
|
default => null,
|
|
};
|
|
}
|
|
|
|
private function jenisSelisihLabel(?string $jenisSelisih): string
|
|
{
|
|
return match ($jenisSelisih) {
|
|
'kekurangan' => 'Kekurangan',
|
|
'kelebihan' => 'Kelebihan',
|
|
default => '-',
|
|
};
|
|
}
|
|
|
|
public function export(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 = $request->tahun ?? $tahunNow;
|
|
$bulan = $request->bulan ?? null;
|
|
$includePppk2025 = $request->boolean('include_pppk_2025');
|
|
|
|
$unitIdForModel = ($unit == 99) ? 0 : $unit;
|
|
|
|
if (! $bulan) {
|
|
$unitName = '';
|
|
if ($unit) {
|
|
$unitKerja = UnitKerja::find($unitIdForModel);
|
|
if ($unitKerja) {
|
|
$unitName = '_'.str_replace(['/', ' ', '.'], '_', $unitKerja->nama);
|
|
}
|
|
} else {
|
|
$unitName = '_Semua_Unit_Kerja';
|
|
}
|
|
|
|
$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',
|
|
];
|
|
$namaBulanSekarang = $bulanNames[(int) $bulanNow];
|
|
$filename = "P1_Januari_sampai_{$namaBulanSekarang}_{$tahun}{$unitName}.xlsx";
|
|
|
|
return Excel::download(new P1Export($jenisPegawai, $tahun, null, $unit, $includePppk2025), $filename);
|
|
}
|
|
|
|
$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',
|
|
];
|
|
$bulanName = $bulanNames[(int) $bulan];
|
|
|
|
$unitName = '';
|
|
if ($unit) {
|
|
$unitKerja = UnitKerja::find($unitIdForModel);
|
|
if ($unitKerja) {
|
|
$unitName = '_'.str_replace(['/', ' ', '.'], '_', $unitKerja->nama);
|
|
}
|
|
} else {
|
|
$unitName = '_Semua_Unit_Kerja';
|
|
}
|
|
|
|
$filename = "P1_{$bulanName}_{$tahun}{$unitName}.xlsx";
|
|
|
|
return Excel::download(new P1Export($jenisPegawai, $tahun, $bulan, $unit, $includePppk2025), $filename);
|
|
}
|
|
|
|
public function exportSelisih(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 = $request->tahun ?? $tahunNow;
|
|
$bulan = $request->bulan ?? null;
|
|
$includePppk2025 = $request->boolean('include_pppk_2025');
|
|
|
|
$unitIdForModel = ($unit == 99) ? 0 : $unit;
|
|
|
|
if (! $bulan) {
|
|
$unitName = '';
|
|
if ($unit) {
|
|
$unitKerja = UnitKerja::find($unitIdForModel);
|
|
if ($unitKerja) {
|
|
$unitName = '_'.str_replace(['/', ' ', '.'], '_', $unitKerja->nama);
|
|
}
|
|
} else {
|
|
$unitName = '_Semua_Unit_Kerja';
|
|
}
|
|
|
|
$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',
|
|
];
|
|
$namaBulanSekarang = $bulanNames[(int) $bulanNow];
|
|
$filename = "P1_Selisih_Januari_sampai_{$namaBulanSekarang}_{$tahun}{$unitName}.xlsx";
|
|
|
|
return Excel::download(new P1SelisihExport($jenisPegawai, $tahun, null, $unit, $includePppk2025), $filename);
|
|
}
|
|
|
|
$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',
|
|
];
|
|
$bulanName = $bulanNames[(int) $bulan];
|
|
|
|
$unitName = '';
|
|
if ($unit) {
|
|
$unitKerja = UnitKerja::find($unitIdForModel);
|
|
if ($unitKerja) {
|
|
$unitName = '_'.str_replace(['/', ' ', '.'], '_', $unitKerja->nama);
|
|
}
|
|
} else {
|
|
$unitName = '_Semua_Unit_Kerja';
|
|
}
|
|
|
|
$filename = "P1_Selisih_{$bulanName}_{$tahun}{$unitName}.xlsx";
|
|
|
|
return Excel::download(new P1SelisihExport($jenisPegawai, $tahun, $bulan, $unit, $includePppk2025), $filename);
|
|
}
|
|
|
|
public function logs(Request $request): JsonResponse
|
|
{
|
|
Log::info('Export P1 Log:', $request->all());
|
|
|
|
return response()->json(['status' => 'success', 'message' => 'Log berhasil disimpan']);
|
|
}
|
|
}
|