update
This commit is contained in:
31
server.js
31
server.js
@@ -403,12 +403,19 @@ app.delete('/api/quotes/:id', async (req, res) => {
|
||||
app.get('/api/invoices', async (req, res) => {
|
||||
try {
|
||||
const result = await pool.query(`
|
||||
SELECT i.*, c.name as customer_name, c.qbo_id as customer_qbo_id
|
||||
SELECT i.*, c.name as customer_name, c.qbo_id as customer_qbo_id,
|
||||
COALESCE((SELECT SUM(pi.amount) FROM payment_invoices pi WHERE pi.invoice_id = i.id), 0) as amount_paid
|
||||
FROM invoices i
|
||||
LEFT JOIN customers c ON i.customer_id = c.id
|
||||
ORDER BY i.created_at DESC
|
||||
`);
|
||||
res.json(result.rows);
|
||||
// balance berechnen
|
||||
const rows = result.rows.map(r => ({
|
||||
...r,
|
||||
amount_paid: parseFloat(r.amount_paid) || 0,
|
||||
balance: (parseFloat(r.total) || 0) - (parseFloat(r.amount_paid) || 0)
|
||||
}));
|
||||
res.json(rows);
|
||||
} catch (error) {
|
||||
console.error('Error fetching invoices:', error);
|
||||
res.status(500).json({ error: 'Error fetching invoices' });
|
||||
@@ -429,25 +436,30 @@ app.get('/api/invoices/next-number', async (req, res) => {
|
||||
app.get('/api/invoices/:id', async (req, res) => {
|
||||
const { id } = req.params;
|
||||
try {
|
||||
// KORRIGIERT: c.line1, c.line2, c.line3, c.line4 statt c.street
|
||||
const invoiceResult = await pool.query(`
|
||||
SELECT i.*, c.name as customer_name, c.line1, c.line2, c.line3, c.line4, c.city, c.state, c.zip_code, c.account_number
|
||||
SELECT i.*, c.name as customer_name, c.qbo_id as customer_qbo_id,
|
||||
c.line1, c.line2, c.line3, c.line4, c.city, c.state, c.zip_code, c.account_number,
|
||||
COALESCE((SELECT SUM(pi.amount) FROM payment_invoices pi WHERE pi.invoice_id = i.id), 0) as amount_paid
|
||||
FROM invoices i
|
||||
LEFT JOIN customers c ON i.customer_id = c.id
|
||||
WHERE i.id = $1
|
||||
`, [id]);
|
||||
|
||||
|
||||
if (invoiceResult.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'Invoice not found' });
|
||||
}
|
||||
|
||||
|
||||
const invoice = invoiceResult.rows[0];
|
||||
invoice.amount_paid = parseFloat(invoice.amount_paid) || 0;
|
||||
invoice.balance = (parseFloat(invoice.total) || 0) - invoice.amount_paid;
|
||||
|
||||
const itemsResult = await pool.query(
|
||||
'SELECT * FROM invoice_items WHERE invoice_id = $1 ORDER BY item_order',
|
||||
[id]
|
||||
);
|
||||
|
||||
|
||||
res.json({
|
||||
invoice: invoiceResult.rows[0],
|
||||
invoice: invoice,
|
||||
items: itemsResult.rows
|
||||
});
|
||||
} catch (error) {
|
||||
@@ -456,6 +468,9 @@ app.get('/api/invoices/:id', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
app.post('/api/invoices', async (req, res) => {
|
||||
const { invoice_number, customer_id, invoice_date, terms, auth_code, tax_exempt, items, created_from_quote_id, scheduled_send_date } = req.body;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user