162 lines
4.7 KiB
PHP
162 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
class AturanPerhitunganHelper
|
|
{
|
|
private static string $filePath = 'settings/aturan_perhitungan.json';
|
|
|
|
public static function getDefaultRules(): array
|
|
{
|
|
return [
|
|
[
|
|
'id' => 1,
|
|
'kode' => 'standar',
|
|
'nama' => 'Aturan Standar (Sebelum Periode 401)',
|
|
'periode' => null,
|
|
'deskripsi' => 'Perhitungan proporsional standar (JTT menggunakan SKP + Tri Dharma/Penunjang, potongan Adhoc dari selisih nilai normal dan adhoc).',
|
|
'status' => false,
|
|
],
|
|
[
|
|
'id' => 2,
|
|
'kode' => 'periode_401',
|
|
'nama' => 'Aturan Baru Periode 401 (Excel)',
|
|
'periode' => 401,
|
|
'deskripsi' => 'Perhitungan baru sesuai Excel: JTT diprioritaskan SKP + Adhoc (maksimal 28 poin), sisa Adhoc tarif tetap Rp 660.000/6 (Rp 110.000/bulan), Tri Dharma & Penunjang masuk ke JF.',
|
|
'status' => true,
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function getAll(): array
|
|
{
|
|
$path = storage_path('app/' . self::$filePath);
|
|
|
|
if (! File::exists($path)) {
|
|
self::save(self::getDefaultRules());
|
|
return self::getDefaultRules();
|
|
}
|
|
|
|
$json = File::get($path);
|
|
$data = json_decode($json, true);
|
|
|
|
if (! is_array($data) || empty($data)) {
|
|
self::save(self::getDefaultRules());
|
|
return self::getDefaultRules();
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public static function find(int|string $id): ?array
|
|
{
|
|
$all = self::getAll();
|
|
|
|
foreach ($all as $item) {
|
|
if ($item['id'] == $id || $item['kode'] == $id) {
|
|
return $item;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static function save(array $rules): bool
|
|
{
|
|
$dir = storage_path('app/settings');
|
|
|
|
if (! File::isDirectory($dir)) {
|
|
File::makeDirectory($dir, 0755, true);
|
|
}
|
|
|
|
$path = storage_path('app/' . self::$filePath);
|
|
|
|
return (bool) File::put($path, json_encode($rules, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
|
}
|
|
|
|
public static function update(int|string $id, array $data): bool
|
|
{
|
|
$all = self::getAll();
|
|
$updated = false;
|
|
|
|
$newStatus = isset($data['status']) ? (bool) $data['status'] : null;
|
|
$targetRule = null;
|
|
|
|
foreach ($all as &$item) {
|
|
if ($item['id'] == $id || $item['kode'] == $id) {
|
|
$targetRule = &$item;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (! $targetRule) {
|
|
return false;
|
|
}
|
|
|
|
if (isset($data['nama'])) {
|
|
$targetRule['nama'] = $data['nama'];
|
|
}
|
|
if (isset($data['deskripsi'])) {
|
|
$targetRule['deskripsi'] = $data['deskripsi'];
|
|
}
|
|
if (array_key_exists('periode', $data)) {
|
|
$targetRule['periode'] = $data['periode'] ? (int) $data['periode'] : null;
|
|
}
|
|
|
|
if ($newStatus !== null) {
|
|
$targetRule['status'] = $newStatus;
|
|
|
|
// Jika mengaktifkan suatu aturan untuk periode tertentu,
|
|
// nonaktifkan aturan lain untuk periode yang sama (atau aturan yang bentrok)
|
|
if ($newStatus && isset($targetRule['periode'])) {
|
|
foreach ($all as &$other) {
|
|
if ($other['id'] != $targetRule['id'] && ($other['periode'] ?? null) == $targetRule['periode']) {
|
|
$other['status'] = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return self::save($all);
|
|
}
|
|
|
|
public static function toggleStatus(int|string $id): bool
|
|
{
|
|
$rule = self::find($id);
|
|
|
|
if (! $rule) {
|
|
return false;
|
|
}
|
|
|
|
$newStatus = ! ($rule['status'] ?? false);
|
|
|
|
return self::update($id, ['status' => $newStatus]);
|
|
}
|
|
|
|
/**
|
|
* Cek apakah aturan perhitungan baru aktif untuk periode yang diberikan.
|
|
* Sesuai instruksi: tanpa mengubah aturan periode sebelumnya (< 401),
|
|
* periode < 401 selalu menggunakan aturan lama (false).
|
|
*/
|
|
public static function isAturanBaruActive(int|string|null $periode): bool
|
|
{
|
|
$periodeInt = (int) $periode;
|
|
|
|
// Periode sebelumnya tidak berubah sama sekali
|
|
if ($periodeInt < 401) {
|
|
return false;
|
|
}
|
|
|
|
// Untuk periode 401 (dan periode setelahnya jika berlaku), cek switch status aturan baru
|
|
$rule = self::find('periode_401');
|
|
|
|
if (! $rule) {
|
|
return true; // Default aktif untuk 401
|
|
}
|
|
|
|
return (bool) ($rule['status'] ?? true);
|
|
}
|
|
}
|