Merge pull request 'refactor: refs #5586 Optimized SQL negativeBases' (!2007) from 5586-optimizeNegativeBases into dev
Reviewed-on: #2007 Reviewed-by: Javi Gallego <jgallego@verdnatura.es> Reviewed-by: Carlos Andrés <carlosap@verdnatura.es>
This commit is contained in:
commit
4d2637764e
|
@ -9,6 +9,8 @@ BEGIN
|
|||
* @return tmp.ticketAmount (ticketFk, taxableBase, tax, code)
|
||||
* @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;
|
||||
CREATE TEMPORARY TABLE tmp.addressCompany
|
||||
(INDEX (addressFk, companyFk))
|
||||
|
@ -24,11 +26,11 @@ BEGIN
|
|||
SET areaFk = vTaxArea;
|
||||
END IF;
|
||||
|
||||
/* Solo se calcula la base imponible (taxableBase) y 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
|
||||
-- Solo se calcula la base imponible (taxableBase) y
|
||||
-- el impuesto se calculará posteriormente
|
||||
-- No se debería cambiar el sistema por problemas con los decimales
|
||||
|
||||
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketTax
|
||||
(PRIMARY KEY (ticketFk, code, rate))
|
||||
ENGINE = MEMORY
|
||||
SELECT * FROM (
|
||||
|
@ -43,22 +45,21 @@ BEGIN
|
|||
JOIN item i ON i.id = s.itemFk
|
||||
JOIN ticket t ON t.id = tmpTicket.ticketFk
|
||||
JOIN supplier su ON su.id = t.companyFk
|
||||
JOIN tmp.addressTaxArea ata
|
||||
ON ata.addressFk = t.addressFk AND ata.companyFk = t.companyFk
|
||||
JOIN itemTaxCountry itc
|
||||
ON itc.itemFk = i.id AND itc.countryFk = su.countryFk
|
||||
JOIN bookingPlanner bp
|
||||
ON bp.countryFk = su.countryFk
|
||||
JOIN tmp.addressTaxArea ata ON ata.addressFk = t.addressFk
|
||||
AND ata.companyFk = t.companyFk
|
||||
JOIN itemTaxCountry itc ON itc.itemFk = i.id
|
||||
AND itc.countryFk = su.countryFk
|
||||
JOIN bookingPlanner bp ON bp.countryFk = su.countryFk
|
||||
AND bp.taxAreaFk = ata.areaFk
|
||||
AND bp.taxClassFk = itc.taxClassFk
|
||||
JOIN pgc ON pgc.code = bp.pgcFk
|
||||
JOIN taxClass tc ON tc.id = bp.taxClassFk
|
||||
GROUP BY tmpTicket.ticketFk, pgc.code, pgc.rate
|
||||
HAVING taxableBase <> 0) t3
|
||||
HAVING taxableBase
|
||||
) t3
|
||||
ORDER BY priority;
|
||||
|
||||
DROP TEMPORARY TABLE IF EXISTS tmp.ticketServiceTax;
|
||||
CREATE TEMPORARY TABLE tmp.ticketServiceTax
|
||||
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketServiceTax
|
||||
(PRIMARY KEY (ticketFk, code, rate))
|
||||
ENGINE = MEMORY
|
||||
SELECT tt.ticketFk,
|
||||
|
@ -70,24 +71,22 @@ BEGIN
|
|||
JOIN ticketService ts ON ts.ticketFk = tt.ticketFk
|
||||
JOIN ticket t ON t.id = tt.ticketFk
|
||||
JOIN supplier su ON su.id = t.companyFk
|
||||
JOIN tmp.addressTaxArea ata
|
||||
ON ata.addressFk = t.addressFk AND ata.companyFk = t.companyFk
|
||||
JOIN bookingPlanner bp
|
||||
ON bp.countryFk = su.countryFk
|
||||
JOIN tmp.addressTaxArea ata ON ata.addressFk = t.addressFk
|
||||
AND ata.companyFk = t.companyFk
|
||||
JOIN bookingPlanner bp ON bp.countryFk = su.countryFk
|
||||
AND bp.taxAreaFk = ata.areaFk
|
||||
AND bp.taxClassFk = ts.taxClassFk
|
||||
JOIN pgc ON pgc.code = bp.pgcFk
|
||||
JOIN taxClass tc ON tc.id = bp.taxClassFk
|
||||
GROUP BY tt.ticketFk, pgc.code
|
||||
HAVING taxableBase <> 0;
|
||||
HAVING taxableBase;
|
||||
|
||||
INSERT INTO tmp.ticketTax (ticketFk, pgcFk, taxableBase, rate, code)
|
||||
SELECT ts.ticketFk, ts.pgcFk, ts.taxableBase, ts.rate, ts.code
|
||||
FROM tmp.ticketServiceTax ts
|
||||
ON DUPLICATE KEY UPDATE ticketTax.taxableBase = VALUES (taxableBase) + ticketTax.taxableBase ;
|
||||
|
||||
DROP TEMPORARY TABLE IF EXISTS tmp.ticketAmount;
|
||||
CREATE TEMPORARY TABLE tmp.ticketAmount
|
||||
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketAmount
|
||||
(INDEX (ticketFk))
|
||||
ENGINE = MEMORY
|
||||
SELECT ticketFk,
|
||||
|
@ -97,7 +96,8 @@ BEGIN
|
|||
FROM tmp.ticketTax
|
||||
GROUP BY ticketFk, code;
|
||||
|
||||
DROP TEMPORARY TABLE tmp.addressCompany;
|
||||
DROP TEMPORARY TABLE tmp.addressTaxArea;
|
||||
DROP TEMPORARY TABLE
|
||||
tmp.addressCompany,
|
||||
tmp.addressTaxArea;
|
||||
END$$
|
||||
DELIMITER ;
|
||||
|
|
|
@ -46,23 +46,19 @@ module.exports = Self => {
|
|||
|
||||
const stmts = [];
|
||||
let stmt;
|
||||
stmts.push(`DROP TEMPORARY TABLE IF EXISTS tmp.ticket`);
|
||||
|
||||
stmts.push(new ParameterizedSQL(
|
||||
`CREATE TEMPORARY TABLE tmp.ticket
|
||||
`CREATE OR REPLACE TEMPORARY TABLE tmp.ticket
|
||||
(KEY (ticketFk))
|
||||
ENGINE = MEMORY
|
||||
SELECT id ticketFk
|
||||
FROM ticket t
|
||||
WHERE shipped BETWEEN ? AND ?
|
||||
WHERE shipped BETWEEN ? AND util.dayEnd(?)
|
||||
AND refFk IS NULL`, [args.from, args.to]));
|
||||
stmts.push(`CALL vn.ticket_getTax(NULL)`);
|
||||
stmts.push(`DROP TEMPORARY TABLE IF EXISTS tmp.filter`);
|
||||
stmts.push(new ParameterizedSQL(
|
||||
`CREATE TEMPORARY TABLE tmp.filter
|
||||
`CREATE OR REPLACE TEMPORARY TABLE tmp.filter
|
||||
ENGINE = MEMORY
|
||||
SELECT
|
||||
co.code company,
|
||||
SELECT co.code company,
|
||||
cou.country,
|
||||
c.id clientId,
|
||||
c.socialName clientSocialName,
|
||||
|
@ -88,15 +84,13 @@ module.exports = Self => {
|
|||
GROUP BY ticketFk
|
||||
HAVING taxableBase < 0
|
||||
) negativeBase ON negativeBase.ticketFk = t.id
|
||||
WHERE t.shipped BETWEEN ? AND ?
|
||||
WHERE t.shipped BETWEEN ? AND util.dayEnd(?)
|
||||
AND t.refFk IS NULL
|
||||
AND c.typeFk IN ('normal','trust')
|
||||
GROUP BY t.clientFk, negativeBase.taxableBase
|
||||
HAVING amount < 0`, [args.from, args.to]));
|
||||
|
||||
stmt = new ParameterizedSQL(`
|
||||
SELECT f.*
|
||||
FROM tmp.filter f`);
|
||||
stmt = new ParameterizedSQL(`SELECT * FROM tmp.filter`);
|
||||
|
||||
if (args.filter) {
|
||||
stmt.merge(conn.makeWhere(args.filter.where));
|
||||
|
|
Loading…
Reference in New Issue