This commit is contained in:
Carlos Jimenez Ruiz 2019-03-01 13:56:30 +01:00
commit 0bdc66093c
8 changed files with 72 additions and 51 deletions

View File

@ -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

View File

@ -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);
});
});

View File

@ -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);
});
});

View File

@ -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() => {

View File

@ -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() => {

View File

@ -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;
};

View File

@ -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;

View File

@ -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;
DELIMITER ;