Writeoff/Livewire/Branch/BranchModal.php

182 lines
6.0 KiB
PHP
Raw Normal View History

2023-10-05 15:32:26 +00:00
<?php
namespace Modules\Writeoff\Livewire\Branch;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
use Modules\Writeoff\Entities\Approval;
2023-10-05 15:32:26 +00:00
use Modules\Writeoff\Entities\Branch;
2023-11-22 08:05:35 +00:00
use Modules\Writeoff\Http\Requests\Branch\StoreBranchRequest;
use Modules\Writeoff\Http\Requests\Branch\UpdateBranchRequest;
2023-10-05 15:32:26 +00:00
class BranchModal extends Component
{
2023-10-09 11:12:51 +00:00
public $id;
2023-10-05 15:32:26 +00:00
public $kode;
public $name;
2023-12-26 08:55:08 +00:00
public $status;
2023-10-05 15:32:26 +00:00
public $edit_mode = false;
protected $listeners = [
2023-10-10 04:18:12 +00:00
'delete' => 'delete',
'update' => 'update',
2024-01-17 04:07:44 +00:00
'reload' => 'reload',
2023-10-05 15:32:26 +00:00
];
public function render()
{
return view('writeoff::livewire.branch.branch-modal');
}
public function submit()
{
$this->validate();
2023-10-09 11:12:51 +00:00
// Validate the form input data
2023-10-05 15:32:26 +00:00
DB::transaction(function () {
// Prepare the data for creating a new user
$data = [
2023-12-26 08:55:08 +00:00
'kode' => $this->kode,
'name' => $this->name,
'status' => $this->status ? 1 : 0,
2023-10-10 04:18:12 +00:00
];
2023-10-05 15:32:26 +00:00
if ($this->edit_mode) {
// Emit a success event with a message
2023-10-10 04:18:12 +00:00
$branch = Branch::find($this->id);
2024-01-17 04:07:44 +00:00
$data['updated_by'] = auth()->user()->id;
$data['updated_at'] = now();
$approval = [
'method' => 'update',
'menu' => 'Parameter Cabang',
'old_request' => json_encode($branch),
'new_request' => json_encode($data),
'description' => 'Update Parameter Cabang',
'status' => '0',
'ref' => $this->kode
];
$is_approval = Approval::where('menu', 'Parameter Cabang')
->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-05 15:32:26 +00:00
} else {
// Emit a success event with a message
2024-01-17 04:07:44 +00:00
$data['created_by'] = auth()->user()->id;
$data['created_at'] = now();
$approval = [
'method' => 'create',
'menu' => 'Parameter Cabang',
'new_request' => json_encode($data),
'description' => 'Create Parameter Cabang',
'status' => '0',
'ref' => $this->kode
];
$is_approval = Approval::where('menu', 'Parameter Cabang')
->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-05 15:32:26 +00:00
}
});
// Reset the form fields after successful submission
$this->reset();
}
2023-10-10 04:18:12 +00:00
public function update($id)
2023-10-05 15:32:26 +00:00
{
$this->edit_mode = true;
$branch = Branch::find($id);
2023-12-26 08:55:08 +00:00
$this->id = $branch->id;
$this->kode = $branch->kode;
$this->name = $branch->name;
$this->status = $branch->status ? true : false;
2023-10-05 15:32:26 +00:00
}
2024-01-10 03:57:27 +00:00
public function reload()
{
$this->edit_mode = false;
$this->reset();
}
2023-11-06 03:36:05 +00:00
public function delete($id)
{
2024-01-17 04:07:44 +00:00
$branch = Branch::find($id);
$old = $branch;
unset($old->deleted_at);
unset($old->deleted_by);
$new = Branch::find($id);
$new->deleted_by = auth()->user()->id;
$new->deleted_at = now();
$approval = [
'method' => 'delete',
'menu' => 'Parameter Cabang',
2024-01-10 03:57:27 +00:00
'old_request' => json_encode($old),
2024-01-17 04:07:44 +00:00
'new_request' => json_encode($new),
'description' => 'Delete Parameter Cabang',
'status' => '0',
'ref' => $branch->kode
];
$is_approval = Approval::where('menu', 'Parameter Cabang')
2023-12-26 08:55:08 +00:00
->where('ref', $branch->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
}
2023-10-05 15:32:26 +00:00
public function hydrate()
{
$this->resetErrorBag();
$this->resetValidation();
}
2023-11-06 03:36:05 +00:00
protected function rules()
{
if ($this->edit_mode) {
2023-11-22 08:05:35 +00:00
$request = new UpdateBranchRequest();
2023-11-06 03:36:05 +00:00
} else {
2023-11-22 08:05:35 +00:00
$request = new StoreBranchRequest();
2023-11-06 03:36:05 +00:00
}
return $request->rules();
}
2023-10-05 15:32:26 +00:00
}