nhance/app/Views/policy_grid_excel.php

572 lines
16 KiB
PHP

<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: {
"sum_insured": "Sum Insured",
},
2: {
"basic_pay": "Basic Pay",
},
3: {
"band_or_grade": "Band or Grade",
"sum_insured": "Sum Insured",
}
},
2: {
"sum_insured": "Sum Insured",
"premium": "Premium"
},
3: {
"sum_insured": "Sum Insured",
"premium": "Premium"
},
4: {
"sum_insured": "Sum Insured",
"from_age": "From Age",
"to_age": "To Age",
"premium": "Premium"
},
5: {
"sum_insured": "Sum Insured",
"from_age": "From Age",
"to_age": "To Age",
"premium": "Premium"
},
6: {
"sum_insured": "Sum Insured",
"from_age": "From Age",
"to_age": "To Age",
"premium": "Premium"
},
7: {
"sum_insured": "Sum Insured",
"from_age": "From Age",
"to_age": "To Age",
"premium": "Premium"
},
8: {
"sum_insured": "Sum Insured",
"grade": "Grade",
"premium": "Premium"
},
9: {
"sum_insured": "Sum Insured",
"premium": "Premium"
},
10: {
"sum_insured": "Sum Insured",
"from_age": "From Age",
"to_age": "To Age",
"premium": "Premium"
},
11: {
"sum_insured": "Sum Insured",
"grade": "Grade",
"premium": "Premium",
"max_si": "Max Si"
},
12: {
"sum_insured": "Sum Insured",
"relationship": "Relationship",
"premium": "Premium"
},
13: {
"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(rack_rate_type) {
let formatType = $('#grid').val();
var secondKey = $('#si_or_bp').val();
var obj = $('#grid');
if (rack_rate_type == 1) {
formatType = $('#additional_grid').val();
obj = $('#additional_grid');
}
if (formatType == 1) {
if (secondKey == "") {
toastr.warning('Please select the Basic Pay, Sum Insured or Band/Grade', 'Warning');
return;
}
}
// //console.log(obj)
// //console.log(formatType)
// //console.log(rack_rate_type)
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;
}
var headerString = Object.values(excel_headers[formatType]).join(
"\t"); // Using specified format type for copying headers
if (formatType == 1) {
var headerString = Object.values(excel_headers[formatType][secondKey]).join(
"\t"); // Using specified format type for copying headers
}
//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(rack_rate_type) {
var data = $('#copied_excel_data').val();
let formatType = $('#grid').val();
var obj = $('#grid');
var secondKey = $('#si_or_bp').val();
if (rack_rate_type == 1) {
data = $('#additional_copied_excel_data').val();
formatType = $('#additional_grid').val();
obj = $('#additional_grid');
}
//console.log(obj);
//console.log(formatType);
//console.log(data);
console.log(secondKey);
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");
console
// 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]);
}
//console.log('expectedHeader', expectedHeader);
//console.log('HEADERS' , JSON.stringify(header))
//console.log('Excepted HEADERS' , JSON.stringify(expectedHeader))
if (secondKey != 2) {
if (JSON.stringify(header) !== JSON.stringify(expectedHeader)) {
const expectedHeaders = JSON.stringify(Object.values(excel_headers[formatType][secondKey]));
const receivedHeaders = JSON.stringify(columnsToKeep.map(i => rows[0].split("\t")[i]));
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 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) {
key = cells[0] // Sum Insured,
} else if (si_or_bp == 2) {
key = cells[0] //Basic Pay
} else if (si_or_bp == 3) {
key = cells[0] + "|" + cells[1] //Band or Grade, Sum Insured
}
break;
case 2:
key = cells[0] + "|" + cells[1] // Sum Insured, Premium
break;
case 3:
key = cells[0] + "|" + cells[1] // Sum Insured, Premium
break;
case 4:
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[
3]; // Sum Insured, From Age, To Age, Premium
break;
case 5:
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[
3]; // Sum Insured, From Age, To Age, Premium
break;
case 6:
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[
3]; // Sum Insured, From Age, To Age, Premium
break;
case 7:
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[
3]; // Sum Insured, From Age, To Age, Premium
break;
case 8:
key = cells[0] + "|" + cells[1] + "|" + cells[2]; // Sum Insured, Grade, Premium
break;
case 9:
key = cells[0] + "|" + cells[1]; // Sum Insured, Premium
break;
case 10:
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[
3]; // Sum Insured, From Age, To Age, Premium
break;
case 11:
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[
3]; // Sum Insured, Grade, Premium, Max SI
break;
case 12:
key = cells[0] + "|" + cells[1] + "|" + cells[2]; // Sum Insured, Relationship, Premium
break;
case 13:
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[3] + "|" + cells[
4]; // 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 (rack_rate_type == 1) {
$('#additional_excel_table').empty();
$('#additional_excel_table').html(table);
} else if (rack_rate_type == 0) {
$('#excel_table').empty();
$('#excel_table').html(table);
}
if(secondKey != 2){
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(rack_rate_type);
$('.excel_textarea').val('');
}
function submitData(rack_rate_type) {
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 (rack_rate_type == 1) {
formatType = $('#additional_grid').val();
table = $('#additional_excel_table table');
obj = $('#additional_grid');
}
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 (formatType == 1) {
if (si_or_bp == 1) {
obj.si_or_bp = si_or_bp;
obj.multiplier = gpa_sum_multiplier ? gpa_sum_multiplier : 0;
} 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;
}
}
});
console.log(jsonData);
console.log('formatType', formatType);
if (rack_rate_type == 1) {
$('#additional_grid_content_input').empty()
if ($('#copyfromexcelforadditional').text() == "Manual entry") {
$('#copyfromexcelforadditional').text("Copy from excel")
} else {
$('#copyfromexcelforadditional').text("Copy from excel");
}
$('#additional_grid_content_input').toggle();
$('#additional_grid_content_from_excel').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 (formatType == 1 || formatType == 2 || formatType == 4 || formatType == 6) {
var FirstIndex = jsonData[0]
addGridHTML(false, FirstIndex, formatType, si_or_bp, 0)
jsonData.shift();
}
$.each(jsonData, function(index, item) {
appendGridtHtml(formatType, rack_rate_type, item)
});
$('input[name="11_max_si[]"]').each(function() {
$(this).trigger('keyup');
});
$(`input[name="${formatType}_si[]"]`).each(function() {
// //console.log($(this));
$(this).trigger('keyup');
$(this).trigger('change').keyup();
});
$(`input[name="${formatType}_premium[]"]`).each(function() {
// //console.log($(this));
$(this).trigger('keyup');
$(this).trigger('change').keyup();
});
$(`input[name="basic_pay[]"]`).each(function() {
// //console.log($(this));
$(this).trigger('keyup');
$(this).trigger('change').keyup();
});
$(`input[name="gpa_basic_si[]"]`).each(function() {
// //console.log($(this));
$(this).trigger('keyup');
$(this).trigger('change').keyup();
});
$(`input[name="gpa_basic_premium[]"]`).each(function() {
// //console.log($(this));
$(this).trigger('keyup');
$(this).trigger('change').keyup();
});
}
</script>