dbo.backend_central_deudores_temp

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_central_deudores_temp
@p_Cuit varchar(20),
@p_meses smallint,
@p_visible bit

/*
- si se quieren todos los registros @p_visible null
- si se piden los visibles (vigente=1), tiene que fijarse que no exista otro registro con la misma entidad/mes con 
  id_fuente = '2' (son los que cargan los usuarios por staff, si despues viene del Bcra tiene, ese del bcra no lo tiene que tomar)
- si se piden los no visibles -> vigente=0, o que exista otro registro con la misma entidad/mes visible con id_fuente = '2'
*/


AS
BEGIN
    SET NOCOUNT ON;
 declare @p_5anios date = getdate() - (5*365)
 declare @p_2anios date = getdate() - (2*365)
 declare @p_3meses date = getdate() - (90) 

 declare @dtStart date, @dtEnd date, @dtEnd_cuit date
 
 select @dtEnd = (select max(fecha_informacion) from fuentes_procesadas where visible = '1')

 select @dtEnd_cuit = convert(date,isnull(max(cendeu.fec_inf),'200112')+'01') from cendeu_temp cendeu where iden = @p_cuit

 if (@dtEnd_cuit > @dtEnd) 
  begin 
     set @dtEnd = @dtEnd_cuit
  end 

 set @dtStart = dateadd(month, (-1)*(@p_meses-1), @dtEnd)

 
 
 begin
 
  select --cendeu.*,
  cendeu.id_cendeu,
  cendeu.codigo_ent,
  cendeu.fec_inf,
  cendeu.tipo,
  cendeu.iden,
  cendeu.actividad, 
  substring(convert(char(2),convert(int,cendeu.situacion)),1,1) as situacion,  
  cendeu.prestamos,
  cendeu.particip,
  cendeu.garotor,
  cendeu.o_cptos,
  cendeu.garprefa,
  cendeu.garprefb,
  cendeu.singarpref,
  cendeu.cgarprefa,
  cendeu.cgarprefb,
  cendeu.sincgarpre,
  cendeu.previ,
  cendeu.deuda_cubr,
  cendeu.proc_jud,
  cendeu.refinanc,
  cendeu.rec_oblig,
  cendeu.sit_juridi,
  cendeu.irre_dispt,
  cendeu.dia_atraso,
  cendeu.vigente,
  cendeu.modified,
  cendeu.id_persona,
  cendeu.deuda_total,
  cendeu.modified_by,
  cendeu.observaciones,
  cendeu.id_fuente,
  cendeu.dbf_filename,
  cendeu.created,
  visible = iif(cendeu.vigente='0' or exists (select * from cendeu_temp b where b.iden = cendeu.iden and cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int) and b.fec_inf = cendeu.fec_inf and b.vigente = '1' and b.id_fuente = '2' and b.id_cendeu != cendeu.id_cendeu),'INACTIVO','ACTIVO')
  , cast(cendeu.codigo_ent as int) as bco_codigo, 
  right('00'+(cast(datepart(MM,cast(fec_inf+'01' as date)) as varchar)),2)+'/'+cast(DATEPART(YY,cast(fec_inf+'01' as date)) as varchar) as periodo_deuda, 
  (deuda_total+prestamos+particip+garotor+o_cptos) as total, EntidadesBancarias.nombre, 
  iif ((
        (cast(fec_inf+'01' as date) < @p_5anios) 
        OR
        (cast(fec_inf+'01' as date) < @p_2anios 
       and 
    --not (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    not exists (select * from cendeu_temp c where c.iden = cendeu.iden and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,c.created) > @p_3meses)))
     OR 
     (cast(fec_inf+'01' as date) < @p_2anios 
      and 
      exists (select * from cendeu_cancela c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         ),'INACTIVO POR HABEAS DATA','DENTRO DEL PLAZO DE HABEAS DATA') as habeas_data
   from cendeu_temp cendeu
  inner join EntidadesBancarias
    on id_banco = cast(cendeu.codigo_ent as int) 
  
  where iden = @p_Cuit
  
   and fec_inf in (select convert(varchar(4),datepart(year,dateadd(month, number, @dtStart))) + right('0' + rtrim(datepart(MONTH,dateadd(month, number, @dtStart))),2) as periodo
       from 
       (select distinct number from master.dbo.spt_values
        where name is null) n
       where dateadd(month, number, @dtStart) <= @dtEnd)
  /* and vigente = @p_visible */ 
   and 
   (
   (@p_visible is null) 
   or (
   (@p_visible = '1' and
    (vigente = '1' and not exists (select * from cendeu_temp b where b.iden = cendeu.iden and cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int) and b.fec_inf = cendeu.fec_inf and b.vigente = '1' and b.id_fuente = '2' and b.id_cendeu != cendeu.id_cendeu)
    and not (
        (cast(fec_inf+'01' as date) < @p_5anios) 
        OR
        (cast(fec_inf+'01' as date) < @p_2anios 
       and 
    --not (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    not exists (select * from cendeu_temp c where c.iden = cendeu.iden and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,c.created) > @p_3meses)))
     OR 
     (cast(fec_inf+'01' as date) < @p_2anios 
      and 
      exists (select * from cendeu_cancela c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
         )
    )
    )
   or 
   (@p_visible = '0' and 
   (vigente = '0' OR 
        (exists (select * from cendeu_temp b where b.iden = cendeu.iden and cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int) and b.fec_inf = cendeu.fec_inf and b.vigente = '1' and b.id_fuente = '2' and b.id_cendeu != cendeu.id_cendeu))
        OR 
     (
        (cast(fec_inf+'01' as date) < @p_5anios) 
        OR
        (cast(fec_inf+'01' as date) < @p_2anios 
       and 
    --not (convert(date,created) > @p_3meses and convert(date,modified) > @p_3meses))
    not exists (select * from cendeu_temp c where c.iden = cendeu.iden and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) and (convert(date,c.created) > @p_3meses)))
     OR 
     (cast(fec_inf+'01' as date) < @p_2anios 
      and 
      exists (select * from cendeu_cancela c 
      where c.iden = cendeu.iden 
        and cast(c.codigo_ent as int) = cast(cendeu.codigo_ent as int) 
        and cast(fec_inf+'01' as date) between convert(date,c.fec_inf_desde) and convert(date,c.fec_inf_hasta)
        and c.vigente = '1'))
        )
   ))
   ))
  
  and not exists (select * from cendeu_temp b 
                   where b.iden = cendeu.iden
       and  cast(b.codigo_ent as int) = cast(cendeu.codigo_ent as int)
       and  b.fec_inf = cendeu.fec_inf
       and  b.vigente = cendeu.vigente
       and (isnull(b.id_fuente,1)) = (isnull(cendeu.id_fuente,1))
       and  b.id_cendeu < cendeu.id_cendeu)
        
  order by cast(cendeu.codigo_ent as int) asc, cendeu.fec_inf desc

  
 end

END

GO

Used By

No items found