2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bi`.`claim_ratio_routine`()
|
|
|
|
BEGIN
|
|
|
|
DECLARE vMonthToRefund INT DEFAULT 4;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PAK 2015-11-20
|
|
|
|
* Se trata de añadir a la tabla Greuges todos los
|
|
|
|
* cargos que luego vamos a utilizar para calcular el recobro
|
|
|
|
*/
|
|
|
|
|
|
|
|
-- Reclamaciones demasiado sensibles
|
|
|
|
|
2024-02-05 08:02:31 +00:00
|
|
|
INSERT INTO vn.greuge(shipped, clientFk, description,
|
|
|
|
amount, greugeTypeFk, ticketFk)
|
2024-04-24 07:53:31 +00:00
|
|
|
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)
|
2024-01-15 11:31:03 +00:00
|
|
|
, 4
|
2024-04-24 07:53:31 +00:00
|
|
|
, s.ticketFk
|
|
|
|
FROM vn.sale s
|
|
|
|
JOIN vn.claimEnd ce ON ce.saleFk = s.id
|
|
|
|
JOIN vn.claim c ON c.id = ce.claimFk
|
|
|
|
WHERE ce.claimDestinationFk NOT IN (1,5)
|
|
|
|
AND NOT ce.isGreuge
|
|
|
|
AND c.claimStateFk = 3;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
-- Reclamaciones que pasan a Maná
|
|
|
|
|
2024-02-05 08:02:31 +00:00
|
|
|
INSERT INTO vn.greuge(shipped, clientFk, description,
|
|
|
|
amount, greugeTypeFk, ticketFk)
|
2024-04-24 07:53:31 +00:00
|
|
|
SELECT c.ticketCreated
|
|
|
|
, c.clientFk
|
|
|
|
, concat('Claim_mana ',c.id,' : ', s.concept)
|
|
|
|
,round( ((c.responsibility -1)/4) * s.quantity * s.price * (100 - s.discount) / 100, 2)
|
2024-01-15 11:31:03 +00:00
|
|
|
,3
|
2024-04-24 07:53:31 +00:00
|
|
|
,s.ticketFk
|
|
|
|
FROM vn.sale s
|
|
|
|
JOIN vn.claimEnd ce ON ce.saleFk = s.id
|
|
|
|
JOIN vn.claim c ON c.id = ce.claimFk
|
|
|
|
WHERE ce.claimDestinationFk NOT IN (1,5)
|
|
|
|
AND NOT ce.isGreuge
|
|
|
|
AND c.claimStateFk = 3
|
|
|
|
AND c.isChargedToMana;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
-- Marcamos para no repetir
|
2024-04-24 07:53:31 +00:00
|
|
|
UPDATE vn.claimEnd ce
|
|
|
|
JOIN vn.claim c ON c.id = ce.claimFk
|
2024-05-30 05:18:30 +00:00
|
|
|
SET ce.isGreuge = TRUE
|
2024-04-24 07:53:31 +00:00
|
|
|
WHERE ce.claimDestinationFk NOT IN (1,5)
|
|
|
|
AND NOT ce.isGreuge
|
|
|
|
AND c.claimStateFk = 3;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
-- Recobros
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.ticket_list;
|
|
|
|
CREATE TEMPORARY TABLE tmp.ticket_list
|
|
|
|
(PRIMARY KEY (Id_Ticket))
|
2024-02-06 13:55:20 +00:00
|
|
|
SELECT DISTINCT t.id Id_Ticket
|
2024-04-24 07:53:31 +00:00
|
|
|
FROM vn.saleComponent sc
|
|
|
|
JOIN vn.sale s ON sc.saleFk = s.id
|
|
|
|
JOIN vn.ticket t ON t.id = s.ticketFk
|
2024-02-05 12:52:28 +00:00
|
|
|
JOIN vn.ticketLastState ts ON ts.ticketFk = t.id
|
|
|
|
JOIN vn.ticketTracking tt ON tt.id = ts.ticketTrackingFk
|
2024-04-24 07:53:31 +00:00
|
|
|
JOIN vn.state st ON st.id = tt.stateFk
|
2024-06-05 06:05:46 +00:00
|
|
|
JOIN vn.alertLevel al ON al.code = 'DELIVERED'
|
2024-04-24 07:53:31 +00:00
|
|
|
WHERE sc.componentFk = 17
|
|
|
|
AND sc.isGreuge = 0
|
2024-02-05 12:52:28 +00:00
|
|
|
AND t.shipped >= '2016-10-01'
|
|
|
|
AND t.shipped < util.VN_CURDATE()
|
2024-06-05 06:05:46 +00:00
|
|
|
AND st.alertLevel >= al.id;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
DELETE g.*
|
2024-02-05 08:02:31 +00:00
|
|
|
FROM vn.greuge g
|
|
|
|
JOIN tmp.ticket_list t ON g.ticketFk = t.Id_Ticket
|
2024-02-14 11:12:40 +00:00
|
|
|
WHERE g.greugeTypeFk = 2;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-02-05 08:02:31 +00:00
|
|
|
INSERT INTO vn.greuge(clientFk, description, amount,shipped,
|
|
|
|
greugeTypeFk, ticketFk)
|
2024-02-07 10:41:22 +00:00
|
|
|
SELECT t.clientFk
|
2024-04-24 07:53:31 +00:00
|
|
|
,concat('recobro ', s.ticketFk), - round(SUM(sc.value*s.quantity),2)
|
|
|
|
AS dif,
|
|
|
|
date(t.shipped)
|
2024-01-15 11:31:03 +00:00
|
|
|
, 2
|
|
|
|
,tt.Id_Ticket
|
2024-04-24 07:53:31 +00:00
|
|
|
FROM vn.sale s
|
|
|
|
JOIN vn.ticket t ON t.id = s.ticketFk
|
2024-02-05 12:52:28 +00:00
|
|
|
JOIN tmp.ticket_list tt ON tt.Id_Ticket = t.id
|
2024-04-24 07:53:31 +00:00
|
|
|
JOIN vn.saleComponent sc
|
|
|
|
ON sc.saleFk = s.id AND sc.componentFk = 17
|
2024-02-05 12:52:28 +00:00
|
|
|
GROUP BY t.id
|
2024-01-15 11:31:03 +00:00
|
|
|
HAVING ABS(dif) > 1;
|
|
|
|
|
2024-04-24 07:53:31 +00:00
|
|
|
UPDATE vn.saleComponent sc
|
|
|
|
JOIN vn.sale s ON s.id = sc.saleFk
|
|
|
|
JOIN tmp.ticket_list tt ON tt.Id_Ticket = s.ticketFk
|
|
|
|
SET sc.isGreuge = 1
|
|
|
|
WHERE sc.componentFk = 17;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Recalculamos la ratio de las reclamaciones, que luego
|
|
|
|
* se va a utilizar en el recobro
|
|
|
|
*/
|
|
|
|
|
|
|
|
REPLACE bi.claims_ratio(Id_Cliente, Consumo, Reclamaciones, Ratio, recobro)
|
2024-04-24 07:53:31 +00:00
|
|
|
SELECT id, 0,0,0,0
|
|
|
|
FROM vn.client;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
REPLACE bi.claims_ratio(Id_Cliente, Consumo, Reclamaciones, Ratio, recobro)
|
|
|
|
SELECT fm.Id_Cliente, 12 * fm.Consumo, Reclamaciones,
|
2024-04-24 07:53:31 +00:00
|
|
|
round(Reclamaciones / (12*fm.Consumo),4), 0
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM bi.facturacion_media_anual fm
|
|
|
|
LEFT JOIN(
|
2024-04-24 07:53:31 +00:00
|
|
|
SELECT c.clientFk, round(sum(-1 * ((c.responsibility -1)/4) *
|
|
|
|
s.quantity * s.price * (100 - s.discount) / 100))
|
2024-01-15 11:31:03 +00:00
|
|
|
AS Reclamaciones
|
2024-04-24 07:53:31 +00:00
|
|
|
FROM vn.sale s
|
|
|
|
JOIN vn.claimEnd ce ON ce.saleFk = s.id
|
|
|
|
JOIN vn.claim c ON c.id = ce.claimFk
|
|
|
|
WHERE ce.claimDestinationFk NOT IN (1,5)
|
|
|
|
AND c.claimStateFk = 3
|
|
|
|
AND c.ticketCreated >= TIMESTAMPADD(YEAR, -1, util.VN_CURDATE())
|
|
|
|
GROUP BY c.clientFk
|
|
|
|
) claims ON claims.clientFk = fm.Id_Cliente;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- Calculamos el porcentaje del recobro para añadirlo al precio de venta
|
|
|
|
UPDATE bi.claims_ratio cr
|
|
|
|
JOIN (
|
2024-02-14 06:07:00 +00:00
|
|
|
SELECT clientFk Id_Cliente, IFNULL(SUM(amount), 0) AS Greuge
|
2024-02-05 08:02:31 +00:00
|
|
|
FROM vn.greuge
|
|
|
|
WHERE shipped <= util.VN_CURDATE()
|
|
|
|
GROUP BY clientFk
|
2024-01-15 11:31:03 +00:00
|
|
|
) g ON g.Id_Cliente = cr.Id_Cliente
|
2024-02-12 07:11:49 +00:00
|
|
|
SET recobro = GREATEST(0,round(IFNULL(Greuge, 0) /
|
|
|
|
(IFNULL(Consumo, 0) * vMonthToRefund / 12 ) ,3));
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
-- Protección neonatos
|
|
|
|
UPDATE bi.claims_ratio cr
|
|
|
|
JOIN vn.firstTicketShipped fts ON fts.clientFk = cr.Id_Cliente
|
|
|
|
SET recobro = 0, Ratio = 0
|
|
|
|
WHERE fts.shipped > TIMESTAMPADD(MONTH,-1,util.VN_CURDATE());
|
|
|
|
|
|
|
|
-- CLIENTE 7983, JULIAN SUAU
|
|
|
|
UPDATE bi.claims_ratio SET recobro = LEAST(0.05, recobro) WHERE Id_Cliente = 7983;
|
|
|
|
|
|
|
|
-- CLIENTE 4358
|
|
|
|
UPDATE bi.claims_ratio SET recobro = GREATEST(0.05, recobro) WHERE Id_Cliente = 4358;
|
|
|
|
|
|
|
|
-- CLIENTE 5523, VERDECORA
|
|
|
|
UPDATE bi.claims_ratio SET recobro = GREATEST(0.12, recobro) WHERE Id_Cliente = 5523;
|
|
|
|
|
2024-04-24 07:53:31 +00:00
|
|
|
-- CLIENTE 15979, SERVEIS VETERINARIS
|
2024-01-15 11:31:03 +00:00
|
|
|
UPDATE bi.claims_ratio SET recobro = GREATEST(0.05, recobro) WHERE Id_Cliente = 15979;
|
|
|
|
|
|
|
|
-- CLIENTE 5189 i 8942, son de CSR i son el mateix client
|
|
|
|
UPDATE bi.claims_ratio cr
|
|
|
|
JOIN (SELECT sum(Consumo * recobro)/sum(Consumo) as recobro
|
|
|
|
FROM bi.claims_ratio
|
|
|
|
WHERE Id_Cliente IN ( 5189,8942)
|
|
|
|
) sub
|
|
|
|
SET cr.recobro = sub.recobro
|
|
|
|
WHERE Id_Cliente IN ( 5189,8942);
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|