salix/db/routines/bi/procedures/greuge_dif_porte_add.sql

106 lines
2.9 KiB
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bi`.`greuge_dif_porte_add`()
BEGIN
2024-04-08 06:37:37 +00:00
/**
* Calculates the greuge based on a specific date in the 'grievanceConfig' table
*/
2024-03-27 16:00:27 +00:00
DECLARE vDateStarted DATETIME;
2024-04-08 06:37:37 +00:00
DECLARE vDateEnded DATETIME DEFAULT (util.VN_CURDATE() - INTERVAL 1 DAY);
2024-03-27 16:00:27 +00:00
DECLARE vDaysSinceLastRecalculation INT;
SELECT daysSinceLastRecalculation INTO vDaysSinceLastRecalculation
FROM vn.greugeConfig;
2024-04-08 06:37:37 +00:00
SET vDateStarted = util.VN_CURDATE() - INTERVAL vDaysSinceLastRecalculation DAY;
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,
2024-04-08 06:37:37 +00:00
SUM((t.zonePrice - t.zoneBonus) * ebv.ratio) teorico,
00000.00 practico,
00000.00 greuge,
2024-03-27 16:00:27 +00:00
t.clientFk,
t.shipped
FROM vn.ticket t
2024-04-08 06:37:37 +00:00
JOIN vn.client c ON c.id = t.clientFk
LEFT JOIN vn.expedition e ON e.ticketFk = t.id
JOIN vn.expeditionBoxVol ebv ON ebv.boxFk = e.freightItemFk
JOIN vn.zone z ON t.zoneFk = z.id
2024-04-08 06:57:56 +00:00
JOIN vn.company cp ON cp.id = t.companyFk
2024-04-08 06:37:37 +00:00
WHERE t.shipped BETWEEN vDateStarted AND vDateEnded
AND c.isRelevant
AND cp.code IN ('VNL', 'VNH')
AND NOT z.isVolumetric
GROUP BY t.id;
-- Agencias que cobran por volumen
INSERT INTO tmp.dp
SELECT sv.ticketFk,
2024-04-08 06:37:37 +00:00
SUM(IFNULL(sv.freight,0)) teorico,
00000.00 practico,
00000.00 greuge,
2024-03-27 16:00:27 +00:00
sv.clientFk,
sv.shipped
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
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
2024-04-08 06:37:37 +00:00
SELECT dp.ticketFk, SUM(s.quantity * sc.value) valor
2024-03-27 16:00:27 +00:00
FROM tmp.dp
2024-04-08 06:57:56 +00:00
JOIN vn.sale s ON s.ticketFk = dp.ticketFk
2024-04-08 06:37:37 +00:00
JOIN vn.saleComponent sc ON sc.saleFk = s.id
JOIN vn.component c ON c.id = sc.componentFk
WHERE c.code = 'delivery'
2024-03-27 16:00:27 +00:00
GROUP BY dp.ticketFk;
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-04-08 06:37:37 +00:00
SELECT dp.ticketFk, SUM(g.amount) Importe
FROM tmp.dp
JOIN vn.greuge g ON g.ticketFk = dp.ticketFk
2024-04-08 06:37:37 +00:00
JOIN vn.greugeType gt ON gt.id = g.greugeTypeFk
WHERE gt.code = 'freightDifference' -- dif_porte
GROUP BY dp.ticketFk;
UPDATE tmp.dp
JOIN tmp.dp_aux USING(ticketFk)
SET greuge = IFNULL(Importe,0);
INSERT INTO vn.greuge (clientFk,description,amount,shipped,greugeTypeFk,ticketFk)
2024-03-27 16:00:27 +00:00
SELECT dp.clientFk,
2024-04-08 06:37:37 +00:00
CONCAT('dif_porte ', dp.ticketFk),
ROUND(IFNULL(dp.teorico,0) - IFNULL(dp.practico,0) - IFNULL(dp.greuge,0),2) Importe,
2024-03-27 16:00:27 +00:00
date(dp.shipped),
1,
dp.ticketFk
FROM tmp.dp
2024-03-27 16:00:27 +00:00
JOIN vn.client c ON c.id = dp.clientFk
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
DROP TEMPORARY TABLE
tmp.dp,
tmp.dp_aux;
END$$
DELIMITER ;