sdf
This commit is contained in:
parent
711ed1e3cf
commit
c8522af331
33
server.js
33
server.js
|
|
@ -283,6 +283,7 @@ app.put('/api/quotes/:id', async (req, res) => {
|
||||||
const client = await pool.connect();
|
const client = await pool.connect();
|
||||||
try {
|
try {
|
||||||
await client.query('BEGIN');
|
await client.query('BEGIN');
|
||||||
|
console.log('Transaction started');
|
||||||
|
|
||||||
const { customer_id, quote_date, tax_exempt, items, tbd_note } = req.body;
|
const { customer_id, quote_date, tax_exempt, items, tbd_note } = req.body;
|
||||||
|
|
||||||
|
|
@ -303,7 +304,10 @@ app.put('/api/quotes/:id', async (req, res) => {
|
||||||
const tax_amount = tax_exempt ? 0 : (subtotal * tax_rate / 100);
|
const tax_amount = tax_exempt ? 0 : (subtotal * tax_rate / 100);
|
||||||
const total = subtotal + tax_amount;
|
const total = subtotal + tax_amount;
|
||||||
|
|
||||||
|
console.log('Calculated totals:', { subtotal, tax_amount, total, has_tbd });
|
||||||
|
|
||||||
// Update quote
|
// Update quote
|
||||||
|
console.log('Updating quote with id:', req.params.id);
|
||||||
const quoteResult = await client.query(
|
const quoteResult = await client.query(
|
||||||
`UPDATE quotes
|
`UPDATE quotes
|
||||||
SET customer_id = $1, quote_date = $2, tax_exempt = $3,
|
SET customer_id = $1, quote_date = $2, tax_exempt = $3,
|
||||||
|
|
@ -315,15 +319,20 @@ app.put('/api/quotes/:id', async (req, res) => {
|
||||||
subtotal, tax_amount, total, has_tbd, tbd_note, req.params.id]
|
subtotal, tax_amount, total, has_tbd, tbd_note, req.params.id]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
console.log('Quote updated, rows affected:', quoteResult.rows.length);
|
||||||
|
|
||||||
if (quoteResult.rows.length === 0) {
|
if (quoteResult.rows.length === 0) {
|
||||||
await client.query('ROLLBACK');
|
await client.query('ROLLBACK');
|
||||||
|
console.log('Quote not found, rolling back');
|
||||||
return res.status(404).json({ error: 'Quote not found' });
|
return res.status(404).json({ error: 'Quote not found' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete old items
|
// Delete old items
|
||||||
|
console.log('Deleting old items');
|
||||||
await client.query('DELETE FROM quote_items WHERE quote_id = $1', [req.params.id]);
|
await client.query('DELETE FROM quote_items WHERE quote_id = $1', [req.params.id]);
|
||||||
|
|
||||||
// Insert new items
|
// Insert new items
|
||||||
|
console.log('Inserting', items.length, 'new items');
|
||||||
for (let i = 0; i < items.length; i++) {
|
for (let i = 0; i < items.length; i++) {
|
||||||
const item = items[i];
|
const item = items[i];
|
||||||
await client.query(
|
await client.query(
|
||||||
|
|
@ -334,12 +343,14 @@ app.put('/api/quotes/:id', async (req, res) => {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('Committing transaction');
|
||||||
await client.query('COMMIT');
|
await client.query('COMMIT');
|
||||||
|
console.log('PUT request successful, sending response');
|
||||||
res.json(quoteResult.rows[0]);
|
res.json(quoteResult.rows[0]);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await client.query('ROLLBACK');
|
await client.query('ROLLBACK');
|
||||||
console.error(err);
|
console.error('PUT Error:', err);
|
||||||
res.status(500).json({ error: 'Database error' });
|
res.status(500).json({ error: 'Database error: ' + err.message });
|
||||||
} finally {
|
} finally {
|
||||||
client.release();
|
client.release();
|
||||||
}
|
}
|
||||||
|
|
@ -454,11 +465,12 @@ app.post('/api/quotes/:id/pdf', async (req, res) => {
|
||||||
format: 'Letter',
|
format: 'Letter',
|
||||||
printBackground: true,
|
printBackground: true,
|
||||||
preferCSSPageSize: false,
|
preferCSSPageSize: false,
|
||||||
|
displayHeaderFooter: false,
|
||||||
margin: {
|
margin: {
|
||||||
top: '0.4in',
|
top: '0.5in',
|
||||||
right: '0.4in',
|
right: '0.5in',
|
||||||
bottom: '0.4in',
|
bottom: '0.5in',
|
||||||
left: '0.4in'
|
left: '0.5in'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -466,11 +478,18 @@ app.post('/api/quotes/:id/pdf', async (req, res) => {
|
||||||
browser = null;
|
browser = null;
|
||||||
|
|
||||||
console.log('PDF generated successfully, size:', pdf.length, 'bytes');
|
console.log('PDF generated successfully, size:', pdf.length, 'bytes');
|
||||||
|
console.log('PDF first bytes:', pdf.slice(0, 20).toString('hex'));
|
||||||
|
|
||||||
|
// Verify PDF header
|
||||||
|
const pdfHeader = pdf.slice(0, 4).toString();
|
||||||
|
if (pdfHeader !== '%PDF') {
|
||||||
|
throw new Error('Generated file is not a valid PDF (missing %PDF header)');
|
||||||
|
}
|
||||||
|
|
||||||
res.setHeader('Content-Type', 'application/pdf');
|
res.setHeader('Content-Type', 'application/pdf');
|
||||||
res.setHeader('Content-Length', pdf.length);
|
res.setHeader('Content-Length', pdf.length);
|
||||||
res.setHeader('Content-Disposition', `attachment; filename="Quote_${quote.quote_number}.pdf"`);
|
res.setHeader('Content-Disposition', `attachment; filename="Quote_${quote.quote_number}.pdf"`);
|
||||||
res.send(pdf);
|
res.end(pdf, 'binary');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('PDF Generation Error:', err);
|
console.error('PDF Generation Error:', err);
|
||||||
if (browser) {
|
if (browser) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue