From 8f6a88f9b6cee4721472b5f85178d520bf5f37e0 Mon Sep 17 00:00:00 2001 From: Joan Date: Tue, 19 Jun 2018 12:10:38 +0200 Subject: [PATCH] Display sale taxes #342 --- client/ticket/src/sale/index.html | 13 +++++++- client/ticket/src/sale/index.js | 23 +++++++++++++ client/ticket/src/summary/index.html | 3 +- .../loopback/common/methods/ticket/getVAT.js | 32 +++++++++++++++++++ .../methods/ticket/specs/get-VAT.spec.js | 19 +++++++++++ .../loopback/common/methods/ticket/summary.js | 13 +------- services/loopback/common/models/ticket.js | 1 + 7 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 services/loopback/common/methods/ticket/getVAT.js create mode 100644 services/loopback/common/methods/ticket/specs/get-VAT.spec.js diff --git a/client/ticket/src/sale/index.html b/client/ticket/src/sale/index.html index bffe95fd8..9e7c48f49 100644 --- a/client/ticket/src/sale/index.html +++ b/client/ticket/src/sale/index.html @@ -1,4 +1,4 @@ - + @@ -98,6 +98,17 @@ No results + + + +
+

Subtotal {{$ctrl.subTotal | currency:' €':2}}

+

VAT {{$ctrl.VAT | currency:' €':2}}

+

Total {{$ctrl.total | currency:' €':2}}

+
+ + +
diff --git a/client/ticket/src/sale/index.js b/client/ticket/src/sale/index.js index d7f2de103..29532e771 100644 --- a/client/ticket/src/sale/index.js +++ b/client/ticket/src/sale/index.js @@ -18,6 +18,29 @@ class Controller extends FilterTicketList { ]; } + getTaxes() { + this.getSubTotal(); + this.getVAT(); + } + + getSubTotal() { + let sales = this.$.index.model.instances; + + this.subTotal = 0.00; + sales.forEach(sale => { + this.subTotal += sale.quantity * sale.price; + }); + } + + getVAT() { + this.$http.get(`/ticket/api/Tickets/${this.ticket.id}/getVAT`).then(res => { + if (res.data) { + this.VAT = res.data; + this.total = this.subTotal + this.VAT; + } + }); + } + get isEditable() { try { return !this.ticket.tracking.state.alertLevel; diff --git a/client/ticket/src/summary/index.html b/client/ticket/src/summary/index.html index 79422001e..3e33e01d8 100644 --- a/client/ticket/src/summary/index.html +++ b/client/ticket/src/summary/index.html @@ -46,10 +46,9 @@

Subtotal {{$ctrl.summary.subTotal | currency:' €':2}}

-

VAT {{$ctrl.summary.totalTax | currency:' €':2}}

+

VAT {{$ctrl.summary.VAT | currency:' €':2}}

Total {{$ctrl.summary.total | currency:' €':2}}

-
diff --git a/services/loopback/common/methods/ticket/getVAT.js b/services/loopback/common/methods/ticket/getVAT.js new file mode 100644 index 000000000..509a0d13c --- /dev/null +++ b/services/loopback/common/methods/ticket/getVAT.js @@ -0,0 +1,32 @@ +module.exports = Self => { + Self.remoteMethod('getVAT', { + description: 'Returns ticket total VAT', + accessType: 'READ', + accepts: [{ + arg: 'id', + type: 'number', + required: true, + description: 'ticket id', + http: {source: 'path'} + }], + returns: { + type: 'number', + root: true + }, + http: { + path: `/:id/getVAT`, + verb: 'GET' + } + }); + + Self.getVAT = async ticketFk => { + let totalTax = 0.00; + let taxes = await Self.app.models.Ticket.getTaxes(ticketFk); + + taxes.forEach(tax => { + totalTax += tax.tax; + }); + + return totalTax; + }; +}; diff --git a/services/loopback/common/methods/ticket/specs/get-VAT.spec.js b/services/loopback/common/methods/ticket/specs/get-VAT.spec.js new file mode 100644 index 000000000..9f78c46d9 --- /dev/null +++ b/services/loopback/common/methods/ticket/specs/get-VAT.spec.js @@ -0,0 +1,19 @@ +const app = require(`${servicesDir}/ticket/server/server`); + +describe('ticket getVAT()', () => { + it('should call the getVAT method and return the response', done => { + app.models.Ticket.getVAT(1) + .then(response => { + expect(response).toEqual(37.55); + done(); + }); + }); + + it(`should call the getVAT method and return zero if doesn't have lines`, done => { + app.models.Ticket.getVAT(13) + .then(response => { + expect(response).toEqual(0); + done(); + }); + }); +}); diff --git a/services/loopback/common/methods/ticket/summary.js b/services/loopback/common/methods/ticket/summary.js index 74018843b..0dcc5f1ee 100644 --- a/services/loopback/common/methods/ticket/summary.js +++ b/services/loopback/common/methods/ticket/summary.js @@ -24,7 +24,7 @@ module.exports = Self => { let summaryObj = await getTicketData(Self, ticketFk); summaryObj.sales = await getSales(models.Sale, ticketFk); summaryObj.subTotal = getSubTotal(summaryObj.sales); - summaryObj.totalTax = await getTotalTax(models.Ticket, ticketFk); + summaryObj.VAT = await models.Ticket.getVAT(ticketFk); summaryObj.total = await models.Ticket.getTotal(ticketFk); return summaryObj; @@ -112,15 +112,4 @@ module.exports = Self => { return subTotal; } - - async function getTotalTax(ticket, ticketFk) { - let totalTax = 0.00; - let taxes = await ticket.getTaxes(ticketFk); - - taxes.forEach(tax => { - totalTax += tax.tax; - }); - - return totalTax; - } }; diff --git a/services/loopback/common/models/ticket.js b/services/loopback/common/models/ticket.js index a0a99a7ca..0d74b4eb1 100644 --- a/services/loopback/common/models/ticket.js +++ b/services/loopback/common/models/ticket.js @@ -11,4 +11,5 @@ module.exports = Self => { require('../methods/ticket/isEditable')(Self); require('../methods/ticket/threeLastActive')(Self); require('../methods/ticket/deleted')(Self); + require('../methods/ticket/getVAT')(Self); };