From 11c6fd2da7045a8543b737a7f3071cdb3cfe0b5e Mon Sep 17 00:00:00 2001 From: Bernat Date: Fri, 1 Mar 2019 12:19:12 +0100 Subject: [PATCH] Cau 8402 fix test and update structure --- modules/order/back/methods/order/getTaxes.js | 2 +- .../back/methods/order/specs/getTaxes.spec.js | 13 ++- .../back/methods/order/specs/getTotal.spec.js | 4 +- .../back/methods/order/specs/getVAT.spec.js | 7 +- .../back/methods/order/specs/summary.spec.js | 2 +- modules/order/back/methods/order/summary.js | 1 - .../db/install/changes/1-orderGetTotal.sql | 4 +- services/db/install/changes/3-orderGetTax.sql | 90 +++++++++++-------- 8 files changed, 72 insertions(+), 51 deletions(-) diff --git a/modules/order/back/methods/order/getTaxes.js b/modules/order/back/methods/order/getTaxes.js index 84101acd8..72d91d23a 100644 --- a/modules/order/back/methods/order/getTaxes.js +++ b/modules/order/back/methods/order/getTaxes.js @@ -38,7 +38,7 @@ module.exports = Self => { stmts.push('CALL hedera.orderGetTax()'); - let orderTaxIndex = stmts.push('SELECT * FROM tmp.orderTax') - 1; + let orderTaxIndex = stmts.push('SELECT * FROM tmp.orderAmount') - 1; stmts.push(` DROP TEMPORARY TABLE diff --git a/modules/order/back/methods/order/specs/getTaxes.spec.js b/modules/order/back/methods/order/specs/getTaxes.spec.js index b41183c98..303b72050 100644 --- a/modules/order/back/methods/order/specs/getTaxes.spec.js +++ b/modules/order/back/methods/order/specs/getTaxes.spec.js @@ -13,10 +13,19 @@ describe('order getTaxes()', () => { expect(result.length).toEqual(0); }); - it('should call the getTaxes method and return the taxes if its called with a known id', async() => { + it('should call the getTaxes method and return the taxes splited if different type of taxes', async() => { let result = await app.models.Order.getTaxes(1); - expect(result[0].tax).toEqual(9.49); + const expectedResult = result[0].tax + result[1].tax; + + expect(expectedResult).toEqual(20.29); + expect(result.length).toEqual(2); + }); + + it('should call the getTaxes method and return the taxes for them same type', async() => { + let result = await app.models.Order.getTaxes(2); + + expect(result[0].tax).toEqual(9.1); expect(result.length).toEqual(1); }); }); diff --git a/modules/order/back/methods/order/specs/getTotal.spec.js b/modules/order/back/methods/order/specs/getTotal.spec.js index b5a942024..613f00241 100644 --- a/modules/order/back/methods/order/specs/getTotal.spec.js +++ b/modules/order/back/methods/order/specs/getTotal.spec.js @@ -1,9 +1,9 @@ const app = require('vn-loopback/server/server'); describe('order getTotal()', () => { - it('should return the total', async() => { + it('should return the order total', async() => { let result = await app.models.Order.getTotal(1); - expect(result).toEqual(145.09); + expect(result).toEqual(155.89); }); }); diff --git a/modules/order/back/methods/order/specs/getVAT.spec.js b/modules/order/back/methods/order/specs/getVAT.spec.js index 6f9b3d55d..bc0abd9af 100644 --- a/modules/order/back/methods/order/specs/getVAT.spec.js +++ b/modules/order/back/methods/order/specs/getVAT.spec.js @@ -2,10 +2,9 @@ const app = require('vn-loopback/server/server'); describe('order getVAT()', () => { it('should call the getVAT method and return the response', async() => { - await app.models.Order.getVAT(1) - .then(response => { - expect(response).toEqual(9.49); - }); + const result = await app.models.Order.getVAT(1); + + expect(result).toEqual(20.29); }); it(`should call the getVAT method and return zero if doesn't have lines`, async() => { diff --git a/modules/order/back/methods/order/specs/summary.spec.js b/modules/order/back/methods/order/specs/summary.spec.js index b0b142a89..f6f917ede 100644 --- a/modules/order/back/methods/order/specs/summary.spec.js +++ b/modules/order/back/methods/order/specs/summary.spec.js @@ -23,7 +23,7 @@ describe('order summary()', () => { it('should return a summary object containing VAT for 1 order', async() => { let result = await app.models.Order.summary(1); - expect(Math.round(result.VAT * 100) / 100).toEqual(9.49); + expect(Math.round(result.VAT * 100) / 100).toEqual(20.29); }); it('should return a summary object containing total for 1 order', async() => { diff --git a/modules/order/back/methods/order/summary.js b/modules/order/back/methods/order/summary.js index e860209b4..5acc5fb2e 100644 --- a/modules/order/back/methods/order/summary.js +++ b/modules/order/back/methods/order/summary.js @@ -25,7 +25,6 @@ module.exports = Self => { summary.subTotal = getSubTotal(summary.rows); summary.VAT = await models.Order.getVAT(orderId); summary.total = await models.Order.getTotal(orderId); - return summary; }; diff --git a/services/db/install/changes/1-orderGetTotal.sql b/services/db/install/changes/1-orderGetTotal.sql index 1e15741c6..dbbc990f2 100644 --- a/services/db/install/changes/1-orderGetTotal.sql +++ b/services/db/install/changes/1-orderGetTotal.sql @@ -18,9 +18,9 @@ DROP TEMPORARY TABLE IF EXISTS tmp.orderTotal; CREATE TEMPORARY TABLE tmp.orderTotal (INDEX (orderFk)) ENGINE = MEMORY -SELECT o.orderFk, IFNULL(SUM(ot.taxBase + ot.tax + ot.equalizationTax), 0.0) AS total +SELECT o.orderFk, IFNULL(SUM(ot.taxableBase + ot.tax), 0.0) AS total FROM tmp.order o -LEFT JOIN tmp.orderTax ot ON o.orderFk = ot.orderFk +LEFT JOIN tmp.orderAmount ot ON o.orderFk = ot.orderFk GROUP BY orderFk; DROP TEMPORARY TABLE IF EXISTS tmp.orderTax; diff --git a/services/db/install/changes/3-orderGetTax.sql b/services/db/install/changes/3-orderGetTax.sql index 73cb7c42e..e9ed5a339 100644 --- a/services/db/install/changes/3-orderGetTax.sql +++ b/services/db/install/changes/3-orderGetTax.sql @@ -3,49 +3,63 @@ DROP procedure IF EXISTS `orderGetTax`; DELIMITER $$ USE `hedera`$$ - CREATE DEFINER=`root`@`%` PROCEDURE `orderGetTax`() -READS SQL DATA + READS SQL DATA BEGIN /** -* Calcula el IVA, y el recargo de equivalencia de un pedido -* desglosados por tipos. -* -* @tabla tmp.order Contiene los identificadores de los pedidos -* @treturn tmp.orderTax Bases imponibles, IVA y recargo de equivalencia -*/ -CALL vn.taxGetRates (NULL); + * Calcula el IVA, y el recargo de equivalencia de un pedido + * desglosados por tipos. + * + * @param vOrder El identificador del pedido + * @treturn tmp.orderTax Bases imponibles, IVA y recargo de equivalencia + */ + DROP TEMPORARY TABLE IF EXISTS tmp.addressCompany; + CREATE TEMPORARY TABLE tmp.addressCompany + (INDEX (addressFk, companyFk)) + ENGINE = MEMORY + SELECT DISTINCT o.address_id addressFk, o.company_id companyFk + FROM tmp.order tmpOrder + JOIN hedera.order o ON o.id = tmpOrder.orderFk; --- Calcula el IVA y el recargo desglosado. + CALL vn.addressTaxArea (); -DROP TEMPORARY TABLE IF EXISTS tmp.orderTax; -CREATE TEMPORARY TABLE tmp.orderTax -(INDEX (orderFk)) -ENGINE = MEMORY -SELECT id orderFk, t.type, t.taxBase, -CAST(IF(t.hasTax, t.taxBase * x.rate, 0) AS DECIMAL(10,2)) tax, -CAST(IF(t.hasEqualizationTax, t.taxBase * x.equalizationTax, 0) AS DECIMAL(10,2)) equalizationTax -FROM ( -SELECT o.id, g.countryFk, g.type -,SUM(CAST(m.amount * m.price AS DECIMAL(10,2))) taxBase -,NOT(c.isVies AND p.countryFk <> c.countryFk) hasTax -,c.isEqualizated != FALSE AS hasEqualizationTax -FROM `order` o -JOIN tmp.order tmpo ON tmpo.orderFk = o.id -JOIN orderRow m ON m.orderFk = o.id -JOIN vn.item a ON a.id = m.itemFk -JOIN vn.client c ON c.id = o.customer_id -JOIN vn.supplier p ON p.id = o.company_id -JOIN tmp.taxClass g -ON g.countryFk = p.countryFk AND g.taxClassFk = a.taxClassFk -GROUP BY o.id, g.type -) t -JOIN tmp.taxType x -ON x.countryFk = t.countryFk AND x.type = t.type; + -- Calcula el IVA y el recargo desglosado. + DROP TEMPORARY TABLE IF EXISTS tmp.orderTax; + CREATE TEMPORARY TABLE tmp.orderTax + (INDEX (orderFk)) + ENGINE = MEMORY + SELECT o.id orderFk, + tc.code, + SUM(m.amount * m.price) taxableBase, + pgc.rate + FROM tmp.order tmpOrder + JOIN `order` o ON o.id = tmpOrder.orderFk + JOIN orderRow m ON m.orderFk = o.id + JOIN vn.item i ON i.id = m.itemFk + JOIN vn.client c ON c.id = o.customer_id + JOIN vn.supplier s ON s.id = o.company_id + JOIN tmp.addressTaxArea ata + ON ata.addressFk = o.address_id AND ata.companyFk = o.company_id + JOIN vn.itemTaxCountry itc + ON itc.itemFk = i.id AND itc.countryFk = s.countryFk + JOIN vn.bookingPlanner bp + ON bp.countryFk = s.countryFk + AND bp.taxAreaFk = ata.areaFk + AND bp.taxClassFk = itc.taxClassFk + JOIN vn.pgc ON pgc.code = bp.pgcFk + JOIN vn.taxClass tc ON tc.id = bp.taxClassFk + GROUP BY tmpOrder.orderFk, pgc.code,pgc.rate + HAVING taxableBase != 0; + + DROP TEMPORARY TABLE IF EXISTS tmp.orderAmount; + CREATE TEMPORARY TABLE tmp.orderAmount + (INDEX (orderFk)) + ENGINE = MEMORY + SELECT orderFk, taxableBase, SUM(CAST(taxableBase * rate / 100 AS DECIMAL(10, 2))) tax,code + FROM tmp.orderTax + GROUP BY orderFk, code; -DROP TEMPORARY TABLE -tmp.taxClass, -tmp.taxType; END$$ -DELIMITER; \ No newline at end of file +DELIMITER ; +