Writeoff/Livewire/Currency/CurrencyModal.php

180 lines
6.2 KiB
PHP

<?php
namespace Modules\Writeoff\Livewire\Currency;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
use Modules\Writeoff\Entities\Approval;
use Modules\Writeoff\Entities\Currency;
use Modules\Writeoff\Http\Requests\Currency\StoreCurrencyRequest;
use Modules\Writeoff\Http\Requests\Currency\UpdateCurrencyRequest;
class CurrencyModal extends Component
{
public $id;
public $kode;
public $name;
public $status;
public $edit_mode = false;
protected $listeners = [
'delete' => 'delete',
'update' => 'update',
'reload' => 'reload',
];
public function render()
{
return view('writeoff::livewire.currency.currency-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,
'name' => $this->name,
'status' => $this->status ? 1 : 0,
];
if ($this->edit_mode) {
// Emit a success event with a message
$currency = Currency::find($this->id);
$data['updated_by'] = auth()->user()->id;
$data['updated_at'] = now();
$approval = [
'method' => 'update',
'menu' => 'Parameter Mata Uang',
'old_request' => json_encode($currency),
'new_request' => json_encode($data),
'description' => 'Update Parameter Mata Uang',
'status' => '0',
'ref' => $this->kode
];
$is_approval = Approval::where('menu', 'Parameter Mata Uang')
->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');
}
} else {
$data['created_by'] = auth()->user()->id;
$data['created_at'] = now();
// Emit a success event with a message
$approval = [
'method' => 'create',
'menu' => 'Parameter Mata Uang',
'new_request' => json_encode($data),
'description' => 'Create Parameter Mata Uang',
'status' => '0',
'ref' => $this->kode
];
$is_approval = Approval::where('menu', 'Parameter Mata Uang')
->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');
}
}
});
// Reset the form fields after successful submission
$this->reset();
}
public function update($id)
{
$this->edit_mode = true;
$currency = Currency::find($id);
$this->id = $currency->id;
$this->kode = $currency->kode;
$this->name = $currency->name;
$this->status = $currency->status ? true : false;
}
public function delete($id)
{
$old = Currency::find($id);
unset($old->deleted_at);
unset($old->deleted_by);
$curency = Currency::find($id);
$curency->delete_by = auth()->user()->id;
$curency->deleted_at = now();
$approval = [
'method' => 'delete',
'menu' => 'Parameter Mata uang',
'new_request' => json_encode($curency),
'old_request' => json_encode($old),
'description' => 'Delete Parameter Mata uang',
'status' => '0',
'ref' => $curency->kode
];
$is_approval = Approval::where('menu', 'Parameter Mata uang')
->where('ref', $curency->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');
}
}
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 UpdateCurrencyRequest();
} else {
$request = new StoreCurrencyRequest();
}
return $request->rules();
}
}