Merge pull request 'refactor: refs #5586 Optimized SQL negativeBases' (!2007) from 5586-optimizeNegativeBases into dev
gitea/salix/pipeline/head This commit looks good Details
gitea/salix/pipeline/pr-dev There was a failure building this commit Details

Reviewed-on: #2007
Reviewed-by: Javi Gallego <jgallego@verdnatura.es>
Reviewed-by: Carlos Andrés <carlosap@verdnatura.es>
This commit is contained in:
Guillermo Bonet 2024-02-20 06:28:04 +00:00
commit 4d2637764e
2 changed files with 52 additions and 58 deletions

View File

@ -9,6 +9,8 @@ BEGIN
* @return tmp.ticketAmount (ticketFk, taxableBase, tax, code) * @return tmp.ticketAmount (ticketFk, taxableBase, tax, code)
* @return tmp.ticketTax (ticketFk, pgcFk, taxableBase, rate, code) Impuesto desglosado para cada ticket. * @return tmp.ticketTax (ticketFk, pgcFk, taxableBase, rate, code) Impuesto desglosado para cada ticket.
*/ */
-- Mantengo el drop porque si no da error en los tests de back de salix
-- Table 'addressCompany' was locked with a READ lock and can't be updated'
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))
@ -24,11 +26,11 @@ BEGIN
SET areaFk = vTaxArea; SET areaFk = vTaxArea;
END IF; END IF;
/* Solo se calcula la base imponible (taxableBase) y el impuesto se calculará posteriormente -- Solo se calcula la base imponible (taxableBase) y
* No se debería cambiar el sistema por problemas con los decimales -- el impuesto se calculará posteriormente
*/ -- No se debería cambiar el sistema por problemas con los decimales
DROP TEMPORARY TABLE IF EXISTS tmp.ticketTax;
CREATE TEMPORARY TABLE tmp.ticketTax CREATE OR REPLACE TEMPORARY TABLE tmp.ticketTax
(PRIMARY KEY (ticketFk, code, rate)) (PRIMARY KEY (ticketFk, code, rate))
ENGINE = MEMORY ENGINE = MEMORY
SELECT * FROM ( SELECT * FROM (
@ -43,22 +45,21 @@ BEGIN
JOIN item i ON i.id = s.itemFk JOIN item i ON i.id = s.itemFk
JOIN ticket t ON t.id = tmpTicket.ticketFk JOIN ticket t ON t.id = tmpTicket.ticketFk
JOIN supplier su ON su.id = t.companyFk JOIN supplier su ON su.id = t.companyFk
JOIN tmp.addressTaxArea ata JOIN tmp.addressTaxArea ata ON ata.addressFk = t.addressFk
ON ata.addressFk = t.addressFk AND ata.companyFk = t.companyFk AND ata.companyFk = t.companyFk
JOIN itemTaxCountry itc JOIN itemTaxCountry itc ON itc.itemFk = i.id
ON itc.itemFk = i.id AND itc.countryFk = su.countryFk AND itc.countryFk = su.countryFk
JOIN bookingPlanner bp JOIN bookingPlanner bp ON bp.countryFk = su.countryFk
ON bp.countryFk = su.countryFk
AND bp.taxAreaFk = ata.areaFk AND bp.taxAreaFk = ata.areaFk
AND bp.taxClassFk = itc.taxClassFk AND bp.taxClassFk = itc.taxClassFk
JOIN pgc ON pgc.code = bp.pgcFk JOIN pgc ON pgc.code = bp.pgcFk
JOIN taxClass tc ON tc.id = bp.taxClassFk JOIN taxClass tc ON tc.id = bp.taxClassFk
GROUP BY tmpTicket.ticketFk, pgc.code, pgc.rate GROUP BY tmpTicket.ticketFk, pgc.code, pgc.rate
HAVING taxableBase <> 0) t3 HAVING taxableBase
) t3
ORDER BY priority; ORDER BY priority;
DROP TEMPORARY TABLE IF EXISTS tmp.ticketServiceTax; CREATE OR REPLACE TEMPORARY TABLE tmp.ticketServiceTax
CREATE TEMPORARY TABLE tmp.ticketServiceTax
(PRIMARY KEY (ticketFk, code, rate)) (PRIMARY KEY (ticketFk, code, rate))
ENGINE = MEMORY ENGINE = MEMORY
SELECT tt.ticketFk, SELECT tt.ticketFk,
@ -70,24 +71,22 @@ BEGIN
JOIN ticketService ts ON ts.ticketFk = tt.ticketFk JOIN ticketService ts ON ts.ticketFk = tt.ticketFk
JOIN ticket t ON t.id = tt.ticketFk JOIN ticket t ON t.id = tt.ticketFk
JOIN supplier su ON su.id = t.companyFk JOIN supplier su ON su.id = t.companyFk
JOIN tmp.addressTaxArea ata JOIN tmp.addressTaxArea ata ON ata.addressFk = t.addressFk
ON ata.addressFk = t.addressFk AND ata.companyFk = t.companyFk AND ata.companyFk = t.companyFk
JOIN bookingPlanner bp JOIN bookingPlanner bp ON bp.countryFk = su.countryFk
ON bp.countryFk = su.countryFk
AND bp.taxAreaFk = ata.areaFk AND bp.taxAreaFk = ata.areaFk
AND bp.taxClassFk = ts.taxClassFk AND bp.taxClassFk = ts.taxClassFk
JOIN pgc ON pgc.code = bp.pgcFk JOIN pgc ON pgc.code = bp.pgcFk
JOIN taxClass tc ON tc.id = bp.taxClassFk JOIN taxClass tc ON tc.id = bp.taxClassFk
GROUP BY tt.ticketFk, pgc.code GROUP BY tt.ticketFk, pgc.code
HAVING taxableBase <> 0; HAVING taxableBase;
INSERT INTO tmp.ticketTax (ticketFk, pgcFk, taxableBase, rate, code) INSERT INTO tmp.ticketTax (ticketFk, pgcFk, taxableBase, rate, code)
SELECT ts.ticketFk, ts.pgcFk, ts.taxableBase, ts.rate, ts.code SELECT ts.ticketFk, ts.pgcFk, ts.taxableBase, ts.rate, ts.code
FROM tmp.ticketServiceTax ts FROM tmp.ticketServiceTax ts
ON DUPLICATE KEY UPDATE ticketTax.taxableBase = VALUES (taxableBase) + ticketTax.taxableBase ; ON DUPLICATE KEY UPDATE ticketTax.taxableBase = VALUES (taxableBase) + ticketTax.taxableBase ;
DROP TEMPORARY TABLE IF EXISTS tmp.ticketAmount; CREATE OR REPLACE TEMPORARY TABLE tmp.ticketAmount
CREATE TEMPORARY TABLE tmp.ticketAmount
(INDEX (ticketFk)) (INDEX (ticketFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT ticketFk, SELECT ticketFk,
@ -97,7 +96,8 @@ BEGIN
FROM tmp.ticketTax FROM tmp.ticketTax
GROUP BY ticketFk, code; GROUP BY ticketFk, code;
DROP TEMPORARY TABLE tmp.addressCompany; DROP TEMPORARY TABLE
DROP TEMPORARY TABLE tmp.addressTaxArea; tmp.addressCompany,
tmp.addressTaxArea;
END$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -46,23 +46,19 @@ module.exports = Self => {
const stmts = []; const stmts = [];
let stmt; let stmt;
stmts.push(`DROP TEMPORARY TABLE IF EXISTS tmp.ticket`);
stmts.push(new ParameterizedSQL( stmts.push(new ParameterizedSQL(
`CREATE TEMPORARY TABLE tmp.ticket `CREATE OR REPLACE TEMPORARY TABLE tmp.ticket
(KEY (ticketFk)) (KEY (ticketFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT id ticketFk SELECT id ticketFk
FROM ticket t FROM ticket t
WHERE shipped BETWEEN ? AND ? WHERE shipped BETWEEN ? AND util.dayEnd(?)
AND refFk IS NULL`, [args.from, args.to])); AND refFk IS NULL`, [args.from, args.to]));
stmts.push(`CALL vn.ticket_getTax(NULL)`); stmts.push(`CALL vn.ticket_getTax(NULL)`);
stmts.push(`DROP TEMPORARY TABLE IF EXISTS tmp.filter`);
stmts.push(new ParameterizedSQL( stmts.push(new ParameterizedSQL(
`CREATE TEMPORARY TABLE tmp.filter `CREATE OR REPLACE TEMPORARY TABLE tmp.filter
ENGINE = MEMORY ENGINE = MEMORY
SELECT SELECT co.code company,
co.code company,
cou.country, cou.country,
c.id clientId, c.id clientId,
c.socialName clientSocialName, c.socialName clientSocialName,
@ -88,15 +84,13 @@ module.exports = Self => {
GROUP BY ticketFk GROUP BY ticketFk
HAVING taxableBase < 0 HAVING taxableBase < 0
) negativeBase ON negativeBase.ticketFk = t.id ) negativeBase ON negativeBase.ticketFk = t.id
WHERE t.shipped BETWEEN ? AND ? WHERE t.shipped BETWEEN ? AND util.dayEnd(?)
AND t.refFk IS NULL AND t.refFk IS NULL
AND c.typeFk IN ('normal','trust') AND c.typeFk IN ('normal','trust')
GROUP BY t.clientFk, negativeBase.taxableBase GROUP BY t.clientFk, negativeBase.taxableBase
HAVING amount < 0`, [args.from, args.to])); HAVING amount < 0`, [args.from, args.to]));
stmt = new ParameterizedSQL(` stmt = new ParameterizedSQL(`SELECT * FROM tmp.filter`);
SELECT f.*
FROM tmp.filter f`);
if (args.filter) { if (args.filter) {
stmt.merge(conn.makeWhere(args.filter.where)); stmt.merge(conn.makeWhere(args.filter.where));