From 4f1fd5412d5769087a2b4b9306b9f9221985d6a5 Mon Sep 17 00:00:00 2001 From: jgallego Date: Tue, 28 Sep 2021 10:57:51 +0200 Subject: [PATCH] faltan test --- db/changes/10370-cucumber/00-acl.sql | 2 + .../back/methods/invoice-in/getTotals.js | 52 +++++++++++++++++++ .../back/methods/invoice-in/toBook.js | 7 --- modules/invoiceIn/back/models/invoice-in.js | 16 +----- modules/invoiceIn/front/descriptor/index.html | 7 ++- modules/invoiceIn/front/descriptor/index.js | 35 +++++++++++-- 6 files changed, 93 insertions(+), 26 deletions(-) create mode 100644 db/changes/10370-cucumber/00-acl.sql create mode 100644 modules/invoiceIn/back/methods/invoice-in/getTotals.js diff --git a/db/changes/10370-cucumber/00-acl.sql b/db/changes/10370-cucumber/00-acl.sql new file mode 100644 index 000000000..5d8d73e5a --- /dev/null +++ b/db/changes/10370-cucumber/00-acl.sql @@ -0,0 +1,2 @@ +INSERT INTO salix.ACL (model, property, accessType, permission, principalType, principalId) + VALUES('InvoiceInDueDay', '*', '*', 'ALLOW', 'ROLE', 'administrative'); diff --git a/modules/invoiceIn/back/methods/invoice-in/getTotals.js b/modules/invoiceIn/back/methods/invoice-in/getTotals.js new file mode 100644 index 000000000..641476570 --- /dev/null +++ b/modules/invoiceIn/back/methods/invoice-in/getTotals.js @@ -0,0 +1,52 @@ +module.exports = Self => { + Self.remoteMethodCtx('getTotals', { + description: 'Return totals for an invoiceIn', + accessType: 'READ', + accepts: { + arg: 'id', + type: 'number', + required: true, + description: 'invoiceIn id', + http: {source: 'path'} + }, + returns: { + type: 'object', + root: true + }, + http: { + path: '/:id/getTotals', + verb: 'GET' + } + }); + + Self.getTotals = async(ctx, id, options) => { + let tx; + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + return (await Self.rawSql(` + SELECT iit.*, + SUM(iidd.amount) totalDueDay + FROM vn.invoiceIn ii + LEFT JOIN (SELECT SUM(iit.taxableBase) totalTaxableBase, + SUM(iit.taxableBase * (1 + (ti.PorcentajeIva / 100))) totalVat + FROM vn.invoiceInTax iit + LEFT JOIN sage.TiposIva ti ON ti.CodigoIva = iit.taxTypeSageFk + WHERE iit.invoiceInFk = ?) iit ON TRUE + LEFT JOIN vn.invoiceInDueDay iidd ON iidd.invoiceInFk = ii.id + WHERE + ii.id = ?`, [id, id]))[0]; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/invoiceIn/back/methods/invoice-in/toBook.js b/modules/invoiceIn/back/methods/invoice-in/toBook.js index 1444df63e..c8ee1282e 100644 --- a/modules/invoiceIn/back/methods/invoice-in/toBook.js +++ b/modules/invoiceIn/back/methods/invoice-in/toBook.js @@ -39,13 +39,6 @@ module.exports = Self => { if (totals.totalDueDay != totals.totalTaxableBase && totals.totalDueDay != totals.totalVal) throw new UserError(`Amounts do not match`); - /* const now = new Date(); - const hasFuturePayments = await models.InvoiceInDueDay.findOne({ - where: { - invoiceInFk: id, - dueDated: {gte: now} - } - });*/ await Self.rawSql(`CALL vn.invoiceInBookingMain(?)`, [id], myOptions); if (tx) await tx.commit(); } catch (e) { diff --git a/modules/invoiceIn/back/models/invoice-in.js b/modules/invoiceIn/back/models/invoice-in.js index eb57b079f..3b5aa65d9 100644 --- a/modules/invoiceIn/back/models/invoice-in.js +++ b/modules/invoiceIn/back/models/invoice-in.js @@ -3,19 +3,5 @@ module.exports = Self => { require('../methods/invoice-in/summary')(Self); require('../methods/invoice-in/clone')(Self); require('../methods/invoice-in/toBook')(Self); - - Self.getTotals = async function getTotals(invoiceInFk) { - return (await Self.rawSql(` - SELECT iit.*, - SUM(iidd.amount) totalDueDay - FROM vn.invoiceIn ii - LEFT JOIN (SELECT SUM(iit.taxableBase) totalTaxableBase, - SUM(iit.taxableBase * (1 + (ti.PorcentajeIva / 100))) totalVat - FROM vn.invoiceInTax iit - LEFT JOIN sage.TiposIva ti ON ti.CodigoIva = iit.taxTypeSageFk - WHERE iit.invoiceInFk = ?) iit ON TRUE - LEFT JOIN vn.invoiceInDueDay iidd ON iidd.invoiceInFk = ii.id - WHERE - ii.id = ?`, [invoiceInFk, invoiceInFk]))[0]; - }; + require('../methods/invoice-in/getTotals')(Self); }; diff --git a/modules/invoiceIn/front/descriptor/index.html b/modules/invoiceIn/front/descriptor/index.html index 50efec2d5..89945ce07 100644 --- a/modules/invoiceIn/front/descriptor/index.html +++ b/modules/invoiceIn/front/descriptor/index.html @@ -1,7 +1,7 @@ @@ -72,3 +72,8 @@ + + \ No newline at end of file diff --git a/modules/invoiceIn/front/descriptor/index.js b/modules/invoiceIn/front/descriptor/index.js index c9081146d..9f3edff93 100644 --- a/modules/invoiceIn/front/descriptor/index.js +++ b/modules/invoiceIn/front/descriptor/index.js @@ -51,9 +51,38 @@ class Controller extends Descriptor { return this.getData(`InvoiceIns/${this.id}`, {filter}) .then(res => this.entity = res.data); } - toBook() { - return this.$http.post(`InvoiceIns/${this.id}/toBook`) - .then(() => this.vnApp.showSuccess(this.$t('InvoiceIn booked'))); + checktoBook() { + let message = ''; + const id = this.invoiceIn.id; + this.$q.all([ + this.$http.get(`InvoiceIns/${this.id}/getTotals`) + .then(res => { + if (res.data.totalDueDay != res.data.totalTaxableBase && res.data.totalDueDay != res.data.totalVat) + message += 'amountsNotMatch'; + }), + this.$http.get('InvoiceInDueDays/count', { + filter: { + where: { + invoiceInFk: id, + dueDated: {gte: new Date()} + } + }}) + .then(res => { + if (res) + message += 'future payments'; + }) + + ]).finally(() => { + if (message > '') + this.$.confirmToBookAnyway.show(); + else + onAcceptToBook(); + }); + } + + onAcceptToBook() { + this.$http.post(`InvoiceIns/${this.id}/toBook`); + this.vnApp.showSuccess(this.$t('InvoiceIn booked')); } }