salix/modules/invoiceIn/back/methods/invoice-in/toUnbook.js

80 lines
2.2 KiB
JavaScript
Raw Normal View History

2024-03-05 15:14:05 +00:00
module.exports = Self => {
Self.remoteMethodCtx('toUnbook', {
description: 'To unbook the invoiceIn',
accessType: 'WRITE',
accepts: {
arg: 'id',
type: 'number',
required: true,
description: 'The invoiceIn id',
http: {source: 'path'}
},
returns: {
type: 'object',
root: true
},
http: {
path: '/:id/toUnbook',
verb: 'POST'
}
});
Self.toUnbook = async(ctx, id, options) => {
let tx;
const models = Self.app.models;
const myOptions = {userId: ctx.req.accessToken.userId};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
let isLinked;
let accountingEntries;
let {'ASIEN': bookEntry} = await models.Xdiario.findOne({
fields: ['ASIEN'],
where: {
and: [
{'CLAVE': id},
{enlazado: 0},
{enlazadoSage: 0}
]
}
}, myOptions);
if (bookEntry) {
accountingEntries = await models.Xdiario.count({ASIEN: bookEntry}, myOptions);
2024-03-06 08:56:22 +00:00
await models.Xdiario.destroyAll({ASIEN: bookEntry}, myOptions);
await Self.updateAll({id}, {isBooked: false}, myOptions);
2024-03-05 15:14:05 +00:00
} else {
const linkedBookEntry = await models.Xdiario.findOne({
fields: ['ASIEN'],
where: {
CLAVE: id,
and: [{or: [{enlazado: true, enlazadoSage: true}]}]
}
}, myOptions);
bookEntry = linkedBookEntry?.ASIEN;
isLinked = true;
}
if (tx) await tx.commit();
return {
isLinked,
bookEntry,
accountingEntries
};
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};