nhance/app/Views/policy_grid_excel.php
2024-05-27 17:38:12 +05:30

242 lines
8.0 KiB
PHP

<style>
table {
border-collapse: collapse;
width: 100%;
}
td, th {
padding: 0.2rem; /* Adjust padding as needed */
}
.error {
background-color: #f8d7da;
}
.duplicate {
background-color: #fff3cd;
}
.container {
margin-top: 20px;
}
.compact-table td, .compact-table th {
padding: 0.1rem; /* Adjust padding as needed */
}
</style>
<div>
<!-- <div> -->
<p>Paste excel data here: <a href="#" type="button" onclick="copyHeaders()">Copy Excel Headers</a></p>
<textarea id="copied_excel_data" name="copied_excel_data" style="width:100%;height:200px;" oninput="generateTable()"></textarea><br>
<!-- </div> -->
<hr>
<div id="excel_table"></div>
<hr>
<div>
<input type="button" id="submitBtn" onclick="submitData()" value="Submit" disabled/>
<br><br>
<p>JSON output:</p>
<div id="json_output"></div>
</div>
<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() {
const headerString = Object.values(excel_headers[formatType]).join("\t"); // Using specified format type for copying headers
navigator.clipboard.writeText(headerString).then(function() {
alert('Headers copied to clipboard');
}, function(err) {
alert('Could not copy headers: ', err);
});
}
function generateTable() {
var data = $('#copied_excel_data').val();
const formatType = $('#grid').val();
alert(formatType);
// Check if Excel data is empty
if (!data.trim()) {
alert('Excel data is empty.');
return;
}
var rows = data.split("\n");
var table = $('<table class="table table-striped compact-table" />');
var header = rows[0].split("\t").map(cell => slugify(cell));
if (!excel_headers[formatType]) {
alert("Unknown format type. Please check the header columns.");
$('#submitBtn').prop('disabled', true);
$('#excel_table').empty();
return;
}
var expectedHeader = Object.keys(excel_headers[formatType]);
if (JSON.stringify(header) !== JSON.stringify(expectedHeader)) {
alert("Header columns do not match expected format:\nExpected: " + JSON.stringify(Object.values(excel_headers[formatType])) + "\nReceived: " + JSON.stringify(rows[0].split("\t")));
$('#submitBtn').prop('disabled', true);
$('#excel_table').empty();
return;
}
var uniqueRows = new Set();
var emptyCellCount = 0;
rows.forEach((rowText, y) => {
var cells = rowText.split("\t");
// Skip empty rows
if (cells.every(cell => !cell.trim())) {
return;
}
var row = $('<tr />');
cells.forEach(cellText => {
row.append('<td>'+cellText+'</td>');
if (!cellText.trim()) {
emptyCellCount++;
}
});
var key;
switch(formatType) {
case 1:
key = cells[0] + "|" + cells[1] + "|" + cells[2]; // Sum Insured, From Age, To Age
break;
case 2:
key = cells[0] + "|" + cells[1]; // Sum Insured, Grade
break;
case 3:
key = cells[0] + "|" + cells[1] + "|" + cells[2] + "|" + cells[3]; // Sum Insured, Relationship, From Age, To Age
break;
case 4:
key = cells[0]; // Sum Insured
break;
case 5:
key = cells[0] + "|" + cells[1]; // Sum Insured, Grade
break;
default:
key = cells.join("|");
}
if (y > 0 && uniqueRows.has(key)) {
row.addClass('duplicate');
} else {
uniqueRows.add(key);
}
table.append(row);
});
$('#submitBtn').prop('disabled', $('.duplicate').length > 0);
$('#excel_table').html(table);
if ($('.duplicate').length > 0) {
alert("Duplicate records found.");
}
alert('Number of empty cells: ' + emptyCellCount);
submitData();
}
function submitData() {
$('#json_output').empty(); // Clear existing JSON output
var table = $('#excel_table table');
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);
});
$('#json_output').text(JSON.stringify(jsonData, null, 2));
}
</script>
</div>