nhance/app/Models/EmployeeModel.php

214 lines
10 KiB
PHP
Executable File

<?php
namespace App\Models;
use CodeIgniter\Model;
class EmployeeModel extends Model
{
protected $table = 'employees';
protected $primaryKey = 'id';
protected $allowedFields = [
"id",
"client_id",
"employee_id",
"change_event",
"relationship",
"relationship_code",
"relationship_id",
"relationship",
"batch_id",
"emp_code",
"name",
"email_personal",
"email_corporate",
"mobile",
"gender",
"dob",
"doj",
"basic_pay",
"band","designation",
"otp",
"family_floater_key",
"emp_status",
"created_by",
"updated_by",
"updated_at",
"is_active",
"file_id",
"is_addon_value",
"is_createdby_hr",
"client_branch_id",
"token_time_out",
"emp_type",
"unit",
"mpin"
];
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = ["checkAndADDCreatedByValue"];
protected $afterInsert = [];
protected $beforeUpdate = ["checkAndUpdateUpdatedByValue"];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
protected function checkAndADDCreatedByValue(array $data)
{
// Check if 'updated_by' value is null or empty
if (empty($data['data']['created_by'])) {
// Set 'updated_by' value to the current session user ID
$data['data']['created_by'] = get_session_userid();
}
return $data;
}
protected function checkAndUpdateUpdatedByValue(array $data)
{
// Check if 'updated_by' value is null or empty
if (empty($data['data']['updated_by'])) {
// Set 'updated_by' value to the current session user ID
$data['data']['updated_by'] = get_session_userid();
}
return $data;
}
// for emp onboard process do not change
public function checkExistingEmp($arr,$client_branch_id)
{
if($arr['temp']['emp_id'] == null)
{
return $this->where('emp_code',$arr['emp_code'])
->where('name',$arr['name'])
->where('client_id',$arr['client_id'])
->where('client_branch_id',$client_branch_id)
->where('is_active',1)
->where('gender',$arr['gender'])
->where('dob',$arr['dob'])
->where('is_active',1)
->where('emp_status','active')
->find();
}
else
{
return $this->where('id',$arr['temp']['emp_id'])->find();
}
}
public function getEmpFamilybyEmpCode(string $client_policy_id = null,string $emp_code = null,string $client_id = null,array $emp_status = [],array $policy_status = [],array $relationship = [],array $client_branch_id = [])
{
$result = $this->select(['employees.id as emp_id', 'employees.client_id','employees.client_branch_id', 'employees.relationship', 'employees.relationship_code', 'employees.change_event', 'employees.emp_code', 'employees.name', 'employees.email_personal', 'employees.email_corporate', 'employees.mobile', 'employees.gender', 'employees.dob', 'employees.doj', 'employees.basic_pay', 'employees.band', 'employees.designation', 'employees.emp_status','employees.unit','employee_polices.id as emp_policy_id', 'employee_polices.employee_id', 'employee_polices.client_policy_id', 'employee_polices.tpa_id', 'employee_polices.uhid', 'employee_polices.batch_code', 'employee_polices.status', 'employee_polices.pre_existing_alignments', 'employee_polices.basic_cover_si', 'employee_polices.date_coverage', 'employee_polices.policy_end_date', 'employee_polices.days', 'employee_polices.premium', 'employee_polices.rata_premimum', 'employee_polices.gst', 'employee_polices.date_of_exit', 'employee_polices.reason_for_exit','policy_type.long_name as policy_name','client_policy.is_addon'])
->join('employee_polices', 'employee_polices.employee_id = employees.id')
->join('client_policy', 'client_policy.id = employee_polices.client_policy_id')
->join('policy_type', 'policy_type.id = client_policy.policy_type_id','left')
->where('employees.is_active',1)
->when($client_policy_id, function($query) use ($client_policy_id){
return $query->where('employee_polices.client_policy_id',$client_policy_id);
})
->where('employee_polices.is_active',1)
->where('employees.is_active',1)
// ->where('employees.emp_code',$emp_code)
->when($emp_code, function($query) use ($emp_code){
return $query->where('employees.emp_code',$emp_code);
})
->when(count($emp_status), function($query) use ($emp_status){
return $query->whereIn('employees.emp_status', $emp_status);
})
->when(count($client_branch_id), function($query) use ($client_branch_id){
return $query->whereIn('employees.client_branch_id', $client_branch_id);
})
->when(count($relationship), function($query) use ($relationship){
return $query->whereIn('employees.relationship', $relationship);
})
->when(count($policy_status), function($query) use ($policy_status){
return $query->whereIn('employee_polices.status', $policy_status);
})
->when($client_id, function($query) use ($client_id){
return $query->where('employees.client_id',$client_id);
})
->findAll();
// dd($this->db->getLastQuery());
return ($result);
}
public function getEmployeePolicy($id)
{
return $this->db->table('employee_polices')
->select(' policy_type.long_name as Policy_Name , client_policy.policy_terms as Policy_Terms, client_policy.client_id as ClientId, client_policy.policy_id as PolicyId,client_policy.id as ClientPolicyId, client_policy.open_for_enrollment as OpenForEnrollment , employee_polices.tpa_id as tpa_id , employee_polices.rand_string as rand_string') // Select all columns from both tables
->join('client_policy', 'client_policy.id = employee_polices.client_policy_id')
->join('policy_type', 'policy_type.id = client_policy.policy_type_id')
->where('client_policy.policy_status', 1)
->where('employee_polices.employee_id', $id)
->get()
->getResult();
}
// for EMP rest API process do not change
public function checkExistingEmployee($arr,$client_branch_id)
{
return $this->where('emp_code',$arr['emp_code'])->where('name',$arr['name'])->where('client_id', $arr['client_id'])->where('client_branch_id', $client_branch_id)->first();
}
public function getEmpListByFileId(int $file_id,array $emp_status = [],array $policy_status = [])
{
$result = $this->select(['employees.id as emp_id', 'employees.client_id', 'employees.relationship', 'employees.relationship_code', 'employees.change_event', 'employees.emp_code', 'employees.name', 'employees.email_personal', 'employees.email_corporate', 'employees.mobile', 'employees.gender', 'employees.dob', 'employees.doj', 'employees.basic_pay', 'employees.band', 'employees.designation', 'employees.emp_status','employee_polices.id as emp_policy_id', 'employee_polices.employee_id', 'employee_polices.client_policy_id', 'employee_polices.tpa_id', 'employee_polices.uhid', 'employee_polices.batch_code', 'employee_polices.status', 'employee_polices.pre_existing_alignments', 'employee_polices.basic_cover_si', 'employee_polices.date_coverage', 'employee_polices.policy_end_date', 'employee_polices.days', 'employee_polices.premium', 'employee_polices.rata_premimum', 'employee_polices.gst', 'employee_polices.date_of_exit', 'employee_polices.reason_for_exit'])
->join('employee_polices', 'employee_polices.employee_id = employees.id')
->where('employees.is_active',1)
->where('employees.file_id',$file_id)
->where('employee_polices.is_active',1)
// ->where('employee_polices.uhid is not null')
// ->where('employee_polices.tpa_id is not null')
->groupStart()
->orWhere('employee_polices.uhid is not null')
->orWhere('employee_polices.tpa_id is not null')
->groupEnd()
->when(count($emp_status), function($query) use ($emp_status){
return $query->whereIn('employees.emp_status', $emp_status);
})
->when(count($policy_status), function($query) use ($policy_status){
return $query->whereIn('employee_polices.status', $policy_status);
})
->findAll();
// ~dd($this->db->getLastQuery());
return ($result);
}
public function getFamiliyCountAndMaxage(string $emp_code)
{
$query = "
SELECT emp_code,family_member_count,max_age
FROM
(
SELECT
emp_code,
COUNT(*) AS family_member_count,
MAX(TIMESTAMPDIFF(YEAR, dob, CURDATE())) AS max_age
FROM employees where emp_code = '{$emp_code}'
GROUP BY emp_code
) AS family_stats
ORDER BY family_member_count DESC, max_age DESC
LIMIT 1;";
$results = $this->db->query($query)->getResult();
return $results;
}
}