refactor: refs #8153 Optimized order_getTax #3188

Merged
guillermo merged 3 commits from 8153-order_getTaxOptimize into dev 2024-11-13 06:07:39 +00:00
1 changed files with 33 additions and 27 deletions
Showing only changes of commit d52f537f0c - Show all commits

View File

@ -15,41 +15,47 @@ BEGIN
ENGINE = MEMORY ENGINE = MEMORY
SELECT DISTINCT o.address_id addressFk, o.company_id companyFk SELECT DISTINCT o.address_id addressFk, o.company_id companyFk
FROM tmp.`order` tmpOrder FROM tmp.`order` tmpOrder
JOIN hedera.`order` o ON o.id = tmpOrder.orderFk; JOIN `order` o ON o.id = tmpOrder.orderFk;
CALL vn.addressTaxArea; CALL vn.addressTaxArea();
-- Calcula el IVA y el recargo desglosado. -- Calcula el IVA y el recargo desglosado.
CREATE OR REPLACE TEMPORARY TABLE tmp.orderTax
DROP TEMPORARY TABLE IF EXISTS tmp.orderTax; (PRIMARY KEY (orderFk, code, rate))
CREATE TEMPORARY TABLE tmp.orderTax
(INDEX (orderFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT o.id orderFk, WITH orders AS (
tc.code, SELECT tor.orderFk,
SUM(m.amount * m.price) taxableBase, oro.amount * oro.price total,
pgc.rate s.countryFk,
FROM tmp.`order` tmpOrder ata.areaFk,
JOIN `order` o ON o.id = tmpOrder.orderFk itc.taxClassFk
JOIN orderRow m ON m.orderFk = o.id FROM orderRow oro
JOIN vn.item i ON i.id = m.itemFk JOIN tmp.order tor ON tor.orderFk = oro.orderFk
JOIN `order` o ON o.id = tor.orderFk
JOIN vn.item i ON i.id = oro.itemFk
JOIN vn.`client` c ON c.id = o.customer_id JOIN vn.`client` c ON c.id = o.customer_id
JOIN vn.supplier s ON s.id = o.company_id JOIN vn.supplier s ON s.id = o.company_id
JOIN tmp.addressTaxArea ata JOIN tmp.addressTaxArea ata ON ata.addressFk = o.address_id
ON ata.addressFk = o.address_id AND ata.companyFk = o.company_id AND ata.companyFk = o.company_id
JOIN vn.itemTaxCountry itc JOIN vn.itemTaxCountry itc ON itc.itemFk = i.id
ON itc.itemFk = i.id AND itc.countryFk = s.countryFk AND itc.countryFk = s.countryFk
JOIN vn.bookingPlanner bp HAVING total
ON bp.countryFk = s.countryFk )
AND bp.taxAreaFk = ata.areaFk SELECT o.orderFk,
AND bp.taxClassFk = itc.taxClassFk tc.code,
JOIN vn.pgc ON pgc.`code` = bp.pgcFk SUM(o.total) taxableBase,
pgc.rate
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
JOIN vn.taxClass tc ON tc.id = bp.taxClassFk JOIN vn.taxClass tc ON tc.id = bp.taxClassFk
GROUP BY tmpOrder.orderFk, pgc.`code`, pgc.rate GROUP BY o.orderFk, pgc.code, pgc.rate
HAVING taxableBase != 0; HAVING taxableBase
ORDER BY bp.priority;
DROP TEMPORARY TABLE IF EXISTS tmp.orderAmount; CREATE OR REPLACE TEMPORARY TABLE tmp.orderAmount
CREATE TEMPORARY TABLE tmp.orderAmount
(INDEX (orderFk)) (INDEX (orderFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT orderFk, taxableBase, `code`, SELECT orderFk, taxableBase, `code`,