IF OBJECT_ID('dbo.sp_fco_crea_tablas_ala') IS NOT NULL 
   DROP PROCEDURE dbo.sp_fco_crea_tablas_ala
GO
CREATE PROCEDURE dbo.sp_fco_crea_tablas_ala
(
   @pch_compania char(4),
   @psi_id_calendario smallint
)
AS
BEGIN
   DECLARE 
      @vsi_anio smallint,
      @vti_mes tinyint,
      @vbi_sociedades bit,      
      @vsy_tabla sysname,
      @vch_sociedad char(10),
      @vch_tipo_nom char(2)
      
    SELECT @vsi_anio = anio, @vti_mes = mes_acumular, @vch_tipo_nom = tipo_nomina
    FROM calendario_procesos
    WHERE id_calendario = @psi_id_calendario
    
    -- SALUD
    SET @vsy_tabla = 'FCO_EPS_' + RTRIM(@pch_compania) + '_' + RTRIM(@vsi_anio) + '_' + RTRIM(@vti_mes)
    IF OBJECT_ID(@vsy_tabla) IS NULL
    BEGIN
     DELETE FCO_EPS
     EXECUTE ('SELECT * INTO ' + @vsy_tabla + ' FROM FCO_EPS')
     EXECUTE ('GRANT ALL ON ' + @vsy_tabla + ' TO PUBLIC')
    END
    
    -- PENSION
    SET @vsy_tabla = 'FCO_FPO_' + RTRIM(@pch_compania) + '_' + RTRIM(@vsi_anio) + '_' + RTRIM(@vti_mes)
    IF OBJECT_ID(@vsy_tabla) IS NULL
    BEGIN
     DELETE FCO_FPO
     EXECUTE ('SELECT * INTO ' + @vsy_tabla + ' FROM FCO_FPO')
     EXECUTE ('GRANT ALL ON ' + @vsy_tabla + ' TO PUBLIC')
    END
    
    -- RIESGOS
    SET @vsy_tabla = 'FCO_ARP_' + RTRIM(@pch_compania) + '_' + RTRIM(@vsi_anio) + '_' + RTRIM(@vti_mes)
    IF OBJECT_ID(@vsy_tabla) IS NULL
    BEGIN
     DELETE FCO_ARP
     EXECUTE ('SELECT * INTO ' + @vsy_tabla + ' FROM FCO_ARP')
     EXECUTE ('GRANT ALL ON ' + @vsy_tabla + ' TO PUBLIC')
    END
    
    -- ISS CONSOLIDADO
    IF EXISTS (SELECT 1 FROM reportes_ala WHERE maneja_ISS = 1 AND ISS_Integrado = 0)
    BEGIN
     SET @vsy_tabla = 'FCO_VW_ISS_' + RTRIM(@pch_compania) + '_' + RTRIM(@vsi_anio) + '_' + RTRIM(@vti_mes)
     IF OBJECT_ID(@vsy_tabla) IS NULL
     BEGIN
        DELETE FCO_VW_ISS
        EXECUTE ('SELECT * INTO ' + @vsy_tabla + ' FROM FCO_VW_ISS')
        EXECUTE ('GRANT ALL ON ' + @vsy_tabla + ' TO PUBLIC')
     END         
    END
    
    -- ISS INTEGRADO
    IF EXISTS (SELECT 1 FROM reportes_ala WHERE maneja_ISS = 1 AND ISS_Integrado = 1)
    BEGIN
     SET @vsy_tabla = 'FCO_ISS_' + RTRIM(@pch_compania) + '_' + RTRIM(@vsi_anio) + '_' + RTRIM(@vti_mes)
     IF OBJECT_ID(@vsy_tabla) IS NULL
     BEGIN
        DELETE FCO_ISS
        EXECUTE ('SELECT * INTO ' + @vsy_tabla + ' FROM FCO_ISS')
        EXECUTE ('GRANT ALL ON ' + @vsy_tabla + ' TO PUBLIC')
     END                  
    END      

END
GO
GRANT EXECUTE ON dbo.sp_fco_crea_tablas_ala TO PUBLIC
GO

------------- cut here ------ seņale hasta aqui y ejecute -----------------------------------


--'Agregar esto al cierre de Nomina justo antes de Borrar las Transacciones_NS'

 /*** JCL. OCT.24/2005.  CREAR TABLAS DE AUTOLIQUIDACION PARA EL MES Y AŅO DEL CALENDARIO ***/
-- EXEC sp_fco_crea_tablas_ala @chcompania, @siid_calendario

