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

81 lines
2.3 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, invoiceInId, options) => {
2024-03-05 15:14:05 +00:00
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 bookEntry = await models.Xdiario.findOne({
2024-03-05 15:14:05 +00:00
fields: ['ASIEN'],
where: {
and: [
{key: invoiceInId},
2024-05-23 14:12:21 +00:00
{enlazado: false},
{enlazadoSage: false}
2024-03-05 15:14:05 +00:00
]
}
}, myOptions);
let asien = bookEntry?.ASIEN;
if (asien) {
accountingEntries = await models.Xdiario.count({ASIEN: asien}, myOptions);
await models.Xdiario.destroyAll({ASIEN: asien}, myOptions);
await Self.updateAll({id: invoiceInId}, {isBooked: false}, myOptions);
2024-03-05 15:14:05 +00:00
} else {
const linkedBookEntry = await models.Xdiario.findOne({
fields: ['ASIEN'],
where: {
key: invoiceInId,
2024-03-05 15:14:05 +00:00
and: [{or: [{enlazado: true, enlazadoSage: true}]}]
}
}, myOptions);
asien = linkedBookEntry?.ASIEN;
2024-03-05 15:14:05 +00:00
isLinked = true;
}
if (tx) await tx.commit();
return {
isLinked,
bookEntry: asien,
2024-03-05 15:14:05 +00:00
accountingEntries
};
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};