refactor: refs #5586 Optimized SQL negativeBases
gitea/salix/pipeline/pr-dev There was a failure building this commit
Details
gitea/salix/pipeline/pr-dev There was a failure building this commit
Details
This commit is contained in:
parent
c4907d1a33
commit
974228eedd
|
@ -9,32 +9,31 @@ 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.
|
||||||
*/
|
*/
|
||||||
DROP TEMPORARY TABLE IF EXISTS tmp.addressCompany;
|
CREATE OR REPLACE TEMPORARY TABLE tmp.addressCompany
|
||||||
CREATE TEMPORARY TABLE tmp.addressCompany
|
|
||||||
(INDEX (addressFk, companyFk))
|
(INDEX (addressFk, companyFk))
|
||||||
ENGINE = MEMORY
|
ENGINE = MEMORY
|
||||||
SELECT DISTINCT t.addressFk, t.companyFk
|
SELECT DISTINCT t.addressFk, t.companyFk
|
||||||
FROM tmp.ticket tmpTicket
|
FROM tmp.ticket tmpTicket
|
||||||
JOIN ticket t ON t.id = tmpTicket.ticketFk;
|
JOIN ticket t ON t.id = tmpTicket.ticketFk;
|
||||||
|
|
||||||
CALL addressTaxArea ();
|
CALL addressTaxArea();
|
||||||
|
|
||||||
IF vTaxArea IS NOT NULL THEN
|
IF vTaxArea IS NOT NULL THEN
|
||||||
UPDATE tmp.addressTaxArea
|
UPDATE tmp.addressTaxArea
|
||||||
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 (
|
||||||
SELECT tmpTicket.ticketFk,
|
SELECT tmpTicket.ticketFk,
|
||||||
bp.pgcFk,
|
bp.pgcFk,
|
||||||
SUM(s.quantity * s.price * (100 - s.discount)/100 ) taxableBase,
|
SUM(s.quantity * s.price * (100 - s.discount) / 100 ) taxableBase,
|
||||||
pgc.rate,
|
pgc.rate,
|
||||||
tc.code,
|
tc.code,
|
||||||
bp.priority
|
bp.priority
|
||||||
|
@ -43,22 +42,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 +68,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 +93,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 ;
|
||||||
|
|
|
@ -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,
|
||||||
|
@ -74,27 +70,25 @@ module.exports = Self => {
|
||||||
c.isTaxDataChecked,
|
c.isTaxDataChecked,
|
||||||
w.id comercialId,
|
w.id comercialId,
|
||||||
CONCAT(w.firstName, ' ', w.lastName) comercialName
|
CONCAT(w.firstName, ' ', w.lastName) comercialName
|
||||||
FROM vn.ticket t
|
FROM vn.ticket t
|
||||||
JOIN vn.company co ON co.id = t.companyFk
|
JOIN vn.company co ON co.id = t.companyFk
|
||||||
JOIN vn.sale s ON s.ticketFk = t.id
|
JOIN vn.sale s ON s.ticketFk = t.id
|
||||||
JOIN vn.client c ON c.id = t.clientFk
|
JOIN vn.client c ON c.id = t.clientFk
|
||||||
JOIN vn.country cou ON cou.id = c.countryFk
|
JOIN vn.country cou ON cou.id = c.countryFk
|
||||||
LEFT JOIN vn.worker w ON w.id = c.salesPersonFk
|
LEFT JOIN vn.worker w ON w.id = c.salesPersonFk
|
||||||
LEFT JOIN (
|
LEFT JOIN (
|
||||||
SELECT ticketFk, taxableBase
|
SELECT ticketFk, taxableBase
|
||||||
FROM tmp.ticketAmount
|
FROM tmp.ticketAmount
|
||||||
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));
|
||||||
|
@ -104,7 +98,10 @@ module.exports = Self => {
|
||||||
|
|
||||||
const negativeBasesIndex = stmts.push(stmt) - 1;
|
const negativeBasesIndex = stmts.push(stmt) - 1;
|
||||||
|
|
||||||
stmts.push(`DROP TEMPORARY TABLE tmp.filter, tmp.ticket, tmp.ticketTax, tmp.ticketAmount`);
|
stmts.push(`DROP TEMPORARY TABLE tmp.filter,
|
||||||
|
tmp.ticket,
|
||||||
|
tmp.ticketTax,
|
||||||
|
tmp.ticketAmount`);
|
||||||
|
|
||||||
const sql = ParameterizedSQL.join(stmts, ';');
|
const sql = ParameterizedSQL.join(stmts, ';');
|
||||||
const result = await conn.executeStmt(sql);
|
const result = await conn.executeStmt(sql);
|
||||||
|
|
Loading…
Reference in New Issue