dbo.Imperium_Compliance_Periodo_Contratista

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

3
Name Data Type Length Description
@periodo int 4
@IDContratista int 4
@led_seccion varchar 1

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

/*

exec [Imperium_Compliance_Periodo_Contratista] 202104,4904,'E'

*/


CREATE OR ALTER PROCEDURE dbo.Imperium_Compliance_Periodo_Contratista
@periodo int,
@IDContratista int,
@led_seccion varchar(1)

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),@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 @estado_complianceV INT
DECLARE @estado_complianceR INT
DECLARE @estado_complianceB INT
DECLARE @estado_complianceY INT




if @led_seccion = 'S'

BEGIN

select 
@estado_complianceV = (SELECT count(LD.doc_id)
 from led_docs LD inner join led_contratistas LC
 on LD.contratista = LC.contratista
 inner join led_subcontratista_periodo LCP on 
  (LC.contratista = LCP.subcontratista
  and LD.periodo = LCP.periodo)
 inner join led_cliente_contratista LCC on
  (LC.tipo_contratista = LCC.tipo_contratista
  and LC.cliente = LCC.cliente
  and LD.tipo_doc = LCC.tipo_doc
  and LD.cliente = LCC.cliente)
 where LD.periodo = @periodo
 and LC.contratista = @IDContratista and LC.estado_contratista = 'A'
 and LD.led_seccion = 'C'
 
 and LCC.requiere_upload in (1,0)
 
 and LCC.activo = 1
 AND LCC.afecta_compliance = 1
 and (LD.compliance_doc = 'V'))
,@estado_complianceR = (SELECT count(LD.doc_id)
 from led_docs LD inner join led_contratistas LC
 on LD.contratista = LC.contratista
 inner join led_subcontratista_periodo LCP on 
  (LC.contratista = LCP.subcontratista
  and LD.periodo = LCP.periodo)
 inner join led_cliente_contratista LCC on
  (LC.tipo_contratista = LCC.tipo_contratista
  and LC.cliente = LCC.cliente
  and LD.tipo_doc = LCC.tipo_doc
  and LD.cliente = LCC.cliente)
 where LD.periodo = @periodo
 and LC.contratista = @IDContratista and LC.estado_contratista = 'A'
 and LD.led_seccion = 'C'
 
 and LCC.requiere_upload in (1,0)
 
 and LCC.activo = 1
 AND LCC.afecta_compliance = 1
 and (LD.compliance_doc = 'R' OR (LD.compliance_doc IN ('B','Y','A') 
          and LD.fecha_tolerancia < getdate())))
,@estado_complianceB = (SELECT count(LD.doc_id)
 from led_docs LD inner join led_contratistas LC
 on LD.contratista = LC.contratista
 inner join led_subcontratista_periodo LCP on 
  (LC.contratista = LCP.subcontratista
  and LD.periodo = LCP.periodo)
 inner join led_cliente_contratista LCC on
  (LC.tipo_contratista = LCC.tipo_contratista
  and LC.cliente = LCC.cliente
  and LD.tipo_doc = LCC.tipo_doc
  and LD.cliente = LCC.cliente)
 where LD.periodo = @periodo
 and LC.contratista = @IDContratista and LC.estado_contratista = 'A'
 and LD.led_seccion = 'C'
 
 and LCC.requiere_upload in (1,0)
 
 and LCC.activo = 1
 AND LCC.afecta_compliance = 1
 and (LD.compliance_doc = 'B'))
,@estado_complianceY = (SELECT count(LD.doc_id)
 from led_docs LD inner join led_contratistas LC
 on LD.contratista = LC.contratista
 inner join led_subcontratista_periodo LCP on 
  (LC.contratista = LCP.subcontratista
  and LD.periodo = LCP.periodo)
 inner join led_cliente_contratista LCC on
  (LC.tipo_contratista = LCC.tipo_contratista
  and LC.cliente = LCC.cliente
  and LD.tipo_doc = LCC.tipo_doc
  and LD.cliente = LCC.cliente)
 where LD.periodo = @periodo
 and LC.contratista = @IDContratista and LC.estado_contratista = 'A'
 and LD.led_seccion = 'C'
 
 and LCC.requiere_upload in (1,0)
 
 and LCC.activo = 1
 --AND LCC.afecta_compliance = 1
 and (LD.compliance_doc = 'B' OR (LD.compliance_doc IN ('Y','A','I') 
          and LD.fecha_tolerancia > getdate())))
END




if @led_seccion = 'C'

BEGIN

select 
@estado_complianceV = (SELECT count(LD.doc_id)
 from led_docs LD inner join led_contratistas LC
 on LD.contratista = LC.contratista
 inner join led_contratista_periodo LCP on 
  (LC.contratista = LCP.contratista
  and LD.periodo = LCP.periodo)
 inner join led_cliente_contratista LCC on
  (LC.tipo_contratista = LCC.tipo_contratista
  and LC.cliente = LCC.cliente
  and LD.tipo_doc = LCC.tipo_doc
  and LD.cliente = LCC.cliente)
 where LD.periodo = @periodo
 and LC.contratista = @IDContratista and LC.estado_contratista = 'A'
 and LD.led_seccion = @led_seccion
 
 and LCC.requiere_upload in (1,0)
 
 and LCC.activo = 1
 AND LCC.afecta_compliance = 1
 and (LD.compliance_doc = 'V'))
,@estado_complianceR = (SELECT count(LD.doc_id)
 from led_docs LD inner join led_contratistas LC
 on LD.contratista = LC.contratista
 inner join led_contratista_periodo LCP on 
  (LC.contratista = LCP.contratista
  and LD.periodo = LCP.periodo)
 inner join led_cliente_contratista LCC on
  (LC.tipo_contratista = LCC.tipo_contratista
  and LC.cliente = LCC.cliente
  and LD.tipo_doc = LCC.tipo_doc
  and LD.cliente = LCC.cliente)
 where LD.periodo = @periodo
 and LC.contratista = @IDContratista and LC.estado_contratista = 'A'
 and LD.led_seccion = @led_seccion
 
 and LCC.requiere_upload in (1,0)
 
 and LCC.activo = 1
 AND LCC.afecta_compliance = 1
 and (LD.compliance_doc = 'R' OR (LD.compliance_doc IN ('B','Y','A') 
          and LD.fecha_tolerancia < getdate())))
,@estado_complianceB = (SELECT count(LD.doc_id)
 from led_docs LD inner join led_contratistas LC
 on LD.contratista = LC.contratista
 inner join led_contratista_periodo LCP on 
  (LC.contratista = LCP.contratista
  and LD.periodo = LCP.periodo)
 inner join led_cliente_contratista LCC on
  (LC.tipo_contratista = LCC.tipo_contratista
  and LC.cliente = LCC.cliente
  and LD.tipo_doc = LCC.tipo_doc
  and LD.cliente = LCC.cliente)
 where LD.periodo = @periodo
 and LC.contratista = @IDContratista and LC.estado_contratista = 'A'
 and LD.led_seccion = @led_seccion
 
 and LCC.requiere_upload in (1,0)
 
 and LCC.activo = 1
 AND LCC.afecta_compliance = 1
 and (LD.compliance_doc = 'B'))
,@estado_complianceY = (SELECT count(LD.doc_id)
 from led_docs LD inner join led_contratistas LC
 on LD.contratista = LC.contratista
 inner join led_contratista_periodo LCP on 
  (LC.contratista = LCP.contratista
  and LD.periodo = LCP.periodo)
 inner join led_cliente_contratista LCC on
  (LC.tipo_contratista = LCC.tipo_contratista
  and LC.cliente = LCC.cliente
  and LD.tipo_doc = LCC.tipo_doc
  and LD.cliente = LCC.cliente)
 where LD.periodo = @periodo
 and LC.contratista = @IDContratista and LC.estado_contratista = 'A'
 and LD.led_seccion = @led_seccion
 
 and LCC.requiere_upload in (1,0)
 
 and LCC.activo = 1
 AND LCC.afecta_compliance = 1
 and (LD.compliance_doc = 'B' OR (LD.compliance_doc IN ('Y','A') 
          and LD.fecha_tolerancia > getdate())))
END



if @led_seccion = 'E'

BEGIN

select 
@estado_complianceV = (SELECT count(LD.doc_id)
 from led_docs LD inner join led_empleados LE
 on LD.empleado = LE.empleado
 inner join led_empleados_periodo LEP on 
  (LE.empleado = LEP.empleado
  and LD.periodo = LEP.periodo
  and LD.contratista = LE.contratista)
 inner join led_cliente_empleado LCE on
  (LE.tipo_empleado = LCE.tipo_empleado
  and LE.cliente = LCE.cliente
  and LD.tipo_doc = LCE.tipo_doc
  and LD.cliente = LCE.cliente
  and LCE.activo=1
  AND LCE.afecta_compliance=1)
 where LD.periodo = @periodo
 and LE.contratista = @IDContratista 
 and LE.estado_empleado = 'A'
 and LD.led_seccion = @led_seccion
 and (LD.compliance_doc = 'V'))
 
,
@estado_complianceR = (SELECT count(LD.doc_id)
 from led_docs LD inner join led_empleados LE
 on LD.empleado = LE.empleado
 inner join led_empleados_periodo LEP on 
  (LE.empleado = LEP.empleado
  and LD.periodo = LEP.periodo
  and LD.contratista = LE.contratista)
 inner join led_cliente_empleado LCE on
  (LE.tipo_empleado = LCE.tipo_empleado
  and LE.cliente = LCE.cliente
  and LD.tipo_doc = LCE.tipo_doc
  and LD.cliente = LCE.cliente
  and LCE.activo=1
  AND LCE.afecta_compliance=1)
 where LD.periodo = @periodo
 and LE.contratista = @IDContratista 
 and LE.estado_empleado = 'A'
 and LD.led_seccion = @led_seccion
 and (LD.compliance_doc = 'R' OR (LD.compliance_doc IN ('B','Y','A') and LD.fecha_tolerancia < getdate()) ))
,
@estado_complianceB = (SELECT count(LD.doc_id)
 from led_docs LD inner join led_empleados LE
 on LD.empleado = LE.empleado
 inner join led_empleados_periodo LEP on 
  (LE.empleado = LEP.empleado
  and LD.periodo = LEP.periodo
  and LD.contratista = LE.contratista)
 inner join led_cliente_empleado LCE on
  (LE.tipo_empleado = LCE.tipo_empleado
  and LE.cliente = LCE.cliente
  and LD.tipo_doc = LCE.tipo_doc
  and LD.cliente = LCE.cliente
  and LCE.activo=1
  AND LCE.afecta_compliance=1)
 where LD.periodo = @periodo
 and LE.contratista = @IDContratista 
 and LE.estado_empleado = 'A'
 and LD.led_seccion = @led_seccion
 and (LD.compliance_doc = 'B'))
,
@estado_complianceY = (SELECT count(LD.doc_id)
 from led_docs LD inner join led_empleados LE
 on LD.empleado = LE.empleado
 inner join led_empleados_periodo LEP on 
  (LE.empleado = LEP.empleado
  and LD.periodo = LEP.periodo
  and LD.contratista = LE.contratista)
 inner join led_cliente_empleado LCE on
  (LE.tipo_empleado = LCE.tipo_empleado
  and LE.cliente = LCE.cliente
  and LD.tipo_doc = LCE.tipo_doc
  and LD.cliente = LCE.cliente
  and LCE.activo=1
  AND LCE.afecta_compliance=1)
 where LD.periodo = @periodo
 and LE.contratista = @IDContratista 
 and LE.estado_empleado = 'A'
 and LD.led_seccion = @led_seccion
 and (LD.compliance_doc = 'B' OR (LD.compliance_doc IN ('Y','A') 
          and LD.fecha_tolerancia > getdate())))


END



if @led_seccion = 'V'

BEGIN

select 
@estado_complianceV = (SELECT count(LD.doc_id)
 from led_docs LD inner join led_vehiculos LE
 on LD.vehiculo = LE.vehiculo
 inner join led_vehiculos_periodo LEP on 
  (LE.vehiculo = LEP.vehiculo
  and LD.periodo = LEP.periodo
  and LD.contratista = LE.contratista)
 inner join led_cliente_vehiculo LCE on
  (LE.tipo_vehiculo = LCE.tipo_vehiculo
  and LE.cliente = LCE.cliente
  and LD.tipo_doc = LCE.tipo_doc
  and LD.cliente = LCE.cliente
  AND LCE.activo=1
  AND LCE.afecta_compliance=1)
 where LD.periodo = @periodo
 and LE.contratista = @IDContratista /*and LE.estado_vehiculo = 'A'*/
 and LD.led_seccion = @led_seccion
 and (LD.compliance_doc = 'V'))
,
@estado_complianceR = (SELECT count(LD.doc_id)
 from led_docs LD inner join led_vehiculos LE
 on LD.vehiculo = LE.vehiculo
 inner join led_vehiculos_periodo LEP on 
  (LE.vehiculo = LEP.vehiculo
  and LD.periodo = LEP.periodo
  and LD.contratista = LE.contratista)
 inner join led_cliente_vehiculo LCE on
  (LE.tipo_vehiculo = LCE.tipo_vehiculo
  and LE.cliente = LCE.cliente
  and LD.tipo_doc = LCE.tipo_doc
  and LD.cliente = LCE.cliente
  AND LCE.activo=1
  AND LCE.afecta_compliance=1)
 where LD.periodo = @periodo
 and LE.contratista = @IDContratista /*and LE.estado_vehiculo = 'A'*/
 and LD.led_seccion = @led_seccion
 and (LD.compliance_doc = 'R' OR (LD.compliance_doc IN ('B','Y','A') 
          and LD.fecha_tolerancia < getdate())))
,
@estado_complianceB = (SELECT count(LD.doc_id)
 from led_docs LD inner join led_vehiculos LE
 on LD.vehiculo = LE.vehiculo
 inner join led_vehiculos_periodo LEP on 
  (LE.vehiculo = LEP.vehiculo
  and LD.periodo = LEP.periodo
  and LD.contratista = LE.contratista)
 inner join led_cliente_vehiculo LCE on
  (LE.tipo_vehiculo = LCE.tipo_vehiculo
  and LE.cliente = LCE.cliente
  and LD.tipo_doc = LCE.tipo_doc
  and LD.cliente = LCE.cliente
  AND LCE.activo=1
  AND LCE.afecta_compliance=1)
 where LD.periodo = @periodo
 and LE.contratista = @IDContratista /*and LE.estado_vehiculo = 'A'*/
 and LD.led_seccion = @led_seccion
 and (LD.compliance_doc = 'B'))
,
@estado_complianceY = (SELECT count(LD.doc_id)
 from led_docs LD inner join led_vehiculos LE
 on LD.vehiculo = LE.vehiculo
 inner join led_vehiculos_periodo LEP on 
  (LE.vehiculo = LEP.vehiculo
  and LD.periodo = LEP.periodo
  and LD.contratista = LE.contratista)
 inner join led_cliente_vehiculo LCE on
  (LE.tipo_vehiculo = LCE.tipo_vehiculo
  and LE.cliente = LCE.cliente
  and LD.tipo_doc = LCE.tipo_doc
  and LD.cliente = LCE.cliente
  AND LCE.activo=1
  AND LCE.afecta_compliance=1)
 where LD.periodo = @periodo
 and LE.contratista = @IDContratista /*and LE.estado_vehiculo = 'A'*/
 and LD.led_seccion = @led_seccion
 and (LD.compliance_doc = 'B' OR (LD.compliance_doc IN ('Y','A') 
          and LD.fecha_tolerancia > getdate())))


END






if @led_seccion = 'S' and @estado_complianceV = 0 and @estado_complianceB = 0 and @estado_complianceR = 0 and @estado_complianceY = 0
 begin
  set @led_seccion = 'C'
  select 1
 end
ELSE IF  @led_seccion = 'C' and (@estado_complianceV > 0 or @estado_complianceB > 0 or @estado_complianceR > 0 or @estado_complianceY > 0)
 begin
  delete led_compliance where contratista = @IDContratista and periodo=@periodo and led_seccion=@Led_Seccion
  select 2
 end
else if @led_seccion = 'S' and (@estado_complianceV > 0 or @estado_complianceB > 0 or @estado_complianceR > 0 or @estado_complianceY > 0)
 begin
  set @led_seccion = 'C'
  select 3
  delete led_compliance where contratista = @IDContratista and periodo=@periodo and led_seccion=@Led_Seccion
 end
ELSE IF  @led_seccion = 'E'
 begin
 select 4
  delete led_compliance where contratista = @IDContratista and periodo=@periodo and led_seccion=@Led_Seccion
 end
ELSE IF  @led_seccion = 'V'
 begin
 select 5
  delete led_compliance where contratista = @IDContratista and periodo=@periodo and led_seccion=@Led_Seccion
 end



SELECT @led_seccion as led_seccion,@estado_complianceV AS estado_complianceVV, @estado_complianceR AS estado_complianceR, @estado_complianceB AS estado_complianceB, @estado_complianceY AS estado_complianceY


/*
if @led_seccion = 'S'
begin
set @led_seccion = 'C'
end
*/




begin try



if (@estado_complianceR > 0)
begin
 /*
  delete led_compliance where contratista = @contratista and periodo=@periodo and led_seccion=@led_seccion
  */

 insert INTO led_compliance (contratista, periodo, led_seccion,estado_compliance)
 VALUES (@IDContratista,@periodo,@Led_Seccion,'R')
end

if (@estado_complianceV > 0 AND @estado_complianceR = 0 AND @estado_complianceY = 0)
begin
 /*
  delete led_compliance where contratista = @contratista and periodo=@periodo and led_seccion=@led_seccion
  */

 insert INTO led_compliance (contratista, periodo, led_seccion,estado_compliance)
 VALUES (@IDContratista,@periodo,@Led_Seccion,'V')
end
/*@estado_complianceV > 0 AND*/
if (@estado_complianceR = 0 AND @estado_complianceY > 0)
begin
 /*
  delete led_compliance where contratista = @contratista and periodo=@periodo and led_seccion=@led_seccion
  */

 insert INTO led_compliance (contratista, periodo, led_seccion,estado_compliance)
 VALUES (@IDContratista,@periodo,@Led_Seccion,'A')
 
end



END TRY

BEGIN CATCH
/*
SET @ERRORNUMBER = 1
*/

/*
SELECT @ERRORNUMBER,@tipo_doc,@estado_doc,@led_seccion,@cliente,@contratista,@empleado,@vehiculo,@periodo,@contratista_1,@insertado,@fecha_tolerancia,@compliance_doc,@fecha_vigencia,@doc_log,@grupo_doc
*/


select 'ERROR: insert led_compliance (contratista, periodo, led_seccion,estado_compliance) values('
SELECT @contratista ,@periodo,@led_seccion,'HHHH'

/*
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
*/




END CATCH

/*
Fetch NEXT FROM LedResumenPeriodo 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
*/

END

GO