diff --git a/db/changes/233601/00-ticketAcl.sql b/db/changes/233601/00-ticketAcl.sql new file mode 100644 index 000000000..0bad0f781 --- /dev/null +++ b/db/changes/233601/00-ticketAcl.sql @@ -0,0 +1,3 @@ +INSERT INTO `salix`.`ACL` (`model`,`property`,`accessType`,`permission`,`principalId`) + VALUES + ('Ticket','volume','READ','ALLOW','employee'); diff --git a/modules/ticket/back/methods/ticket/getVolume.js b/modules/ticket/back/methods/ticket/getVolume.js index 6029020ba..5b710e07f 100644 --- a/modules/ticket/back/methods/ticket/getVolume.js +++ b/modules/ticket/back/methods/ticket/getVolume.js @@ -9,41 +9,27 @@ module.exports = Self => { description: 'ticket id', http: {source: 'path'} }], - returns: [{ - arg: 'saleVolume', - type: ['object'] + returns: { + type: 'object', + root: true }, - { - arg: 'packingTypeVolume', - type: ['object'] - }], http: { path: `/:id/getVolume`, verb: 'GET' } }); - Self.getVolume = async(ticketFk, options) => { - const myOptions = {}; - - if (typeof options == 'object') - Object.assign(myOptions, options); - - const saleVolume = await Self.rawSql(` - SELECT saleFk, volume - FROM vn.saleVolume - WHERE ticketFk = ?`, [ticketFk], myOptions); - + Self.getVolume = async ticketFk => { const packingTypeVolume = await Self.rawSql(` SELECT s.itemPackingTypeFk code, - i.description, + i.description, SUM(s.volume) volume FROM vn.saleVolume s - LEFT JOIN vn.itemPackingType i + LEFT JOIN vn.itemPackingType i ON i.code = s.itemPackingTypeFk WHERE s.ticketFk = ? - GROUP BY s.itemPackingTypeFk`, [ticketFk], myOptions); + GROUP BY s.itemPackingTypeFk`, [ticketFk]); - return [saleVolume, packingTypeVolume]; + return packingTypeVolume; }; }; diff --git a/modules/ticket/back/methods/ticket/specs/volume.spec.js b/modules/ticket/back/methods/ticket/specs/volume.spec.js new file mode 100644 index 000000000..4787cdf0d --- /dev/null +++ b/modules/ticket/back/methods/ticket/specs/volume.spec.js @@ -0,0 +1,11 @@ +const models = require('vn-loopback/server/server').models; + +fdescribe('ticket volume()', () => { + it('should return the tickets matching the filter', async() => { + const ticketId = 1; + const filter = {order: ['concept']}; + const result = await models.Ticket.volume(ticketId, filter); + + expect(result.length).toBe(4); + }); +}); diff --git a/modules/ticket/back/methods/ticket/volume.js b/modules/ticket/back/methods/ticket/volume.js new file mode 100644 index 000000000..3cc606c4b --- /dev/null +++ b/modules/ticket/back/methods/ticket/volume.js @@ -0,0 +1,65 @@ +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; + +module.exports = Self => { + Self.remoteMethod('volume', { + description: 'Return the volume of a ticket', + accessType: 'READ', + accepts: [ + { + arg: 'id', + type: 'number', + required: true, + description: 'The ticket id', + http: {source: 'path'} + }, + { + arg: 'filter', + type: 'object' + }], + returns: { + type: 'object', + root: true + }, + http: { + path: '/:id/volume', + verb: 'GET' + } + }); + + Self.volume = async(id, filter) => { + const conn = Self.dataSource.connector; + + const stmt = new ParameterizedSQL( + `SELECT + s.id, + s.itemFk, + s.quantity, + i.name, + i.subName, + i.itemPackingTypeFk, + i.value5, + i.value6, + i.value7, + i.value8, + i.value9, + i.value10, + sv.volume + FROM vn.sale s + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.saleVolume sv ON sv.saleFk = s.id + WHERE s.ticketFk = ?` + , [id] + ); + + stmt.merge(conn.makeSuffix(filter)); + + const result = await conn.executeStmt(stmt); + + const outputArray = result.map(({name, subName, itemPackingTypeFk, ...rest}) => ({ + ...rest, + item: {name, subName, itemPackingTypeFk}, + })); + + return outputArray; + }; +}; diff --git a/modules/ticket/back/models/ticket-methods.js b/modules/ticket/back/models/ticket-methods.js index 14cb104be..efda6b7a4 100644 --- a/modules/ticket/back/models/ticket-methods.js +++ b/modules/ticket/back/models/ticket-methods.js @@ -42,5 +42,6 @@ module.exports = function(Self) { require('../methods/ticket/expeditionPalletLabel')(Self); require('../methods/ticket/saveSign')(Self); require('../methods/ticket/invoiceTickets')(Self); + require('../methods/ticket/volume')(Self); require('../methods/ticket/docuwareDownload')(Self); }; diff --git a/modules/ticket/front/volume/index.html b/modules/ticket/front/volume/index.html index ff0a86772..3d133fc35 100644 --- a/modules/ticket/front/volume/index.html +++ b/modules/ticket/front/volume/index.html @@ -1,10 +1,9 @@ @@ -54,7 +53,7 @@ {{::sale.item.itemPackingTypeFk}} {{::sale.quantity}} - {{::sale.saleVolume.volume | number:3}} + {{::sale.volume | number:3}} diff --git a/modules/ticket/front/volume/index.js b/modules/ticket/front/volume/index.js index 7acecf570..1b7c3d45b 100644 --- a/modules/ticket/front/volume/index.js +++ b/modules/ticket/front/volume/index.js @@ -4,12 +4,6 @@ import Section from 'salix/components/section'; class Controller extends Section { constructor($element, $) { super($element, $); - this.filter = { - include: { - relation: 'item' - }, - order: 'concept' - }; this.ticketVolumes = []; } @@ -25,19 +19,10 @@ class Controller extends Section { } applyVolumes() { - const ticket = this.sales[0].ticketFk; - this.$http.get(`Tickets/${ticket}/getVolume`).then(res => { - const saleVolume = res.data.saleVolume; - - const volumes = new Map(); - for (const volume of saleVolume) - volumes.set(volume.saleFk, volume); - - for (const sale of this.sales) - sale.saleVolume = volumes.get(sale.id); - - this.packingTypeVolume = res.data.packingTypeVolume; - }); + this.$http.get(`Tickets/${this.$params.id}/getVolume`) + .then(res => { + this.packingTypeVolume = res.data; + }); } }