From d52f537f0cb3d993eba447dbdbe42ffb21c70c12 Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 12 Nov 2024 13:04:23 +0100 Subject: [PATCH 1/2] refactor: refs #8153 Optimized order_getTax --- .../hedera/procedures/order_getTax.sql | 60 ++++++++++--------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/db/routines/hedera/procedures/order_getTax.sql b/db/routines/hedera/procedures/order_getTax.sql index d24ffe7ef..8fa965343 100644 --- a/db/routines/hedera/procedures/order_getTax.sql +++ b/db/routines/hedera/procedures/order_getTax.sql @@ -15,41 +15,47 @@ BEGIN 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; + JOIN `order` o ON o.id = tmpOrder.orderFk; - CALL vn.addressTaxArea; + CALL vn.addressTaxArea(); -- Calcula el IVA y el recargo desglosado. - - DROP TEMPORARY TABLE IF EXISTS tmp.orderTax; - CREATE TEMPORARY TABLE tmp.orderTax - (INDEX (orderFk)) + CREATE OR REPLACE TEMPORARY TABLE tmp.orderTax + (PRIMARY KEY (orderFk, code, rate)) ENGINE = MEMORY - SELECT o.id orderFk, + WITH orders AS ( + SELECT tor.orderFk, + oro.amount * oro.price total, + s.countryFk, + ata.areaFk, + itc.taxClassFk + FROM orderRow oro + 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.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, tc.code, - SUM(m.amount * m.price) taxableBase, + SUM(o.total) 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 + 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 - GROUP BY tmpOrder.orderFk, pgc.`code`, pgc.rate - HAVING taxableBase != 0; + GROUP BY o.orderFk, pgc.code, pgc.rate + HAVING taxableBase + ORDER BY bp.priority; - DROP TEMPORARY TABLE IF EXISTS tmp.orderAmount; - CREATE TEMPORARY TABLE tmp.orderAmount + CREATE OR REPLACE TEMPORARY TABLE tmp.orderAmount (INDEX (orderFk)) ENGINE = MEMORY SELECT orderFk, taxableBase, `code`, From 127c6fced0cd744794f2eda0e65d3667dfcf8bab Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 12 Nov 2024 13:25:51 +0100 Subject: [PATCH 2/2] refactor: refs #8153 Optimized order_getTax --- db/routines/hedera/procedures/order_getTax.sql | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/db/routines/hedera/procedures/order_getTax.sql b/db/routines/hedera/procedures/order_getTax.sql index 8fa965343..90c47bfc7 100644 --- a/db/routines/hedera/procedures/order_getTax.sql +++ b/db/routines/hedera/procedures/order_getTax.sql @@ -9,6 +9,7 @@ BEGIN * @param vOrder El identificador del pedido * @return tmp.orderTax Bases imponibles, IVA y recargo de equivalencia */ + -- No poner create or replace, ya que da problemas DROP TEMPORARY TABLE IF EXISTS tmp.addressCompany; CREATE TEMPORARY TABLE tmp.addressCompany (INDEX (addressFk, companyFk)) @@ -55,7 +56,9 @@ BEGIN HAVING taxableBase ORDER BY bp.priority; - CREATE OR REPLACE TEMPORARY TABLE tmp.orderAmount + -- No poner create or replace, ya que da problemas + DROP TEMPORARY TABLE IF EXISTS tmp.orderAmount; + CREATE TEMPORARY TABLE tmp.orderAmount (INDEX (orderFk)) ENGINE = MEMORY SELECT orderFk, taxableBase, `code`,