refs #6184 Added saveCmr
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Guillermo Bonet 2023-10-11 11:43:21 +02:00
parent 1a2b58af9c
commit 3e522b08b6
3 changed files with 85 additions and 1 deletions

View File

@ -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;
}
};
};

View File

@ -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();

View File

@ -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);
};