This commit is contained in:
VENKATESHWARAN 2026-08-07 15:45:08 +05:30
parent 6d3cf50442
commit 43ee415407
2 changed files with 235 additions and 203 deletions

View File

@ -1714,9 +1714,9 @@ class EmployeeRestController extends AdminController
if ($value > 0 && $key != 'elders_count') {
if ($value != 0 && $key === 'childrens') {
if ($value > 2) {
$value = 2;
}
// if ($value > 2) {
// $value = 2;
// }
for ($i = 1; $i <= $value; $i++) {
$result[] = "child" . $i;
@ -7199,9 +7199,9 @@ class EmployeeRestController extends AdminController
}
/**
* Policy-wise employee details API.
* Policy-wise employee details API (chat context).
* Input: emp_code, client_id (numeric id or MD5 hash of id)
* Returns one object per enrolled active policy with members, claims, premium and terms.
* user_key format: {employee_id}_{client_id}
*/
public function getEmployeePolicyWiseDetails()
{
@ -7243,8 +7243,24 @@ class EmployeeRestController extends AdminController
], 200);
}
$employeeIds = array_column($employeeRows, 'id');
$dayInterval = 10;
$selfEmployee = null;
foreach ($employeeRows as $row) {
$rel = strtolower(trim((string) ($row['relationship'] ?? '')));
$floater = strtolower(trim((string) ($row['family_floater_key'] ?? '')));
if ($rel === 'self' || $floater === 'self') {
$selfEmployee = $row;
break;
}
}
if ($selfEmployee === null) {
$selfEmployee = $employeeRows[0];
}
$employeeId = (string) $selfEmployee['id'];
$clientIdStr = (string) $client_id;
$userKey = $employeeId . '_' . $clientIdStr;
$employeeIds = array_column($employeeRows, 'id');
$dayInterval = 10;
$enrolledPolicyIds = $this->employeePolicyModel
->select('client_policy_id')
@ -7283,14 +7299,13 @@ class EmployeeRestController extends AdminController
], 200);
}
$result = [];
$policies = [];
foreach ($enrolledPolicies as $policy) {
$clientPolicyId = (int) $policy['id'];
$policyTypeId = (int) $policy['policy_type_id'];
$ticketMeta = $this->getPolicyTicketMeta($policyTypeId);
$termsRaw = json_decode($policy['policy_terms'] ?? '{}');
$termsArray = json_decode($policy['policy_terms'] ?? '{}', true) ?: [];
$ticketMeta = $this->getPolicyTicketMeta($policyTypeId);
$termsRaw = json_decode($policy['policy_terms'] ?? '{}');
$termsArray = json_decode($policy['policy_terms'] ?? '{}', true) ?: [];
$coveredMembers = $this->employeePolicyModel
->select('
@ -7298,22 +7313,10 @@ class EmployeeRestController extends AdminController
employees.emp_code,
employees.name,
employees.relationship,
employees.relationship_code,
employees.family_floater_key,
employees.gender,
employees.dob,
employees.doj,
employees.email_personal,
employees.email_corporate,
employees.mobile,
employees.band,
employees.designation,
employees.unit,
employees.emp_status,
employees.basic_pay,
employees.client_id,
employees.client_branch_id,
employee_polices.id AS emp_policy_id,
employee_polices.uhid,
employee_polices.tpa_id,
employee_polices.rand_string,
@ -7324,12 +7327,7 @@ class EmployeeRestController extends AdminController
employee_polices.payable_employee,
employee_polices.date_coverage,
employee_polices.policy_end_date AS member_policy_end_date,
employee_polices.days,
employee_polices.status AS emp_policy_status,
employee_polices.pre_existing_alignments,
employee_polices.claim_status,
employee_polices.date_of_exit,
employee_polices.reason_for_exit
employee_polices.status AS emp_policy_status
')
->join('employees', 'employees.id = employee_polices.employee_id', 'left')
->whereIn('employee_polices.employee_id', $employeeIds)
@ -7343,52 +7341,56 @@ class EmployeeRestController extends AdminController
continue;
}
$empDetails = $this->buildEmpDetailsByRelation($coveredMembers);
$premiumDetails = $this->buildPremiumDetails($coveredMembers, $policyTypeId);
$claimDetails = $this->getClaimsForPolicy($emp_code, $clientPolicyId);
$premiumSummary = $this->buildPremiumDetails($coveredMembers, $policyTypeId);
$claimsSummary = $this->getClaimsForPolicy($emp_code, $clientPolicyId);
$sumInsured = $premiumSummary['sum_insured'];
$displayTerms = [];
if (!empty($termsArray['enrollment_display_key'])) {
$displayTerms = $termsArray['enrollment_display_key'];
$benefits = [];
if (!empty($termsArray['enrollment_display_key']) && is_array($termsArray['enrollment_display_key'])) {
$benefits = $termsArray['enrollment_display_key'];
} else {
$displayTerms = $this->policyTermsFiter($termsRaw, $ticketMeta['policy_group']);
$benefits = $this->policyTermsFiter($termsRaw, $ticketMeta['policy_group']);
}
$result[] = [
'policy_details' => [
'client_id' => (int) $policy['client_id'],
'client_branch_id' => (int) $policy['client_branch_id'],
'client_policy_id' => $clientPolicyId,
'policy_no' => $policy['policy_no'],
'policy_type_id' => $policyTypeId,
'policy_type' => $policy['policy_type'],
'policy_name' => $policy['policy_type'],
'heading' => $policy['policy_long_name'],
'insurer_name' => $policy['insurer_name'],
'tpa_name' => $policy['tpa_name'],
'network_hospitals_url' => $policy['network_hospitals_url'],
'policy_start_date' => $this->convertDateFormatDisplay($policy['policy_start_date']),
'policy_end_date' => $this->convertDateFormatDisplay($policy['policy_end_date']),
'claims_grace_date' => $this->convertDateFormatDisplay($policy['claims_grace_date']),
'policy_status' => ((int) $policy['policy_status'] === 1) ? 'Active' : 'InActive',
'is_addon' => (int) $policy['is_addon'],
'open_for_enrollment' => (int) ($policy['open_for_enrollment'] ?? 0),
'inception_type' => $policy['inception_type'],
'disclaimer' => $policy['disclaimer'],
'sum_insured_label' => $ticketMeta['sum_insured_label'],
'claim_subject' => $ticketMeta['claim_subject'],
'terms_and_conditions' => [
'raw' => $termsArray,
'display' => $displayTerms,
],
'premium_details' => $premiumDetails,
'claim_details' => $claimDetails,
'emp_details' => $empDetails,
],
$policyTerms = [];
if (!empty($termsArray['enrollment_display_key']) && is_array($termsArray['enrollment_display_key'])) {
$policyTerms = $termsArray['enrollment_display_key'];
}
$familyFloaters = $this->extractFamilyFloaters($termsArray);
$isFamilyFloater = true;
if (isset($termsArray['family_floater']) && strpos(strtolower((string) $termsArray['family_floater']), 'no') !== false) {
$isFamilyFloater = false;
}
$policies[] = [
'policy_type' => $policy['policy_type'],
'policy_no' => $policy['policy_no'],
'policy_name' => $policy['policy_type'],
'heading' => $policy['policy_long_name'],
'insurer_name' => $policy['insurer_name'],
'tpa_name' => $policy['tpa_name'] ?: null,
'network_hospitals_url' => $policy['network_hospitals_url'] ?: null,
'policy_status' => ((int) $policy['policy_status'] === 1) ? 'Active' : 'Inactive',
'policy_start_date' => $this->convertDateFormatDisplay($policy['policy_start_date']),
'policy_end_date' => $this->convertDateFormatDisplay($policy['policy_end_date']),
'claims_grace_date' => $this->convertDateFormatDisplay($policy['claims_grace_date']),
'sum_insured_label' => $ticketMeta['sum_insured_label'],
'sum_insured' => (string) $sumInsured,
'sum_insured_display' => $this->formatAmountDisplay($sumInsured),
'claim_subject' => $ticketMeta['claim_subject'],
'family_floater' => $isFamilyFloater,
'family_floaters' => $familyFloaters,
'age_limits' => $this->extractAgeLimits($termsRaw),
'members' => $this->buildPolicyMembersList($coveredMembers),
'benefits' => $benefits ?: new \stdClass(),
'policy_terms' => $policyTerms ?: new \stdClass(),
'premium_summary' => $premiumSummary,
'claims_summary' => $claimsSummary,
];
}
if (empty($result)) {
if (empty($policies)) {
return $this->respond([
'status' => 'failed',
'code' => 404,
@ -7400,10 +7402,23 @@ class EmployeeRestController extends AdminController
return $this->respond([
'status' => 'success',
'code' => 200,
'message' => 'Employee policy-wise details fetched successfully',
'message' => 'Policy chat context fetched successfully',
'data' => [
'policy_data' => $result,
'support' => $this->getClientSupportContacts((int) $client_id),
'employee_id' => $employeeId,
'client_id' => $clientIdStr,
'user_key' => $userKey,
'employee_summary' => [
'name' => $selfEmployee['name'] ?? null,
'emp_code' => $selfEmployee['emp_code'] ?? $emp_code,
'designation' => $selfEmployee['designation'] ?? null,
'band' => $selfEmployee['band'] ?? null,
'unit' => $selfEmployee['unit'] ?? null,
'emp_status' => $selfEmployee['emp_status'] ?? null,
'email_corporate' => $selfEmployee['email_corporate'] ?? null,
'mobile' => $selfEmployee['mobile'] ?? null,
],
'policies' => $policies,
'support' => $this->getClientSupportContacts((int) $client_id),
],
], 200);
} catch (\Exception $e) {
@ -7479,148 +7494,159 @@ class EmployeeRestController extends AdminController
{
if ($policyTypeId === 1) {
return [
'policy_group' => 'gpa',
'ticket_type_id' => 2,
'ticket_settled_status_id'=> 24,
'claim_subject' => 'Claim GPA',
'sum_insured_label' => 'Sum Assured',
'policy_group' => 'gpa',
'ticket_type_id' => 2,
'ticket_settled_status_id' => 24,
'claim_subject' => 'Claim GPA',
'sum_insured_label' => 'Sum Assured',
];
}
if ($policyTypeId === 6) {
return [
'policy_group' => 'other',
'ticket_type_id' => 3,
'ticket_settled_status_id'=> 34,
'claim_subject' => 'Claim EDLI',
'sum_insured_label' => 'Sum Assured',
'policy_group' => 'other',
'ticket_type_id' => 3,
'ticket_settled_status_id' => 34,
'claim_subject' => 'Claim EDLI',
'sum_insured_label' => 'Sum Assured',
];
}
if ($policyTypeId === 7) {
return [
'policy_group' => 'other',
'ticket_type_id' => 4,
'ticket_settled_status_id'=> 44,
'claim_subject' => 'Claim GTLI',
'sum_insured_label' => 'Sum Assured',
'policy_group' => 'other',
'ticket_type_id' => 4,
'ticket_settled_status_id' => 44,
'claim_subject' => 'Claim GTLI',
'sum_insured_label' => 'Sum Assured',
];
}
if ($policyTypeId === 72) {
return [
'policy_group' => 'other',
'ticket_type_id' => 72,
'ticket_settled_status_id'=> 76,
'claim_subject' => 'Claim OPD',
'sum_insured_label' => 'Sum Insured',
'policy_group' => 'other',
'ticket_type_id' => 72,
'ticket_settled_status_id' => 76,
'claim_subject' => 'Claim OPD',
'sum_insured_label' => 'Sum Insured',
];
}
return [
'policy_group' => 'gmc',
'ticket_type_id' => 1,
'ticket_settled_status_id'=> 11,
'claim_subject' => 'Claim GMC',
'sum_insured_label' => 'Sum Insured',
'policy_group' => 'gmc',
'ticket_type_id' => 1,
'ticket_settled_status_id' => 11,
'claim_subject' => 'Claim GMC',
'sum_insured_label' => 'Sum Insured',
];
}
/**
* Group covered members as:
* self => object, spouse/child/parent/parent_in_law/... => array
*/
private function buildEmpDetailsByRelation(array $coveredMembers): array
private function extractFamilyFloaters(array $termsArray): array
{
$empDetails = [
'self' => new \stdClass(),
$floaters = $termsArray['family_floaters'] ?? [];
if (!is_array($floaters)) {
$floaters = [];
}
return [
'self' => $floaters['self'] ?? 0,
'spouse' => $floaters['spouse'] ?? 0,
'childrens' => $floaters['childrens'] ?? 0,
'parents' => $floaters['parents'] ?? 0,
'parents-in-law' => $floaters['parents-in-law'] ?? ($floaters['parents_in_law'] ?? 0),
'either-parents-pil' => $floaters['either-parents-pil'] ?? 0,
'elders_count' => $floaters['elders_count'] ?? null,
];
}
private function extractAgeLimits($termsRaw): array
{
$defaults = [
'self' => ['min' => 18, 'max' => 60],
'spouse' => ['min' => 18, 'max' => 60],
'child' => ['min' => 0, 'max' => 25],
'elders' => ['min' => 18, 'max' => 60],
];
if (is_object($termsRaw) && isset($termsRaw->age_ratio) && is_object($termsRaw->age_ratio)) {
$ageRatio = $termsRaw->age_ratio;
return [
'self' => [
'min' => $ageRatio->self->min ?? $defaults['self']['min'],
'max' => $ageRatio->self->max ?? $defaults['self']['max'],
],
'spouse' => [
'min' => $ageRatio->spouse->min ?? $defaults['spouse']['min'],
'max' => $ageRatio->spouse->max ?? $defaults['spouse']['max'],
],
'child' => [
'min' => $ageRatio->child->min ?? $defaults['child']['min'],
'max' => $ageRatio->child->max ?? $defaults['child']['max'],
],
'elders' => [
'min' => $ageRatio->elders->min ?? $defaults['elders']['min'],
'max' => $ageRatio->elders->max ?? $defaults['elders']['max'],
],
];
}
return $defaults;
}
private function buildPolicyMembersList(array $coveredMembers): array
{
$members = [];
foreach ($coveredMembers as $member) {
$relationKey = $this->normalizeRelationKey($member);
$memberData = $this->formatMemberDetails($member);
if ($relationKey === 'self') {
$empDetails['self'] = $memberData;
continue;
}
if (!isset($empDetails[$relationKey]) || !is_array($empDetails[$relationKey])) {
$empDetails[$relationKey] = [];
}
$empDetails[$relationKey][] = $memberData;
$members[] = $this->formatChatContextMember($member);
}
return $empDetails;
usort($members, function ($a, $b) {
$order = ['self' => 0, 'spouse' => 1, 'son' => 2, 'daughter' => 3];
$aKey = strtolower((string) ($a['relationship'] ?? ''));
$bKey = strtolower((string) ($b['relationship'] ?? ''));
return ($order[$aKey] ?? 99) <=> ($order[$bKey] ?? 99);
});
return $members;
}
private function normalizeRelationKey(array $member): string
{
$floaterKey = strtolower(trim((string) ($member['family_floater_key'] ?? '')));
$floaterKey = preg_replace('/\d+/', '', $floaterKey);
if (in_array($floaterKey, ['self', 'spouse', 'child', 'parent', 'parent_in_law'], true)) {
return $floaterKey;
}
$relationship = trim((string) ($member['relationship'] ?? ''));
if (strcasecmp($relationship, 'Self') === 0) {
return 'self';
}
$mapped = $this->RelationshipMap($relationship);
if (!empty($mapped)) {
return $mapped;
}
$normalized = strtolower(str_replace([' ', '-'], '_', $relationship));
return $normalized !== '' ? $normalized : 'other';
}
private function formatMemberDetails(array $member): array
private function formatChatContextMember(array $member): array
{
$eCardDownload = null;
if (!empty($member['tpa_id']) && !empty($member['rand_string'])) {
$eCardDownload = base_url('download-e-card/') . $member['rand_string'] . '/1';
}
$basicCoverSi = $member['basic_cover_si'] ?? 0;
return [
'employee_id' => (int) ($member['employee_id'] ?? 0),
'emp_policy_id' => (int) ($member['emp_policy_id'] ?? 0),
'emp_code' => $member['emp_code'] ?? null,
'name' => $member['name'] ?? null,
'relationship' => $member['relationship'] ?? null,
'relationship_code' => $member['relationship_code'] ?? null,
'family_floater_key' => $member['family_floater_key'] ?? null,
'gender' => $member['gender'] ?? null,
'dob' => !empty($member['dob']) ? $this->convertDateFormatDMY($member['dob']) : null,
'doj' => !empty($member['doj']) ? $this->convertDateFormatDMY($member['doj']) : null,
'email_personal' => $member['email_personal'] ?? null,
'email_corporate' => $member['email_corporate'] ?? null,
'mobile' => $member['mobile'] ?? null,
'band' => $member['band'] ?? null,
'designation' => $member['designation'] ?? null,
'unit' => $member['unit'] ?? null,
'emp_status' => $member['emp_status'] ?? null,
'basic_pay' => $member['basic_pay'] ?? null,
'client_id' => isset($member['client_id']) ? (int) $member['client_id'] : null,
'client_branch_id' => isset($member['client_branch_id']) ? (int) $member['client_branch_id'] : null,
'uhid' => $member['uhid'] ?? null,
'tpa_id' => $member['tpa_id'] ?? null,
'eCardDownload' => $eCardDownload,
'basic_cover_si' => $member['basic_cover_si'] ?? 0,
'premium' => $member['premium'] ?? 0,
'rata_premimum' => $member['rata_premimum'] ?? 0,
'gst' => $member['gst'] ?? 0,
'payable_employee' => isset($member['payable_employee']) ? (int) $member['payable_employee'] : 0,
'date_coverage' => !empty($member['date_coverage']) ? $this->convertDateFormatDMY($member['date_coverage']) : null,
'member_policy_end_date' => !empty($member['member_policy_end_date']) ? $this->convertDateFormatDMY($member['member_policy_end_date']) : null,
'days' => $member['days'] ?? null,
'emp_policy_status' => $member['emp_policy_status'] ?? null,
'pre_existing_alignments' => $member['pre_existing_alignments'] ?? null,
'claim_status' => $member['claim_status'] ?? null,
'date_of_exit' => !empty($member['date_of_exit']) ? $this->convertDateFormatDMY($member['date_of_exit']) : null,
'reason_for_exit' => $member['reason_for_exit'] ?? null,
'name' => $member['name'] ?? null,
'relationship' => $member['relationship'] ?? null,
'gender' => $member['gender'] ?? null,
'dob' => !empty($member['dob']) ? $this->convertDateFormatDMY($member['dob']) : null,
'emp_status' => $member['emp_status'] ?? null,
'basic_cover_si' => $basicCoverSi,
'basic_cover_si_display' => $this->formatAmountDisplay($basicCoverSi),
'premium' => $member['premium'] ?? null,
'gst' => $member['gst'] ?? null,
'payable_employee' => isset($member['payable_employee']) ? (int) $member['payable_employee'] : 0,
'date_coverage' => !empty($member['date_coverage']) ? $this->convertDateFormatDMY($member['date_coverage']) : null,
'member_policy_end_date' => !empty($member['member_policy_end_date']) ? $this->convertDateFormatDMY($member['member_policy_end_date']) : null,
'emp_policy_status' => $member['emp_policy_status'] ?? null,
'eCardDownload' => $eCardDownload,
];
}
private function formatAmountDisplay($amount): string
{
if ($amount === null || $amount === '') {
return '0';
}
if (function_exists('formatIndianCurrency')) {
return (string) formatIndianCurrency($amount);
}
return number_format((float) $amount, 0, '.', ',');
}
private function buildPremiumDetails(array $coveredMembers, int $policyTypeId): array
{
$siValue = 0;
@ -7637,28 +7663,25 @@ class EmployeeRestController extends AdminController
$rataTotal += (float) ($member['rata_premimum'] ?? 0);
}
// Match existing app behaviour: hide premium/gst for base GPA/GMC in summary views
$hidePremium = in_array($policyTypeId, [1, 2], true);
return [
'sum_insured' => $siValue,
'total_premium' => $hidePremium ? 0 : round($premiumTotal),
'total_gst' => $hidePremium ? 0 : round($gstTotal),
'total_rata_premium'=> $hidePremium ? 0 : round($rataTotal),
'total_with_gst' => $hidePremium ? 0 : round($premiumTotal + $gstTotal),
'member_count' => count($coveredMembers),
'sum_insured' => $siValue,
'total_premium' => $hidePremium ? 0 : round($premiumTotal),
'total_gst' => $hidePremium ? 0 : round($gstTotal),
'total_rata_premium' => $hidePremium ? 0 : round($rataTotal),
'total_with_gst' => $hidePremium ? 0 : round($premiumTotal + $gstTotal),
'member_count' => count($coveredMembers),
];
}
private function getClaimsForPolicy(string $empCode, int $clientPolicyId): array
{
$db = db_connect();
$db = db_connect();
$claims = $db->table('ticket_master tm')
->select([
'tm.id AS ticket_id',
'tm.claim_number AS claim_no',
'tm.ticket_type_id',
'tm.claim_status_id',
"CASE
WHEN tcs.display_name IS NULL OR tcs.display_name = ''
THEN tcs.claim_status
@ -7669,25 +7692,14 @@ class EmployeeRestController extends AdminController
THEN tm.tpa_claim_type
ELSE 'Reimbursement'
END AS claim_type",
'tm.emp_name',
'tm.emp_code',
'tm.relationship',
'tm.insured_name',
'tm.relationship',
'tm.hospital_name',
'tm.doa',
'tm.dod',
'tm.claim_amount',
'tm.approved_amount',
'tm.si_amt',
'tm.policy_no',
'tm.tpa_no',
'tm.client_policy_id',
'DATE_FORMAT(tm.created_at, "%d-%m-%Y") AS ticket_created_date',
'tm.raised_date',
'tm.registration_date',
'tm.approved_date',
'tm.settled_date',
'tm.denial_date',
'tm.denial_reason',
'tm.utr_details',
])
@ -7699,18 +7711,37 @@ class EmployeeRestController extends AdminController
->get()
->getResultArray();
$formattedClaims = [];
$totalClaimAmount = 0;
$totalApprovedAmount = 0;
foreach ($claims as $claim) {
$totalClaimAmount += (float) ($claim['claim_amount'] ?? 0);
$totalApprovedAmount += (float) ($claim['approved_amount'] ?? 0);
$formattedClaims[] = [
'ticket_id' => (string) ($claim['ticket_id'] ?? ''),
'claim_no' => $claim['claim_no'] ?? null,
'status' => $claim['status'] ?? null,
'claim_type' => $claim['claim_type'] ?? null,
'insured_name' => $claim['insured_name'] ?? null,
'relationship' => $claim['relationship'] ?? null,
'hospital_name' => $claim['hospital_name'] ?? null,
'doa' => $claim['doa'] ?? null,
'dod' => $claim['dod'] ?? null,
'claim_amount' => $claim['claim_amount'] ?? null,
'approved_amount' => $claim['approved_amount'] ?? null,
'settled_date' => $claim['settled_date'] ?? null,
'denial_reason' => $claim['denial_reason'] ?? null,
'utr_details' => $claim['utr_details'] ?? null,
];
}
return [
'count' => count($claims),
'total_claim_amount' => round($totalClaimAmount, 2),
'total_approved_amount' => round($totalApprovedAmount, 2),
'claims' => $claims,
'count' => count($formattedClaims),
'total_claim_amount' => round($totalClaimAmount, 2),
'total_approved_amount' => round($totalApprovedAmount, 2),
'claims' => $formattedClaims,
];
}

View File

@ -252,6 +252,7 @@ class EmployeePolicyModel extends Model
$result = $this->select([
'employee_polices.*',
'employee_polices.tpa_id as uhid',
'policy_type.policy_type as policy_name',
'im.short_name as insurer_short_name',
'ib.branch_name as insurer_branch_name',