CasperSecurity
<?php
namespace App\Http\Livewire\Hrms;
use App\Imports\EmployeeRegistrationImport;
use App\Mail\WelcomeOnBoard;
use App\Models\Designation;
use App\Models\EmployeeRegistration;
use App\Models\EmployeeType;
use App\Models\Role;
use App\Models\RoleUser;
use App\Models\SettingsCity;
use App\Models\Team;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Livewire\Component;
use Livewire\WithFileUploads;
use Livewire\WithPagination;
use Maatwebsite\Excel\Facades\Excel;
class EmployeeInformationLivewire extends Component
{
use WithPagination;
use WithFileUploads;
protected $paginationTheme = 'bootstrap';
public $page_name="Employee Informations";
public $search;
public $perPage=10;
public $orderBy='id';
public $orderAsc='1';
public $name,$employee_photo,$employee_id,$user_id,$email_id,$mobile_no,$gender,$blood_group,$date_of_birth,$designation,$qualification,$manager_id,$hr_id,$role_id,$team_id,$city,$date_of_joining,$employment_status,$status,$created_by;
public $employee_category;
public $wages_type;
public $modelid;
public $showmodal=false;
public $upload_csv_file;
public $successmsg="The Employee account has been created successfully.";
public $updatemsg="The Employee account has been updated successfully.";
public $deletemsg="The Employee account has been deleted successfully.";
public $duplicateerrormsg="Sorry !!! Employee name has already exists in our database. Please Try Another Name";
public $errormsg="Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = ['delete'];
public function uplodEmployee(){
$this->validate([
'upload_csv_file'=>'required'
],[
'upload_csv_file.required'=>'This field is required'
]);
Excel::import(new EmployeeRegistrationImport(),$this->upload_csv_file);
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>"Employee Informations have been uploaded successfully."
]);
}
public $rules=[
'name'=>'required|max:255',
'employee_photo'=>'nullable|max:2048',
'employee_id'=>'nullable',
'email_id'=>'nullable|email||unique:employee_registrations,email_id',
'mobile_no'=>'required|numeric|digits:10||unique:employee_registrations,mobile_no',
'gender'=>'required',
'blood_group'=>'required',
'date_of_birth'=>'required|date',
'designation'=>'required',
'qualification'=>'required',
'manager_id'=>'nullable',
'hr_id'=>'nullable',
'role_id'=>'required',
'team_id'=>'required',
'city'=>'required',
'date_of_joining'=>'required|date',
'employment_status'=>'required',
'employee_category'=>'nullable',
'wages_type'=>'required',
];
public $updatedrules=[
'name'=>'required|max:255',
'employee_photo'=>'nullable|max:2048',
'employee_id'=>'required',
'email_id'=>'nullable|email',
'mobile_no'=>'nullable|numeric|digits:10',
'gender'=>'required',
'blood_group'=>'required',
'date_of_birth'=>'required|date',
'designation'=>'required',
'qualification'=>'required',
'manager_id'=>'nullable',
'hr_id'=>'nullable',
'role_id'=>'required',
'team_id'=>'required',
'city'=>'required',
'date_of_joining'=>'required|date',
'employment_status'=>'required',
'employee_category'=>'nullable',
'wages_type'=>'required',
];
public $messages=[
'name.required'=>'This field is required',
'name.max'=>'This field allows maximum 255 character',
'employee_id.required'=>'This field is required',
'email_id.required'=>'This field is required',
'email_id.email'=>'Please provide a valid email address',
'email_id.unique'=>'This email id already exists in our database',
'mobile_no.required'=>'This field is required',
'mobile_no.numeric'=>'Please provide a valid mobile no',
'mobile_no.digits'=>'Please provide a 10 digits mobile no',
'mobile_no.unique'=>'This mobile no already exists in our database',
'gender.required'=>'This field is required',
'blood_group.required'=>'This field is required',
'date_of_birth.required'=>'This field is required',
'designation.required'=>'This field is required',
'qualification.required'=>'This field is required',
'manager_id.required'=>'This field is required',
'hr_id.required'=>'This field is required',
'role_id.required'=>'This field is required',
'team_id.required'=>'This field is required',
'city.required'=>'This field is required',
'date_of_joining.required'=>'This field is required',
'employment_status.required'=>'This field is required',
'employee_category.required'=>'This field is required',
'wages_type.required'=>'This field is required',
];
public function updatedEmployeePhoto($val)
{
$this->employee_photo = $this->employee_photo->store('employee_photo');
}
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick(){
$this->cleanVar();
$this->resetValidation();
$this->showmodal=true;
//$this->dispatchBrowserEvent('livewire:load');
}
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->name=null;
$this->employee_photo=null;
$this->employee_id=null;
$this->user_id=null;
$this->email_id=null;
$this->mobile_no=null;
$this->gender=null;
$this->blood_group=null;
$this->date_of_birth=null;
$this->designation=null;
$this->qualification=null;
$this->manager_id=null;
$this->hr_id=null;
$this->role_id=null;
$this->team_id=null;
$this->city=null;
$this->date_of_joining=null;
$this->employment_status=null;
$this->employee_category=null;
$this->wages_type=null;
$this->status=null;
$this->showmodal=false;
}
public function loadData($id){
$response=EmployeeRegistration::find($id);
if($response){
$this->name=$response['name'];
$this->employee_photo=$response['employee_photo'];
$this->employee_id=$response['employee_id'];
$this->user_id=$response['user_id'];
$this->email_id=$response['email_id'];
$this->mobile_no=$response['mobile_no'];
$this->gender=$response['gender'];
$this->blood_group=$response['blood_group'];
$this->date_of_birth=$response['date_of_birth'];
if($response['designation']){
$designationinfo=Designation::where('id',$response['designation'])->first();
if($designationinfo){
$this->designation=$designationinfo['id'];
}
}
$this->qualification=$response['qualification'];
$this->manager_id=$response['manager_id'];
$this->hr_id=$response['hr_id'];
$this->role_id=$response['role_id'];
$this->team_id=$response['team_id'];
$this->city=$response['city_id'];
$this->date_of_joining=$response['date_of_joining'];
$this->employment_status=$response['employment_status'];
$this->employee_category=$response['employee_category'];
$this->wages_type=$response['wages_type'];
$this->status=$response['status'];
}
}
//todo: Employee Registration Save
public function save(){
$this->validate($this->rules,$this->messages);
$designation="";
// if($this->designation){
// $designation=Designation::find($this->designation)->designation_name;
// }
$empregcount=EmployeeRegistration::get()->count() + 1;
if($empregcount<10){
$this->employee_id="OSSE00".$empregcount;
}else if($empregcount<100){
$this->employee_id="OSSE0".$empregcount;
}else{
$this->employee_id="OSSE".$empregcount;
}
$response=EmployeeRegistration::create([
'name'=>$this->name,
'employee_photo'=>$this->employee_photo,
'employee_id'=>$this->employee_id,
'email_id'=>$this->email_id,
'mobile_no'=>$this->mobile_no,
'gender'=>$this->gender,
'blood_group'=>$this->blood_group,
'date_of_birth'=>$this->date_of_birth,
'designation'=>$this->designation,
'qualification'=>$this->qualification,
'manager_id'=>$this->manager_id,
'hr_id'=>$this->hr_id,
'role_id'=>$this->role_id,
'team_id'=>$this->team_id,
'city_id'=>$this->city,
'date_of_joining'=>$this->date_of_joining,
'employment_status'=>$this->employment_status,
'employee_category'=>$this->employee_category,
'wages_type'=>$this->wages_type,
'status'=>'1',
'created_by'=>Auth::user()->name,
]);
if($response){
/*$userinfo=User::where('email',$this->email_id)->first();
$userpassword="";
if(!$userinfo){
$userpassword='Password@'.$this->employee_id;
$userinfo=User::create([
'name'=>$this->name,
'email'=>$this->email_id,
'password'=>Hash::make($userpassword),
'designation_id'=>$this->designation,
]);
if($userinfo){
$response->user_id=$userinfo['id'];
$response->save();
$role = Role::find($this->role_id);
$team = Team::find($this->team_id);
if($role) {
$userinfo->attachRole($role,$team);
}
}
}*/
/*if($response && $userinfo){
$employeeregistraion=json_decode($response,true);
$userinformation=json_decode($userinfo,true);
$userinformation['password_string']=$userpassword;
$userinfo->notify(new \App\Notifications\EmployeeRegistration($employeeregistraion,$userinformation));
}*/
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->successmsg
]);
}else{
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
//todo: Employee Registration Update
public function update(){
$this->validate($this->updatedrules,$this->messages);
$response=EmployeeRegistration::find($this->modelid);
if($response){
$designation="";
if($this->designation){
$designation=Designation::find($this->designation)->designation_name;
}
$response->update([
'name'=>$this->name,
'employee_photo'=>$this->employee_photo,
'employee_id'=>$this->employee_id,
'email_id'=>$this->email_id,
'mobile_no'=>$this->mobile_no,
'gender'=>$this->gender,
'blood_group'=>$this->blood_group,
'date_of_birth'=>$this->date_of_birth,
'designation'=>$this->designation,
'qualification'=>$this->qualification,
'manager_id'=>$this->manager_id,
'hr_id'=>$this->hr_id,
'role_id'=>$this->role_id,
'team_id'=>$this->team_id,
'city_id'=>$this->city,
'date_of_joining'=>$this->date_of_joining,
'employment_status'=>$this->employment_status,
'employee_category'=>$this->employee_category,
'wages_type'=>$this->wages_type,
'status'=>$this->status,
'modified_by'=>Auth::user()->name,
]);
if($response){
/* $userinfo=User::where('email',$this->email_id)->first();
if($userinfo){
$userinfo->name=$response['name'];
$userinfo->email=$response['email_id'];
$userinfo->designation_id=$this->designation;
$userinfo->status=$this->status;
$userinfo->save();
$checkroleuser=RoleUser::where('user_id',$response->id)->get();
if($checkroleuser){
foreach ($checkroleuser as $roleuser){
$role = Role::find($roleuser->role_id);
$team = Team::find($roleuser->team_id);
$userinfo->detachRole($role,$team);
}
}
$role = Role::find($this->role_id);
$team = Team::find($this->team_id);
if($role) {
$userinfo->attachRole($role,$team);
}
}*/
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>$this->updatemsg
]);
}else{
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>$this->errormsg
]);
}
}
}
//todo: Employee Registration Delete
public function delete(){
$response=EmployeeRegistration::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(){
$datas =EmployeeRegistration::search($this->search)->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')->paginate($this->perPage);
foreach ($datas as $data){
$data->user;
if($data->manager ==""){
$data->manger_name="NA";
}else{
$data->manger_name=$data->manager->name;
}
// if($data->getDesignation ==""){
// $data->designation="NA";
// }else{
// $data->designation=$data->getDesignation->designation_name;
// }
if($data->hr ==""){
$data->hr_name="NA";
}else{
$data->hr_name=$data->hr->name;
}
if($data->role ==""){
$data->role_name="NA";
}else{
$data->role_name=$data->role->display_name;
}
if($data->team ==""){
$data->team_name="NA";
}else{
$data->team_name=$data->team->display_name;
}
if($data->employeetype ==""){
$data->employee_type_name="NA";
}else{
$data->employee_type_name=$data->employeetype->emp_type_name;
}
if($data->city ==""){
$data->city_name="NA";
}else{
$data->city_name=$data->city->city_name;
}
}
return $datas;
}
public function getEmpChildren($employees){
$employeeundermanger=[];
if(sizeof($employees)>0){
foreach ($employees as $employee){
$empimage="";
if($employee['employee_photo']){
$empimage=Storage::url($employee['employee_photo']);
}else{
if($employee['gender']=='Male'){
$empimage=asset('icons/employee_male.png');
}else{
$empimage=asset('icons/employee_female.png');
}
}
array_push($employeeundermanger,[
'id'=>$employee['employee_id'],
'data'=>[
'imageURL'=>$empimage,
'name'=>$employee['name'],
],
'options'=>[
'nodeBGColor'=>'#fff',
'nodeBGColorHover'=>'#fff',
],
'children'=>$this->getEmpChildren($employee['employeeundermanger']),
]);
}
}
return $employeeundermanger;
}
public function getAllEmployee(){
$emparray=[];
$employees=EmployeeRegistration::with('employeeundermanger')->orderBy('manager_id','asc')->get();
foreach ($employees as $emp){
$empimage="";
if($emp['employee_photo']){
$empimage=Storage::url($emp['employee_photo']);
}else{
if($emp['gender']=='Male'){
$empimage=asset('icons/employee_male.png');
}else{
$empimage=asset('icons/employee_female.png');
}
}
array_push($emparray,[
'id'=>$emp['employee_id'],
'data'=>[
'imageURL'=>$empimage,
'name'=>$emp['name'],
],
'options'=>[
'nodeBGColor'=>'#fff',
'nodeBGColorHover'=>'#fff',
],
'children'=>$this->getEmpChildren($emp['employeeundermanger']),
]);
}
if(sizeof($emparray)>0){
return json_encode($emparray[0],true);
}else{
return json_encode([],true);
}
}
public function render()
{
$datatable=$this->read();
$allemployees=$this->getAllEmployee();
$designations=Designation::select('id','designation_name')->where('status',1)->get();
$departments=Team::select('id','display_name')->get();
$roles=Role::select('id','display_name')->where('id','>',1)->get();
$employees=EmployeeRegistration::select('id','name','designation')->where('wages_type','MONTHLY')->where('status','1')->get();
$employeetypes=EmployeeType::select('id','emp_type_name')->where('status',1)->get();
$cities=SettingsCity::select('id','city_name')->where('status',1)->get();
return view('livewire.hrms.employee-information-livewire',compact('datatable','designations','departments','roles','employees','employeetypes','cities','allemployees'));
}
}