2024-04-16 15:01:14 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`ticket_setRisk`(
|
2024-07-31 12:59:12 +00:00
|
|
|
vClientFk INT
|
|
|
|
)
|
2024-04-16 15:01:14 +00:00
|
|
|
BEGIN
|
|
|
|
/**
|
2024-07-31 12:59: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
|
|
|
*/
|
2024-07-31 12:59:12 +00:00
|
|
|
IF (SELECT COUNT(*) FROM client WHERE id = vClientFk AND typeFk = 'normal') THEN
|
2024-04-16 15:01:14 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tTicketRisk
|
2024-07-31 12:59:12 +00:00
|
|
|
(PRIMARY KEY (ticketFk))
|
2024-04-16 15:01:14 +00:00
|
|
|
ENGINE = MEMORY
|
2024-07-31 12:59:12 +00:00
|
|
|
WITH ticket AS (
|
2024-07-31 17:38:50 +00:00
|
|
|
SELECT t.id ticketFk,
|
|
|
|
t.companyFk,
|
|
|
|
DATE(t.shipped) dated,
|
|
|
|
t.totalWithVat,
|
|
|
|
ticket_isProblemCalcNeeded(t.id) isProblemCalcNeeded
|
|
|
|
FROM vn.ticket t
|
|
|
|
JOIN vn.clientConfig cc
|
|
|
|
WHERE t.clientFk = vClientFk
|
|
|
|
AND t.refFk IS NULL
|
|
|
|
AND NOT t.isDeleted
|
|
|
|
AND IFNULL(t.totalWithVat, 0) <> 0
|
|
|
|
AND t.shipped > (util.VN_CURDATE() - INTERVAL cc.riskScope MONTH)
|
|
|
|
), uninvoiced AS (
|
2024-07-12 14:47:24 +00:00
|
|
|
SELECT companyFk, dated, SUM(totalWithVat) amount
|
|
|
|
FROM ticket
|
2024-07-31 17:38:50 +00:00
|
|
|
GROUP BY companyFk, dated
|
|
|
|
), companies AS (
|
|
|
|
SELECT DISTINCT companyFk FROM uninvoiced
|
|
|
|
), balance AS (
|
|
|
|
SELECT SUM(IFNULL(amount, 0))amount, companyFk
|
|
|
|
FROM (
|
|
|
|
SELECT cr.amount, c.companyFk
|
|
|
|
FROM companies c
|
|
|
|
LEFT JOIN vn.clientRisk cr ON cr.companyFk = c.companyFk
|
|
|
|
AND cr.clientFk = vClientFk
|
|
|
|
UNION ALL
|
|
|
|
SELECT -(SUM(t.amount) / 100) amount, c.companyFk
|
|
|
|
FROM companies c
|
|
|
|
LEFT JOIN hedera.tpvMerchant tm ON tm.companyFk = c.companyFk
|
|
|
|
LEFT JOIN hedera.tpvTransaction t ON t.merchantFk = tm.id
|
|
|
|
AND t.clientFk = vClientFk
|
|
|
|
AND t.receiptFk IS NULL
|
|
|
|
AND t.`status` = 'ok'
|
|
|
|
) sub
|
|
|
|
WHERE companyFk
|
|
|
|
GROUP BY companyFk
|
|
|
|
), receipt AS (
|
|
|
|
SELECT r.companyFk, DATE(r.payed) dated, SUM(r.amountPaid) amount
|
|
|
|
FROM vn.receipt r
|
|
|
|
JOIN companies c ON c.companyFk = r.companyFk
|
|
|
|
WHERE r.clientFk = vClientFk
|
|
|
|
AND r.payed > util.VN_CURDATE()
|
|
|
|
GROUP BY r.companyFk, DATE(r.payed)
|
|
|
|
), risk AS (
|
2024-05-13 10:32:46 +00:00
|
|
|
SELECT b.companyFk,
|
2024-07-31 17:38:50 +00:00
|
|
|
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
|
2024-07-31 17:38:50 +00:00
|
|
|
LEFT JOIN receipt r ON r.dated > ui.dated
|
|
|
|
AND r.companyFk = ui.companyFk
|
2024-05-13 10:32:46 +00:00
|
|
|
GROUP BY b.companyFk, ui.dated
|
2024-07-31 17:38:50 +00:00
|
|
|
)
|
|
|
|
SELECT ti.ticketFk, r.amount, ti.isProblemCalcNeeded
|
|
|
|
FROM ticket ti
|
|
|
|
JOIN risk r ON r.dated = ti.dated
|
|
|
|
AND r.companyFk = ti.companyFk;
|
2024-07-12 14:47:24 +00:00
|
|
|
|
|
|
|
UPDATE ticket t
|
|
|
|
JOIN tTicketRisk tr ON tr.ticketFk = t.id
|
2024-07-31 12:59:12 +00:00
|
|
|
SET t.risk = IF(tr.isProblemCalcNeeded, tr.amount, NULL);
|
2024-05-02 16:13:12 +00:00
|
|
|
|
2024-05-03 08:19:05 +00:00
|
|
|
DROP TEMPORARY TABLE tTicketRisk;
|
2024-07-31 12:59:12 +00:00
|
|
|
END IF;
|
2024-04-16 15:01:14 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|