dbo.LN_excel_RSE2

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

2
Name Data Type Length Description
@p_cliente int 4
@p_lista varchar -1

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
/*
[LN_excel_RSE2] 5546348, '106,674'
[LN_excel_RSE] 5546348, '95,98,105,107,122,132,137,142,143,148,149,158,160,161,190,194,197,201,202,207,223,224,227,232,234,235,241,242,243,256,258,259,266,274,279,287,302,305,308,311,317,318,320,321,324,326,334,338,339,342,345,349,357,359,360,363,382,384,385,391,394,396,397,404,412,413,431,438,443,447,457,461,468,473,476,484,486,488,489,491,492,495,497,499,501,504,506,507,514,517,526,532,536,550,555,557,560,561,572,583,584,589,592,595,598,603,609,611,613,617,620,625,626,641,642,647,648,651,653,655,656,657,658,660,666,668,674,690,691,698,700,708,714,719,737,743,747,749,752,755,756,757,762,763,765,767,768,769,4085,4086,4089,4090,4092,4095,4096,4097,4102,4108,4110,4111,4113,4114,4115,4116,4117,4119,4124,4126,4130,4131,4132,4134,4135,4139,4142,4182'
[LN_excel_RSE] 5546348, null
*/



CREATE OR ALTER PROCEDURE dbo.LN_excel_RSE2 
  @p_cliente int,
  @p_lista varchar(max) = null
 
AS
BEGIN
 SET NOCOUNT ON;
 declare @resultado table (contraparte int, pais varchar(100), familia varchar(150), nombre varchar(500), identificador varchar(20), tax_id varchar(20), campo_id varchar(max), campo_nro int,  nombre_campo varchar(200), etiqueta varchar(300),resultado varchar(MAX))
 declare @valores table (contraparte int, gestion int, doc_id int, seccion int, campo int, tipo_campo varchar(1), nombre_campo varchar(200), etiqueta varchar(300),fila int, col int, valor_numerico float, valor_texto varchar(max), valor_fecha smalldatetime, valor_fecha_srt varchar(200))

 declare @contraparte int 
    declare @campo_id varchar(max)
 declare @campo_nro int 
 declare @id_padron int
 declare @pais varchar(100)
 declare @nombre varchar(500)
 declare @familia varchar(150)
 declare @identificador varchar(20)
 declare @tax_id varchar(20)
 declare @respuesta varchar(max)
 declare @cont int, @gestion int, @doc_id int, @seccion int, @campo int, @tipo_campo varchar(1), @nombre_campo varchar(200), @etiqueta varchar(300),@fila int, @col int, @valor_numerico float, @valor_texto varchar(max), @valor_fecha smalldatetime, @valor_fecha_srt varchar(200)



 if (@p_lista is NOT null)
 begin
  DECLARE C_Cursor CURSOR FOR

   SELECT LN_contrapartes.contraparte, ln_contrapartes.id_padron, 
    country.country_name, padrones.nombre_persona,
    identificadores.abreviatura, padrones.identificacion, 
    LN_familias.nombre_familia

   FROM LN_contrapartes
   INNER JOIN dbo.DelimitedSplit8K(@p_lista, ',') Contrapartes ON LN_contrapartes.contraparte = Contrapartes.Item

   left join LN_gestiones on LN_gestiones.contraparte = LN_contrapartes.contraparte and
                                                     LN_gestiones.desde <= GETDATE() and LN_gestiones.hasta >= GETDATE()
   INNER JOIN padrones ON LN_contrapartes.id_padron = padrones.id_padron
   INNER JOIN identificadores ON padrones.identificador = identificadores.identificador
   INNER JOIN country ON identificadores.country_code = country.country_code
   INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
   INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
   INNER JOIN clientes ON LN_contrapartes.cliente = clientes.cliente
   INNER JOIN users ON LN_contraparte_familias.created_by = users.user_id
   LEFT OUTER JOIN users as users1 ON LN_contraparte_familias.modified_by = users1.user_id
   where LN_contrapartes.cliente = @p_cliente
   and LN_contrapartes.activo = 1
   ORDER BY country.country_name, LN_familias.nombre_familia, padrones.nombre_persona


 end
 else
 begin
  DECLARE C_Cursor CURSOR FOR
   SELECT LN_contrapartes.contraparte, ln_contrapartes.id_padron, 
    country.country_name, padrones.nombre_persona,
    identificadores.abreviatura, padrones.identificacion, 
    LN_familias.nombre_familia

   FROM LN_contrapartes
   left join LN_gestiones on LN_gestiones.contraparte = LN_contrapartes.contraparte and
                                                     LN_gestiones.desde <= GETDATE() and LN_gestiones.hasta >= GETDATE()
   INNER JOIN padrones ON LN_contrapartes.id_padron = padrones.id_padron
   INNER JOIN identificadores ON padrones.identificador = identificadores.identificador
   INNER JOIN country ON identificadores.country_code = country.country_code
   INNER JOIN LN_contraparte_familias ON LN_contrapartes.contraparte = LN_contraparte_familias.contraparte
   INNER JOIN LN_familias ON LN_contraparte_familias.familia = LN_familias.familia
   INNER JOIN clientes ON LN_contrapartes.cliente = clientes.cliente
   INNER JOIN users ON LN_contraparte_familias.created_by = users.user_id
   LEFT OUTER JOIN users as users1 ON LN_contraparte_familias.modified_by = users1.user_id
   where LN_contrapartes.cliente = @p_cliente
   and LN_contrapartes.activo = 1
   ORDER BY country.country_name, LN_familias.nombre_familia, padrones.nombre_persona

 end 
 OPEN C_Cursor
 FETCH NEXT FROM C_Cursor INTO @contraparte, @id_padron, 
          @pais, @nombre, 
          @identificador, @tax_id, 
          @familia
  WHILE @@fetch_status = 0
  BEGIN
  insert into @valores exec ln_docs_valores @contraparte,  'MEDIO_AMBIENTE' 
  insert into @valores exec ln_docs_valores @contraparte,  'MEDIO_AMBIENTE_DETALLE'
  insert into @valores exec ln_docs_valores @contraparte,  'EXPEDIENTE_DISCIPLINARIO'
  insert into @valores exec ln_docs_valores @contraparte,  'EXPEDIENTE_DISCIPLINARIO_CORRECCION' 
  insert into @valores exec ln_docs_valores @contraparte,  'ISO_14001' 
  insert into @valores exec ln_docs_valores @contraparte,  'MEDIDAS_PRESERVACION_MEDIO_AMBIENTE'    
  insert into @valores exec ln_docs_valores @contraparte,  'MEDIDAS_PRESERVACION_MEDIO_AMBIENTE_DETALLE'
  insert into @valores exec ln_docs_valores @contraparte,  'MEDIDAS_SALUD_HIGIENE' 
  insert into @valores exec ln_docs_valores @contraparte,  'MEDIDAS_SALUD_HIGIENE_DETALLE' 
  insert into @valores exec ln_docs_valores @contraparte,  'CONTRATAN_SECTORES_DESFAVORECIDOS'
  insert into @valores exec ln_docs_valores @contraparte,  'FOMENTO_CULTURA'
  insert into @valores exec ln_docs_valores @contraparte,  'FOMENTO_CULTURA_DETALLE'
  
  declare @i int = 1
  while @i <= 12
    begin
     set @campo_nro = @i
     set @campo_id  = case @i
          when 1 then 'MEDIO_AMBIENTE'  
          when 2 then 'MEDIO_AMBIENTE_DETALLE'
          when 3 then 'EXPEDIENTE_DISCIPLINARIO' 
          when 4 then 'EXPEDIENTE_DISCIPLINARIO_CORRECCION'
          when 5 then 'ISO_14001'
          when 6 then 'MEDIDAS_PRESERVACION_MEDIO_AMBIENTE'   
          when 7 then 'MEDIDAS_PRESERVACION_MEDIO_AMBIENTE_DETALLE'
          when 8 then 'MEDIDAS_SALUD_HIGIENE' 
          when 9 then 'MEDIDAS_SALUD_HIGIENE_DETALLE' 
          when 10 then 'CONTRATAN_SECTORES_DESFAVORECIDOS' 
          when 11 then 'FOMENTO_CULTURA' 
          when 12 then 'FOMENTO_CULTURA_DETALLE' 
         end 
     set @nombre_campo = null
     set @etiqueta = null
     set @respuesta = ''
     insert into @resultado values (@contraparte, @pais, @familia, @nombre, @identificador, @tax_id, @campo_id, @campo_nro, @nombre_campo, @etiqueta, isnull(@respuesta,'')) 
     set @i = @i + 1
    end 

  DECLARE C_Resp CURSOR FOR
  select * from @valores where contraparte = @contraparte 
  OPEN C_Resp
   FETCH NEXT FROM C_Resp INTO @cont, @gestion, @doc_id, @seccion, @campo, @tipo_campo, @nombre_campo , @etiqueta ,@fila , @col, @valor_numerico , @valor_texto, @valor_fecha , @valor_fecha_srt 
    WHILE @@fetch_status = 0
    BEGIN
    set @respuesta = null 
    select @campo_id = campo_id from LN_tipo_doc_campos where campo = @campo
    set @campo_nro = case @campo_id
      when 'MEDIO_AMBIENTE'  then 1
      when 'MEDIO_AMBIENTE_DETALLE' then 2
      when 'EXPEDIENTE_DISCIPLINARIO' then 3
      when 'EXPEDIENTE_DISCIPLINARIO_CORRECCION' then 4
      when 'ISO_14001' then 5
      when 'MEDIDAS_PRESERVACION_MEDIO_AMBIENTE'    then 6
      when 'MEDIDAS_PRESERVACION_MEDIO_AMBIENTE_DETALLE' then 7
      when 'MEDIDAS_SALUD_HIGIENE' then 8
      when 'MEDIDAS_SALUD_HIGIENE_DETALLE' then 9
      when 'CONTRATAN_SECTORES_DESFAVORECIDOS' then 10
      when 'FOMENTO_CULTURA' then 11
      when 'FOMENTO_CULTURA_DETALLE' then 12
     end 
    
    EXEC [LN_excel_opciones_RSE] @campo_id , @valor_numerico, @valor_texto, @valor_fecha_srt, @respuesta output
    update @resultado 
    set nombre_campo = @nombre_campo, etiqueta = @etiqueta, resultado = isnull(@respuesta,'')
    where contraparte = @cont 
      and campo_nro = @campo_nro 
    FETCH NEXT FROM C_Resp INTO @cont, @gestion, @doc_id, @seccion, @campo, @tipo_campo, @nombre_campo , @etiqueta ,@fila , @col, @valor_numerico , @valor_texto, @valor_fecha , @valor_fecha_srt 
    END 
  CLOSE C_Resp
  DEALLOCATE C_Resp
  FETCH NEXT FROM C_Cursor INTO @contraparte, @id_padron, 
          @pais, @nombre, 
          @identificador, @tax_id, 
          @familia  
  END 
 CLOSE C_Cursor
 DEALLOCATE C_Cursor
  
 

 select * from @resultado order by contraparte, campo_nro 

END 

/*
select * from ln_contrapartes where contraparte = 107
select contraparte from ln_contrapartes where cliente = 5546348 and id_padron in (select id_padron from padrones where identificacion in 
(
'30711198594',
'30707509992',
'30682335706',
'30709849367',
'30711617813',
'30708614714',
'30698011145',
'30682472142',
'30708954639',
'30709493147',
'30711004552',
'30714908886',
'30714191949',
'33570210659',
'30711663041',
'30708468955',
'30707768971',
'30687288145',
'30688343220',
'30715219731',
'30709928712',
'30708276800',
'30712164952',
'30686273578',
'30709607622',
'30709037508',
'30709771600',
'30521765794',
'30668336597',
'30709641030',
'33697301629',
'30711148147',
'33715730869',
'30681519994',
'33699685459',
'30707314903',
'30710756968',
'33626346389',
'30611030270',
'30710637527',
'30710370571',
'30711743711',
'30709739170',
'30688428846',
'30711370338',
'30714073555',
'33612399889',
'30711085714',
'30709655724',
'30708179244',
'30710694385',
'30710953569',
'30621012041',
'30698491740',
'30714917230',
'30715485768',
'30710235801',
'30711491526',
'30587524348',
'30679636037',
'30709664030',
'33710591429',
'30710687834',
'30710846630',
'33714141789',
'30712553401',
'30710806892',
'30682101802',
'30709449849',
'30701009610',
'30715487507',
'30697280975',
'30709732494',
'30517252847',
'30714928674',
'30709597880',
'30710623992',
'33615248539',
'30694258138',
'30710137982',
'30686508923',
'30712453423',
'30707256172',
'30710053304',
'30630936213',
'30710174667',
'30709157589',
'30712256458',
'30708841532',
'30600176664',
'30707989897',
'30708747862',
'30708236027',
'30707309586',
'30707045066',
'30715379704',
'30586740535',
'30711050716',
'30659980238',
'30689734673',
'30711944970',
'33707563589',
'30575170125',
'30657198400',
'30709418099',
'30707924841',
'30709993182',
'30711033412',
'30692845605',
'30715766562',
'33712329799',
'30711054401',
'33710415639',
'30692302598',
'30707597360',
'30592199544',
'33710164059',
'30567176351',
'30708141646',
'30713391804',
'30660791295',
'30707193170',
'30709699756',
'30714489670',
'30656992235',
'30715255541',
'30708894601',
'30576721710',
'30709457167',
'30702055128',
'30711189692',
'30711171467',
'30708829885',
'30566429191',
'30698482253',
'30598929226',
'30711138532',
'30711516383',
'30712134409',
'30710523467',
'30712400435',
'30711118906',
'30546969874',
'30712280235',
'30709202193',
'30704464076',
'30716250837',
'30714707171',
'30708286431',
'30715757369',
'30698507892',
'30573518477',
'33710103289',
'30698498222',
'30708689064',
'30714387711',
'20255834356',
'20220362427',
'20213207742',
'20175668412',
'20102389175',
'20055232114',
'20315644616',
'20309334249',
'20276723406',
'20161595439',
'20200830351',
'30663483605',
'30692989070',
'33698351719',
'30710687842',
'30710578156',
'30708808632',
'30710516053',
'20208084136',
'20162934008'))

*/

GO

Used By

No items found