dbo.balances_reporte_14012022

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

4
Name Data Type Length Description
@desde int 4
@cliente int 4
@servicio int 4
@sector int 4

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
exec balances_reporte 2017, 5546348,47,114
*/

CREATE OR ALTER PROCEDURE dbo.balances_reporte_14012022  
@desde int = 2017,
@cliente int = null, 
@servicio int = null, 
@sector int = null 
AS
BEGIN
    SET NOCOUNT ON;
 declare @cuit varchar(20)
 declare @nombre varchar(500)
 declare @anio int 
 declare @rentabilidad decimal(20,2)
 declare @clae int  = null
 declare @descripcion varchar(500) = null 
 declare @sectorn int
 declare @sectord varchar(500)
 declare @hasta int = @desde + 4
 declare @fecha_desde date = convert(date,convert(char(4),@desde) + '-01-01') 
 declare @fecha_hasta date = convert(date,convert(char(4),@hasta) + '-12-31')

 declare @anio1 int = @desde
 declare @anio2 int = @anio1 + 1
 declare @anio3 int = @anio1 + 2
 declare @anio4 int = @anio1 + 3
 declare @anio5 int = @anio1 + 4

 declare @reporte table (cuit varchar(100), nombre varchar(500), clae int, descripcion varchar(500),
                         sectorn int, sectord varchar(500), 
                         anio1 int, rentabilidad1 decimal(20,2),
                         anio2 int, rentabilidad2 decimal(20,2),
                         anio3 int, rentabilidad3 decimal(20,2),
                         anio4 int, rentabilidad4 decimal(20,2),
                         anio5 int, rentabilidad5 decimal(20,2))

if (@cliente is null and @servicio is null)
begin
 DECLARE C_cursor CURSOR FOR
  select cuit, /*fecha_balance,*/ year(fecha_balance) as anio, nombre_persona, 
   --iif(res_vtanet!=0,(ganancia / res_vtanet) * 100,0) as rentabilidad,
   ind_utilfin 
   from balances_contables
   inner join padrones on (padrones.identificacion = balances_contables.cuit and habilitado = 1 ) 
   where balances_contables.visible = 1 and estado  = 'PUBLICADO' 
   and fecha_balance between @fecha_desde and @fecha_hasta 
   and bal_manifestacion = 'B'
end
else
begin 
 DECLARE C_cursor CURSOR FOR

  select cuit, /*fecha_balance,*/ year(fecha_balance) as anio, nombre_persona, 
   --iif(res_vtanet!=0,(ganancia / res_vtanet) * 100,0) as rentabilidad,
   ind_utilfin 
   from balances_contables
   inner join padrones on (padrones.identificacion = balances_contables.cuit and habilitado = 1 ) 
   
   inner join ln_contrapartes on padrones.id_padron = ln_contrapartes.id_padron 
   inner join ln_gestiones on ln_gestiones.contraparte = ln_contrapartes.contraparte
   inner join ln_contraparte_familias on ln_contraparte_familias.contraparte = ln_contrapartes.contraparte
   inner join ln_familias on ln_familias.familia = ln_contraparte_familias.familia
   
   where  balances_contables.visible = 1 and estado  = 'PUBLICADO' 
   and fecha_balance between @fecha_desde and @fecha_hasta 
   and bal_manifestacion = 'B'

   and ln_contrapartes.cliente = @cliente and ln_contrapartes.activo = 1 
   and ln_gestiones.desde <= getdate() and ln_gestiones.hasta >= getdate() and ln_gestiones.vigente = 1
   and ln_familias.visible = 1
   and ln_contrapartes.servicio_contratado = @servicio  
end 
 OPEN C_cursor
 FETCH NEXT FROM C_cursor INTO @cuit, @anio, @nombre, @rentabilidad
 WHILE @@fetch_status = 0
 BEGIN

  /*
   if  @cliente is not null 
   begin 
   if not exists (select *
   from ln_contrapartes 
   inner join padrones on padrones.id_padron = ln_contrapartes.id_padron 
   inner join ln_gestiones on ln_gestiones.contraparte = ln_contrapartes.contraparte
   inner join ln_contraparte_familias on ln_contraparte_familias.contraparte = ln_contrapartes.contraparte
   inner join ln_familias on ln_familias.familia = ln_contraparte_familias.familia
   where ln_contrapartes.cliente = @cliente and ln_contrapartes.activo = 1 
   and ln_gestiones.desde <= getdate() and ln_gestiones.hasta >= getdate() and ln_gestiones.vigente = 1
   and ln_familias.visible = 1
   and identificacion = @cuit and ln_contrapartes.servicio_contratado = @servicio) 
   begin 
   FETCH NEXT FROM C_cursor INTO @cuit, @anio, @nombre, @rentabilidad
   continue
   end 
   end
   */


  select top 1 
    @clae = cod_clae,   
    @descripcion = clae.descripcion,
    @sectorn = r1.cod_ramo, 
    @sectord = r2.descripcion 
   from clae
   left join ramos r1 on r1.cod_subramo = clae.cod_clae 
   left join ramos r2 on (r2.cod_ramo = r1.cod_ramo and r2.tipo_ramo = 'R')
   where clae.cuit = @cuit
   and clae.visible = 1
   and clae.descripcion is not null
   and clae.formulario is not null -- son las que se cargaron actualizadas de afip
   order by principal desc
  
  if (@sector is not null and @sector != isnull(@sectorn,0))
   begin 
    FETCH NEXT FROM C_cursor INTO @cuit, @anio, @nombre, @rentabilidad
    continue
   end 

  if (@anio = @anio1) 
  begin
  if not exists (select * from @reporte where cuit = @cuit)
   insert into @reporte (cuit, nombre, anio1, rentabilidad1,clae,descripcion,sectorn,sectord) 
   values (@cuit,@nombre,@anio1,@rentabilidad,@clae,@descripcion,@sectorn,@sectord)
  else 
   update @reporte set anio1 = @anio1, rentabilidad1 = @rentabilidad 
   where cuit = @cuit
  end 

  if (@anio = @anio2) 
  begin
  if not exists (select * from @reporte where cuit = @cuit)
   insert into @reporte (cuit, nombre, anio2, rentabilidad2,clae,descripcion,sectorn,sectord) 
   values (@cuit,@nombre,@anio2,@rentabilidad,@clae,@descripcion,@sectorn,@sectord)
  else 
   update @reporte set anio2 = @anio2, rentabilidad2 = @rentabilidad 
   where cuit = @cuit
  end 

  if (@anio = @anio3) 
  begin
  if not exists (select * from @reporte where cuit = @cuit)
   insert into @reporte (cuit, nombre, anio3, rentabilidad3,clae,descripcion,sectorn,sectord) 
   values (@cuit,@nombre,@anio3,@rentabilidad,@clae,@descripcion,@sectorn,@sectord)
  else 
   update @reporte set anio3 = @anio3, rentabilidad3 = @rentabilidad 
   where cuit = @cuit
  end 
   
  if (@anio = @anio4) 
  begin
  if not exists (select * from @reporte where cuit = @cuit)
   insert into @reporte (cuit, nombre, anio4, rentabilidad4,clae,descripcion,sectorn,sectord) 
   values (@cuit,@nombre,@anio4,@rentabilidad,@clae,@descripcion,@sectorn,@sectord)
  else 
   update @reporte set anio4 = @anio4, rentabilidad4 = @rentabilidad 
   where cuit = @cuit
  end 

  if (@anio = @anio5) 
  begin
  if not exists (select * from @reporte where cuit = @cuit)
   insert into @reporte (cuit, nombre, anio5, rentabilidad5,clae,descripcion,sectorn,sectord) 
   values (@cuit,@nombre,@anio5,@rentabilidad,@clae,@descripcion,@sectorn,@sectord)
  else 
   update @reporte set anio5 = @anio5, rentabilidad5 = @rentabilidad 
   where cuit = @cuit
  end 
        
  FETCH NEXT FROM C_cursor INTO @cuit, @anio, @nombre, @rentabilidad
 END
 CLOSE C_cursor
 DEALLOCATE C_cursor

 select * from @reporte
END
GO

Used By

No items found