new filters
This commit is contained in:
@@ -50,20 +50,28 @@ function buildPaymentLinkHtml(invoice) {
|
||||
// GET all invoices
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const { has_parts, empty_cost_only } = req.query;
|
||||
const CATEGORIES = { '5': 'Labor', '9': 'Parts', '115': 'Subscription' };
|
||||
const { category, item_search } = req.query;
|
||||
|
||||
let whereClauses = [];
|
||||
if (has_parts === 'true') {
|
||||
const costCondition = empty_cost_only === 'true'
|
||||
? `AND (ii.unit_cost IS NULL OR ii.unit_cost = '')`
|
||||
: '';
|
||||
let params = [];
|
||||
|
||||
if (category && CATEGORIES[category]) {
|
||||
whereClauses.push(`EXISTS (
|
||||
SELECT 1 FROM invoice_items ii
|
||||
WHERE ii.invoice_id = i.id
|
||||
AND ii.qbo_item_id = '9'
|
||||
${costCondition}
|
||||
WHERE ii.invoice_id = i.id AND ii.qbo_item_id = $${params.length + 1}
|
||||
)`);
|
||||
params.push(category);
|
||||
}
|
||||
|
||||
if (item_search && item_search.trim()) {
|
||||
whereClauses.push(`EXISTS (
|
||||
SELECT 1 FROM invoice_items ii
|
||||
WHERE ii.invoice_id = i.id AND ii.description ILIKE $${params.length + 1}
|
||||
)`);
|
||||
params.push('%' + item_search.trim() + '%');
|
||||
}
|
||||
|
||||
const whereSQL = whereClauses.length > 0 ? 'WHERE ' + whereClauses.join(' AND ') : '';
|
||||
|
||||
const result = await pool.query(`
|
||||
@@ -73,7 +81,7 @@ router.get('/', async (req, res) => {
|
||||
LEFT JOIN customers c ON i.customer_id = c.id
|
||||
${whereSQL}
|
||||
ORDER BY i.created_at DESC
|
||||
`);
|
||||
`, params);
|
||||
const rows = result.rows.map(r => ({
|
||||
...r,
|
||||
amount_paid: parseFloat(r.amount_paid) || 0,
|
||||
|
||||
Reference in New Issue
Block a user