2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bi`.`greuge_dif_porte_add`()
|
|
|
|
BEGIN
|
2024-03-27 16:00:27 +00:00
|
|
|
DECLARE vDateStarted DATETIME;
|
|
|
|
DECLARE vDateEnded DATETIME DEFAULT TIMESTAMPADD(DAY,-1,util.VN_CURDATE());
|
|
|
|
DECLARE vDaysSinceLastRecalculation INT;
|
|
|
|
|
|
|
|
SELECT daysSinceLastRecalculation INTO vDaysSinceLastRecalculation
|
|
|
|
FROM vn.greugeConfig;
|
|
|
|
|
|
|
|
SET vDateStarted = TIMESTAMPADD(DAY, -vDaysSinceLastRecalculation, util.VN_CURDATE());
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.dp;
|
|
|
|
|
|
|
|
-- Agencias que no cobran por volumen
|
|
|
|
CREATE TEMPORARY TABLE tmp.dp
|
|
|
|
(PRIMARY KEY (ticketFk))
|
|
|
|
ENGINE = MEMORY
|
|
|
|
SELECT t.id ticketFk,
|
|
|
|
SUM((t.zonePrice - t.zoneBonus) * ebv.ratio) AS teorico,
|
|
|
|
00000.00 as practico,
|
|
|
|
00000.00 as greuge,
|
2024-03-27 16:00:27 +00:00
|
|
|
t.clientFk,
|
|
|
|
t.shipped
|
|
|
|
FROM vn.ticket t
|
2024-01-15 11:31:03 +00:00
|
|
|
JOIN vn2008.Clientes cli ON cli.Id_cliente = t.clientFk
|
2024-02-05 08:02:31 +00:00
|
|
|
LEFT JOIN vn.expedition e ON e.ticketFk = t.id
|
|
|
|
JOIN vn.expeditionBoxVol ebv ON ebv.boxFk = e.freightItemFk
|
2024-01-15 11:31:03 +00:00
|
|
|
JOIN vn.zone z ON t.zoneFk = z.id
|
2024-03-27 16:00:27 +00:00
|
|
|
WHERE t.shipped between vDateStarted AND vDateEnded
|
|
|
|
AND cli.`real`
|
|
|
|
AND t.companyFk IN (442 , 567)
|
|
|
|
AND z.isVolumetric = FALSE
|
2024-01-15 11:31:03 +00:00
|
|
|
GROUP BY t.id;
|
|
|
|
|
|
|
|
-- Agencias que cobran por volumen
|
|
|
|
INSERT INTO tmp.dp
|
|
|
|
SELECT sv.ticketFk,
|
|
|
|
SUM(IFNULL(sv.freight,0)) AS teorico,
|
|
|
|
00000.00 as practico,
|
|
|
|
00000.00 as greuge,
|
2024-03-27 16:00:27 +00:00
|
|
|
sv.clientFk,
|
|
|
|
sv.shipped
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM vn.saleVolume sv
|
2024-03-27 16:00:27 +00:00
|
|
|
JOIN vn.zone z ON z.id = sv.zoneFk
|
|
|
|
AND sv.shipped BETWEEN vDateStarted AND vDateEnded
|
2024-01-15 11:31:03 +00:00
|
|
|
AND z.isVolumetric != FALSE
|
|
|
|
GROUP BY sv.ticketFk;
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.dp_aux;
|
|
|
|
|
|
|
|
CREATE TEMPORARY TABLE tmp.dp_aux
|
|
|
|
(PRIMARY KEY (ticketFk))
|
|
|
|
ENGINE = MEMORY
|
|
|
|
SELECT dp.ticketFk, sum(Cantidad * Valor) as valor
|
2024-03-27 16:00:27 +00:00
|
|
|
FROM tmp.dp
|
|
|
|
JOIN vn2008.Movimientos m ON m.Id_Ticket = dp.ticketFk
|
|
|
|
JOIN vn2008.Movimientos_componentes mc using(Id_Movimiento)
|
|
|
|
WHERE mc.Id_Componente = 15
|
|
|
|
GROUP BY dp.ticketFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
UPDATE tmp.dp
|
|
|
|
JOIN tmp.dp_aux USING(ticketFk)
|
|
|
|
SET practico = IFNULL(valor,0);
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tmp.dp_aux;
|
|
|
|
|
|
|
|
CREATE TEMPORARY TABLE tmp.dp_aux
|
|
|
|
(PRIMARY KEY (ticketFk))
|
|
|
|
ENGINE = MEMORY
|
2024-02-05 08:02:31 +00:00
|
|
|
SELECT dp.ticketFk, sum(g.amount) Importe
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM tmp.dp
|
2024-02-05 08:02:31 +00:00
|
|
|
JOIN vn.greuge g ON g.ticketFk = dp.ticketFk
|
|
|
|
WHERE g.greugeTypeFk = 1 -- dif_porte
|
2024-01-15 11:31:03 +00:00
|
|
|
GROUP BY dp.ticketFk;
|
|
|
|
|
|
|
|
UPDATE tmp.dp
|
|
|
|
JOIN tmp.dp_aux USING(ticketFk)
|
|
|
|
SET greuge = IFNULL(Importe,0);
|
|
|
|
|
2024-02-05 08:02:31 +00:00
|
|
|
INSERT INTO vn.greuge (clientFk,description,amount,shipped,greugeTypeFk,ticketFk)
|
2024-03-27 16:00:27 +00:00
|
|
|
SELECT dp.clientFk,
|
|
|
|
concat('dif_porte ', dp.ticketFk),
|
|
|
|
round(IFNULL(dp.teorico,0) - IFNULL(dp.practico,0) - IFNULL(dp.greuge,0),2) as Importe,
|
|
|
|
date(dp.shipped),
|
|
|
|
1,
|
|
|
|
dp.ticketFk
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM tmp.dp
|
2024-03-27 16:00:27 +00:00
|
|
|
JOIN vn.client c ON c.id = dp.clientFk
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE ABS(IFNULL(dp.teorico,0) - IFNULL(dp.practico,0) - IFNULL(dp.greuge,0)) > 1
|
|
|
|
AND c.isRelevant;
|
2024-03-27 16:00:27 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
DROP TEMPORARY TABLE
|
|
|
|
tmp.dp,
|
|
|
|
tmp.dp_aux;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|