CasperSecurity
<?php
namespace App\Http\Livewire\Projectmanager;
use App\Models\LeaveEncashmentApproval;
use App\Support\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Livewire\WithPagination;
use Spatie\Activitylog\Models\Activity;
class LeaveEncashmentApprovalLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $modelid;
public $search=null;
public $orderBy = 'id';
public $perPage = 10;
public $orderAsc = false;
public $status=true;
public $employee_code,$employee_name,$department,$designation,$leave_type,$no_of_days,$project_manager,$remarks,$created_by;
public function cleanVars()
{
$this->modelid=null;
$this->employee_code=null;
$this->employee_name=null;
$this->department=null;
$this->designation=null;
$this->leave_type=null;
$this->no_of_days=null;
$this->project_manager=null;
$this->remarks=null;
$this->status=null;
}
// public function updatedProjectManager(){
// dd($this->project_manager);
// }
//todo::Notifcation Of Success Or Error Message
public $successcreatemsg="Your informations have been successfully saved!";
public $successupdatemsg="Your changes have been successfully updated!";
public $successdeletemsg="Your selected data has been successfully deleted!";
public $errormsg="Oops !!! Something went wrong.";
public function createsuccessmsg(){
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'title'=>'DATA SAVED',
'message' => $this->successcreatemsg,
]);
}
public function updatesuccessmsg(){
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'title'=>'DATA UPDATED',
'message' => $this->successupdatemsg,
]);
}
public function deletesuccessmsg(){
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'title'=>'DATA DELETED',
'message' => $this->successdeletemsg,
]);
}
public function errormsg(){
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'title'=>'Sorry !!!',
'message'=>$this->errormsg,
]);
}
public function commonmsg($type,$title,$msg){
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => $type,
'title'=>$title,
'message' => $msg,
]);
}
//todo: Employee Gender Create Rules
protected $rules=[
'employee_code' => 'required',
'employee_name' => 'required',
'department' => 'required',
'designation' => 'required',
'leave_type' => 'required',
'no_of_days' => 'required',
'project_manager' => 'required',
'remarks' => 'required|max:100',
'status' => 'nullable',
];
//todo: Employee Gender Updated Rules
protected $updaterules=[
'employee_code' => 'required',
'employee_name' => 'required',
'department' => 'required',
'designation' => 'required',
'leave_type' => 'required',
'no_of_days' => 'required',
'project_manager' => 'required',
'remarks' => 'required|max:100',
'status' => 'nullable',
];
//todo: Employee Gender Validation Messages
protected $messages = [
'employee_code.required' => 'This field cannot be empty.',
'employee_name.required' => 'This field cannot be empty.',
'department.required' => 'This field cannot be empty.',
'designation.required' => 'This field cannot be empty.',
'leave_type.required' => 'This field cannot be empty.',
'no_of_days.required' => 'This field cannot be empty.',
'status.required' => 'This field cannot be empty.',
];
public function resetvalid(){
$this->resetValidation();
}
public function updatingSearch()
{
$this->resetPage();
}
public function createclick(){
$this->cleanVars();
$this->resetValidation();
$this->status=true;
}
public function updateclick($id){
$this->modelid=$id;
$this->loadData($id);
}
public function loadData($id){
$data=LeaveEncashmentApproval::find($id);
if($data){
$this->employee_code=$data->employee_code;
$this->employee_name=$data->employee_name;
$this->department=$data->department;
$this->designation=$data->designation;
$this->leave_type=$data->leave_type;
$this->no_of_days=$data->no_of_days;
$this->project_manager=$data->project_manager;
$this->remarks=$data->remarks;
$this->status=$data->status;
}
//dd($this->lat);
}
public function deleteclick($id){
$this->modelid=$id;
}
/* modelData */
public function setDataForPostPut(){
return [
'employee_code' => $this->employee_code,
'employee_name' => $this->employee_name,
'department' => $this->department,
'designation' => $this->designation,
'leave_type' => $this->leave_type,
'no_of_days' => $this->no_of_days,
'project_manager' => $this->project_manager,
'remarks' => $this->remarks,
'created_by' => $this->created_by,
];
}
public function submit()
{
$validatedData = $this->validate($this->rules,$this->messages);
$response=LeaveEncashmentApproval::where('employee_code',$this->employee_code)->first();
if($response){
$activity = Activity::all()->last();
$activity->description;
$activity->subject;
$activity->changes;
$this->dispatchBrowserEvent('closemodal');
$this->commonmsg('error',"Sorry !!!","Gender name already exists.");// name already exists function
}else{
$response=LeaveEncashmentApproval::create($this->setDataForPostPut());
if($response) {
//session()->flash('success', $this->successmsg );
$this->dispatchBrowserEvent('closemodal');
$this->createsuccessmsg();
}else{
$this->dispatchBrowserEvent('closemodal');
$this->errormsg();
}
}
//dd($validatedData);
}
public function update()
{
$validatedData = $this->validate($this->updaterules,$this->messages);
$response=LeaveEncashmentApproval::find($this->modelid);
$response->update($this->setDataForPostPut());
if($response) {
$activity = Activity::all()->last();
$activity->description;
$activity->subject;
$activity->changes;
$this->cleanVars();
//session()->flash('success', $this->successmsg );
$this->dispatchBrowserEvent('closemodal');
$this->updatesuccessmsg();
}else{
//session()->flash('error',$this->errormsg);
$this->dispatchBrowserEvent('closemodal');
$this->errormsg();
}
}
public function delete(){
$response=LeaveEncashmentApproval::find($this->modelid);
if($response->delete()) {
$activity = Activity::all()->last();
$activity->description;
$activity->subject;
$activity->changes;
$this->cleanVars();
//session()->flash('success', $this->successmsg );
$this->dispatchBrowserEvent('closemodal');
$this->deletesuccessmsg();
}else{
//session()->flash('error',$this->errormsg);
$this->dispatchBrowserEvent('closemodal');
$this->errormsg();
}
}
//todo: Employee Gender Get All Data For Table
public function read($search,$perPage,$orderBy,$orderAsc){
$request=new Request();
$request->search = $search;
$request->orderBy = $orderBy;
$request->orderAsc = $orderAsc;
$response=LeaveEncashmentApproval::search($search)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->whereNull('deleted_at') //todo: Employee Gender Softdelete condition
->get();
if($response) {
return (new Collection($response))->paginate($perPage);
}else{
return (new Collection([]))->paginate($perPage);
}
}
public function render()
{
$datatables=new Collection();
$this->created_by=Auth::user()->id;
$datatables=$this->read($this->search,$this->perPage,$this->orderBy,$this->orderAsc);
return view('livewire.projectmanager.leave-encashment-approval-livewire', compact('datatables'));
}
}