EVOLUTION-NINJA
Edit File: Employee_detaills.php
<?php namespace App\Controllers; use App\Models\EmployeeModel; use CodeIgniter\API\ResponseTrait; class Employee_detaills extends BaseController { use ResponseTrait; protected $db; public function __construct() { $this->db = \Config\Database::connect(); } public function employee_application_form() { $id=session()->get('id'); if($id){ return view('employee/employee_application_form'); }else{ return redirect()->to('/'); } } public function add_employee_application_form(){ $id=session()->get('id'); if($id){ $db=\Config\Database::connect(); $data=$this->request->getVar(); $data['created_at']=date('Y-m-d H:i:s'); $adno_model = $db->table('student_list'); $adno_model->select('admission_no'); $adno_model->like('admission_no', 'GTRAC'); $adno_data = $adno_model->get()->getResultArray(); $last = end($adno_data); if ($last) { $last_admission_no = $last['admission_no']; $ad1 = intval(substr($last_admission_no, 5)) + 1; $ad_no = 'GTRAC' . str_pad($ad1, 6, '0', STR_PAD_LEFT); } else { $ad_no = 'GTRAC000001'; } $image = $this->request->getFile('profile-image'); if ($image->isValid() && !$image->hasMoved()) { $newName = uniqid() . '.' . $image->getClientExtension(); $image->move('public/assets/studentimages', $newName); } else { return $this->response->setJSON(['result' => 0, 'message' => 'Failed to upload profile picture']); exit(); } $data['profile_image']=$newName; $data['admission_no']=$ad_no; $data['created_by']=session()->get('username'); $data['delete_status']=1; $builder=$db->table('student_list'); $insert=$builder->insert($data); if($insert){ return $this->response->setJSON(['result'=>1,'message'=>'Application submitted successfully']); }else{ return $this->response->setJSON(['result'=>0,'message'=>'Failed to submit']); } }else{ return $this->response->setJSON(['result'=>0,'message'=>'please login']); } } public function student_list_data() { $id=session()->get('id'); if($id){ $db=\Config\Database::connect(); $builder=$db->table('student_list'); $builder->select('student_list.*,header.header_name as header,header_value.value_name as value'); $builder->join('header','header.id=student_list.header_name'); $builder->join('header_value','header_value.id=student_list.value_name'); $builder->where('student_list.delete_status','1'); $data=$builder->get()->getResultArray(); // echo 'hello'; // print_r($data);die(); if($data){ return $this->response->setJSON($data); }else{ return $this->response->setJSON(['result'=>0,'message'=>'Failed to load data']); } }else{ // echo 'hello';die(); return $this->response->setJSON(['result'=>0,'message'=>'please login']); } } public function student_delete() { $id=$this->request->getVar('id'); // echo $id;die(); $db=\Config\Database::connect(); $builder=$db->table('student_list'); $data=[ 'delete_status'=>0 ]; $builder->where('id',$id); $delete=$builder->update($data); if($delete){ return $this->response->setJSON(['result'=>1]); }else{ return $this->response->setJSON(['result'=>0]); } } public function student_edit() { $id=session()->get('id'); if($id){ $sid=$this->request->getVar('id'); // echo $sid;die(); $db=\Config\Database::connect(); $builder=$db->table('student_list'); $builder->where('id',$sid); $data=$builder->get()->getRowArray(); // print_r($data);die(); // $data['value']=$data; return view('studentapplication/student_edit',$data); }else{ return $this->response->setJSON(['result'=>0,'message'=>'please login']); } } public function application_form_update(){ $id=session()->get('id'); if($id){ $db=\Config\Database::connect(); $data=$this->request->getVar(); $sid=$data['id']; $data['created_at']=date('Y-m-d H:i:s'); $adno_model = $db->table('student_list'); $adno_model->select('admission_no'); $adno_model->like('admission_no', 'GTRAC'); $adno_data = $adno_model->get()->getResultArray(); $last = end($adno_data); if ($last) { $last_admission_no = $last['admission_no']; $ad1 = intval(substr($last_admission_no, 5)) + 1; $ad_no = 'GTRAC' . str_pad($ad1, 6, '0', STR_PAD_LEFT); } else { $ad_no = 'GTRAC000001'; } $image = $this->request->getFile('profile-image'); // $existingImagePath = $this->request->getVar('profile-image'); if ($image->isValid() && !$image->hasMoved()) { $newName = uniqid() . '.' . $image->getClientExtension(); $image->move('public/assets/studentimages', $newName); $data['profile_image']=$newName; } else { $existingImagePath=''; $imagename=$db->table('student_list'); $imagename->where('id',$sid); $imagerow=$imagename->get()->getRowArray(); $imagerow['profile_image'] = $existingImagePath; } $data['admission_no']=$ad_no; $data['created_by']=session()->get('username'); $data['delete_status']=1; $builder=$db->table('student_list'); $builder->where('id',$sid); $insert=$builder->update($data); if($insert){ return $this->response->setJSON(['result'=>1,'message'=>'Application submitted successfully']); }else{ return $this->response->setJSON(['result'=>0,'message'=>'Failed to submit']); } }else{ return $this->response->setJSON(['result'=>0,'message'=>'please login']); } } public function student_details() { $id=session()->get('id'); if($id){ $sid=$this->request->getVar('id'); // echo $sid;die(); $db=\Config\Database::connect(); $builder=$db->table('student_list'); $builder->select('student_list.*,header.header_name as header,header_value.value_name as value'); $builder->join('header','header.id=student_list.header_name'); $builder->join('header_value','header_value.id=student_list.value_name'); $builder->where('student_list.id',$sid); $data=$builder->get()->getRowArray(); // print_r($data);die(); // $data['value']=$data; if($data){ return $this->response->setJSON(['result'=>1,'data'=>$data]); }else{ return $this->response->setJSON(['result'=>0,'message'=>'Failed to fetch']); } }else{ return $this->response->setJSON(['result'=>0,'message'=>'please login']); } } // ----------------------------------------Add Employee--------------------------------------------------------------------// public function add_employee() { $payload=$this->request->getPost(); // print_r($payload);die(); if ($this->request->isAJAX()) { // Handle file upload $file = $this->request->getFile('profile_img'); $profileImgPath = ''; if ($file && $file->isValid() && !$file->hasMoved()) { $newName = $file->getRandomName(); $file->move('public/assets/EMP_profile_img', $newName); $profileImgPath = $newName; } $employeeModel = new EmployeeModel(); $latestEmployee = $employeeModel->orderBy('employee_id', 'DESC')->first(); $newEmployeeId = $latestEmployee ? 'EMP' . str_pad((int)substr($latestEmployee['employee_id'], 3) + 1, 3, '0', STR_PAD_LEFT) : 'EMP001'; $data = [ 'employee_id' => $newEmployeeId, 'first_name' => $this->request->getPost('first-name'), 'last_name' => $this->request->getPost('last-name'), 'email' => $this->request->getPost('email-id'), 'department' => $this->request->getPost('department'), 'title' => $this->request->getPost('title'), 'source_of_hire' => $this->request->getPost('source-of-hire'), 'date_of_joining' => $this->request->getPost('dateofjoining'), 'employee_status' => $this->request->getPost('employee-status'), 'work_phone' => $this->request->getPost('workphone'), 'employee_type' => $this->request->getPost('employee-type'), 'role' => $this->request->getPost('role'), 'pan_number' => $this->request->getPost('pan-number'), 'date_of_birth' => $this->request->getPost('dateofbirth'), 'marital_status' => $this->request->getPost('marital-status'), 'emp_address' => $this->request->getPost('emp-address'), 'residential_address' => $this->request->getPost('residential-address'), 'job_description' => $this->request->getPost('job-description'), 'gender' => $this->request->getPost('gender'), 'about_me' => $this->request->getPost('about-me'), 'profile_img' => $profileImgPath ]; $employeeId = $employeeModel->insert($data); $company_names = $this->request->getPost('company_name[]'); $job_titles = $this->request->getPost('job_title[]'); $from_dates = $this->request->getPost('from_date[]'); $to_dates = $this->request->getPost('to_date[]'); $job_descriptions = $this->request->getPost('job_description[]'); if(!empty($company_names)){ $work_data = []; foreach ($company_names as $key => $company_name) { $work_data[] = [ 'employee_id' => $employeeId, 'company' => $company_name, 'job_title' => $job_titles[$key], 'from_date' => $from_dates[$key], 'to_date' => $to_dates[$key], 'description' => $job_descriptions[$key], ]; } $db = \Config\Database::connect(); $builder = $db->table('employee_work_experience'); foreach ($work_data as $work) { $builder->insert($work); } echo 'Work experience inserted successfully'; } // // ------------------------------edu------------------------------------------ $college_names = $this->request->getPost('collegeName[]'); $diploma_degrees = $this->request->getPost('degree[]'); $fields_of_study = $this->request->getPost('fieldOfStudy[]'); $dates_of_completion = $this->request->getPost('completionDate[]'); $additional_notes = $this->request->getPost('additionalNotes[]'); $interests = $this->request->getPost('interests[]'); $aggregates = $this->request->getPost('aggregate[]'); // $uploads = $this->request->getPost('upload[]'); if(!empty($college_names)){ $education_data = []; foreach ($college_names as $key => $college_name) { $education_data[] = [ 'employee_id' => $employeeId, 'college_name' => $college_name, 'diploma_degree' => $diploma_degrees[$key], 'field_of_study' => $fields_of_study[$key], 'completion_date' => $dates_of_completion[$key], 'additional_notes' => $additional_notes[$key], 'interests' => $interests[$key], 'aggregate' => $aggregates[$key], // 'upload' => isset($uploads[$key]) ? $uploads[$key] : null, ]; } $db = \Config\Database::connect(); $builder = $db->table('employee_education'); // Insert each set of education data foreach ($education_data as $education) { $builder->insert($education); } echo 'Education experience inserted successfully'; } // ----------------------dependent---------------- $names = $this->request->getPost('name[]'); $relationship = $this->request->getPost('relationship[]'); $dob = $this->request->getPost('dob[]'); if(!empty($names)){ $dependent_data = []; foreach ($names as $key => $name) { $dependent_data[] = [ 'employee_id' => $employeeId, 'name' => $name, 'relationship' => $relationship[$key], 'dob' => $dob[$key], ]; } $db = \Config\Database::connect(); $builder = $db->table('employee_dependents'); foreach ($dependent_data as $dependent) { $builder->insert($dependent); } echo 'Dep experience inserted successfully'; } } else { $response['success'] = false; $response['message'] = 'Failed to add employee'; } echo 'insertred';die(); } // ------------------generate emp_id-------------------- public function get_latest_employee_id() { $employeeModel = new EmployeeModel(); $latestEmployee = $employeeModel->orderBy('employee_id', 'DESC')->first(); $newEmployeeId = $latestEmployee ? 'EMP' . str_pad((int)substr($latestEmployee['employee_id'], 3) + 1, 3, '0', STR_PAD_LEFT) : 'EMP001'; return $this->response->setJSON(['employee_id' => $newEmployeeId]); } // --------------------emp list----------------------------------- public function employee_list(){ return view('employee/employee_list'); } // ----------------------------fetch emp-list-------------------------- public function fetch_emp_list() { $id = $this->request->getPost('id'); $employeeModel = new EmployeeModel(); $employees = $employeeModel->getEmployeeList(); $gridData = []; foreach ($employees as $employee) { $gridData[] = [ 'employee_id' => $employee['employee_id'], 'first_name' => $employee['first_name'], 'last_name' => $employee['last_name'], 'department' => $employee['department'], 'title' => $employee['title'], 'work_phone' => $employee['work_phone'], 'email' => $employee['email'], 'date_of_joining' => $employee['date_of_joining'], 'emp_id' =>$employee['id'] ]; } return $this->response->setJSON(['rows' => $gridData]); } // ----------------------edit----------------------------------- // public function editEmpList() // { // $employee_id = $this->request->getVar('employee_id'); // return view('employee/edit_emp_list', ['employee_id' => $employee_id]); // } public function editEmpList() { $model = new EmployeeModel(); $employee_id = $this->request->getVar('employee_id'); $model->where('employee_id', $employee_id); $id = $model->get()->getRowArray(); $id = $id['id']; return view('employee/edit_emp_list', ['id' => $id]); } // ---------------------------------get details------------------------------------------------- public function getEmployeeDetails() { $model = new EmployeeModel(); $employeeId = $this->request->getVar('employee_id'); $model->where('id',$employeeId); $employee = $model->get()->getRowArray(); // print_r($employee['profile_img']);die(); if ($employee) { $profileImgPath = $employee['profile_img'] ? base_url('public/assets/EMP_profile_img/' . $employee['profile_img']) : ''; $workExperience = $model->getWorkData($employee['id']); $education = $model->getEducation($employee['id']); $dependents = $model->getDependents($employee['id']); $responseData = [ 'employee_id' => $employee['employee_id'], 'first_name' => $employee['first_name'], 'last_name' => $employee['last_name'], 'email' => $employee['email'], 'department' => $employee['department'], 'title' => $employee['title'], 'source_of_hire' => $employee['source_of_hire'], 'date_of_joining' => $employee['date_of_joining'], 'employee_status' => $employee['employee_status'], 'work_phone' => $employee['work_phone'], 'employee_type' => $employee['employee_type'], 'role' => $employee['role'], 'pan_number' => $employee['pan_number'], 'date_of_birth' => $employee['date_of_birth'], 'marital_status' => $employee['marital_status'], 'emp_address' => $employee['emp_address'], 'residential_address' => $employee['residential_address'], 'job_description' => $employee['job_description'], 'gender' => $employee['gender'], 'about_me' => $employee['about_me'], 'profile_img' => $profileImgPath, 'work_experience' => $workExperience, 'education' => $education, 'dependents' => $dependents ]; return $this->response->setJSON($responseData); } else { return $this->response->setStatusCode(404)->setJSON(['error' => 'Employee not found']); } } // -------------------update------------------------------------------ // public function updateEmployee() // { // if ($this->request->isAJAX()) { // $employee_id = $this->request->getVar('employee_id'); // // print_r($employee_id);die(); // $employeeModel = new EmployeeModel(); // $work_experience = json_decode($this->request->getVar('work_experience'), true); // $education_experience = json_decode($this->request->getVar('education_data'), true); // $dependents = json_decode($this->request->getVar('dependents_data'), true); // $data = [ // 'employee_id'=>$employee_id, // 'first_name' => $this->request->getPost('first_name'), // 'last_name' => $this->request->getPost('last_name'), // 'email' => $this->request->getPost('email'), // 'department' => $this->request->getPost('department'), // 'title' => $this->request->getPost('title'), // 'source_of_hire' => $this->request->getPost('source_of_hire'), // 'date_of_joining' => $this->request->getPost('date_of_joining'), // 'employee_status' => $this->request->getPost('employee_status'), // 'work_phone' => $this->request->getPost('work_phone'), // 'employee_type' => $this->request->getPost('employee_type'), // 'role' => $this->request->getPost('role'), // 'pan_number' => $this->request->getPost('pan_number'), // 'date_of_birth' => $this->request->getPost('date_of_birth'), // 'marital_status' => $this->request->getPost('marital_status'), // 'emp_address' => $this->request->getPost('emp_address'), // 'residential_address' => $this->request->getPost('residential_address'), // 'job_description' => $this->request->getPost('job_description'), // 'gender' => $this->request->getPost('gender'), // 'about_me' => $this->request->getPost('about_me') // ]; // $file = $this->request->getFile('profile_img'); // if ($file && $file->isValid() && !$file->hasMoved()) { // $newName = $file->getRandomName(); // $file->move('public/assets/EMP_profile_img', $newName); // $data['profile_img'] = $newName; // } // $employeeModel->updateEmployee($employee_id, $data); // // print_r($employee_id);die(); // if (!empty($work_experience)) { // foreach ($work_experience as $exp) { // $employeeModel->updatework([ // 'id'=>$exp['id'], // // 'employee_id' => $employee_id, // 'company' => $exp['company'], // 'job_title' => $exp['job_title'], // 'from_date' => $exp['from_date'], // 'to_date' => $exp['to_date'], // 'description' => $exp['description'] // ]); // } // } // // print_r('hi');die(); // if (!empty($education_experience)) { // foreach ($education_experience as $education) { // $employeeModel->updateEducation([ // 'id'=>$education['id'], // // 'employee_id' => $employee_id, // 'college_name' => $education['collegeName'], // 'diploma_degree' => $education['degree'], // 'field_of_study' => $education['fieldOfStudy'], // 'completion_date' => $education['completionDate'], // 'additional_notes' => $education['additionalNotes'], // 'interests' => $education['interests'], // 'aggregate' => $education['aggregate'], // // 'upload'=>$upload['upload'] // ]); // } // } // // print_r($dependents);die(); // if (!empty($dependents)) { // foreach ($dependents as $dependent) { // $employeeModel->updateDependent([ // 'id'=>$dependent['id'], // // 'employee_id' => $employee_id, // 'name' => $dependent['name'], // 'relationship' => $dependent['relationship'], // 'dob' => $dependent['dob'] // ]); // } // } // return $this->response->setJSON(['status' => 'success', 'message' => 'Employee data updated successfully']); // } else { // return redirect()->back()->with('error', 'Invalid request'); // } // } public function updateEmployee() { if ($this->request->isAJAX()) { $employeeModel = new EmployeeModel(); $employee_id = $this->request->getVar('employee_id'); // $employeeModel->where('employee_id',$employee_id); // $id =$employeeModel->get()->getRowArray(); // $id=$id['id']; // print_r($id);die(); $work_experience = json_decode($this->request->getVar('work_experience'), true); $education_experience = json_decode($this->request->getVar('education_data'), true); $dependents = json_decode($this->request->getVar('dependents_data'), true); $data = [ 'first_name' => $this->request->getPost('first_name'), 'last_name' => $this->request->getPost('last_name'), 'email' => $this->request->getPost('email'), 'department' => $this->request->getPost('department'), 'title' => $this->request->getPost('title'), 'source_of_hire' => $this->request->getPost('source_of_hire'), 'date_of_joining' => $this->request->getPost('date_of_joining'), 'employee_status' => $this->request->getPost('employee_status'), 'work_phone' => $this->request->getPost('work_phone'), 'employee_type' => $this->request->getPost('employee_type'), 'role' => $this->request->getPost('role'), 'pan_number' => $this->request->getPost('pan_number'), 'date_of_birth' => $this->request->getPost('date_of_birth'), 'marital_status' => $this->request->getPost('marital_status'), 'emp_address' => $this->request->getPost('emp_address'), 'residential_address' => $this->request->getPost('residential_address'), 'job_description' => $this->request->getPost('job_description'), 'gender' => $this->request->getPost('gender'), 'about_me' => $this->request->getPost('about_me') ]; $file = $this->request->getFile('profile_img'); if ($file && $file->isValid() && !$file->hasMoved()) { $newName = $file->getRandomName(); $file->move('public/assets/EMP_profile_img', $newName); $data['profile_img'] = $newName; } $employeeModel->updateEmployee($employee_id, $data); // print_r($employee_id);die(); if (!empty($work_experience)) { foreach ($work_experience as $exp) { // Check if work experience exists if (isset($exp['id']) && !empty($exp['id'])) { $existingWork = $employeeModel->getWorkById($exp['id']); if ($existingWork) { // Update existing work experience $employeeModel->updateWork([ 'id' => $exp['id'], 'company' => $exp['company'], 'job_title' => $exp['job_title'], 'from_date' => $exp['from_date'], 'to_date' => $exp['to_date'], 'description' => $exp['description'] ]); } } else { // Insert new work experience without existing ID $employeeModel->insertWork([ // 'id' => $exp['id'], 'employee_id' => $employee_id, 'company' => $exp['company'], 'job_title' => $exp['job_title'], 'from_date' => $exp['from_date'], 'to_date' => $exp['to_date'], 'description' => $exp['description'] ]); } } } foreach ($education_experience as $education) { if (isset($education['id']) && !empty($education['id'])) { $existingEducation = $employeeModel->getEducationById($education['id']); if ($existingEducation) { // Update existing education record $employeeModel->updateEducation([ 'id' => $education['id'], // 'employee_id' => $employee_id, // If employee_id is already in the $data array, no need to include it here again // 'college_name' => $education['collegeName'], 'diploma_degree' => $education['degree'], 'field_of_study' => $education['fieldOfStudy'], 'completion_date' => $education['completionDate'], 'additional_notes' => $education['additionalNotes'], 'interests' => $education['interests'], 'aggregate' => $education['aggregate'], ]); } } else { // Insert new education record without existing ID $employeeModel->insertEducation([ // 'id' => $education['id'], 'employee_id' => $employee_id, // 'college_name' => $education['collegeName'], 'diploma_degree' => $education['degree'], 'field_of_study' => $education['fieldOfStudy'], 'completion_date' => $education['completionDate'], 'additional_notes' => $education['additionalNotes'], 'interests' => $education['interests'], 'aggregate' => $education['aggregate'], ]); } } // Updating dependents data foreach ($dependents as $dependent) { if (isset($dependent['id']) && !empty($dependent['id'])) { $existingDependent = $employeeModel->getDependentById($dependent['id']); if ($existingDependent) { // Update existing dependent record $employeeModel->updateDependent([ 'id' => $dependent['id'], // 'employee_id' => $employee_id, // If employee_id is already in the $data array, no need to include it here again 'name' => $dependent['name'], 'relationship' => $dependent['relationship'], 'dob' => $dependent['dob'] ]); } } else { // Insert new dependent record without existing ID $employeeModel->insertDependent([ 'employee_id' => $employee_id, 'name' => $dependent['name'], 'relationship' => $dependent['relationship'], 'dob' => $dependent['dob'] ]); } } return $this->response->setJSON(['status' => 'success', 'message' => 'Employee data updated successfully']); } else { return redirect()->back()->with('error', 'Invalid request'); } } // --------------------delete------------------------------ public function deleteEmployee() { $employeeId = $this->request->getPost('employee_id'); // var_dump($employeeId);die(); $employeeModel = new EmployeeModel(); $deleteStatus = $employeeModel->where('employee_id', $employeeId)->delete(); if ($deleteStatus) { return $this->respond(['status' => 'success', 'message' => 'Employee deleted successfully']); } else { return $this->fail(['status' => 'error', 'message' => 'Failed to delete employee'], 400); } } // --------------delete-row------------- // public function delete_row() // { // $id = $this->request->getPost('id'); // // $db = \Config\Database::connect(); // // $db->table('employee_work_experience')->where('id', $id)->delete(); // // $db->table('employee_education')->where('id', $id)->delete(); // // $db->table('employee_dependents')->where('id', $id)->delete(); // $id = $this->request->getPost('id'); // $db = \Config\Database::connect(); // $db->table('employee_work_experience')->where('id', $id)->delete(); // // print_r($id);die(); // $db->table('employee_education')->where('id', $id)->delete(); // // print_r($id);die(); // $db->table('employee_dependents')->where('id', $id)->delete(); // return $this->response->setJSON(['success' => true]); // } public function delete_row() { $id = $this->request->getPost('id'); // Check if the row exists before attempting deletion $db = \Config\Database::connect(); $workExperienceExists = $db->table('employee_work_experience')->where('id', $id)->get()->getRow(); $educationExists = $db->table('employee_education')->where('id', $id)->get()->getRow(); $dependentsExists = $db->table('employee_dependents')->where('id', $id)->get()->getRow(); if ($workExperienceExists || $educationExists || $dependentsExists) { // If any of the records exist, proceed with deletion if ($workExperienceExists) { $db->table('employee_work_experience')->where('id', $id)->delete(); } if ($educationExists) { $db->table('employee_education')->where('id', $id)->delete(); } if ($dependentsExists) { $db->table('employee_dependents')->where('id', $id)->delete(); } return $this->response->setJSON(['success' => true]); } else { // If none of the records exist, return an error return $this->response->setJSON(['success' => false, 'message' => 'Record not found']); } } } ?>