DELIMITER $$ CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`travel_weeklyClone`(vSinceWeek INT, vToWeek INT) BEGIN /** * Clona los traslados plantilla para las semanas pasadas por parámetros. * * @param vSinceWeek Número de semanas en el futuro para empezar a clonar * @param vToWeek Número de semenas en el futuro hasta donde clonar */ DECLARE vCounter INT; DECLARE vCurrentTravelFk INT; DECLARE vShipped DATE; DECLARE vLanded DATE; DECLARE vWarehouseInFk INT; DECLARE vWarehouseOutFk INT; DECLARE vAgencyModeFk INT; DECLARE vRef VARCHAR(255); DECLARE vNewTravelFk INT; DECLARE vDone BOOL; DECLARE vRsTravel CURSOR FOR SELECT travelFk, @a := TIMESTAMPADD(DAY,vCounter * 7 - WEEKDAY(util.VN_CURDATE()) - 1 + weekDay, util.VN_CURDATE()), @a := TIMESTAMPADD(DAY,duration,@a), warehouseOutFk, warehouseInFk, `ref`, agencyModeFk FROM travelClonedWeekly WHERE travelFk; DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE; SET vCounter = vSinceWeek; vWeekLoop :LOOP INSERT IGNORE INTO travel (shipped, landed, warehouseOutFk, warehouseInFk, agencyModeFk, ref, cargoSupplierFk, kg) SELECT @a := TIMESTAMPADD(DAY, vCounter * 7 - WEEKDAY(util.VN_CURDATE()) - 1 + weekDay, util.VN_CURDATE()), @a := TIMESTAMPADD(DAY, duration, @a), warehouseOutFk, warehouseInFk, agencyModeFk, ref, supplierFk, kg FROM travelClonedWeekly WHERE travelFk IS NULL; OPEN vRsTravel; l: LOOP SET vDone = FALSE; FETCH vRsTravel INTO vCurrentTravelFk, vShipped, vLanded, vWarehouseOutFk, vWarehouseInFk, vRef, vAgencyModeFk; IF vDone THEN LEAVE l; END IF; BEGIN DECLARE CONTINUE HANDLER FOR SQLEXCEPTION RESIGNAL SQLSTATE '01000'; CALL travel_cloneWithEntries(vCurrentTravelFk, vShipped, vLanded, vWarehouseOutFk, vWarehouseInFk, vRef, vAgencyModeFk, vNewTravelFk); END; END LOOP; CLOSE vRsTravel; IF vCounter = vToWeek THEN LEAVE vWeekLoop; END IF; SET vCounter = vCounter + 1; END LOOP vWeekLoop; END$$ DELIMITER ;