Add 'orders' for customer button in the customers page. Fix bug in add line item

This commit is contained in:
2026-07-11 20:42:37 -04:00
parent 1eccda5cf3
commit 9fa6b146e5
9 changed files with 51 additions and 19 deletions
+23 -10
View File
@@ -59,7 +59,9 @@ export function getOrderDetail(id: string | number) {
return { ...order, lineItems, workLog, totals };
}
router.get('/', (_req, res) => {
router.get('/', (req, res) => {
const customerId = req.query.customer_id ? Number(req.query.customer_id) : null;
const orders = db
.prepare(
`SELECT so.id, so.order_number, so.status, so.job_type, so.date_in, so.due_date,
@@ -68,6 +70,7 @@ router.get('/', (_req, res) => {
JOIN customers c ON c.id = so.customer_id
LEFT JOIN order_totals ot ON ot.service_order_id = so.id
WHERE so.deleted_at IS NULL
AND (? IS NULL OR so.customer_id = ?)
ORDER BY CASE so.status
WHEN 'OPEN' THEN 0
WHEN 'IN PROGRESS' THEN 1
@@ -78,9 +81,13 @@ router.get('/', (_req, res) => {
so.date_in DESC,
so.order_number DESC`
)
.all();
.all(customerId, customerId);
res.render('orders/list.njk', { orders });
const customerFilter = customerId
? db.prepare('SELECT id, name FROM customers WHERE id = ?').get(customerId)
: null;
res.render('orders/list.njk', { orders, customerFilter });
});
router.get('/orders/new', (_req, res) => {
@@ -137,8 +144,8 @@ router.post('/orders', (req, res) => {
const orderInfo = db
.prepare(
`INSERT INTO service_orders
(order_number, customer_id, clock_id, job_type, date_in, due_date, terms)
VALUES (?, ?, ?, ?, ?, ?, ?)`
(order_number, customer_id, clock_id, job_type, date_in, due_date, terms, internal_note)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
)
.run(
orderNumber,
@@ -147,7 +154,8 @@ router.post('/orders', (req, res) => {
b.job_type || null,
b.date_in || todayStr(),
b.due_date || null,
b.terms || null
b.terms || null,
b.internal_note || null
);
return Number(orderInfo.lastInsertRowid);
@@ -167,6 +175,11 @@ router.get('/orders/:id', (req, res) => {
res.render('orders/detail.njk', { order, jobTypes: JOB_TYPES, statuses: STATUSES });
});
router.get('/orders/:id/totals', (req, res) => {
const totals = db.prepare('SELECT * FROM order_totals WHERE service_order_id = ?').get(req.params.id);
res.render('orders/_totals.njk', { order: { id: req.params.id }, totals });
});
router.post('/orders/:id/line-items', (req, res) => {
const orderId = req.params.id;
const b = req.body as Record<string, string>;
@@ -187,9 +200,9 @@ router.post('/orders/:id/line-items', (req, res) => {
.run(orderId, kind, b.stock_code || null, b.description || null, quantity, unitPrice, countRow.n);
const line = db.prepare('SELECT * FROM line_items WHERE id = ?').get(info.lastInsertRowid);
const totals = db.prepare('SELECT * FROM order_totals WHERE service_order_id = ?').get(orderId);
res.render('orders/_line_item_added.njk', { line, order: { id: orderId }, totals, oob: true });
res.set('HX-Trigger', 'line-items-changed');
res.render('orders/_line_item_row.njk', { line, order: { id: orderId } });
});
router.delete('/orders/:id/line-items/:lineId', (req, res) => {
@@ -197,8 +210,8 @@ router.delete('/orders/:id/line-items/:lineId', (req, res) => {
req.params.lineId,
req.params.id
);
const totals = db.prepare('SELECT * FROM order_totals WHERE service_order_id = ?').get(req.params.id);
res.render('orders/_totals.njk', { totals, oob: true });
res.set('HX-Trigger', 'line-items-changed');
res.send('');
});
router.post('/orders/:id/work-log', (req, res) => {