diff --git a/app/Controllers/EmployeeMultiEventServiceController.php b/app/Controllers/EmployeeMultiEventServiceController.php index 231cd231..fef1e2f3 100644 --- a/app/Controllers/EmployeeMultiEventServiceController.php +++ b/app/Controllers/EmployeeMultiEventServiceController.php @@ -238,7 +238,7 @@ class EmployeeMultiEventServiceController extends BaseController 'format' => null, 'allowed_values' => null, 'custom' => 'check_dup_email', - 'params' => ['row', 'existing_mobilenos'] + 'params' => ['row', 'existing_mobilenos', 'excel_data', 'row_key'] ], 'pre_existing_ailments' => [ 'col_idx' => 14, diff --git a/app/Controllers/EmployeeServiceController.php b/app/Controllers/EmployeeServiceController.php index f490be1e..ecf2d49a 100755 --- a/app/Controllers/EmployeeServiceController.php +++ b/app/Controllers/EmployeeServiceController.php @@ -241,7 +241,7 @@ class EmployeeServiceController extends AdminController 'format' => null, 'allowed_values' => null, 'custom' => 'check_dup_email', - 'params' => ['row', 'existing_mobilenos'] + 'params' => ['row', 'existing_mobilenos', 'excel_data', 'row_key'] ], 'pre_existing_ailments' => [ 'col_idx' => 14, diff --git a/app/Helpers/excel_util_helper.php b/app/Helpers/excel_util_helper.php index 326c2d14..5aa68990 100755 --- a/app/Helpers/excel_util_helper.php +++ b/app/Helpers/excel_util_helper.php @@ -2173,8 +2173,8 @@ if (!function_exists('check_dup_mobileno')) { // } // } -if (!function_exists('check_dup_email')) { - function check_dup_email(array $row, array $existing_mobilenos) +if (!function_exists('check_dup_email_old')) { + function check_dup_email_old(array $row, array $existing_mobilenos) { $errorMessages = []; @@ -2203,6 +2203,54 @@ if (!function_exists('check_dup_email')) { } } +if (!function_exists('check_dup_email')) { + function check_dup_email(array $row, array $existing_mobilenos, $excel_data = [], $row_key = null) + { + $errorMessages = []; + $current_relation = strtolower(trim($row['5'] ?? '')); + $current_email = trim((string) ($row['13'] ?? '')); + + // Validate email + $emailCheck = is_valid_or_empty_email($current_email); + if (!$emailCheck) { + $errorMessages[] = 'Invalid email format'; + } + + // Check duplicate only if email is not empty + if (!empty($current_email)) { + foreach ($existing_mobilenos as $value) { + if ($current_relation === 'self' && strcasecmp($current_email, trim((string) ($value['email_corporate'] ?? ''))) === 0) { + $errorMessages[] = 'Duplicate Email'; + break; + } + } + + foreach ($excel_data as $ed_row => $value) { + if ($row_key !== null && (string) $ed_row === (string) $row_key) { + continue; + } + + $excel_relation = strtolower(trim($value['5'] ?? '')); + $excel_email = trim((string) ($value['13'] ?? '')); + + if ($current_relation === 'self' && $excel_relation === 'self' && $excel_email !== '' && strcasecmp($current_email, $excel_email) === 0) { + $duplicate_row_no = is_numeric($ed_row) ? $ed_row + 1 : $ed_row; + $current_row_no = is_numeric($row_key) ? $row_key + 1 : $row_key; + $errorMessages[] = "This email has already been used for another self in Excel at row no. " . ($duplicate_row_no) . " and " . ($current_row_no); + break; + } + } + } + + // Prepare final return + if (!empty($errorMessages)) { + return ['status' => false, 'error' => implode(' & ', $errorMessages)]; + } + + return ['status' => true, 'error' => '']; + } +} + if (!function_exists('generate_family_relationship_array')) { function generate_family_relationship_array($family_structure_from_policy_terms)