dbo.Subcontratistas_enriquecer_

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

3
Name Data Type Length Description
@accion varchar 20
@cliente int 4
@Fecha date 3

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/* Se corre todos los dias con el insert_masivo
- se cargan las altas del dia anterior
- el dia 1 de cada mes se cargan los que cumplen 3 meses
*/

/*
exec [Subcontratistas_enriquecer_] 'INSERT',5546348
exec [Subcontratistas_enriquecer_] 'LISTAR',5546348,'2024-12-01'
exec [Subcontratistas_enriquecer_] 'LISTAR_FULL',5546348,'2025-01-01'

exec [Subcontratistas_enriquecer_] 'PROCESAR',5546348
select subcontratista, contratista, fecha_actualizacion, count(*) 
from Subcontratistas_enriquecer  
group by subcontratista, contratista, fecha_actualizacion
having count(*) > 1
*/

CREATE OR ALTER PROCEDURE dbo.Subcontratistas_enriquecer_
    @accion  VARCHAR(20) = 'LISTAR',
    @cliente INT  = 5546348, 
 @Fecha   DATE = NULL --> En la acción LISTAR se va a listar los que tengan actualización 
       --  en el mes de la fecha que se manda como parámetro
       --  en INSERT se toma la fecha de ayer
AS
BEGIN

 if (@accion = 'INSERT' or @Fecha is null) SET @Fecha = getdate()
 DECLARE @Ayer            DATE = DATEADD(DAY,-1, @Fecha)
 DECLARE @inicioMes       DATE = DATEADD(DAY, 1, EOMONTH(@Fecha, -1))
 DECLARE @finMes          DATE = EOMONTH(@Fecha)
 DECLARE @inicioTresMeses DATE = dateadd(m,-3,@inicioMes)
 DECLARE @finTresMeses    DATE = EOMONTH(@inicioTresMeses)
 --select @inicioMes, @finMes, @inicioTresMeses, @finTresMeses

 if (@accion = 'INSERT')
 begin
  -- Todas las altas de ayer 
  insert into [Subcontratistas_enriquecer] (cliente, subcontratista, contratista, fecha_actualizacion)
  select distinct a.cliente, a.contraparte, a.id_padre, @Fecha
  from  ln_contrapartes a
  inner join ln_contrapartes b on b.contraparte = a.id_padre
  inner join padrones p on a.id_padron = p.id_padron  
  where a.cliente = @cliente
  and a.activo = 1
  and a.subcontratista = 1
  and a.country_code = 'AR'
  and len(p.identificacion) = 11 
  and a.vigencia_hasta >= @inicioMes
  and b.activo = 1
  and convert(date,a.created) = @Ayer 
  -- Que ya no lo hayamos cargado
  and not exists 
    (select * FROM [Subcontratistas_enriquecer] s2 
      where s2.subcontratista = a.contraparte 
        and s2.contratista = a.id_padre
     and s2.fecha_actualizacion = @Fecha)

  -- El 1 de cada mes: Los que cumplieron 3 meses de la última actualización y siguen activos / vigentes 
  if (@Fecha = @inicioMes) 
  begin
   insert into [Subcontratistas_enriquecer] (cliente, subcontratista, contratista, fecha_actualizacion)
   select s.cliente, s.subcontratista, s.contratista, @Fecha
   FROM [Subcontratistas_enriquecer] s
   inner join ln_contrapartes a on a.contraparte = s.subcontratista and a.activo = 1 and a.vigencia_hasta >= @inicioMes
   inner join ln_contrapartes b on b.contraparte = s.contratista    and b.activo = 1
    where s.cliente = @cliente and s.fecha_actualizacion between @inicioTresMeses and @finTresMeses
     
    -- 05/02/2025: Que no tenga una actualización de menos de 3 meses 
    and not exists (select * from [Subcontratistas_enriquecer] s1 
                     where s1.cliente = s.cliente and s1.subcontratista = s.subcontratista and s1.fecha_actualizacion > @finTresMeses) 

    -- Que ya no lo hayamos cargado
    and not exists 
    (select * FROM [Subcontratistas_enriquecer] s2 
      where s2.subcontratista = s.subcontratista 
        and s2.contratista = s.contratista 
     and s2.fecha_actualizacion = @Fecha) 
  end 

  -- Los mandamos a consultar_afip para que este todo actualizado, 
  -- principalmente las personas físicas que no se hace nada manualmente (pueden estar fallecidas)
  insert into consultar_afip (proceso, fecha, prioridad, procesado, cuit) 
   select 'SUBCAMX',@Fecha,1,0,p.identificacion 
     FROM [Subcontratistas_enriquecer] s
    inner join ln_contrapartes a on a.contraparte = s.subcontratista 
    inner join padrones p on a.id_padron = p.id_padron  
    where s.fecha_actualizacion = @fecha 
 
 end 

 if (@accion = 'LISTAR')
 begin
  select distinct p.nombre_persona as 'nombre', p.identificacion as 'cuit'
  FROM [Subcontratistas_enriquecer] s
  inner join ln_contrapartes a on a.contraparte = s.subcontratista 
  inner join padrones p  on a.id_padron = p.id_padron 
  where s.cliente = @cliente and s.fecha_actualizacion between @inicioMes and @finMes
 end 


 if (@accion = 'LISTAR_FULL')
 begin


  declare @distintos int = (select count(distinct p.identificacion)
         FROM [Subcontratistas_enriquecer] s
         inner join ln_contrapartes a on a.contraparte = s.subcontratista 
         inner join padrones p  on a.id_padron = p.id_padron 
         where s.cliente = @cliente and s.fecha_actualizacion between @inicioMes and @finMes)


  select distinct @distintos as cantidad_a_facturar, 
  s.contratista, po.nombre_persona as 'nombre_contratista', po.identificacion as 'cuit_contratista', 
  s.subcontratista, p.nombre_persona as 'nombre_subcontratista', p.identificacion as 'cuit_subcontratista', 
  a.vigencia_desde, a.vigencia_hasta,
  iif(convert(date,a.created) between @inicioMes and @finMes,'ALTA','ACTUALIZACION') as tipo, 
  s.fecha_actualizacion, 

  (select max(fecha_actualizacion) from [Subcontratistas_enriquecer] s2 
    where s2.cliente = @cliente and s2.contratista = s.contratista 
      and s2.subcontratista = s.subcontratista
      and s2.fecha_actualizacion < @inicioMes) as actualizacion_anterior
  
  FROM [Subcontratistas_enriquecer] s
  inner join ln_contrapartes a on a.contraparte = s.subcontratista 
  inner join ln_contrapartes padre on padre.contraparte = s.contratista

  inner join padrones p  on a.id_padron = p.id_padron 
  inner join padrones po  on po.id_padron = padre.id_padron 

  where s.cliente = @cliente and s.fecha_actualizacion between @inicioMes and @finMes

  order by p.nombre_persona, po.nombre_persona
 end 



 if (@accion = 'PROCESAR')
 begin
  -- Todos los que siguen activos / vigentes 
  select distinct s.cliente, p.identificacion as cuit 
  FROM [Subcontratistas_enriquecer] s
  inner join ln_contrapartes a on a.contraparte = s.subcontratista and a.activo = 1 and a.vigencia_hasta >= @inicioMes
  inner join padrones p  on a.id_padron = p.id_padron 
  where s.cliente = @cliente and p.tipo_persona != 'F'
 end 

END

  --select * from Subcontratistas_enriquecer order by fecha_actualizacion desc
GO