CasperSecurity
<?php
namespace App\Http\Livewire\Hrms;
use App\Models\EmployeeRegistration;
use App\Models\OtRequest;
use App\Models\Otrule;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Livewire\WithPagination;
class OTRequestLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage=10;
public $orderBy='id';
public $orderAsc='1';
public $employee_id,$req_id,$ot_type_id,$ot_date,$ot_hour,$ot_reason,
$hr_approval_status,$remarks,$manager_id,$manager_approval_status,$ot,$hr_id,$status,$data;
public $modelid;
public $showmodal=false;
public $successmsg="The Ot Request has been saved successfully.";
public $updatemsg="The Ot Type has been updated successfully.";
public $deletemsg="The Ot Type data has been deleted successfully.";
public $duplicateerrormsg="Sorry !!! Ot Type 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=[
'employee_id.required'=>'This field is required',
'req_id.required'=>'This field is required',
'ot_type_id.required'=>'This field is required',
'ot_date.required'=>'This field is required',
'ot_hour.required'=>'This field is required',
];
public $updatedrules=[
'employee_id.required'=>'This field is required',
'req_id.required'=>'This field is required',
'ot_type_id.required'=>'This field is required',
'ot_date.required'=>'This field is required',
'ot_hour.required'=>'This field is required',
];
public $messages=[
'employee_id.required'=>'This field is required',
'req_id.required'=>'This field is required',
'ot_type_id.required'=>'This field is required',
'ot_date.required'=>'This field is required',
'ot_hour.required'=>'This field is required',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick(){
$this->employee_id=Auth::user()->employee_id;
$req=OtRequest::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="OT".$add;
//dd($this->req_id);
// dd($this->employee_id);
$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=OTRequest::find($id);
if($response){
$this->employee_id=$response['employee_id'];
$this->req_id=$response['req_id'];
$this->ot_type=$response['ot_type'];
$this->ot_date=$response['ot_date'];
$this->ot_hour=$response['ot_hour'];
$this->ot_reason=$response['ot_reason'];
$this->manager_id=$response['manager_id'];
$this->manager_approval_status=$response['manager_approval_status'];
$this->hr_id=$response['hr_id'];
$this->hr_approval_status=$response['hr_approval_status'];
$this->remarks=$response['remarks'];
$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) {
$otdata=[
'employee_id' => $this->employee_id,
'req_id' => $this->req_id,
'ot_type_id' => $this->ot_type_id,
'ot_date' => $this->ot_date,
'ot_hour' => $this->ot_hour,
'ot_reason' => $this->ot_reason,
'manager_id' => $this->manager_id,
// 'manager_approval_status' => $this->manager_approval_status,
'hr_id' => $this->hr_id,
// 'hr_approval_status' => $this->hr_approval_status,
'remarks' => $this->remarks,
];
$response = OTRequest::create($otdata);
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=OTRequest::find($this->modelid);
if($response){
$response->update([
'employee_id'=>$this->employee_id,
'req_id'=>$this->req_id,
'ot_type_id'=>$this->ot_type_id,
'ot_date'=>$this->ot_date,
'ot_hour'=>$this->ot_hour,
'ot_reason'=>$this->ot_reason,
'manager_id'=>$this->manager_id,
'manager_approval_status'=>$this->manager_approval_status,
'hr_id'=>$this->hr_id,
'hr_approval_status'=>$this->hr_approval_status,
'remarks'=>$this->remarks,
'status'=>$this->status,
]);
//dd($response);
if($response){
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->updatemsg
]);
}else{
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
public function delete(){
$response=OTRequest::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(){
$ot_requests=OTRequest::search($this->search)
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
return $ot_requests;
}
public function render()
{
$datatable = [];
$datatable = $this->read();
if (sizeof($datatable) > 0) {
foreach ($datatable as $key => $data) {
$ot = Otrule::where('id', $data->ot_type_id)->first();
if ($ot) {
$datatable[$key]['ot_type'] = $ot->ot_type;
} else {
$datatable[$key]['ot_type'] = "NA";
}
}
}
$ottype = Otrule::where('status', 1)->get();
return view('livewire.hrms.ot-request-livewire',compact('datatable','ottype'));
}
}