CasperSecurity
<?php
namespace App\Http\Livewire\Finance;
use Livewire\Component;
use App\Models\AccountGroup;
use App\Models\AccountType;
use Livewire\WithPagination;
class ChartofAccounGroupsLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage=10;
public $orderBy='id';
public $orderAsc='1';
public $status,$data;
public $modelid;
public $showmodal=false;
public $account_type_id,$account_group_code,$account_group_name,$account_type_name;
public $pageno_det,$activeTab;
public $successmsg="The Payroll data has been saved successfully.";
public $updatemsg="The Payroll item data has been updated successfully.";
public $deletemsg="The Payroll item data has been deleted successfully.";
public $duplicateerrormsg="Sorry !!! Payroll item name has already exists in our database. Please Try Another Name";
public $errormsg="Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = [
'page_det'=>'setPage'
];
public function setPage($page){
$this->pageno_det=$page;
//dd('');
}
public $rules=[
'account_type_name'=>'required',
'account_group_code'=>'required|max:25',
'account_group_name'=>'required',
];
public $updatedrules=[
'account_type_name'=>'required',
'account_group_code'=>'required',
'account_group_name'=>'required',
'status'=>'required',
];
public $messages=[
'account_type_name.required'=>'This field is required',
'account_group_code.required'=>'This field is required',
'account_group_name.required'=>'This field is required',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick($id,$name){
//dd($id);
$this->resetValidation();
$this->account_type_id=$id;
$this->account_type_name=$name;
$count_data=AccountGroup::all();
$this->account_group_code=sizeof($count_data)+1;
$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->account_type_name=null;
$this->account_group_code=null;
$this->account_group_name=null;
$this->status=null;
$this->showmodal=false;
}
public function loadData($id){
$response=AccountGroup::find($id);
if($response){
$acc_type_name=AccountType::where('id',$response->account_type_id)->first();
$this->account_type_id=$response['account_type_id'];
$this->account_type_name= $acc_type_name['account_type'];
$this->account_group_code=$response['account_group_code'];
$this->account_group_name=$response['account_group_name'];
$this->status=$response['status'];
}
}
public function save(){
$this->validate($this->rules,$this->messages);
$response = AccountGroup::create([
'account_type_id' => $this->account_type_id,
'account_group_code' => $this->account_group_code,
'account_group_name' => $this->account_group_name,
]);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$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=AccountGroup::find($this->modelid);
if($response){
$response->update([
'account_group_code' => $this->account_group_code,
'account_group_name' => $this->account_group_name,
'status'=>$this->status,
]);
if($response){
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->updatemsg
]);
}else{
// $this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
public function delete(){
$response=AccountGroup::find($this->modelid);
if($response){
if($response->delete()){
$this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->deletemsg
]);
}else{
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
// public function getComponentDetails(){
// if(session('session_tab')){
// $response=AccountGroup::search($this->search)->where('account_type_id',session('session_tab'))
// ->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
// ->paginate($this->perPage);;
// $this->group_details=AccountType::where('id',session('session_tab'))->first();
// return $response;
// }
// else{
// $response=[];
// $this->group_details=AccountType::where('id',session('session_tab'))->first();
// return $response;
// }
// }
public function switchTab($id)
{
// dd($id);
$check=AccountType::where('id',$id)->first();
if($check){
$this->activeTab = $id;
session([
'session_tab'=>$id,
]);
return redirect('Finance/groups');
}
}
public function ledgerheadClick($id){
$check=AccountGroup::where('id',$id)->first();
// dd($check);
if($check){
session([
'group_det'=>$id,
]);
return redirect('Finance/legder_head');
}
}
public function render()
{
// dd($this->pageno_det);
if(session('session_tab')){
$this->activeTab=session('session_tab');
$accounttypes=AccountType::where('status',1)->get();
$datatable=AccountGroup::search($this->search)->where('account_type_id',session('session_tab'))
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);;
$this->group_details=AccountType::where('id',session('session_tab'))->first();
//dd($this->group_details);
}
else{
// dd(2);
$datatable=[];
$this->group_details=AccountType::where('id',session('session_tab'))->first();
}
// if($this->group_details){
// dd($this->group_details);
// }
//dd($this->group_details);
return view('livewire.finance.chartof-accoun-groups-livewire',compact('datatable','accounttypes'));
}
}