feat: Turn issues into calculated columns refs#7213 #2646

Merged
carlosap merged 3 commits from 7213-problemas-columnas into dev 2024-07-15 10:55:07 +00:00
16 changed files with 245 additions and 144 deletions

View File

@ -0,0 +1,27 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`ticket_isProblemCalcNeeded`(
vSelf INT
)
RETURNS BOOL
DETERMINISTIC
BEGIN
/**
* Check if the ticket requires to update column vn.ticket.problem
*
* @param vSelf Id ticket
* @return BOOL
*/
DECLARE vIsProblemCalcNeeded BOOL;
SELECT COUNT(*) INTO vIsProblemCalcNeeded
FROM ticket t
JOIN client c ON c.id = t.clientFk
JOIN agencyMode am ON am.id = t.agencyModeFk
JOIN deliveryMethod dm ON dm.id = am.deliveryMethodFk
WHERE t.id = vSelf
AND dm.code IN ('AGENCY','DELIVERY','PICKUP')
AND c.typeFk = 'normal';
RETURN vIsProblemCalcNeeded;
END$$
DELIMITER ;

View File

@ -1,5 +1,8 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`buyUltimate`(vWarehouseFk SMALLINT, vDated DATE) CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`buyUltimate`(
vWarehouseFk SMALLINT,
vDated DATE
)
BEGIN BEGIN
/** /**
* Calcula las últimas compras realizadas hasta una fecha * Calcula las últimas compras realizadas hasta una fecha
@ -19,21 +22,22 @@ BEGIN
FROM cache.last_buy FROM cache.last_buy
WHERE warehouse_id = vWarehouseFk OR vWarehouseFk IS NULL; WHERE warehouse_id = vWarehouseFk OR vWarehouseFk IS NULL;
CALL buyUltimateFromInterval(vWarehouseFk, util.VN_CURDATE(), vDated); IF vDated >= util.VN_CURDATE() THEN
CALL buyUltimateFromInterval(vWarehouseFk, util.VN_CURDATE(), vDated);
REPLACE INTO tmp.buyUltimate REPLACE INTO tmp.buyUltimate
SELECT itemFk, buyFk, warehouseFk, landed landing SELECT itemFk, buyFk, warehouseFk, landed landing
FROM tmp.buyUltimateFromInterval FROM tmp.buyUltimateFromInterval
WHERE (warehouseFk = vWarehouseFk OR vWarehouseFk IS NULL) WHERE (warehouseFk = vWarehouseFk OR vWarehouseFk IS NULL)
AND landed <= vDated AND landed <= vDated
AND NOT isIgnored; AND NOT isIgnored;
INSERT IGNORE INTO tmp.buyUltimate
SELECT itemFk, buyFk, warehouseFk, landed landing
FROM tmp.buyUltimateFromInterval
WHERE (warehouseFk = vWarehouseFk OR vWarehouseFk IS NULL)
AND landed > vDated
ORDER BY isIgnored = FALSE DESC;
INSERT IGNORE INTO tmp.buyUltimate
SELECT itemFk, buyFk, warehouseFk, landed landing
FROM tmp.buyUltimateFromInterval
WHERE (warehouseFk = vWarehouseFk OR vWarehouseFk IS NULL)
AND landed > vDated
ORDER BY isIgnored = FALSE DESC;
END IF;
END$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -1,5 +1,9 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`buyUltimateFromInterval`(vWarehouseFk SMALLINT, vStarted DATE, vEnded DATE) CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`buyUltimateFromInterval`(
vWarehouseFk SMALLINT,
vStarted DATE,
vEnded DATE
)
BEGIN BEGIN
/** /**
* Calcula las últimas compras realizadas * Calcula las últimas compras realizadas
@ -21,12 +25,13 @@ BEGIN
-- Item -- Item
DROP TEMPORARY TABLE IF EXISTS tmp.buyUltimateFromInterval; DROP TEMPORARY TABLE IF EXISTS tmp.buyUltimateFromInterval;
CREATE TEMPORARY TABLE tmp.buyUltimateFromInterval CREATE TEMPORARY TABLE tmp.buyUltimateFromInterval
(PRIMARY KEY (itemFk, warehouseFk), INDEX(buyFk), INDEX(landed), INDEX(warehouseFk), INDEX(itemFk)) (PRIMARY KEY (itemFk, warehouseFk),
INDEX(buyFk), INDEX(landed), INDEX(warehouseFk), INDEX(itemFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT itemFk, SELECT itemFk,
warehouseFk, warehouseFk,
buyFk, buyFk,
MAX(landed) landed, landed,
isIgnored isIgnored
FROM (SELECT b.itemFk, FROM (SELECT b.itemFk,
t.warehouseInFk warehouseFk, t.warehouseInFk warehouseFk,
@ -45,93 +50,117 @@ BEGIN
INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed, isIgnored) INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed, isIgnored)
SELECT SELECT itemFk,
b.itemFk, warehouseFk,
t.warehouseInFk warehouseFk, buyFk,
b.id buyFk, landed,
t.landed, isIgnored
b.isIgnored FROM (SELECT b.itemFk,
FROM buy b t.warehouseInFk warehouseFk,
JOIN entry e ON e.id = b.entryFk b.id buyFk,
JOIN travel t ON t.id = e.travelFk t.landed,
WHERE t.landed > vEnded b.isIgnored
AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk) FROM buy b
AND b.price2 > 0 JOIN entry e ON e.id = b.entryFk
AND NOT b.isIgnored JOIN travel t ON t.id = e.travelFk
GROUP BY itemFk, warehouseInFk; WHERE t.landed > vEnded
AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
AND b.price2 > 0
AND NOT b.isIgnored
ORDER BY NOT b.isIgnored DESC, t.landed DESC, b.id DESC
LIMIT 10000000000000000000) sub
GROUP BY itemFk, warehouseFk;
INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed, isIgnored) INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed, isIgnored)
SELECT SELECT itemFk,
b.itemFk, warehouseFk,
t.warehouseInFk warehouseFk, buyFk,
b.id buyFk, landed,
t.landed, isIgnored
b.isIgnored FROM (SELECT b.itemFk,
FROM buy b t.warehouseInFk warehouseFk,
JOIN entry e ON e.id = b.entryFk b.id buyFk,
JOIN travel t ON t.id = e.travelFk t.landed,
WHERE t.landed BETWEEN vStarted AND vEnded b.isIgnored
AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk) FROM buy b
AND b.quantity = 0 JOIN entry e ON e.id = b.entryFk
GROUP BY itemFk, warehouseInFk; JOIN travel t ON t.id = e.travelFk
WHERE t.landed BETWEEN vStarted AND vEnded
AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
AND b.quantity = 0
ORDER BY NOT b.isIgnored DESC, t.landed DESC, b.id DESC
LIMIT 10000000000000000000) sub
GROUP BY itemFk, warehouseFk;
-- ItemOriginal -- ItemOriginal
INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed, isIgnored) INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed, isIgnored)
SELECT itemFk, SELECT itemFk,
warehouseFk, warehouseFk,
buyFk, buyFk,
MAX(landed) landed, landed,
isIgnored isIgnored
FROM (SELECT b.itemFk, FROM (SELECT b.itemFk,
t.warehouseInFk warehouseFk, t.warehouseInFk warehouseFk,
b.id buyFk, b.id buyFk,
t.landed, t.landed,
itemOriginalFk, itemOriginalFk,
b.isIgnored b.isIgnored
FROM buy b FROM buy b
JOIN entry e ON e.id = b.entryFk JOIN entry e ON e.id = b.entryFk
JOIN travel t ON t.id = e.travelFk JOIN travel t ON t.id = e.travelFk
WHERE t.landed BETWEEN vStarted AND vEnded WHERE t.landed BETWEEN vStarted AND vEnded
AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk) AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
AND b.price2 > 0 AND b.price2 > 0
AND NOT b.isIgnored AND NOT b.isIgnored
AND b.quantity > 0 AND b.quantity > 0
AND itemOriginalFk AND itemOriginalFk
ORDER BY t.landed DESC, b.id DESC ORDER BY t.landed DESC, b.id DESC
LIMIT 10000000000000000000) sub LIMIT 10000000000000000000) sub
GROUP BY itemOriginalFk, warehouseFk; GROUP BY itemFk, warehouseFk;
INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed, isIgnored)
SELECT itemFk,
warehouseFk,
buyFk,
landed,
isIgnored
FROM (SELECT b.itemFk,
t.warehouseInFk warehouseFk,
b.id buyFk,
t.landed,
b.isIgnored
FROM buy b
JOIN entry e ON e.id = b.entryFk
JOIN travel t ON t.id = e.travelFk
WHERE t.landed > vEnded
AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
AND b.price2 > 0
AND NOT b.isIgnored
AND itemOriginalFk
ORDER BY t.landed DESC, b.id DESC
LIMIT 10000000000000000000) sub
GROUP BY itemFk, warehouseFk;
INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed, isIgnored) INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed, isIgnored)
SELECT SELECT itemFk,
b.itemFk, warehouseFk,
t.warehouseInFk warehouseFk, buyFk,
b.id buyFk, landed,
t.landed, isIgnored
b.isIgnored FROM
FROM buy b (SELECT b.itemFk,
JOIN entry e ON e.id = b.entryFk t.warehouseInFk warehouseFk,
JOIN travel t ON t.id = e.travelFk b.id buyFk,
WHERE t.landed > vEnded t.landed,
AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk) b.isIgnored
AND b.price2 > 0 FROM buy b
AND NOT b.isIgnored JOIN entry e ON e.id = b.entryFk
AND itemOriginalFk JOIN travel t ON t.id = e.travelFk
GROUP BY itemOriginalFk, warehouseInFk; WHERE t.landed BETWEEN vStarted AND vEnded
AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
INSERT IGNORE INTO tmp.buyUltimateFromInterval(itemFk, warehouseFk, buyFk, landed, isIgnored) AND b.quantity = 0
SELECT AND itemOriginalFk
b.itemFk, ORDER BY t.landed DESC, b.id DESC
t.warehouseInFk warehouseFk, LIMIT 10000000000000000000) sub
b.id buyFk, GROUP BY itemFk, warehouseFk;
t.landed,
b.isIgnored
FROM buy b
JOIN entry e ON e.id = b.entryFk
JOIN travel t ON t.id = e.travelFk
WHERE t.landed BETWEEN vStarted AND vEnded
AND (vWarehouseFk IS NULL OR t.warehouseInFk = vWarehouseFk)
AND b.quantity = 0
AND itemOriginalFk
GROUP BY itemOriginalFk, warehouseInFk;
END$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -7,7 +7,7 @@ BEGIN
* Update column sale.problem with a problem code * Update column sale.problem with a problem code
* *
* @param vProblemCode Code to set or unset * @param vProblemCode Code to set or unset
* @table tmp.sale(saleFk, hasProblem) * @table tmp.sale(saleFk, hasProblem, isProblemCalcNeeded)
*/ */
DECLARE vSaleFk INT; DECLARE vSaleFk INT;
DECLARE vHasProblem INT; DECLARE vHasProblem INT;

View File

@ -10,9 +10,13 @@ BEGIN
* @param vSelf Id del sale * @param vSelf Id del sale
*/ */
CREATE OR REPLACE TEMPORARY TABLE tmp.sale CREATE OR REPLACE TEMPORARY TABLE tmp.sale
(INDEX(saleFk)) (INDEX(saleFk, isProblemCalcNeeded))
ENGINE = MEMORY ENGINE = MEMORY
SELECT vSelf saleFk, sale_hasComponentLack(vSelf) hasProblem; SELECT vSelf saleFk,
sale_hasComponentLack(vSelf) hasProblem,
ticket_isProblemCalcNeeded(ticketFk) isProblemCalcNeeded
FROM sale
WHERE id = vSelf;
CALL sale_setProblem('hasComponentLack'); CALL sale_setProblem('hasComponentLack');

View File

@ -10,11 +10,13 @@ BEGIN
* @param vComponentFk Id component * @param vComponentFk Id component
*/ */
CREATE OR REPLACE TEMPORARY TABLE tmp.sale CREATE OR REPLACE TEMPORARY TABLE tmp.sale
(INDEX(saleFk)) (INDEX(saleFk, isProblemCalcNeeded))
ENGINE = MEMORY ENGINE = MEMORY
SELECT saleFk, sale_hasComponentLack(saleFk)hasProblem SELECT saleFk,
sale_hasComponentLack(saleFk) hasProblem,
ticket_isProblemCalcNeeded(ticketFk) isProblemCalcNeeded
FROM ( FROM (
SELECT s.id saleFk SELECT s.id saleFk, s.ticketFk
FROM ticket t FROM ticket t
JOIN sale s ON s.ticketFk = t.id JOIN sale s ON s.ticketFk = t.id
LEFT JOIN saleComponent sc ON sc.saleFk = s.id LEFT JOIN saleComponent sc ON sc.saleFk = s.id

View File

@ -11,9 +11,10 @@ BEGIN
DECLARE vWarehouseFk INT; DECLARE vWarehouseFk INT;
DECLARE vShipped DATE; DECLARE vShipped DATE;
DECLARE vQuantity INT; DECLARE vQuantity INT;
DECLARE vIsProblemCalcNeeded BOOL;
SELECT s.itemFk, t.warehouseFk, t.shipped, s.quantity SELECT s.itemFk, t.warehouseFk, t.shipped, s.quantity, ticket_isProblemCalcNeeded(t.id)
INTO vItemFk, vWarehouseFk, vShipped, vQuantity INTO vItemFk, vWarehouseFk, vShipped, vQuantity, vIsProblemCalcNeeded
FROM sale s FROM sale s
JOIN ticket t ON t.id = s.ticketFk JOIN ticket t ON t.id = s.ticketFk
WHERE s.id = vSelf; WHERE s.id = vSelf;
@ -21,7 +22,9 @@ BEGIN
CALL buyUltimate(vWarehouseFk, vShipped); CALL buyUltimate(vWarehouseFk, vShipped);
CREATE OR REPLACE TEMPORARY TABLE tmp.sale CREATE OR REPLACE TEMPORARY TABLE tmp.sale
SELECT vSelf saleFk, MOD(vQuantity, b.`grouping`) hasProblem SELECT vSelf saleFk,
MOD(vQuantity, b.`grouping`) hasProblem,
vIsProblemCalcNeeded isProblemCalcNeeded
FROM tmp.buyUltimate bu FROM tmp.buyUltimate bu
JOIN buy b ON b.id = bu.buyFk JOIN buy b ON b.id = bu.buyFk
WHERE bu.itemFk = vItemFk; WHERE bu.itemFk = vItemFk;

View File

@ -4,10 +4,11 @@ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_setProblem`(
) )
BEGIN BEGIN
/** /**
* Update column ticket.problem with a problem code * Update column ticket.problem with a problem code and SET NULL when
* the problem is not requiered
* *
* @param vProblemCode Code to set or unset * @param vProblemCode Code to set or unset
* @table tmp.ticket(ticketFk, hasProblem) * @table tmp.ticket(ticketFk, hasProblem, isProblemCalcNeeded)
*/ */
DECLARE vTicketFk INT; DECLARE vTicketFk INT;
DECLARE vHasProblem INT; DECLARE vHasProblem INT;

View File

@ -9,9 +9,11 @@ BEGIN
* @param vClientFk Id Cliente, if NULL all clients * @param vClientFk Id Cliente, if NULL all clients
*/ */
CREATE OR REPLACE TEMPORARY TABLE tmp.ticket CREATE OR REPLACE TEMPORARY TABLE tmp.ticket
(INDEX(ticketFk)) (INDEX(ticketFk, isProblemCalcNeeded))
ENGINE = MEMORY ENGINE = MEMORY
SELECT t.id ticketFk, FALSE hasProblem SELECT t.id ticketFk,
FALSE hasProblem,
ticket_isProblemCalcNeeded(t.id) isProblemCalcNeeded
FROM ticket t FROM ticket t
WHERE t.shipped >= util.VN_CURDATE() WHERE t.shipped >= util.VN_CURDATE()
AND (vClientFk IS NULL OR t.clientFk = vClientFk); AND (vClientFk IS NULL OR t.clientFk = vClientFk);

View File

@ -9,9 +9,11 @@ BEGIN
* @param vSelf Id ticket, if NULL ALL tickets * @param vSelf Id ticket, if NULL ALL tickets
*/ */
CREATE OR REPLACE TEMPORARY TABLE tmp.ticket CREATE OR REPLACE TEMPORARY TABLE tmp.ticket
(INDEX(ticketFk)) (INDEX(ticketFk, isProblemCalcNeeded))
ENGINE = MEMORY ENGINE = MEMORY
SELECT t.id ticketFk, FALSE hasProblem SELECT t.id ticketFk,
FALSE hasProblem,
ticket_isProblemCalcNeeded(t.id) isProblemCalcNeeded
FROM ticket t FROM ticket t
WHERE t.shipped >= util.VN_CURDATE() WHERE t.shipped >= util.VN_CURDATE()
AND (vSelf IS NULL OR t.id = vSelf); AND (vSelf IS NULL OR t.id = vSelf);

View File

@ -10,21 +10,30 @@ BEGIN
*/ */
DECLARE vHasRisk BOOL; DECLARE vHasRisk BOOL;
DECLARE vHasHighRisk BOOL; DECLARE vHasHighRisk BOOL;
DECLARE vIsProblemCalcNeeded BOOL;
SELECT t.risk > (c.credit + 10), ((t.risk - cc.riskTolerance) > (c.credit + 10)) SELECT t.risk > (c.credit + 10),
INTO vHasRisk, vHasHighRisk (t.risk - cc.riskTolerance) > (c.credit + 10),
ticket_isProblemCalcNeeded(t.id)
INTO vHasRisk, vHasHighRisk, vIsProblemCalcNeeded
FROM client c FROM client c
JOIN ticket t ON t.clientFk = c.id JOIN ticket t ON t.clientFk = c.id
JOIN clientConfig cc JOIN clientConfig cc
WHERE t.id = vSelf; WHERE t.id = vSelf;
CREATE OR REPLACE TEMPORARY TABLE tmp.ticket CREATE OR REPLACE TEMPORARY TABLE tmp.ticket
SELECT vSelf ticketFk, vHasRisk hasProblem; ENGINE = MEMORY
SELECT vSelf ticketFk,
vHasRisk hasProblem,
vIsProblemCalcNeeded isProblemCalcNeeded;
CALL ticket_setProblem('hasRisk'); CALL ticket_setProblem('hasRisk');
CREATE OR REPLACE TEMPORARY TABLE tmp.ticket CREATE OR REPLACE TEMPORARY TABLE tmp.ticket
SELECT vSelf ticketFk, vHasHighRisk hasProblem; ENGINE = MEMORY
SELECT vSelf ticketFk,
vHasHighRisk hasProblem,
vIsProblemCalcNeeded isProblemCalcNeeded;
CALL ticket_setProblem('hasHighRisk'); CALL ticket_setProblem('hasHighRisk');

View File

@ -19,7 +19,10 @@ BEGIN
CALL buyUltimate(vWarehouseFk, vDated); CALL buyUltimate(vWarehouseFk, vDated);
CREATE OR REPLACE TEMPORARY TABLE tmp.sale CREATE OR REPLACE TEMPORARY TABLE tmp.sale
SELECT s.id saleFk , MOD(s.quantity, b.`grouping`) hasProblem (INDEX(saleFk, isProblemCalcNeeded))
SELECT s.id saleFk ,
MOD(s.quantity, b.`grouping`) hasProblem,
ticket_isProblemCalcNeeded(t.id) isProblemCalcNeeded
FROM ticket t FROM ticket t
JOIN sale s ON s.ticketFk = t.id JOIN sale s ON s.ticketFk = t.id
JOIN tmp.buyUltimate bu ON bu.itemFk = s.itemFk JOIN tmp.buyUltimate bu ON bu.itemFk = s.itemFk

View File

@ -9,9 +9,11 @@ BEGIN
* @param vClientFk Id cliente, if NULL all clients * @param vClientFk Id cliente, if NULL all clients
*/ */
CREATE OR REPLACE TEMPORARY TABLE tmp.ticket CREATE OR REPLACE TEMPORARY TABLE tmp.ticket
(INDEX(ticketFk)) (INDEX(ticketFk, isProblemCalcNeeded))
ENGINE = MEMORY ENGINE = MEMORY
SELECT t.id ticketFk, IF(c.isTaxDataChecked, FALSE, TRUE) hasProblem SELECT t.id ticketFk,
IF(c.isTaxDataChecked, FALSE, TRUE) hasProblem,
ticket_isProblemCalcNeeded(t.id) isProblemCalcNeeded
Review

si posem ticket_isProblemCalcNeeded en el WHERE ens evitem tant la insercio en la taula temporal, com la posterior eliminació en el procediment, y la columna isProblemCalcNeeded ja no seria necesaria, simplement el fet d'estar la linea es que hi ha que calcular-ho.

si posem ticket_isProblemCalcNeeded en el WHERE ens evitem tant la insercio en la taula temporal, com la posterior eliminació en el procediment, y la columna isProblemCalcNeeded ja no seria necesaria, simplement el fet d'estar la linea es que hi ha que calcular-ho.
Review

es tenen que posar a NULL

es tenen que posar a NULL
FROM ticket t FROM ticket t
JOIN client c ON c.id = t.clientFk JOIN client c ON c.id = t.clientFk
WHERE t.shipped >= util.VN_CURDATE() WHERE t.shipped >= util.VN_CURDATE()

View File

@ -8,13 +8,17 @@ BEGIN
* *
* @param vSelf Id del ticket * @param vSelf Id del ticket
*/ */
CREATE OR REPLACE TEMPORARY TABLE tmp.ticket CREATE OR REPLACE TEMPORARY TABLE tmp.ticket
(INDEX(ticketFk)) (INDEX(ticketFk, isProblemCalcNeeded))
ENGINE = MEMORY ENGINE = MEMORY
SELECT vSelf ticketFk, ticket_isTooLittle(vSelf) hasProblem; SELECT vSelf ticketFk,
ticket_isTooLittle(vSelf) hasProblem,
ticket_isProblemCalcNeeded(vSelf) isProblemCalcNeeded;
CALL ticket_setProblem('isTooLittle'); CALL ticket_setProblem('isTooLittle');
DROP TEMPORARY TABLE tmp.ticket; DROP TEMPORARY TABLE tmp.ticket;
END$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -10,7 +10,7 @@ BEGIN
* @param vItemFk Id del item, NULL ALL items * @param vItemFk Id del item, NULL ALL items
*/ */
CREATE OR REPLACE TEMPORARY TABLE tmp.ticket CREATE OR REPLACE TEMPORARY TABLE tmp.ticket
(INDEX(ticketFk)) (INDEX(ticketFk, isProblemCalcNeeded))
ENGINE = MEMORY ENGINE = MEMORY
WITH tickets AS( WITH tickets AS(
SELECT t.id ticketFk SELECT t.id ticketFk
@ -19,7 +19,9 @@ BEGIN
WHERE t.shipped >= util.VN_CURDATE() WHERE t.shipped >= util.VN_CURDATE()
AND (s.itemFk = vItemFk OR vItemFk IS NULL) AND (s.itemFk = vItemFk OR vItemFk IS NULL)
GROUP BY t.id GROUP BY t.id
)SELECT ticketFk, ticket_isTooLittle(ticketFk) hasProblem )SELECT ticketFk,
ticket_isTooLittle(ticketFk) hasProblem,
ticket_isProblemCalcNeeded(t.id) isProblemCalcNeeded
FROM tickets; FROM tickets;
CALL ticket_setProblem('isTooLittle'); CALL ticket_setProblem('isTooLittle');

View File

@ -8,6 +8,7 @@ BEGIN
* @param vClientFk Id cliente * @param vClientFk Id cliente
*/ */
DECLARE vHasDebt BOOL; DECLARE vHasDebt BOOL;
DECLARE vStarted DATETIME;
SELECT COUNT(*) INTO vHasDebt SELECT COUNT(*) INTO vHasDebt
FROM `client` FROM `client`
@ -16,22 +17,24 @@ BEGIN
IF vHasDebt THEN IF vHasDebt THEN
SELECT util.VN_CURDATE() - INTERVAL riskScope MONTH INTO vStarted
FROM clientConfig;
CREATE OR REPLACE TEMPORARY TABLE tTicketRisk CREATE OR REPLACE TEMPORARY TABLE tTicketRisk
(KEY (ticketFk)) (KEY (ticketFk))
ENGINE = MEMORY ENGINE = MEMORY
WITH ticket AS( WITH ticket AS(
SELECT id ticketFk, companyFk, DATE(shipped) dated SELECT id ticketFk,
FROM vn.ticket t companyFk,
DATE(shipped) dated,
totalWithVat,
ticket_isProblemCalcNeeded(id) isProblemCalcNeeded
FROM vn.ticket
WHERE clientFk = vClientFk WHERE clientFk = vClientFk
AND refFk IS NULL AND refFk IS NULL
AND NOT isDeleted AND NOT isDeleted
AND totalWithoutVat <> 0 AND IFNULL(totalWithVat, 0) <> 0
), dated AS( AND shipped > vStarted
SELECT t.companyFk, MIN(DATE(t.dated) - INTERVAL cc.riskScope MONTH) started,
MAX(DATE(t.dated)) ended
FROM ticket t
JOIN vn.clientConfig cc
GROUP BY t.companyFk
), balance AS( ), balance AS(
SELECT SUM(amount)amount, companyFk SELECT SUM(amount)amount, companyFk
FROM ( FROM (
@ -49,15 +52,11 @@ BEGIN
WHERE companyFk WHERE companyFk
GROUP BY companyFk GROUP BY companyFk
), uninvoiced AS( ), uninvoiced AS(
SELECT t.companyFk, DATE(t.shipped) dated, SUM(IFNULL(t.totalWithVat, 0)) amount SELECT companyFk, dated, SUM(totalWithVat) amount
FROM vn.ticket t FROM ticket
JOIN dated d GROUP BY companyFk, dated
WHERE t.clientFk = vClientFk
AND t.refFk IS NULL
AND t.shipped BETWEEN d.started AND d.ended
GROUP BY t.companyFk, DATE(t.shipped)
), receipt AS( ), receipt AS(
SELECT companyFk,DATE(payed) dated, SUM(amountPaid) amount SELECT companyFk, DATE(payed) dated, SUM(amountPaid) amount
FROM vn.receipt FROM vn.receipt
WHERE clientFk = vClientFk WHERE clientFk = vClientFk
AND payed > util.VN_CURDATE() AND payed > util.VN_CURDATE()
@ -79,7 +78,15 @@ BEGIN
UPDATE ticket t UPDATE ticket t
JOIN tTicketRisk tr ON tr.ticketFk = t.id JOIN tTicketRisk tr ON tr.ticketFk = t.id
SET t.risk = tr.amount; SET t.risk = tr.amount
WHERE tr.isProblemCalcNeeded
ORDER BY t.id;
UPDATE ticket t
JOIN tTicketRisk tr ON tr.ticketFk = t.id
SET t.risk = NULL
WHERE tr.isProblemCalcNeeded
ORDER BY t.id;
DROP TEMPORARY TABLE tTicketRisk; DROP TEMPORARY TABLE tTicketRisk;
END IF; END IF;