refactor: refs #8106 Optimized ticket_getTax #3122

Merged
guillermo merged 2 commits from 8106-negativeBasesOptimize into dev 2024-10-18 08:27:09 +00:00
2 changed files with 42 additions and 31 deletions

View File

@ -1,5 +1,7 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ticket_getTax`(IN vTaxArea VARCHAR(25)) CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ticket_getTax`(
vTaxArea VARCHAR(25)
)
BEGIN BEGIN
/** /**
* Calcula la base imponible, el IVA y el recargo de equivalencia para * Calcula la base imponible, el IVA y el recargo de equivalencia para
@ -33,30 +35,39 @@ BEGIN
CREATE OR REPLACE 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 ( WITH sales AS (
SELECT tmpTicket.ticketFk, SELECT s.ticketFk,
guillermo marked this conversation as resolved Outdated

los campos s.id y s.ticketFk no son necesarios en la primera SELECT

los campos s.id y s.ticketFk no son necesarios en la primera SELECT

El s.id ok, pero el s.ticketFk sí es necesario, ya que se utiliza en el GROUP BY

El s.id ok, pero el s.ticketFk sí es necesario, ya que se utiliza en el GROUP BY
s.itemFk,
s.quantity * s.price * (100 - s.discount) / 100 total,
t.companyFk,
t.addressFk,
su.countryFk,
ata.areaFk,
itc.taxClassFk
FROM vn.sale s
JOIN tmp.ticket tmp ON tmp.ticketFk = s.ticketFk
JOIN vn.ticket t ON t.id = s.ticketFk
JOIN vn.supplier su ON su.id = t.companyFk
JOIN tmp.addressTaxArea ata ON ata.addressFk = t.addressFk
AND ata.companyFk = t.companyFk
JOIN vn.itemTaxCountry itc ON itc.itemFk = s.itemFk
AND itc.countryFk = su.countryFk
HAVING total
)
SELECT s.ticketFk,
bp.pgcFk, bp.pgcFk,
SUM(s.quantity * s.price * (100 - s.discount) / 100 ) taxableBase, SUM(s.total) taxableBase,
pgc.rate, pgc.rate,
tc.code, tc.code,
bp.priority bp.priority
FROM tmp.ticket tmpTicket FROM sales s
JOIN sale s ON s.ticketFk = tmpTicket.ticketFk JOIN vn.bookingPlanner bp ON bp.countryFk = s.countryFk
JOIN item i ON i.id = s.itemFk AND bp.taxAreaFk = s.areaFk
JOIN ticket t ON t.id = tmpTicket.ticketFk AND bp.taxClassFk = s.taxClassFk
JOIN supplier su ON su.id = t.companyFk JOIN vn.pgc ON pgc.code = bp.pgcFk
JOIN tmp.addressTaxArea ata ON ata.addressFk = t.addressFk JOIN vn.taxClass tc ON tc.id = bp.taxClassFk
AND ata.companyFk = t.companyFk GROUP BY s.ticketFk, pgc.code, pgc.rate
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 HAVING taxableBase
) t3
ORDER BY priority; ORDER BY priority;
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketServiceTax CREATE OR REPLACE TEMPORARY TABLE tmp.ticketServiceTax

View File

@ -48,13 +48,13 @@ module.exports = Self => {
let stmt; let stmt;
stmts.push(new ParameterizedSQL( stmts.push(new ParameterizedSQL(
`CREATE OR REPLACE TEMPORARY TABLE tmp.ticket `CREATE OR REPLACE TEMPORARY TABLE tmp.ticket
(KEY (ticketFk)) (INDEX (ticketFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT id ticketFk SELECT id ticketFk
FROM ticket t FROM ticket
WHERE shipped BETWEEN ? AND util.dayEnd(?) 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 ticket_getTax(NULL)`);
stmts.push(new ParameterizedSQL( stmts.push(new ParameterizedSQL(
`CREATE OR REPLACE TEMPORARY TABLE tmp.filter `CREATE OR REPLACE TEMPORARY TABLE tmp.filter
ENGINE = MEMORY ENGINE = MEMORY
@ -71,12 +71,12 @@ module.exports = Self => {
c.isTaxDataChecked, c.isTaxDataChecked,
w.id comercialId, w.id comercialId,
u.name workerName u.name workerName
FROM vn.ticket t FROM ticket t
JOIN vn.company co ON co.id = t.companyFk JOIN company co ON co.id = t.companyFk
JOIN vn.sale s ON s.ticketFk = t.id JOIN sale s ON s.ticketFk = t.id
JOIN vn.client c ON c.id = t.clientFk JOIN client c ON c.id = t.clientFk
JOIN vn.country cou ON cou.id = c.countryFk JOIN country cou ON cou.id = c.countryFk
LEFT JOIN vn.worker w ON w.id = c.salesPersonFk LEFT JOIN worker w ON w.id = c.salesPersonFk
JOIN account.user u ON u.id = w.id JOIN account.user u ON u.id = w.id
LEFT JOIN ( LEFT JOIN (
SELECT ticketFk, taxableBase SELECT ticketFk, taxableBase