FIX_SI_MAPPING_SRI
This commit is contained in:
parent
1c7d855720
commit
3920684bcc
@ -52,6 +52,9 @@
|
||||
</div>
|
||||
|
||||
<script>
|
||||
var si_amt_parent_policy = new Set();
|
||||
var si_amt_base_policy = new Set();
|
||||
|
||||
function loadModal(client_policy_id, base_policy_id) {
|
||||
$('.loader').fadeIn();
|
||||
$('.loader-mask').fadeIn();
|
||||
@ -65,7 +68,7 @@ function loadModal(client_policy_id, base_policy_id) {
|
||||
$('.loader-mask').delay(350).fadeOut('slow');
|
||||
};
|
||||
|
||||
var si_amt_base_policy = new Set();
|
||||
|
||||
|
||||
function checkExistingMapping(client_policy_id, base_policy_id) {
|
||||
$.ajax({
|
||||
@ -78,27 +81,17 @@ function checkExistingMapping(client_policy_id, base_policy_id) {
|
||||
success: function(response) {
|
||||
if (response.status === true && response.code === 200 && response.data.length > 0) {
|
||||
// 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
|
||||
fetchBasePolicy(client_policy_id, base_policy_id);
|
||||
fetchParentPolicy(client_policy_id, base_policy_id);
|
||||
|
||||
// Wait for dropdowns to be populated
|
||||
setTimeout(() => {
|
||||
fetchBasePolicy(client_policy_id, base_policy_id).then(()=>{
|
||||
fetchParentPolicy(client_policy_id, base_policy_id).then(()=>{
|
||||
// setTimeout(() => {
|
||||
response.data.forEach((mapping, index) => {
|
||||
if (index === 0) {
|
||||
// Handle first row
|
||||
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
|
||||
firstRow.find('select[name="choose_si_amount_from_base_policy"]')
|
||||
.val(mapping.base_policy_si_amount)
|
||||
@ -113,22 +106,13 @@ function checkExistingMapping(client_policy_id, base_policy_id) {
|
||||
// Store pk value
|
||||
firstRow.find('input[name="primary_key"]').val(mapping.pk);
|
||||
} else {
|
||||
// Add new row for subsequent mappings
|
||||
addRow();
|
||||
$('#addButton').trigger("click");
|
||||
// For subsequent mappings, add a new row WITHOUT triggering the add button
|
||||
addSIMappingRow();
|
||||
|
||||
// Wait for the new row to be properly initialized
|
||||
setTimeout(() => {
|
||||
// setTimeout(() => {
|
||||
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
|
||||
newRow.find('select[name="choose_si_amount_from_base_policy"]')
|
||||
.val(mapping.base_policy_si_amount)
|
||||
@ -142,11 +126,17 @@ function checkExistingMapping(client_policy_id, base_policy_id) {
|
||||
|
||||
// Store pk value
|
||||
newRow.find('input[name="primary_key"]').val(mapping.pk);
|
||||
}, 500);
|
||||
// }, 500);
|
||||
}
|
||||
});
|
||||
}, 1000); // Wait for fetchBasePolicy and fetchParentPolicy to complete
|
||||
}else{
|
||||
// }, 1000);
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
// Wait for dropdowns to be populated
|
||||
// Wait for fetchBasePolicy and fetchParentPolicy to complete
|
||||
} else {
|
||||
fetchBasePolicy(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) {
|
||||
$.ajax({
|
||||
return $.ajax({
|
||||
url: '<?php echo base_url("util/getPolicySIRacketData")?>',
|
||||
method: 'GET',
|
||||
data: {
|
||||
@ -180,17 +170,17 @@ function fetchBasePolicy(client_policy_id, base_policy_id) {
|
||||
}
|
||||
|
||||
// Convert Set back to an array to use in the dropdown
|
||||
si_amt_base_policy = Array.from(si_amt_base_policy);
|
||||
console.log("Unique SI amounts:", si_amt_base_policy); // Check the result
|
||||
var si_amt_base_policy_array = Array.from(si_amt_base_policy);
|
||||
console.log("Unique SI amounts:", si_amt_base_policy_array); // Check the result
|
||||
|
||||
// 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>');
|
||||
|
||||
// Append the new options
|
||||
for (let i = 0; i < si_amt_base_policy.length; i++) {
|
||||
console.log("Appending option:", si_amt_base_policy[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>`);
|
||||
for (let i = 0; i < si_amt_base_policy_array.length; i++) {
|
||||
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_array[i]}">${si_amt_base_policy_array[i]}</option>`);
|
||||
}
|
||||
|
||||
} 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) {
|
||||
$.ajax({
|
||||
return $.ajax({
|
||||
url: '<?= base_url("util/fetchPolicySIDataForParentPolicy")?>',
|
||||
method: 'GET',
|
||||
data: {
|
||||
@ -224,17 +213,17 @@ function fetchParentPolicy(client_policy_id, base_policy_id) {
|
||||
}
|
||||
|
||||
// Convert Set back to an array to use in the dropdown
|
||||
si_amt_parent_policy = Array.from(si_amt_parent_policy);
|
||||
console.log("Unique SI amounts:", si_amt_parent_policy); // Check the result
|
||||
var si_amt_parent_policy_array = Array.from(si_amt_parent_policy);
|
||||
console.log("Unique SI amounts:", si_amt_parent_policy_array); // Check the result
|
||||
|
||||
// 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>');
|
||||
|
||||
// Append the new options
|
||||
for (let i = 0; i < si_amt_parent_policy.length; i++) {
|
||||
console.log("Appending option:", si_amt_parent_policy[i]); // Log each SI value
|
||||
$('#choose_si_amount_for_policy').append(`<option value="${si_amt_parent_policy[i]}">${si_amt_parent_policy[i]}</option>`);
|
||||
for (let i = 0; i < si_amt_parent_policy_array.length; i++) {
|
||||
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_array[i]}">${si_amt_parent_policy_array[i]}</option>`);
|
||||
}
|
||||
|
||||
} else {
|
||||
@ -256,98 +245,10 @@ $(document).ready(function() {
|
||||
$("textarea.select2-search__field").css('resize', 'none');
|
||||
|
||||
// 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
|
||||
$(document).on('click', '.add-row', function() {
|
||||
addRow();
|
||||
addSIMappingRow();
|
||||
});
|
||||
|
||||
// Remove an SI Mapping row
|
||||
@ -446,6 +347,9 @@ function submitForm() {
|
||||
});
|
||||
}
|
||||
function resetFormAndRemoveRows() {
|
||||
|
||||
si_amt_parent_policy.clear();
|
||||
si_amt_base_policy.clear();
|
||||
// Reset the 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');
|
||||
}
|
||||
|
||||
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>
|
||||
Loading…
Reference in New Issue
Block a user