dbo.sp_la_estrella

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

1
Name Data Type Length Description
@paso int 4

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
-- Cuando solo se quiere emitir el ultimo archivo de salida, mandar como parametro paso = 1
-- exec [sp_la_estrella] 1
CREATE OR ALTER PROCEDURE dbo.sp_la_estrella   
@paso int = null 

AS 
BEGIN
    SET NOCOUNT ON;
 declare @desde datetime, 
   @hasta datetime
 if (@paso is null) 
 begin
  set @desde = (select fecha from proceso_la_estrella)
  set @hasta = getdate()
  update proceso_la_estrella set fecha = @hasta

  insert into Sujetos_obligados_la_estrella (cod_clae, nombre_persona, identificacion, tipo_persona, tipo_novedad, created) 
  select cod_clae, p.nombre_persona, p.identificacion,
  case (isnull(ltrim(rtrim(p.tipo_persona)),'')) 
    when 'J' then 'J'
    when 'F' then 'F'
    else iif(clae.cuit like '3%','J','F')
  end as tipo_persona,
  'A' as tipo_novedad,
  @hasta 

  from clae 
   inner join padrones p on p.identificacion = cuit and p.habilitado = 1 and p.identificador in (select identificador from identificadores where country_code = 'AR')
   where visible = 1 
     and formulario = 883 
     and clae.cod_clae in (select cod_clae from /*clae_provincia_seguros*/ clae_la_estrella)
     and not exists (select * from datos_personas where datos_personas.c01_cuit = clae.cuit and (defuncion = 1 or fecha_defuncion is not null))
     and created > @desde
 end 
 else
 begin
  set @hasta = (select fecha from proceso_la_estrella)
 end 

 select cod_clae, nombre_persona, identificacion, tipo_persona, tipo_novedad
 from Sujetos_obligados_la_estrella 
 where created = @hasta
    -- Agregado 01/07/2024 
    -- Muestra = 1
    -- Para cada CUIT/CLAE
    -- Ultima fecha en el archivo de altas es nulo y ultimo fecha de bajas es nulo -> QUIERE DECIR QUE NUNCA SE INFORMÓ => Ahora lo muestro
    -- Ultima fecha en el archivo de bajas >= Ultima fecha en el archivo de altas -> QUIERE DECIR QUE SE DIO DE BAJA => Ahora lo muestro
       and 
  (select
  iif((MAX(altas.created) is null and MAX(bajas.created) is null) 
   or 
   (isnull(MAX(bajas.created),'1900-01-01') >= isnull(MAX(altas.created),'1900-01-01'))
   , 1,0) from Sujetos_obligados_la_estrella altas
  left join Sujetos_obligados_la_estrella_bajas bajas on (bajas.identificacion = altas.identificacion and bajas.cod_clae = altas.cod_clae)
  where altas.identificacion = Sujetos_obligados_la_estrella.identificacion 
    and altas.cod_clae = Sujetos_obligados_la_estrella.cod_clae
    and altas.created < @hasta) = 1
  -- Fin agregado 01/07/2024 -- 
 order by id


END 


/*
----------------
-- Para BAJAS --
----------------
sp_la_estrella_bajas  (se puede mandar una fecha como parametro, si se quieren las bajas de los registros informados despues de una fecha)
Antes hay que pasar esos registros por AFIP, para poder tener los Clae actualizados 
*/


/*
select * from clae_la_estrella
inner join ramos on ramos.cod_subramo = cod_clae
*/

GO

Used By

No items found