2019-02-27 11:03:54 +00:00
|
|
|
USE `hedera`;
|
|
|
|
DROP procedure IF EXISTS `orderGetTax`;
|
|
|
|
|
|
|
|
DELIMITER $$
|
|
|
|
USE `hedera`$$
|
|
|
|
CREATE DEFINER=`root`@`%` PROCEDURE `orderGetTax`()
|
2019-03-01 11:19:12 +00:00
|
|
|
READS SQL DATA
|
2019-02-27 11:03:54 +00:00
|
|
|
BEGIN
|
|
|
|
/**
|
2019-03-01 11:19:12 +00:00
|
|
|
* 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;
|
2019-02-27 11:03:54 +00:00
|
|
|
|
2019-03-01 11:19:12 +00:00
|
|
|
CALL vn.addressTaxArea ();
|
2019-02-27 11:03:54 +00:00
|
|
|
|
2019-03-01 11:19:12 +00:00
|
|
|
-- 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;
|
2019-02-27 11:03:54 +00:00
|
|
|
|
|
|
|
END$$
|
|
|
|
|
2019-03-01 11:19:12 +00:00
|
|
|
DELIMITER ;
|
|
|
|
|