salix/db/changes/10141-zoneDoCalc/02-insertPastTickets.sql

62 lines
1.3 KiB
MySQL
Raw Normal View History

2020-02-17 11:01:11 +00:00
USE `vn`;
DROP procedure IF EXISTS `zone_doCalcInitialize`;
DELIMITER $$
USE `vn`$$
CREATE DEFINER=`root`@`%` PROCEDURE `zone_doCalcInitialize`()
proc: BEGIN
/**
* Initialize ticket
2020-02-20 14:15:50 +00:00
* si en 01-07-20 aun esta este proc, kkear
2020-02-17 11:01:11 +00:00
*/
DECLARE vDone BOOL;
DECLARE vTicketFk INT;
DECLARE vLanded DATE;
DECLARE vZoneFk INT;
DECLARE cCur CURSOR FOR
SELECT t.id, t.landed, t.zoneFk
FROM ticket t
2020-02-20 14:15:50 +00:00
WHERE (zonePrice IS NULL OR zoneBonus IS NULL)
2020-02-17 11:01:11 +00:00
AND landed >= '2019-01-01' AND shipped >= '2019-01-01'
GROUP BY landed, zoneFk;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET vDone = TRUE;
OPEN cCur;
myLoop: LOOP
SET vDone = FALSE;
FETCH cCur INTO vTicketFk, vLanded, vZoneFk;
IF vDone THEN
LEAVE myLoop;
END IF;
DROP TEMPORARY TABLE IF EXISTS tmp.zone;
CREATE TEMPORARY TABLE tmp.zone
(INDEX (id))
ENGINE = MEMORY
SELECT vZoneFk id;
CALL zone_getOptionsForLanding(vLanded, TRUE);
UPDATE ticket t
LEFT JOIN tmp.zoneOption zo ON TRUE
2020-02-20 14:15:50 +00:00
SET zonePrice = zo.price, zoneBonus = zo.bonus
2020-02-17 11:01:11 +00:00
WHERE t.zoneFk = vZoneFk AND landed = vLanded;
UPDATE ticket t
LEFT JOIN vn.zone z ON z.id = t.zoneFk
2020-02-20 14:15:50 +00:00
SET zonePrice = z.price, zoneBonus = z.bonus
2020-02-17 11:01:11 +00:00
WHERE t.zonePrice IS NULL AND z.id = vZoneFk
AND landed >= '2019-01-01' AND shipped >= '2019-01-01';
END LOOP;
CLOSE cCur;
END$$
DELIMITER ;