CasperSecurity
<?php
namespace App\Http\Livewire\Hrms;
use App\Models\EmployeeRegistration;
use App\Models\TravelRequest;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Livewire\WithPagination;
class TravelApprovalLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $modelid;
public $showmodal = false;
public $successmsg = "The Travel Type has been saved successfully.";
public $approvemsg = "The Travel Request has been approve successfully.";
public $rejectmsg = "The Travel Type data has been rejetct successfully.";
public $errormsg = "Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = ['reject'];
public function closemodalclick()
{
$this->resetValidation();
}
public function cleanVar()
{
$this->modelid = null;
$this->status = null;
$this->showmodal = false;
}
public function approveclick($id)
{
$response = TravelRequest::find($id);
if ($response) {
if($response->manager_approval_status==1){
$response->update(['manager_approval_status'=>2]);
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' =>'Approved By Manager.Waiting for HR approval'
]);
}
elseif($response->manager_approval_status==2 && $response->hr_approval_status==1){
$response->update(['hr_approval_status'=>2,
'status'=>2
]);
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => 'Approved By HR'
]);
}
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
public function rejectclick($id)
{
$response = TravelRequest::find($id);
if ($response) {
if ($response) {
$response->update(['status'=>0]);
$this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->rejectmsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function read()
{
if(Auth::user()->name=="superadmin" || Auth::user()->name=="admin"){
$travel_requests = TravelRequest::search($this->search)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $travel_requests;
}
else{
$empdet=EmployeeRegistration::where('employee_id',Auth::user()->employee_id)->first();
if($empdet){
$travel_requests = TravelRequest::where('manager_id',$empdet->id)->orWhere('hr_id',$empdet->id)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $travel_requests;
}
else{
return [];
}
}
}
public function render()
{
$datatable=$this->read();
return view('livewire.hrms.travel-approval-livewire',compact('datatable'));
}
}