From 671c6bd3ff2a3e97c0b4b9f763489c6b26b5febb Mon Sep 17 00:00:00 2001 From: Joan Date: Wed, 8 Aug 2018 14:26:54 +0200 Subject: [PATCH] Ticket list refactor. Ticket problems, ticket total #245 & #461 --- client/ticket/src/index/index.html | 87 ++++++----- .../changes/1.0.10/05-ticketGetProblems.sql | 146 ++++++++++++++++++ .../changes/1.0.10/06-ticketGetFullList.sql | 32 ++++ .../loopback/common/methods/ticket/filter.js | 68 ++++++++ .../methods/ticket/specs/filter.spec.js | 11 ++ services/loopback/common/models/ticket.js | 1 + 6 files changed, 306 insertions(+), 39 deletions(-) create mode 100644 services/db/install/changes/1.0.10/05-ticketGetProblems.sql create mode 100644 services/db/install/changes/1.0.10/06-ticketGetFullList.sql create mode 100644 services/loopback/common/methods/ticket/filter.js create mode 100644 services/loopback/common/methods/ticket/specs/filter.spec.js diff --git a/client/ticket/src/index/index.html b/client/ticket/src/index/index.html index b7175f05d..412f7f23f 100644 --- a/client/ticket/src/index/index.html +++ b/client/ticket/src/index/index.html @@ -1,6 +1,6 @@ - - - - - - - - - - - - - - - - - - - + + + + Id + Salesperson + Date + Hour + Alias + Province + State + Agency + Warehouse + Invoice + Route + Total + + + + + - - - - - - - - - - - - - - -
IdSalespersonDateHourAliasProvinceStateAgencyWarehouseInvoiceRoute
{{::ticket.id}}{{::ticket.client.salesPerson.name | dashIfEmpty}}{{::ticket.shipped | date:'dd/MM/yyyy'}}{{::ticket.shipped | date:'HH:mm'}} + + + + + {{::ticket.id}} + {{::ticket.salesPerson | dashIfEmpty}} + {{::ticket.shipped | date:'dd/MM/yyyy'}} + {{::ticket.shipped | date:'HH:mm'}} + {{::ticket.nickname}} - {{::ticket.address.province.name}}{{::ticket.tracking.state.name}}{{::ticket.agencyMode.name}}{{::ticket.warehouse.name}}{{::ticket.refFk | dashIfEmpty}}{{::ticket.routeFk | dashIfEmpty}} + + {{::ticket.province}} + {{::ticket.state}} + {{::ticket.agencyMode}} + {{::ticket.warehouse}} + {{::ticket.refFk | dashIfEmpty}} + {{::ticket.routeFk | dashIfEmpty}} + {{::ticket.total | currency: '€': 2}} + -
- + + + +
- \ No newline at end of file + + \ No newline at end of file diff --git a/services/db/install/changes/1.0.10/05-ticketGetProblems.sql b/services/db/install/changes/1.0.10/05-ticketGetProblems.sql new file mode 100644 index 000000000..efabfd1b6 --- /dev/null +++ b/services/db/install/changes/1.0.10/05-ticketGetProblems.sql @@ -0,0 +1,146 @@ +USE `vn`; +DROP procedure IF EXISTS `ticketGetProblems`; + +DELIMITER $$ +USE `vn`$$ +CREATE DEFINER=`root`@`%` PROCEDURE `ticketGetProblems`() +BEGIN +/* + * Necesita la tabla tmp.ticket + * + */ + DECLARE vWarehouse INT; + DECLARE vDate DATE; + DECLARE vAvailableCache INT; + DECLARE vVisibleCache INT; + DECLARE vDone INT DEFAULT 0; + + DECLARE vCursor CURSOR FOR + SELECT DISTINCT tt.warehouseFk, date(tt.shipped) + FROM tmp.ticket tt + WHERE DATE(tt.shipped) BETWEEN CURDATE() + AND TIMESTAMPADD(DAY, 1.9, CURDATE()); + + DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = 1; + + DROP TEMPORARY TABLE IF EXISTS tmp.ticketProblems; + CREATE TEMPORARY TABLE tmp.ticketProblems ( + ticketFk INT(11), + problem VARCHAR(50), + INDEX (ticketFk) + ) + ENGINE = MEMORY; + + -- CONGELADO + INSERT INTO tmp.ticketProblems(ticketFk, problem) + SELECT DISTINCT tt.ticketFk, 'CONGELADO' + FROM tmp.ticket tt + JOIN vn.client c ON c.id = tt.clientFk + WHERE c.isFreezed; + + -- eliminamos tickets con problemas para no volverlos a mirar + DROP TEMPORARY TABLE IF EXISTS tmp.ticketListFiltered; + + CREATE TEMPORARY TABLE tmp.ticketListFiltered + (PRIMARY KEY (ticketFk)) + ENGINE = MEMORY + SELECT tt.ticketFk, c.id + FROM tmp.ticket tt + JOIN vn.client c ON c.id = tt.clientFk + WHERE c.isFreezed = 0; + + DROP TEMPORARY TABLE IF EXISTS tmp.client_list; + CREATE TEMPORARY TABLE tmp.client_list + (PRIMARY KEY (Id_Cliente)) + ENGINE = MEMORY + SELECT DISTINCT tt.clientFk AS Id_Cliente + FROM tmp.ticket tt; + + -- RIESGO + CALL vn2008.risk_vs_client_list(CURDATE()); + + INSERT INTO tmp.ticketProblems(ticketFk, problem) + SELECT DISTINCT tt.ticketFk, 'RIESGO' + FROM tmp.ticketListFiltered tt + JOIN vn.ticket t ON t.id = tt.ticketFk + JOIN vn.agencyMode a ON t.agencyModeFk = a.id + JOIN tmp.risk r ON r.Id_Cliente = t.clientFk + JOIN vn.client c ON c.id = t.clientFk + WHERE r.risk > c.credit + 10 + AND a.deliveryMethodFk != 3; -- para que las recogidas se preparen + + -- eliminamos tickets con problemas para no volverlos a mirar + DELETE tlf FROM tmp.ticketListFiltered tlf + JOIN tmp.ticketProblems tp ON tlf.ticketFk = tp.ticketFk; + + -- CODIGO 100 + INSERT INTO tmp.ticketProblems(ticketFk, problem) + SELECT DISTINCT tt.ticketFk, 'COD 100' + FROM tmp.ticket tt + JOIN sale s ON s.ticketFk = tt.ticketFk + WHERE s.itemFk = 100; + + -- eliminamos tickets con problemas para no volverlos a mirar + DELETE tlf FROM tmp.ticketListFiltered tlf + JOIN tmp.ticketProblems tp ON tlf.ticketFk = tp.ticketFk; + + OPEN vCursor; + + WHILE NOT vDone + DO + FETCH vCursor INTO vWarehouse, vDate; + + CALL cache.visible_refresh(vVisibleCache, FALSE, vWarehouse); + CALL cache.available_refresh(vAvailableCache, FALSE, vWarehouse, vDate); + + -- El disponible es menor que 0 + INSERT INTO tmp.ticketProblems(ticketFk, problem) + SELECT tt.ticketFk, i.name + FROM tmp.ticket tt + JOIN vn.ticket t ON t.id = tt.ticketFk + LEFT JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it on it.id = i.typeFk + LEFT JOIN cache.visible v ON i.id = v.item_id + AND v.calc_id = vVisibleCache + LEFT JOIN cache.available av ON av.item_id = i.id + AND av.calc_id = vAvailableCache + WHERE date(t.shipped) = vDate + AND categoryFk != 6 + AND s.quantity > IFNULL(v.visible, 0) + AND IFNULL(av.available, 0) < 0 + AND s.isPicked = FALSE + AND NOT i.generic + AND vWarehouse = t.warehouseFk; + + -- eliminamos tickets con problemas para no volverlos a mirar + DELETE tlf FROM tmp.ticketListFiltered tlf + JOIN tmp.ticketProblems tp ON tlf.ticketFk = tp.ticketFk; + + -- Amarillo: El disponible es mayor que cero y la cantidad supera el visible, estando aun sin preparar + INSERT INTO tmp.ticketProblems(ticketFk, problem) + SELECT tt.ticketFk, CONCAT('RETRASO ', i.name) + FROM tmp.ticket tt + JOIN vn.ticket t ON t.id = tt.ticketFk + LEFT JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it on it.id = i.typeFk + LEFT JOIN cache.visible v ON i.id = v.item_id AND v.calc_id = vVisibleCache + LEFT JOIN cache.available av ON av.item_id = i.id AND av.calc_id = vAvailableCache + WHERE IFNULL(av.available, 0) >= 0 + AND s.quantity > IFNULL(v.visible, 0) + AND s.isPicked = FALSE + AND s.reserved = FALSE + AND it.categoryFk != 6 + AND date(t.shipped) = vDate + AND NOT i.generic + AND CURDATE() = vDate + AND t.warehouseFk = vWarehouse; + END WHILE; + + CLOSE vCursor; + + DROP TEMPORARY TABLE tmp.ticketListFiltered; +END$$ + +DELIMITER ; diff --git a/services/db/install/changes/1.0.10/06-ticketGetFullList.sql b/services/db/install/changes/1.0.10/06-ticketGetFullList.sql new file mode 100644 index 000000000..ca2e6d00a --- /dev/null +++ b/services/db/install/changes/1.0.10/06-ticketGetFullList.sql @@ -0,0 +1,32 @@ +USE `vn`; +DROP procedure IF EXISTS `ticketGetFullList`; + +DELIMITER $$ +USE `vn`$$ +CREATE DEFINER=`root`@`%` PROCEDURE `ticketGetFullList`() +BEGIN +/** + * Obtiene un listado de tickets + * junto con el precio total y los problemas + * + * @table tmp.ticket(ticketFk) Identificadores de los tickets a calcular + * @return Listado de tickets + */ + CALL ticketGetTotal(); + CALL ticketGetProblems(); + + DROP TEMPORARY TABLE IF EXISTS tmp.ticketFullList; + CREATE TEMPORARY TABLE tmp.ticketFullList ENGINE = MEMORY + SELECT t.*, tt.total, tp.problem + FROM tmp.ticket t + JOIN tmp.ticketTotal tt ON tt.ticketFk = t.ticketFk + LEFT JOIN tmp.ticketProblems tp ON tp.ticketFk = t.ticketFk; + + DROP TEMPORARY TABLE + tmp.ticket, + tmp.ticketTotal, + tmp.ticketProblems; +END$$ + +DELIMITER ; + diff --git a/services/loopback/common/methods/ticket/filter.js b/services/loopback/common/methods/ticket/filter.js new file mode 100644 index 000000000..62dfdd147 --- /dev/null +++ b/services/loopback/common/methods/ticket/filter.js @@ -0,0 +1,68 @@ + +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; + +module.exports = Self => { + Self.remoteMethod('filter', { + description: 'Find all instances of the model matched by filter from the data source.', + accessType: 'READ', + accepts: [ + { + arg: 'filter', + type: 'Object', + description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string', + http: {source: 'query'} + } + ], + returns: { + type: ['Object'], + root: true + }, + http: { + path: `/filter`, + verb: 'GET' + } + }); + + Self.filter = async filter => { + let stmt = new ParameterizedSQL( + `DROP TEMPORARY TABLE IF EXISTS tmp.ticket; + CREATE TEMPORARY TABLE tmp.ticket + (PRIMARY KEY (ticketFk)) ENGINE = MEMORY + SELECT + t.id, + t.id AS ticketFk, + t.shipped, + t.nickname, + t.refFk, + t.routeFk, + t.agencyModeFk, + t.warehouseFk, + t.clientFk, + c.salesPersonFk, + a.provinceFk, + ts.stateFk, + p.name AS province, + w.name AS warehouse, + am.name AS agencyMode, + st.name AS state, + wk.name AS salesPerson + FROM ticket t + LEFT JOIN address a ON a.id = t.addressFk + LEFT JOIN province p ON p.id = a.provinceFk + LEFT JOIN warehouse w ON w.id = t.warehouseFk + LEFT JOIN agencyMode am ON am.id = t.agencyModeFk + LEFT JOIN ticketState ts ON ts.ticketFk = t.id + LEFT JOIN state st ON st.id = ts.stateFk + LEFT JOIN client c ON c.id = t.clientFk + LEFT JOIN worker wk ON wk.id = c.salesPersonFk`); + + stmt.merge(Self.buildSuffix(filter, 't')); + stmt.merge(';CALL ticketGetFullList()'); + stmt.merge(';SELECT * FROM tmp.ticketFullList tfl'); + stmt.merge(Self.buildSuffix(filter, 'tfl')); + + let result = await Self.rawStmt(stmt); + + return result[3]; + }; +}; diff --git a/services/loopback/common/methods/ticket/specs/filter.spec.js b/services/loopback/common/methods/ticket/specs/filter.spec.js new file mode 100644 index 000000000..38de4fabe --- /dev/null +++ b/services/loopback/common/methods/ticket/specs/filter.spec.js @@ -0,0 +1,11 @@ +const app = require(`${servicesDir}/ticket/server/server`); + +describe('ticket filter()', () => { + it('should call the filter method', async() => { + let filter = {order: 'shipped DESC'}; + let result = await app.models.Ticket.filter(filter); + let ticketId = result[0].id; + + expect(ticketId).toEqual(15); + }); +}); diff --git a/services/loopback/common/models/ticket.js b/services/loopback/common/models/ticket.js index a29590c2f..f86bb6544 100644 --- a/services/loopback/common/models/ticket.js +++ b/services/loopback/common/models/ticket.js @@ -15,4 +15,5 @@ module.exports = Self => { require('../methods/ticket/getSales')(Self); require('../methods/ticket/getSalesPersonMana')(Self); require('../methods/ticket/getShipped')(Self); + require('../methods/ticket/filter')(Self); };