From 8aa9918d844ec0c9b525b7bcf7a6afe0d07cc49d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Mon, 31 Aug 2020 15:03:00 +0200 Subject: [PATCH 1/3] 2409 - Added filter by selection --- .../item/back/methods/item/getLastEntries.js | 28 --------- .../back/methods/item/lastEntriesFilter.js | 62 +++++++++++++++++++ ...ries.spec.js => lastEntriesFilter.spec.js} | 6 +- modules/item/back/models/item.js | 2 +- modules/item/front/last-entries/index.html | 48 +++++++++++--- modules/item/front/last-entries/index.js | 42 +++++++++++-- 6 files changed, 141 insertions(+), 47 deletions(-) delete mode 100644 modules/item/back/methods/item/getLastEntries.js create mode 100644 modules/item/back/methods/item/lastEntriesFilter.js rename modules/item/back/methods/item/specs/{getLastEntries.spec.js => lastEntriesFilter.spec.js} (73%) diff --git a/modules/item/back/methods/item/getLastEntries.js b/modules/item/back/methods/item/getLastEntries.js deleted file mode 100644 index a438afcb6..000000000 --- a/modules/item/back/methods/item/getLastEntries.js +++ /dev/null @@ -1,28 +0,0 @@ -module.exports = Self => { - Self.remoteMethod('getLastEntries', { - description: 'Returns last entries', - accessType: 'READ', - accepts: [{ - arg: 'filter', - type: 'object', - required: true, - description: 'itemFk, id' - }], - returns: { - type: 'Array', - root: true - }, - http: { - path: `/getLastEntries`, - verb: 'GET' - } - }); - - Self.getLastEntries = async filter => { - let where = filter.where; - let query = `CALL vn.itemLastEntries(?, ?)`; - let [lastEntries] = await Self.rawSql(query, [where.itemFk, where.date]); - - return lastEntries; - }; -}; diff --git a/modules/item/back/methods/item/lastEntriesFilter.js b/modules/item/back/methods/item/lastEntriesFilter.js new file mode 100644 index 000000000..63e154b9e --- /dev/null +++ b/modules/item/back/methods/item/lastEntriesFilter.js @@ -0,0 +1,62 @@ + +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; + +module.exports = Self => { + Self.remoteMethod('lastEntriesFilter', { + description: 'Returns last entries', + accessType: 'READ', + accepts: [{ + arg: 'filter', + type: 'object', + required: true, + description: 'itemFk, id' + }], + returns: { + type: 'Array', + root: true + }, + http: { + path: `/lastEntriesFilter`, + verb: 'GET' + } + }); + + Self.lastEntriesFilter = async filter => { + const conn = Self.dataSource.connector; + const stmt = new ParameterizedSQL( + `SELECT + w.id AS warehouseFk, + w.name AS warehouse, + tr.landed, + b.id AS buyFk, + b.entryFk, + b.isIgnored, + b.price2, + b.price3, + b.stickers, + b.packing, + b.grouping, + b.groupingMode, + b.weight, + i.stems, + b.quantity, + b.buyingValue, + b.packageFk , + s.id AS supplierFk, + s.name AS supplier + FROM itemType it + RIGHT JOIN (entry e + LEFT JOIN supplier s ON s.id = e.supplierFk + RIGHT JOIN buy b ON b.entryFk = e.id + LEFT JOIN item i ON i.id = b.itemFk + LEFT JOIN ink ON ink.id = i.inkFk + LEFT JOIN travel tr ON tr.id = e.travelFk + LEFT JOIN warehouse w ON w.id = tr.warehouseInFk + LEFT JOIN origin o ON o.id = i.originFk + ) ON it.id = i.typeFk + LEFT JOIN edi.ekt ek ON b.ektFk = ek.id`); + stmt.merge(conn.makeSuffix(filter)); + + return conn.executeStmt(stmt); + }; +}; diff --git a/modules/item/back/methods/item/specs/getLastEntries.spec.js b/modules/item/back/methods/item/specs/lastEntriesFilter.spec.js similarity index 73% rename from modules/item/back/methods/item/specs/getLastEntries.spec.js rename to modules/item/back/methods/item/specs/lastEntriesFilter.spec.js index 5c996d8e3..9ae54afe5 100644 --- a/modules/item/back/methods/item/specs/getLastEntries.spec.js +++ b/modules/item/back/methods/item/specs/lastEntriesFilter.spec.js @@ -1,10 +1,10 @@ const app = require('vn-loopback/server/server'); -describe('item getLastEntries()', () => { +describe('item lastEntriesFilter()', () => { it('should return one entry for a given item', async() => { let date = new Date(); let filter = {where: {itemFk: 1, date: date}}; - let result = await app.models.Item.getLastEntries(filter); + let result = await app.models.Item.lastEntriesFilter(filter); expect(result.length).toEqual(1); }); @@ -15,7 +15,7 @@ describe('item getLastEntries()', () => { date.setMonth(date.getMonth() - 2, 1); let filter = {where: {itemFk: 1, date: date}}; - let result = await app.models.Item.getLastEntries(filter); + let result = await app.models.Item.lastEntriesFilter(filter); expect(result.length).toEqual(5); }); diff --git a/modules/item/back/models/item.js b/modules/item/back/models/item.js index d273404ea..9ddd056e6 100644 --- a/modules/item/back/models/item.js +++ b/modules/item/back/models/item.js @@ -5,7 +5,7 @@ module.exports = Self => { require('../methods/item/clone')(Self); require('../methods/item/updateTaxes')(Self); require('../methods/item/getBalance')(Self); - require('../methods/item/getLastEntries')(Self); + require('../methods/item/lastEntriesFilter')(Self); require('../methods/item/getSummary')(Self); require('../methods/item/getCard')(Self); require('../methods/item/regularize')(Self); diff --git a/modules/item/front/last-entries/index.html b/modules/item/front/last-entries/index.html index 6f2dba989..f306fb4f0 100644 --- a/modules/item/front/last-entries/index.html +++ b/modules/item/front/last-entries/index.html @@ -1,11 +1,15 @@ + auto-load="true" + order="landed DESC, buyFk DESC" + limit="20"> - + @@ -20,8 +24,8 @@ Ig - Warehouse - Landed + Warehouse + Landed Entry P.P.U P.P.P @@ -29,11 +33,11 @@ Packing Grouping Stems - Quantity + Quantity Cost Kg. - Cube - Provider + Cube + Provider @@ -70,6 +74,30 @@ - - + + + + + + Filter by selection + + + Exclude selection + + + Remove filter + + + Remove all filters + + + \ No newline at end of file diff --git a/modules/item/front/last-entries/index.js b/modules/item/front/last-entries/index.js index 3f9d7be36..11a2bf20e 100644 --- a/modules/item/front/last-entries/index.js +++ b/modules/item/front/last-entries/index.js @@ -6,17 +6,23 @@ class Controller extends Section { constructor($element, $) { super($element, $); - let defaultDate = new Date(); - defaultDate.setDate(defaultDate.getDate() - 75); - defaultDate.setHours(0, 0, 0, 0); + const from = new Date(); + from.setDate(from.getDate() - 75); + from.setHours(0, 0, 0, 0); + + const to = new Date(); + to.setDate(to.getDate() + 10); + to.setHours(23, 59, 59, 59); this.filter = { where: { itemFk: this.$params.id, - date: defaultDate + shipped: { + between: [from, to] + } } }; - this._date = defaultDate; + this._date = from; } set date(value) { @@ -31,6 +37,32 @@ class Controller extends Section { get date() { return this._date; } + + exprBuilder(param, value) { + switch (param) { + case 'id': + case 'quantity': + case 'packageFk': + return {[`b.${param}`]: value}; + case 'supplierFk': + return {[`s.id`]: value}; + case 'warehouseFk': + return {'tr.warehouseInFk': value}; + case 'landed': + return {'tr.landed': { + between: this.dateRange(value)} + }; + } + } + + dateRange(value) { + const minHour = new Date(value); + minHour.setHours(0, 0, 0, 0); + const maxHour = new Date(value); + maxHour.setHours(23, 59, 59, 59); + + return [minHour, maxHour]; + } } Controller.$inject = ['$element', '$scope']; From 0a542a205780033bc32d819ace3a59ba5f5148b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Tue, 1 Sep 2020 07:38:03 +0200 Subject: [PATCH 2/3] Updated unit test --- .../item/specs/lastEntriesFilter.spec.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/modules/item/back/methods/item/specs/lastEntriesFilter.spec.js b/modules/item/back/methods/item/specs/lastEntriesFilter.spec.js index 9ae54afe5..f64bfed8a 100644 --- a/modules/item/back/methods/item/specs/lastEntriesFilter.spec.js +++ b/modules/item/back/methods/item/specs/lastEntriesFilter.spec.js @@ -1,21 +1,23 @@ const app = require('vn-loopback/server/server'); describe('item lastEntriesFilter()', () => { + const minDate = new Date(value); + minHour.setHours(0, 0, 0, 0); + const maxDate = new Date(value); + maxHour.setHours(23, 59, 59, 59); + it('should return one entry for a given item', async() => { - let date = new Date(); - let filter = {where: {itemFk: 1, date: date}}; - let result = await app.models.Item.lastEntriesFilter(filter); + const filter = {where: {itemFk: 1, landed: {between: [minDate, maxDate]}}}; + const result = await app.models.Item.lastEntriesFilter(filter); expect(result.length).toEqual(1); }); it('should return five entries for a given item', async() => { - let date = new Date(); + minDate.setMonth(minDate.getMonth() - 2, 1); - date.setMonth(date.getMonth() - 2, 1); - - let filter = {where: {itemFk: 1, date: date}}; - let result = await app.models.Item.lastEntriesFilter(filter); + const filter = {where: {itemFk: 1, landed: {between: [minDate, maxDate]}}}; + const result = await app.models.Item.lastEntriesFilter(filter); expect(result.length).toEqual(5); }); From 8ae7871318989ba6bca48b5d4723595d49591b30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Tue, 1 Sep 2020 07:56:56 +0200 Subject: [PATCH 3/3] Removed procedure --- .../10200-normality/00-itemLastEntries.sql | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 db/changes/10200-normality/00-itemLastEntries.sql diff --git a/db/changes/10200-normality/00-itemLastEntries.sql b/db/changes/10200-normality/00-itemLastEntries.sql new file mode 100644 index 000000000..ce600b39f --- /dev/null +++ b/db/changes/10200-normality/00-itemLastEntries.sql @@ -0,0 +1,44 @@ + +USE `vn`; +DROP procedure IF EXISTS `vn`.`itemLastEntries`; + +DELIMITER $$ +USE `vn`$$ +CREATE DEFINER=`root`@`%` PROCEDURE `itemLastEntries__`(IN `vItem` INT, IN `vDays` DATE) +BEGIN + SELECT + w.id AS warehouseFk, + w.name AS warehouse, + tr.landed, + b.entryFk, + b.isIgnored, + b.price2, + b.price3, + b.stickers, + b.packing, + b.`grouping`, + b.groupingMode, + b.weight, + i.stems, + b.quantity, + b.buyingValue, + b.packageFk , + s.id AS supplierFk, + s.name AS supplier + FROM itemType it + RIGHT JOIN (entry e + LEFT JOIN supplier s ON s.id = e.supplierFk + RIGHT JOIN buy b ON b.entryFk = e.id + LEFT JOIN item i ON i.id = b.itemFk + LEFT JOIN ink ON ink.id = i.inkFk + LEFT JOIN travel tr ON tr.id = e.travelFk + LEFT JOIN warehouse w ON w.id = tr.warehouseInFk + LEFT JOIN origin o ON o.id = i.originFk + ) ON it.id = i.typeFk + LEFT JOIN edi.ekt ek ON b.ektFk = ek.id + WHERE b.itemFk = vItem And tr.shipped BETWEEN vDays AND DATE_ADD(CURDATE(), INTERVAl + 10 DAY) + ORDER BY tr.landed DESC , b.id DESC; +END$$ + +DELIMITER ; +;