From 5a11e9ac768500ec56fff025084392316620cf38 Mon Sep 17 00:00:00 2001 From: jorgep Date: Tue, 5 Mar 2024 16:14:05 +0100 Subject: [PATCH] feat: refs #6942 toUnbook --- modules/client/back/models/XDiario.json | 8 +- .../back/methods/invoice-in/toUnbook.js | 78 +++++++++++++++++++ modules/invoiceIn/back/models/invoice-in.js | 1 + 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 modules/invoiceIn/back/methods/invoice-in/toUnbook.js diff --git a/modules/client/back/models/XDiario.json b/modules/client/back/models/XDiario.json index be543393d..e1eac2aa1 100644 --- a/modules/client/back/models/XDiario.json +++ b/modules/client/back/models/XDiario.json @@ -76,7 +76,13 @@ }, "enlazadoSage": { "type": "boolean" - } + }, + "enlazado": { + "type": "boolean" + }, + "CLAVE": { + "type": "number" + } }, "relations": { "company": { diff --git a/modules/invoiceIn/back/methods/invoice-in/toUnbook.js b/modules/invoiceIn/back/methods/invoice-in/toUnbook.js new file mode 100644 index 000000000..4ba37330b --- /dev/null +++ b/modules/invoiceIn/back/methods/invoice-in/toUnbook.js @@ -0,0 +1,78 @@ +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); + + await models.Xdiario.destroyAll({where: {ASIEN: bookEntry}}, myOptions); + } 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; + } + }; +}; diff --git a/modules/invoiceIn/back/models/invoice-in.js b/modules/invoiceIn/back/models/invoice-in.js index af5efbcdf..ffa285ce7 100644 --- a/modules/invoiceIn/back/models/invoice-in.js +++ b/modules/invoiceIn/back/models/invoice-in.js @@ -10,6 +10,7 @@ module.exports = Self => { require('../methods/invoice-in/invoiceInEmail')(Self); require('../methods/invoice-in/getSerial')(Self); require('../methods/invoice-in/corrective')(Self); + require('../methods/invoice-in/toUnbook')(Self); Self.rewriteDbError(function(err) { if (err.code === 'ER_ROW_IS_REFERENCED_2' && err.sqlMessage.includes('vehicleInvoiceIn'))