diff --git a/modules/client/back/models/XDiario.json b/modules/client/back/models/XDiario.json index e1eac2aa1..5c277783a 100644 --- a/modules/client/back/models/XDiario.json +++ b/modules/client/back/models/XDiario.json @@ -80,8 +80,11 @@ "enlazado": { "type": "boolean" }, - "CLAVE": { - "type": "number" + "key": { + "type": "number", + "mysql": { + "columnName": "CLAVE" + } } }, "relations": { diff --git a/modules/invoiceIn/back/methods/invoice-in/specs/toUnbook.spec.js b/modules/invoiceIn/back/methods/invoice-in/specs/toUnbook.spec.js new file mode 100644 index 000000000..b7d98e307 --- /dev/null +++ b/modules/invoiceIn/back/methods/invoice-in/specs/toUnbook.spec.js @@ -0,0 +1,32 @@ +const models = require('vn-loopback/server/server').models; + +describe('invoiceIn toUnbook()', () => { + it('should check that invoiceIn is unbooked', async() => { + const userId = 1; + const ctx = { + req: { + + accessToken: {userId: userId}, + headers: {origin: 'http://localhost:5000'}, + } + }; + const invoiceInId = 1; + const tx = await models.InvoiceIn.beginTransaction({}); + const options = {transaction: tx}; + + try { + await models.InvoiceIn.toBook(ctx, invoiceInId, options); + const bookEntry = await models.InvoiceIn.toUnbook(ctx, invoiceInId, options); + const invoiceIn = await models.InvoiceIn.findById(invoiceInId, null, options); + + expect(bookEntry.accountingEntries).toEqual(4); + expect(bookEntry.isLinked).toBeFalsy(); + expect(invoiceIn.isBooked).toEqual(false); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); diff --git a/modules/invoiceIn/back/methods/invoice-in/toUnbook.js b/modules/invoiceIn/back/methods/invoice-in/toUnbook.js index 1aa25ed0b..dca0d84dc 100644 --- a/modules/invoiceIn/back/methods/invoice-in/toUnbook.js +++ b/modules/invoiceIn/back/methods/invoice-in/toUnbook.js @@ -19,7 +19,7 @@ module.exports = Self => { } }); - Self.toUnbook = async(ctx, id, options) => { + Self.toUnbook = async(ctx, invoiceInId, options) => { let tx; const models = Self.app.models; const myOptions = {userId: ctx.req.accessToken.userId}; @@ -36,27 +36,27 @@ module.exports = Self => { let isLinked; let accountingEntries; - let {'ASIEN': bookEntry} = await models.Xdiario.findOne({ + let bookEntry = await models.Xdiario.findOne({ fields: ['ASIEN'], where: { and: [ - {'CLAVE': id}, + {key: invoiceInId}, {enlazado: 0}, {enlazadoSage: 0} ] } }, myOptions); - if (bookEntry) { - accountingEntries = await models.Xdiario.count({ASIEN: bookEntry}, myOptions); + if (bookEntry?.ASIEN) { + accountingEntries = await models.Xdiario.count({ASIEN: bookEntry.ASIEN}, myOptions); - await models.Xdiario.destroyAll({ASIEN: bookEntry}, myOptions); - await Self.updateAll({id}, {isBooked: false}, myOptions); + await models.Xdiario.destroyAll({ASIEN: bookEntry.ASIEN}, myOptions); + await Self.updateAll({id: invoiceInId}, {isBooked: false}, myOptions); } else { const linkedBookEntry = await models.Xdiario.findOne({ fields: ['ASIEN'], where: { - CLAVE: id, + key: invoiceInId, and: [{or: [{enlazado: true, enlazadoSage: true}]}] } }, myOptions); @@ -68,7 +68,7 @@ module.exports = Self => { return { isLinked, - bookEntry, + bookEntry: bookEntry?.ASIEN, accountingEntries }; } catch (e) {