nhance/app/Views/policy_grid_excel.php
2026-02-09 12:57:17 +05:30

743 lines
23 KiB
PHP
Executable File

<style>
table {
border-collapse: collapse;
width: 100%;
}
td,
th {
padding: 0.2rem;
}
.error {
background-color: #f8d7da;
}
.duplicate {
background-color: #fff3cd;
}
.container {
margin-top: 20px;
}
.compact-table td,
.compact-table th {
padding: 0.1rem;
}
</style>
<script>
const formatType = 0; // Specify the format type here
const excel_headers = {
1: {
1: {
"unit": "Unit",
"sum_insured": "Sum Insured",
},
2: {
"unit": "Unit",
"basic_pay": "Basic Pay",
},
3: {
"unit": "Unit",
"band_or_grade": "Band or Grade",
"sum_insured": "Sum Insured",
}
},
2: {
"unit": "Unit",
"sum_insured": "Sum Insured",
"premium": "Premium"
},
3: {
"unit": "Unit",
"sum_insured": "Sum Insured",
"premium": "Premium"
},
4: {
"unit": "Unit",
"sum_insured": "Sum Insured",
"from_age": "From Age",
"to_age": "To Age",
"premium": "Premium"
},
5: {
"unit": "Unit",
"sum_insured": "Sum Insured",
"from_age": "From Age",
"to_age": "To Age",
"premium": "Premium"
},
6: {
"unit": "Unit",
"sum_insured": "Sum Insured",
"from_age": "From Age",
"to_age": "To Age",
"premium": "Premium"
},
7: {
"unit": "Unit",
"sum_insured": "Sum Insured",
"from_age": "From Age",
"to_age": "To Age",
"premium": "Premium"
},
8: {
"unit": "Unit",
"sum_insured": "Sum Insured",
"grade": "Grade",
"premium": "Premium"
},
9: {
"unit": "Unit",
"sum_insured": "Sum Insured",
"premium": "Premium"
},
10: {
"unit": "Unit",
"sum_insured": "Sum Insured",
"from_age": "From Age",
"to_age": "To Age",
"premium": "Premium"
},
11: {
"unit": "Unit",
"sum_insured": "Sum Insured",
"grade": "Grade",
"premium": "Premium",
"max_si": "Max Si"
},
12: {
"unit": "Unit",
"sum_insured": "Sum Insured",
"relationship": "Relationship",
"premium": "Premium"
},
13: {
"unit": "Unit",
"sum_insured": "Sum Insured",
"relationship": "Relationship",
"from_age": "From Age",
"to_age": "To Age",
"premium": "Premium"
}
};
function slugify(text) {
return text.toString().toLowerCase().replace(/\s+/g, '_').replace(/[^\w\-]+/g, '').replace(/\-\-+/g, '_')
.replace(/^-+/, '').replace(/-+$/, '');
}
function copyHeaders(unique_id) {
var client_units = localStorage.getItem('client_units');
client_units = JSON.parse(client_units);
// console.log('copyHeaders function client_units', client_units);
// console.log('copyHeaders function client_units.length', client_units.length)
let formatType = $('#grid').val();
var secondKey = $('#si_or_bp').val();
var obj = $('#grid');
if (unique_id != 'na') {
formatType = $('#grid_'+unique_id).val();
obj = $('#grid_'+unique_id);
}
//console.log(obj)
//console.log(formatType)
//console.log(unique_id)
if (formatType == 1) {
if (secondKey == "") {
toastr.warning('Please select the Basic Pay, Sum Insured and Band/Grade', 'Warning');
return;
}
}
//console.log(obj)
//console.log(formatType)
//console.log(unique_id)
if (!formatType && formatType == "") {
toastr.warning('Please select the Policy Premium Type', 'Warning');
return;
}
// //console.log('after toastr')
// //console.log('excel_headers[formatType]', excel_headers[formatType])
// //console.log('excel_headers[formatType]', typeof excel_headers[formatType])
if (excel_headers[formatType] == undefined) {
toastr.error('Headers not found', 'Warning');
return;
}
if(client_units.length == 1){
delete excel_headers[formatType].unit;
}
var headerString = Object.values(excel_headers[formatType]).join("\t"); // Using specified format type for copying headers
console.log('copyHeaders function default headerString', headerString)
if (formatType == 1) {
// console.log('copyHeaders function formatType 1', formatType)
if(client_units.length == 1){
// console.log('copyHeaders function client_units.length', client_units.length)
delete excel_headers[formatType][secondKey].unit;
}
var headerString = Object.values(excel_headers[formatType][secondKey]).join("\t"); // Using specified format type for copying headers
}
console.log('copyHeaders function converted headerString', headerString);
//console.log('headerString', headerString);
navigator.clipboard.writeText(headerString).then(function() {
toastr.success('Headers copied to clipboard', 'Success');
}, function(err) {
toastr.error(err, 'Could not copy headers:');
});
}
function generateTable(unique_id) {
// $('.loader').fadeIn();
// $('.loader-mask').fadeIn();
console.log('generateTable Function called : ', printCurrentTime());
var client_units = localStorage.getItem('client_units');
client_units = JSON.parse(client_units);
var data = $('#copied_excel_data').val();
let formatType = $('#grid').val();
var obj = $('#grid');
var secondKey = $('#si_or_bp').val() ?? '';
if (unique_id != 'na') {
data = $('#copied_excel_data_'+unique_id).val();
formatType = $('#grid_'+unique_id).val();
obj = $('#grid_'+unique_id);
}
console.log('generateTable obj', obj);
console.log('generateTable formatType', formatType);
// console.log('generateTable data');
// console.log(data)
console.log('generateTable secondKey', secondKey);
console.log('generateTable client_units', client_units);
if (!formatType || formatType == "") {
$('.excel_table_class').empty();
$('.excel_textarea').val('');
toastr.warning('Please select the Policy Premium Type', 'Warning');
return;
}
// Check if Excel data is empty
if (!data.trim()) {
toastr.warning('Excel data is empty.', 'Warning');
return;
}
var rows = data.split("\n");
//console.log('rows', rows)
// Filter out empty rows
rows = rows.filter(rowText => rowText.split("\t").some(cell => cell.trim()));
//console.log('rows', rows)
if (rows.length === 0) {
toastr.warning('All rows are empty after filtering.', 'Warning');
return;
}
var header = rows[0].split("\t");
// Determine the columns to keep (non-empty columns)
var columnsToKeep = [];
for (let i = 0; i < header.length; i++) {
if (rows.some(rowText => rowText.split("\t")[i].trim())) {
columnsToKeep.push(i);
}
}
// Filter header based on columns to keep
header = columnsToKeep.map(i => slugify(header[i]));
if (!excel_headers[formatType]) {
toastr.warning("Unknown format type. Please check the header columns.", 'Warning');
$('.excel_table_class').empty();
$('.excel_textarea').val('');
return;
}
var expectedHeader = Object.keys(excel_headers[formatType]);
//console.log('expectedHeader 1st', expectedHeader);
if (formatType == 1) {
// console.log('si_or_bp secondKey', secondKey)
expectedHeader = Object.keys(excel_headers[formatType][secondKey]);
}
// remove unit key from the array, if the client unit is one
if (client_units.length === 1) {
expectedHeader = expectedHeader.filter(key => key !== 'unit');
}
// console.log('expectedHeader', expectedHeader);
// console.log('HEADERS' , JSON.stringify(header));
// console.log('Excepted HEADERS' , JSON.stringify(expectedHeader));
// console.log('secondKey' , secondKey);
if (secondKey != 2 && secondKey != undefined) {
// console.log('expectedHeader' + secondKey + '-' + formatType);
console.log('expectedHeader', expectedHeader);
// console.log(excel_headers[formatType]);
if (JSON.stringify(header) !== JSON.stringify(expectedHeader)) {
var expectedHeaders;
if (formatType == 1 ) {
expectedHeaders = JSON.stringify(Object.values(excel_headers[formatType][secondKey]));
} else {
expectedHeaders = JSON.stringify(Object.values(excel_headers[formatType]));
}
const receivedHeaders = JSON.stringify(columnsToKeep.map(i => rows[0].split("\t")[i]));
console.log('receivedHeaders', receivedHeaders);
Swal.fire({
title: "Header Mismatch",
html: `
<p>Header columns do not match expected format:</p>
<p><strong>Expected:</strong> ${expectedHeaders}</p>
<p><strong>Received:</strong> ${receivedHeaders}</p>
`,
icon: "error"
});
$('.excel_table_class').empty();
$('.excel_textarea').val('');
return;
}
}
var table = $('<table data-custom-table-css="table" class="table table-striped compact-table" />');
var uniqueRows = new Set();
var emptyCellCount = 0;
rows.forEach((rowText, y) => {
var cells = rowText.split("\t").filter((_, i) => columnsToKeep.includes(i));
var row = $('<tr />');
// Skip empty rows after filtering columns
if (cells.every(cell => !cell.trim())) {
return;
}
cells.forEach(cellText => {
row.append('<td>' + cellText + '</td>');
if (!cellText.trim()) {
emptyCellCount++;
}
});
var si_or_bp = $('#si_or_bp').val();
var key;
switch (formatType) {
case 1:
if (si_or_bp == 1) {
if (client_units.length == 1) {
key = cells[0]; //Sum Insured
}else{
key = cells[0] + "|" + cells[1]; //Units, Sum Insured
}
} else if (si_or_bp == 2) {
if (client_units.length == 1) {
key = cells[0]; //Basic Pay
}else{
key = cells[0] + "|" + cells[1]; //Units, Basic Pay
}
} else if (si_or_bp == 3) {
if (client_units.length == 1) {
key = cells[0] + "|" + cells[1];//Band or Grade, Sum Insured
}else{
key = cells[0] + "|" + cells[1] + "|" + cells[2]; //Units, Band or Grade, Sum Insured
}
}
break;
case 2:
if (client_units.length == 1) {
key = cells[0]+ "|" + cells[1]; // Sum Insured, Premium
} else {
key = cells[0] + "|" + cells[1] + "|" + cells[2]; // Units, Sum Insured, Premium
}
break;
case 3:
if (client_units.length == 1) {
key = cells[0]+ "|" + cells[1]; // Sum Insured, Premium
} else {
key = cells[0] + "|" + cells[1]+ "|" + cells[2]; // Units, Sum Insured, Premium
}
break;
case 4:
if (client_units.length == 1) {
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[3]; // Sum Insured, From Age, To Age, Premium
} else {
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[3] + "|" + cells[4]; // Units, Sum Insured, From Age, To Age, Premium
}
break;
case 5:
if (client_units.length == 1) {
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[3]; // Sum Insured, From Age, To Age, Premium
} else {
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[3] + "|" + cells[4]; // Units Sum Insured, From Age, To Age, Premium
}
break;
case 6:
if (client_units.length == 1) {
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[3]; // Sum Insured, From Age, To Age, Premium
} else {
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[3] + "|" + cells[4]; //Units, Sum Insured, From Age, To Age, Premium
}
break;
case 7:
if (client_units.length == 1) {
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[3]; // Sum Insured, From Age, To Age, Premium
} else {
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[3] + "|" + cells[4]; // Units Sum Insured, From Age, To Age, Premium
}
break;
case 8:
if (client_units.length == 1) {
key = cells[0] + "|" + cells[1] + "|" + cells[2]; // Sum Insured, Grade, Premium
} else {
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[3]; // Units, Sum Insured, Grade, Premium
}
break;
case 9:
if (client_units.length == 1) {
key = cells[0] + "|" + cells[2]; // Sum Insured, Premium
} else {
key = cells[0] + "|" + cells[1] + "|" + cells[2]; //Units, Sum Insured, Premium+ "|" + cells[5]
}
break;
case 10:
if (client_units.length == 1) {
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[3]; // Sum Insured, From Age, To Age, Premium
} else {
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[3] + "|" + cells[4]; //Units, Sum Insured, From Age, To Age, Premium
}
break;
case 11:
if (client_units.length == 1) {
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[3]; // Sum Insured, Grade, Premium, Max SI
} else {
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[3] + "|" + cells[4]; // Units, Sum Insured, Grade, Premium, Max SI
}
break;
case 12:
if (client_units.length == 1) {
key = cells[0] + "|" + cells[1] + "|" + cells[2]; // Sum Insured, Relationship, Premium
} else {
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[3]; //Units Sum Insured, Relationship, Premium
}
break;
case 13:
if (client_units.length == 1) {
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[3] + "|" + cells[4] ; // Sum Insured, Relationship, From Age, To Age, Premium
} else {
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[3] + "|" + cells[4] + "|" + cells[5]; // Units, Sum Insured, Relationship, From Age, To Age, Premium
}
break;
default:
key = cells.join("|");
}
if (y > 0 && uniqueRows.has(key)) {
row.addClass('duplicate');
} else {
uniqueRows.add(key);
}
table.append(row);
});
if (unique_id != 'na') {
$('#excel_table_'+unique_id).empty();
$('#excel_table_'+unique_id).html(table);
}else{
$('#excel_table').empty();
$('#excel_table').html(table);
}
if(secondKey != 2 && secondKey != undefined){
// console.log(secondKey)
if ($('.duplicate').length > 0) {
//console.log('test');
toastr.warning("Duplicates found!", "Warning");
$('.excel_table_class').empty();
$('.excel_textarea').val('');
}
}
if (emptyCellCount > 0) {
toastr.warning('Number of empty cells: ' + emptyCellCount);
$('.excel_table_class').empty();
$('.excel_textarea').val('');
}
submitData(unique_id, client_units);
$('.excel_textarea').val('');
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
console.log('generateTable Function end : ', printCurrentTime());
}
function submitData(unique_id, client_units)
{
console.log('submitData Function called : ', printCurrentTime());
console.log('submitData function unique id', unique_id);
console.log('submitData function client_units', client_units);
console.log('submitData function client_units first index', client_units[0]);
let formatType = $('#grid').val();
var table = $('#excel_table table');
var obj = $('#grid');
var si_or_bp = $('#si_or_bp').val();
var basic_multiplier = $('#basic_multiplier').val();
var premium_multiplier = $('#premium_multiplier').val();
var gpa_sum_multiplier2 = $('#gpa_sum_multiplier2').val();
var gpa_sum_multiplier = $('#gpa_sum_multiplier').val();
if (unique_id != 'na') {
formatType = $('#grid_'+unique_id).val();
table = $('#excel_table_'+unique_id+' table');
obj = $('#grid_'+unique_id);
}
var headers = $(table).find('tr').first().find('td').map(function() {
return slugify($(this).text());
}).get();
var rows = $(table).find('tr:gt(0)').map(function() {
return $(this).find('td').map(function() {
return $(this).text();
}).get();
}).get();
var jsonData = [];
$(table).find('tr:gt(0)').each(function(idx) {
var row = $(this).find('td').map(function() {
return $(this).text();
}).get();
var rowData = {};
row.forEach((cell, colIdx) => {
rowData[headers[colIdx]] = cell;
});
jsonData.push(rowData);
});
//console.log(jsonData);
jsonData.forEach(obj => {
// console.log('obj', obj)
// console.log('length of the obj', obj.length)
if ('sum_insured' in obj) {
obj.si = obj.sum_insured;
delete obj.sum_insured;
}
if ('from_age' in obj) {
obj.age_from = obj.from_age;
delete obj.from_age;
}
if ('to_age' in obj) {
obj.age_to = obj.to_age;
delete obj.to_age;
}
if ('band_or_grade' in obj) {
obj.grade = obj.band_or_grade;
delete obj.band_or_grade;
}
if (client_units.length == 1) {
if (!('unit' in obj)) {
obj.unit = client_units[0];
}
}
if (formatType == 1) {
if (si_or_bp == 1) {
obj.si_or_bp = si_or_bp;
obj.multiplier = gpa_sum_multiplier ? gpa_sum_multiplier : 0;
if( obj.multiplier == 0){
obj.premium = 0;
}else{
obj.premium = obj.si * obj.multiplier / 1000
}
} else if (si_or_bp == 2) {
obj.si_or_bp = si_or_bp;
obj.basic_multiplier = basic_multiplier ? basic_multiplier : 0;
obj.multiplier = premium_multiplier ? premium_multiplier : 0;
obj.si = obj.basic_pay * obj.basic_multiplier
obj.premium = obj.si * obj.multiplier / 1000
} else if (si_or_bp == 3) {
obj.si_or_bp = si_or_bp;
obj.multiplier = gpa_sum_multiplier2 ? gpa_sum_multiplier2 : 0;
if( obj.multiplier == 0){
obj.premium = 0;
}else{
obj.premium = obj.si * obj.multiplier / 1000
}
}
}
});
console.log(jsonData);
// console.log('formatType', formatType);
if (unique_id != 'na') {
$('#grid_content_input_for_additional_'+unique_id).empty()
if ($('#copyfromexcel_'+unique_id).text() == "Manual entry") {
$('#copyfromexcel_'+unique_id).text("Copy from excel")
} else {
$('#copyfromexcel_'+unique_id).text("Copy from excel");
}
$('#grid_content_input_for_additional_'+unique_id).toggle();
$('#grid_content_from_excel_'+unique_id).toggle();
} else {
$('#grid_content_input').empty()
if ($('#copyfromexcel').text() == "Manual entry") {
$('#copyfromexcel').text("Copy from excel")
} else {
$('#copyfromexcel').text("Copy from excel");
}
$('#grid_content_input').toggle();
$('#grid_content_from_excel').toggle();
}
if(unique_id == 'na'){
unique_id = null;
}
if (formatType == 1 || formatType == 2 || formatType == 4 || formatType == 6 || formatType == 9) {
var FirstIndex = jsonData[0]
addGridHTML(false, FirstIndex, formatType, si_or_bp, unique_id);
jsonData.shift();
}
$.each(jsonData, function(index, item) {
appendGridtHtml(formatType, unique_id, item);
});
$('input[name="11_max_si[]"]').each(function() {
$(this).trigger('keyup');
});
$(`input[name="${formatType}_si[]"]`).each(function() {
$(this).trigger('keyup');
$(this).trigger('change').keyup();
});
$(`input[name="${formatType}_premium[]"]`).each(function() {
$(this).trigger('keyup');
$(this).trigger('change').keyup();
});
$(`input[name="basic_pay[]"]`).each(function() {
$(this).trigger('keyup');
$(this).trigger('change').keyup();
});
$(`input[name="gpa_basic_si[]"]`).each(function() {
$(this).trigger('keyup');
$(this).trigger('change').keyup();
});
$(`input[name="gpa_basic_premium[]"]`).each(function() {
$(this).trigger('keyup');
$(this).trigger('change').keyup();
});
$(`input[name="gpa_sum_si2[]"]`).each(function() {
$(this).trigger('keyup');
$(this).trigger('change').keyup();
});
$(`input[name="gpa_sum_si[]"]`).each(function() {
$(this).trigger('keyup');
$(this).trigger('change').keyup();
});
$(`input[name="gpa_premium"]`).each(function() {
$(this).trigger('keyup');
$(this).trigger('change').keyup();
});
$(`input[name="gpa_si"]`).each(function() {
$(this).trigger('keyup');
$(this).trigger('change').keyup();
});
$(`input[name="gpa_premium29[]"]`).each(function() {
$(this).trigger('keyup');
$(this).trigger('change').keyup();
});
$(`input[name="gpa_si29[]"]`).each(function() {
$(this).trigger('keyup');
$(this).trigger('change').keyup();
});
console.log('submitData Function end : ', printCurrentTime());
}
</script>