From 84b958e78a2d043045815d6a60e9e596f7a8f1df Mon Sep 17 00:00:00 2001 From: lomamech Date: Sat, 15 Oct 2022 12:00:56 +0100 Subject: [PATCH] bug(Asset Register) - Hide the asset where quantity in stock is 0 - Add HAVING filters in SQL Clause format closes #6842 --- server/controllers/stock/core.js | 5 ++++- server/lib/filter.js | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/server/controllers/stock/core.js b/server/controllers/stock/core.js index 19a1ac4fa4..89588e6648 100644 --- a/server/controllers/stock/core.js +++ b/server/controllers/stock/core.js @@ -309,7 +309,8 @@ async function getAssets(params) { ) AS last_scan ON last_scan.asset_uuid = l.uuid `; - const groupByClause = ` GROUP BY l.uuid, m.depot_uuid ORDER BY i.code, l.label `; + const groupByClause = ` GROUP BY l.uuid, m.depot_uuid `; + const havingClause = ` HAVING quantity > 0 `; const filters = getLotFilters(params); if (['scanned', 'unscanned'].includes(params.scan_status)) { @@ -318,6 +319,8 @@ async function getAssets(params) { } filters.setGroup(groupByClause); + filters.setHaving(havingClause); + filters.setOrder('ORDER BY i.code, l.label'); const query = filters.applyQuery(sql); const queryParameters = filters.parameters(); diff --git a/server/lib/filter.js b/server/lib/filter.js index 488f87289e..83ca952d14 100644 --- a/server/lib/filter.js +++ b/server/lib/filter.js @@ -45,6 +45,7 @@ class FilterParser { this._parseUuids = _.isUndefined(options.parseUuids) ? true : options.parseUuids; this._autoParseStatements = _.isUndefined(options.autoParseStatements) ? false : options.autoParseStatements; this._group = ''; + this._having = ''; } /** @@ -203,6 +204,18 @@ class FilterParser { this._group = groupString; } + /** + * @method setHaving + * + * @description + * Allows setting the SQL Having in the HAVING statement. A developer is expected to + * provide a valid SQL string. This will be appended to the SQL statement after the + * WHERE clause and/or GROUP BY + */ + setHaving(havingClause) { + this._having = havingClause; + } + applyQuery(sql, ignoreLimit = false) { // optionally call utility method to parse all remaining options as simple // equality filters into `_statements` @@ -215,8 +228,9 @@ class FilterParser { const conditionStatements = this._parseStatements(); const order = this._order; const group = this._group; + const having = this._having; - return `${sql} WHERE ${conditionStatements} ${group} ${order} ${limitCondition}`; + return `${sql} WHERE ${conditionStatements} ${group} ${having} ${order} ${limitCondition}`; } parameters() {