capex-commander / script.js
HDHaro's picture
generate Capex sheet with a downloadable excel sheet, complete with a dashboard tab, displaying all the metrics from the main sheet
efaacf1 verified
Raw History Blame Contribute Delete
8.05 kB
document.addEventListener('DOMContentLoaded', function() {
// Initialize Feather Icons
feather.replace();
// Initialize Charts
const categoryCtx = document.getElementById('categoryChart').getContext('2d');
const statusCtx = document.getElementById('statusChart').getContext('2d');
let categoryChart = new Chart(categoryCtx, {
type: 'doughnut',
data: {
labels: ['Facilities', 'Technology', 'Equipment', 'Vehicles'],
datasets: [{
data: [12500, 0, 0, 0],
backgroundColor: [
'#3B82F6',
'#10B981',
'#F59E0B',
'#EF4444'
],
borderWidth: 0
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
cutout: '70%',
plugins: {
legend: {
position: 'bottom',
labels: {
boxWidth: 12,
padding: 20
}
}
}
}
});
let statusChart = new Chart(statusCtx, {
type: 'bar',
data: {
labels: ['Planned', 'Approved', 'In Progress', 'Completed'],
datasets: [{
label: 'Amount',
data: [12500, 0, 0, 0],
backgroundColor: '#3B82F6',
borderRadius: 4
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
grid: {
display: false
}
},
x: {
grid: {
display: false
}
}
},
plugins: {
legend: {
display: false
}
}
}
});
// Add row functionality
document.getElementById('addRowBtn').addEventListener('click', function() {
const tbody = document.querySelector('#capexTable tbody');
const newRow = document.createElement('tr');
newRow.innerHTML = `
<td class="px-6 py-4 whitespace-nowrap">
<input type="text" class="w-full border-0 focus:ring-2 focus:ring-blue-500 rounded" value="">
</td>
<td class="px-6 py-4 whitespace-nowrap">
<select class="w-full border-0 focus:ring-2 focus:ring-blue-500 rounded bg-transparent">
<option>Facilities</option>
<option>Technology</option>
<option>Equipment</option>
<option>Vehicles</option>
</select>
</td>
<td class="px-6 py-4">
<input type="text" class="w-full border-0 focus:ring-2 focus:ring-blue-500 rounded" value="">
</td>
<td class="px-6 py-4 whitespace-nowrap">
<input type="number" class="w-full border-0 focus:ring-2 focus:ring-blue-500 rounded" value="0">
</td>
<td class="px-6 py-4 whitespace-nowrap">
<input type="date" class="w-full border-0 focus:ring-2 focus:ring-blue-500 rounded" value="${new Date().toISOString().split('T')[0]}">
</td>
<td class="px-6 py-4 whitespace-nowrap">
<select class="w-full border-0 focus:ring-2 focus:ring-blue-500 rounded bg-transparent">
<option>Planned</option>
<option>Approved</option>
<option>In Progress</option>
<option>Completed</option>
</select>
</td>
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
<button class="text-red-500 hover:text-red-700 delete-row">
<i data-feather="trash-2"></i>
</button>
</td>
`;
tbody.appendChild(newRow);
feather.replace();
});
// Delete row functionality
document.addEventListener('click', function(e) {
if (e.target.closest('.delete-row')) {
const row = e.target.closest('tr');
row.remove();
updateDashboard();
}
});
// Update dashboard when inputs change
document.addEventListener('input', function(e) {
if (e.target.tagName === 'INPUT' || e.target.tagName === 'SELECT') {
updateDashboard();
}
});
// Export to Excel functionality
document.getElementById('exportBtn').addEventListener('click', exportToExcel);
// Function to update dashboard metrics
function updateDashboard() {
const rows = document.querySelectorAll('#capexTable tbody tr');
let total = 0;
const categories = {
'Facilities': 0,
'Technology': 0,
'Equipment': 0,
'Vehicles': 0
};
const statuses = {
'Planned': 0,
'Approved': 0,
'In Progress': 0,
'Completed': 0
};
rows.forEach(row => {
const amount = parseFloat(row.querySelector('td:nth-child(4) input').value) || 0;
const category = row.querySelector('td:nth-child(2) select').value;
const status = row.querySelector('td:nth-child(6) select').value;
total += amount;
categories[category] += amount;
statuses[status] += amount;
});
// Update total
document.getElementById('totalCapex').textContent = '$' + total.toLocaleString();
// Update category chart
categoryChart.data.datasets[0].data = Object.values(categories);
categoryChart.update();
// Update status chart
statusChart.data.datasets[0].data = Object.values(statuses);
statusChart.update();
}
// Function to export data to Excel
function exportToExcel() {
const wb = XLSX.utils.book_new();
// Create data sheet
const dataRows = [];
const headers = ['Project', 'Category', 'Description', 'Amount', 'Date', 'Status'];
dataRows.push(headers);
document.querySelectorAll('#capexTable tbody tr').forEach(row => {
const rowData = [
row.querySelector('td:nth-child(1) input').value,
row.querySelector('td:nth-child(2) select').value,
row.querySelector('td:nth-child(3) input').value,
parseFloat(row.querySelector('td:nth-child(4) input').value) || 0,
row.querySelector('td:nth-child(5) input').value,
row.querySelector('td:nth-child(6) select').value
];
dataRows.push(rowData);
});
const dataWs = XLSX.utils.aoa_to_sheet(dataRows);
XLSX.utils.book_append_sheet(wb, dataWs, "Capex Data");
// Create dashboard sheet
const dashboardData = [
['Metric', 'Value'],
['Total Capex', parseFloat(document.getElementById('totalCapex').textContent.replace(/[^0-9.-]+/g,""))],
...Object.entries(categoryChart.data.datasets[0].data).map(([i, value]) =>
[`${categoryChart.data.labels[i]} Capex`, value]
),
...Object.entries(statusChart.data.datasets[0].data).map(([i, value]) =>
[`${statusChart.data.labels[i]} Capex`, value]
)
];
const dashboardWs = XLSX.utils.aoa_to_sheet(dashboardData);
XLSX.utils.book_append_sheet(wb, dashboardWs, "Dashboard");
// Export the workbook
XLSX.writeFile(wb, 'capex_report.xlsx');
}
// Initial dashboard update
updateDashboard();
});