Writeoff/Livewire/Rekening/RekeningModal.php
2024-02-01 13:32:15 +07:00

212 lines
7.6 KiB
PHP

<?php
namespace Modules\Writeoff\Livewire\Rekening;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
use Modules\Writeoff\Entities\Approval;
use Modules\Writeoff\Entities\Branch;
use Modules\Writeoff\Entities\Currency;
use Modules\Writeoff\Entities\Debitur;
use Modules\Writeoff\Entities\LoanType;
use Modules\Writeoff\Entities\Product;
use Modules\Writeoff\Entities\Rekening;
use Modules\Writeoff\Http\Requests\Rekening\StoreRekeningRequest;
use Modules\Writeoff\Http\Requests\Rekening\UpdateRekeningRequest;
class RekeningModal extends Component
{
public $id;
public $nomor_rekening;
public $status_rekening;
public $branch_id;
public $debitur_id;
public $loan_type_id;
public $currency_id;
public $status;
public $limit_ref;
public $registered_at;
public $edit_mode = false;
protected $listeners = [
'delete' => 'delete',
'update' => 'update',
'reload' => 'reload',
];
public function render()
{
$cabang = Branch::all();
$debitur = Debitur::all();
$loan_type = LoanType::all();
$currency = Currency::all();
return view('writeoff::livewire.rekening.rekening-modal', compact([
'cabang',
'debitur',
'loan_type',
'currency'
]));
}
public function submit()
{
$this->validate();
// Validate the form input data
DB::transaction(function () {
// Prepare the data for creating a new user
$data = [
'nomor_rekening' => $this->nomor_rekening,
'branch_id' => $this->branch_id,
'debitur_id' => $this->debitur_id,
'loan_type_id' => $this->loan_type_id,
'currency_id' => $this->currency_id,
'status' => $this->status,
'status_rekening' => $this->status_rekening,
'limit_ref' => $this->limit_ref,
'registered_at' => $this->registered_at
];
if ($this->edit_mode) {
// Emit a success event with a message
$rekening = Rekening::find($this->id);
$data['updated_by'] = auth()->user()->id;
$data['updated_at'] = now();
$approval = [
'method' => 'update',
'menu' => 'Parameter Rekening',
'old_request' => json_encode($rekening),
'new_request' => json_encode($data),
'description' => 'Update Parameter Rekening',
'status' => '0',
'ref' => $this->nomor_rekening
];
$is_approval = Approval::where('menu', 'Parameter Rekening')
->where('ref', $this->nomor_rekening)
->where('status', '0')
->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' => 'Parameter Rekening',
'new_request' => json_encode($data),
'description' => 'Create Parameter Rekening',
'status' => '0',
'ref' => $this->nomor_rekening
];
$is_approval = Approval::where('menu', 'Parameter Rekening')
->where('ref', $this->nomor_rekening)
->where('status', '0')
->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;
$rekening = Rekening::find($id);
$this->id = $rekening->id;
$this->nomor_rekening = $rekening->nomor_rekening;
$this->branch_id = $rekening->branch_id;
$this->debitur_id = $rekening->debitur_id;
$this->loan_type_id = $rekening->loan_type_id;
$this->currency_id = $rekening->currency_id;
$this->status = $rekening->status == 1 ? true : false;
$this->status_rekening = $rekening->status_rekening == 1 ? true : false;
$this->limit_ref = $rekening->limit_ref;
$this->registered_at = $rekening->registered_at;
}
public function delete($id)
{
$old = Rekening::find($id);
unset($old->deleted_at);
unset($old->delete_by);
$rekening = Rekening::find($id);
$rekening->delete_by = auth()->user()->id;
$rekening->deleted_at = now();
$approval = [
'method' => 'delete',
'menu' => 'Parameter Rekening',
'old_request' => json_encode($old),
'new_request' => json_encode($rekening),
'description' => 'Delete Parameter Rekening',
'status' => '0',
'ref' => $rekening->nomor_rekening
];
$is_approval = Approval::where('menu', 'Parameter Rekening')
->where('ref', $rekening->nomor_rekening)
->where('status', '0')
->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 reload()
{
$this->edit_mode = false;
$this->reset();
}
public function hydrate()
{
$this->resetErrorBag();
$this->resetValidation();
}
protected function rules()
{
if ($this->edit_mode) {
$request = new UpdateRekeningRequest();
} else {
$request = new StoreRekeningRequest();
}
return $request->rules();
}
}