salix/db/routines/vn/procedures/workerCalendar_calculateYea...

60 lines
1.4 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`workerCalendar_calculateYear`(vYear INT, vWorkerFk INT)
BEGIN
/**
* Calcula los días y horas de vacaciones en función de un trabajador y año
*
* @param vYear
* @param vWorkerFk
* @return tmp.workerCalendarCalculateYear (days, hours, daysEnjoyed, hoursEnjoyed)
*/
DECLARE vDone BOOL;
DECLARE vBusinessFk INT;
DECLARE cur CURSOR FOR
SELECT b.id
FROM business b
WHERE vYear BETWEEN YEAR(b.started) AND IFNULL(YEAR(b.ended), vYear)
AND b.workerFk = vWorkerFk;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET vDone = TRUE;
DROP TEMPORARY TABLE IF EXISTS tmp.workerCalendarCalculateYear;
CREATE TEMPORARY TABLE tmp.workerCalendarCalculateYear
(days DEC(5,2),
hours DEC(5,2),
daysEnjoyed DEC(5,2),
hoursEnjoyed DEC(5,2))
ENGINE = MEMORY;
INSERT INTO tmp.workerCalendarCalculateYear VALUES(0, 0, 0, 0);
OPEN cur;
l: LOOP
SET vDone = FALSE;
FETCH cur INTO vBusinessFk;
IF vDone THEN
LEAVE l;
END IF;
CALL workerCalendar_calculateBusiness(vYear, vBusinessFk);
UPDATE tmp.workerCalendarCalculateYear w
JOIN tmp.workerCalendarCalculateBusiness wc
SET w.days = w.days + wc.days,
w.hours = w.hours + wc.hours,
w.daysEnjoyed = w.daysEnjoyed + wc.daysEnjoyed,
w.hoursEnjoyed = w.hoursEnjoyed + wc.hoursEnjoyed;
DROP TEMPORARY TABLE tmp.workerCalendarCalculateBusiness;
END LOOP;
CLOSE cur;
END$$
DELIMITER ;