Menambahkan fungsi untuk mengunggah lampiran saat membuat dan memperbarui permohonan, serta mencatat riwayat perubahan permohonan dengan menggunakan PermohonanHistoryService. Fitur ini memastikan file lampiran tersimpan dengan benar dan perubahan terhadap permohonan terdokumentasi.
156 lines
4.0 KiB
PHP
156 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace Modules\Lpj\Models;
|
|
|
|
use Modules\Lpj\Database\Factories\PermohonanFactory;
|
|
use Modules\Lpj\Services\PermohonanHistoryService;
|
|
use Modules\Usermanagement\Models\User;
|
|
|
|
class Permohonan extends Base
|
|
{
|
|
protected $table = 'permohonan';
|
|
protected $fillable = [
|
|
'nomor_registrasi',
|
|
'tanggal_permohonan',
|
|
'user_id',
|
|
'branch_id',
|
|
'tujuan_penilaian_id',
|
|
'debiture_id',
|
|
'keterangan',
|
|
'dokumen',
|
|
'jenis_fasilitas_kredit_id',
|
|
'nilai_plafond_id',
|
|
'status',
|
|
'authorized_at',
|
|
'authorized_status',
|
|
'authorized_by',
|
|
// andy add
|
|
'registrasi_catatan',
|
|
'registrasi_by',
|
|
'registrasi_at',
|
|
'jenis_penilaian_id',
|
|
'region_id',
|
|
// andy add
|
|
'status_bayar',
|
|
'nilai_njop',
|
|
// andy add
|
|
'registrasi_catatan',
|
|
'registrasi_by',
|
|
'registrasi_at',
|
|
'jenis_penilaian_id',
|
|
'region_id',
|
|
'attachment'
|
|
];
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::creating(function ($permohonan) {
|
|
static::handleFileUpload($permohonan);
|
|
});
|
|
|
|
static::updating(function ($permohonan) {
|
|
static::handleFileUpload($permohonan);
|
|
});
|
|
|
|
static::created(function ($permohonan) {
|
|
static::createHistory($permohonan, 'created');
|
|
});
|
|
|
|
static::updated(function ($permohonan) {
|
|
static::createHistory($permohonan, 'updated');
|
|
});
|
|
}
|
|
|
|
protected static function handleFileUpload($permohonan)
|
|
{
|
|
if (request()->hasFile('attachment')) {
|
|
$file = request()->file('attachment');
|
|
$fileName = time() . '_' . $file->getClientOriginalName();
|
|
$filePath = $file->storeAs('permohonan_attachments', $fileName, 'public');
|
|
|
|
// Delete old file if it exists
|
|
if ($permohonan->attachment) {
|
|
Storage::disk('public')->delete($permohonan->attachment);
|
|
}
|
|
|
|
$permohonan->attachment = $filePath;
|
|
}
|
|
}
|
|
|
|
protected static function createHistory($permohonan, $action)
|
|
{
|
|
$historyService = app(PermohonanHistoryService::class);
|
|
|
|
$status = $permohonan->status;
|
|
$keterangan = request()->input('keterangan'); // Get keterangan from request
|
|
$beforeRequest = $action === 'updated' ? $permohonan->getOriginal() : [];
|
|
$afterRequest = $permohonan->toArray();
|
|
$file = $permohonan->attachment ? Storage::disk('public')->path($permohonan->attachment) : null;
|
|
|
|
$historyService->createHistory(
|
|
$permohonan,
|
|
$status,
|
|
$keterangan,
|
|
$beforeRequest,
|
|
$afterRequest,
|
|
$file
|
|
);
|
|
}
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function branch()
|
|
{
|
|
return $this->belongsTo(Branch::class);
|
|
}
|
|
|
|
public function tujuanPenilaian()
|
|
{
|
|
return $this->belongsTo(TujuanPenilaian::class);
|
|
}
|
|
|
|
public function debiture()
|
|
{
|
|
return $this->belongsTo(Debiture::class);
|
|
}
|
|
|
|
public function documents()
|
|
{
|
|
return $this->hasMany(DokumenJaminan::class);
|
|
}
|
|
|
|
public function nilaiPlafond()
|
|
{
|
|
return $this->belongsTo(NilaiPlafond::class);
|
|
}
|
|
|
|
public function jenisFasilitasKredit()
|
|
{
|
|
return $this->belongsTo(JenisFasilitasKredit::class);
|
|
}
|
|
|
|
public function penilaian()
|
|
{
|
|
return $this->belongsTo(Penilaian::class, 'nomor_registrasi', 'nomor_registrasi');
|
|
}
|
|
|
|
public function penawaranTender()
|
|
{
|
|
return $this->hasMany(PenawaranTender::class, 'nomor_registrasi');
|
|
}
|
|
|
|
public function region()
|
|
{
|
|
return $this->belongsTo(Regions::class, 'region_id');
|
|
}
|
|
|
|
public function penawaran(){
|
|
return $this->belongsTo(PenawaranTender::class, 'nomor_registrasi', 'nomor_registrasi');
|
|
}
|
|
}
|