File: /var/www/html/dev-hrms/app/Http/Controllers/EmployeeTrainingController.php
<?php
namespace App\Http\Controllers;
use App\Models\EmployeeProfile;
use App\Models\Profiles;
use App\Models\Approval;
use App\Models\FamilyMember;
use App\Models\EmployeeNomineeDetail;
use App\Models\EmployeeTrainingDetail;
use App\Library\UserLib;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Http\Request;
class EmployeeTrainingController extends Controller
{
protected $userLib;
protected $UserSessionId;
protected $TeamList = array();
protected $LevelList = array();
protected $ViewProfileIds = array();
protected $ViewApprovalUsers = array();
public function __construct(UserLib $userLib)
{
$this->userLib = $userLib;
}
public function getAccessUserProfileList($ProfileId = '')
{
$users = Profiles::select('*')
->leftJoin('tbl_roles', 'tbl_roles.RoleId', '=', 'tbl_profiles.RoleId')
->where('tbl_roles.ReadPermission', 'like', '1')
->where('tbl_roles.WritePermission', 'like', '1')
->where('tbl_roles.UpdatePermission', 'like', '1')
->where('tbl_roles.ApprovalPermission', 'like', '1')
->where('tbl_profiles.ServiceBookApprovalPermission', 'like', 'YES')
->where('tbl_profiles.ProfileId', 'like', $ProfileId) //check not equal to user profile
->get();
if ($users->count() > 0) { //email is exist
foreach ($users as $key => $UValue) {
$ProfileId = $UValue['ProfileId'];
$ProfileName = $UValue['ProfileName'];
$ParentId = $UValue['ParentId'];
$this->TeamList[] = $UValue;
$this->ViewProfileIds[] = $ProfileId;
if ($ParentId) {
$this->getAccessUserProfileList($ParentId);
}
}
}
return $responseData = $this->ViewProfileIds;
//return response()->json($responseData);
}
// Get Approval User
public function getApprovalUserLevel($ProfileId = '', $Level = 1)
{
$users = Profiles::select('*')
->leftJoin('tbl_roles', 'tbl_roles.RoleId', '=', 'tbl_profiles.RoleId')
->where('tbl_roles.ReadPermission', 'like', '1')
->where('tbl_roles.WritePermission', 'like', '1')
->where('tbl_roles.UpdatePermission', 'like', '1')
->where('tbl_roles.ApprovalPermission', 'like', '1')
//->where('tbl_profiles.ServiceBookApprovalPermission','like','YES')
->where('tbl_profiles.ProfileId', 'like', $ProfileId) //check not equal to user profile
->get();
// echo $users;
//exit;
if ($users->count() > 0) { //email is exist
foreach ($users as $key => $UValue) {
$ProfileId = $UValue['ProfileId'];
$ProfileName = $UValue['ProfileName'];
$ParentId = $UValue['ParentId'];
$this->TeamList[] = $UValue;
$this->ViewApprovalUsers[] = $ProfileId;
//$this->LevelList[] = $Level;
//echo $Level.'-'.$ParentId.'<br/>';
if ($Level >= 2) {
continue;
}
if ($ParentId) {
$Level++;
$this->getApprovalUserLevel($ParentId, $Level);
}
}
}
return $this->ViewApprovalUsers;
}
public function addEmpTrainingDetail()
{
$this->UserSessionId = session('UserId');
if (empty($this->UserSessionId)) {
return redirect('/');
}
return view('admin.EmployeeProfile.add-training-detail');
}
public function empTrainingDetailList()
{
$this->UserSessionId = session('UserId');
if (empty($this->UserSessionId)) {
return redirect('/');
}
$TrainingDetailList = EmployeeTrainingDetail::select('tbl_employees_training_details.*', 'tbl_employees.EmployeeName as EmployeeName')
->leftJoin('tbl_employees', 'tbl_employees_training_details.EmployeeId', 'tbl_employees.EmployeeId')
->where('tbl_employees_training_details.Status', '1')
->where('tbl_employees_training_details.IsDeleted', '0')
->get();
return view('admin.EmployeeProfile.training-detail-list', ['TrainingDetailList' => $TrainingDetailList]);
}
### Store Training Detail
public function storeTrainingDetail(Request $request)
{
$this->UserSessionId = session('UserId');
if (empty($this->UserSessionId)) {
return redirect('/');
}
$input = $request->all();
$data = $input;
$StartDate = '';
if ($request->input('StartDate')) {
$SDate = str_replace('/', '-', $request->input('StartDate'));
}
$StartDate = date('Y-m-d', strtotime($SDate));
$EndDate = '';
if ($request->input('EndDate')) {
$EDate = str_replace('/', '-', $request->input('EndDate'));
}
$EndDate = date('Y-m-d', strtotime($EDate));
// Training Certificate
$CertName = $this->userLib->useridGenrator();
$TrainingCertificatePath = '';
if ($request->hasFile('TrainingCertificate')) {
$file = $request->file('TrainingCertificate');
$extension = $file->getClientOriginalExtension();
$FileName = $CertName . '-' . time() . '.' . $extension;
Storage::putFileAs(
'public/training_certificate',
$file,
$FileName
);
$TrainingCertificatePath = Storage::url('training_certificate/' . $FileName);
}
unset($data['_token']);
unset($data['StartDate']);
unset($data['EndDate']);
unset($data['TrainingCertificate']);
$data['TrainingDetailId'] = $this->userLib->useridGenrator();
$data['FromDate'] = $StartDate;
$data['ToDate'] = $EndDate;
$data['TrainingCertificatePath'] = $TrainingCertificatePath;
$data['IsDeleted'] = 0;
$data['Status'] = 1;
$data['Created_by'] = session('UserId');
$data['Created_at'] = date('Y-m-d H:i:s', time());
$data['Updated_by'] = session('UserId');
$data['Updated_at'] = date('Y-m-d H:i:s', time());
$TrainingDetail = EmployeeTrainingDetail::insert($data);
if ($TrainingDetail) {
return response()->json('status_success');
} else {
return response()->json('status_failed');
}
}
### Edit Training Detail for Edit
public function editTrainingDetail(Request $request)
{
$this->UserSessionId = session('UserId');
if (empty($this->UserSessionId)) {
return redirect('/');
}
$TrainingId = $request->input('TrainingId');
$GetTrainingDetail = EmployeeTrainingDetail::select('tbl_employees_training_details.*')
->where('TrainingDetailId', $TrainingId)
->get()
->first();
return response()->json([
'status' => 200,
'GetTrainingDetail' => $GetTrainingDetail,
]);
}
### Update Training Detail
public function updateTrainingDetail(Request $request)
{
$this->UserSessionId = session('UserId');
if (empty($this->UserSessionId)) {
return redirect('/');
}
$input = $request->all();
$data = $input;
$TrainingId = $request->input('TrainingId');
$StartDate = '';
if ($request->input('StartDate')) {
$SDate = str_replace('/', '-', $request->input('StartDate'));
}
$StartDate = date('Y-m-d', strtotime($SDate));
$EndDate = '';
if ($request->input('EndDate')) {
$EDate = str_replace('/', '-', $request->input('EndDate'));
}
$EndDate = date('Y-m-d', strtotime($EDate));
// Training Certificate
$CertName = $this->userLib->useridGenrator();
if ($request->hasFile('TrainingCertificate')) {
$file = $request->file('TrainingCertificate');
$extension = $file->getClientOriginalExtension();
$FileName = $CertName . '-' . time() . '.' . $extension;
Storage::putFileAs(
'public/training_certificate',
$file,
$FileName
);
$TrainingCertificatePath = Storage::url('training_certificate/' . $FileName);
$data['TrainingCertificatePath'] = $TrainingCertificatePath;
}
unset($data['_token']);
unset($data['StartDate']);
unset($data['EndDate']);
unset($data['TrainingCertificate']);
unset($data['TrainingId']);
$data['FromDate'] = $StartDate;
$data['ToDate'] = $EndDate;
$data['Updated_by'] = session('UserId');
$data['Updated_at'] = date('Y-m-d H:i:s', time());
$TrainingDetail = EmployeeTrainingDetail::where('TrainingDetailId', $TrainingId)->update($data);
if ($TrainingDetail) {
return response()->json('status_success');
} else {
return response()->json('status_failed');
}
}
#### Delete Training Detail
public function deleteTrainingDetail(Request $request)
{
$this->UserSessionId = session('UserId');
if (empty($this->UserSessionId)) {
return redirect('/');
}
$data = array();
$TrainingId = $request->input('TrainingId');
$data['IsDeleted'] = 1;
$data['Status'] = 0;
$data['Updated_by'] = session('UserId');
$data['Updated_at'] = date('Y-m-d H:i:s', time());
$deleteData = EmployeeTrainingDetail::where('TrainingDetailId', $TrainingId)->update($data);
if ($deleteData) {
return response()->json('status_success');
} else {
return response()->json('status_failed');
}
}
#### Delete Permanent Training Detail
public function destroyTrainingDetail(Request $request)
{
$this->UserSessionId = session('UserId');
if (empty($this->UserSessionId)) {
return redirect('/');
}
$TrainingId = $request->input('TrainingId');
$destroyData = EmployeeTrainingDetail::where('TrainingDetailId', $TrainingId);
$destroyData->delete();
if ($destroyData) {
return response()->json('status_success');
} else {
return response()->json('status_failed');
}
}
#### Submit For Approval
public function SubmitTrainingDetailApproval(Request $request, $ProfileId = '')
{
$this->UserSessionId = session('UserId');
if (empty($this->UserSessionId)) {
return redirect('/');
}
$data = array();
$EmployeeId = $request->input('EmployeeId');
$ProfileId = $request->input('ProfileId');
$UserId = $request->input('UserId');
$ApprovalRemark = $request->input('ApprovalRemark');
// In case of Farward for review
$ModuleId = $request->input('ModuleId');
$data['ApprovalStatus'] = 0;
$data['Updated_by'] = session('UserId');
$data['Updated_at'] = date('Y-m-d H:i:s', time());
if ($ModuleId) {
$GetNoOfApproval = EmployeeTrainingDetail::where('EmployeeId', $EmployeeId)
->where('ApprovalLock', '1')
->where('ApprovalStatus', '0')
->where('IsDeleted', '0')
->get();
foreach ($GetNoOfApproval as $key => $ApprovalCount) {
$NoOfApproval = $ApprovalCount->NoOfApproval + 1;
// Update Employee EmployeeTrainingDetail Table in case of Farward for review
$data['NoOfApproval'] = $NoOfApproval;
$ApprovalRequest = EmployeeTrainingDetail::where('EmployeeId', $EmployeeId)
->where('ApprovalLock', '1')
->where('ApprovalStatus', '0')
->where('IsDeleted', '0')
->update($data);
}
// Update Approval Table in case of Farward for review
$update_approval['ApprovalStatus'] = '3'; // Approve = 1, Reject = 2, Forward = 3.
$update_approval['ApprovalLevel'] = $NoOfApproval;
$update_approval['Updated_by'] = session('UserId');
$update_approval['Updated_at'] = date('Y-m-d H:i:s', time());
$Update_Approval_Module = Approval::where('ModuleId', $ModuleId)
->where('ProfileId', session('ProfileId'))
->where('UserId', session('UserId'))
->where('ApprovalStatus', '=', '0')
->update($update_approval);
if ($Update_Approval_Module) {
$approval = array();
$approval['ModuleId'] = $ModuleId;
$approval['EmployeeId'] = $EmployeeId;
$approval['ModuleTitle'] = 'TrainingDetail';
$approval['ApprovalStatus'] = '0';
$approval['ApprovalComments'] = $ApprovalRemark;
$approval['ApprovalFrom'] = session('UserId');
$approval['ProfileId'] = $ProfileId;
$approval['UserId'] = $UserId;
//$approval['ApprovalLevel'] = '';
$approval['Created_at'] = date('Y-m-d H:i:s', time());
$approval['Created_by'] = session('UserId');
$approval['Updated_by'] = session('UserId');
$approval['Updated_at'] = date('Y-m-d H:i:s', time());
$Approval_Module_New = Approval::insert($approval);
if ($Approval_Module_New) {
return response()->json('status_success');
} else {
return response()->json('status_fail');
}
}
} else {
$data['ApprovalLock'] = 1;
$data['NoOfApproval'] = 0;
$data['ApprovalComment'] = '';
$ApprovalRequest = EmployeeTrainingDetail::where('EmployeeId', $EmployeeId)
->where('ApprovalStatus', '=', '0')
->where('ApprovalLock', '0')
->where('IsDeleted', '0')
->update($data);
if ($ApprovalRequest) {
$approval = array();
$approval['ModuleId'] = $this->userLib->useridGenrator();
$approval['EmployeeId'] = $EmployeeId;
$approval['ModuleTitle'] = 'TrainingDetail';
$approval['ApprovalStatus'] = '0';
$approval['ApprovalComments'] = $ApprovalRemark;
$approval['ApprovalFrom'] = session('UserId');
$approval['ProfileId'] = $ProfileId;
$approval['UserId'] = $UserId;
//$approval['ApprovalLevel'] = '';
$approval['Created_at'] = date('Y-m-d H:i:s', time());
$approval['Created_by'] = session('UserId');
$approval['Updated_at'] = date('Y-m-d H:i:s', time());
$approval['Updated_by'] = session('UserId');
$Approval_Module = Approval::insert($approval);
if ($Approval_Module) {
return response()->json('status_success');
} else {
return response()->json('status_fail');
}
}
}
}
#### Approve Employee Training Detail
public function ApproveTrainingDetail(Request $request, $EmployeeId = '', $ModuleId = '')
{
$this->UserSessionId = session('UserId');
if (empty($this->UserSessionId)) {
return redirect('/');
}
$LoginUserProfileId = session('ProfileId');
$UserId = session('UserId');
$EmployeeId = $request->input('EmployeeId');
$ModuleId = $request->input('ModuleId');
$ApprovalStatusTbl = $request->input('ApprovalStatus');
$GetNoOfApproval = EmployeeTrainingDetail::select('NoOfApproval')
->where('EmployeeId', $EmployeeId)
->where('ApprovalStatus', '0')
->where('ApprovalLock', '1')
//->where('NoOfApproval', '!=', '0')
->where('IsDeleted', '0')
->orderBy('id', 'DESC')
->get()
->first();
$NoOfApproval = $GetNoOfApproval->NoOfApproval + 1;
$approval = array();
$approval['ApprovalStatus'] = $ApprovalStatusTbl;
$approval['ApprovalLevel'] = $NoOfApproval;
$approval['Updated_by'] = session('UserId');
$approval['Updated_at'] = date('Y-m-d H:i:s', time());
$ApprovalUpdate = Approval::where('ModuleId', $ModuleId)->where('EmployeeId', $EmployeeId)->where('ProfileId', $LoginUserProfileId)->where('UserId', $UserId)->update($approval);
if ($ApprovalUpdate) {
// Update EmployeeTrainingDetail Table
$data = array();
$data['ApprovalStatus'] = 1;
$data['NoOfApproval'] = $NoOfApproval;
$data['Updated_by'] = session('UserId');
$data['Updated_at'] = date('Y-m-d H:i:s', time());
$approveData = EmployeeTrainingDetail::where('EmployeeId', $EmployeeId)
->where('ApprovalLock', '1')
->where('ApprovalStatus', '0')
->where('IsDeleted', '0')
->update($data);
return response()->json('status_success');
} else {
return response()->json('status_failed');
}
}
#### Reject Employee Training Detail
public function RejectTrainingDetail(Request $request)
{
$this->UserSessionId = session('UserId');
if (empty($this->UserSessionId)) {
return redirect('/');
}
$data = array();
$EmployeeId = $request->input('EmployeeId');
$Comment = $request->input('RejectComment');
$LoginUserProfileId = session('ProfileId');
$UserId = session('UserId');
$ModuleId = $request->input('ModuleId');
$ApprovalStatusTbl = $request->input('ApprovalStatus');
$GetNoOfApproval = EmployeeTrainingDetail::select('NoOfApproval')
->where('EmployeeId', $EmployeeId)
->where('ApprovalStatus', '0')
->where('ApprovalLock', '1')
//->where('NoOfApproval', '!=', '0')
->where('IsDeleted', '0')
->orderBy('id', 'DESC')
->get()
->first();
$NoOfApproval = $GetNoOfApproval->NoOfApproval + 1;
// Rejected = 2,
$data['ApprovalStatus'] = $ApprovalStatusTbl;
$data['ApprovalLock'] = 0;
$data['NoOfApproval'] = $NoOfApproval;
$data['ApprovalComment'] = $Comment;
$data['Updated_by'] = session('UserId');
$data['Updated_at'] = date('Y-m-d H:i:s', time());
$approval = array();
$approval['ApprovalStatus'] = $ApprovalStatusTbl;
$approval['ApprovalLevel'] = $NoOfApproval;
$approval['ApprovalComments'] = $Comment;
$approval['Updated_by'] = session('UserId');
$approval['Updated_at'] = date('Y-m-d H:i:s', time());
$ApprovalUpdate = Approval::where('ModuleId', $ModuleId)->where('EmployeeId', $EmployeeId)->where('ProfileId', $LoginUserProfileId)->where('UserId', $UserId)->update($approval);
if ($ApprovalUpdate) {
$rejectData = EmployeeTrainingDetail::where('EmployeeId', $EmployeeId)
->where('ApprovalLock', '1')
->where('ApprovalStatus', '0')
->where('IsDeleted', '0')->update($data);
if ($rejectData) {
return response()->json('status_success');
} else {
return response()->json('status_failed');
}
} else {
return response()->json('status_failed');
}
}
}