64 lines
2.2 KiB
JavaScript
64 lines
2.2 KiB
JavaScript
function initBillingPage(projectId) {
|
|
const saveBtn = document.getElementById('save-billing');
|
|
const billingAmountInputs = document.querySelectorAll('.billing-amount');
|
|
const totalBillingCell = document.getElementById('total-billing');
|
|
|
|
function parseEuroNumber(str) {
|
|
if (!str) return 0;
|
|
// Remove thousand separators (.) and replace decimal comma (,) with a period (.)
|
|
const cleanedStr = str.toString().replace(/\./g, '').replace(',', '.');
|
|
return parseFloat(cleanedStr);
|
|
}
|
|
|
|
function formatToEuro(num) {
|
|
return num.toLocaleString('de-DE', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
|
|
}
|
|
|
|
function updateTotalBilling() {
|
|
let total = 0;
|
|
billingAmountInputs.forEach(input => {
|
|
total += parseEuroNumber(input.value) || 0;
|
|
});
|
|
totalBillingCell.textContent = formatToEuro(total);
|
|
}
|
|
|
|
billingAmountInputs.forEach(input => {
|
|
input.addEventListener('input', updateTotalBilling); // Use 'input' for better response
|
|
});
|
|
|
|
if (saveBtn) {
|
|
saveBtn.addEventListener('click', function() {
|
|
const billingData = [];
|
|
billingAmountInputs.forEach(input => {
|
|
billingData.push({
|
|
month: input.dataset.month,
|
|
amount: parseEuroNumber(input.value) // Send the parsed number
|
|
});
|
|
});
|
|
|
|
const formData = new FormData();
|
|
formData.append('projectId', projectId);
|
|
formData.append('billingData', JSON.stringify(billingData));
|
|
|
|
fetch('save_billing.php', {
|
|
method: 'POST',
|
|
body: formData
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
alert('Billing data saved successfully!');
|
|
} else {
|
|
alert('Error saving billing data: ' + data.error);
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error:', error);
|
|
alert('An unexpected error occurred.');
|
|
});
|
|
});
|
|
}
|
|
|
|
// Initial calculation
|
|
updateTotalBilling();
|
|
} |