CasperSecurity
<?php
namespace App\Http\Livewire\Finance;
use App\Models\JournalType;
use Livewire\Component;
use Livewire\WithPagination;
class JournalTypeLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage=10;
public $orderBy='id';
public $orderAsc='1';
public $journal_type_name,$journal_type_code,$status,$data;
public $modelid;
public $showmodal=false;
public $successmsg="The Journal Type data has been saved successfully.";
public $updatemsg="The Journal Type data has been updated successfully.";
public $deletemsg="The Journal Type data has been deleted successfully.";
public $duplicateerrormsg="Sorry !!! Journal Type name has already exists in our database. Please Try Another Name";
public $errormsg="Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = ['delete'];
public $rules=[
'journal_type_name'=>'required',
];
public $updatedrules=[
'journal_type_name'=>'required|',
'status'=>'required',
];
public $messages=[
'journal_type_name.required'=>'This field is required',
];
public function updatingSearch()
{
$this->resetPage();
}
public function resetvalid()
{
$this->resetValidation();
}
public function showmodalclick(){
$this->cleanVar();
$this->resetValidation();
$this->showmodal=true;
}
public function closemodalclick(){
$this->resetValidation();
$this->showmodal=false;
}
public function updateclick($id){
$this->modelid=$id;
$this->loadData($this->modelid);
$this->showmodal=true;
}
public function deleteclick($id){
$this->modelid=$id;
$this->dispatchBrowserEvent('confirmdelete',[
'message'=>'Are you sure want to delete this data ?',
'funcname'=>'delete'
]);
}
public function cleanVar(){
$this->modelid=null;
$this->status=null;
$this->journal_type_name = null;
}
public function loadData($id){
$response=JournalType::find($id);
if($response){
$this->journal_type_name=$response['journal_type_name'];
$this->status=$response['status'];
}
}
public function save(){
$this->validate($this->rules,$this->messages);
$response=JournalType::where('journal_type_name',$this->journal_type_name)->latest()->first();
if($response){
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->duplicateerrormsg
]);
}else {
$journal=JournalType::all();
if(sizeof($journal)<10){
$add="0".sizeof($journal)+1;
}
else{
$add=sizeof($journal+1);
}
$this->journal_type_code="J".$add;
$response = JournalType::create([
'journal_type_code'=>$this->journal_type_code,
'journal_type_name'=>$this->journal_type_name,
]);
//dd($response);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function update(){
$this->validate($this->updatedrules,$this->messages);
$response=JournalType::find($this->modelid);
if($response){
$response->update([
'journal_type_name'=>$this->journal_type_name,
'status'=>$this->status,
]);
if($response){
$this->cleanVar();
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->updatemsg
]);
}else{
// $this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
public function delete(){
$response=JournalType::find($this->modelid);
if($response){
if($response->delete()){
$this->cleanVar();
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->deletemsg
]);
}else{
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
public function read(){
$journal_types=JournalType::where('journal_type_name','like','%'.$this->search.'%')
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $journal_types;
}
public function render()
{
$datatable=$this->read();
return view('livewire.finance.journal-type-livewire',compact('datatable'));
}
}