new Filter

This commit is contained in:
2026-05-29 10:46:48 -05:00
parent 4a62b20a42
commit 17be918883
4 changed files with 76 additions and 2 deletions

View File

@@ -50,11 +50,28 @@ function buildPaymentLinkHtml(invoice) {
// GET all invoices
router.get('/', async (req, res) => {
try {
const { has_parts_or_subscription, empty_cost_only } = req.query;
let whereClauses = [];
if (has_parts_or_subscription === 'true') {
const costCondition = empty_cost_only === 'true'
? `AND (ii.unit_cost IS NULL OR ii.unit_cost = '')`
: '';
whereClauses.push(`EXISTS (
SELECT 1 FROM invoice_items ii
WHERE ii.invoice_id = i.id
AND (ii.qbo_item_id = '9' OR ii.qbo_item_id = '115')
${costCondition}
)`);
}
const whereSQL = whereClauses.length > 0 ? 'WHERE ' + whereClauses.join(' AND ') : '';
const result = await pool.query(`
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
${whereSQL}
ORDER BY i.created_at DESC
`);
const rows = result.rows.map(r => ({