From ca0eee96258d13a8abd89d142117bdfa3516316b Mon Sep 17 00:00:00 2001 From: Venkatesh Date: Fri, 7 Aug 2026 17:03:45 +0530 Subject: [PATCH] FIX_EXECED_DEPENDENT_APPROVED_COUNT --- app/Controllers/EmployeeRestController.php | 103 +++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/app/Controllers/EmployeeRestController.php b/app/Controllers/EmployeeRestController.php index e8cc0f87..64ae7539 100755 --- a/app/Controllers/EmployeeRestController.php +++ b/app/Controllers/EmployeeRestController.php @@ -7080,6 +7080,20 @@ class EmployeeRestController extends AdminController } if ($status === 'approved') { + $floaterLimitCheck = $this->checkFamilyFloaterDependentLimit( + $employee, + (int) $client_policy_id, + (int) $employee_id + ); + + if ($floaterLimitCheck['allowed'] === false) { + return $this->respond([ + 'status' => 'failed', + 'code' => 400, + 'data' => $floaterLimitCheck['message'], + ], 200); + } + $employee_update_data = [ 'updated_by' => $updated_by, 'processed_by' => $processed_by_role, @@ -7153,6 +7167,95 @@ class EmployeeRestController extends AdminController } } + /** + * On approve: ensure spouse/child counts stay within policy_terms.family_floaters limits. + * + * @return array{allowed: bool, message: string|null} + */ + private function checkFamilyFloaterDependentLimit(array $employee, int $clientPolicyId, int $employeeId): array + { + $relationship = strtolower(trim((string) ($employee['relationship'] ?? ''))); + $floaterKey = ! empty($employee['family_floater_key']) + ? preg_replace('/\d/', '', strtolower(trim((string) $employee['family_floater_key']))) + : strtolower((string) ($this->RelationshipMap($employee['relationship'] ?? '') ?? '')); + + $isSpouse = ($relationship === 'spouse' || $floaterKey === 'spouse'); + $isChild = in_array($relationship, ['son', 'daughter'], true) || $floaterKey === 'child'; + + // Only spouse / child are validated against family_floaters for dependent-add approve + if (! $isSpouse && ! $isChild) { + return ['allowed' => true, 'message' => null]; + } + + $clientPolicy = $this->clientPolicyModel->select('policy_terms')->where('id', $clientPolicyId)->first(); + if (empty($clientPolicy['policy_terms'])) { + return ['allowed' => true, 'message' => null]; + } + + $policyTerms = json_decode($clientPolicy['policy_terms'], true); + if (! is_array($policyTerms) || empty($policyTerms['family_floaters'])) { + return ['allowed' => true, 'message' => null]; + } + + $familyFloaters = is_array($policyTerms['family_floaters']) + ? $policyTerms['family_floaters'] + : (array) $policyTerms['family_floaters']; + + $allowedSpouseCount = (int) ($familyFloaters['spouse'] ?? 0); + $allowedChildCount = (int) ($familyFloaters['childrens'] ?? 0); + + $existingFamily = $this->employeeModel + ->select('employees.id, employees.relationship, employees.family_floater_key') + ->join('employee_polices', 'employee_polices.employee_id = employees.id') + ->where('employees.emp_code', $employee['emp_code']) + ->where('employees.client_id', $employee['client_id']) + ->where('employee_polices.client_policy_id', $clientPolicyId) + ->where('employees.emp_status', 'active') + ->where('employee_polices.status', 'active') + ->where('employees.is_active', 1) + ->where('employee_polices.is_active', 1) + ->where('employees.id !=', $employeeId) + ->findAll(); + + $activeSpouseCount = 0; + $activeChildCount = 0; + + foreach ($existingFamily as $member) { + $memberRelationship = strtolower(trim((string) ($member['relationship'] ?? ''))); + $memberFloaterKey = ! empty($member['family_floater_key']) + ? preg_replace('/\d/', '', strtolower(trim((string) $member['family_floater_key']))) + : ''; + + if ($memberRelationship === 'spouse' || $memberFloaterKey === 'spouse') { + $activeSpouseCount++; + } elseif (in_array($memberRelationship, ['son', 'daughter'], true) || $memberFloaterKey === 'child') { + $activeChildCount++; + } + } + + if ($isSpouse) { + if (($activeSpouseCount + 1) > $allowedSpouseCount) { + return [ + 'allowed' => false, + 'message' => 'Spouse count exceeds policy family floater limit. Allowed: ' + . $allowedSpouseCount . ', existing active: ' . $activeSpouseCount, + ]; + } + } + + if ($isChild) { + if (($activeChildCount + 1) > $allowedChildCount) { + return [ + 'allowed' => false, + 'message' => 'Child count exceeds policy family floater limit. Allowed: ' + . $allowedChildCount . ', existing active: ' . $activeChildCount, + ]; + } + } + + return ['allowed' => true, 'message' => null]; + } + /** * List dependent-add workflow members (API + mobile). * Returns only pending_approval, approved, and rejected dependents (not all members).