2023-10-10 04:18:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\Writeoff\Livewire\Currency;
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Livewire\Component;
|
2023-12-26 06:35:41 +00:00
|
|
|
use Modules\Writeoff\Entities\Approval;
|
2023-10-10 04:18:12 +00:00
|
|
|
use Modules\Writeoff\Entities\Currency;
|
2023-11-06 03:36:05 +00:00
|
|
|
use Modules\Writeoff\Http\Requests\Currency\StoreCurrencyRequest;
|
|
|
|
use Modules\Writeoff\Http\Requests\Currency\UpdateCurrencyRequest;
|
2023-10-10 04:18:12 +00:00
|
|
|
|
|
|
|
class CurrencyModal extends Component
|
|
|
|
{
|
|
|
|
public $id;
|
|
|
|
public $kode;
|
|
|
|
public $name;
|
2023-12-26 08:55:08 +00:00
|
|
|
public $status;
|
2023-10-10 04:18:12 +00:00
|
|
|
|
|
|
|
public $edit_mode = false;
|
|
|
|
|
|
|
|
protected $listeners = [
|
|
|
|
'delete' => 'delete',
|
|
|
|
'update' => 'update',
|
2024-01-17 04:16:00 +00:00
|
|
|
'reload' => 'reload',
|
2023-10-10 04:18:12 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
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 = [
|
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
|
|
|
];
|
|
|
|
|
|
|
|
if ($this->edit_mode) {
|
|
|
|
// Emit a success event with a message
|
|
|
|
$currency = Currency::find($this->id);
|
|
|
|
|
2024-01-17 04:16:00 +00:00
|
|
|
$data['updated_by'] = auth()->user()->id;
|
|
|
|
$data['updated_at'] = now();
|
|
|
|
|
2023-12-26 08:00:48 +00:00
|
|
|
$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');
|
|
|
|
}
|
2023-10-10 04:18:12 +00:00
|
|
|
} else {
|
2024-01-17 04:16:00 +00:00
|
|
|
$data['created_by'] = auth()->user()->id;
|
|
|
|
$data['created_at'] = now();
|
2023-10-10 04:18:12 +00:00
|
|
|
// Emit a success event with a message
|
2023-12-26 08:00:48 +00:00
|
|
|
$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');
|
|
|
|
}
|
2023-10-10 04:18:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Reset the form fields after successful submission
|
|
|
|
$this->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update($id)
|
|
|
|
{
|
|
|
|
$this->edit_mode = true;
|
|
|
|
|
|
|
|
$currency = Currency::find($id);
|
|
|
|
|
2023-12-26 08:55:08 +00:00
|
|
|
$this->id = $currency->id;
|
|
|
|
$this->kode = $currency->kode;
|
|
|
|
$this->name = $currency->name;
|
|
|
|
$this->status = $currency->status ? true : false;
|
2023-10-10 04:18:12 +00:00
|
|
|
}
|
|
|
|
|
2023-11-06 03:36:05 +00:00
|
|
|
public function delete($id)
|
|
|
|
{
|
2024-01-17 04:16:00 +00:00
|
|
|
$old = Currency::find($id);
|
|
|
|
unset($old->deleted_at);
|
|
|
|
unset($old->deleted_by);
|
|
|
|
|
2023-12-26 08:00:48 +00:00
|
|
|
$curency = Currency::find($id);
|
|
|
|
$curency->delete_by = auth()->user()->id;
|
2023-12-26 06:45:38 +00:00
|
|
|
$curency->deleted_at = now();
|
2023-12-26 06:35:41 +00:00
|
|
|
|
|
|
|
$approval = [
|
|
|
|
'method' => 'delete',
|
|
|
|
'menu' => 'Parameter Mata uang',
|
2024-01-17 04:16:00 +00:00
|
|
|
'new_request' => json_encode($curency),
|
2024-01-17 04:19:07 +00:00
|
|
|
'old_request' => json_encode($old),
|
2023-12-26 06:35:41 +00:00
|
|
|
'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');
|
|
|
|
}
|
2023-11-06 03:36:05 +00:00
|
|
|
}
|
|
|
|
|
2024-01-10 04:40:32 +00:00
|
|
|
public function reload()
|
|
|
|
{
|
|
|
|
$this->edit_mode = false;
|
|
|
|
$this->reset();
|
|
|
|
}
|
|
|
|
|
2023-10-10 04:18:12 +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 UpdateCurrencyRequest();
|
|
|
|
} else {
|
|
|
|
$request = new StoreCurrencyRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $request->rules();
|
|
|
|
}
|
2023-10-10 04:18:12 +00:00
|
|
|
}
|
|
|
|
|