2023-11-13 12:13:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\Writeoff\Livewire\Debitur;
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Livewire\Component;
|
2023-12-26 06:35:41 +00:00
|
|
|
use Modules\Writeoff\Entities\Approval;
|
2023-11-13 12:13:33 +00:00
|
|
|
use Modules\Writeoff\Entities\Branch;
|
|
|
|
use Modules\Writeoff\Entities\Debitur;
|
|
|
|
use Modules\Writeoff\Http\Requests\Debitur\StoreDebiturRequest;
|
2023-11-22 14:35:06 +00:00
|
|
|
use Modules\Writeoff\Http\Requests\Debitur\UpdateDebiturRequest;
|
2023-11-13 12:13:33 +00:00
|
|
|
|
|
|
|
class DebiturModal extends Component
|
|
|
|
{
|
|
|
|
public $id;
|
|
|
|
public $kode;
|
|
|
|
public $name;
|
|
|
|
public $branch_id;
|
|
|
|
public $status;
|
|
|
|
public $address;
|
|
|
|
public $npwp;
|
|
|
|
public $registered_at;
|
|
|
|
|
|
|
|
public $edit_mode = false;
|
|
|
|
|
|
|
|
protected $listeners = [
|
|
|
|
'delete' => 'delete',
|
|
|
|
'update' => 'update',
|
2024-01-10 04:40:39 +00:00
|
|
|
'reload' => 'reload',
|
2023-11-13 12:13:33 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
$cabang = Branch::all();
|
|
|
|
return view('writeoff::livewire.debitur.debitur-modal', compact('cabang'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function submit()
|
|
|
|
{
|
|
|
|
$this->validate();
|
|
|
|
|
|
|
|
// Validate the form input data
|
|
|
|
DB::transaction(function () {
|
|
|
|
// Prepare the data for creating a new user
|
|
|
|
$data = [
|
|
|
|
'kode' => $this->kode,
|
|
|
|
'name' => $this->name,
|
|
|
|
'branch_id' => $this->branch_id,
|
|
|
|
'status' => $this->status,
|
|
|
|
'address' => $this->address,
|
|
|
|
'npwp' => $this->npwp,
|
|
|
|
'registered_at' => $this->registered_at,
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($this->edit_mode) {
|
|
|
|
// Emit a success event with a message
|
|
|
|
$debitur = Debitur::find($this->id);
|
|
|
|
|
2023-12-26 08:00:48 +00:00
|
|
|
$approval = [
|
|
|
|
'method' => 'update',
|
|
|
|
'menu' => 'Parameter Debitur',
|
|
|
|
'old_request' => json_encode($debitur),
|
|
|
|
'new_request' => json_encode($data),
|
|
|
|
'description' => 'Update Parameter Debitur',
|
|
|
|
'status' => '0',
|
|
|
|
'ref' => $this->kode
|
|
|
|
];
|
|
|
|
|
|
|
|
$is_approval = Approval::where('menu', 'Parameter Debitur')
|
|
|
|
->where('ref', $this->kode)
|
|
|
|
->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');
|
|
|
|
}
|
2023-11-13 12:13:33 +00:00
|
|
|
} else {
|
|
|
|
// Emit a success event with a message
|
2023-12-26 08:00:48 +00:00
|
|
|
$approval = [
|
|
|
|
'method' => 'create',
|
|
|
|
'menu' => 'Parameter Debitur',
|
|
|
|
'new_request' => json_encode($data),
|
|
|
|
'description' => 'Create Parameter Debitur',
|
|
|
|
'status' => '0',
|
|
|
|
'ref' => $this->kode
|
|
|
|
];
|
|
|
|
|
|
|
|
$is_approval = Approval::where('menu', 'Parameter Debitur')
|
|
|
|
->where('ref', $this->kode)
|
|
|
|
->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');
|
|
|
|
}
|
2023-11-13 12:13:33 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Reset the form fields after successful submission
|
|
|
|
$this->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update($id)
|
|
|
|
{
|
|
|
|
$this->edit_mode = true;
|
|
|
|
|
|
|
|
$debitur = Debitur::find($id);
|
|
|
|
|
|
|
|
$this->id = $debitur->id;
|
|
|
|
$this->kode = $debitur->kode;
|
|
|
|
$this->name = $debitur->name;
|
|
|
|
$this->branch_id = $debitur->branch_id;
|
2023-12-26 06:35:41 +00:00
|
|
|
$this->status = $debitur->status == 1 ? true : false;
|
2023-11-13 12:13:33 +00:00
|
|
|
$this->address = $debitur->address;
|
|
|
|
$this->npwp = $debitur->npwp;
|
|
|
|
$this->registered_at = $debitur->registered_at;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($id)
|
|
|
|
{
|
2023-12-26 06:35:41 +00:00
|
|
|
$debitur = Debitur::find($id);
|
2023-12-26 06:45:38 +00:00
|
|
|
$debitur->delete_by = auth()->user()->id;
|
|
|
|
$debitur->deleted_at = now();
|
2023-11-13 12:13:33 +00:00
|
|
|
|
2023-12-26 06:35:41 +00:00
|
|
|
$approval = [
|
|
|
|
'method' => 'delete',
|
|
|
|
'menu' => 'Parameter Debitur',
|
|
|
|
'old_request' => json_encode($debitur),
|
|
|
|
'description' => 'Delete Parameter Debitur',
|
|
|
|
'status' => '0',
|
|
|
|
'ref' => $debitur->kode
|
|
|
|
];
|
|
|
|
|
|
|
|
$is_approval = Approval::where('menu', 'Parameter Debitur')
|
|
|
|
->where('ref', $debitur->kode)
|
|
|
|
->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');
|
|
|
|
}
|
2023-11-13 12:13:33 +00:00
|
|
|
}
|
|
|
|
|
2024-01-10 04:40:39 +00:00
|
|
|
public function reload()
|
|
|
|
{
|
|
|
|
$this->edit_mode = false;
|
|
|
|
$this->reset();
|
|
|
|
}
|
|
|
|
|
2023-11-13 12:13:33 +00:00
|
|
|
public function hydrate()
|
|
|
|
{
|
|
|
|
$this->resetErrorBag();
|
|
|
|
$this->resetValidation();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function rules()
|
|
|
|
{
|
|
|
|
if ($this->edit_mode) {
|
2023-11-22 14:35:06 +00:00
|
|
|
$request = new UpdateDebiturRequest();
|
2023-11-13 12:13:33 +00:00
|
|
|
} else {
|
|
|
|
$request = new StoreDebiturRequest();
|
|
|
|
}
|
|
|
|
return $request->rules();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|