dbo.subcontratistas_amx_

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
*/

CREATE OR ALTER PROCEDURE dbo.subcontratistas_amx_  

AS
BEGIN
    SET NOCOUNT ON;
 declare @cuit_contratista varchar(20),
   @contraparte_contratista int,
   @cuit_subcontratista varchar(20),
   @contraparte_subcontratista int,
   @tipo_servicio varchar(10),
   @familia_subcontratista int,
   @familia_en_base int,
   @vigente_desde date,
   @vigente_hasta date,
   @fecha_carga date,
   @fecha_modificado date,
   @fecha_baja date,
   @fecha_actualizado_sql date, 
   @id int 

 declare @i int = 0
 declare @J int = 0
 declare @k int = 0

 declare @id_padron int = null
 declare @contraparte_nueva int

 DECLARE C_Cursor CURSOR FOR
  select  cuit_contratista,
    contraparte_contratista,
    cuit_subcontratista,
    contraparte_subcontratista,
    tipo_servicio,
    familia_subcontratista,
    familia_en_base,
    vigente_desde,
    vigente_hasta,
    fecha_carga,
    fecha_modificado,
    fecha_baja,
    fecha_actualizado_sql, id 
    from subcontratistas_amx 
   where fecha_actualizado_sql is  null
     and ((/*len(cuit_contratista) = 11 and*/ len(cuit_subcontratista) = 11)
            or cuit_contratista like 'CHE%')
     --select * from subcontratistas_amx where contraparte_contratista is null
     --select * from users where user_id = 94437923

 OPEN C_Cursor
 FETCH NEXT FROM C_Cursor INTO 
        @cuit_contratista,
        @contraparte_contratista,
        @cuit_subcontratista,
        @contraparte_subcontratista,
        @tipo_servicio,
        @familia_subcontratista,
        @familia_en_base,
        @vigente_desde,
        @vigente_hasta,
        @fecha_carga,
        @fecha_modificado,
        @fecha_baja,
        @fecha_actualizado_sql, 
        @id

 WHILE @@fetch_status = 0
 BEGIN 
  set @i = @i + 1
  
  if (@familia_subcontratista is null) 
  begin 
   if (@tipo_servicio = 'S') set @familia_subcontratista = 37
   if (@tipo_servicio = 'M') set @familia_subcontratista = 61
   if (@tipo_servicio = 'A') set @familia_subcontratista = 62
   update subcontratistas_amx 
      set familia_subcontratista = @familia_subcontratista 
    where id = @id
  end 

  if (@contraparte_contratista is null)
  begin
   set @id_padron = null
   set @id_padron = (select top 1 id_padron from padrones 
                      where identificacion = @cuit_contratista and habilitado = 1)
      if (@id_padron is not null and 
      (select count(*) from ln_contrapartes 
       where cliente = 5546348 
    and id_padron = @id_padron
    and subcontratista = 0 
    and activo = 1) = 1)
   begin
    set @contraparte_contratista = (select contraparte from ln_contrapartes 
             where cliente = 5546348 
               and id_padron = @id_padron
               and subcontratista = 0
               and activo = 1)
    update subcontratistas_amx 
     set contraparte_contratista = @contraparte_contratista 
     where id = @id
   end 
  end

  if (@contraparte_contratista is not null and @contraparte_subcontratista is null)
  begin
   set @id_padron = null
   set @id_padron = (select top 1 id_padron from padrones 
                      where identificacion = @cuit_subcontratista and habilitado = 1)
      if (@id_padron is not null and 
    (select count(*) from ln_contrapartes 
    where cliente = 5546348 
    and id_padron = @id_padron
    and subcontratista = 1
    and activo = 1
    and id_padre = @contraparte_contratista) = 1)
   begin
    set @contraparte_subcontratista =  (select contraparte from ln_contrapartes 
              where cliente = 5546348 
                and id_padron = @id_padron
                and subcontratista = 1
                and activo = 1
                and id_padre = @contraparte_contratista)
    update subcontratistas_amx 
       set contraparte_subcontratista = @contraparte_subcontratista 
     where id = @id
   end 
  end

  if (@contraparte_contratista is not null and @contraparte_subcontratista is not null)
  begin
   if (select count(*) from  ln_contrapartes 
       where cliente   = 5546348
       and contraparte = @contraparte_subcontratista
       and id_padre    = @contraparte_contratista
       and subcontratista = 1 
       and activo = 1) = 1 
   begin
       SET @j = @j + 1
    if (@fecha_baja is not null) set  @vigente_hasta = @fecha_baja
    
    update ln_contrapartes 
       set vigencia_desde =  @vigente_desde, 
        vigencia_hasta  = @vigente_hasta,
        modified        = convert(date,getdate()), 
        modified_by     = 527975456 -- user marita
        where cliente   = 5546348
        and contraparte = @contraparte_subcontratista
        and id_padre    = @contraparte_contratista
        and subcontratista = 1 
        and activo = 1 
                
    set @familia_en_base = (select familia from ln_contraparte_familias 
                             where contraparte = @contraparte_subcontratista)
    if (@familia_en_base != @familia_subcontratista) begin
     update LN_contraparte_familias 
        set familia = @familia_subcontratista ,
            modified = convert(date,getdate()), 
            modified_by = 527975456 -- user marita
      where contraparte = @contraparte_subcontratista
    end

    update subcontratistas_amx 
       set familia_en_base = @familia_en_base,
           fecha_actualizado_sql = convert(date,getdate()),
        accion = 'UPDATE'
     where id = @id 

   end 
  end 

  if (@contraparte_contratista is not null and @contraparte_subcontratista is null)
  begin
   set @id_padron = null
   set @contraparte_nueva = null 
   set @id_padron = (select top 1 id_padron from padrones 
                      where identificacion = @cuit_subcontratista and habilitado = 1)
   if (@id_padron is not null)
   begin
   SET @k = @k + 1
   
   if (@fecha_baja is not null) set  @vigente_hasta = @fecha_baja

   insert into ln_contrapartes 
      (id_padron, cliente, country_code, activo, clave,
      clave_temporal,
      clave_cambio,
      clave_expira,
      created,created_by,
      vigencia_desde, vigencia_hasta,
      carga_inicial, subcontratista,id_padre)
      values 
      (@id_padron, '5546348', 'AR',1,'amx123456',
      0,
      NULL,
      '2030-12-31 00:00:00',
      convert(date,getdate()), 527975456, -- user marita
      @vigente_desde, @vigente_hasta,
      '0', '1', @contraparte_contratista)

    SET @contraparte_nueva = SCOPE_IDENTITY()
    --select @contraparte_nueva 

    insert into ln_gestiones(cliente, contraparte, desde,hasta, vigente, created, created_by)
    values ('5546348',@contraparte_nueva, '2020-03-01', '2099-12-31',1, convert(date,getdate()), 527975456) -- user marita

    insert into ln_contraparte_familias (contraparte, familia,estado_homologacion, activo, created, created_by)
    values (@contraparte_nueva, @familia_subcontratista, 'N', 1, convert(date,getdate()), 527975456) -- user marita

    update subcontratistas_amx 
       set fecha_actualizado_sql = convert(date,getdate()), 
           contraparte_subcontratista = @contraparte_nueva, 
        accion = 'INSERT'
     where id = @id 
   end
  end 
  FETCH NEXT FROM C_Cursor INTO 
        @cuit_contratista,
        @contraparte_contratista,
        @cuit_subcontratista,
        @contraparte_subcontratista,
        @tipo_servicio,
        @familia_subcontratista,
        @familia_en_base,
        @vigente_desde,
        @vigente_hasta,
        @fecha_carga,
        @fecha_modificado,
        @fecha_baja,
        @fecha_actualizado_sql, 
        @id 

 END  
 CLOSE C_Cursor
 DEALLOCATE C_Cursor
 select @i as leidos, @j as actualizados, @k as altas 
END
GO

Used By

No items found