Writeoff/Livewire/GuaranteeType/GuaranteeTypeModal.php

179 lines
6.2 KiB
PHP
Raw Normal View History

2023-10-10 08:44:27 +00:00
<?php
namespace Modules\Writeoff\Livewire\GuaranteeType;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
use Modules\Writeoff\Entities\Approval;
2023-10-10 08:44:27 +00:00
use Modules\Writeoff\Entities\GuaranteeType;
use Modules\Writeoff\Http\Requests\GuaranteeType\StoreGuaranteeTypeRequest;
use Modules\Writeoff\Http\Requests\GuaranteeType\UpdateGuaranteeTypeRequest;
class GuaranteeTypeModal extends Component
{
public $id;
public $kode;
public $name;
2023-12-26 08:55:08 +00:00
public $status;
2023-10-10 08:44:27 +00:00
public $edit_mode = false;
protected $listeners = [
'delete' => 'delete',
'update' => 'update',
2024-01-10 04:50:54 +00:00
'reload' => 'reload',
2023-10-10 08:44:27 +00:00
];
public function render()
{
return view('writeoff::livewire.guarantee-type.guarantee-type-modal');
}
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,
2023-12-26 08:55:08 +00:00
'name' => $this->name,
'status' => $this->status ? 1 : 0,
2023-10-10 08:44:27 +00:00
];
if ($this->edit_mode) {
// Emit a success event with a message
$guarante = GuaranteeType::find($this->id);
2023-10-10 08:44:27 +00:00
$data['updated_by'] = auth()->user()->id;
$data['updated_at'] = now();
$approval = [
'method' => 'update',
'menu' => 'Parameter Jenis Jaminan',
'old_request' => json_encode($guarante),
'new_request' => json_encode($data),
'description' => 'Update Parameter Jenis Jaminan',
'status' => '0',
'ref' => $this->kode
];
$is_approval = Approval::where('menu', 'Parameter Jenis Jaminan')
->where('ref', $this->kode)
->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');
}
2023-10-10 08:44:27 +00:00
} else {
// Emit a success event with a message
$data['created_by'] = auth()->user()->id;
$data['created_at'] = now();
$approval = [
'method' => 'create',
'menu' => 'Parameter Jenis Jaminan',
'new_request' => json_encode($data),
'description' => 'Create Parameter Jenis Jaminan',
'status' => '0',
'ref' => $this->kode
];
$is_approval = Approval::where('menu', 'Parameter Jenis Jaminan')
->where('ref', $this->kode)
->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');
}
2023-10-10 08:44:27 +00:00
}
});
// Reset the form fields after successful submission
$this->reset();
}
public function update($id)
{
$this->edit_mode = true;
$guarantee_type = GuaranteeType::find($id);
$this->id = $guarantee_type->id;
$this->kode = $guarantee_type->kode;
$this->name = $guarantee_type->name;
2023-12-26 08:55:08 +00:00
$this->status = $guarantee_type->status ? true : false;
2023-10-10 08:44:27 +00:00
}
2023-11-06 03:36:05 +00:00
public function delete($id)
{
$old = GuaranteeType::find($id);
unset($old->deleted_at);
unset($old->deleted_by);
$guarantee = GuaranteeType::find($id);
$guarantee->delete_by = auth()->user()->id;
$guarantee->deleted_at = now();
$approval = [
'method' => 'delete',
'menu' => 'Parameter Jenis Jaminan',
'new_request' => json_encode($guarantee),
'old_request' => json_encode($old),
'description' => 'Delete Parameter Jenis Jaminan',
'status' => '0',
'ref' => $guarantee->kode
];
$is_approval = Approval::where('menu', 'Parameter Jenis Jaminan')
->where('ref', $guarantee->kode)
->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');
}
2023-11-06 03:36:05 +00:00
}
2024-01-10 04:50:54 +00:00
public function reload()
{
$this->edit_mode = false;
$this->reset();
}
2023-10-10 08:44:27 +00:00
public function hydrate()
{
$this->resetErrorBag();
$this->resetValidation();
}
2023-11-06 03:36:05 +00:00
protected function rules()
{
if ($this->edit_mode) {
$request = new UpdateGuaranteeTypeRequest();
} else {
$request = new StoreGuaranteeTypeRequest();
}
return $request->rules();
}
2023-10-10 08:44:27 +00:00
}