refactor: refs #6701 Refactor claim_ratio_routine
gitea/salix/pipeline/pr-dev This commit looks good
Details
gitea/salix/pipeline/pr-dev This commit looks good
Details
This commit is contained in:
parent
3146f4f5b6
commit
883f6f18a3
|
@ -7,57 +7,82 @@ BEGIN
|
||||||
* cargos que luego vamos a utilizar para calcular el recobro
|
* cargos que luego vamos a utilizar para calcular el recobro
|
||||||
*/
|
*/
|
||||||
DECLARE vMonthToRefund INT DEFAULT 4;
|
DECLARE vMonthToRefund INT DEFAULT 4;
|
||||||
DECLARE vRecoveryGreugeType INT;
|
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');
|
||||||
|
|
||||||
SELECT id INTO vRecoveryGreugeType
|
IF vRecoveryGreugeType IS NULL
|
||||||
FROM greugeType
|
OR vManaGreugeType IS NULL
|
||||||
WHERE code = 'recovery';
|
OR vClaimGreugeType IS NULL
|
||||||
|
OR vDebtComponentType IS NULL THEN
|
||||||
|
|
||||||
|
CALL util.throw('Required variables not found');
|
||||||
|
END IF;
|
||||||
|
|
||||||
-- Reclamaciones demasiado sensibles
|
-- Reclamaciones demasiado sensibles
|
||||||
INSERT INTO greuge(shipped, clientFk, description,
|
INSERT INTO greuge(
|
||||||
amount, greugeTypeFk, ticketFk)
|
shipped,
|
||||||
SELECT c.ticketCreated
|
clientFk,
|
||||||
, c.clientFk
|
`description`,
|
||||||
, concat('Claim ', c.id,' : ', s.concept)
|
amount,
|
||||||
,round( -1 * ((c.responsibility -1)/4) * s.quantity *
|
greugeTypeFk,
|
||||||
s.price * (100 - s.discount) / 100, 2)
|
ticketFk
|
||||||
, 4
|
)
|
||||||
, s.ticketFk
|
|
||||||
FROM sale s
|
|
||||||
JOIN claimEnd ce ON ce.saleFk = s.id
|
|
||||||
JOIN 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 greuge(shipped,
|
|
||||||
clientFk,
|
|
||||||
`description`,
|
|
||||||
amount,
|
|
||||||
greugeTypeFk,
|
|
||||||
ticketFk)
|
|
||||||
SELECT c.ticketCreated,
|
SELECT c.ticketCreated,
|
||||||
c.clientFk,
|
c.clientFk,
|
||||||
concat('Claim_mana ',c.id,' : ', s.concept),
|
CONCAT('Claim ', c.id,' : ', s.concept),
|
||||||
round( ((c.responsibility -1)/4) * s.quantity * s.price * (100 - s.discount) / 100, 2),
|
ROUND(-1 * ((c.responsibility - 1) / 4) * s.quantity *
|
||||||
3,
|
s.price * (100 - s.discount) / 100, 2),
|
||||||
|
vClaimGreugeType,
|
||||||
s.ticketFk
|
s.ticketFk
|
||||||
FROM sale s
|
FROM sale s
|
||||||
JOIN claimEnd ce ON ce.saleFk = s.id
|
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 claim c ON c.id = ce.claimFk
|
||||||
WHERE ce.claimDestinationFk NOT IN (1,5)
|
JOIN claimState cs ON cs.id = c.claimStateFk
|
||||||
|
WHERE ce.claimDestinationFk NOT IN ('Bueno', 'Corregido')
|
||||||
AND NOT ce.isGreuge
|
AND NOT ce.isGreuge
|
||||||
AND c.claimStateFk = 3
|
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;
|
AND c.isChargedToMana;
|
||||||
|
|
||||||
-- Marcamos para no repetir
|
-- Marcamos para no repetir
|
||||||
UPDATE claimEnd ce
|
UPDATE claimEnd ce
|
||||||
|
JOIN claimDestination cd ON cd.id = ce.claimDestinationFk
|
||||||
JOIN claim c ON c.id = ce.claimFk
|
JOIN claim c ON c.id = ce.claimFk
|
||||||
|
JOIN claimState cs ON cs.id = c.claimStateFk
|
||||||
SET c.isChargedToMana = TRUE
|
SET c.isChargedToMana = TRUE
|
||||||
WHERE ce.claimDestinationFk NOT IN (1,5)
|
WHERE cd.description NOT IN ('Bueno', 'Corregido')
|
||||||
AND NOT ce.isGreuge
|
AND NOT ce.isGreuge
|
||||||
AND c.claimStateFk = 3;
|
AND cs.code = 'resolved';
|
||||||
|
|
||||||
-- Recobros
|
-- Recobros
|
||||||
CREATE OR REPLACE TEMPORARY TABLE tTicketList
|
CREATE OR REPLACE TEMPORARY TABLE tTicketList
|
||||||
|
@ -69,23 +94,26 @@ BEGIN
|
||||||
JOIN ticketLastState ts ON ts.ticketFk = t.id
|
JOIN ticketLastState ts ON ts.ticketFk = t.id
|
||||||
JOIN ticketTracking tt ON tt.id = ts.ticketTrackingFk
|
JOIN ticketTracking tt ON tt.id = ts.ticketTrackingFk
|
||||||
JOIN state st ON st.id = tt.stateFk
|
JOIN state st ON st.id = tt.stateFk
|
||||||
WHERE sc.componentFk = 17
|
JOIN alertLevel al ON al.id = st.alertLevel
|
||||||
AND sc.isGreuge = 0
|
WHERE sc.componentFk = vDebtComponentType
|
||||||
|
AND NOT sc.isGreuge
|
||||||
AND t.shipped >= '2016-10-01'
|
AND t.shipped >= '2016-10-01'
|
||||||
AND t.shipped < util.VN_CURDATE()
|
AND t.shipped < util.VN_CURDATE()
|
||||||
AND st.alertLevel >= 3;
|
AND al.code = 'DELIVERED';
|
||||||
|
|
||||||
DELETE g.*
|
DELETE g.*
|
||||||
FROM greuge g
|
FROM greuge g
|
||||||
JOIN tTicketList t ON t.ticketFk = g.ticketFk
|
JOIN tTicketList t ON t.ticketFk = g.ticketFk
|
||||||
WHERE g.greugeTypeFk = vRecoveryGreugeType;
|
WHERE g.greugeTypeFk = vRecoveryGreugeType;
|
||||||
|
|
||||||
INSERT INTO greuge(clientFk,
|
INSERT INTO greuge(
|
||||||
`description`,
|
clientFk,
|
||||||
amount,
|
`description`,
|
||||||
shipped,
|
amount,
|
||||||
greugeTypeFk,
|
shipped,
|
||||||
ticketFk)
|
greugeTypeFk,
|
||||||
|
ticketFk
|
||||||
|
)
|
||||||
SELECT t.clientFk,
|
SELECT t.clientFk,
|
||||||
'Recobro',
|
'Recobro',
|
||||||
- ROUND(SUM(sc.value * s.quantity), 2) dif,
|
- ROUND(SUM(sc.value * s.quantity), 2) dif,
|
||||||
|
@ -96,51 +124,68 @@ BEGIN
|
||||||
JOIN ticket t ON t.id = s.ticketFk
|
JOIN ticket t ON t.id = s.ticketFk
|
||||||
JOIN tTicketList tl ON tl.ticketFk = t.id
|
JOIN tTicketList tl ON tl.ticketFk = t.id
|
||||||
JOIN saleComponent sc ON sc.saleFk = s.id
|
JOIN saleComponent sc ON sc.saleFk = s.id
|
||||||
AND sc.componentFk = 17
|
AND sc.componentFk = vDebtComponentType
|
||||||
GROUP BY t.id
|
GROUP BY t.id
|
||||||
HAVING ABS(dif) > 1;
|
HAVING ABS(dif) > 1;
|
||||||
|
|
||||||
UPDATE saleComponent sc
|
UPDATE saleComponent sc
|
||||||
JOIN sale s ON s.id = sc.saleFk
|
JOIN sale s ON s.id = sc.saleFk
|
||||||
JOIN tTicketList tl ON tl.ticketFk = s.ticketFk
|
JOIN tTicketList tl ON tl.ticketFk = s.ticketFk
|
||||||
SET sc.isGreuge = TRUE
|
SET sc.isGreuge = TRUE
|
||||||
WHERE sc.componentFk = 17;
|
WHERE sc.componentFk = vDebtComponentType;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Recalculamos la ratio de las reclamaciones, que luego
|
* Recalculamos la ratio de las reclamaciones, que luego
|
||||||
* se va a utilizar en el recobro
|
* se va a utilizar en el recobro
|
||||||
*/
|
*/
|
||||||
REPLACE claimRatio(clientFk, yearSale, claimAmount, claimingRate, priceIncreasing)
|
REPLACE claimRatio(
|
||||||
SELECT id, 0, 0, 0, 0
|
clientFk,
|
||||||
FROM client;
|
yearSale,
|
||||||
|
claimAmount,
|
||||||
|
claimingRate,
|
||||||
|
priceIncreasing
|
||||||
|
)
|
||||||
|
SELECT id, 0, 0, 0, 0 FROM client;
|
||||||
|
|
||||||
REPLACE claimRatio(clientFk, yearSale, claimAmount, claimingRate, priceIncreasing)
|
REPLACE claimRatio(
|
||||||
SELECT cac.clientFk, 12 * cac.invoiced, totalClaims,
|
clientFk,
|
||||||
ROUND(totalClaims / (12 * cac.invoiced), 4), 0
|
yearSale,
|
||||||
|
claimAmount,
|
||||||
|
claimingRate,
|
||||||
|
priceIncreasing
|
||||||
|
)
|
||||||
|
SELECT cac.clientFk,
|
||||||
|
12 * cac.invoiced,
|
||||||
|
totalClaims,
|
||||||
|
ROUND(totalClaims / (12 * cac.invoiced), 4),
|
||||||
|
0
|
||||||
FROM bs.clientAnnualConsumption cac
|
FROM bs.clientAnnualConsumption cac
|
||||||
LEFT JOIN(
|
LEFT JOIN (
|
||||||
SELECT c.clientFk, round(sum(-1 * ((c.responsibility -1)/4) *
|
SELECT c.clientFk,
|
||||||
s.quantity * s.price * (100 - s.discount) / 100))
|
ROUND(SUM(-1 * ((c.responsibility - 1) / 4) *
|
||||||
totalClaims
|
s.quantity * s.price * (100 - s.discount)
|
||||||
|
/ 100)) totalClaims
|
||||||
FROM sale s
|
FROM sale s
|
||||||
JOIN claimEnd ce ON ce.saleFk = s.id
|
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 claim c ON c.id = ce.claimFk
|
||||||
WHERE ce.claimDestinationFk NOT IN (1,5)
|
JOIN claimState cs ON cs.id = c.claimStateFk
|
||||||
AND c.claimStateFk = 3
|
WHERE ce.claimDestinationFk NOT IN ('Bueno', 'Corregido')
|
||||||
|
AND cs.code = 'resolved'
|
||||||
AND c.ticketCreated >= util.VN_CURDATE() - INTERVAL 1 YEAR
|
AND c.ticketCreated >= util.VN_CURDATE() - INTERVAL 1 YEAR
|
||||||
GROUP BY c.clientFk
|
GROUP BY c.clientFk
|
||||||
) claims ON claims.clientFk = fm.Id_Cliente;
|
) sub ON sub.clientFk = cac.clientFk;
|
||||||
|
|
||||||
-- Calculamos el porcentaje del recobro para añadirlo al precio de venta
|
-- Calculamos el porcentaje del recobro para añadirlo al precio de venta
|
||||||
UPDATE claimRatio cr
|
UPDATE claimRatio cr
|
||||||
JOIN (
|
JOIN (
|
||||||
SELECT clientFk, IFNULL(SUM(amount), 0) AS Greuge
|
SELECT clientFk, IFNULL(SUM(amount), 0) greuge
|
||||||
FROM greuge
|
FROM greuge
|
||||||
WHERE shipped <= util.VN_CURDATE()
|
WHERE shipped <= util.VN_CURDATE()
|
||||||
GROUP BY clientFk
|
GROUP BY clientFk
|
||||||
) g ON g.clientFk = cr.clientFk
|
) sub ON subg.clientFk = cr.clientFk
|
||||||
SET cr.priceIncreasing = GREATEST(0, round(IFNULL(Greuge, 0) /
|
SET cr.priceIncreasing = GREATEST(0, ROUND(IFNULL(sub.greuge, 0) /
|
||||||
(IFNULL(Consumo, 0) * vMonthToRefund / 12 ), 3));
|
(IFNULL(cr.yearSale, 0) * vMonthToRefund / 12 ), 3));
|
||||||
|
|
||||||
-- Protección neonatos
|
-- Protección neonatos
|
||||||
UPDATE claimRatio cr
|
UPDATE claimRatio cr
|
||||||
|
|
Loading…
Reference in New Issue