From 4bc391e083441b983c876d7c886bdbac47fbae25 Mon Sep 17 00:00:00 2001 From: gerard Date: Thu, 28 Jun 2018 15:24:27 +0200 Subject: [PATCH] Tarea #354 seccion ticket.line menu de mover lineas --- .../common/methods/agency/getFirstShipped.js | 27 ++++++++++ .../common/methods/sale/moveToNewTicket.js | 52 +++++++++++++++++++ .../common/methods/sale/moveToTicket.js | 2 +- .../loopback/common/methods/ticket/create.js | 40 ++++++++++++++ services/loopback/common/models/agency.js | 2 + services/loopback/common/models/ticket.js | 3 +- 6 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 services/loopback/common/methods/agency/getFirstShipped.js create mode 100644 services/loopback/common/methods/sale/moveToNewTicket.js create mode 100644 services/loopback/common/methods/ticket/create.js diff --git a/services/loopback/common/methods/agency/getFirstShipped.js b/services/loopback/common/methods/agency/getFirstShipped.js new file mode 100644 index 000000000..15d831209 --- /dev/null +++ b/services/loopback/common/methods/agency/getFirstShipped.js @@ -0,0 +1,27 @@ +module.exports = Self => { + Self.remoteMethod('getFirstShipped', { + description: 'Returns the first shipped and landed possible for params', + accessType: '', + accepts: [{ + arg: 'params', + type: 'object', + required: true, + description: 'agencyModeFk, addressFk, warehouseFk' + }], + returns: { + type: 'object', + root: true + }, + http: { + path: `/getFirstShipped`, + verb: 'get' + } + }); + + Self.getFirstShipped = async params => { + let query = `CALL vn.agencyHourGetFirstShipped(?, ?, ?)`; + let [result] = await Self.rawSql(query, [params.agencyModeFk, params.addressFk, params.warehouseFk]); + + return result[0]; + }; +}; diff --git a/services/loopback/common/methods/sale/moveToNewTicket.js b/services/loopback/common/methods/sale/moveToNewTicket.js new file mode 100644 index 000000000..7f9854c56 --- /dev/null +++ b/services/loopback/common/methods/sale/moveToNewTicket.js @@ -0,0 +1,52 @@ +module.exports = Self => { + Self.remoteMethod('moveToNewTicket', { + description: 'Change the state of a ticket', + accessType: '', + accepts: [{ + arg: 'ticketParams', + type: 'object', + required: true, + description: '[sales IDs], newTicketFk, actualTicketFk', + http: {source: 'body'} + }, { + arg: 'sales', + type: 'object', + required: true, + description: '[sales IDs]', + http: {source: 'body'} + }], + returns: { + type: 'string', + root: true + }, + http: { + path: `/moveToNewTicket`, + verb: 'post' + } + }); + + Self.moveToNewTicket = async params => { + let model = Self.app.models; + let thisTicketIsEditable = await model.Ticket.isEditable(params.ticket.oldTicketFk); + if (!thisTicketIsEditable) + throw new Error(`The sales of this ticket can't be modified`); + + let travelDates = await model.Agency.getFirstShipped(params.ticket); + console.log(travelDates); + let newTicketParams = { + clientFk: params.ticket.clientFk, + addressFk: params.ticket.addressFk, + agencyModeFk: params.ticket.agencyModeFk, + warehouseFk: params.ticket.warehouseFk, + shipped: travelDates.vShipped, + landed: travelDates.vLanded + }; + + let newTicket = await model.Ticket.create(newTicketParams); + + for (let i = 0; i < params.sales.length; i++) { + await model.Sale.update({id: params.sales[i].id}, {ticketFk: newTicket.id}); + } + return newTicket; + }; +}; diff --git a/services/loopback/common/methods/sale/moveToTicket.js b/services/loopback/common/methods/sale/moveToTicket.js index 45b7f49df..4ecc72d19 100644 --- a/services/loopback/common/methods/sale/moveToTicket.js +++ b/services/loopback/common/methods/sale/moveToTicket.js @@ -26,7 +26,7 @@ module.exports = Self => { let newTicketIsEditable = await Self.app.models.Ticket.isEditable(params.newTicketFk); if (!newTicketIsEditable) - throw new Error(`The sales of this ticket can't be modified`); + throw new Error(`The sales of that ticket can't be modified`); for (let i = 0; i < params.sales.length; i++) { await Self.app.models.Sale.update({id: params.sales[i].id}, {ticketFk: params.newTicketFk}); diff --git a/services/loopback/common/methods/ticket/create.js b/services/loopback/common/methods/ticket/create.js new file mode 100644 index 000000000..db7c100cb --- /dev/null +++ b/services/loopback/common/methods/ticket/create.js @@ -0,0 +1,40 @@ +module.exports = Self => { + Self.remoteMethod('create', { + description: 'Create a newticket and returns the new ID', + accessType: 'WRITE', + accepts: [{ + arg: 'params', + type: 'object', + required: true, + description: 'ClientFk, Shipped, WharehouseFk, CompanyFk, AddressFk, AgencyModeFk, RouteFk, Landed', + http: {source: 'body'} + }], + returns: { + type: 'number', + root: true + }, + http: { + path: `/create`, + verb: 'post' + } + }); + + Self.create = async params => { + let existsAddress = await Self.app.models.Address.findOne({where: {id: params.addressFk, clientFk: params.clientFk}}); + if (!existsAddress) + throw new Error(`This address doesn't exist`); + + let query = `CALL vn.ticketListCreate(?, ?, ?, ?, ?, ?, ?, ?);`; + let result = await Self.rawSql(query, [ + params.clientFk, + params.shipped, + params.wharehouseFk, + params.companyFk, + params.addressFk, + params.agencyModeFk, + params.routeFk, + params.landed + ]); + return result; + }; +}; diff --git a/services/loopback/common/models/agency.js b/services/loopback/common/models/agency.js index ac6c4d03a..251ba183a 100644 --- a/services/loopback/common/models/agency.js +++ b/services/loopback/common/models/agency.js @@ -1,3 +1,5 @@ module.exports = Self => { Self.defineScope({where: {isManaged: {neq: 0}}}); + //require('../methods/agency/sendsThatDay')(Self); + require('../methods/agency/getFirstShipped')(Self); }; diff --git a/services/loopback/common/models/ticket.js b/services/loopback/common/models/ticket.js index 0d74b4eb1..2c26b92f8 100644 --- a/services/loopback/common/models/ticket.js +++ b/services/loopback/common/models/ticket.js @@ -7,9 +7,10 @@ module.exports = Self => { require('../methods/ticket/getTotal')(Self); require('../methods/ticket/getTaxes')(Self); require('../methods/ticket/componentUpdate')(Self); -// require('../methods/ticket/create')(Self); + require('../methods/ticket/create')(Self); require('../methods/ticket/isEditable')(Self); require('../methods/ticket/threeLastActive')(Self); require('../methods/ticket/deleted')(Self); require('../methods/ticket/getVAT')(Self); + require('../methods/ticket/getSalesPersonMana')(Self); };