dbo.Carga_Led_Docs_Empleados

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.Carga_Led_Docs_Empleados
@periodo int,
@IDCliente int

AS

BEGIN
 SET NOCOUNT ON;


DECLARE @ERRORNUMBER SMALLINT 

if (@periodo is null)
BEGIN
 SET @periodo = convert(varchar(4),datepart(year,getdate())) + RIGHT('00' +convert(varchar(2),datepart(month,getdate())),2)

END



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 LedCargaDocs Cursor
FOR
SELECT  distinct led_tipos_doc.tipo_doc,
'N' as estado_doc,
led_tipos_doc.led_seccion,
led_empleados.cliente,
led_empleados.contratista,  
led_empleados.empleado as empleado,
null as vehiculo,
/*
convert(varchar(4),datepart(year,getdate())) + convert(varchar(2),datepart(month,getdate())) as periodo,
*/

@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((isnull(led_cliente_empleado.dias_tolerancia,0) + getdate())>getdate() ,'B','R') as compliance_doc,
iif(ISNULL(led_tipos_doc.periodicidad,'M') = 'M',@LastDayNextMonth,IIF(ISNULL(led_tipos_doc.periodicidad,'A') = 'A',@LastDay_AnyMonth,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)
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)
and led_grupos_doc.cliente = @IDCliente
and led_empleados.estado_empleado = 'A'

DECLARE @dbDocID BIGINT = 0
Open LedCargaDocs


Fetch NEXT FROM LedCargaDocs 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 <> -1)
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)
select @dbDocID as dbDocID

BEGIN
BEGIN TRY
SELECT 'NO USAR'
/*
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 LedCargaDocs 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 LedCargaDocs
DEALLOCATE LedCargaDocs

END

GO

Used By

No items found