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 TravelRequestLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $employee_id, $req_id, $travel_from, $travel_to, $from_location,$to_location,$travel_reason,$manager_id,$hr_id,$status, $data;
public $modelid;
public $showmodal = false;
public $successmsg = "The Travel Request data has been saved successfully.";
public $updatemsg = "The Travel Request data has been updated successfully.";
public $deletemsg = "The Travel Request data has been deleted successfully.";
public $duplicateerrormsg = "Sorry !!! Travel Request 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 = [
'travel_reason.required' => 'This field is required',
'employee_id.required' => 'This field is required',
'req_id.required' => 'This field is required',
'travel_from.required' => 'This field is required',
'travel_to.required' => 'This field is required',
'from_location.required' => 'This field is required',
'to_location.required' => 'This field is required',
];
public $updatedrules = [
'travel_reason.required' => 'This field is required',
'employee_id.required' => 'This field is required',
'req_id.required' => 'This field is required',
'travel_from.required' => 'This field is required',
'travel_to.required' => 'This field is required',
'from_location.required' => 'This field is required',
'to_location.required' => 'This field is required',
];
public $messages = [
'travel_reason.required' => 'This field is required',
'travel_reason.max' => 'This field allows maximum 195 character',
'employee_id' => 'This field is required',
'req_id' => 'This field is required',
'travel_from' => 'This field is required',
'travel_to' => 'This field is required',
'from_location' => 'This field is required',
'to_location' => 'This field is required',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick()
{
$this->employee_id=Auth::user()->employee_id;
$req=TravelRequest::all();
if(sizeof($req)<10){
$add="000".sizeof($req)+1;
}
elseif(sizeof($req)<100){
$add="00".sizeof($req)+1;
}
elseif(sizeof($req)<1000){
$add="0".sizeof($req)+1;
}
else{
$add=sizeof($req+1);
}
$this->req_id="TR".$add;
$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->showmodal = false;
}
public function loadData($id)
{
$response = TravelRequest::find($id);
if ($response) {
$this->employee_id = $response['employee_id'];
$this->req_id = $response['req_id'];
$this->travel_from = $response['travel_from'];
$this->travel_to = $response['travel_to'];
$this->from_location = $response['from_location'];
$this->to_location = $response['to_location'];
$this->travel_reason = $response['travel_reason'];
$this->manager_id = $response['$manager_id'];
$this->hr_id = $response['hr_id'];
$this->status = $response['status'];
}
}
public function save()
{
$this->validate($this->rules, $this->messages);
$empdet = EmployeeRegistration::where('employee_id', $this->employee_id)->first();
if ($empdet->manager_id != null) {
$this->manager_id = $empdet->manager_id;
}
if ($empdet->hr_id != null) {
$this->hr_id = $empdet->hr_id;
}
if ($this->hr_id && $this->manager_id) {
$traveldata=[
'employee_id' => $this->employee_id,
'req_id' => $this->req_id,
'travel_from' => $this->travel_from,
'travel_to' => $this->travel_to,
'from_location' => $this->from_location,
'to_location' => $this->to_location,
'travel_reason' => $this->travel_reason,
'manager_id' => $this->manager_id,
'hr_id' => $this->hr_id,
];
$response = TravelRequest::create($traveldata);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
} elseif ($this->hr_id == null && $this->manager_id == null) {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => 'You need to assigned Manager and HR.'
]);
} elseif ($this->hr_id) {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => 'You need to assigned manager.'
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => 'You need to assigned HR.'
]);
}
}
public function update()
{
$this->validate($this->updatedrules, $this->messages);
$response = TravelRequest::find($this->modelid);
if ($response) {
$response->update([
'employee_id' => $this->employee_id,
'req_id' => $this->req_id,
'travel_from' => $this->travel_from,
'travel_to' => $this->travel_to,
'from_location' => $this->from_location,
'to_location' => $this->to_location,
'travel_reason' => $this->travel_reason,
'manager_id' => $this->manager_id,
'hr_id' => $this->hr_id,
'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 = TravelRequest::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 read()
{
$travel_requests = TravelRequest::search($this->search)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $travel_requests;
}
public function render()
{
$dataTables = $this->read();
return view('livewire.hrms.travel-request-livewire',compact('dataTables'));
}
}