MERGE_UAT_SI_MAPPING_ISSUE

This commit is contained in:
Venba 2025-03-04 07:04:56 +00:00
commit d2269334a0

View File

@ -52,6 +52,9 @@
</div> </div>
<script> <script>
var si_amt_parent_policy = new Set();
var si_amt_base_policy = new Set();
function loadModal(client_policy_id, base_policy_id) { function loadModal(client_policy_id, base_policy_id) {
$('.loader').fadeIn(); $('.loader').fadeIn();
$('.loader-mask').fadeIn(); $('.loader-mask').fadeIn();
@ -65,7 +68,7 @@ function loadModal(client_policy_id, base_policy_id) {
$('.loader-mask').delay(350).fadeOut('slow'); $('.loader-mask').delay(350).fadeOut('slow');
}; };
var si_amt_base_policy = new Set();
function checkExistingMapping(client_policy_id, base_policy_id) { function checkExistingMapping(client_policy_id, base_policy_id) {
$.ajax({ $.ajax({
@ -78,27 +81,17 @@ function checkExistingMapping(client_policy_id, base_policy_id) {
success: function(response) { success: function(response) {
if (response.status === true && response.code === 200 && response.data.length > 0) { if (response.status === true && response.code === 200 && response.data.length > 0) {
// Clear existing rows except the first one // Clear existing rows except the first one
$('.si-mapping-row:not(:first)').remove(); $('.si-mapping-row:not(:first)').empty();
// First, ensure base policy and parent policy data is loaded // First, ensure base policy and parent policy data is loaded
fetchBasePolicy(client_policy_id, base_policy_id); fetchBasePolicy(client_policy_id, base_policy_id).then(()=>{
fetchParentPolicy(client_policy_id, base_policy_id); fetchParentPolicy(client_policy_id, base_policy_id).then(()=>{
// setTimeout(() => {
// Wait for dropdowns to be populated
setTimeout(() => {
response.data.forEach((mapping, index) => { response.data.forEach((mapping, index) => {
if (index === 0) { if (index === 0) {
// Handle first row // Handle first row
const firstRow = $('.si-mapping-row:first'); const firstRow = $('.si-mapping-row:first');
// Clear existing values
firstRow.find('select[name="choose_si_amount_from_base_policy"]')
.val('')
.trigger('change');
firstRow.find('select[name="choose_si_amount_for_policy[]"]')
.val([])
.trigger('change');
// Set base policy SI amount // Set base policy SI amount
firstRow.find('select[name="choose_si_amount_from_base_policy"]') firstRow.find('select[name="choose_si_amount_from_base_policy"]')
.val(mapping.base_policy_si_amount) .val(mapping.base_policy_si_amount)
@ -113,22 +106,13 @@ function checkExistingMapping(client_policy_id, base_policy_id) {
// Store pk value // Store pk value
firstRow.find('input[name="primary_key"]').val(mapping.pk); firstRow.find('input[name="primary_key"]').val(mapping.pk);
} else { } else {
// Add new row for subsequent mappings // For subsequent mappings, add a new row WITHOUT triggering the add button
addRow(); addSIMappingRow();
$('#addButton').trigger("click");
// Wait for the new row to be properly initialized // Wait for the new row to be properly initialized
setTimeout(() => { // setTimeout(() => {
const newRow = $('.si-mapping-row:last'); const newRow = $('.si-mapping-row:last');
// Clear existing values
newRow.find('select[name="choose_si_amount_from_base_policy"]')
.val('')
.trigger('change');
newRow.find('select[name="choose_si_amount_for_policy[]"]')
.val([])
.trigger('change');
// Set base policy SI amount // Set base policy SI amount
newRow.find('select[name="choose_si_amount_from_base_policy"]') newRow.find('select[name="choose_si_amount_from_base_policy"]')
.val(mapping.base_policy_si_amount) .val(mapping.base_policy_si_amount)
@ -142,11 +126,17 @@ function checkExistingMapping(client_policy_id, base_policy_id) {
// Store pk value // Store pk value
newRow.find('input[name="primary_key"]').val(mapping.pk); newRow.find('input[name="primary_key"]').val(mapping.pk);
}, 500); // }, 500);
} }
}); });
}, 1000); // Wait for fetchBasePolicy and fetchParentPolicy to complete // }, 1000);
}else{ })
})
// Wait for dropdowns to be populated
// Wait for fetchBasePolicy and fetchParentPolicy to complete
} else {
fetchBasePolicy(client_policy_id, base_policy_id); fetchBasePolicy(client_policy_id, base_policy_id);
fetchParentPolicy(client_policy_id, base_policy_id); fetchParentPolicy(client_policy_id, base_policy_id);
} }
@ -159,7 +149,7 @@ function checkExistingMapping(client_policy_id, base_policy_id) {
} }
function fetchBasePolicy(client_policy_id, base_policy_id) { function fetchBasePolicy(client_policy_id, base_policy_id) {
$.ajax({ return $.ajax({
url: '<?php echo base_url("util/getPolicySIRacketData")?>', url: '<?php echo base_url("util/getPolicySIRacketData")?>',
method: 'GET', method: 'GET',
data: { data: {
@ -180,17 +170,17 @@ function fetchBasePolicy(client_policy_id, base_policy_id) {
} }
// Convert Set back to an array to use in the dropdown // Convert Set back to an array to use in the dropdown
si_amt_base_policy = Array.from(si_amt_base_policy); var si_amt_base_policy_array = Array.from(si_amt_base_policy);
console.log("Unique SI amounts:", si_amt_base_policy); // Check the result console.log("Unique SI amounts:", si_amt_base_policy_array); // Check the result
// Clear existing options in the dropdown // Clear existing options in the dropdown
// $('#choose_si_amount_from_base_policy').empty(); $('#choose_si_amount_from_base_policy').empty();
$('#choose_si_amount_from_base_policy').append('<option value="">Select SI Amount</option>'); $('#choose_si_amount_from_base_policy').append('<option value="">Select SI Amount</option>');
// Append the new options // Append the new options
for (let i = 0; i < si_amt_base_policy.length; i++) { for (let i = 0; i < si_amt_base_policy_array.length; i++) {
console.log("Appending option:", si_amt_base_policy[i]); // Log each SI value console.log("Appending option:", si_amt_base_policy_array[i]); // Log each SI value
$('#choose_si_amount_from_base_policy').append(`<option value="${si_amt_base_policy[i]}">${si_amt_base_policy[i]}</option>`); $('#choose_si_amount_from_base_policy').append(`<option value="${si_amt_base_policy_array[i]}">${si_amt_base_policy_array[i]}</option>`);
} }
} else { } else {
@ -203,9 +193,8 @@ function fetchBasePolicy(client_policy_id, base_policy_id) {
}); });
} }
var si_amt_parent_policy = new Set();
function fetchParentPolicy(client_policy_id, base_policy_id) { function fetchParentPolicy(client_policy_id, base_policy_id) {
$.ajax({ return $.ajax({
url: '<?= base_url("util/fetchPolicySIDataForParentPolicy")?>', url: '<?= base_url("util/fetchPolicySIDataForParentPolicy")?>',
method: 'GET', method: 'GET',
data: { data: {
@ -224,17 +213,17 @@ function fetchParentPolicy(client_policy_id, base_policy_id) {
} }
// Convert Set back to an array to use in the dropdown // Convert Set back to an array to use in the dropdown
si_amt_parent_policy = Array.from(si_amt_parent_policy); var si_amt_parent_policy_array = Array.from(si_amt_parent_policy);
console.log("Unique SI amounts:", si_amt_parent_policy); // Check the result console.log("Unique SI amounts:", si_amt_parent_policy_array); // Check the result
// Clear existing options in the dropdown // Clear existing options in the dropdown
// $('#choose_si_amount_for_policy').empty(); $('#choose_si_amount_for_policy').empty();
$('#choose_si_amount_for_policy').append('<option value="">Select SI Amount</option>'); $('#choose_si_amount_for_policy').append('<option value="">Select SI Amount</option>');
// Append the new options // Append the new options
for (let i = 0; i < si_amt_parent_policy.length; i++) { for (let i = 0; i < si_amt_parent_policy_array.length; i++) {
console.log("Appending option:", si_amt_parent_policy[i]); // Log each SI value console.log("Appending option:", si_amt_parent_policy_array[i]); // Log each SI value
$('#choose_si_amount_for_policy').append(`<option value="${si_amt_parent_policy[i]}">${si_amt_parent_policy[i]}</option>`); $('#choose_si_amount_for_policy').append(`<option value="${si_amt_parent_policy_array[i]}">${si_amt_parent_policy_array[i]}</option>`);
} }
} else { } else {
@ -256,98 +245,10 @@ $(document).ready(function() {
$("textarea.select2-search__field").css('resize', 'none'); $("textarea.select2-search__field").css('resize', 'none');
// Function to get all selected base policy values // Function to get all selected base policy values
function getSelectedBasePolicies() {
var selectedBasePolicies = [];
$('.si-mapping-row').each(function() {
var selectedBasePolicy = $(this).find('select[name="choose_si_amount_from_base_policy"]').val();
if (selectedBasePolicy) {
selectedBasePolicies.push(selectedBasePolicy);
}
});
return selectedBasePolicies;
}
function addRow() {
// Get selected base policies from all previous rows
var selectedBasePolicies = getSelectedBasePolicies();
// Check if si_amt_base_policy has been populated
if (si_amt_base_policy.length === 0) {
console.log("SI amounts not yet loaded.");
return; // Prevent adding the row if the data isn't ready
}
// Create a new row with the same structure as the existing row
var newRow = $('<div class="form-row si-mapping-row">');
// Generate unique IDs for the new row's selects
var uniqueIdForBasePolicy = 'choose_base_policy_' + Date.now();
var uniqueIdForSIAmountForPolicy = 'choose_si_amount_for_policy_' + Date.now();
newRow.append(
'<input name = "primary_key" type="hidden" id="'+uniqueIdForBasePolicy+'" value="">'+
'<div class="form-group col-md-3">' +
'<label>Base Policy SI Amount<span class="text-danger">*</span></label>' +
'<select class="form-control" id="' + uniqueIdForBasePolicy + '" name="choose_si_amount_from_base_policy">' +
'<option value="">Choose SI Amount</option>' +
'</select>' +
'</div>'
);
newRow.append(
'<div class="form-group col-md-3">' +
'<label>Policy SI Amount</label><br />' +
'<select class="form-control" id="' + uniqueIdForSIAmountForPolicy + '" name="choose_si_amount_for_policy[]" multiple>' +
'</select>' +
'</div>'
);
newRow.append(
'<div class="form-group">' +
'<div class="btn-group" style="margin-top: 45px;">' +
'<button type="button" class="btn btn-danger remove-row" style="margin-right: 5px;">x</button>' +
'<button type="button" class="btn btn-success add-row">+</button>' +
'</div>' +
'</div>'
);
// Append the new row to the container
$('#si-mapping-container').append(newRow);
// Loop through and append SI amounts to the new row's 'choose_si_amount_from_base_policy' dropdown
si_amt_base_policy.forEach(function(value) {
newRow.find('select[name="choose_si_amount_from_base_policy"]').append('<option value="'+value+'">'+value+'</option>');
});
// Remove selected base policies from the new row's 'choose_si_amount_from_base_policy' options
newRow.find('select[name="choose_si_amount_from_base_policy"] option').each(function() {
var value = $(this).val();
if (selectedBasePolicies.includes(value)) {
$(this).remove();
}
});
// Append the unique SI amounts to 'choose_si_amount_for_policy[]'
si_amt_parent_policy.forEach(function(value) {
newRow.find('select[name="choose_si_amount_for_policy[]"]').append('<option value="'+value+'">'+value+'</option>');
});
// Re-initialize select2 for the new row's dropdowns
newRow.find('select[name="choose_si_amount_for_policy[]"]').select2({
placeholder: 'Select SI Amount'
});
newRow.find('select[name="choose_si_amount_from_base_policy"]').select2({
placeholder: 'Choose SI Amount'
});
$("textarea.select2-search__field").attr('rows', '1');
$("textarea.select2-search__field").css('resize', 'none');
}
// Use event delegation to bind the click event for .add-row // Use event delegation to bind the click event for .add-row
$(document).on('click', '.add-row', function() { $(document).on('click', '.add-row', function() {
addRow(); addSIMappingRow();
}); });
// Remove an SI Mapping row // Remove an SI Mapping row
@ -446,6 +347,9 @@ function submitForm() {
}); });
} }
function resetFormAndRemoveRows() { function resetFormAndRemoveRows() {
si_amt_parent_policy.clear();
si_amt_base_policy.clear();
// Reset the form fields // Reset the form fields
$('#si_mapping_form')[0].reset(); // Reset all form fields $('#si_mapping_form')[0].reset(); // Reset all form fields
@ -462,5 +366,93 @@ function resetFormAndRemoveRows() {
$('#choose_si_amount_for_policy').val('').trigger('change'); $('#choose_si_amount_for_policy').val('').trigger('change');
} }
function getSelectedBasePolicies() {
var selectedBasePolicies = [];
$('.si-mapping-row').each(function() {
var selectedBasePolicy = $(this).find('select[name="choose_si_amount_from_base_policy"]').val();
if (selectedBasePolicy) {
selectedBasePolicies.push(selectedBasePolicy);
}
});
return selectedBasePolicies;
}
function addSIMappingRow() {
// Get selected base policies from all previous rows
var selectedBasePolicies = getSelectedBasePolicies();
// Check if si_amt_base_policy has been populated
if (si_amt_base_policy.length === 0) {
console.log("SI amounts not yet loaded.");
return; // Prevent adding the row if the data isn't ready
}
// Create a new row with the same structure as the existing row
var newRow = $('<div class="form-row si-mapping-row">');
// Generate unique IDs for the new row's selects
var uniqueIdForBasePolicy = 'choose_base_policy_' + Date.now();
var uniqueIdForSIAmountForPolicy = 'choose_si_amount_for_policy_' + Date.now();
newRow.append(
'<input name="primary_key" type="hidden" value="">'+
'<div class="form-group col-md-3">' +
'<label>Base Policy SI Amount<span class="text-danger">*</span></label>' +
'<select class="form-control" id="' + uniqueIdForBasePolicy + '" name="choose_si_amount_from_base_policy">' +
'<option value="">Choose SI Amount</option>' +
'</select>' +
'</div>'
);
newRow.append(
'<div class="form-group col-md-3">' +
'<label>Policy SI Amount</label><br />' +
'<select class="form-control" id="' + uniqueIdForSIAmountForPolicy + '" name="choose_si_amount_for_policy[]" multiple>' +
'</select>' +
'</div>'
);
newRow.append(
'<div class="form-group">' +
'<div class="btn-group" style="margin-top: 45px;">' +
'<button type="button" class="btn btn-danger remove-row" style="margin-right: 5px;">x</button>' +
'<button type="button" class="btn btn-success add-row">+</button>' +
'</div>' +
'</div>'
);
// Append the new row to the container
$('#si-mapping-container').append(newRow);
// First, populate all options
si_amt_base_policy.forEach(function(value) {
newRow.find('select[name="choose_si_amount_from_base_policy"]').append('<option value="'+value+'">'+value+'</option>');
});
// Then, remove already selected values from the dropdown
selectedBasePolicies.forEach(function(value) {
if (value) { // Check if the value is not empty
newRow.find('select[name="choose_si_amount_from_base_policy"] option[value="'+value+'"]').remove();
}
});
// Append the unique SI amounts to 'choose_si_amount_for_policy[]'
si_amt_parent_policy.forEach(function(value) {
newRow.find('select[name="choose_si_amount_for_policy[]"]').append('<option value="'+value+'">'+value+'</option>');
});
// Initialize select2 for the new dropdowns
newRow.find('select[name="choose_si_amount_for_policy[]"]').select2({
placeholder: 'Select SI Amount'
});
// newRow.find('select[name="choose_si_amount_from_base_policy"]').select2({
// placeholder: 'Choose SI Amount'
// });
$("textarea.select2-search__field").attr('rows', '1');
$("textarea.select2-search__field").css('resize', 'none');
}
</script> </script>