dbo.Imperium_Multi_Compliance_Periodo_ContratistaDEV

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

CREATE OR ALTER PROCEDURE dbo.Imperium_Multi_Compliance_Periodo_ContratistaDEV
@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 LD.contratista = @IDContratista
 and LD.led_seccion = 'C'
 
 and LCC.requiere_upload = 1
 
 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 LD.contratista = @IDContratista
 and LD.led_seccion = 'C'
 
 and LCC.requiere_upload = 1
 
 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 LD.contratista = @IDContratista
 and LD.led_seccion = 'C'
 
 and LCC.requiere_upload = 1
 
 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 LD.contratista = @IDContratista
 and LD.led_seccion = 'C'
 
 and LCC.requiere_upload = 1
 
 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 = '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 LD.contratista = @IDContratista
and LD.led_seccion = @led_seccion

and LCC.requiere_upload = 1

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 LD.contratista = @IDContratista
and LD.led_seccion = @led_seccion

and LCC.requiere_upload = 1

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(doc_id) as cant
       from led_docs LD inner join led_contratista_periodo LCP on  (Ld.contratista = LCP.contratista and LD.periodo = LCP.periodo)
where LD.periodo = @periodo AND 
ld.TIPO_DOC IN (SELECT DISTINCT TIPO_DOC FROM led_cliente_contratista WHERE tipo_contratista IN (
select DISTINCT tipo_contratista
from led_contratista_multi 
where activo = 1 
and vigencia_desde < getdate() 
and (vigencia_hasta is null or getdate() < vigencia_hasta)
and contratista = @IDContratista
and requiere_upload = 1
and activo = 1
AND afecta_compliance = 1)) 
      and LD.contratista = @IDContratista
       and LD.led_seccion = @led_seccion
       and (LD.compliance_doc = 'B' OR (LD.compliance_doc IN ('Y','A') 
                                                                   and LD.fecha_tolerancia > getdate()))
)
,@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 LD.contratista = @IDContratista
and LD.led_seccion = @led_seccion

and LCC.requiere_upload = 1

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 = 'C'

BEGIN

select 
@estado_complianceV = (SELECT COUNT(ld.doc_id) from led_docs LD inner join led_contratista_periodo LCP on  (Ld.contratista = LCP.contratista and LD.periodo = LCP.periodo)
where LD.periodo = @periodo AND 
ld.TIPO_DOC IN (select lcc.tipo_doc
from led_cliente_contratista lcc inner join led_contratista_multi lcm
on lcc.tipo_contratista = lcm.tipo_contratista
where lcm.contratista = @IDContratista
and lcc.activo =1
and lcm.activo = 1
and lcc.requiere_upload = 1
and lcc.afecta_compliance = 1
and lcm.vigencia_desde <= GETDATE()
and (lcm.vigencia_hasta IS NULL OR lcm.vigencia_hasta >= GETDATE())
group by lcc.tipo_doc) 
      and LD.contratista = @IDContratista
       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_contratista_periodo LCP on  (Ld.contratista = LCP.contratista and LD.periodo = LCP.periodo)
where LD.periodo = @periodo AND 
ld.TIPO_DOC IN (select lcc.tipo_doc
from led_cliente_contratista lcc inner join led_contratista_multi lcm
on lcc.tipo_contratista = lcm.tipo_contratista
where lcm.contratista = @IDContratista
and lcc.activo =1
and lcm.activo = 1
and lcc.requiere_upload = 1
and lcc.afecta_compliance = 1
and lcm.vigencia_desde <= GETDATE()
and (lcm.vigencia_hasta IS NULL OR lcm.vigencia_hasta >= GETDATE())
group by lcc.tipo_doc) 
      and LD.contratista = @IDContratista
       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_contratista_periodo LCP on  (Ld.contratista = LCP.contratista and LD.periodo = LCP.periodo)
where LD.periodo = @periodo AND 
ld.TIPO_DOC IN (select lcc.tipo_doc
from led_cliente_contratista lcc inner join led_contratista_multi lcm
on lcc.tipo_contratista = lcm.tipo_contratista
where lcm.contratista = @IDContratista
and lcc.activo =1
and lcm.activo = 1
and lcc.requiere_upload = 1
and lcc.afecta_compliance = 1
and lcm.vigencia_desde <= GETDATE()
and (lcm.vigencia_hasta IS NULL OR lcm.vigencia_hasta >= GETDATE())
group by lcc.tipo_doc) 
      and LD.contratista = @IDContratista
       and LD.led_seccion = @led_seccion
       and (LD.compliance_doc = 'B' OR (LD.compliance_doc IN ('Y','A') 
  and LD.fecha_tolerancia > getdate()))
)
,@estado_complianceY = (SELECT COUNT(ld.doc_id) from led_docs LD inner join led_contratista_periodo LCP on  (Ld.contratista = LCP.contratista and LD.periodo = LCP.periodo)
where LD.periodo = @periodo AND 
ld.TIPO_DOC IN (select lcc.tipo_doc
from led_cliente_contratista lcc inner join led_contratista_multi lcm
on lcc.tipo_contratista = lcm.tipo_contratista
where lcm.contratista = @IDContratista
and lcc.activo =1
and lcm.activo = 1
and lcc.requiere_upload = 1
and lcc.afecta_compliance = 1
and lcm.vigencia_desde <= GETDATE()
and (lcm.vigencia_hasta IS NULL OR lcm.vigencia_hasta >= GETDATE())
group by lcc.tipo_doc) 
      and LD.contratista = @IDContratista
       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 = '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)
 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 LD.contratista = @IDContratista
 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_periodo LEP on  (Ld.contratista = LEP.contratista and LD.periodo = LEP.periodo)
where LD.periodo = @periodo AND 
ld.TIPO_DOC IN (select distinct lce.tipo_doc
from led_cliente_empleado lce inner join led_empleado_multi lem
on lce.tipo_empleado = lem.tipo_empleado
where lem.contratista = @IDContratista
and lce.activo =1
and lem.activo = 1
and lce.requiere_upload = 1
and lce.afecta_compliance = 1
and lem.vigencia_desde <= GETDATE()
and (lem.vigencia_hasta IS NULL OR lem.vigencia_hasta >= GETDATE())
group by lce.tipo_doc)
       and LD.contratista = @IDContratista
       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)
 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 LD.contratista = @IDContratista
 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)
 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 LD.contratista = @IDContratista
 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)
 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 LD.contratista = @IDContratista
 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)
 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 LD.contratista = @IDContratista
 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)
 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 LD.contratista = @IDContratista
 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)
 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 LD.contratista = @IDContratista
 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

Used By

No items found