2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `hedera`.`order_getTax`()
|
|
|
|
READS SQL DATA
|
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Calcula el IVA, y el recargo de equivalencia de un pedido
|
|
|
|
* desglosados por tipos.
|
|
|
|
*
|
|
|
|
* @param vOrder El identificador del pedido
|
|
|
|
* @return tmp.orderTax Bases imponibles, IVA y recargo de equivalencia
|
|
|
|
*/
|
2024-11-12 12:25:51 +00:00
|
|
|
-- No poner create or replace, ya que da problemas
|
2024-01-15 11:31:03 +00:00
|
|
|
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
|
2024-11-12 12:04:23 +00:00
|
|
|
JOIN `order` o ON o.id = tmpOrder.orderFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-11-12 12:04:23 +00:00
|
|
|
CALL vn.addressTaxArea();
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
-- Calcula el IVA y el recargo desglosado.
|
2024-11-12 12:04:23 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tmp.orderTax
|
|
|
|
(PRIMARY KEY (orderFk, code, rate))
|
2024-01-15 11:31:03 +00:00
|
|
|
ENGINE = MEMORY
|
2024-11-12 12:04:23 +00:00
|
|
|
WITH orders AS (
|
|
|
|
SELECT tor.orderFk,
|
|
|
|
oro.amount * oro.price total,
|
|
|
|
s.countryFk,
|
|
|
|
ata.areaFk,
|
|
|
|
itc.taxClassFk
|
2024-11-13 06:10:52 +00:00
|
|
|
FROM hedera.orderRow oro
|
2024-11-12 12:04:23 +00:00
|
|
|
JOIN tmp.order tor ON tor.orderFk = oro.orderFk
|
2024-11-13 06:12:49 +00:00
|
|
|
JOIN hedera.`order` o ON o.id = tor.orderFk
|
2024-11-12 12:04:23 +00:00
|
|
|
JOIN vn.item i ON i.id = oro.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
|
|
|
|
HAVING total
|
|
|
|
)
|
|
|
|
SELECT o.orderFk,
|
2024-01-15 11:31:03 +00:00
|
|
|
tc.code,
|
2024-11-12 12:04:23 +00:00
|
|
|
SUM(o.total) taxableBase,
|
2024-01-15 11:31:03 +00:00
|
|
|
pgc.rate
|
2024-11-12 12:04:23 +00:00
|
|
|
FROM orders o
|
|
|
|
JOIN vn.bookingPlanner bp ON bp.countryFk = o.countryFk
|
|
|
|
AND bp.taxAreaFk = o.areaFk
|
|
|
|
AND bp.taxClassFk = o.taxClassFk
|
|
|
|
JOIN vn.pgc ON pgc.code = bp.pgcFk
|
2024-01-15 11:31:03 +00:00
|
|
|
JOIN vn.taxClass tc ON tc.id = bp.taxClassFk
|
2024-11-12 12:04:23 +00:00
|
|
|
GROUP BY o.orderFk, pgc.code, pgc.rate
|
|
|
|
HAVING taxableBase
|
|
|
|
ORDER BY bp.priority;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-11-12 12:25:51 +00:00
|
|
|
-- No poner create or replace, ya que da problemas
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.orderAmount;
|
|
|
|
CREATE TEMPORARY TABLE tmp.orderAmount
|
2024-01-15 11:31:03 +00:00
|
|
|
(INDEX (orderFk))
|
|
|
|
ENGINE = MEMORY
|
|
|
|
SELECT orderFk, taxableBase, `code`,
|
|
|
|
SUM(CAST(taxableBase * rate / 100 AS DECIMAL(10, 2))) tax
|
|
|
|
FROM tmp.orderTax
|
|
|
|
GROUP BY orderFk, `code`;
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tmp.addressTaxArea;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|