fet: refs #7150 greuge_dif_porte_add
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Robert Ferrús 2024-04-08 08:37:37 +02:00
parent ed1bc06301
commit 5517c29c1c
1 changed files with 29 additions and 21 deletions

View File

@ -1,14 +1,19 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bi`.`greuge_dif_porte_add`() CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bi`.`greuge_dif_porte_add`()
BEGIN BEGIN
/**
* Calculates the greuge based on a specific date in the 'grievanceConfig' table
*/
DECLARE vDateStarted DATETIME; DECLARE vDateStarted DATETIME;
DECLARE vDateEnded DATETIME DEFAULT TIMESTAMPADD(DAY,-1,util.VN_CURDATE()); DECLARE vDateEnded DATETIME DEFAULT (util.VN_CURDATE() - INTERVAL 1 DAY);
DECLARE vDaysSinceLastRecalculation INT; DECLARE vDaysSinceLastRecalculation INT;
SELECT daysSinceLastRecalculation INTO vDaysSinceLastRecalculation SELECT daysSinceLastRecalculation INTO vDaysSinceLastRecalculation
FROM vn.greugeConfig; FROM vn.greugeConfig;
SET vDateStarted = TIMESTAMPADD(DAY, -vDaysSinceLastRecalculation, util.VN_CURDATE()); SET vDateStarted = util.VN_CURDATE() - INTERVAL vDaysSinceLastRecalculation DAY;
DROP TEMPORARY TABLE IF EXISTS tmp.dp; DROP TEMPORARY TABLE IF EXISTS tmp.dp;
@ -17,28 +22,29 @@ BEGIN
(PRIMARY KEY (ticketFk)) (PRIMARY KEY (ticketFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT t.id ticketFk, SELECT t.id ticketFk,
SUM((t.zonePrice - t.zoneBonus) * ebv.ratio) AS teorico, SUM((t.zonePrice - t.zoneBonus) * ebv.ratio) teorico,
00000.00 as practico, 00000.00 practico,
00000.00 as greuge, 00000.00 greuge,
t.clientFk, t.clientFk,
t.shipped t.shipped
FROM vn.ticket t FROM vn.ticket t
JOIN vn2008.Clientes cli ON cli.Id_cliente = t.clientFk JOIN vn.client c ON c.id = t.clientFk
LEFT JOIN vn.expedition e ON e.ticketFk = t.id LEFT JOIN vn.expedition e ON e.ticketFk = t.id
JOIN vn.expeditionBoxVol ebv ON ebv.boxFk = e.freightItemFk JOIN vn.expeditionBoxVol ebv ON ebv.boxFk = e.freightItemFk
JOIN vn.zone z ON t.zoneFk = z.id JOIN vn.zone z ON t.zoneFk = z.id
WHERE t.shipped between vDateStarted AND vDateEnded JOIN vn.companyFk cp ON cp.id = t.companyFk
AND cli.`real` WHERE t.shipped BETWEEN vDateStarted AND vDateEnded
AND t.companyFk IN (442 , 567) AND c.isRelevant
AND z.isVolumetric = FALSE AND cp.code IN ('VNL', 'VNH')
AND NOT z.isVolumetric
GROUP BY t.id; GROUP BY t.id;
-- Agencias que cobran por volumen -- Agencias que cobran por volumen
INSERT INTO tmp.dp INSERT INTO tmp.dp
SELECT sv.ticketFk, SELECT sv.ticketFk,
SUM(IFNULL(sv.freight,0)) AS teorico, SUM(IFNULL(sv.freight,0)) teorico,
00000.00 as practico, 00000.00 practico,
00000.00 as greuge, 00000.00 greuge,
sv.clientFk, sv.clientFk,
sv.shipped sv.shipped
FROM vn.saleVolume sv FROM vn.saleVolume sv
@ -52,11 +58,12 @@ BEGIN
CREATE TEMPORARY TABLE tmp.dp_aux CREATE TEMPORARY TABLE tmp.dp_aux
(PRIMARY KEY (ticketFk)) (PRIMARY KEY (ticketFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT dp.ticketFk, sum(Cantidad * Valor) as valor SELECT dp.ticketFk, SUM(s.quantity * sc.value) valor
FROM tmp.dp FROM tmp.dp
JOIN vn2008.Movimientos m ON m.Id_Ticket = dp.ticketFk JOIN vn.sale s ON s.ticketFk = t.id
JOIN vn2008.Movimientos_componentes mc using(Id_Movimiento) JOIN vn.saleComponent sc ON sc.saleFk = s.id
WHERE mc.Id_Componente = 15 JOIN vn.component c ON c.id = sc.componentFk
WHERE c.code = 'delivery'
GROUP BY dp.ticketFk; GROUP BY dp.ticketFk;
UPDATE tmp.dp UPDATE tmp.dp
@ -68,10 +75,11 @@ BEGIN
CREATE TEMPORARY TABLE tmp.dp_aux CREATE TEMPORARY TABLE tmp.dp_aux
(PRIMARY KEY (ticketFk)) (PRIMARY KEY (ticketFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT dp.ticketFk, sum(g.amount) Importe SELECT dp.ticketFk, SUM(g.amount) Importe
FROM tmp.dp FROM tmp.dp
JOIN vn.greuge g ON g.ticketFk = dp.ticketFk JOIN vn.greuge g ON g.ticketFk = dp.ticketFk
WHERE g.greugeTypeFk = 1 -- dif_porte JOIN vn.greugeType gt ON gt.id = g.greugeTypeFk
WHERE gt.code = 'freightDifference' -- dif_porte
GROUP BY dp.ticketFk; GROUP BY dp.ticketFk;
UPDATE tmp.dp UPDATE tmp.dp
@ -80,8 +88,8 @@ BEGIN
INSERT INTO vn.greuge (clientFk,description,amount,shipped,greugeTypeFk,ticketFk) INSERT INTO vn.greuge (clientFk,description,amount,shipped,greugeTypeFk,ticketFk)
SELECT dp.clientFk, SELECT dp.clientFk,
concat('dif_porte ', dp.ticketFk), CONCAT('dif_porte ', dp.ticketFk),
round(IFNULL(dp.teorico,0) - IFNULL(dp.practico,0) - IFNULL(dp.greuge,0),2) as Importe, ROUND(IFNULL(dp.teorico,0) - IFNULL(dp.practico,0) - IFNULL(dp.greuge,0),2) Importe,
date(dp.shipped), date(dp.shipped),
1, 1,
dp.ticketFk dp.ticketFk