dbo.backend_central_deudores

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
/*


exec backend_central_deudores '27350890861',50,1


*/

CREATE OR ALTER PROCEDURE dbo.backend_central_deudores
@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_2anios date = getdate() - (5*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 fuente = 'cendeu' and 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 

 select @dtEnd_cuit = convert(date,isnull(max(cendeu.fec_inf),'200112')+'01') from 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

 --- Primero: Si hay datos en ceudeu_temp, los pasa a cendeu y los borra de cendeu temp
  insert into cendeu 
             (codigo_ent,
     fec_inf,
     tipo,
     iden,
     id_persona,
     actividad,
     situacion,
     prestamos,
     particip,
     garotor,
     o_cptos,
     garprefa,
     garprefb,
     singarpref,
     cgarprefa,
     cgarprefb,
     sincgarpre,
     previ,
     deuda_cubr,
     proc_jud,
     refinanc,
     rec_oblig,
     sit_juridi,
     irre_dispt,
     dia_atraso,
     id_fuente,
     vigente,
     created,
     created_by,
     modified,
     modified_by,
     dbf_filename,
     deuda_total,
     observaciones)
  select   codigo_ent,
     fec_inf,
     tipo,
     iden,
     id_persona,
     actividad,
     substring(convert(char(2),convert(int,situacion)),1,1) as situacion, 
     --situacion,
     prestamos,
     particip,
     garotor,
     o_cptos,
     garprefa,
     garprefb,
     singarpref,
     cgarprefa,
     cgarprefb,
     sincgarpre,
     previ,
     deuda_cubr,
     proc_jud,
     refinanc,
     rec_oblig,
     sit_juridi,
     irre_dispt,
     dia_atraso,
     id_fuente,
     vigente,
     created,
     created_by,
     modified,
     modified_by,
     dbf_filename,
     deuda_total,
     observaciones
   from cendeu_temp 
  where iden = @p_Cuit 

  delete from cendeu_temp where iden = @p_Cuit 
 ----------------
 
  select --cendeu.*,
  distinct cendeu.id_cendeu,
  cendeu.codigo_ent,
  cendeu.fec_inf,
  cendeu.tipo,
  cendeu.iden,
  cendeu.actividad, 
  --cendeu.situacion,
  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,
        cendeu.vigente as idVigente,
  visible = iif(cendeu.vigente='0' or exists (select * from cendeu 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 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
  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 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 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 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 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 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