36293-vm/assets/js/project_details.js
Flatlogic Bot c76337f9a8 over5
2025-11-26 17:09:25 +00:00

82 lines
3.5 KiB
JavaScript

document.addEventListener('DOMContentLoaded', function () {
const overrideBtn = document.getElementById('override-btn');
if (overrideBtn) {
overrideBtn.addEventListener('click', function () {
const projectId = this.dataset.projectId;
const month = this.dataset.month;
if (this.textContent.trim() === 'Override') {
this.textContent = 'Save';
this.classList.remove('btn-warning');
this.classList.add('btn-success');
makeEditable('WIP', month);
makeEditable('Opening-Balance', month);
makeEditable('Billings', month);
makeEditable('Expenses', month);
} else if (this.textContent.trim() === 'Save') {
const wip = document.getElementById(`wip-${month}-input`).value;
const openingBalance = document.getElementById(`opening-balance-${month}-input`).value;
const billings = document.getElementById(`billings-${month}-input`).value;
const expenses = document.getElementById(`expenses-${month}-input`).value;
const formData = new FormData();
formData.append('projectId', projectId);
formData.append('month', month);
formData.append('wip', wip);
formData.append('openingBalance', openingBalance);
formData.append('billings', billings);
formData.append('expenses', expenses);
fetch('save_override.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
this.textContent = 'Overridden';
this.classList.remove('btn-success');
this.classList.add('btn-secondary');
this.disabled = true;
updateCell('WIP', month, wip);
updateCell('Opening-Balance', month, openingBalance);
updateCell('Billings', month, billings);
updateCell('Expenses', month, expenses);
} else {
alert('Error saving override: ' + data.error);
}
})
.catch(error => {
console.error('Error:', error);
alert('An unexpected error occurred.');
});
}
});
}
function makeEditable(metric, month) {
const cell = document.getElementById(`${metric.toLowerCase().replace(/\s/g, '-')}-${month}`);
if (cell) {
let value = cell.textContent.replace(/€/g, '').replace(/,/g, '');
let numericValue = parseFloat(value);
if (isNaN(numericValue)) {
numericValue = 0;
}
cell.innerHTML = `<input type="number" id="${metric.toLowerCase().replace(/\s/g, '-')}-${month}-input" class="form-control" value="${numericValue.toFixed(2)}">`;
}
}
function updateCell(metric, month, value) {
const cell = document.getElementById(`${metric.toLowerCase().replace(/\s/g, '-')}-${month}`);
if (cell) {
const numericValue = value === '' ? 0 : parseFloat(value);
cell.innerHTML = `${numericValue.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
}
}
});