nhance/app/Views/policy_grid_excel.php

412 lines
12 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 = {
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 obj = $('#grid');
if(rack_rate_type == 1){
formatType = $('#additional_grid').val();
obj = $('#additional_grid');
}
console.log(obj)
console.log(formatType)
if(!formatType && formatType == ""){
toastr.warning('Please select the Policy Premium Type', 'Warning');
return;
}
const headerString = Object.values(excel_headers[formatType]).join("\t"); // Using specified format type for copying headers
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');
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);
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");
// Filter out empty rows
rows = rows.filter(rowText => rowText.split("\t").some(cell => cell.trim()));
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]);
if (JSON.stringify(header) !== JSON.stringify(expectedHeader)) {
const expectedHeaders = JSON.stringify(Object.values(excel_headers[formatType]));
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 key;
switch (formatType) {
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 ($('.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);
}
function submitData(rack_rate_type) {
let formatType = $('#grid').val();
var table = $('#excel_table table');
var obj = $('#grid');
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 => {
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;
}
});
console.log(jsonData);
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();
}
$.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');
});
$(`input[name="${formatType}_premium[]"]`).each(function() {
// console.log($(this));
$(this).trigger('keyup');
});
}
</script>