This commit is contained in:
2026-02-24 16:35:03 -06:00
parent be834fa9a0
commit 29a37ad98a
4 changed files with 311 additions and 108 deletions

View File

@@ -940,61 +940,65 @@ function getInvoiceItems() {
async function handleInvoiceSubmit(e) {
e.preventDefault();
const items = getInvoiceItems();
if (items.length === 0) {
alert('Please add at least one item');
return;
}
const invoiceNumber = document.getElementById('invoice-number').value.trim();
// Invoice Number ist jetzt OPTIONAL
// Wenn angegeben, muss sie numerisch sein
if (invoiceNumber && !/^\d+$/.test(invoiceNumber)) {
alert('Invalid invoice number. Must be a numeric value or left empty.');
return;
}
const data = {
invoice_number: invoiceNumber || null, // null wenn leer
customer_id: parseInt(document.getElementById('invoice-customer').value),
invoice_number: document.getElementById('invoice-number').value || null,
customer_id: document.getElementById('invoice-customer').value,
invoice_date: document.getElementById('invoice-date').value,
terms: document.getElementById('invoice-terms').value,
auth_code: document.getElementById('invoice-authorization').value,
tax_exempt: document.getElementById('invoice-tax-exempt').checked,
scheduled_send_date: document.getElementById('invoice-send-date').value || null,
items: items
scheduled_send_date: document.getElementById('invoice-send-date')?.value || null,
items: getInvoiceItems() // Deine bestehende Funktion
};
if (!data.customer_id) {
alert('Please select a customer.');
return;
}
if (!data.items || data.items.length === 0) {
alert('Please add at least one item.');
return;
}
const invoiceId = currentInvoiceId;
const url = invoiceId ? `/api/invoices/${invoiceId}` : '/api/invoices';
const method = invoiceId ? 'PUT' : 'POST';
// Spinner anzeigen
showSpinner(invoiceId ? 'Saving invoice & syncing QBO...' : 'Creating invoice & exporting to QBO...');
try {
let response;
if (currentInvoiceId) {
response = await fetch(`/api/invoices/${currentInvoiceId}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
} else {
response = await fetch('/api/invoices', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
}
const response = await fetch(url, {
method,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
const result = await response.json();
if (response.ok) {
closeInvoiceModal();
loadInvoices();
// Info über QBO Status
if (result.qbo_doc_number) {
console.log(`✅ Invoice saved & exported to QBO: #${result.qbo_doc_number}`);
} else if (result.qbo_synced) {
console.log('✅ Invoice saved & synced to QBO');
} else {
console.log('✅ Invoice saved locally (QBO sync pending)');
}
// Invoices neu laden
if (window.invoiceView) window.invoiceView.loadInvoices();
} else {
alert(result.error || 'Error saving invoice');
alert(`Error: ${result.error}`);
}
} catch (error) {
console.error('Error:', error);
alert('Error saving invoice');
} finally {
hideSpinner();
}
}