2023-10-23 12:51:09 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`timeControl_calculate`(
|
2023-10-23 12:51:09 +00:00
|
|
|
vDatedFrom DATETIME,
|
|
|
|
vDatedTo DATETIME)
|
|
|
|
BEGIN
|
|
|
|
/*
|
|
|
|
* Agrupa por trabajador y día, el tiempo de trabajo y descanso retribuido(si tiene).
|
|
|
|
* Los registros horarios incorrectos (tmp.timeControlError) no se considerarán.
|
|
|
|
* Si un trabajador ha trabajado más de un cierto umbral de tiempo (vTimeToBreakTime)
|
|
|
|
* y no ha tenido descansos que superen un parámetro determinado(vMaxTimeToBreak),
|
|
|
|
* se le añadirá un tiempo de descanso (vBreakTime) a sus horas trabajadas.
|
|
|
|
* El tiempo de descanso solo se añade si el trabajador realmente disfrutó del descanso.
|
|
|
|
* Si disfrutó de menos tiempo de descanso, solo se añade el tiempo que disfrutó.
|
|
|
|
*
|
|
|
|
* @param vDatedFrom
|
|
|
|
* @param vDatedTo
|
|
|
|
*
|
|
|
|
* @return tmp.timeControlCalculate
|
|
|
|
* (workerFk, dated, timeWorkSeconds, timeWorkSexagesimal, timeWorkDecimal, timed)
|
|
|
|
*/
|
|
|
|
DECLARE vHourSeconds INTEGER;
|
|
|
|
DECLARE vDatedFromYesterday DATETIME;
|
2024-01-15 11:31:03 +00:00
|
|
|
DECLARE vDatedToTomorrow DATETIME;
|
2023-10-23 12:51:09 +00:00
|
|
|
DECLARE vTimeToBreakTime INT;
|
|
|
|
DECLARE vBreakTime INT;
|
|
|
|
DECLARE vMaxTimeToBreak INT;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2023-10-23 12:51:09 +00:00
|
|
|
SELECT DATE_SUB(vDatedFrom, INTERVAL 1 DAY), DATE_ADD(vDatedTo, INTERVAL 1 DAY)
|
|
|
|
INTO vDatedFromYesterday, vDatedToTomorrow;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2023-10-23 12:51:09 +00:00
|
|
|
SELECT timeToBreakTime, breakTime, maxTimeToBreak, TIME_TO_SEC('01:00:00')
|
|
|
|
INTO vTimeToBreakTime, vBreakTime, vMaxTimeToBreak, vHourSeconds
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM workerTimeControlConfig
|
2023-10-23 12:51:09 +00:00
|
|
|
LIMIT 1;
|
|
|
|
|
|
|
|
CALL timeControl_getError(vDatedFromYesterday, vDatedToTomorrow);
|
|
|
|
|
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tmp.workerTimeControl
|
|
|
|
(INDEX(userFk, timed), INDEX(timed), INDEX(direction))
|
|
|
|
ENGINE = MEMORY
|
2024-01-15 11:31:03 +00:00
|
|
|
SELECT wtc.userFk,
|
|
|
|
wtc.timed,
|
2023-10-23 12:51:09 +00:00
|
|
|
DATE(wtc.timed) dated,
|
|
|
|
wtc.direction,
|
|
|
|
TRUE isReal
|
|
|
|
FROM workerTimeControl wtc
|
2024-01-15 11:31:03 +00:00
|
|
|
JOIN tmp.`user` u ON u.userFk = wtc.userFk
|
2023-10-23 12:51:09 +00:00
|
|
|
LEFT JOIN (
|
|
|
|
SELECT wtc.userFk, MIN(wtc.timed) firstIn
|
|
|
|
FROM workerTimeControl wtc
|
|
|
|
JOIN tmp.`user` u ON u.userFk = wtc.userFk
|
|
|
|
LEFT JOIN tmp.timeControlError tce ON tce.id = wtc.id
|
|
|
|
WHERE wtc.timed BETWEEN vDatedFromYesterday AND vDatedToTomorrow
|
|
|
|
AND wtc.direction = 'in'
|
|
|
|
AND tce.id IS NULL
|
|
|
|
GROUP BY userFk
|
|
|
|
) fi ON wtc.userFk = fi.userFk
|
|
|
|
LEFT JOIN (
|
|
|
|
SELECT wtc.userFk, MAX(wtc.timed) lastOut
|
|
|
|
FROM workerTimeControl wtc
|
|
|
|
JOIN tmp.`user` u ON u.userFk = wtc.userFk
|
|
|
|
LEFT JOIN tmp.timeControlError tce ON tce.id = wtc.id
|
|
|
|
WHERE wtc.timed BETWEEN vDatedFromYesterday AND vDatedToTomorrow
|
|
|
|
AND wtc.direction = 'out'
|
|
|
|
AND tce.id IS NULL
|
|
|
|
GROUP BY userFk
|
|
|
|
) lo ON wtc.userFk = lo.userFk
|
|
|
|
LEFT JOIN tmp.timeControlError tce ON tce.id = wtc.id
|
|
|
|
WHERE wtc.timed BETWEEN fi.firstIn AND lo.lastOut
|
|
|
|
AND tce.id IS NULL
|
|
|
|
ORDER BY wtc.userFk, wtc.timed;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2023-10-23 12:51:09 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tmp.wtcToinsert
|
2024-01-15 11:31:03 +00:00
|
|
|
(INDEX(timed))
|
2023-10-23 12:51:09 +00:00
|
|
|
ENGINE = MEMORY
|
|
|
|
WITH wtc AS(
|
|
|
|
SELECT timed,
|
|
|
|
userFk,
|
|
|
|
dated,
|
|
|
|
direction,
|
2024-01-15 11:31:03 +00:00
|
|
|
LEAD(dated) OVER
|
2023-10-23 12:51:09 +00:00
|
|
|
(PARTITION BY userFk, dated ORDER BY timed) nextDay,
|
2024-01-15 11:31:03 +00:00
|
|
|
LEAD(userFk) OVER
|
2023-10-23 12:51:09 +00:00
|
|
|
(PARTITION BY userFk ORDER BY timed) nextUserFk,
|
|
|
|
ROW_NUMBER() OVER (ORDER BY userFk, timed) MOD 2 isOdd
|
|
|
|
FROM tmp.workerTimeControl
|
|
|
|
WHERE timed BETWEEN vDatedFromYesterday AND vDatedToTomorrow
|
|
|
|
ORDER BY userFk, timed
|
2024-01-15 11:31:03 +00:00
|
|
|
), wtcToinsert AS(
|
|
|
|
SELECT userFk,
|
2023-10-23 12:51:09 +00:00
|
|
|
dated,
|
2024-01-15 11:31:03 +00:00
|
|
|
IF(userFk = nextUserFk
|
|
|
|
AND nextDay IS NULL
|
|
|
|
AND isOdd
|
2023-10-23 12:51:09 +00:00
|
|
|
AND direction <> 'out', TRUE, FALSE) outNextDay,
|
2024-01-15 11:31:03 +00:00
|
|
|
IF(userFk = nextUserFk
|
|
|
|
AND nextDay IS NULL
|
|
|
|
AND NOT isOdd
|
|
|
|
AND direction <> 'out', TRUE, FALSE) outNextDayWhitBreak
|
2023-10-23 12:51:09 +00:00
|
|
|
FROM wtc
|
|
|
|
HAVING outNextDay OR outNextDayWhitBreak
|
2024-01-15 11:31:03 +00:00
|
|
|
)SELECT userFk, util.dayEnd(dated) timed, 'out' direction
|
2023-10-23 12:51:09 +00:00
|
|
|
FROM wtcToinsert
|
|
|
|
WHERE outNextDay
|
2024-01-15 11:31:03 +00:00
|
|
|
UNION ALL
|
2023-10-23 12:51:09 +00:00
|
|
|
SELECT userFk, dated + INTERVAL 1 DAY, 'in'
|
|
|
|
FROM wtcToinsert
|
|
|
|
WHERE outNextDay
|
2024-01-15 11:31:03 +00:00
|
|
|
UNION ALL
|
|
|
|
SELECT userFk, util.dayEnd(dated) - INTERVAL 1 SECOND, 'middle'
|
2023-10-23 12:51:09 +00:00
|
|
|
FROM wtcToinsert
|
|
|
|
WHERE outNextDayWhitBreak
|
|
|
|
UNION ALL
|
|
|
|
SELECT userFk, util.dayEnd(dated), 'out'
|
|
|
|
FROM wtcToinsert
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE outNextDayWhitBreak
|
|
|
|
UNION ALL
|
2023-10-23 12:51:09 +00:00
|
|
|
SELECT userFk, dated + INTERVAL 1 DAY, 'in'
|
|
|
|
FROM wtcToinsert
|
|
|
|
WHERE outNextDayWhitBreak
|
2024-01-15 11:31:03 +00:00
|
|
|
UNION ALL
|
2023-10-23 12:51:09 +00:00
|
|
|
SELECT userFk, dated + INTERVAL 1 DAY + INTERVAL 1 SECOND, 'middle'
|
|
|
|
FROM wtcToinsert
|
|
|
|
WHERE outNextDayWhitBreak;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2023-10-23 12:51:09 +00:00
|
|
|
INSERT INTO tmp.workerTimeControl (userFk, timed, dated, direction, isReal)
|
|
|
|
SELECT userFk, timed, DATE(timed), direction, FALSE
|
|
|
|
FROM tmp.wtcToinsert;
|
|
|
|
|
|
|
|
SET @accumulatedForBreakTime = 0;
|
2024-01-15 11:31:03 +00:00
|
|
|
SET @oldrealDay = NULL;
|
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tmp.timeControlCalculate
|
2023-10-23 12:51:09 +00:00
|
|
|
WITH workerTimed AS (
|
|
|
|
SELECT
|
|
|
|
userFk,
|
|
|
|
dated,
|
|
|
|
timed,
|
|
|
|
(direction ='in' AND isReal) breakPoint,
|
|
|
|
SUM(CASE WHEN (direction ='in' AND isReal) THEN TRUE ELSE FALSE END)
|
|
|
|
OVER (ORDER BY userFk, timed) AS realDay,
|
2024-01-15 11:31:03 +00:00
|
|
|
TIMESTAMPDIFF(SECOND, LAG(timed)
|
2023-10-23 12:51:09 +00:00
|
|
|
OVER (PARTITION BY userFk, dated ORDER BY timed), timed) gapTime,
|
2024-01-15 11:31:03 +00:00
|
|
|
ROW_NUMBER()
|
2023-10-23 12:51:09 +00:00
|
|
|
OVER (PARTITION BY userFk, dated ORDER BY timed) MOD 2 isOdd
|
|
|
|
FROM tmp.workerTimeControl
|
|
|
|
WHERE timed BETWEEN vDatedFromYesterday AND vDatedToTomorrow
|
|
|
|
), accumulated AS (
|
2024-01-15 11:31:03 +00:00
|
|
|
SELECT SUM(IF(isOdd, 0, gapTime))
|
2023-10-23 12:51:09 +00:00
|
|
|
OVER (PARTITION BY userFk,dated ORDER BY userFk,timed) accumulatedWorkTime,
|
2024-01-15 11:31:03 +00:00
|
|
|
SUM(IF(NOT isOdd OR breakPoint OR gapTime >= vMaxTimeToBreak OR gapTime IS NULL, 0, gapTime))
|
|
|
|
OVER (PARTITION BY realDay ORDER BY realDay,timed) accumulatedBreakTime,
|
|
|
|
IF(realDay <> @oldrealDay OR (isOdd AND gapTime >= vMaxTimeToBreak),
|
2023-10-23 12:51:09 +00:00
|
|
|
@accumulatedForBreakTime := 0,
|
2024-01-15 11:31:03 +00:00
|
|
|
@accumulatedForBreakTime := @accumulatedForBreakTime +
|
2023-10-23 12:51:09 +00:00
|
|
|
IF(isOdd, 0, gapTime )) accumulatedForBreakTime,
|
|
|
|
@oldrealDay := realDay,
|
|
|
|
userFk,
|
|
|
|
dated,
|
|
|
|
realDay
|
|
|
|
FROM workerTimed
|
2024-01-15 11:31:03 +00:00
|
|
|
), totalWorked AS (
|
2023-10-23 12:51:09 +00:00
|
|
|
SELECT userFk,
|
|
|
|
dated,
|
2024-01-15 11:31:03 +00:00
|
|
|
MAX(accumulatedWorkTime) +
|
|
|
|
IF(MAX(accumulatedForBreakTime) + LEAST(vBreakTime, MAX(accumulatedBreakTime)) >= vTimeToBreakTime,
|
|
|
|
LEAST(vBreakTime, MAX(accumulatedBreakTime)),
|
2023-10-23 12:51:09 +00:00
|
|
|
0) timeWorkSeconds
|
|
|
|
FROM accumulated
|
|
|
|
GROUP BY userFk, dated
|
|
|
|
)SELECT tw.userFk,
|
|
|
|
tw.dated,
|
|
|
|
timeWorkSeconds,
|
|
|
|
SEC_TO_TIME(timeWorkSeconds) timeWorkSexagesimal,
|
|
|
|
timeWorkSeconds / vHourSeconds timeWorkDecimal,
|
|
|
|
sub.tableTimed
|
|
|
|
FROM totalWorked tw
|
|
|
|
JOIN (
|
|
|
|
SELECT userFk,
|
|
|
|
dated,
|
2024-01-15 11:31:03 +00:00
|
|
|
GROUP_CONCAT(DATE_FORMAT(timed, "%H:%i") ORDER BY timed ASC
|
2023-10-23 12:51:09 +00:00
|
|
|
SEPARATOR ' - ')tableTimed
|
|
|
|
FROM tmp.workerTimeControl
|
|
|
|
WHERE timed BETWEEN vDatedFromYesterday AND vDatedToTomorrow
|
|
|
|
AND isReal
|
|
|
|
GROUP BY userFk, dated
|
|
|
|
)sub ON sub.dated = tw.dated
|
|
|
|
AND sub.userFk = tw.userFk
|
|
|
|
WHERE tw.dated BETWEEN vDatedFrom AND vDatedTo;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2023-10-23 12:51:09 +00:00
|
|
|
DROP TEMPORARY TABLE tmp.timeControlError;
|
|
|
|
DROP TEMPORARY TABLE tmp.wtcToinsert;
|
|
|
|
DROP TEMPORARY TABLE tmp.workerTimeControl;
|
|
|
|
END$$
|
2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER ;
|