From 3e522b08b6c7fc93eb36b1721814f6efbfe428f0 Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 11 Oct 2023 11:43:21 +0200 Subject: [PATCH] refs #6184 Added saveCmr --- modules/ticket/back/methods/ticket/saveCmr.js | 82 +++++++++++++++++++ .../ticket/back/methods/ticket/saveSign.js | 3 +- modules/ticket/back/models/ticket-methods.js | 1 + 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 modules/ticket/back/methods/ticket/saveCmr.js diff --git a/modules/ticket/back/methods/ticket/saveCmr.js b/modules/ticket/back/methods/ticket/saveCmr.js new file mode 100644 index 000000000..ce39ba20a --- /dev/null +++ b/modules/ticket/back/methods/ticket/saveCmr.js @@ -0,0 +1,82 @@ +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethodCtx('saveCmr', { + description: 'Save sign', + accessType: 'WRITE', + accepts: + [ + { + arg: 'tickets', + type: ['number'], + required: true, + description: 'The tickets' + } + ], + http: { + path: `/saveCmr`, + verb: 'POST' + } + }); + + Self.saveCmr = async(ctx, tickets, options) => { + const models = Self.app.models; + const myOptions = {userId: ctx.req.accessToken.userId}; + let tx; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + for (const ticketId of tickets) { + const cmrFk = await models.Ticket.findOne( + {where: {id: ticketId}, + fields: ['cmrFk'] + }, myOptions); + + if (cmrFk) { + const dmsTypeCmr = await models.DmsType.findOne({ + where: {code: 'ticket'}, + fields: ['id'] + }, myOptions); + + const hasDmsCmr = await models.TicketDms.findOne({ + where: { ticketFk: ticketId }, + fields: ['dmsFk'], + include: { + relation: 'dms', + scope: { + where: { dmsTypeFk: dmsTypeCmr } + } + } + }, myOptions); + + if (!hasDmsCmr) { + const zip = await models.Route.downloadCmrsZip(ctx, cmr, myOptions); + const ticket = await models.Ticket.findById(ticketId, null, myOptions); + const ctxUploadFile = Object.assign({}, zip); + ctxUploadFile.args = { + warehouseId: ticket.warehouseFk, + companyId: ticket.companyFk, + dmsTypeId: dmsTypeCmr.id, + reference: '', + description: `Documento comprimido - CMR ${cmrFk}`, + hasFile: false + }; + await models.Dms.uploadFile(ctxUploadFile, myOptions); + } + } + } + if (tx) await tx.commit(); + return; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/ticket/back/methods/ticket/saveSign.js b/modules/ticket/back/methods/ticket/saveSign.js index 9888328e7..015a95afb 100644 --- a/modules/ticket/back/methods/ticket/saveSign.js +++ b/modules/ticket/back/methods/ticket/saveSign.js @@ -91,7 +91,7 @@ module.exports = Self => { } ] }, myOptions); - const dmsType = await models.DmsType.findOne({where: {code: 'Ticket'}, fields: ['id']}, myOptions); + const dmsType = await models.DmsType.findOne({where: {code: 'ticket'}, fields: ['id']}, myOptions); const ctxUploadFile = Object.assign({}, ctx); if (ticket.route() === null) throw new UserError('Ticket without route'); @@ -131,6 +131,7 @@ module.exports = Self => { const ticket = await models.Ticket.findById(ticketId, null, myOptions); await ticket.updateAttribute('isSigned', true, myOptions); await Self.rawSql(`CALL vn.ticket_setState(?, ?)`, [ticketId, 'DELIVERED'], myOptions); + await models.Ticket.saveCmr(ticketId, myOptions); } if (tx) await tx.commit(); diff --git a/modules/ticket/back/models/ticket-methods.js b/modules/ticket/back/models/ticket-methods.js index 14cb104be..c932548e1 100644 --- a/modules/ticket/back/models/ticket-methods.js +++ b/modules/ticket/back/models/ticket-methods.js @@ -41,6 +41,7 @@ module.exports = function(Self) { require('../methods/ticket/collectionLabel')(Self); require('../methods/ticket/expeditionPalletLabel')(Self); require('../methods/ticket/saveSign')(Self); + require('../methods/ticket/saveCmr')(Self); require('../methods/ticket/invoiceTickets')(Self); require('../methods/ticket/docuwareDownload')(Self); };