dbo.Imperium_contratista_Estado_Ratificacion

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

1
Name Data Type Length Description
@p_contratista int 4

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE OR ALTER PROCEDURE dbo.Imperium_contratista_Estado_Ratificacion 
 -- Add the parameters for the stored procedure here

 /*
  Dado un contratista principal chequea que tenga ratificadas todas las nóminas
  de recursos activos en el período vencido, empleados, vehículos, subcontratistas y recursos
  de sus subcontratistas
 
  El SP devuelve:
  0: tiene pendiente de ratificar en el período vencido
  1: nóminas ratificadas en el período vencido
  -1: el contratista no está ingresado en el período vencido
 
  06/02/18 : cambio en la lógica. No se chequea la existencia de recursos en el período, sólo devuelve -1
  cuando el contratista no está en el período. Esto es porque siempre se tiene que ratificar la nómina,
  haya o no recursos afectados.
 
 
  */



 @p_contratista        int = NULL

 
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;

    -- Insert statements for procedure here
 DECLARE @estado_ratificacion INT = -1

 DECLARE @total_ratificacion INT = 0

 IF (@p_contratista > 0) BEGIN

  IF EXISTS (SELECT contratista, periodo 
     FROM led_contratista_periodo
     WHERE contratista = @p_contratista     
     AND periodo = dbo.PeriodoVencido() ) BEGIN

   DECLARE @empleados  int
   /*EXEC Imperium_vista_contratista_Pendientes_Ratificar @p_contratista, @p_contratista, 'E', @empleados output*/
   IF EXISTS (SELECT ISNULL(ratif_empleados,0)
      from led_contratista_periodo
      where contratista = @p_contratista
      and periodo = dbo.PeriodoVencido()) BEGIN

    SELECT @empleados = ISNULL(ratif_empleados,0)
      from led_contratista_periodo
      where contratista = @p_contratista
      and periodo = dbo.PeriodoVencido()

   END   
   SET @total_ratificacion = @total_ratificacion + @empleados

   DECLARE @vehiculos  int
   /*EXEC Imperium_vista_contratista_Pendientes_Ratificar @p_contratista, @p_contratista, 'V', @vehiculos output*/
   IF EXISTS (SELECT ISNULL(ratif_vehiculos,0)
      from led_contratista_periodo
      where contratista = @p_contratista
      and periodo = dbo.PeriodoVencido()) BEGIN

    SELECT @vehiculos = ISNULL(ratif_vehiculos,0)
      from led_contratista_periodo
      where contratista = @p_contratista
      and periodo = dbo.PeriodoVencido()

   END
   SET @total_ratificacion = @total_ratificacion + @vehiculos
   
   DECLARE @subcontratistas  int
   /*EXEC Imperium_vista_contratista_Pendientes_Ratificar @p_contratista, @p_contratista, 'S', @subcontratistas output*/
   IF EXISTS (SELECT ISNULL(ratif_subcontratistas,0)
      from led_contratista_periodo
      where contratista = @p_contratista
      and periodo = dbo.PeriodoVencido()) BEGIN

    SELECT @subcontratistas = ISNULL(ratif_subcontratistas,0)
      from led_contratista_periodo
      where contratista = @p_contratista
      and periodo = dbo.PeriodoVencido()

   END
   SET @total_ratificacion = @total_ratificacion + @subcontratistas

   DECLARE @rec_subcontratistas  int
   /*EXEC Imperium_vista_contratista_Pendientes_Ratificar @p_contratista, @p_contratista, 'R', @rec_subcontratistas output*/
   IF EXISTS (SELECT ISNULL(ratif_rec_subcontratistas,0)
      from led_contratista_periodo
      where contratista = @p_contratista
      and periodo = dbo.PeriodoVencido()) BEGIN

    SELECT @rec_subcontratistas = ISNULL(ratif_rec_subcontratistas,0)
      from led_contratista_periodo
      where contratista = @p_contratista
      and periodo = dbo.PeriodoVencido()

   END
   SET @total_ratificacion = @total_ratificacion + @rec_subcontratistas

   
   IF (@total_ratificacion = 4) 
    SET @estado_ratificacion = 1
   ELSE
    SET @estado_ratificacion = 0


   /*
   
   
    /* debe tener recursos cargados en el período */

    IF ( EXISTS (SELECT empleado FROM led_empleados_periodo
    WHERE contratista_1 = @p_contratista
    AND periodo = dbo.PeriodoVencido()) OR
   
    EXISTS (SELECT vehiculo FROM led_vehiculos_periodo
    WHERE contratista_1 = @p_contratista
    AND periodo = dbo.PeriodoVencido()) OR
   
    EXISTS (SELECT subcontratista FROM led_subcontratista_periodo
    WHERE contratista_1 = @p_contratista
    AND periodo = dbo.PeriodoVencido()) ) BEGIN
   
   
    /*
 asumo todo ratificado */
    SET @estado_ratificacion = 1
   
    DECLARE @empleados  int
    EXEC Imperium_vista_contratista_Pendientes_Ratificar @p_contratista, @p_contratista, 'E', @empleados output
   
    SET @total_ratificacion = @total_ratificacion + @empleados
   
    IF (@empleados >= 0) BEGIN
    SET @estado_ratificacion = (@estado_ratificacion & @empleados)
    END
   
   
   
    DECLARE @vehiculos  int
    EXEC Imperium_vista_contratista_Pendientes_Ratificar @p_contratista, @p_contratista, 'V', @vehiculos output
   
    SET @total_ratificacion = @total_ratificacion + @vehiculos
   
    IF (@vehiculos >= 0) BEGIN
    SET @estado_ratificacion = (@estado_ratificacion & @vehiculos)
    END
   
   
   
    DECLARE @subcontratistas  int
    EXEC Imperium_vista_contratista_Pendientes_Ratificar @p_contratista, @p_contratista, 'S', @subcontratistas output
   
    SET @total_ratificacion = @total_ratificacion + @subcontratistas
   
    IF (@subcontratistas >= 0) BEGIN
    SET @estado_ratificacion = (@estado_ratificacion & @subcontratistas)
    END
   
   
    DECLARE @subcontratista INT
   
    DECLARE QSubc CURSOR FOR
    SELECT led_subcontratista_periodo.subcontratista,
    ISNULL(led_subcontratista_periodo.ratif_empleados, 0) AS ratif_empleados,
    ISNULL(led_subcontratista_periodo.ratif_vehiculos, 0) AS ratif_vehiculos
    FROM led_subcontratista_periodo
    INNER JOIN led_contratistas ON led_subcontratista_periodo.subcontratista = led_contratistas.contratista
    WHERE led_subcontratista_periodo.contratista_1 = @p_contratista
    AND led_subcontratista_periodo.periodo = dbo.PeriodoVencido()
    AND led_contratistas.estado_contratista = 'A'
   
   
    OPEN QSubc
   
    FETCH NEXT FROM QSubc INTO @subcontratista, @empleados, @vehiculos
   
    WHILE (@@FETCH_STATUS = 0) BEGIN
   
    EXEC Imperium_vista_contratista_Pendientes_Ratificar @p_contratista, @subcontratista, 'E', @empleados output
   
    IF (@empleados >= 0) BEGIN
    SET @estado_ratificacion = (@estado_ratificacion & @empleados)
    END
   
   
   
    EXEC Imperium_vista_contratista_Pendientes_Ratificar @p_contratista, @subcontratista, 'V', @vehiculos output
   
    IF (@vehiculos >= 0) BEGIN
    SET @estado_ratificacion = (@estado_ratificacion & @vehiculos)
    END
   
   
    FETCH NEXT FROM QSubc INTO @subcontratista, @empleados, @vehiculos
    END
   
    CLOSE QSubc
    DEALLOCATE QSubc
   
    END */


  END


  select @estado_ratificacion as estado_ratificacion

 END
 
END

GO

Used By

No items found