fix: refs #7213 componentLack y setRisk #2820

Merged
carlosap merged 2 commits from 7213-Hotfix-calculo-problemas into master 2024-07-31 14:05:33 +00:00
3 changed files with 65 additions and 81 deletions

View File

@ -14,7 +14,7 @@ BEGIN
ENGINE = MEMORY ENGINE = MEMORY
SELECT vSelf saleFk, SELECT vSelf saleFk,
sale_hasComponentLack(vSelf) hasProblem, sale_hasComponentLack(vSelf) hasProblem,
ticket_isProblemCalcNeeded(ticketFk) isProblemCalcNeeded (ticket_isProblemCalcNeeded(ticketFk) AND quantity > 0) isProblemCalcNeeded
FROM sale FROM sale
WHERE id = vSelf; WHERE id = vSelf;

View File

@ -14,9 +14,9 @@ BEGIN
ENGINE = MEMORY ENGINE = MEMORY
SELECT saleFk, SELECT saleFk,
sale_hasComponentLack(saleFk) hasProblem, sale_hasComponentLack(saleFk) hasProblem,
ticket_isProblemCalcNeeded(ticketFk) isProblemCalcNeeded (ticket_isProblemCalcNeeded(ticketFk) AND quantity > 0) isProblemCalcNeeded
FROM ( FROM (
SELECT s.id saleFk, s.ticketFk SELECT s.id saleFk, s.ticketFk, s.quantity
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

@ -1,41 +1,31 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_setRisk`( CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_setRisk`(
vClientFk INT) vClientFk INT
)
BEGIN BEGIN
/** /**
* Update the risk for a client with pending tickets * Update the risk for a client with pending tickets.
* *
* @param vClientFk Id cliente * @param vClientFk Id cliente
*/ */
DECLARE vHasDebt BOOL; IF (SELECT COUNT(*) FROM client WHERE id = vClientFk AND typeFk = 'normal') THEN
DECLARE vStarted DATETIME;
SELECT COUNT(*) INTO vHasDebt
FROM `client`
WHERE id = vClientFk
AND typeFk = 'normal';
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)) (PRIMARY KEY (ticketFk))
ENGINE = MEMORY ENGINE = MEMORY
WITH ticket AS( WITH ticket AS (
SELECT id ticketFk, SELECT t.id ticketFk,
companyFk, t.companyFk,
DATE(shipped) dated, DATE(t.shipped) dated,
totalWithVat, t.totalWithVat,
ticket_isProblemCalcNeeded(id) isProblemCalcNeeded ticket_isProblemCalcNeeded(t.id) isProblemCalcNeeded
FROM vn.ticket FROM vn.ticket t
WHERE clientFk = vClientFk JOIN vn.clientConfig cc
AND refFk IS NULL WHERE t.clientFk = vClientFk
AND NOT isDeleted AND t.refFk IS NULL
AND IFNULL(totalWithVat, 0) <> 0 AND NOT t.isDeleted
AND shipped > vStarted AND IFNULL(t.totalWithVat, 0) <> 0
), balance AS( AND t.shipped > (util.VN_CURDATE() - INTERVAL cc.riskScope MONTH)
), balance AS (
SELECT SUM(amount)amount, companyFk SELECT SUM(amount)amount, companyFk
FROM ( FROM (
SELECT amount, companyFk SELECT amount, companyFk
@ -47,46 +37,40 @@ BEGIN
JOIN hedera.tpvMerchant tm ON t.id = t.merchantFk JOIN hedera.tpvMerchant tm ON t.id = t.merchantFk
WHERE clientFk = vClientFk WHERE clientFk = vClientFk
AND receiptFk IS NULL AND receiptFk IS NULL
AND status = 'ok' AND `status` = 'ok'
) sub ) sub
WHERE companyFk WHERE companyFk
GROUP BY companyFk GROUP BY companyFk
), uninvoiced AS( ), uninvoiced AS (
SELECT companyFk, dated, SUM(totalWithVat) amount SELECT companyFk, dated, SUM(totalWithVat) amount
FROM ticket FROM ticket
GROUP BY companyFk, dated GROUP BY companyFk, dated
), 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()
GROUP BY companyFk, DATE(payed) GROUP BY companyFk, DATE(payed)
), risk AS( ), risk AS (
SELECT b.companyFk, SELECT b.companyFk,
ui.dated, ui.dated,
SUM(ui.amount) OVER (PARTITION BY b.companyFk ORDER BY ui.dated ) + SUM(ui.amount) OVER (PARTITION BY b.companyFk ORDER BY ui.dated) +
b.amount + b.amount +
SUM(IFNULL(r.amount, 0)) amount SUM(IFNULL(r.amount, 0)) amount
FROM balance b FROM balance b
JOIN uninvoiced ui ON ui.companyFk = b.companyFk JOIN uninvoiced ui ON ui.companyFk = b.companyFk
LEFT JOIN receipt r ON r.dated > ui.dated AND r.companyFk = ui.companyFk LEFT JOIN receipt r ON r.dated > ui.dated
AND r.companyFk = ui.companyFk
GROUP BY b.companyFk, ui.dated GROUP BY b.companyFk, ui.dated
) )
SELECT ti.ticketFk, r.amount, ti.isProblemCalcNeeded SELECT ti.ticketFk, r.amount, ti.isProblemCalcNeeded
FROM ticket ti FROM ticket ti
JOIN risk r ON r.dated = ti.dated AND r.companyFk = ti.companyFk; JOIN risk r ON r.dated = ti.dated
AND r.companyFk = ti.companyFk;
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 = IF(tr.isProblemCalcNeeded, tr.amount, NULL);
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;