dbo.backend_morosidad

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_meses smallint 2
@p_visible bit 1

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

CREATE OR ALTER PROCEDURE dbo.backend_morosidad
@p_Cuit varchar(20),
@p_meses smallint,
@p_visible bit


AS
BEGIN
    SET NOCOUNT ON;
 DECLARE @p_5anios smalldatetime = getdate() - 1825
 DECLARE @p_2anios smalldatetime = getdate() - 730
   
 DECLARE @from_date date=CAST(DATEADD(month, (-1)*(@p_meses), GETDATE()) AS DATE)

 begin
 
  select morosidad.*,
  estado = case morosidad.vigente
  when 1 then 'ACTIVO' 
  when 0 then 'INACTIVO' 
  end,
  
  right('00'+(cast(datepart(DD,fecha) as varchar)),2)+'/'+right('00'+(cast(datepart(MM,fecha) as varchar)),2)+'/'+cast(DATEPART(YY,fecha) as varchar) as fecha_dmy, 
  
  Fuentes.fue_nombre as entidad, 
  Eventos_morosidad.cau_nombre as cau_nombre,
  Eventos_morosidad.tipo as tipo,

  iif ((
        (fecha < @p_5anios) 
        OR 
     (fecha < @p_2anios 
      and 
      exists (select * from morosidad_cancela c 
      where c.iden = morosidad.iden 
        and cast(c.codigo_ent as int) = cast(morosidad.codigo_ent as int) 
        and fecha between c.fec_inf_desde and c.fec_inf_hasta
        and c.vigente = '1'))
         ),'INACTIVO POR HABEAS DATA','DENTRO DEL PLAZO DE HABEAS DATA') as habeas_data, 
  
  RIGHT('             ' + Ltrim(Rtrim(convert(char(13),monto_original))),13) as monto_1,
     RIGHT('             ' + Ltrim(Rtrim(convert(char(13),monto_act))),13) as monto_2

  
   from morosidad

  left join Fuentes
    on Fuentes.fue_codigo = morosidad.codigo_ent
  
  left join Eventos_morosidad
    on Eventos_morosidad.cau_codigo = morosidad.cau_codigo

  where iden    = @p_Cuit
    and fecha   > @from_date
    -- and vigente = @p_visible
    and 
    (
        (@p_visible = '1' and morosidad.vigente = '1' 
         and ((morosidad.fecha >= @p_5anios and not exists (select * from morosidad_cancela c 
                 where c.iden = morosidad.iden 
                 and cast(c.codigo_ent as int) = cast(morosidad.codigo_ent as int) 
                 and fecha between c.fec_inf_desde and c.fec_inf_hasta
                 and c.vigente = '1'))
     or (morosidad.fecha >= @p_2anios and exists (select * from morosidad_cancela c 
                 where c.iden = morosidad.iden 
                 and cast(c.codigo_ent as int) = cast(morosidad.codigo_ent as int) 
                 and fecha between c.fec_inf_desde and c.fec_inf_hasta
                 and c.vigente = '1'))
         )
     )

   or (@p_visible = '0' and 
       morosidad.vigente = '0' 
          or morosidad.fecha < @p_5anios 
       or (morosidad.fecha < @p_2anios and exists (select * from morosidad_cancela c 
                 where c.iden = morosidad.iden 
                 and cast(c.codigo_ent as int) = cast(morosidad.codigo_ent as int) 
                 and fecha between c.fec_inf_desde and c.fec_inf_hasta
                 and c.vigente = '1'))
   ) 
   )
    
     
  
  order by morosidad.codigo_ent, morosidad.fecha desc
 end

END

GO