From 9667d31441fe8b1beff0ad33d49f8092b5dcd030 Mon Sep 17 00:00:00 2001 From: robert Date: Wed, 27 Mar 2024 17:00:27 +0100 Subject: [PATCH 1/5] feat: refs #7150 --- .../bi/procedures/greuge_dif_porte_add.sql | 60 ++++++++++--------- .../10971-turquoiseRuscus/00-firstScript.sql | 5 ++ 2 files changed, 37 insertions(+), 28 deletions(-) create mode 100644 db/versions/10971-turquoiseRuscus/00-firstScript.sql diff --git a/db/routines/bi/procedures/greuge_dif_porte_add.sql b/db/routines/bi/procedures/greuge_dif_porte_add.sql index 02bd9eae4..09cdfe076 100644 --- a/db/routines/bi/procedures/greuge_dif_porte_add.sql +++ b/db/routines/bi/procedures/greuge_dif_porte_add.sql @@ -1,8 +1,14 @@ DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bi`.`greuge_dif_porte_add`() BEGIN - DECLARE datSTART DATETIME DEFAULT TIMESTAMPADD(DAY,-60,util.VN_CURDATE()); -- '2019-07-01' - DECLARE datEND DATETIME DEFAULT TIMESTAMPADD(DAY,-1,util.VN_CURDATE()); + 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()); DROP TEMPORARY TABLE IF EXISTS tmp.dp; @@ -14,19 +20,17 @@ BEGIN SUM((t.zonePrice - t.zoneBonus) * ebv.ratio) AS teorico, 00000.00 as practico, 00000.00 as greuge, - t.clientFk, - t.shipped - FROM - vn.ticket t + t.clientFk, + t.shipped + FROM vn.ticket t JOIN vn2008.Clientes cli ON cli.Id_cliente = 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 - WHERE - t.shipped between datSTART AND datEND - AND cli.`real` - AND t.companyFk IN (442 , 567) - AND z.isVolumetric = FALSE + WHERE t.shipped between vDateStarted AND vDateEnded + AND cli.`real` + AND t.companyFk IN (442 , 567) + AND z.isVolumetric = FALSE GROUP BY t.id; -- Agencias que cobran por volumen @@ -35,11 +39,11 @@ BEGIN SUM(IFNULL(sv.freight,0)) AS teorico, 00000.00 as practico, 00000.00 as greuge, - sv.clientFk, - sv.shipped + sv.clientFk, + sv.shipped FROM vn.saleVolume sv - JOIN vn.zone z ON z.id = sv.zoneFk - AND sv.shipped BETWEEN datSTART AND datEND + JOIN vn.zone z ON z.id = sv.zoneFk + AND sv.shipped BETWEEN vDateStarted AND vDateEnded AND z.isVolumetric != FALSE GROUP BY sv.ticketFk; @@ -49,11 +53,11 @@ BEGIN (PRIMARY KEY (ticketFk)) ENGINE = MEMORY SELECT dp.ticketFk, sum(Cantidad * Valor) as valor - 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; + 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; UPDATE tmp.dp JOIN tmp.dp_aux USING(ticketFk) @@ -75,17 +79,17 @@ BEGIN SET greuge = IFNULL(Importe,0); INSERT INTO vn.greuge (clientFk,description,amount,shipped,greugeTypeFk,ticketFk) - 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 + 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 FROM tmp.dp - JOIN vn.client c ON c.id = dp.clientFk + 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; - + DROP TEMPORARY TABLE tmp.dp, tmp.dp_aux; diff --git a/db/versions/10971-turquoiseRuscus/00-firstScript.sql b/db/versions/10971-turquoiseRuscus/00-firstScript.sql new file mode 100644 index 000000000..39e754563 --- /dev/null +++ b/db/versions/10971-turquoiseRuscus/00-firstScript.sql @@ -0,0 +1,5 @@ +ALTER TABLE IF EXISTS `vn`.`greugeConfig` + ADD COLUMN IF NOT EXISTS `daysSinceLastRecalculation` int(11) NOT NULL; + +INSERT INTO vn.greugeConfig (daysSinceLastRecalculation) + VALUES (15); \ No newline at end of file -- 2.40.1 From ed1bc063014b365b674c434e1ee52f54bb191147 Mon Sep 17 00:00:00 2001 From: robert Date: Wed, 27 Mar 2024 17:30:52 +0100 Subject: [PATCH 2/5] feat: refs #7150 --- db/versions/10971-turquoiseRuscus/00-firstScript.sql | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/db/versions/10971-turquoiseRuscus/00-firstScript.sql b/db/versions/10971-turquoiseRuscus/00-firstScript.sql index 39e754563..702f2d2aa 100644 --- a/db/versions/10971-turquoiseRuscus/00-firstScript.sql +++ b/db/versions/10971-turquoiseRuscus/00-firstScript.sql @@ -1,5 +1,4 @@ ALTER TABLE IF EXISTS `vn`.`greugeConfig` ADD COLUMN IF NOT EXISTS `daysSinceLastRecalculation` int(11) NOT NULL; -INSERT INTO vn.greugeConfig (daysSinceLastRecalculation) - VALUES (15); \ No newline at end of file +UPDATE vn.greugeConfig SET daysSinceLastRecalculation=15; \ No newline at end of file -- 2.40.1 From 5517c29c1cbf2455fa3524e6bd5be8ab508f633d Mon Sep 17 00:00:00 2001 From: robert Date: Mon, 8 Apr 2024 08:37:37 +0200 Subject: [PATCH 3/5] fet: refs #7150 greuge_dif_porte_add --- .../bi/procedures/greuge_dif_porte_add.sql | 50 +++++++++++-------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/db/routines/bi/procedures/greuge_dif_porte_add.sql b/db/routines/bi/procedures/greuge_dif_porte_add.sql index 09cdfe076..5f82d72bc 100644 --- a/db/routines/bi/procedures/greuge_dif_porte_add.sql +++ b/db/routines/bi/procedures/greuge_dif_porte_add.sql @@ -1,14 +1,19 @@ DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bi`.`greuge_dif_porte_add`() BEGIN + +/** + * Calculates the greuge based on a specific date in the 'grievanceConfig' table + */ + 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; SELECT daysSinceLastRecalculation INTO vDaysSinceLastRecalculation 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; @@ -17,28 +22,29 @@ BEGIN (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, + SUM((t.zonePrice - t.zoneBonus) * ebv.ratio) teorico, + 00000.00 practico, + 00000.00 greuge, t.clientFk, t.shipped 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 JOIN vn.expeditionBoxVol ebv ON ebv.boxFk = e.freightItemFk JOIN vn.zone z ON t.zoneFk = z.id - WHERE t.shipped between vDateStarted AND vDateEnded - AND cli.`real` - AND t.companyFk IN (442 , 567) - AND z.isVolumetric = FALSE + JOIN vn.companyFk cp ON cp.id = t.companyFk + 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, - SUM(IFNULL(sv.freight,0)) AS teorico, - 00000.00 as practico, - 00000.00 as greuge, + SUM(IFNULL(sv.freight,0)) teorico, + 00000.00 practico, + 00000.00 greuge, sv.clientFk, sv.shipped FROM vn.saleVolume sv @@ -52,11 +58,12 @@ BEGIN CREATE TEMPORARY TABLE tmp.dp_aux (PRIMARY KEY (ticketFk)) ENGINE = MEMORY - SELECT dp.ticketFk, sum(Cantidad * Valor) as valor + SELECT dp.ticketFk, SUM(s.quantity * sc.value) valor 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 + JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + WHERE c.code = 'delivery' GROUP BY dp.ticketFk; UPDATE tmp.dp @@ -68,10 +75,11 @@ BEGIN CREATE TEMPORARY TABLE tmp.dp_aux (PRIMARY KEY (ticketFk)) ENGINE = MEMORY - SELECT dp.ticketFk, sum(g.amount) Importe + SELECT dp.ticketFk, SUM(g.amount) Importe FROM tmp.dp 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; UPDATE tmp.dp @@ -80,8 +88,8 @@ BEGIN INSERT INTO vn.greuge (clientFk,description,amount,shipped,greugeTypeFk,ticketFk) SELECT dp.clientFk, - concat('dif_porte ', dp.ticketFk), - round(IFNULL(dp.teorico,0) - IFNULL(dp.practico,0) - IFNULL(dp.greuge,0),2) as Importe, + CONCAT('dif_porte ', dp.ticketFk), + ROUND(IFNULL(dp.teorico,0) - IFNULL(dp.practico,0) - IFNULL(dp.greuge,0),2) Importe, date(dp.shipped), 1, dp.ticketFk -- 2.40.1 From 657150a9a7e928132ae724b9cf09323007a24b15 Mon Sep 17 00:00:00 2001 From: robert Date: Mon, 8 Apr 2024 08:57:56 +0200 Subject: [PATCH 4/5] feat: refs #7150 greuge_dif_porte_add --- db/routines/bi/procedures/greuge_dif_porte_add.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db/routines/bi/procedures/greuge_dif_porte_add.sql b/db/routines/bi/procedures/greuge_dif_porte_add.sql index 5f82d72bc..e54678037 100644 --- a/db/routines/bi/procedures/greuge_dif_porte_add.sql +++ b/db/routines/bi/procedures/greuge_dif_porte_add.sql @@ -32,7 +32,7 @@ BEGIN 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 - JOIN vn.companyFk cp ON cp.id = t.companyFk + JOIN vn.company cp ON cp.id = t.companyFk WHERE t.shipped BETWEEN vDateStarted AND vDateEnded AND c.isRelevant AND cp.code IN ('VNL', 'VNH') @@ -60,7 +60,7 @@ BEGIN ENGINE = MEMORY SELECT dp.ticketFk, SUM(s.quantity * sc.value) valor FROM tmp.dp - JOIN vn.sale s ON s.ticketFk = t.id + JOIN vn.sale s ON s.ticketFk = dp.ticketFk JOIN vn.saleComponent sc ON sc.saleFk = s.id JOIN vn.component c ON c.id = sc.componentFk WHERE c.code = 'delivery' -- 2.40.1 From c30c0de92c9e22e588c41d5b9497708aa11db5db Mon Sep 17 00:00:00 2001 From: robert Date: Wed, 10 Apr 2024 08:09:54 +0200 Subject: [PATCH 5/5] feat: refs #7150 --- .../bi/procedures/greuge_dif_porte_add.sql | 82 +++++++++---------- .../10971-turquoiseRuscus/00-firstScript.sql | 4 +- 2 files changed, 43 insertions(+), 43 deletions(-) diff --git a/db/routines/bi/procedures/greuge_dif_porte_add.sql b/db/routines/bi/procedures/greuge_dif_porte_add.sql index e54678037..330ff92b8 100644 --- a/db/routines/bi/procedures/greuge_dif_porte_add.sql +++ b/db/routines/bi/procedures/greuge_dif_porte_add.sql @@ -8,12 +8,12 @@ BEGIN DECLARE vDateStarted DATETIME; DECLARE vDateEnded DATETIME DEFAULT (util.VN_CURDATE() - INTERVAL 1 DAY); - DECLARE vDaysSinceLastRecalculation INT; + DECLARE vDaysAgoOffset INT; - SELECT daysSinceLastRecalculation INTO vDaysSinceLastRecalculation + SELECT daysAgoOffset INTO vDaysAgoOffset FROM vn.greugeConfig; - SET vDateStarted = util.VN_CURDATE() - INTERVAL vDaysSinceLastRecalculation DAY; + SET vDateStarted = util.VN_CURDATE() - INTERVAL vDaysAgoOffset DAY; DROP TEMPORARY TABLE IF EXISTS tmp.dp; @@ -21,37 +21,37 @@ BEGIN CREATE TEMPORARY TABLE tmp.dp (PRIMARY KEY (ticketFk)) ENGINE = MEMORY - SELECT t.id ticketFk, - SUM((t.zonePrice - t.zoneBonus) * ebv.ratio) teorico, - 00000.00 practico, - 00000.00 greuge, - t.clientFk, - t.shipped - FROM vn.ticket t - 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 - JOIN vn.company cp ON cp.id = t.companyFk - WHERE t.shipped BETWEEN vDateStarted AND vDateEnded - AND c.isRelevant - AND cp.code IN ('VNL', 'VNH') - AND NOT z.isVolumetric - GROUP BY t.id; + SELECT t.id ticketFk, + SUM((t.zonePrice - t.zoneBonus) * ebv.ratio) teorico, + 00000.00 practico, + 00000.00 greuge, + t.clientFk, + t.shipped + FROM vn.ticket t + 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 + JOIN vn.company cp ON cp.id = t.companyFk + 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, - SUM(IFNULL(sv.freight,0)) teorico, - 00000.00 practico, - 00000.00 greuge, - sv.clientFk, - sv.shipped - FROM vn.saleVolume sv - JOIN vn.zone z ON z.id = sv.zoneFk - AND sv.shipped BETWEEN vDateStarted AND vDateEnded - AND z.isVolumetric != FALSE - GROUP BY sv.ticketFk; + SUM(IFNULL(sv.freight,0)) teorico, + 00000.00 practico, + 00000.00 greuge, + sv.clientFk, + sv.shipped + FROM vn.saleVolume sv + 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; @@ -67,7 +67,7 @@ BEGIN GROUP BY dp.ticketFk; UPDATE tmp.dp - JOIN tmp.dp_aux USING(ticketFk) + JOIN tmp.dp_aux USING(ticketFk) SET practico = IFNULL(valor,0); DROP TEMPORARY TABLE tmp.dp_aux; @@ -77,22 +77,22 @@ BEGIN ENGINE = MEMORY SELECT dp.ticketFk, SUM(g.amount) Importe FROM tmp.dp - JOIN vn.greuge g ON g.ticketFk = dp.ticketFk - JOIN vn.greugeType gt ON gt.id = g.greugeTypeFk - WHERE gt.code = 'freightDifference' -- dif_porte - GROUP BY dp.ticketFk; + JOIN vn.greuge g ON g.ticketFk = dp.ticketFk + 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) + JOIN tmp.dp_aux USING(ticketFk) SET greuge = IFNULL(Importe,0); INSERT INTO vn.greuge (clientFk,description,amount,shipped,greugeTypeFk,ticketFk) SELECT dp.clientFk, - CONCAT('dif_porte ', dp.ticketFk), - ROUND(IFNULL(dp.teorico,0) - IFNULL(dp.practico,0) - IFNULL(dp.greuge,0),2) Importe, - date(dp.shipped), - 1, - dp.ticketFk + CONCAT('dif_porte ', dp.ticketFk), + ROUND(IFNULL(dp.teorico,0) - IFNULL(dp.practico,0) - IFNULL(dp.greuge,0),2) Importe, + date(dp.shipped), + 1, + dp.ticketFk FROM tmp.dp JOIN vn.client c ON c.id = dp.clientFk WHERE ABS(IFNULL(dp.teorico,0) - IFNULL(dp.practico,0) - IFNULL(dp.greuge,0)) > 1 diff --git a/db/versions/10971-turquoiseRuscus/00-firstScript.sql b/db/versions/10971-turquoiseRuscus/00-firstScript.sql index 702f2d2aa..82a0117c3 100644 --- a/db/versions/10971-turquoiseRuscus/00-firstScript.sql +++ b/db/versions/10971-turquoiseRuscus/00-firstScript.sql @@ -1,4 +1,4 @@ ALTER TABLE IF EXISTS `vn`.`greugeConfig` - ADD COLUMN IF NOT EXISTS `daysSinceLastRecalculation` int(11) NOT NULL; + ADD COLUMN IF NOT EXISTS `daysAgoOffset` int(11) NOT NULL; -UPDATE vn.greugeConfig SET daysSinceLastRecalculation=15; \ No newline at end of file +UPDATE vn.greugeConfig SET daysAgoOffset=15; \ No newline at end of file -- 2.40.1