2024-04-16 15:01:14 +00:00
|
|
|
DELIMITER $$
|
2024-05-13 07:54:27 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_setRisk`(
|
2024-05-13 11:31:58 +00:00
|
|
|
vClientFk INT)
|
2024-04-16 15:01:14 +00:00
|
|
|
BEGIN
|
|
|
|
/**
|
2024-05-02 16:13:12 +00:00
|
|
|
* Update the risk for a client with pending tickets
|
2024-04-16 15:01:14 +00:00
|
|
|
*
|
2024-05-13 11:31:58 +00:00
|
|
|
* @param vClientFk Id cliente
|
2024-04-16 15:01:14 +00:00
|
|
|
*/
|
|
|
|
DECLARE vHasDebt BOOL;
|
2024-07-12 14:47:24 +00:00
|
|
|
DECLARE vStarted DATETIME;
|
2024-05-02 16:13:12 +00:00
|
|
|
|
2024-04-16 15:01:14 +00:00
|
|
|
SELECT COUNT(*) INTO vHasDebt
|
|
|
|
FROM `client`
|
2024-05-13 11:31:58 +00:00
|
|
|
WHERE id = vClientFk
|
2024-04-16 15:01:14 +00:00
|
|
|
AND typeFk = 'normal';
|
2024-05-02 16:13:12 +00:00
|
|
|
|
2024-04-16 15:01:14 +00:00
|
|
|
IF vHasDebt THEN
|
2024-05-02 16:13:12 +00:00
|
|
|
|
2024-07-12 14:47:24 +00:00
|
|
|
SELECT util.VN_CURDATE() - INTERVAL riskScope MONTH INTO vStarted
|
|
|
|
FROM clientConfig;
|
|
|
|
|
2024-04-16 15:01:14 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tTicketRisk
|
|
|
|
(KEY (ticketFk))
|
|
|
|
ENGINE = MEMORY
|
|
|
|
WITH ticket AS(
|
2024-07-12 14:47:24 +00:00
|
|
|
SELECT id ticketFk,
|
|
|
|
companyFk,
|
|
|
|
DATE(shipped) dated,
|
|
|
|
totalWithVat,
|
|
|
|
ticket_isProblemCalcNeeded(id) isProblemCalcNeeded
|
|
|
|
FROM vn.ticket
|
2024-05-13 11:31:58 +00:00
|
|
|
WHERE clientFk = vClientFk
|
2024-04-16 15:01:14 +00:00
|
|
|
AND refFk IS NULL
|
|
|
|
AND NOT isDeleted
|
2024-07-12 14:47:24 +00:00
|
|
|
AND IFNULL(totalWithVat, 0) <> 0
|
|
|
|
AND shipped > vStarted
|
2024-04-16 15:01:14 +00:00
|
|
|
), balance AS(
|
2024-05-13 10:32:46 +00:00
|
|
|
SELECT SUM(amount)amount, companyFk
|
2024-04-16 15:01:14 +00:00
|
|
|
FROM (
|
2024-05-13 10:32:46 +00:00
|
|
|
SELECT amount, companyFk
|
2024-04-16 15:01:14 +00:00
|
|
|
FROM vn.clientRisk
|
2024-05-13 11:31:58 +00:00
|
|
|
WHERE clientFk = vClientFk
|
2024-04-16 15:01:14 +00:00
|
|
|
UNION ALL
|
2024-05-13 10:32:46 +00:00
|
|
|
SELECT -(SUM(amount) / 100) amount, tm.companyFk
|
2024-04-16 15:01:14 +00:00
|
|
|
FROM hedera.tpvTransaction t
|
2024-05-13 10:32:46 +00:00
|
|
|
JOIN hedera.tpvMerchant tm ON t.id = t.merchantFk
|
2024-05-13 11:31:58 +00:00
|
|
|
WHERE clientFk = vClientFk
|
2024-04-16 15:01:14 +00:00
|
|
|
AND receiptFk IS NULL
|
|
|
|
AND status = 'ok'
|
|
|
|
) sub
|
2024-05-13 10:32:46 +00:00
|
|
|
WHERE companyFk
|
|
|
|
GROUP BY companyFk
|
2024-04-16 15:01:14 +00:00
|
|
|
), uninvoiced AS(
|
2024-07-12 14:47:24 +00:00
|
|
|
SELECT companyFk, dated, SUM(totalWithVat) amount
|
|
|
|
FROM ticket
|
|
|
|
GROUP BY companyFk, dated
|
2024-04-16 15:01:14 +00:00
|
|
|
), receipt AS(
|
2024-07-12 14:47:24 +00:00
|
|
|
SELECT companyFk, DATE(payed) dated, SUM(amountPaid) amount
|
2024-04-16 15:01:14 +00:00
|
|
|
FROM vn.receipt
|
2024-05-13 11:31:58 +00:00
|
|
|
WHERE clientFk = vClientFk
|
2024-04-16 15:01:14 +00:00
|
|
|
AND payed > util.VN_CURDATE()
|
2024-05-13 10:32:46 +00:00
|
|
|
GROUP BY companyFk, DATE(payed)
|
2024-04-16 15:01:14 +00:00
|
|
|
), risk AS(
|
2024-05-13 10:32:46 +00:00
|
|
|
SELECT b.companyFk,
|
|
|
|
ui.dated,
|
|
|
|
SUM(ui.amount) OVER (PARTITION BY b.companyFk ORDER BY ui.dated ) +
|
2024-04-16 15:01:14 +00:00
|
|
|
b.amount +
|
|
|
|
SUM(IFNULL(r.amount, 0)) amount
|
|
|
|
FROM balance b
|
2024-05-13 10:32:46 +00:00
|
|
|
JOIN uninvoiced ui ON ui.companyFk = b.companyFk
|
|
|
|
LEFT JOIN receipt r ON r.dated > ui.dated AND r.companyFk = ui.companyFk
|
|
|
|
GROUP BY b.companyFk, ui.dated
|
2024-04-16 15:01:14 +00:00
|
|
|
)
|
2024-07-16 13:14:10 +00:00
|
|
|
SELECT ti.ticketFk, r.amount, ti.isProblemCalcNeeded
|
2024-04-16 15:01:14 +00:00
|
|
|
FROM ticket ti
|
2024-05-13 10:32:46 +00:00
|
|
|
JOIN risk r ON r.dated = ti.dated AND r.companyFk = ti.companyFk;
|
2024-05-02 16:13:12 +00:00
|
|
|
|
2024-04-16 15:01:14 +00:00
|
|
|
UPDATE ticket t
|
|
|
|
JOIN tTicketRisk tr ON tr.ticketFk = t.id
|
2024-07-12 14:47:24 +00:00
|
|
|
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
|
2024-07-31 12:38:12 +00:00
|
|
|
WHERE NOT tr.isProblemCalcNeeded
|
2024-07-12 14:47:24 +00:00
|
|
|
ORDER BY t.id;
|
2024-05-02 16:13:12 +00:00
|
|
|
|
2024-05-03 08:19:05 +00:00
|
|
|
DROP TEMPORARY TABLE tTicketRisk;
|
2024-04-16 15:01:14 +00:00
|
|
|
END IF;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|