Writeoff/Livewire/SubrogasiJamkrindo/SubrogasiJamkrindoModal.php

203 lines
7.8 KiB
PHP
Raw Normal View History

<?php
namespace Modules\Writeoff\Livewire\SubrogasiJamkrindo;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
use Modules\Writeoff\Entities\Approval;
use Modules\Writeoff\Entities\Branch;
use Modules\Writeoff\Entities\Debitur;
use Modules\Writeoff\Entities\Rekening;
use Modules\Writeoff\Entities\SubrogasiJamkrindo;
2023-12-05 11:17:27 +00:00
use Modules\Writeoff\Http\Requests\SubrogasiJamkrindo\StoreHapusBukuRequest;
use Modules\Writeoff\Http\Requests\SubrogasiJamkrindo\UpdateHapusBukuRequest;
class SubrogasiJamkrindoModal extends Component
{
public $id;
public $nomor_pinjaman;
public $kode_debitur;
public $nama_debitur;
public $kode_cabang;
public $nama_cabang;
public $nomor_rekening;
public $nama_rekening;
public $plafond;
public $tenor;
public $tanggal_pengajuan_klaim;
public $piutang_subrogasi;
public $total_bayar_subrogasi;
public $sisa_piutang_subrogasi;
public $is_lunas_subrogasi;
public $keterangan;
public $status;
public $edit_mode = false;
protected $listeners = [
'delete' => 'delete',
'update' => 'update',
];
public function render()
{
return view('writeoff::livewire.subrogasi-jamkrindo.subrogasi-jamkrindo-modal');
}
public function submit()
{
$this->validate();
// Validate the form input data
DB::transaction(function () {
// Prepare the data for creating a new user
$data = [
'nomor_pinjaman' => $this->nomor_pinjaman,
'kode_debitur' => $this->kode_debitur,
'nama_debitur' => $this->nama_debitur,
'kode_cabang' => $this->kode_cabang,
'nama_cabang' => $this->nama_cabang,
'nomor_rekening' => $this->nomor_rekening,
'nama_rekening' => $this->nama_rekening,
'plafond' => $this->plafond,
'tenor' => $this->tenor,
'tanggal_pengajuan_klaim' => $this->tanggal_pengajuan_klaim,
'piutang_subrogasi' => $this->piutang_subrogasi,
'total_bayar_subrogasi' => $this->total_bayar_subrogasi,
'sisa_piutang_subrogasi' => $this->sisa_piutang_subrogasi,
'is_lunas_subrogasi' => $this->is_lunas_subrogasi,
'keterangan' => $this->keterangan,
'status' => $this->status
];
if ($this->edit_mode) {
// Emit a success event with a message
$subrogasi_jamkrindo = SubrogasiJamkrindo::find($this->id);
$subrogasi_jamkrindo->update($data);
$this->dispatch('success', __('Subrogasi Jamkrindo updated'));
} else {
// Emit a success event with a message
SubrogasiJamkrindo::create($data);
$this->dispatch('success', __('New Subrogasi Jamkrindo created'));
}
});
// Reset the form fields after successful submission
//$this->reset();
}
public function update($id)
{
$this->edit_mode = true;
$subrogasi_jamkrindo = SubrogasiJamkrindo::find($id);
$this->id = $subrogasi_jamkrindo->id;
$this->nomor_pinjaman = $subrogasi_jamkrindo->nomor_pinjaman;
$this->kode_debitur = $subrogasi_jamkrindo->kode_debitur;
$this->nama_debitur = $subrogasi_jamkrindo->nama_debitur;
$this->kode_cabang = $subrogasi_jamkrindo->kode_cabang;
$this->nama_cabang = $subrogasi_jamkrindo->nama_cabang;
$this->nomor_rekening = $subrogasi_jamkrindo->nomor_rekening;
$this->nama_rekening = $subrogasi_jamkrindo->nama_rekening;
$this->plafond = $subrogasi_jamkrindo->plafond;
$this->tenor = $subrogasi_jamkrindo->tenor;
$this->tanggal_pengajuan_klaim = $subrogasi_jamkrindo->tanggal_pengajuan_klaim;
$this->piutang_subrogasi = $subrogasi_jamkrindo->piutang_subrogasi;
$this->total_bayar_subrogasi = $subrogasi_jamkrindo->total_bayar_subrogasi;
$this->sisa_piutang_subrogasi = $subrogasi_jamkrindo->sisa_piutang_subrogasi;
$this->is_lunas_subrogasi = $subrogasi_jamkrindo->is_lunas_subrogasi == 1;
$this->keterangan = $subrogasi_jamkrindo->keterangan;
$this->status = $subrogasi_jamkrindo->status == 1;
}
public function getDebitur()
{
$debitur = Debitur::where('kode', $this->kode_debitur)->first();
if ($debitur) {
$this->nama_debitur = $debitur->name;
} else {
$this->nama_debitur = '';
$this->addError('nama_debitur', 'Debitur Tidak Ditemukan');
}
}
public function getBranch()
{
$branch = Branch::where('kode', $this->kode_cabang)->first();
if ($branch) {
$this->nama_cabang = $branch->name;
} else {
$this->nama_cabang = '';
$this->addError('nama_cabang', 'Cabang Tidak Ditemukan');
}
}
public function getRekening()
{
$rekening = Rekening::where('nomor_rekening', $this->nomor_rekening)->first();
if ($rekening) {
$debitur = Debitur::where('id', $rekening->debitur_id)->first();
if ($debitur) {
$this->nama_rekening = $debitur->name;
} else {
$this->nama_rekening = '';
$this->addError('nama_rekening', 'Nomor Rekening Tidak Ditemukan');
}
} else {
$this->nama_rekening = '';
$this->addError('nama_rekening', 'Nomor Rekening Tidak Ditemukan');
}
}
public function delete($id)
{
$subrogasi = SubrogasiJamkrindo::find($id);
$approval = [
'method' => 'delete',
'menu' => 'Data Subrogasi Jamkrindo',
'old_request' => json_encode($subrogasi),
'description' => 'Delete Data Subrogasi Jamkrindo',
'status' => '0',
'ref' => $subrogasi->nomor_pinjaman
];
$is_approval = Approval::where('menu', 'Data Subrogasi Jamkrindo')
->where('ref', $subrogasi->nomor_pinjaman)
->where('status', '0')
->where('method', 'delete')
->get()
->first();
if ($is_approval) {
$this->dispatch('error', 'Data Sedang Menunggu Approval');
} else {
Approval::create($approval);
$this->dispatch('success', 'Data Berhasil Di Hapus, Menunggu Approval');
}
}
public function hydrate()
{
$this->resetErrorBag();
$this->resetValidation();
}
protected function rules()
{
if ($this->edit_mode) {
2023-12-05 11:17:27 +00:00
$request = new UpdateHapusBukuRequest();
} else {
2023-12-05 11:17:27 +00:00
$request = new StoreHapusBukuRequest();
}
return $request->rules();
}
}