new Filter
This commit is contained in:
@@ -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 => ({
|
||||
|
||||
Reference in New Issue
Block a user