CasperSecurity
<?php
namespace App\Http\Livewire\Hrms;
use App\Models\OdRequest;
use App\Models\EmployeeRegistration;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Livewire\WithPagination;
class OdRequestLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $employee_id, $req_id, $od_from,$od_to,$od_location,$od_reason,$manager_id,$hr_id,$status, $data;
public $modelid;
public $showmodal = false;
public $successmsg = "The OD Request data has been saved successfully.";
public $updatemsg = "The OD Request data has been updated successfully.";
public $deletemsg = "The OD Request data has been deleted successfully.";
public $errormsg = "Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = ['delete'];
public $rules = [
'od_reason.required' => 'This field is required',
'employee_id.required'=> 'This field is required',
'req_id.required' => 'This field is required',
'od_from.required' =>'This field is required',
'od_to.required' => 'This field is required',
'od_location.required' =>'This field is required',
];
public $updatedrules = [
'od_reason.required' => 'This field is required',
'employee_id.required'=> 'This field is required',
'req_id.required' => 'This field is required',
'od_from.required' =>'This field is required',
'od_to.required' => 'This field is required',
'od_location.required' =>'This field is required',
];
public $messages = [
'od_reason.required' => 'This field is required',
'od_reason.max' => 'This field allows maximum 195 character',
'employee_id'=> 'This field is required',
'req_id' => 'This field is required',
'od_from' =>'This field is required',
'od_to' => 'This field is required',
'od_location' =>'This field is required',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick(){
$this->employee_id=Auth::user()->employee_id;
$req=OdRequest::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="OD".$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 = OdRequest::find($id);
if ($response) {
$this->employee_id = $response['employee_id'];
$this->req_id = $response['req_id'];
$this->od_from = $response['od_from'];
$this->od_to = $response['od_to'];
$this->od_location = $response['od_location'];
$this->od_reason = $response['od_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);
// dd('');
// $response = OdRequest::where('employee_id', $this->employee_id)->latest()->first();
// if ($response) {
$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!=null && $this->manager_id!=null) {
$oddata=[
'employee_id'=> $this->employee_id,
'req_id' => $this->req_id,
'od_from' => $this->od_from,
'od_to' => $this->od_to,
'od_location' => $this->od_location,
'od_reason' => $this->od_reason,
'manager_id' => $this->manager_id,
'hr_id' => $this->hr_id,
];
$response = OdRequest::create($oddata);
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 = OdRequest::find($this->modelid);
if ($response) {
$response->update([
'employee_id'=> $this->employee_id,
'req_id' => $this->req_id,
'od_from' => $this->od_from,
'od_to' => $this->od_to,
'od_location' => $this->od_location,
'od_reason' => $this->od_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 = OdRequest::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()
{
$od_requests=OdRequest::search($this->search)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $od_requests;
}
public function render()
{
$datatable = $this->read();
return view('livewire.hrms.od-request-livewire',compact('datatable'));
}
}