diff --git a/modules/item/back/methods/item/filter.js b/modules/item/back/methods/item/filter.js index 4e390227b..ad7edfa8c 100644 --- a/modules/item/back/methods/item/filter.js +++ b/modules/item/back/methods/item/filter.js @@ -62,13 +62,26 @@ module.exports = Self => { Self.filter = async(ctx, filter) => { let conn = Self.dataSource.connector; + let codeWhere; + + if (ctx.args.search) { + let items = await Self.app.models.ItemBarcode.find({ + where: {code: ctx.args.search}, + fields: ['itemFk'] + }); + let itemIds = []; + for (const item of items) + itemIds.push(item.itemFk); + + codeWhere = {'i.id': {inq: itemIds}}; + } let where = buildFilter(ctx.args, (param, value) => { switch (param) { case 'search': return /^\d+$/.test(value) - ? {or: [{'i.id': value}, {'ib.code': value}]} - : {or: [{'i.name': {like: `%${value}%`}}, {'ib.code': value}]}; + ? {or: [{'i.id': value}, codeWhere]} + : {or: [{'i.name': {like: `%${value}%`}}, codeWhere]}; case 'id': return {'i.id': value}; case 'description': @@ -83,8 +96,8 @@ module.exports = Self => { return {'i.isActive': value}; } }); - filter = mergeFilters(filter, {where}); + let stmts = []; let stmt; @@ -123,8 +136,7 @@ module.exports = Self => { LEFT JOIN origin ori ON ori.id = i.originFk LEFT JOIN cache.last_buy lb ON lb.item_id = i.id AND lb.warehouse_id = t.warehouseFk LEFT JOIN vn.buy b ON b.id = lb.buy_id - LEFT JOIN itemPlacement itn ON itn.itemFk = i.id AND itn.warehouseFk = t.warehouseFk - LEFT JOIN itemBarcode ib ON ib.itemFk = i.id` + LEFT JOIN itemPlacement itn ON itn.itemFk = i.id AND itn.warehouseFk = t.warehouseFk` ); if (ctx.args.tags) { @@ -150,7 +162,6 @@ module.exports = Self => { } stmt.merge(conn.makeWhere(filter.where)); - stmt.merge(`GROUP BY i.id`); stmt.merge(conn.makePagination(filter)); let itemsIndex = stmts.push(stmt) - 1;