7226-testToMaster_2416 #2320
|
@ -8,12 +8,12 @@ BEGIN
|
||||||
|
|
||||||
DECLARE vDateStarted DATETIME;
|
DECLARE vDateStarted DATETIME;
|
||||||
DECLARE vDateEnded DATETIME DEFAULT (util.VN_CURDATE() - INTERVAL 1 DAY);
|
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;
|
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;
|
DROP TEMPORARY TABLE IF EXISTS tmp.dp;
|
||||||
|
|
||||||
|
@ -21,37 +21,37 @@ BEGIN
|
||||||
CREATE TEMPORARY TABLE tmp.dp
|
CREATE TEMPORARY TABLE tmp.dp
|
||||||
(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) teorico,
|
SUM((t.zonePrice - t.zoneBonus) * ebv.ratio) teorico,
|
||||||
00000.00 practico,
|
00000.00 practico,
|
||||||
00000.00 greuge,
|
00000.00 greuge,
|
||||||
t.clientFk,
|
t.clientFk,
|
||||||
t.shipped
|
t.shipped
|
||||||
FROM vn.ticket t
|
FROM vn.ticket t
|
||||||
JOIN vn.client c ON c.id = 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
|
||||||
JOIN vn.company cp ON cp.id = t.companyFk
|
JOIN vn.company cp ON cp.id = t.companyFk
|
||||||
WHERE t.shipped BETWEEN vDateStarted AND vDateEnded
|
WHERE t.shipped BETWEEN vDateStarted AND vDateEnded
|
||||||
AND c.isRelevant
|
AND c.isRelevant
|
||||||
AND cp.code IN ('VNL', 'VNH')
|
AND cp.code IN ('VNL', 'VNH')
|
||||||
AND NOT z.isVolumetric
|
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)) teorico,
|
SUM(IFNULL(sv.freight,0)) teorico,
|
||||||
00000.00 practico,
|
00000.00 practico,
|
||||||
00000.00 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 vDateStarted AND vDateEnded
|
AND sv.shipped BETWEEN vDateStarted AND vDateEnded
|
||||||
AND z.isVolumetric != FALSE
|
AND z.isVolumetric != FALSE
|
||||||
GROUP BY sv.ticketFk;
|
GROUP BY sv.ticketFk;
|
||||||
|
|
||||||
DROP TEMPORARY TABLE IF EXISTS tmp.dp_aux;
|
DROP TEMPORARY TABLE IF EXISTS tmp.dp_aux;
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ BEGIN
|
||||||
GROUP BY dp.ticketFk;
|
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;
|
||||||
|
@ -77,22 +77,22 @@ BEGIN
|
||||||
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
|
||||||
JOIN vn.greugeType gt ON gt.id = g.greugeTypeFk
|
JOIN vn.greugeType gt ON gt.id = g.greugeTypeFk
|
||||||
WHERE gt.code = 'freightDifference' -- dif_porte
|
WHERE gt.code = 'freightDifference' -- dif_porte
|
||||||
GROUP BY dp.ticketFk;
|
GROUP BY dp.ticketFk;
|
||||||
|
|
||||||
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) 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
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
ALTER TABLE IF EXISTS `vn`.`greugeConfig`
|
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;
|
UPDATE vn.greugeConfig SET daysAgoOffset=15;
|
Loading…
Reference in New Issue