dbo.sp_balances_2007_grupos_empleados

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
-- [sp_balances_2007_grupos_actualiza] '30695556531'
CREATE OR ALTER PROCEDURE dbo.sp_balances_2007_grupos_empleados 
AS
BEGIN
    SET NOCOUNT ON;
 declare @id int, 
         @cuit varchar(20),
   @fecha_balance date,
   @desde char(4), 
   @hasta char(4),
   @anio int,
   @tabla1 varchar(100), 
   @tabla2 varchar(100), 
   @periodo1 int, 
   @periodo2 int,
   @cant_empleados int, 
   @sueldos decimal(20,2)
 /* UPDATE balances_por_grupo 
  set cant_empleados = null, sueldos = null, empleados_periodo = null
      where fecha_balance between '2007-01-01' and '2007-12-31'*/

 DECLARE C_balances CURSOR FOR
  select id, cuit, fecha_balance
    from balances_por_grupo 
    --where cuit = '30709723274'
    --where id > 16470
   /*-- para año 2011 -- */
   where fecha_balance between '2007-01-01' and '2007-12-31'
   order by id

 OPEN C_balances
 FETCH NEXT FROM C_balances INTO @id, @cuit, @fecha_balance
 WHILE @@fetch_status = 0
 BEGIN
 
  set @anio  = DATEPART(year,@fecha_balance) 

  /* -- para año 2010 -- */
  --if (@anio = 2010) set @anio = '2009'
  
  set @desde = convert(varchar,@anio-2000) + '01'
  set @hasta = convert(varchar,@anio-2000) + '12'
  set @tabla1 = null
  set @tabla2 = null
  set @periodo1 = null
  set @periodo2 = null

  select @tabla1 = 'empleados', 
      @periodo1 = max(periodo)
   from empleados 
   where cuit = @cuit
   and periodo between @desde and @hasta 
   and CONVERT(VARCHAR,periodo) not like '%12' 
   and CONVERT(VARCHAR,periodo) not like '%06'

  select @tabla2 = 'historico', 
    @periodo2 = max(periodo) 
   from empleados_historico 
   where cuit = @cuit 
   and periodo between @desde and @hasta 
   and CONVERT(VARCHAR,periodo) not like '%12' 
   and CONVERT(VARCHAR,periodo) not like '%06'

     --select @periodo1, @periodo2
     if (@periodo1 is null and @periodo2 is null) 
  begin
    
   set @anio  = DATEPART(year,@fecha_balance)+1 
   set @desde = convert(varchar,@anio-2000) + '01'
   set @hasta = convert(varchar,@anio-2000) + '12'
   set @tabla1 = null
   set @tabla2 = null
   set @periodo1 = null
   set @periodo2 = null

   select @tabla1 = 'empleados', 
       @periodo1 = max(periodo)
    from empleados 
    where cuit = @cuit
    and periodo between @desde and @hasta 
    and CONVERT(VARCHAR,periodo) not like '%12' 
    and CONVERT(VARCHAR,periodo) not like '%06'

   select @tabla2 = 'historico', 
     @periodo2 = max(periodo) 
    from empleados_historico 
    where cuit = @cuit 
    and periodo between @desde and @hasta 
    and CONVERT(VARCHAR,periodo) not like '%12' 
    and CONVERT(VARCHAR,periodo) not like '%06'

   --select @periodo1, @periodo2
   if (@periodo1 is null and @periodo2 is null) 
   begin
     FETCH NEXT FROM C_balances INTO @id, @cuit, @fecha_balance
     continue 
   end 
  end 

  if (@periodo1 is not null)
  begin
     
   set @cant_empleados = (select count(distinct cuil) from empleados where cuit = @cuit and periodo = @periodo1)
   set @sueldos        = (select sum(remimp) from empleados  a
                           where a.cuit = @cuit 
         and a.periodo = @periodo1
         and not exists (select * from empleados b
                  where b.cuit = a.cuit 
                 and b.cuil = a.cuil
              and b.periodo = a.periodo
              and b.id_empleado > a.id_empleado))  
          /*
             select @cant_empleados = count(cuil),
                           @sueldos = sum(remimp) 
           from empleados a 
              where a.cuit = @cuit 
            and a.periodo = @periodo1
                and id_empleado  in (select id_empleado 
                                       from empleados b
              where b.cuit = a.cuit 
            and b.cuil = a.cuil
            and b.periodo = a.periodo
            and b.created = (select max(created) 
                              from empleados c
                             where b.cuit = c.cuit 
                               and b.cuil = c.cuil
                               and b.periodo = c.periodo)
             )
                     */

     update balances_por_grupo
     set cant_empleados = @cant_empleados, sueldos = @sueldos, empleados_periodo = @periodo1 where id = @id 
  end
  else
  begin
      
   set @cant_empleados = (select count(distinct cuil) from empleados_historico where cuit = @cuit and periodo = @periodo2)
   set @sueldos        = (select sum(remimp) from empleados_historico  a
                           where a.cuit = @cuit 
         and a.periodo = @periodo2
         and not exists (select * from empleados_historico b
                  where b.cuit = a.cuit 
                 and b.cuil = a.cuil
              and b.periodo = a.periodo
              and b.id_empleado > a.id_empleado)) 
    /*
       select @cant_empleados = count(cuil),
                     @sueldos = sum(remimp) 
     from empleados_historico a 
        where a.cuit = @cuit 
      and a.periodo = @periodo2
          and id_empleado  in (select id_empleado 
                                 from empleados_historico b
        where b.cuit = a.cuit 
      and b.cuil = a.cuil
      and b.periodo = a.periodo
      and b.created = (select max(created) 
                        from empleados_historico c
                       where b.cuit = c.cuit 
                         and b.cuil = c.cuil
                         and b.periodo = c.periodo)
       )
               */

     update balances_por_grupo
     set cant_empleados = @cant_empleados, sueldos = @sueldos, empleados_periodo = @periodo2 where id = @id 
  end 

  FETCH NEXT FROM C_balances INTO @id, @cuit, @fecha_balance
 END  
 CLOSE C_Balances
 DEALLOCATE C_Balances

  
END 
GO

Used By

No items found