FIX_75. The system should allow the same HR contact to be linked with different corporates, instead of restricting them to just one.

This commit is contained in:
venba-Inspriron-3558 2025-10-10 18:58:15 +05:30
parent 1d9bc397a7
commit 2dc2447fb6
4 changed files with 75 additions and 1 deletions

View File

@ -172,6 +172,7 @@ $routes->group("/client", ["filter" => "authMVC"], function ($routes) {
$routes->group("others", ["filter" => "authMVC"], function ($routes) {
$routes->post("create", "ClientController::createOtherTabContent");
$routes->post('check-duplicate', 'ClientController::validateDuplicateByClientBranch');
});
});

View File

@ -132,6 +132,15 @@ class ClientController extends AdminController
//--------------------------------------------------------------------------------------------------------
public function validateDuplicateByClientBranch()
{
$request = service('request');
$value = $request->getPost('value');
$clientId = $request->getPost('client_id');
$branchId = $request->getPost('branch_id');
$isDuplicate = $this->clientModel->isDuplicateByClientBranch($value, $clientId, $branchId);
return $this->response->setJSON(['isDuplicate' => $isDuplicate]);
}
public function checkDuplicateTableFieldValue()
{
$table = $this->request->getPost('table');

View File

@ -226,5 +226,18 @@ class ClientModel extends Model
return $result;
}
public function isDuplicateByClientBranch($email, $clientId, $branchId)
{
$builder = $this->db->table('level_contacts lc')
->select('lc.id')
->join('client_branch cb', 'lc.ref_id = cb.id', 'left')
->where('lc.email', $email)
->where('lc.contact_type', 'client')
->where('cb.client_id', $clientId)
->where('lc.ref_id', $branchId)
->get();
return $builder->getNumRows() > 0 ? true : false;
}
}

View File

@ -649,7 +649,7 @@ function appendContactHtml(contact = false, reset = false) {
<div class="form-row">
<div class="form-group col-md-6">
<label for="${uniqueId}_last_name">Email<span class="text-danger">*</span></label>
<input value="${contact !== undefined && contact !== false ? contact.email : ''}" type="text" class="form-control" placeholder="Enter Contact Email" name="email[]" id="${uniqueId}_email" data-parsley-trigger="change" data-parsley-type="email" onkeyup="validateInput(this, 'level_contacts', 'email', 'branchBtnSubmit')" required>
<input value="${contact !== undefined && contact !== false ? contact.email : ''}" type="text" class="form-control" placeholder="Enter Contact Email" name="email[]" id="${uniqueId}_email" data-parsley-trigger="change" data-parsley-type="email" onkeyup="validateDuplicateByClientBranch(this, 'branchBtnSubmit')" required>
</div>
<div class="form-group col-md-6">
<label for="${uniqueId}_mobile">Mobile<span class="text-danger">*</span></label>
@ -972,6 +972,57 @@ function validateInput(input, table, field, submitButId){
}
function validateDuplicateByClientBranch(input, submitButId) {
let value = $(input).val().trim();
let clientId = $('#client_id_branch').val();
let branchId = $('#branch_id_primarykey').val();
console.log(`Ln968 cId: ${clientId} | bId: ${branchId}`);
// Don't forgot be careful
// 1 Local duplication check (User entered)
let isLocalDuplicate = false;
$('input[name="email[]"]').each(function() {
if (this !== input && $(this).val().trim() === value && value !== '') {
isLocalDuplicate = true;
return false; // break loop
}
});
if (isLocalDuplicate) {
console.log(`r u n Local`);
toastr.warning("Email is duplicate!", 'WARNING');
$('#' + submitButId).prop('disabled', true);
return; // dont call server if duplicate in UI
}
// Don't forgot be careful
// 2 Server-side duplicate check (DB)
if (!isLocalDuplicate && value !== '') {
console.log(`r u n Server`);
$.ajax({
url: '<?= base_url("client/others/check-duplicate") ?>',
type: 'POST',
data: {
client_id: clientId,
branch_id: branchId,
value: value
},
dataType: 'json',
success: function(response) {
if (response.isDuplicate) {
toastr.warning("Email already exists!", 'WARNING');
$('#' + submitButId).prop('disabled', true);
} else {
$('#' + submitButId).prop('disabled', false);
}
},
error: function(xhr, status, error) {
console.error('AJAX Error:', error);
}
});
}
}
function getContactsData() {
const contacts = [];