diff --git a/modules/invoiceIn/back/methods/invoice-in/getTotals.js b/modules/invoiceIn/back/methods/invoice-in/getTotals.js index 641476570f..626948fbd5 100644 --- a/modules/invoiceIn/back/methods/invoice-in/getTotals.js +++ b/modules/invoiceIn/back/methods/invoice-in/getTotals.js @@ -1,5 +1,5 @@ module.exports = Self => { - Self.remoteMethodCtx('getTotals', { + Self.remoteMethod('getTotals', { description: 'Return totals for an invoiceIn', accessType: 'READ', accepts: { @@ -19,7 +19,7 @@ module.exports = Self => { } }); - Self.getTotals = async(ctx, id, options) => { + Self.getTotals = async(id, options) => { let tx; const myOptions = {}; diff --git a/modules/invoiceIn/back/methods/invoice-in/toBook.js b/modules/invoiceIn/back/methods/invoice-in/toBook.js index c8ee1282e4..ab2befc05a 100644 --- a/modules/invoiceIn/back/methods/invoice-in/toBook.js +++ b/modules/invoiceIn/back/methods/invoice-in/toBook.js @@ -22,7 +22,6 @@ module.exports = Self => { }); Self.toBook = async(ctx, id, options) => { - const models = Self.app.models; let tx; const myOptions = {}; @@ -35,10 +34,6 @@ module.exports = Self => { } try { - const totals = await models.InvoiceIn.getTotals(id); - if (totals.totalDueDay != totals.totalTaxableBase && totals.totalDueDay != totals.totalVal) - throw new UserError(`Amounts do not match`); - await Self.rawSql(`CALL vn.invoiceInBookingMain(?)`, [id], myOptions); if (tx) await tx.commit(); } catch (e) { diff --git a/modules/invoiceIn/front/descriptor/index.js b/modules/invoiceIn/front/descriptor/index.js index 9f3edff93d..8af4806d41 100644 --- a/modules/invoiceIn/front/descriptor/index.js +++ b/modules/invoiceIn/front/descriptor/index.js @@ -51,14 +51,17 @@ class Controller extends Descriptor { return this.getData(`InvoiceIns/${this.id}`, {filter}) .then(res => this.entity = res.data); } + 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'; + const taxableBaseNotEqualDueDay = res.data.totalDueDay != res.data.totalTaxableBase; + const vatNotEqualDueDay = res.data.totalDueDay != res.data.totalVat; + if (taxableBaseNotEqualDueDay && vatNotEqualDueDay) + message += 'amountsDoNotMatch'; }), this.$http.get('InvoiceInDueDays/count', { filter: { @@ -73,7 +76,7 @@ class Controller extends Descriptor { }) ]).finally(() => { - if (message > '') + if (message.length) this.$.confirmToBookAnyway.show(); else onAcceptToBook(); @@ -81,8 +84,10 @@ class Controller extends Descriptor { } onAcceptToBook() { - this.$http.post(`InvoiceIns/${this.id}/toBook`); - this.vnApp.showSuccess(this.$t('InvoiceIn booked')); + this.$http.post(`InvoiceIns/${this.id}/toBook`) + .then(() => { + this.vnApp.showSuccess(this.$t('InvoiceIn booked')); + }); } } diff --git a/modules/invoiceIn/front/descriptor/index.spec.js b/modules/invoiceIn/front/descriptor/index.spec.js index 235d04a43c..e7215b6dc2 100644 --- a/modules/invoiceIn/front/descriptor/index.spec.js +++ b/modules/invoiceIn/front/descriptor/index.spec.js @@ -9,31 +9,26 @@ describe('vnInvoiceInDescriptor', () => { beforeEach(inject(($componentController, _$httpBackend_) => { $httpBackend = _$httpBackend_; controller = $componentController('vnInvoiceInDescriptor', {$element: null}); + controller.invoiceIn = {id: 1}; + $httpBackend.when('GET', `InvoiceIns/${controller.invoiceIn.id}`).respond({id: 1}); })); describe('loadData()', () => { it(`should perform a get query to store the invoice in data into the controller`, () => { - const id = 1; - const response = {id: 1}; - - $httpBackend.expectGET(`InvoiceIns/${id}`).respond(response); - controller.id = id; - $httpBackend.flush(); - - expect(controller.invoiceIn).toEqual(response); + expect(controller.invoiceIn).toEqual({id: 1}); }); }); - describe('toBook()', () => { - it(`should perform a post query to book the invoiSce`, () => { + describe('onAcceptToBook()', () => { + it(`should perform a post query to book the invoice`, () => { + controller.vnApp = {showSuccess: jest.fn()}; const id = 1; - const response = {id: 1}; - $httpBackend.expectPOST(`InvoiceIns/${id}/toBook`).respond(response); - controller.id = id; + $httpBackend.expectPOST(`InvoiceIns/${id}/toBook`).respond(); + controller.onAcceptToBook(); $httpBackend.flush(); - expect(controller.invoiceIn).toEqual(response); + expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('InvoiceIn booked'); }); }); });