From 4e5c6ff882061ed3b09fe9724223eb3868965a2e Mon Sep 17 00:00:00 2001 From: Carlos Jimenez Ruiz Date: Thu, 25 Apr 2019 08:28:08 +0200 Subject: [PATCH] #1356 advanced search refactor --- .../back/methods/invoiceOut/filter.js | 128 +++++++++++++++++ modules/invoiceOut/back/models/invoiceOut.js | 1 + modules/invoiceOut/front/index/index.html | 18 +-- modules/invoiceOut/front/index/index.js | 43 +----- modules/invoiceOut/front/summary/index.html | 2 +- .../invoiceOut/front/summary/locale/es.yml | 8 -- modules/invoiceOut/front/summary/style.scss | 2 +- modules/item/back/methods/item/filter.js | 2 +- modules/route/back/methods/route/filter.js | 131 ++++++++++++++++++ modules/route/back/models/route.js | 1 + modules/route/front/index/index.html | 17 +-- modules/route/front/index/index.js | 59 ++------ 12 files changed, 295 insertions(+), 117 deletions(-) create mode 100644 modules/invoiceOut/back/methods/invoiceOut/filter.js create mode 100644 modules/route/back/methods/route/filter.js diff --git a/modules/invoiceOut/back/methods/invoiceOut/filter.js b/modules/invoiceOut/back/methods/invoiceOut/filter.js new file mode 100644 index 000000000..e9f60473a --- /dev/null +++ b/modules/invoiceOut/back/methods/invoiceOut/filter.js @@ -0,0 +1,128 @@ + +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; +const buildFilter = require('vn-loopback/util/filter').buildFilter; +const mergeFilters = require('vn-loopback/util/filter').mergeFilters; + +module.exports = Self => { + Self.remoteMethodCtx('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'} + }, + { + arg: 'search', + type: 'String', + description: 'Searchs the invoiceOut by id', + http: {source: 'query'} + }, { + arg: 'clientFk', + type: 'Integer', + description: 'The client id', + http: {source: 'query'} + }, { + arg: 'hasPdf', + type: 'Boolean', + description: 'Whether the the invoiceOut has PDF or not', + http: {source: 'query'} + }, { + arg: 'amount', + type: 'Number', + description: 'The amount filter', + http: {source: 'query'} + }, { + arg: 'min', + type: 'Number', + description: 'The minimun amount flter', + http: {source: 'query'} + }, { + arg: 'max', + type: 'Number', + description: 'The maximun amount flter', + http: {source: 'query'} + }, { + arg: 'issued', + type: 'Date', + description: 'The issued date filter', + http: {source: 'query'} + }, { + arg: 'created', + type: 'Date', + description: 'The created date filter', + http: {source: 'query'} + }, { + arg: 'dued', + type: 'Date', + description: 'The due date filter', + http: {source: 'query'} + } + ], + returns: { + type: ['Object'], + root: true + }, + http: { + path: `/filter`, + verb: 'GET' + } + }); + + Self.filter = async(ctx, filter) => { + let conn = Self.dataSource.connector; + + let where = buildFilter(ctx.args, (param, value) => { + switch (param) { + case 'search': + return {ref: {like: `%${value}%`}}; + case 'min': + return {amount: {gte: value}}; + case 'max': + return {amount: {lte: value}}; + case 'hasPdf': + return {'i.pdf': value}; + case 'created': + return {'i.created': value}; + case 'amount': + case 'clientFk': + case 'companyFk': + case 'issued': + case 'dued': + return {[param]: value}; + } + }); + + filter = mergeFilters(ctx.args.filter, {where}); + + let stmts = []; + let stmt; + + stmt = new ParameterizedSQL( + `SELECT + i.id, + i.ref, + i.issued, + i.amount, + i.created, + i.dued, + i.clientFk, + i.pdf AS hasPdf, + c.socialName AS clientSocialName, + co.code AS companyCode + FROM invoiceOut i + LEFT JOIN client c ON c.id = i.clientFk + LEFT JOIN company co ON co.id = i.companyFk` + ); + + stmt.merge(conn.makeSuffix(filter)); + let itemsIndex = stmts.push(stmt) - 1; + + let sql = ParameterizedSQL.join(stmts, ';'); + let result = await conn.executeStmt(sql); + return itemsIndex === 0 ? result : result[itemsIndex]; + }; +}; + diff --git a/modules/invoiceOut/back/models/invoiceOut.js b/modules/invoiceOut/back/models/invoiceOut.js index a3edaa28f..2d7c691fc 100644 --- a/modules/invoiceOut/back/models/invoiceOut.js +++ b/modules/invoiceOut/back/models/invoiceOut.js @@ -1,4 +1,5 @@ module.exports = Self => { + require('../methods/invoiceOut/filter')(Self); require('../methods/invoiceOut/summary')(Self); require('../methods/invoiceOut/download')(Self); }; diff --git a/modules/invoiceOut/front/index/index.html b/modules/invoiceOut/front/index/index.html index 98c286dbc..8a7db4f25 100644 --- a/modules/invoiceOut/front/index/index.html +++ b/modules/invoiceOut/front/index/index.html @@ -1,20 +1,16 @@ + data="invoiceOuts" + order="issued DESC">
@@ -36,7 +32,7 @@ - {{::invoiceOut.ref | dashIfEmpty}} @@ -46,11 +42,11 @@ - {{::invoiceOut.client.name | dashIfEmpty}} + {{::invoiceOut.clientSocialName | dashIfEmpty}} {{::invoiceOut.created | dateTime:'dd/MM/yyyy' | dashIfEmpty}} - {{::invoiceOut.company.code | dashIfEmpty}} + {{::invoiceOut.companyCode | dashIfEmpty}} {{::invoiceOut.dued | dateTime:'dd/MM/yyyy' | dashIfEmpty}} +

Desglose impositivo

-

Desglose impositivo

Type Taxable base diff --git a/modules/invoiceOut/front/summary/locale/es.yml b/modules/invoiceOut/front/summary/locale/es.yml index 4444939c7..d1b4a2406 100644 --- a/modules/invoiceOut/front/summary/locale/es.yml +++ b/modules/invoiceOut/front/summary/locale/es.yml @@ -1,11 +1,3 @@ -Driver: Conductor -Vehicle: VehĂ­culo -Packages: Bultos -Starting time: H. Inicio -Finishing time: H. Fin -Km Start: Km de inicio -Km End: Km de fin -PC: CP Date: Fecha Created: Creada Due: Vencimiento diff --git a/modules/invoiceOut/front/summary/style.scss b/modules/invoiceOut/front/summary/style.scss index aef44fbf9..f2cf53381 100644 --- a/modules/invoiceOut/front/summary/style.scss +++ b/modules/invoiceOut/front/summary/style.scss @@ -1,7 +1,7 @@ @import "variables"; -vn-route-summary .summary { +vn-invoice-out-summary .summary { max-width: $width-large; vn-icon[icon=insert_drive_file]{ diff --git a/modules/item/back/methods/item/filter.js b/modules/item/back/methods/item/filter.js index 0da463ad4..d1ad0aab7 100644 --- a/modules/item/back/methods/item/filter.js +++ b/modules/item/back/methods/item/filter.js @@ -41,7 +41,7 @@ module.exports = Self => { }, { arg: 'hasVisible', type: 'Boolean', - description: 'Whether the the item has o not visible', + description: 'Whether the the item has visible or not', http: {source: 'query'} }, { arg: 'isActive', diff --git a/modules/route/back/methods/route/filter.js b/modules/route/back/methods/route/filter.js new file mode 100644 index 000000000..b45680642 --- /dev/null +++ b/modules/route/back/methods/route/filter.js @@ -0,0 +1,131 @@ + +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; +const buildFilter = require('vn-loopback/util/filter').buildFilter; +const mergeFilters = require('vn-loopback/util/filter').mergeFilters; + +module.exports = Self => { + Self.remoteMethodCtx('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'} + }, { + arg: 'search', + type: 'String', + description: 'Searchs the route by id', + http: {source: 'query'} + }, { + arg: 'workerFk', + type: 'Integer', + description: 'The worker id', + http: {source: 'query'} + }, { + arg: 'agencyModeFk', + type: 'Integer', + description: 'The agencyMode id', + http: {source: 'query'} + }, { + arg: 'to', + type: 'Date', + description: 'The to date filter', + http: {source: 'query'} + }, { + arg: 'from', + type: 'Date', + description: 'The to date filter', + http: {source: 'query'} + }, { + arg: 'vehicleFk', + type: 'Integer', + description: 'The vehicle id', + http: {source: 'query'} + }, { + arg: 'm3', + type: 'Number', + description: 'The m3 filter', + http: {source: 'query'} + }, { + arg: 'description', + type: 'String', + description: 'The description filter', + http: {source: 'query'} + } + ], + returns: { + type: ['Object'], + root: true + }, + http: { + path: `/filter`, + verb: 'GET' + } + }); + + Self.filter = async(ctx, filter) => { + let conn = Self.dataSource.connector; + + let where = buildFilter(ctx.args, (param, value) => { + switch (param) { + case 'search': + return {'r.id': value}; + case 'from': + return {'r.created': {gte: value}}; + case 'to': + return {'r.created': {lte: value}}; + case 'm3': + return {'r.m3': value}; + case 'description': + return {'r.description': {like: `%${value}%`}}; + case 'workerFk': + case 'vehicleFk': + case 'agencyModeFk': + return {[param]: value}; + } + }); + + filter = mergeFilters(ctx.args.filter, {where}); + + let stmts = []; + let stmt; + + stmt = new ParameterizedSQL( + `SELECT + r.id, + r.workerFk, + r.created, + r.vehicleFk, + r.agencyModeFk, + r.time, + r.isOk, + r.kmStart, + r.kmEnd, + r.started, + r.finished, + r.gestdocFk, + r.cost, + r.m3, + r.description, + am.name agencyName, + u.nickname AS workerNickname, + v.numberPlate AS vehiclePlateNumber + FROM route r + LEFT JOIN agencyMode am ON am.id = r.agencyModeFk + LEFT JOIN vehicle v ON v.id = r.vehicleFk + LEFT JOIN worker w ON w.id = r.workerFk + LEFT JOIN account.user u ON u.id = w.userFk` + ); + + + stmt.merge(conn.makeSuffix(filter)); + let itemsIndex = stmts.push(stmt) - 1; + + let sql = ParameterizedSQL.join(stmts, ';'); + let result = await conn.executeStmt(sql); + return itemsIndex === 0 ? result : result[itemsIndex]; + }; +}; + diff --git a/modules/route/back/models/route.js b/modules/route/back/models/route.js index e3ebabb68..bd637822f 100644 --- a/modules/route/back/models/route.js +++ b/modules/route/back/models/route.js @@ -1,4 +1,5 @@ module.exports = Self => { + require('../methods/route/filter')(Self); require('../methods/route/summary')(Self); require('../methods/route/getTickets')(Self); require('../methods/route/guessPriority')(Self); diff --git a/modules/route/front/index/index.html b/modules/route/front/index/index.html index 43238fd4d..b9935ba45 100644 --- a/modules/route/front/index/index.html +++ b/modules/route/front/index/index.html @@ -1,7 +1,6 @@ @@ -11,9 +10,7 @@ @@ -41,12 +38,12 @@ - {{::route.worker.user.nickname}} + ng-click="$ctrl.showWorkerDescriptor($event, route.workerFk)"> + {{::route.workerNickname}} - {{::route.agencyMode.name | dashIfEmpty}} - {{::route.vehicle.numberPlate | dashIfEmpty}} + {{::route.agencyName | dashIfEmpty}} + {{::route.vehiclePlateNumber | dashIfEmpty}} {{::route.created | dateTime:'dd/MM/yyyy' | dashIfEmpty}} {{::route.m3 | dashIfEmpty}} {{::route.description | dashIfEmpty}} @@ -72,7 +69,7 @@ + worker-fk="$ctrl.selectedWorker">
diff --git a/modules/route/front/index/index.js b/modules/route/front/index/index.js index 0fa102c4d..4b0536fd0 100644 --- a/modules/route/front/index/index.js +++ b/modules/route/front/index/index.js @@ -4,61 +4,15 @@ export default class Controller { constructor($scope, vnToken) { this.accessToken = vnToken.token; this.$ = $scope; - - this.filter = { - include: [ - { - relation: 'agencyMode', - scope: { - fields: ['name'] - } - }, - { - relation: 'vehicle', - scope: { - fields: ['numberPlate'] - } - }, - { - relation: 'worker', - scope: { - fields: ['userFk'], - include: { - relation: 'user', - scope: { - fields: ['nickname'] - } - } - } - }, - ] - }; } - exprBuilder(param, value) { - switch (param) { - case 'search': - return {id: value}; - case 'from': - return {created: {gte: value}}; - case 'to': - return {created: {lte: value}}; - case 'workerFk': - case 'vehicleFk': - case 'agencyModeFk': - case 'm3': - case 'description': - return {[param]: value}; - } - } - - showWorkerDescriptor(event, userId) { + showWorkerDescriptor(event, workerFk) { if (event.defaultPrevented) return; event.preventDefault(); - event.stopPropagation(); + event.stopImmediatePropagation(); - this.selectedWorker = userId; + this.selectedWorker = workerFk; this.$.workerDescriptor.parent = event.target; this.$.workerDescriptor.show(); } @@ -69,6 +23,13 @@ export default class Controller { event.preventDefault(); event.stopImmediatePropagation(); } + + onSearch(params) { + if (params) + this.$.model.applyFilter(null, params); + else + this.$.model.clear(); + } } Controller.$inject = ['$scope', 'vnToken'];