Merge pull request 'refactor: refs #8153 Optimized order_getTax' (!3188) from 8153-order_getTaxOptimize into dev
gitea/salix/pipeline/head There was a failure building this commit
Details
gitea/salix/pipeline/head There was a failure building this commit
Details
Reviewed-on: #3188 Reviewed-by: Carlos Andrés <carlosap@verdnatura.es>
This commit is contained in:
commit
a426e9c7c6
|
@ -9,45 +9,54 @@ BEGIN
|
||||||
* @param vOrder El identificador del pedido
|
* @param vOrder El identificador del pedido
|
||||||
* @return tmp.orderTax Bases imponibles, IVA y recargo de equivalencia
|
* @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;
|
DROP TEMPORARY TABLE IF EXISTS tmp.addressCompany;
|
||||||
CREATE TEMPORARY TABLE tmp.addressCompany
|
CREATE TEMPORARY TABLE tmp.addressCompany
|
||||||
(INDEX (addressFk, companyFk))
|
(INDEX (addressFk, companyFk))
|
||||||
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 (
|
||||||
|
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,
|
tc.code,
|
||||||
SUM(m.amount * m.price) taxableBase,
|
SUM(o.total) taxableBase,
|
||||||
pgc.rate
|
pgc.rate
|
||||||
FROM tmp.`order` tmpOrder
|
FROM orders o
|
||||||
JOIN `order` o ON o.id = tmpOrder.orderFk
|
JOIN vn.bookingPlanner bp ON bp.countryFk = o.countryFk
|
||||||
JOIN orderRow m ON m.orderFk = o.id
|
AND bp.taxAreaFk = o.areaFk
|
||||||
JOIN vn.item i ON i.id = m.itemFk
|
AND bp.taxClassFk = o.taxClassFk
|
||||||
JOIN vn.`client` c ON c.id = o.customer_id
|
JOIN vn.pgc ON pgc.code = bp.pgcFk
|
||||||
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
|
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;
|
||||||
|
|
||||||
|
-- No poner create or replace, ya que da problemas
|
||||||
DROP TEMPORARY TABLE IF EXISTS tmp.orderAmount;
|
DROP TEMPORARY TABLE IF EXISTS tmp.orderAmount;
|
||||||
CREATE TEMPORARY TABLE tmp.orderAmount
|
CREATE TEMPORARY TABLE tmp.orderAmount
|
||||||
(INDEX (orderFk))
|
(INDEX (orderFk))
|
||||||
|
|
Loading…
Reference in New Issue