2024-05-02 12:59:30 +00:00
|
|
|
DELIMITER $$
|
2024-07-22 09:52:42 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`claimRatio_add`()
|
2024-05-02 12:59:30 +00:00
|
|
|
BEGIN
|
|
|
|
/*
|
2024-05-03 12:15:55 +00:00
|
|
|
* Añade a la tabla greuges todos los cargos necesario y
|
|
|
|
* que luego lo utilizamos para calcular el recobro.
|
2024-05-02 12:59:30 +00:00
|
|
|
*/
|
2024-06-17 07:42:16 +00:00
|
|
|
DECLARE vMonthToRefund INT
|
|
|
|
DEFAULT (SELECT monthsToRefund FROM claimConfig);
|
2024-05-03 11:47:55 +00:00
|
|
|
DECLARE vRecoveryGreugeType INT
|
|
|
|
DEFAULT (SELECT id FROM greugeType WHERE code = 'recovery');
|
|
|
|
DECLARE vManaGreugeType INT
|
|
|
|
DEFAULT (SELECT id FROM greugeType WHERE code = 'mana');
|
|
|
|
DECLARE vClaimGreugeType INT
|
|
|
|
DEFAULT (SELECT id FROM greugeType WHERE code = 'claim');
|
|
|
|
DECLARE vDebtComponentType INT
|
|
|
|
DEFAULT (SELECT id FROM component WHERE code = 'debtCollection');
|
2024-05-02 12:59:30 +00:00
|
|
|
|
2024-06-17 07:42:16 +00:00
|
|
|
IF vMonthToRefund IS NULL
|
|
|
|
OR vRecoveryGreugeType IS NULL
|
2024-05-03 11:47:55 +00:00
|
|
|
OR vManaGreugeType IS NULL
|
|
|
|
OR vClaimGreugeType IS NULL
|
|
|
|
OR vDebtComponentType IS NULL THEN
|
|
|
|
|
|
|
|
CALL util.throw('Required variables not found');
|
|
|
|
END IF;
|
2024-05-02 12:59:30 +00:00
|
|
|
|
|
|
|
-- Reclamaciones demasiado sensibles
|
2024-05-03 11:47:55 +00:00
|
|
|
INSERT INTO greuge(
|
|
|
|
shipped,
|
|
|
|
clientFk,
|
|
|
|
`description`,
|
|
|
|
amount,
|
|
|
|
greugeTypeFk,
|
|
|
|
ticketFk
|
|
|
|
)
|
|
|
|
SELECT c.ticketCreated,
|
|
|
|
c.clientFk,
|
|
|
|
CONCAT('Claim ', c.id,' : ', s.concept),
|
|
|
|
ROUND(-1 * ((c.responsibility - 1) / 4) * s.quantity *
|
|
|
|
s.price * (100 - s.discount) / 100, 2),
|
|
|
|
vClaimGreugeType,
|
|
|
|
s.ticketFk
|
2024-05-02 12:59:30 +00:00
|
|
|
FROM sale s
|
|
|
|
JOIN claimEnd ce ON ce.saleFk = s.id
|
2024-05-03 11:47:55 +00:00
|
|
|
JOIN claimDestination cd ON cd.id = ce.claimDestinationFk
|
2024-05-02 12:59:30 +00:00
|
|
|
JOIN claim c ON c.id = ce.claimFk
|
2024-05-03 11:47:55 +00:00
|
|
|
JOIN claimState cs ON cs.id = c.claimStateFk
|
2024-06-13 11:11:50 +00:00
|
|
|
WHERE cd.description NOT IN ('Bueno', 'Corregido')
|
2024-05-02 12:59:30 +00:00
|
|
|
AND NOT ce.isGreuge
|
2024-05-03 11:47:55 +00:00
|
|
|
AND cs.code = 'resolved';
|
2024-05-02 12:59:30 +00:00
|
|
|
|
|
|
|
-- Reclamaciones que pasan a Maná
|
2024-05-03 11:47:55 +00:00
|
|
|
INSERT INTO greuge(
|
|
|
|
shipped,
|
|
|
|
clientFk,
|
|
|
|
`description`,
|
|
|
|
amount,
|
|
|
|
greugeTypeFk,
|
|
|
|
ticketFk
|
|
|
|
)
|
2024-05-02 12:59:30 +00:00
|
|
|
SELECT c.ticketCreated,
|
|
|
|
c.clientFk,
|
2024-05-03 11:47:55 +00:00
|
|
|
CONCAT('Claim_mana ', c.id,' : ', s.concept),
|
2024-05-03 12:15:55 +00:00
|
|
|
ROUND(((c.responsibility - 1) / 4) * s.quantity *
|
|
|
|
s.price * (100 - s.discount) / 100, 2),
|
2024-05-03 11:47:55 +00:00
|
|
|
vManaGreugeType,
|
2024-05-02 12:59:30 +00:00
|
|
|
s.ticketFk
|
|
|
|
FROM sale s
|
|
|
|
JOIN claimEnd ce ON ce.saleFk = s.id
|
2024-05-03 11:47:55 +00:00
|
|
|
JOIN claimDestination cd ON cd.id = ce.claimDestinationFk
|
2024-05-02 12:59:30 +00:00
|
|
|
JOIN claim c ON c.id = ce.claimFk
|
2024-05-03 11:47:55 +00:00
|
|
|
JOIN claimState cs ON cs.id = c.claimStateFk
|
|
|
|
WHERE cd.description NOT IN ('Bueno', 'Corregido')
|
2024-05-02 12:59:30 +00:00
|
|
|
AND NOT ce.isGreuge
|
2024-05-03 11:47:55 +00:00
|
|
|
AND cs.code = 'resolved'
|
2024-05-02 12:59:30 +00:00
|
|
|
AND c.isChargedToMana;
|
|
|
|
|
|
|
|
-- Marcamos para no repetir
|
|
|
|
UPDATE claimEnd ce
|
2024-05-03 11:47:55 +00:00
|
|
|
JOIN claimDestination cd ON cd.id = ce.claimDestinationFk
|
2024-05-02 12:59:30 +00:00
|
|
|
JOIN claim c ON c.id = ce.claimFk
|
2024-05-03 11:47:55 +00:00
|
|
|
JOIN claimState cs ON cs.id = c.claimStateFk
|
2024-06-03 09:41:09 +00:00
|
|
|
SET ce.isGreuge = TRUE
|
2024-05-03 11:47:55 +00:00
|
|
|
WHERE cd.description NOT IN ('Bueno', 'Corregido')
|
2024-05-02 12:59:30 +00:00
|
|
|
AND NOT ce.isGreuge
|
2024-05-03 11:47:55 +00:00
|
|
|
AND cs.code = 'resolved';
|
2024-05-02 12:59:30 +00:00
|
|
|
|
|
|
|
-- Recobros
|
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tTicketList
|
2024-06-13 11:11:50 +00:00
|
|
|
(PRIMARY KEY (ticketFk))
|
2024-06-17 07:42:16 +00:00
|
|
|
ENGINE = MEMORY
|
2024-06-13 11:11:50 +00:00
|
|
|
SELECT DISTINCT s.ticketFk
|
|
|
|
FROM saleComponent sc
|
|
|
|
JOIN sale s ON sc.saleFk = s.id
|
|
|
|
JOIN ticket t ON t.id = s.ticketFk
|
|
|
|
JOIN ticketLastState ts ON ts.ticketFk = t.id
|
|
|
|
JOIN ticketTracking tt ON tt.id = ts.ticketTrackingFk
|
|
|
|
JOIN state st ON st.id = tt.stateFk
|
|
|
|
JOIN alertLevel al ON al.id = st.alertLevel
|
|
|
|
WHERE sc.componentFk = vDebtComponentType
|
|
|
|
AND NOT sc.isGreuge
|
2024-06-17 07:42:16 +00:00
|
|
|
AND t.shipped >= (SELECT minShipped FROM claimConfig)
|
2024-06-13 11:11:50 +00:00
|
|
|
AND t.shipped < util.VN_CURDATE()
|
|
|
|
AND al.code = 'DELIVERED';
|
2024-05-02 12:59:30 +00:00
|
|
|
|
|
|
|
DELETE g.*
|
|
|
|
FROM greuge g
|
|
|
|
JOIN tTicketList t ON t.ticketFk = g.ticketFk
|
|
|
|
WHERE g.greugeTypeFk = vRecoveryGreugeType;
|
|
|
|
|
2024-05-03 11:47:55 +00:00
|
|
|
INSERT INTO greuge(
|
|
|
|
clientFk,
|
|
|
|
`description`,
|
|
|
|
amount,
|
|
|
|
shipped,
|
|
|
|
greugeTypeFk,
|
|
|
|
ticketFk
|
|
|
|
)
|
2024-05-02 12:59:30 +00:00
|
|
|
SELECT t.clientFk,
|
|
|
|
'Recobro',
|
|
|
|
- ROUND(SUM(sc.value * s.quantity), 2) dif,
|
|
|
|
DATE(t.shipped),
|
|
|
|
vRecoveryGreugeType,
|
|
|
|
tl.ticketFk
|
|
|
|
FROM sale s
|
|
|
|
JOIN ticket t ON t.id = s.ticketFk
|
|
|
|
JOIN tTicketList tl ON tl.ticketFk = t.id
|
|
|
|
JOIN saleComponent sc ON sc.saleFk = s.id
|
2024-05-03 11:47:55 +00:00
|
|
|
AND sc.componentFk = vDebtComponentType
|
2024-05-02 12:59:30 +00:00
|
|
|
GROUP BY t.id
|
2024-05-03 11:47:55 +00:00
|
|
|
HAVING ABS(dif) > 1;
|
2024-05-02 12:59:30 +00:00
|
|
|
|
|
|
|
UPDATE saleComponent sc
|
|
|
|
JOIN sale s ON s.id = sc.saleFk
|
|
|
|
JOIN tTicketList tl ON tl.ticketFk = s.ticketFk
|
|
|
|
SET sc.isGreuge = TRUE
|
2024-05-03 11:47:55 +00:00
|
|
|
WHERE sc.componentFk = vDebtComponentType;
|
2024-06-13 11:11:50 +00:00
|
|
|
|
|
|
|
REPLACE claimRatio(
|
2024-05-03 11:47:55 +00:00
|
|
|
clientFk,
|
|
|
|
yearSale,
|
|
|
|
claimAmount,
|
|
|
|
claimingRate,
|
|
|
|
priceIncreasing
|
|
|
|
)
|
2024-06-13 11:11:50 +00:00
|
|
|
SELECT c.id,
|
2024-05-03 11:47:55 +00:00
|
|
|
12 * cac.invoiced,
|
|
|
|
totalClaims,
|
|
|
|
ROUND(totalClaims / (12 * cac.invoiced), 4),
|
|
|
|
0
|
2024-06-13 11:11:50 +00:00
|
|
|
FROM client c
|
|
|
|
LEFT JOIN bs.clientAnnualConsumption cac ON cac.clientFk = c.id
|
2024-05-03 11:47:55 +00:00
|
|
|
LEFT JOIN (
|
|
|
|
SELECT c.clientFk,
|
|
|
|
ROUND(SUM(-1 * ((c.responsibility - 1) / 4) *
|
2024-05-03 12:15:55 +00:00
|
|
|
s.quantity * s.price * (100 - s.discount)
|
|
|
|
/ 100)) totalClaims
|
2024-05-02 12:59:30 +00:00
|
|
|
FROM sale s
|
|
|
|
JOIN claimEnd ce ON ce.saleFk = s.id
|
2024-05-03 11:47:55 +00:00
|
|
|
JOIN claimDestination cd ON cd.id = ce.claimDestinationFk
|
2024-05-02 12:59:30 +00:00
|
|
|
JOIN claim c ON c.id = ce.claimFk
|
2024-05-03 11:47:55 +00:00
|
|
|
JOIN claimState cs ON cs.id = c.claimStateFk
|
2024-06-13 11:11:50 +00:00
|
|
|
WHERE cd.description NOT IN ('Bueno', 'Corregido')
|
2024-05-03 11:47:55 +00:00
|
|
|
AND cs.code = 'resolved'
|
2024-05-02 12:59:30 +00:00
|
|
|
AND c.ticketCreated >= util.VN_CURDATE() - INTERVAL 1 YEAR
|
|
|
|
GROUP BY c.clientFk
|
2024-06-13 11:11:50 +00:00
|
|
|
) sub ON sub.clientFk = c.id;
|
2024-05-02 12:59:30 +00:00
|
|
|
|
|
|
|
-- Calculamos el porcentaje del recobro para añadirlo al precio de venta
|
|
|
|
UPDATE claimRatio cr
|
|
|
|
JOIN (
|
2024-05-03 11:47:55 +00:00
|
|
|
SELECT clientFk, IFNULL(SUM(amount), 0) greuge
|
2024-05-02 12:59:30 +00:00
|
|
|
FROM greuge
|
|
|
|
WHERE shipped <= util.VN_CURDATE()
|
|
|
|
GROUP BY clientFk
|
2024-05-03 12:15:55 +00:00
|
|
|
) sub ON sub.clientFk = cr.clientFk
|
2024-05-03 11:47:55 +00:00
|
|
|
SET cr.priceIncreasing = GREATEST(0, ROUND(IFNULL(sub.greuge, 0) /
|
2024-06-03 09:46:52 +00:00
|
|
|
(IFNULL(cr.yearSale, 0) * vMonthToRefund / 12 ), 3));
|
2024-05-02 12:59:30 +00:00
|
|
|
|
|
|
|
-- Protección neonatos
|
|
|
|
UPDATE claimRatio cr
|
|
|
|
JOIN firstTicketShipped fts ON fts.clientFk = cr.clientFk
|
|
|
|
SET cr.priceIncreasing = 0,
|
|
|
|
cr.claimingRate = 0
|
2024-06-03 09:46:52 +00:00
|
|
|
WHERE fts.shipped > util.VN_CURDATE() - INTERVAL 1 MONTH;
|
2024-05-03 12:15:55 +00:00
|
|
|
|
2024-05-02 12:59:30 +00:00
|
|
|
DROP TEMPORARY TABLE tTicketList;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|