feat: refs #7150 #2224

Merged
robert merged 11 commits from 7150-greuge_dif_porte_add into dev 2024-04-10 07:01:14 +00:00
2 changed files with 66 additions and 50 deletions

View File

@ -1,8 +1,19 @@
DELIMITER $$ DELIMITER $$
robert marked this conversation as resolved
Review

Faltan comentarios

Faltan comentarios
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
DECLARE datSTART DATETIME DEFAULT TIMESTAMPADD(DAY,-60,util.VN_CURDATE()); -- '2019-07-01'
DECLARE datEND DATETIME DEFAULT TIMESTAMPADD(DAY,-1,util.VN_CURDATE()); /**
robert marked this conversation as resolved Outdated

usar INTERVAL

usar INTERVAL
* Calculates the greuge based on a specific date in the 'grievanceConfig' table
*/

daysSinceLastRecalculation ? el recalculo se hace diariamente, esa descripción es confusa

daysSinceLastRecalculation ? el recalculo se hace diariamente, esa descripción es confusa

Podrías decirme que nombre crees que seria correcto?

Podrías decirme que nombre crees que seria correcto?
DECLARE vDateStarted DATETIME;
DECLARE vDateEnded DATETIME DEFAULT (util.VN_CURDATE() - INTERVAL 1 DAY);
DECLARE vDaysAgoOffset INT;
robert marked this conversation as resolved Outdated

usar INTERVAL

usar INTERVAL
SELECT daysAgoOffset INTO vDaysAgoOffset
FROM vn.greugeConfig;
SET vDateStarted = util.VN_CURDATE() - INTERVAL vDaysAgoOffset DAY;
DROP TEMPORARY TABLE IF EXISTS tmp.dp; DROP TEMPORARY TABLE IF EXISTS tmp.dp;
@ -10,53 +21,53 @@ BEGIN
CREATE TEMPORARY TABLE tmp.dp CREATE TEMPORARY TABLE tmp.dp
robert marked this conversation as resolved
Review

Alias 'AS' no son necesarios

Alias 'AS' no son necesarios
(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,
robert marked this conversation as resolved Outdated

eliminar las dependencias de vn2008

eliminar las dependencias de vn2008
00000.00 as greuge, 00000.00 greuge,
t.clientFk, t.clientFk,
t.shipped t.shipped
FROM FROM vn.ticket t
vn.ticket t JOIN vn.client c ON c.id = t.clientFk
JOIN vn2008.Clientes cli ON cli.Id_cliente = t.clientFk LEFT JOIN vn.expedition e ON e.ticketFk = t.id
robert marked this conversation as resolved Outdated

Sin números

Sin números
LEFT JOIN vn.expedition e ON e.ticketFk = t.id JOIN vn.expeditionBoxVol ebv ON ebv.boxFk = e.freightItemFk
robert marked this conversation as resolved Outdated

NOT

NOT
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 JOIN vn.company cp ON cp.id = t.companyFk
WHERE WHERE t.shipped BETWEEN vDateStarted AND vDateEnded
t.shipped between datSTART AND datEND AND c.isRelevant
AND cli.`real` AND cp.code IN ('VNL', 'VNH')
AND t.companyFk IN (442 , 567) AND NOT z.isVolumetric
AND z.isVolumetric = FALSE 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
JOIN vn.zone z ON z.id = sv.zoneFk JOIN vn.zone z ON z.id = sv.zoneFk
AND sv.shipped BETWEEN datSTART AND datEND AND sv.shipped BETWEEN vDateStarted AND vDateEnded
AND z.isVolumetric != FALSE AND z.isVolumetric != FALSE
GROUP BY sv.ticketFk; GROUP BY sv.ticketFk;
robert marked this conversation as resolved
Review

SUM mayúsculas

SUM mayúsculas
DROP TEMPORARY TABLE IF EXISTS tmp.dp_aux; DROP TEMPORARY TABLE IF EXISTS tmp.dp_aux;
robert marked this conversation as resolved Outdated

vn2008 ¿?

vn2008 ¿?
CREATE TEMPORARY TABLE tmp.dp_aux CREATE TEMPORARY TABLE tmp.dp_aux
(PRIMARY KEY (ticketFk)) (PRIMARY KEY (ticketFk))
robert marked this conversation as resolved Outdated

Si estamos refactorizando no deben de aparecer números, JOIN al código de componente correspondiente

Si estamos refactorizando no deben de aparecer números, JOIN al código de componente correspondiente
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 = dp.ticketFk
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
GROUP BY dp.ticketFk; WHERE c.code = 'delivery'
GROUP BY dp.ticketFk;
UPDATE tmp.dp UPDATE tmp.dp
JOIN tmp.dp_aux USING(ticketFk) JOIN tmp.dp_aux USING(ticketFk)
SET practico = IFNULL(valor,0); SET practico = IFNULL(valor,0);
DROP TEMPORARY TABLE tmp.dp_aux; DROP TEMPORARY TABLE tmp.dp_aux;
@ -64,28 +75,29 @@ 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
GROUP BY dp.ticketFk; WHERE gt.code = 'freightDifference' -- dif_porte
GROUP BY dp.ticketFk;
robert marked this conversation as resolved Outdated

funciones en mayúsculas

funciones en mayúsculas
UPDATE tmp.dp UPDATE tmp.dp
JOIN tmp.dp_aux USING(ticketFk) JOIN tmp.dp_aux USING(ticketFk)
SET greuge = IFNULL(Importe,0); SET greuge = IFNULL(Importe,0);
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
FROM tmp.dp 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 WHERE ABS(IFNULL(dp.teorico,0) - IFNULL(dp.practico,0) - IFNULL(dp.greuge,0)) > 1
AND c.isRelevant; AND c.isRelevant;
DROP TEMPORARY TABLE DROP TEMPORARY TABLE
tmp.dp, tmp.dp,
tmp.dp_aux; tmp.dp_aux;

View File

@ -0,0 +1,4 @@
ALTER TABLE IF EXISTS `vn`.`greugeConfig`
ADD COLUMN IF NOT EXISTS `daysAgoOffset` int(11) NOT NULL;
UPDATE vn.greugeConfig SET daysAgoOffset=15;