From 3c1eb1bbc4e48d11c57f68331f5b2107ca9d1b4d Mon Sep 17 00:00:00 2001 From: venba-Inspriron-3558 Date: Sat, 11 Oct 2025 18:48:08 +0530 Subject: [PATCH] FIX_resolved - Email duplication based on ClientBrach testcase wise. --- app/Views/client_branch.php | 117 ++++++++++++++++++++++++++---------- 1 file changed, 85 insertions(+), 32 deletions(-) diff --git a/app/Views/client_branch.php b/app/Views/client_branch.php index 0fc1fa1..34e5201 100755 --- a/app/Views/client_branch.php +++ b/app/Views/client_branch.php @@ -956,55 +956,108 @@ function validateDuplicateByClientBranch(input, field, submitButId) { let message = label ? label + " is duplicate!" : "Value is duplicate!"; console.log(`cId: ${clientId} | bId: ${branchId}`); - + // Don't forgot be careful // 1 Local duplication check (User entered) let isLocalDuplicate = false; $('input[name="' + field + '[]"]').each(function(index) { + let compareVal = $(this).val().trim(); console.log(`Entered value: ${value} | Contact ${index+1} value: ${$(this).val()}`); - if (this !== input && $(this).val().trim() === value) { + if (this !== input && compareVal !== '' && compareVal === value) { isLocalDuplicate = true; - return false; // break loop + return false; } }); if (isLocalDuplicate) { console.log(`r u n Local`); - console.log(`btn Dis - true`); + console.log(`duplicate found for ${field}`); toastr.warning(message, 'WARNING'); $('#' + submitButId).prop('disabled', true); - return; // don’t call server if duplicate in UI + return; } - + + // important Skip empty values + if (value === '') { + checkAllFieldsValid(submitButId); + return; + } + // Don't forgot be careful // 2 Server-side duplicate check (DB) - if (!isLocalDuplicate && value !== '') { - - $.ajax({ - url: '', - type: 'POST', - data: { - client_id: clientId, - branch_id: branchId, - value: value, - field: field - }, - dataType: 'json', - success: function(response) { - if (response.isDuplicate) { - console.log(`r u n Server`); - console.log(`btn Dis - true`); - toastr.warning(message, 'WARNING'); - $('#' + submitButId).prop('disabled', true); - } else { - console.log(`btn Dis - false`); - $('#' + submitButId).prop('disabled', false); - } - }, - error: function(xhr, status, error) { - console.error('AJAX Error:', error); + $.ajax({ + url: '', + type: 'POST', + data: { + client_id: clientId, + branch_id: branchId, + value: value, + field: field + }, + dataType: 'json', + success: function(response) { + if (response.isDuplicate) { + console.log(`r u n Server`); + console.log(`duplicate found for ${field}`); + toastr.warning(message, 'WARNING'); + $('#' + submitButId).prop('disabled', true); + } else { + console.log(`No duplicate for ${field}`); + checkAllFieldsValid(submitButId); } - }); + }, + error: function(xhr, status, error) { + console.error('AJAX Error:', error); + } + }); +} + + +// recheck all contacts before enabling submit +function checkAllFieldsValid(submitButId) { + let emailDuplicates = false; + let mobileDuplicates = false; + + // cross check all EMAIL duplicates + let emailSeen = []; + $('input[name="email[]"]').each(function() { + let val = $(this).val().trim(); + if (val && emailSeen.includes(val)) { + emailDuplicates = true; + } else if (val) { + emailSeen.push(val); + } + }); + + // cross check all MOBILE duplicates + let mobileSeen = []; + $('input[name="mobile[]"]').each(function() { + let val = $(this).val().trim(); + if (val && mobileSeen.includes(val)) { + mobileDuplicates = true; + } else if (val) { + mobileSeen.push(val); + } + }); + + if (emailDuplicates || mobileDuplicates) { + $('#' + submitButId).prop('disabled', true); + // show correct message based on what’s duplicated + if (emailDuplicates && mobileDuplicates) { + toastr.warning("Email and Mobile values are duplicate!", "WARNING"); + console.log('Both Email and Mobile duplicates'); + } else if (emailDuplicates) { + toastr.warning("Email duplicate!", "WARNING"); + console.log('Cross Check Email duplicates'); + } else if (mobileDuplicates) { + toastr.warning("Mobile duplicate!", "WARNING"); + console.log('Cross Check Mobile duplicates'); + } + console.log(`btn Dis - true`); + } else { + console.log('unique — enable'); + console.log(`btn Dis - false`); + $('#' + submitButId).prop('disabled', false); } }