CasperSecurity
<?php
namespace App\Http\Livewire\Finance;
use Livewire\Component;
use App\Models\CashType;
use App\Models\LedgerHead;
use App\Models\LedgerheadType;
use Livewire\WithPagination;
class CashTypeLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $cash_type_id, $cash_type_code,$cash_type_name,$ledger_head, $status, $data;
public $modelid;
public $showmodal = false;
public $successmsg = "The Cash Type has been saved successfully.";
public $updatemsg = "The Cash Type has been updated successfully.";
public $deletemsg = "The Cash Type data has been deleted successfully.";
public $duplicateerrormsg = "Sorry !!! Cash 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 = [
/* 'cash_type_id' => 'required|max:25',*/
'cash_type_name' => 'required',
'ledger_head' => 'required',
];
public $updatedrules = [
/* 'cash_type_id' => 'required|max:25',*/
'cash_type_name' => 'required',
'ledger_head' => 'required',
'status' => 'required',
];
public $messages = [
/* 'cash_type_id.required' => 'This field is required',*/
'cash_type_name.required' => 'This field is required',
'ledger_head.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->cash_type_name=null;
$this->ledger_head=null;
$this->status=null;
}
public function loadData($id)
{
$response = CashType::find($id);
if ($response) {
$this->cash_type_name = $response['cash_type_name'];
$this->ledger_head=$response['ledger_head_id'];
$this->status = $response['status'];
}
}
public function save(){
$this->validate($this->rules,$this->messages);
$response=CashType::where('cash_type_name',$this->cash_type_name)->first();
if($response){
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->duplicateerrormsg
]);
}else {
$response = CashType::create([
/*'cash_type_id' => $this->cash_type_id,*/
'cash_type_name' => $this->cash_type_name,
'ledger_head_id'=>$this->ledger_head,
]);
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 =CashType::find($this->modelid);
if ($response) {
$response->update([
/* 'cash_type_id' => $this->cash_type_id,*/
'cash_type_name' => $this->cash_type_name,
'ledger_head_id'=>$this->ledger_head,
'status' => $this->status,
]);
//dd($response);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->updatemsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function delete()
{
$response = CashType::find($this->modelid);
if ($response) {
if ($response->delete()) {
$this->cleanVar();
$this->dispatchBrowserEvent('closemodal');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->deletemsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function read()
{
$cash_types = CashType::where('cash_type_name','like','%'.$this->search.'%')
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $cash_types;
}
public function render()
{
$datatable = [];
$datatable = $this->read();
$ledgertype=LedgerheadType::where('ledger_type','Cash')->latest()->first();
if ($ledgertype != null){
$ledgerhead=LedgerHead::where('status',1)->where('ledger_type_id',$ledgertype->id)->get();
} else {
$ledgerhead = [];
}
return view('livewire.finance.cash-type-livewire', compact('datatable', 'ledgerhead','ledgertype'));
}
}