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'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-03-11 11:11:13 +00:00
|
|
|
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;
|
|
|
|
|
2024-03-11 11:11:13 +00:00
|
|
|
let bookEntry = await models.Xdiario.findOne({
|
2024-03-05 15:14:05 +00:00
|
|
|
fields: ['ASIEN'],
|
|
|
|
where: {
|
|
|
|
and: [
|
2024-03-11 11:11:13 +00:00
|
|
|
{key: invoiceInId},
|
2024-05-23 14:12:21 +00:00
|
|
|
{enlazado: false},
|
|
|
|
{enlazadoSage: false}
|
2024-03-05 15:14:05 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}, myOptions);
|
|
|
|
|
2024-05-27 10:34:27 +00:00
|
|
|
let asien = bookEntry?.ASIEN;
|
|
|
|
if (asien) {
|
|
|
|
accountingEntries = await models.Xdiario.count({ASIEN: asien}, myOptions);
|
2024-03-06 12:41:56 +00:00
|
|
|
|
2024-05-27 10:34:27 +00:00
|
|
|
await models.Xdiario.destroyAll({ASIEN: asien}, myOptions);
|
2024-03-11 11:11:13 +00:00
|
|
|
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: {
|
2024-03-11 11:11:13 +00:00
|
|
|
key: invoiceInId,
|
2024-03-05 15:14:05 +00:00
|
|
|
and: [{or: [{enlazado: true, enlazadoSage: true}]}]
|
|
|
|
}
|
|
|
|
}, myOptions);
|
|
|
|
|
2024-05-27 10:34:27 +00:00
|
|
|
asien = linkedBookEntry?.ASIEN;
|
2024-03-05 15:14:05 +00:00
|
|
|
isLinked = true;
|
|
|
|
}
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
|
|
|
return {
|
|
|
|
isLinked,
|
2024-05-27 10:34:27 +00:00
|
|
|
bookEntry: asien,
|
2024-03-05 15:14:05 +00:00
|
|
|
accountingEntries
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|