Merge pull request 'refactor: refs #7519 Refactor claim_ratio_routine' (!2394) from 6701-claimRatioRoutine into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #2394
Reviewed-by: Carlos Andrés <carlosap@verdnatura.es>
This commit is contained in:
Guillermo Bonet 2024-06-21 05:05:20 +00:00
commit 6874474207
4 changed files with 206 additions and 171 deletions

View File

@ -839,9 +839,9 @@ INSERT INTO `vn`.`config`(`id`, `mdbServer`, `fakeEmail`, `defaultersMaxAmount`,
INSERT INTO `vn`.`greugeType`(`id`, `name`, `code`) INSERT INTO `vn`.`greugeType`(`id`, `name`, `code`)
VALUES VALUES
(1, 'Diff', 'diff'), (1, 'Diff', 'diff'),
(2, 'Recover', 'recover'), (2, 'Recovery', 'recovery'),
(3, 'Mana', 'mana'), (3, 'Mana', 'mana'),
(4, 'Reclaim', 'reclaim'), (4, 'Claim', 'claim'),
(5, 'Heritage', 'heritage'), (5, 'Heritage', 'heritage'),
(6, 'Miscellaneous', 'miscellaneous'), (6, 'Miscellaneous', 'miscellaneous'),
(7, 'Freight Pickup', 'freightPickUp'); (7, 'Freight Pickup', 'freightPickUp');
@ -1885,9 +1885,9 @@ INSERT INTO `vn`.`claimEnd`(`id`, `saleFk`, `claimFk`, `workerFk`, `claimDestina
(1, 31, 4, 21, 2), (1, 31, 4, 21, 2),
(2, 32, 3, 21, 3); (2, 32, 3, 21, 3);
INSERT INTO `vn`.`claimConfig`(`id`, `maxResponsibility`) INSERT INTO `vn`.`claimConfig`(`id`, `maxResponsibility`, `monthsToRefund`, `minShipped`)
VALUES VALUES
(1, 50); (1, 5, 4, '2016-10-01');
INSERT INTO `vn`.`claimRatio`(`clientFk`, `yearSale`, `claimAmount`, `claimingRate`, `priceIncreasing`, `packingRate`) INSERT INTO `vn`.`claimRatio`(`clientFk`, `yearSale`, `claimAmount`, `claimingRate`, `priceIncreasing`, `packingRate`)
VALUES VALUES

View File

@ -1,167 +0,0 @@
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
INSERT INTO vn.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)
, 4
, 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;
-- Reclamaciones que pasan a Maná
INSERT INTO vn.greuge(shipped, clientFk, description,
amount, greugeTypeFk, ticketFk)
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)
,3
,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;
-- Marcamos para no repetir
UPDATE vn.claimEnd ce
JOIN vn.claim c ON c.id = ce.claimFk
SET ce.isGreuge = TRUE
WHERE ce.claimDestinationFk NOT IN (1,5)
AND NOT ce.isGreuge
AND c.claimStateFk = 3;
-- Recobros
DROP TEMPORARY TABLE IF EXISTS tmp.ticket_list;
CREATE TEMPORARY TABLE tmp.ticket_list
(PRIMARY KEY (Id_Ticket))
SELECT DISTINCT t.id Id_Ticket
FROM vn.saleComponent sc
JOIN vn.sale s ON sc.saleFk = s.id
JOIN vn.ticket t ON t.id = s.ticketFk
JOIN vn.ticketLastState ts ON ts.ticketFk = t.id
JOIN vn.ticketTracking tt ON tt.id = ts.ticketTrackingFk
JOIN vn.state st ON st.id = tt.stateFk
JOIN vn.alertLevel al ON al.code = 'DELIVERED'
WHERE sc.componentFk = 17
AND sc.isGreuge = 0
AND t.shipped >= '2016-10-01'
AND t.shipped < util.VN_CURDATE()
AND st.alertLevel >= al.id;
DELETE g.*
FROM vn.greuge g
JOIN tmp.ticket_list t ON g.ticketFk = t.Id_Ticket
WHERE g.greugeTypeFk = 2;
INSERT INTO vn.greuge(clientFk, description, amount,shipped,
greugeTypeFk, ticketFk)
SELECT t.clientFk
,concat('recobro ', s.ticketFk), - round(SUM(sc.value*s.quantity),2)
AS dif,
date(t.shipped)
, 2
,tt.Id_Ticket
FROM vn.sale s
JOIN vn.ticket t ON t.id = s.ticketFk
JOIN tmp.ticket_list tt ON tt.Id_Ticket = t.id
JOIN vn.saleComponent sc
ON sc.saleFk = s.id AND sc.componentFk = 17
GROUP BY t.id
HAVING ABS(dif) > 1;
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;
/*
* 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)
SELECT id, 0,0,0,0
FROM vn.client;
REPLACE bi.claims_ratio(Id_Cliente, Consumo, Reclamaciones, Ratio, recobro)
SELECT fm.Id_Cliente, 12 * fm.Consumo, Reclamaciones,
round(Reclamaciones / (12*fm.Consumo),4), 0
FROM bi.facturacion_media_anual fm
LEFT JOIN(
SELECT c.clientFk, round(sum(-1 * ((c.responsibility -1)/4) *
s.quantity * s.price * (100 - s.discount) / 100))
AS Reclamaciones
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;
-- Calculamos el porcentaje del recobro para añadirlo al precio de venta
UPDATE bi.claims_ratio cr
JOIN (
SELECT clientFk Id_Cliente, IFNULL(SUM(amount), 0) AS Greuge
FROM vn.greuge
WHERE shipped <= util.VN_CURDATE()
GROUP BY clientFk
) g ON g.Id_Cliente = cr.Id_Cliente
SET recobro = GREATEST(0,round(IFNULL(Greuge, 0) /
(IFNULL(Consumo, 0) * vMonthToRefund / 12 ) ,3));
-- 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;
-- CLIENTE 15979, SERVEIS VETERINARIS
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 ;

View File

@ -0,0 +1,190 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`claimRatio_add`()
BEGIN
/*
* Añade a la tabla greuges todos los cargos necesario y
* que luego lo utilizamos para calcular el recobro.
*/
DECLARE vMonthToRefund INT
DEFAULT (SELECT monthsToRefund FROM claimConfig);
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');
IF vMonthToRefund IS NULL
OR vRecoveryGreugeType IS NULL
OR vManaGreugeType IS NULL
OR vClaimGreugeType IS NULL
OR vDebtComponentType IS NULL THEN
CALL util.throw('Required variables not found');
END IF;
-- Reclamaciones demasiado sensibles
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
FROM sale s
JOIN claimEnd ce ON ce.saleFk = s.id
JOIN claimDestination cd ON cd.id = ce.claimDestinationFk
JOIN claim c ON c.id = ce.claimFk
JOIN claimState cs ON cs.id = c.claimStateFk
WHERE cd.description NOT IN ('Bueno', 'Corregido')
AND NOT ce.isGreuge
AND cs.code = 'resolved';
-- Reclamaciones que pasan a Maná
INSERT INTO greuge(
shipped,
clientFk,
`description`,
amount,
greugeTypeFk,
ticketFk
)
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),
vManaGreugeType,
s.ticketFk
FROM sale s
JOIN claimEnd ce ON ce.saleFk = s.id
JOIN claimDestination cd ON cd.id = ce.claimDestinationFk
JOIN claim c ON c.id = ce.claimFk
JOIN claimState cs ON cs.id = c.claimStateFk
WHERE cd.description NOT IN ('Bueno', 'Corregido')
AND NOT ce.isGreuge
AND cs.code = 'resolved'
AND c.isChargedToMana;
-- Marcamos para no repetir
UPDATE claimEnd ce
JOIN claimDestination cd ON cd.id = ce.claimDestinationFk
JOIN claim c ON c.id = ce.claimFk
JOIN claimState cs ON cs.id = c.claimStateFk
SET ce.isGreuge = TRUE
WHERE cd.description NOT IN ('Bueno', 'Corregido')
AND NOT ce.isGreuge
AND cs.code = 'resolved';
-- Recobros
CREATE OR REPLACE TEMPORARY TABLE tTicketList
(PRIMARY KEY (ticketFk))
ENGINE = MEMORY
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
AND t.shipped >= (SELECT minShipped FROM claimConfig)
AND t.shipped < util.VN_CURDATE()
AND al.code = 'DELIVERED';
DELETE g.*
FROM greuge g
JOIN tTicketList t ON t.ticketFk = g.ticketFk
WHERE g.greugeTypeFk = vRecoveryGreugeType;
INSERT INTO greuge(
clientFk,
`description`,
amount,
shipped,
greugeTypeFk,
ticketFk
)
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
AND sc.componentFk = vDebtComponentType
GROUP BY t.id
HAVING ABS(dif) > 1;
UPDATE saleComponent sc
JOIN sale s ON s.id = sc.saleFk
JOIN tTicketList tl ON tl.ticketFk = s.ticketFk
SET sc.isGreuge = TRUE
WHERE sc.componentFk = vDebtComponentType;
REPLACE claimRatio(
clientFk,
yearSale,
claimAmount,
claimingRate,
priceIncreasing
)
SELECT c.id,
12 * cac.invoiced,
totalClaims,
ROUND(totalClaims / (12 * cac.invoiced), 4),
0
FROM client c
LEFT JOIN bs.clientAnnualConsumption cac ON cac.clientFk = c.id
LEFT JOIN (
SELECT c.clientFk,
ROUND(SUM(-1 * ((c.responsibility - 1) / 4) *
s.quantity * s.price * (100 - s.discount)
/ 100)) totalClaims
FROM sale s
JOIN claimEnd ce ON ce.saleFk = s.id
JOIN claimDestination cd ON cd.id = ce.claimDestinationFk
JOIN claim c ON c.id = ce.claimFk
JOIN claimState cs ON cs.id = c.claimStateFk
WHERE cd.description NOT IN ('Bueno', 'Corregido')
AND cs.code = 'resolved'
AND c.ticketCreated >= util.VN_CURDATE() - INTERVAL 1 YEAR
GROUP BY c.clientFk
) sub ON sub.clientFk = c.id;
-- Calculamos el porcentaje del recobro para añadirlo al precio de venta
UPDATE claimRatio cr
JOIN (
SELECT clientFk, IFNULL(SUM(amount), 0) greuge
FROM greuge
WHERE shipped <= util.VN_CURDATE()
GROUP BY clientFk
) sub ON sub.clientFk = cr.clientFk
SET cr.priceIncreasing = GREATEST(0, ROUND(IFNULL(sub.greuge, 0) /
(IFNULL(cr.yearSale, 0) * vMonthToRefund / 12 ), 3));
-- Protección neonatos
UPDATE claimRatio cr
JOIN firstTicketShipped fts ON fts.clientFk = cr.clientFk
SET cr.priceIncreasing = 0,
cr.claimingRate = 0
WHERE fts.shipped > util.VN_CURDATE() - INTERVAL 1 MONTH;
DROP TEMPORARY TABLE tTicketList;
END$$
DELIMITER ;

View File

@ -0,0 +1,12 @@
UPDATE IGNORE bs.nightTask
SET `schema` = 'vn',
`procedure` = 'claimRatio_add'
WHERE `procedure` = 'claim_ratio_routine';
ALTER TABLE vn.claimConfig
ADD monthsToRefund int(11) DEFAULT NULL NULL,
ADD minShipped date DEFAULT NULL NULL;
UPDATE IGNORE vn.claimConfig
SET monthsToRefund = 4,
minShipped = '2016-10-01';