317 lines
12 KiB
PHP
317 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Perhitungan\Remunerasi;
|
|
|
|
use App\Exports\ThrExport;
|
|
use App\Exports\ThrSelisihExport;
|
|
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\Request;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class THRController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$title = 'THR';
|
|
$team = Team::find(session('team_id'));
|
|
|
|
if ($team->unit_kerja_id === null || $team->unit_kerja_id === 0) {
|
|
$units = UnitKerja::get();
|
|
|
|
return view('perhitungan.remunerasi.thr.index', compact('units', 'team', 'title'));
|
|
}
|
|
|
|
return view('perhitungan.remunerasi.thr.index', compact('team', 'title'));
|
|
}
|
|
|
|
public function data(Request $request)
|
|
{
|
|
$tahunNow = Carbon::now()->format('Y');
|
|
$team = Team::find(session('team_id'));
|
|
|
|
if ($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;
|
|
|
|
// Create cache key based on parameters
|
|
$cacheKey = "thr_data_{$jenisPegawai}_{$tahun}_{$unit}";
|
|
|
|
// Get data from cache or fetch from API
|
|
$data = Cache::remember($cacheKey, now()->addDay(), function () use ($jenisPegawai, $tahun, $unit) {
|
|
$dawaiService = new DawaiService;
|
|
|
|
return $dawaiService->getDataThr($jenisPegawai, $tahun, $unit, false);
|
|
});
|
|
|
|
// Filter data yang total_thr nya tidak nol
|
|
$filteredData = collect($data)->filter(function ($item) {
|
|
return ($item['total_thr'] ?? 0) > 0;
|
|
})->sortBy('nama')->values();
|
|
|
|
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('total_thr_formatted', function ($row) {
|
|
return 'Rp. '.number_format($row['total_thr'] ?? 0, 2, ',', '.');
|
|
})
|
|
->addColumn('total_pajak_thr_formatted', function ($row) {
|
|
return 'Rp. '.number_format($row['total_pajak_thr'] ?? 0, 2, ',', '.');
|
|
})
|
|
->addColumn('total_thr_setelah_pajak_formatted', function ($row) {
|
|
return 'Rp. '.number_format($row['total_thr_setelah_pajak'] ?? 0, 2, ',', '.');
|
|
})
|
|
->addColumn('detail', function ($row) {
|
|
return json_encode([
|
|
'golongan' => $row['golongan'] ?? '-',
|
|
'status_pegawai' => $row['status_pegawai'] ?? '-',
|
|
'poin_p2' => $row['poin_p2'] ?? '-',
|
|
'faktor' => $row['faktor'] ?? '-',
|
|
'jabatan_fungsional' => $row['jabatan_fungsional'] ?? '-',
|
|
'jabatan_tugas_tambahan' => $row['jabatan_tugas_tambahan'] ?? '-',
|
|
'thr_p1' => $row['thr_p1'] ?? [],
|
|
'thr_p2' => $row['thr_p2'] ?? [],
|
|
'steps' => $row['steps'] ?? [],
|
|
]);
|
|
})
|
|
->rawColumns(['detail'])
|
|
->make();
|
|
}
|
|
|
|
public function dataSelisih(Request $request)
|
|
{
|
|
$tahunNow = Carbon::now()->format('Y');
|
|
$team = Team::find(session('team_id'));
|
|
|
|
if ($team->unit_kerja_id === null) {
|
|
$unit = $request->unit;
|
|
} else {
|
|
$unit = $team->unit_kerja_id;
|
|
}
|
|
|
|
$jenisPegawai = $request->jenis_pegawai ?? '';
|
|
$tahun = $request->tahun ?? $tahunNow;
|
|
$jenisSelisih = $request->jenis_selisih ?? '';
|
|
|
|
// Create cache key for selisih data
|
|
$cacheKey = "thr_selisih_data_{$jenisPegawai}_{$tahun}_{$unit}";
|
|
|
|
// Get data from cache or fetch from API
|
|
$data = Cache::remember($cacheKey, now()->addDay(), function () use ($jenisPegawai, $tahun, $unit) {
|
|
$dawaiService = new DawaiService;
|
|
|
|
return $dawaiService->getDataThr($jenisPegawai, $tahun, $unit, true);
|
|
});
|
|
|
|
// Filter data yang total_thr nya tidak nol
|
|
$filteredData = collect($data)->filter(function ($item) use ($jenisSelisih) {
|
|
if (($item['total_thr'] ?? 0) <= 0) {
|
|
return false;
|
|
}
|
|
|
|
if ($jenisSelisih !== '' && $this->resolveJenisSelisih($item) !== $jenisSelisih) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
})->sortBy('nama')->values();
|
|
|
|
return DataTables::of($filteredData)
|
|
->addIndexColumn()
|
|
->addColumn('nip', function ($row) {
|
|
return ltrim($row['nip'] ?? '', "'");
|
|
})
|
|
->addColumn('nama', function ($row) {
|
|
return $row['nama'] ?? '';
|
|
})
|
|
->addColumn('nama_unit', function ($row) {
|
|
return $row['nama_unit'] ?? '';
|
|
})
|
|
->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_thr_formatted', function ($row) {
|
|
$value = $row['selisih_thr'] ?? 0;
|
|
|
|
return 'Rp. '.number_format($value, 2, ',', '.');
|
|
})
|
|
->addColumn('selisih_pajak_formatted', function ($row) {
|
|
$selisihBruto = $row['selisih_thr'] ?? 0;
|
|
$selisihNetto = $row['selisih_thr_netto'] ?? 0;
|
|
$value = $selisihBruto - $selisihNetto;
|
|
|
|
return 'Rp. '.number_format($value, 2, ',', '.');
|
|
})
|
|
->addColumn('selisih_total_formatted', function ($row) {
|
|
$value = $row['selisih_thr_netto'] ?? 0;
|
|
|
|
return 'Rp. '.number_format($value, 2, ',', '.');
|
|
})
|
|
->addColumn('detail', function ($row) {
|
|
return json_encode([
|
|
'golongan' => $row['golongan'] ?? '-',
|
|
'status_pegawai' => $row['status_pegawai'] ?? '-',
|
|
'poin_p2' => $row['poin_p2'] ?? '-',
|
|
'faktor' => $row['faktor'] ?? '-',
|
|
'jabatan_fungsional' => $row['jabatan_fungsional'] ?? '-',
|
|
'jabatan_tugas_tambahan' => $row['jabatan_tugas_tambahan'] ?? '-',
|
|
'thr_p1' => $row['thr_p1'] ?? [],
|
|
'thr_p2' => $row['thr_p2'] ?? [],
|
|
'steps' => $row['steps'] ?? [],
|
|
]);
|
|
})
|
|
->rawColumns(['detail'])
|
|
->make();
|
|
}
|
|
|
|
/**
|
|
* API Dawai untuk THR tidak mengirim status selisih eksplisit (beda dengan P1 yang punya
|
|
* status_selisih_harga_p1), jadi jenis selisih diturunkan dari tanda selisih bruto:
|
|
* negatif = kekurangan (pegawai dibayar kurang), positif = kelebihan, nol/tidak ada = null.
|
|
*/
|
|
private function resolveJenisSelisih(array $item): ?string
|
|
{
|
|
$selisihBruto = $item['selisih_thr'] ?? 0;
|
|
|
|
// Kalau bruto tidak ada bedanya tapi netto ada (mis. karena golongan berubah sehingga
|
|
// tarif pajak ikut berubah), pakai selisih netto supaya kasus selisih murni pajak tetap
|
|
// kelihatan di kolom/filter Keterangan, bukan malah tampil '-'.
|
|
$selisih = $selisihBruto != 0 ? $selisihBruto : ($item['selisih_thr_netto'] ?? 0);
|
|
|
|
return match (true) {
|
|
$selisih < 0 => 'kekurangan',
|
|
$selisih > 0 => 'kelebihan',
|
|
default => null,
|
|
};
|
|
}
|
|
|
|
private function jenisSelisihLabel(?string $jenisSelisih): string
|
|
{
|
|
return match ($jenisSelisih) {
|
|
'kekurangan' => 'Kekurangan',
|
|
'kelebihan' => 'Kelebihan',
|
|
default => '-',
|
|
};
|
|
}
|
|
|
|
public function export(Request $request)
|
|
{
|
|
$tahunNow = Carbon::now()->format('Y');
|
|
$team = Team::find(session('team_id'));
|
|
|
|
// Prioritaskan pilihan user dari request, baru fallback ke team unit
|
|
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;
|
|
|
|
// Debug log
|
|
Log::info('THR Export Request', [
|
|
'jenis_pegawai' => $jenisPegawai,
|
|
'tahun' => $tahun,
|
|
'unit' => $unit,
|
|
'request_all' => $request->all(),
|
|
]);
|
|
|
|
if (! $tahun) {
|
|
return response()->json(['error' => 'Tahun harus dipilih'], 400);
|
|
}
|
|
|
|
// Get unit name for filename
|
|
// Model UnitKerja uses 0 for Universitas, but blade and DAWAI API use 99
|
|
$unitIdForModel = ($unit == 99) ? 0 : $unit;
|
|
$unitName = '';
|
|
if ($unit) {
|
|
$unitKerja = UnitKerja::find($unitIdForModel);
|
|
if ($unitKerja) {
|
|
$unitName = '_'.str_replace(['/', ' ', '.'], '_', $unitKerja->nama);
|
|
}
|
|
} else {
|
|
// If no unit selected, show "Semua Unit Kerja"
|
|
$unitName = '_Semua_Unit_Kerja';
|
|
}
|
|
|
|
$filename = "THR_{$tahun}{$unitName}.xlsx";
|
|
|
|
return Excel::download(new ThrExport($jenisPegawai, $tahun, $unit), $filename);
|
|
}
|
|
|
|
public function exportSelisih(Request $request)
|
|
{
|
|
$tahunNow = Carbon::now()->format('Y');
|
|
$team = Team::find(session('team_id'));
|
|
|
|
// Prioritaskan pilihan user dari request, baru fallback ke team unit
|
|
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;
|
|
|
|
if (! $tahun) {
|
|
return response()->json(['error' => 'Tahun harus dipilih'], 400);
|
|
}
|
|
|
|
// Get unit name for filename
|
|
// Model UnitKerja uses 0 for Universitas, but blade and DAWAI API use 99
|
|
$unitIdForModel = ($unit == 99) ? 0 : $unit;
|
|
$unitName = '';
|
|
if ($unit) {
|
|
$unitKerja = UnitKerja::find($unitIdForModel);
|
|
if ($unitKerja) {
|
|
$unitName = '_'.str_replace(['/', ' ', '.'], '_', $unitKerja->nama);
|
|
}
|
|
} else {
|
|
// If no unit selected, show "Semua Unit Kerja"
|
|
$unitName = '_Semua_Unit_Kerja';
|
|
}
|
|
|
|
$filename = "THR_Selisih_{$tahun}{$unitName}.xlsx";
|
|
|
|
// Send original unit value to DAWAI API (99 for Universitas)
|
|
return Excel::download(new ThrSelisihExport($jenisPegawai, $tahun, $unit), $filename);
|
|
}
|
|
|
|
public function logs(Request $request)
|
|
{
|
|
// Ambil data dari request
|
|
$data = $request->all();
|
|
|
|
// Log informasi ke file log Laravel
|
|
Log::info('Export THR Log:', $data);
|
|
|
|
// Berikan respons ke klien
|
|
return response()->json(['status' => 'success', 'message' => 'Log berhasil disimpan']);
|
|
}
|
|
}
|