dbo.obtener_estado_de_resultados_contables

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

3
Name Data Type Length Description
@p_Cuit varchar 20
@p_todos varchar 1
@ids varchar 100

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
obtener_estado_de_resultados_contables_test '30501677643'
obtener_estado_de_resultados_contables null,null,'366137,366138'
*/


CREATE OR ALTER PROCEDURE dbo.obtener_estado_de_resultados_contables
@p_Cuit varchar(20), 
@p_todos varchar(1) = 'N',
@ids varchar(100) = NULL   
AS

BEGIN
    SET NOCOUNT ON;
    SET ANSI_PADDING ON
    SET ANSI_WARNINGS ON
    SET CONCAT_NULL_YIELDS_NULL ON 

  /*
         SELECT top 5 convert(varchar(10),balances_fide.fecha_balance,103) as fecha_balance,
          isnull(res_vtanet,0) as ventas_netas,isnull(res_compras,0) as compras_del_ejercicio,res_cosmer,
          (isnull(res_vtanet,0) + isnull(res_cosmer,0)) as resultado_bruto,
   ((isnull(res_gascom,0) + isnull(res_gasadm,0) + isnull(res_gasfin,0)) / isnull(res_vtanet,0)) as grafico_gastos_totales,
          isnull(amortiza,0) as amortizaciones,res_gascom, res_gasadm,isnull(res_gasfin,0) as gastos_financieros,isnull(res_otros,0) as otros,isnull(res_resinv,0) as resultado_inversiones_perma,
          isnull(res_ie,0) as otros_ingresos_y_egresos,isnull(res_otrosing,0) as otros_ingresos, isnull(res_otrosegr,0) as otros_egresos,res_resfin,isnull(res_iva,0) as impuesto_a_las_ganancias,
          (balances_fide.res_vtanet + isnull(res_cosmer,0) + isnull(res_gascom,0) +isnull(res_gasadm,0) +isnull(res_gasfin,0) +isnull(res_otros,0)+isnull(res_resinv,0) +isnull(res_ie,0)+isnull(res_otrosing,0) +isnull(res_otrosegr,0) +isnull(res_resfin,0) +isnull(res_iva,0)) as ganancias
          from balances_fide where cuit = @p_cuit and datediff(YY,fecha_balance,getdate()) < 7 and visible = 1 ORDER BY FECHA_BALANCE DESC
   */


if(ISNULL(@ids,'') != '')
BEGIN

  set @ids  = '0,'+@ids 
 /*
   declare @data xml
   declare @temp table(id int)
   select @data = '<t>' + replace(@ids, ',', '</t><t>') + '</t>'
   insert into @temp
           select t.c.value('.', 'int') as id
           from @data.nodes('t') as t(c)
   */

  SELECT convert(varchar(10),bc.fecha_balance,103) as fecha_balance,bc.fecha_balance as fecha_order,
  isnull(bc.res_vtanet,0) as res_vtanet , 
  isnull(bc.res_compras,0) as res_compras , 
  isnull(bc.res_cosmer,0) as res_cosmer , 
  isnull(bc.amortiza,0) as amortiza , 
  isnull(bc.res_gasfin,0) as res_gasfin , 
  isnull(bc.res_iva,0) as res_iva , 
  isnull(bc.res_otrosegr,0) as res_otrosegr , 
  isnull(bc.res_otrosing,0) as res_otrosing , 
  isnull(bc.res_gascom,0) as res_gascom , 
  isnull(bc.res_gasadm,0) as res_gasadm , 
  isnull(bc.ganancia,0) as ganancia ,  
  isnull(bc.res_resfin,0) as res_resfin,
  monedas.nombre_moneda
  from balances_contables as bc inner join monedas on bc.moneda = monedas.moneda 
   inner join dbo.DelimitedSplit8K(@ids, ',') lista on bc.id_balance= lista.Item
 where -- id_balance in (SELECT id FROM @temp) AND 
  isnull(bc.visible,0) = 1
  AND isnull(estado,'') = 'PUBLICADO'
  order by fecha_order 

END
ELSE 
BEGIN
  declare @p_top int = 5 
   if (@p_todos = 'S') begin set @p_top = 6 end 
   SELECT top (@p_top)  
    bc.estado as estado,convert(varchar(10),bc.fecha_balance,103) as fecha_balance,bc.fecha_balance as fecha_order,
  isnull(bc.res_vtanet,0) as res_vtanet , 
  isnull(bc.res_compras,0) as res_compras , 
  isnull(bc.res_cosmer,0) as res_cosmer , 
  isnull(bc.amortiza,0) as amortiza , 
  isnull(bc.res_gasfin,0) as res_gasfin , 
  isnull(bc.res_iva,0) as res_iva , 
  isnull(bc.res_otrosegr,0) as res_otrosegr , 
  isnull(bc.res_otrosing,0) as res_otrosing , 
  isnull(bc.res_gascom,0) as res_gascom , 
  isnull(bc.res_gasadm,0) as res_gasadm , 
  isnull(bc.ganancia,0) as ganancia ,  
  isnull(bc.res_resfin,0) as res_resfin,
  monedas.nombre_moneda

  from balances_contables as bc  inner join monedas on bc.moneda = monedas.moneda 
  where bc.cuit = @p_cuit and bc.fecha_balance >= convert(date,dateadd(yy, -6, getdate()))
  AND (@p_todos = 'S' or isnull(bc.visible,0) = 1)
  AND isnull(estado,'') != 'ELIMINADO'
  order by fecha_order desc, id_balance desc
END 


END
--'30501677643'

GO

Used By

No items found