Writeoff/Livewire/HapusBuku/NomorPinjamanModal.php

80 lines
2.7 KiB
PHP

<?php
namespace Modules\Writeoff\Livewire\HapusBuku;
use Livewire\Component;
use Modules\Writeoff\Entities\Approval;
use Modules\Writeoff\Entities\HapusBuku;
use Modules\Writeoff\Entities\Rekening;
class NomorPinjamanModal extends Component
{
public $nomor_pinjaman;
public $edit_mode = false;
protected $listeners = [
'delete' => 'delete'
];
public function render()
{
return view('writeoff::livewire.hapus-buku.nomor-pinjaman-modal');
}
public function submit()
{
$this->validate([
'nomor_pinjaman' => 'required',
]);
$rekenings = Rekening::with('loan_type', 'debitur')
->where('nomor_rekening', $this->nomor_pinjaman)
->whereDoesntHave('hapusBuku')
->whereHas('loan_type', function ($query) {
$query->whereBetween('kode', [3000, 3999]);
})
->get()
->first();
if ($rekenings) {
$this->dispatch('success', __('Nomor Pinjaman ditemukan'));
$this->redirect(route('pencatatan.hapus_buku.index', ['rekening' => $this->nomor_pinjaman]));
} else {
$this->addError('nomor_pinjaman', 'Nomor Pinjaman Salah Atau Nomor Pinjaman sudah terdaftar');
}
//$this->dispatch('showHapusBuku');
}
public function delete($id)
{
$hapusbuku = HapusBuku::find($id);
$hapusbuku->delete_by = auth()->user()->id;
$hapusbuku->deleted_at = now();
$approval = [
'method' => 'delete',
'menu' => 'Hapus Buku',
'old_request' => json_encode($hapusbuku),
'description' => 'Delete Data Hapus Buku',
'status' => '0',
'ref' => $hapusbuku->nomor_pinjaman
];
$is_approval = Approval::where('menu', 'Hapus Buku')
->where('ref', $hapusbuku->nomor_pinjaman)
->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');
}
}
}