Writeoff/Livewire/SubrogasiJamkrindo/SubrogasiJamkrindoModal.php

259 lines
10 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;
2024-02-21 05:02:07 +00:00
use Modules\Writeoff\Http\Requests\SubrogasiJamkrindo\StoreSubrogasiJamkrindoRequest;
use Modules\Writeoff\Http\Requests\SubrogasiJamkrindo\UpdateSubrogasiJamkrindoRequest;
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
];
2024-01-17 04:49:27 +00:00
if ($this->edit_mode) {
// Emit a success event with a message
$subrogasi_jamkrindo = SubrogasiJamkrindo::find($this->id);
$data['updated_by'] = auth()->user()->id;
$data['updated_at'] = now();
$approval = [
'method' => 'update',
'menu' => 'Subrogasi Jamkrindo',
'old_request' => json_encode($subrogasi_jamkrindo),
'new_request' => json_encode($data),
'description' => 'Update Subrogasi Jamkrindo',
'status' => '0',
2024-02-21 05:02:07 +00:00
'ref' => $this->nomor_pinjaman
2024-01-17 04:49:27 +00:00
];
$is_approval = Approval::where('menu', 'Subrogasi Jamkrindo')
2024-02-21 05:02:07 +00:00
->where('ref', $this->nomor_pinjaman)
2024-01-17 04:49:27 +00:00
->where('status', '0')
->where('method', 'update')
->get()
->first();
if ($is_approval) {
$this->dispatch('error', 'Data Sedang Menunggu Approval');
} else {
Approval::create($approval);
$this->dispatch('success', 'Data Berhasil Di Update, Menunggu Approval');
}
} else {
$data['created_by'] = auth()->user()->id;
$data['created_at'] = now();
// Emit a success event with a message
$approval = [
'method' => 'create',
'menu' => 'Subrogasi Jamkrindo',
'new_request' => json_encode($data),
'description' => 'Create Subrogasi Jamkrindo',
'status' => '0',
2024-02-21 05:02:07 +00:00
'ref' => $this->nomor_pinjaman
2024-01-17 04:49:27 +00:00
];
$is_approval = Approval::where('menu', 'Subrogasi Jamkrindo')
2024-02-21 05:02:07 +00:00
->where('ref', $this->nomor_pinjaman)
2024-01-17 04:49:27 +00:00
->where('status', '0')
->where('method', 'create')
->get()
->first();
if ($is_approval) {
$this->dispatch('error', 'Data Sedang Menunggu Approval');
$this->reset();
} else {
Approval::create($approval);
$this->dispatch('success', 'Data Berhasil Di Input, Menunggu Approval');
}
}
});
// 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)
{
2024-01-17 04:49:27 +00:00
$old = SubrogasiJamkrindo::find($id);
unset($old->deleted_at);
unset($old->deleted_by);
$subrogasi = SubrogasiJamkrindo::find($id);
$subrogasi->delete_by = auth()->user()->id;
$subrogasi->deleted_at = now();
$approval = [
'method' => 'delete',
2024-01-17 04:49:27 +00:00
'menu' => 'Subrogasi Jamkrindo',
2024-01-17 04:52:49 +00:00
'old_request' => json_encode($old),
'new_request' => json_encode($subrogasi),
'description' => 'Delete Data Subrogasi Jamkrindo',
'status' => '0',
'ref' => $subrogasi->nomor_pinjaman
];
2024-01-17 04:49:27 +00:00
$is_approval = Approval::where('menu', '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) {
2024-02-21 05:02:07 +00:00
$request = new UpdateSubrogasiJamkrindoRequest();
} else {
2024-02-21 05:02:07 +00:00
$request = new StoreSubrogasiJamkrindoRequest();
}
return $request->rules();
}
}