Created
November 28, 2013 16:12
-
-
Save gepser/7694301 to your computer and use it in GitHub Desktop.
Calcula el tiempo laboral en segundos de los días de los extremos en un conjunto de días. La función se complementa con la de segundos laborales (https://gist.github.com/gepser/7694205) y es una sub función de la función Tiempo_Laboral ().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
La función obtiene el tiempo laboral en segundos de un empleado en los días de los extremos. | |
Esta función trabaja sobre una tabla de horarios por empleado donde cada empleado puede tener muchos horarios por día. | |
Se asume que no hay traslapes de horarios. | |
Un ejemplo de la tabla podría ser así: | |
dia|hora_inicio|hora_fin|empleado | |
2 |08:00 |13:00 |15 | |
2 |14:00 |18:00 |15 | |
3 |08:00 |13:00 |15 | |
3 |14:00 |18:00 |15 | |
4 |07:00 |13:00 |15 --Miércoles sólo medio día | |
5 |08:00 |13:00 |15 | |
5 |14:00 |18:00 |15 | |
6 |08:00 |13:00 |15 | |
6 |14:00 |18:00 |15 | |
*/ | |
set ANSI_NULLS ON | |
set QUOTED_IDENTIFIER ON | |
GO | |
ALTER FUNCTION [dbo].[Segundos_Laborales_Extremos] ( @empleado int, @fecha_inicial DATETIME, @fecha_final DATETIME) | |
RETURNS INT | |
AS | |
BEGIN | |
DECLARE @hora_inicio DATETIME, @hora_fin DATETIME | |
DECLARE @segundos_laborales INT, @mismo_dia INT | |
DECLARE @hora_inicio_parametro VARCHAR(10), @hora_fin_parametro VARCHAR(10) | |
SET @hora_inicio_parametro = CAST(DATEPART(hour,@fecha_inicial) AS VARCHAR(10)) + ':' +CAST(DATEPART(minute,@fecha_inicial) AS VARCHAR(10)) + ':' + CAST(DATEPART(second,@fecha_inicial) AS VARCHAR(10)) | |
SET @hora_fin_parametro = CAST(DATEPART(hour,@fecha_final) AS VARCHAR(10)) + ':' + CAST(DATEPART(minute,@fecha_final) AS VARCHAR(10)) + ':' + CAST(DATEPART(second,@fecha_final) AS VARCHAR(10)) | |
SET @segundos_laborales = 0 | |
SET @mismo_dia = DATEDIFF(day,@fecha_inicial,@fecha_final) --0=el mismo dia|>0=dias diferentes|<0=error | |
IF @mismo_dia = 0 | |
BEGIN | |
DECLARE num_horario CURSOR FOR | |
select hora_inicio, hora_fin | |
from tabla_horario | |
where empleado = @empleado | |
and dia = DATEPART(dw,@fecha_inicial) | |
order by hora_inicio | |
OPEN num_horario | |
FETCH NEXT FROM num_horario INTO @hora_inicio, @hora_fin | |
WHILE @@FETCH_STATUS = 0 | |
BEGIN | |
IF DATEDIFF(s,@hora_fin_parametro,@hora_inicio) >= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales | |
END | |
ELSE IF DATEDIFF(s,@hora_inicio_parametro,@hora_fin) <= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales | |
END | |
ELSE IF DATEDIFF(s,@hora_inicio_parametro,@hora_inicio) >= 0 AND DATEDIFF(s,@hora_fin_parametro,@hora_inicio) <= 0 AND DATEDIFF(s,@hora_fin_parametro,@hora_fin) >= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales + DATEDIFF(s,@hora_inicio,@hora_fin_parametro) | |
END | |
ELSE IF DATEDIFF(s,@hora_inicio_parametro,@hora_inicio) >= 0 AND DATEDIFF(s,@hora_fin_parametro,@hora_fin) <= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales + DATEDIFF(s,@hora_inicio,@hora_fin) | |
END | |
ELSE IF DATEDIFF(s,@hora_inicio_parametro,@hora_inicio) <= 0 AND DATEDIFF(s,@hora_fin_parametro,@hora_fin) >= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales + DATEDIFF(s,@hora_inicio_parametro,@hora_fin_parametro) | |
END | |
ELSE IF DATEDIFF(s,@hora_inicio_parametro,@hora_inicio) <= 0 AND DATEDIFF(s,@hora_inicio_parametro,@hora_fin) >= 0 AND DATEDIFF(s,@hora_fin_parametro,@hora_fin) <= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales + DATEDIFF(s,@hora_inicio_parametro,@hora_fin) | |
END | |
FETCH NEXT FROM num_horario INTO @hora_inicio, @hora_fin | |
END | |
CLOSE num_horario | |
DEALLOCATE num_horario | |
END | |
/*SON DIAS DIFERENTES*/ | |
ELSE IF @mismo_dia > 0 | |
BEGIN | |
/*PRIMER DIA*/ | |
DECLARE num_horario CURSOR FOR | |
select hora_inicio, hora_fin | |
from tabla_horario | |
where empleado = @empleado | |
and dia = DATEPART(dw,@fecha_inicial) | |
order by hora_inicio | |
OPEN num_horario | |
FETCH NEXT FROM num_horario INTO @hora_inicio, @hora_fin | |
WHILE @@FETCH_STATUS = 0 | |
BEGIN | |
IF DATEDIFF(s,@hora_inicio_parametro,@hora_inicio) > 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales + DATEDIFF(s,@hora_inicio,@hora_fin) | |
END | |
ELSE IF DATEDIFF(s,@hora_inicio_parametro,@hora_inicio) <= 0 AND DATEDIFF(s,@hora_inicio_parametro,@hora_fin) >= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales + DATEDIFF(s,@hora_inicio_parametro,@hora_fin) | |
END | |
ELSE IF DATEDIFF(s,@hora_inicio_parametro,@hora_fin) < 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales | |
END | |
FETCH NEXT FROM num_horario INTO @hora_inicio, @hora_fin | |
END | |
CLOSE num_horario | |
DEALLOCATE num_horario | |
/*ULTIMO DIA*/ | |
DECLARE num_horario CURSOR FOR | |
select hora_inicio, hora_fin | |
from tabla_horario | |
where empleado = @empleado | |
and dia = DATEPART(dw,@fecha_final) | |
order by hora_inicio | |
OPEN num_horario | |
FETCH NEXT FROM num_horario INTO @hora_inicio, @hora_fin | |
WHILE @@FETCH_STATUS = 0 | |
BEGIN | |
IF DATEDIFF(s,@hora_fin_parametro,@hora_fin) <= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales + DATEDIFF(s,@hora_inicio,@hora_fin) | |
END | |
ELSE IF DATEDIFF(s,@hora_fin_parametro,@hora_fin) >= 0 AND DATEDIFF(s,@hora_fin_parametro,@hora_inicio) <= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales + DATEDIFF(s,@hora_inicio,@hora_fin_parametro) | |
END | |
ELSE IF DATEDIFF(s,@hora_fin_parametro,@hora_inicio) >= 0 | |
BEGIN | |
SET @segundos_laborales = @segundos_laborales | |
END | |
FETCH NEXT FROM num_horario INTO @hora_inicio, @hora_fin | |
END | |
CLOSE num_horario | |
DEALLOCATE num_horario | |
END | |
RETURN @segundos_laborales | |
END | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment