Description
Properties
| Name | Value |
|---|---|
| ANSI Nulls ON | True |
| Quoted Identifier ON | True |
| Encrypted | False |
| Execute As | |
| Assembly |
Parameters
2| Name | Data Type | Length | Description |
|---|---|---|---|
| @periodo | int | 4 | |
| @IDCliente | int | 4 |
SQL Script
Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
CREATE OR ALTER PROCEDURE dbo.Imperium_Carga_Docs_Empleados
@periodo int,
@IDCliente int
AS
BEGIN
SET NOCOUNT ON;
DECLARE @ERRORNUMBER SMALLINT
declare @periodoAnt int
DECLARE @PeriodoModalidad int
DECLARE @Modalidad VARCHAR(1)
/*
declare @periodo int = 201612
DECLARE @IDCliente INT = 5546348
*/
DECLARE @periodoV INT = NULL
DECLARE @periodoVAnt INT = NULL
DECLARE @periodoA INT = NULL
DECLARE @NombreCliente varchar(200)
DECLARE @periodoYear INT = NULL
DECLARE @periodoMonth INT = NULL
SET @periodoYear = convert(varchar(4),datepart(year,getdate()))
SET @periodoMonth = convert(varchar(2),datepart(month,getdate()))
SET @periodoYear = datepart(year,getdate())
SET @periodoMonth = datepart(month,getdate())
SET @periodoA = convert(varchar(4),datepart(year,getdate())) + RIGHT('00' +convert(varchar(2),datepart(month,getdate())),2)
if (@periodoMonth = 1)
begin
set @periodoV = convert(varchar(4),@periodoYear-1) + RIGHT('00' +convert(varchar(2),12),2)
set @periodoVAnt = convert(varchar(4),@periodoYear-1) + RIGHT('00' +convert(varchar(2),11),2)
end
if (@periodoMonth > 1)
begin
set @periodoV = convert(varchar(4),datepart(year,getdate())) + RIGHT('00' +convert(varchar(2),datepart(month,getdate())-1),2)
set @periodoVAnt = convert(varchar(4),datepart(year,getdate())) + RIGHT('00' +convert(varchar(2),datepart(month,getdate())-2),2)
end
set @periodoVAnt = dbo.PeriodoMenosDos()
set @periodoV = dbo.PeriodoVencido()
set @periodoA = dbo.PeriodoAdelantado()
if @Periodo = @periodoA
BEGIN
SET @Modalidad = 'A'
END
ELSE
BEGIN
SET @Modalidad = 'V'
END
if (@IDCliente = 9335603)
begin
set @IDCliente = 9335603
end
select @periodoV as periodoV,@periodoA as periodoA,@periodoVAnt as periodoVANT,@PERIODO AS PERIODO,@periodoYear as periodoYear,@periodoMonth as periodoMonth,@Modalidad AS Modalidad,@PeriodoModalidad AS PeriodoModalidad
/*
truncate table led_docs_previo
*/
DECLARE @LastDay_AnyMonth DATETIME
--Last Day of Any Month and Year
DECLARE @dtDate DATETIME, @nextMonth DATETIME, @LastDayNextMonth DATETIME
SET @dtDate = substring(CONVERT(varchar(6),@periodo),1,4) + '-' + substring(CONVERT(varchar(6),@periodo),5,2) + '-01'
SET @nextMonth = DATEADD(mm, 1, @dtDate)
set @LastDay_AnyMonth = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@dtDate)+1,0))
set @LastDayNextMonth = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@nextMonth)+1,0))
DECLARE @tipo_doc INT,@estado_doc VARCHAR(1),@led_seccion VARCHAR(1),@cliente INT,@contratista INT,@empleado INT,@vehiculo INT
DECLARE @contratista_1 INT,@insertado DATETIME,@fecha_tolerancia DATETIME,@compliance_doc VARCHAR(1),@fecha_vigencia DATETIME,@doc_log VARCHAR(150),@grupo_doc INT
DECLARE @doc_id_lista INT
DECLARE ImperiumCargaDocs Cursor
FOR
SELECT distinct led_tipos_doc.tipo_doc,
iif(LDAnt.fecha_vigencia > GETDATE(),LDAnt.estado_doc,led_cliente_empleado.estado_inicial) as estado_doc,
led_tipos_doc.led_seccion,
led_empleados.cliente,
led_empleados.contratista,
led_empleados.empleado as empleado,
null as vehiculo,
@periodo AS periodo,
led_empleados.contratista_1,
GETDATE() as insertado,
/*
dateadd(ms,-3,Convert(varchar(10),isnull(led_cliente_empleado.dias_tolerancia,0) + getdate(),111)) as fecha_tolerancia,
*/
iif(@Modalidad = 'A',(getdate() + led_cliente_empleado.dias_tolerancia),convert(varchar(4),year(getdate())) + '-' + RIGHT('0' + RTRIM(MONTH(getdate())), 2) + '-' + RIGHT('0' + RTRIM(led_cliente_empleado.dias_tolerancia), 2)) as fecha_tolerancia,
iif(LDAnt.fecha_vigencia > GETDATE(),LDAnt.compliance_doc,iif(led_cliente_empleado.estado_inicial='N','B',iif(led_cliente_empleado.estado_inicial='R','R',iif(led_cliente_empleado.estado_inicial='V','V',iif(led_cliente_empleado.estado_inicial='I','A','B'))))) as compliance_doc,
iif(LDAnt.fecha_vigencia > getdate(),LDAnt.fecha_vigencia,NULL) as fecha_vigencia,
convert(varchar(16),getdate(),120) + ' GENERACION AUTOMATICA DE DOCUMENTOS' AS doc_log,
led_grupo_tipo_doc.grupo_doc,led_tipos_doc.tipo_doc_lista as doc_id_lista
FROM led_empleados INNER JOIN
led_cliente_empleado ON led_empleados.cliente = led_cliente_empleado.cliente
AND led_empleados.tipo_empleado = led_cliente_empleado.tipo_empleado INNER JOIN
clientes ON led_empleados.cliente = clientes.cliente INNER JOIN
led_tipos_doc ON led_cliente_empleado.tipo_doc = led_tipos_doc.tipo_doc inner join
led_grupo_tipo_doc ON led_cliente_empleado.tipo_doc = led_grupo_tipo_doc.tipo_doc
inner join led_grupos_doc on led_grupo_tipo_doc.grupo_doc = led_grupos_doc.grupo_doc
INNER JOIN led_empleados_periodo on (led_empleados.empleado = led_empleados_periodo.empleado and led_empleados_periodo.periodo = @periodo and led_empleados_periodo.tipo_baja is null)
left JOIN LED_DOCS LDAnt ON (LDAnt.tipo_doc = led_tipos_doc.tipo_doc AND LDAnt.led_seccion = led_tipos_doc.led_seccion AND LDAnt.empleado = led_empleados.empleado
AND LDAnt.periodo = @periodoVAnt AND LDAnt.grupo_doc = led_grupo_tipo_doc.grupo_doc)
WHERE (clientes.cliente = @IDCliente) AND (led_cliente_empleado.activo = 1)
/*
and ((led_tipos_doc.doc_alta = 0 OR led_tipos_doc.doc_alta IS NULL)
and (led_tipos_doc.doc_baja = 0 OR led_tipos_doc.doc_baja IS NULL) or led_tipos_doc.doc_alta = 1 and (LDAnt.fecha_vigencia > getdate() OR LDAnt.fecha_vigencia IS NULL))
*/
and ( ISNULL(led_tipos_doc.doc_alta,0) = 0
or (led_tipos_doc.doc_alta = 1 /*and
(LDAnt.fecha_vigencia > getdate() OR LDAnt.fecha_vigencia IS NULL)*/
) )
and ISNULL(led_tipos_doc.doc_baja,0) = 0
and led_grupos_doc.cliente = @IDCliente
and led_empleados.estado_empleado = 'A'
and led_tipos_doc.modalidad = @Modalidad
/*
and led_empleados.empleado = 103590
*/
/*
and led_tipos_doc.tipo_doc in (457,738,755)
*/
DECLARE @dbDocID BIGINT = 0
Open ImperiumCargaDocs
Fetch NEXT FROM ImperiumCargaDocs INTO @tipo_doc,@estado_doc,@led_seccion,@cliente,@contratista,@empleado,@vehiculo,@periodo,@contratista_1,@insertado,@fecha_tolerancia,@compliance_doc,@fecha_vigencia,@doc_log,@grupo_doc,@doc_id_lista
While @@FETCH_STATUS =0
BEGIN
SELECT @tipo_doc,@estado_doc,@led_seccion,@cliente,@contratista,@empleado,@vehiculo,@periodo,@contratista_1,@insertado,@fecha_tolerancia,@compliance_doc,@fecha_vigencia,@doc_log,@grupo_doc,@doc_id_lista
SET @dbDocID = 0
select @dbDocID = doc_id from led_docs
where tipo_doc = @tipo_doc
and periodo = @periodo
and led_seccion = @led_seccion
and contratista = @contratista
and empleado = @empleado
/*
IF (@dbDocID > 0 )
BEGIN
select @dbDocID as dbDocID
END
*/
IF (@dbDocID = 0)
/*
select @dbDocID as dbDocID
*/
BEGIN
BEGIN TRY
insert into led_docs (
tipo_doc,
estado_doc,
led_seccion,
cliente,
contratista,
empleado,
vehiculo,
periodo,
contratista_1,
insertado,
fecha_tolerancia,
compliance_doc,
fecha_vigencia,
doc_log,
grupo_doc,
doc_id_lista
)
values
(@tipo_doc,@estado_doc,@led_seccion,@cliente,@contratista,@empleado,@vehiculo,@periodo,@contratista_1,@insertado,@fecha_tolerancia,@compliance_doc,@fecha_vigencia,@doc_log,@grupo_doc,@doc_id_lista)
END TRY
BEGIN CATCH
SET @ERRORNUMBER = 1
END CATCH
END
Fetch NEXT FROM ImperiumCargaDocs INTO @tipo_doc,@estado_doc,@led_seccion,@cliente,@contratista,@empleado,@vehiculo,@periodo,@contratista_1,@insertado,@fecha_tolerancia,@compliance_doc,@fecha_vigencia,@doc_log,@grupo_doc,@doc_id_lista
END
CLOSE ImperiumCargaDocs
DEALLOCATE ImperiumCargaDocs
END
GO
GO
CREATE OR ALTER PROCEDURE dbo.Imperium_Carga_Docs_Empleados
@periodo int,
@IDCliente int
AS
BEGIN
SET NOCOUNT ON;
DECLARE @ERRORNUMBER SMALLINT
declare @periodoAnt int
DECLARE @PeriodoModalidad int
DECLARE @Modalidad VARCHAR(1)
/*
declare @periodo int = 201612
DECLARE @IDCliente INT = 5546348
*/
DECLARE @periodoV INT = NULL
DECLARE @periodoVAnt INT = NULL
DECLARE @periodoA INT = NULL
DECLARE @NombreCliente varchar(200)
DECLARE @periodoYear INT = NULL
DECLARE @periodoMonth INT = NULL
SET @periodoYear = convert(varchar(4),datepart(year,getdate()))
SET @periodoMonth = convert(varchar(2),datepart(month,getdate()))
SET @periodoYear = datepart(year,getdate())
SET @periodoMonth = datepart(month,getdate())
SET @periodoA = convert(varchar(4),datepart(year,getdate())) + RIGHT('00' +convert(varchar(2),datepart(month,getdate())),2)
if (@periodoMonth = 1)
begin
set @periodoV = convert(varchar(4),@periodoYear-1) + RIGHT('00' +convert(varchar(2),12),2)
set @periodoVAnt = convert(varchar(4),@periodoYear-1) + RIGHT('00' +convert(varchar(2),11),2)
end
if (@periodoMonth > 1)
begin
set @periodoV = convert(varchar(4),datepart(year,getdate())) + RIGHT('00' +convert(varchar(2),datepart(month,getdate())-1),2)
set @periodoVAnt = convert(varchar(4),datepart(year,getdate())) + RIGHT('00' +convert(varchar(2),datepart(month,getdate())-2),2)
end
set @periodoVAnt = dbo.PeriodoMenosDos()
set @periodoV = dbo.PeriodoVencido()
set @periodoA = dbo.PeriodoAdelantado()
if @Periodo = @periodoA
BEGIN
SET @Modalidad = 'A'
END
ELSE
BEGIN
SET @Modalidad = 'V'
END
if (@IDCliente = 9335603)
begin
set @IDCliente = 9335603
end
select @periodoV as periodoV,@periodoA as periodoA,@periodoVAnt as periodoVANT,@PERIODO AS PERIODO,@periodoYear as periodoYear,@periodoMonth as periodoMonth,@Modalidad AS Modalidad,@PeriodoModalidad AS PeriodoModalidad
/*
truncate table led_docs_previo
*/
DECLARE @LastDay_AnyMonth DATETIME
--Last Day of Any Month and Year
DECLARE @dtDate DATETIME, @nextMonth DATETIME, @LastDayNextMonth DATETIME
SET @dtDate = substring(CONVERT(varchar(6),@periodo),1,4) + '-' + substring(CONVERT(varchar(6),@periodo),5,2) + '-01'
SET @nextMonth = DATEADD(mm, 1, @dtDate)
set @LastDay_AnyMonth = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@dtDate)+1,0))
set @LastDayNextMonth = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@nextMonth)+1,0))
DECLARE @tipo_doc INT,@estado_doc VARCHAR(1),@led_seccion VARCHAR(1),@cliente INT,@contratista INT,@empleado INT,@vehiculo INT
DECLARE @contratista_1 INT,@insertado DATETIME,@fecha_tolerancia DATETIME,@compliance_doc VARCHAR(1),@fecha_vigencia DATETIME,@doc_log VARCHAR(150),@grupo_doc INT
DECLARE @doc_id_lista INT
DECLARE ImperiumCargaDocs Cursor
FOR
SELECT distinct led_tipos_doc.tipo_doc,
iif(LDAnt.fecha_vigencia > GETDATE(),LDAnt.estado_doc,led_cliente_empleado.estado_inicial) as estado_doc,
led_tipos_doc.led_seccion,
led_empleados.cliente,
led_empleados.contratista,
led_empleados.empleado as empleado,
null as vehiculo,
@periodo AS periodo,
led_empleados.contratista_1,
GETDATE() as insertado,
/*
dateadd(ms,-3,Convert(varchar(10),isnull(led_cliente_empleado.dias_tolerancia,0) + getdate(),111)) as fecha_tolerancia,
*/
iif(@Modalidad = 'A',(getdate() + led_cliente_empleado.dias_tolerancia),convert(varchar(4),year(getdate())) + '-' + RIGHT('0' + RTRIM(MONTH(getdate())), 2) + '-' + RIGHT('0' + RTRIM(led_cliente_empleado.dias_tolerancia), 2)) as fecha_tolerancia,
iif(LDAnt.fecha_vigencia > GETDATE(),LDAnt.compliance_doc,iif(led_cliente_empleado.estado_inicial='N','B',iif(led_cliente_empleado.estado_inicial='R','R',iif(led_cliente_empleado.estado_inicial='V','V',iif(led_cliente_empleado.estado_inicial='I','A','B'))))) as compliance_doc,
iif(LDAnt.fecha_vigencia > getdate(),LDAnt.fecha_vigencia,NULL) as fecha_vigencia,
convert(varchar(16),getdate(),120) + ' GENERACION AUTOMATICA DE DOCUMENTOS' AS doc_log,
led_grupo_tipo_doc.grupo_doc,led_tipos_doc.tipo_doc_lista as doc_id_lista
FROM led_empleados INNER JOIN
led_cliente_empleado ON led_empleados.cliente = led_cliente_empleado.cliente
AND led_empleados.tipo_empleado = led_cliente_empleado.tipo_empleado INNER JOIN
clientes ON led_empleados.cliente = clientes.cliente INNER JOIN
led_tipos_doc ON led_cliente_empleado.tipo_doc = led_tipos_doc.tipo_doc inner join
led_grupo_tipo_doc ON led_cliente_empleado.tipo_doc = led_grupo_tipo_doc.tipo_doc
inner join led_grupos_doc on led_grupo_tipo_doc.grupo_doc = led_grupos_doc.grupo_doc
INNER JOIN led_empleados_periodo on (led_empleados.empleado = led_empleados_periodo.empleado and led_empleados_periodo.periodo = @periodo and led_empleados_periodo.tipo_baja is null)
left JOIN LED_DOCS LDAnt ON (LDAnt.tipo_doc = led_tipos_doc.tipo_doc AND LDAnt.led_seccion = led_tipos_doc.led_seccion AND LDAnt.empleado = led_empleados.empleado
AND LDAnt.periodo = @periodoVAnt AND LDAnt.grupo_doc = led_grupo_tipo_doc.grupo_doc)
WHERE (clientes.cliente = @IDCliente) AND (led_cliente_empleado.activo = 1)
/*
and ((led_tipos_doc.doc_alta = 0 OR led_tipos_doc.doc_alta IS NULL)
and (led_tipos_doc.doc_baja = 0 OR led_tipos_doc.doc_baja IS NULL) or led_tipos_doc.doc_alta = 1 and (LDAnt.fecha_vigencia > getdate() OR LDAnt.fecha_vigencia IS NULL))
*/
and ( ISNULL(led_tipos_doc.doc_alta,0) = 0
or (led_tipos_doc.doc_alta = 1 /*and
(LDAnt.fecha_vigencia > getdate() OR LDAnt.fecha_vigencia IS NULL)*/
) )
and ISNULL(led_tipos_doc.doc_baja,0) = 0
and led_grupos_doc.cliente = @IDCliente
and led_empleados.estado_empleado = 'A'
and led_tipos_doc.modalidad = @Modalidad
/*
and led_empleados.empleado = 103590
*/
/*
and led_tipos_doc.tipo_doc in (457,738,755)
*/
DECLARE @dbDocID BIGINT = 0
Open ImperiumCargaDocs
Fetch NEXT FROM ImperiumCargaDocs INTO @tipo_doc,@estado_doc,@led_seccion,@cliente,@contratista,@empleado,@vehiculo,@periodo,@contratista_1,@insertado,@fecha_tolerancia,@compliance_doc,@fecha_vigencia,@doc_log,@grupo_doc,@doc_id_lista
While @@FETCH_STATUS =0
BEGIN
SELECT @tipo_doc,@estado_doc,@led_seccion,@cliente,@contratista,@empleado,@vehiculo,@periodo,@contratista_1,@insertado,@fecha_tolerancia,@compliance_doc,@fecha_vigencia,@doc_log,@grupo_doc,@doc_id_lista
SET @dbDocID = 0
select @dbDocID = doc_id from led_docs
where tipo_doc = @tipo_doc
and periodo = @periodo
and led_seccion = @led_seccion
and contratista = @contratista
and empleado = @empleado
/*
IF (@dbDocID > 0 )
BEGIN
select @dbDocID as dbDocID
END
*/
IF (@dbDocID = 0)
/*
select @dbDocID as dbDocID
*/
BEGIN
BEGIN TRY
insert into led_docs (
tipo_doc,
estado_doc,
led_seccion,
cliente,
contratista,
empleado,
vehiculo,
periodo,
contratista_1,
insertado,
fecha_tolerancia,
compliance_doc,
fecha_vigencia,
doc_log,
grupo_doc,
doc_id_lista
)
values
(@tipo_doc,@estado_doc,@led_seccion,@cliente,@contratista,@empleado,@vehiculo,@periodo,@contratista_1,@insertado,@fecha_tolerancia,@compliance_doc,@fecha_vigencia,@doc_log,@grupo_doc,@doc_id_lista)
END TRY
BEGIN CATCH
SET @ERRORNUMBER = 1
END CATCH
END
Fetch NEXT FROM ImperiumCargaDocs INTO @tipo_doc,@estado_doc,@led_seccion,@cliente,@contratista,@empleado,@vehiculo,@periodo,@contratista_1,@insertado,@fecha_tolerancia,@compliance_doc,@fecha_vigencia,@doc_log,@grupo_doc,@doc_id_lista
END
CLOSE ImperiumCargaDocs
DEALLOCATE ImperiumCargaDocs
END
GO