SET QUOTED_IDENTIFIER OFF
IF OBJECT_ID('dbo.sp_fco_crea_vw_iss') IS NOT NULL
    DROP PROCEDURE dbo.sp_fco_crea_vw_iss
GO
CREATE PROCEDURE dbo.sp_fco_crea_vw_iss
(                            
    @pch_compania char(4),
    @psi_anio smallint,
    @pti_mes tinyint,
    @pvc_suc varchar(10),
    @pbi_generar_mm bit,
    @pbi_regenerar bit
)                            
/**********************************************************************/
/*** Modulo:      Modulo Fiscal Colombiano Version FCO - ADAM V.3   ***/
/*** Descripcion: Crea tabla de autoliquidacion de aportes al ISS   ***/
/***              Metodologia GALA - Autol                          ***/
/*** Autor:       Juan Carlos Leguizamon                            ***/
/*** Fecha MF:    Julio 10 de 2001                                  ***/
/*** Fecha FCO:   Marzo 27 de 2006                                  ***/
/**********************************************************************/
AS                            
DECLARE                             
    @vch_acum_imp char(15),
    @vch_acum_tie char(15),
    
    @vch_periodo char(8),
    
    @vch_agr_02 char(5), /* IBC de Pension */
    @vch_agr_03 char(5), /* Cotizacion Obligatoria de Pension */
    @vch_agr_04 char(5), /* Fondo de Solidaridad Pensional */
    @vch_agr_05 char(5), /* Cotizacion Obligatoria de Salud */
    @vch_agr_08 char(5), /* Vacaciones */
    @vch_agr_09 char(5), /* Cotizacion Obligatoria a Riesgos Profesionales */
    @vch_agr_10 char(5), /* Licencia de Maternidad */
    @vch_agr_11 char(5), /* Incapacidad por Enfermedad General */
    @vch_agr_12 char(5), /* Incapacidad por Accidente de Trabajo */
    @vch_agr_16 char(5), /* IBC de Salud */
    @vch_agr_17 char(5), /* IBC Riesgos Profesionales */
    @vch_agr_17i char(5), /* IBC Riesgos Profesionales Caso Especial Grasco -Usar IBC De Salud- */    
    @vch_agr_18 char(5), /* Licencia No Remunerada y Sanciones */
    
    @vsy_tabla sysname,
    @vbi_existe bit,
    @vvc_sql varchar(3000),
    
    @vvc_cond_0 varchar(255), --WHERE
    @vvc_cond_1 varchar(255), --AND
    @vvc_cond_2 varchar(255), --AND
    @vvc_cond_3 varchar(255),  --AND
    
    @vbi_arp_iss bit,
    @vbi_iss_redondea bit,
    @vvc_control_suc varchar(10),
    @cdc_ap_riesgos decimal(9,0),
    
    @cch_sucursal char(10),
    @cvc_nro_aut varchar(10),
    @cdc_vlr_aut decimal(9,0),
    
    @vch_random char(4),
    @vdc_total_suc decimal(15,0),
    
    @vvc_iss_eps varchar(10), /* Código del ISS como EPS de los trabajadores en ADAM */
    @vvc_iss_afp varchar(10), /* Código del ISS como AFP de los trabajadores en ADAM */

    @vbi_vac_salud bit,
    @vbi_vac_todos bit,
    @vti_mes_ant tinyint,
    @vsi_anio_ant smallint

BEGIN                            
    /* Campos que interesan de la tabla Acumulados Mensuales (importe y tiempo) */
    SELECT @vch_acum_imp = 'acum_imp_mes_' + Right('00' + RTrim(@pti_mes), 2),
           @vch_acum_tie = 'acum_tie_mes_' + Right('00' + RTrim(@pti_mes), 2)
                            
    /* Período de acumulados */
    SET @vch_periodo = "'" + RTrim(@psi_anio) + Right('00' + RTrim(@pti_mes), 2) + "'"
    
    /* Control del codigo de la sucursal */
    SET @vvc_control_suc = IsNull(@pvc_suc, '0')
    IF Len(@vvc_control_suc) = 0 SET @vvc_control_suc = '0'
                            
    /*** Recuperar Agrupaciones de Conceptos ***/
    SELECT @vch_agr_02 = "'" + dbo.fn_fco_parametro_agr(@pch_compania, 'autoliq', 02) + "'",
           @vch_agr_03 = "'" + dbo.fn_fco_parametro_agr(@pch_compania, 'autoliq', 03) + "'",
           @vch_agr_04 = "'" + dbo.fn_fco_parametro_agr(@pch_compania, 'autoliq', 04) + "'",
           @vch_agr_05 = "'" + dbo.fn_fco_parametro_agr(@pch_compania, 'autoliq', 05) + "'",
           @vch_agr_08 = "'" + dbo.fn_fco_parametro_agr(@pch_compania, 'autoliq', 08) + "'",
           @vch_agr_09 = "'" + dbo.fn_fco_parametro_agr(@pch_compania, 'autoliq', 09) + "'",
           @vch_agr_10 = "'" + dbo.fn_fco_parametro_agr(@pch_compania, 'autoliq', 10) + "'",
           @vch_agr_11 = "'" + dbo.fn_fco_parametro_agr(@pch_compania, 'autoliq', 11) + "'",
           @vch_agr_12 = "'" + dbo.fn_fco_parametro_agr(@pch_compania, 'autoliq', 12) + "'",
           @vch_agr_16 = "'" + dbo.fn_fco_parametro_agr(@pch_compania, 'autoliq', 16) + "'",
           @vch_agr_17 = "'" + dbo.fn_fco_parametro_agr(@pch_compania, 'autoliq', 17) + "'",
           @vch_agr_18 = "'" + dbo.fn_fco_parametro_agr(@pch_compania, 'autoliq', 18) + "'"
    
    /* Recueperar los códigos del ISS como EPS y AFP */
    SELECT @vvc_iss_eps = dato FROM vw_fdt_parametros x
    WHERE x.pais = 'COL' AND x.compania = @pch_compania AND x.parametro = 'otros' AND x.secuencia = 11
    
    SET @vvc_iss_eps = IsNull(@vvc_iss_eps, 'ISS')
    
    SELECT @vvc_iss_afp = dato FROM vw_fdt_parametros x
    WHERE x.pais = 'COL' AND x.compania = @pch_compania AND x.parametro = 'otros' AND x.secuencia = 10
    
    SET @vvc_iss_afp = IsNull(@vvc_iss_afp, 'ISS')
    
    /*** Saber si el ISS es la ARP de la compañia ***/
    SELECT @vbi_arp_iss = arp_iss, @vbi_iss_redondea = iss_redondea
    FROM fco_cias_datos_adi
    WHERE compania = @pch_compania
    
    SET @vbi_arp_iss = IsNull(@vbi_arp_iss, 0)

    IF @vbi_arp_iss = 1
        IF EXISTS (SELECT 1 FROM vw_fdt_parametros WHERE pais = 'COL' AND compania = @pch_compania AND parametro = 'otros' AND secuencia = 12 AND dato = 'SI')
            -- Caso Especial para Grasco, se utiliza el IBC de Salud en lugar del de Riesgos
            SET @vch_agr_17i = @vch_agr_16
        ELSE
            SET @vch_agr_17i = @vch_agr_17

    -- Si el ISS no es la ARP averiguar si la empresa quiere reportar vacaciones y de que forma
    IF @vbi_arp_iss = 0
    BEGIN
        SELECT @vbi_vac_salud = 0, @vbi_vac_todos = 0

        IF EXISTS (SELECT 1 FROM vw_fdt_parametros WHERE pais = 'COL' AND compania = @pch_compania AND parametro = 'otros' AND secuencia = 13 AND dato = 'SI')
            SELECT @vbi_vac_salud = 1, @vbi_vac_todos = 0

        IF EXISTS (SELECT 1 FROM vw_fdt_parametros WHERE pais = 'COL' AND compania = @pch_compania AND parametro = 'otros' AND secuencia = 14 AND dato = 'SI')
            SELECT @vbi_vac_salud = 0, @vbi_vac_todos = 1    
    END

    /*** Establecer el Nombre de la Tabla de trabajo ***/
    SET @vsy_tabla = 'FCO_VW_ISS_' + RTrim(@pch_compania) + '_' + RTrim(@psi_anio) + '_' + RTrim(@pti_mes)
    
    SET @vbi_existe = 0
    /*** Verificar Existencia de la Tabla ***/
    IF OBJECT_ID(@vsy_tabla) IS NOT NULL
        SET @vbi_existe = 1
    ELSE
        SET @vbi_existe = 0
         
    /*** Borrar datos de la tabla modelo ***/                        
    DELETE FCO_VW_ISS                            
                            
    IF @vbi_existe = 0
    BEGIN
        /*** Crear y dar permisos a la tabla de autoliquidacion ***/
        EXECUTE ('SELECT * INTO dbo.' + @vsy_tabla + ' FROM FCO_VW_ISS')  
        /** La siguiente línea causa error cuando los usuarios no son dbowner **/
        /** sin embargo las tablas de autoliquidacion se crearan desde el cierre */
        /** por lo tanto está linea no debería ejecutarse nunca (solo en salida en vivo) */
        EXECUTE ('GRANT SELECT, UPDATE, DELETE, INSERT ON dbo.' + @vsy_tabla + ' TO PUBLIC')
    END
    
    EXEC ('SELECT COUNT(*) cuantos INTO ##tmpcuantosVWISS FROM ' + @vsy_tabla)
    IF (SELECT cuantos FROM ##tmpcuantosVWISS) = 0 SET @vbi_existe = 0  
    DROP TABLE ##tmpcuantosVWISS

    IF @pbi_regenerar = 1
    BEGIN                            
        IF @vvc_control_suc <> '0'                            
            SELECT @vvc_cond_0 = " WHERE COD_SUC = '" + @pvc_suc + "'", -- DELETE e INSERT en depurada
                   @vvc_cond_1 = " AND SUC.dato = '" + @pvc_suc + "'", -- Insert General
                   @vvc_cond_2 = " AND COD_SUC = '" + @pvc_suc + "'", -- Novedades
                   @vvc_cond_3 = " AND SUC = '" + @pvc_suc + "'" -- Fantasmas
        ELSE
            SELECT @vvc_cond_0 = '', @vvc_cond_1 = '', @vvc_cond_2 = '', @vvc_cond_3 = ''

        EXECUTE ('DELETE FROM ' + @vsy_tabla + @vvc_cond_0)                            
    END                            
    ELSE                            
        SELECT @vvc_cond_0 = '', @vvc_cond_1 = '', @vvc_cond_2 = '', @vvc_cond_3 = ''
                            
    IF @vbi_existe = 0 Or @pbi_regenerar = 1                            
    BEGIN                            
        /* Insertar los valores No Depurados a la tabla modelo (FCO_VW_ISS) */
        SET @vvc_sql =
            " INSERT INTO FCO_VW_ISS" +
            " SELECT am.compania, tg.trabajador," +
            " CASE WHEN itr.es_eps = 1 THEN 'ISS' ELSE 'Otro' END, " +
            " CASE WHEN itr.es_afp = 1 THEN 'ISS' ELSE 'Otro' END, suc.dato," +
            " tid.tipo_id As C10A, t.clave_unica As C10B, t.nombre As C10C," +
            " ' ' C11A, ' ' C11B, ' ' C11C, 0 C11D, 0 C11E,"+
            " C12A = IsNull(CASE am.agr_conceptos WHEN Case When itr.es_eps = 1 Then " + @vch_agr_16 + " Else " + @vch_agr_02 + " End THEN " + @vch_acum_tie + " ELSE 0 END,0)," +
            " C13A = IsNull(CASE am.agr_conceptos WHEN Case When itr.es_eps = 1 Then " + @vch_agr_16 + " Else " + @vch_agr_02 + " End THEN Round((" + @vch_acum_imp + "-1)/1000,0)*1000 ELSE 0 END,0)," +
            CASE WHEN @vbi_iss_redondea = 1 THEN
                " C14A = Case When itr.es_afp = 0 Then 0 Else IsNull(CASE am.agr_conceptos WHEN " + @vch_agr_03 + " THEN Round((" + @vch_acum_imp + "-1)/100,0)*100 ELSE 0 END,0) End," +
                " C14B = Case When itr.es_afp = 0 Then 0 Else IsNull(CASE am.agr_conceptos WHEN " + @vch_agr_04 + " THEN Round((" + @vch_acum_imp + "-1)/100,0)*100 ELSE 0 END,0) End," +
                " C14C = Case When itr.es_eps = 0 Then 0 Else IsNull(CASE am.agr_conceptos WHEN " + @vch_agr_05 + " THEN Round((" + @vch_acum_imp + "-1)/100,0)*100 ELSE 0 END,0) End,"
            ELSE
                " C14A = Case When itr.es_afp = 0 Then 0 Else IsNull(CASE am.agr_conceptos WHEN " + @vch_agr_03 + " THEN " + @vch_acum_imp + " ELSE 0 END,0) End," +
                " C14B = Case When itr.es_afp = 0 Then 0 Else IsNull(CASE am.agr_conceptos WHEN " + @vch_agr_04 + " THEN " + @vch_acum_imp + " ELSE 0 END,0) End," +
                " C14C = Case When itr.es_eps = 0 Then 0 Else IsNull(CASE am.agr_conceptos WHEN " + @vch_agr_05 + " THEN " + @vch_acum_imp + " ELSE 0 END,0) End,"
            END +
            " ' ' C25N, 0 C25C, ' ' C26N, 0 C26C" +
            " FROM trabajadores t" +
            " INNER JOIN trabajadores_grales tg ON t.trabajador = tg.trabajador" +
            " INNER JOIN vw_fco_iss_trabajador itr ON itr.compania = tg.compania AND itr.trabajador = tg.trabajador" +
            " INNER JOIN vw_fco_div_trabajador suc ON suc.compania = tg.compania AND suc.trabajador = tg.trabajador" +
            " INNER JOIN acumulados_mensuales am ON am.compania = tg.compania AND am.trabajador = tg.trabajador" +
            " INNER JOIN vw_fco_tid_trabajador tid ON tg.compania = tid.compania AND tg.trabajador = tid.trabajador" +
            " WHERE am.compania = '" + @pch_compania + "'" + 
              " AND am.anio = " + RTrim(@psi_anio) +
              " AND Convert(char(6), tg.fecha_ingreso, 112) <= " + @vch_periodo +
              " AND (tg.fecha_baja IS NULL OR Convert(char(6), tg.fecha_baja, 112) >= " + @vch_periodo + ")" +
              " AND am.agr_conceptos IN (" + @vch_agr_02 + "," + @vch_agr_03 + "," + @vch_agr_04 + "," + @vch_agr_05 + "," + @vch_agr_16 + ")" + 
            @vvc_cond_1
        EXECUTE (@vvc_sql)
        IF @@ERROR <> 0 RAISERROR ('Error al llenar la tabla FCO_VW_ISS', 16, 1)
                                        
        IF @vbi_arp_iss = 1
        BEGIN                            
            /* Parte 2. No están afiliados ni en Salud ni en Pensión, pero el ISS es la ARP */
            SET @vvc_sql =
            " INSERT INTO FCO_VW_ISS" +
            " SELECT am.compania, tg.trabajador, 'Otro' COD_EPS, 'Otro' COD_FPO, suc.dato," +
            " tid.tipo_id As C10A, t.clave_unica As C10B, t.nombre As C10C, ' ' C11A, ' ' C11B, ' ' C11C, 0 C11D, 0 C11E,"+
            " C12A = IsNull(CASE am.agr_conceptos WHEN " + @vch_agr_17 + " THEN " + @vch_acum_tie + " ELSE 0 END,0)," +
            " C13A = IsNull(CASE am.agr_conceptos WHEN " + @vch_agr_17i + " THEN Round((" + @vch_acum_imp + "-1)/1000,0)*1000 ELSE 0 END,0)," +
            " C14A = 0, C14B = 0, C14C = 0, ' ' C25N, C25C = 0, ' ' C26N, C26C = 0" +
            " FROM trabajadores t" +
            " INNER JOIN trabajadores_grales tg ON t.trabajador = tg.trabajador" +
            " INNER JOIN vw_fco_div_trabajador suc ON suc.compania = tg.compania AND suc.trabajador = tg.trabajador" +
            " INNER JOIN acumulados_mensuales am ON am.compania = tg.compania AND am.trabajador = tg.trabajador" +
            " INNER JOIN vw_fco_tid_trabajador tid ON tg.compania = tid.compania AND tg.trabajador = tid.trabajador" +
            " WHERE am.compania = '" + @pch_compania + "'" +
              " AND am.anio = " + RTrim(@psi_anio) +
              " AND am.agr_conceptos IN (" + @vch_agr_17 + "," + @vch_agr_17i +") " + @vvc_cond_1 +
              " AND NOT EXISTS (SELECT 1 FROM vw_fco_iss_trabajador x WHERE x.compania = tg.compania AND x.trabajador = tg.trabajador)"
            EXECUTE (@vvc_sql)
            IF @@ERROR <> 0 RAISERROR ('Error al llenar la tabla FCO_VW_ISS (Solo ARP)', 16, 1)
        END /*IF @vbi_arp_iss = 1 */
                  
        /* Inserta los valores Depurados a la tabla del periodo */                            
        SELECT @vvc_sql =                              
        " INSERT INTO " + @vsy_tabla +                            
        " SELECT CIA, COD_TRAB, COD_EPS, COD_FPO, COD_SUC, C10A, C10B, C10C," +                            
        " MAX(C11A), MAX(C11B), MAX(C11C), MAX(C11D), MAX(C11E), MAX(C12A), MAX(C13A)," +                            
        " MAX(C14A), MAX(C14B), MAX(C14C), MAX(C25N), MAX(C25C), MAX(C26N), MAX(C26C)" +                            
        " FROM FCO_VW_ISS" + @vvc_cond_0 +                            
        " GROUP BY CIA, C10B, C10C, COD_TRAB, COD_EPS, COD_FPO, COD_SUC, C10A"                            
        EXECUTE (@vvc_sql)                            
        IF @@ERROR <> 0 RAISERROR ('Error al llenar la tabla del período', 16, 1)
                                           
        /******* I N S E R C I O N    D E   N O V E D A D E S *******/                                       
        SET @vvc_sql = 
            " UPDATE " + @vsy_tabla +                            
            " SET C11E = 0, C11C = ' '" +   /* JAS - 20040811 */                
            " WHERE CIA = '" + @pch_compania + "'" +  @vvc_cond_2                          
        EXECUTE (@vvc_sql)                          
        
        /* NOVEDAD I: INCAPACIDADES POR LICENCIA DE MATERNIDAD */
        SET @vvc_sql =
        " UPDATE " + @vsy_tabla +
        " SET C11B = CASE " + @vch_acum_imp + " WHEN 0 THEN ' ' ELSE 'I' END," +
        " C11E = " + @vch_acum_tie +
        " FROM acumulados_mensuales am" +
        " WHERE CIA = am.compania " +
          " AND COD_TRAB = am.trabajador " +
          " AND COD_EPS = 'ISS' " +
          " AND CIA ='" + @pch_compania + "'" +
          " AND am.anio = " + RTrim(@psi_anio) + 
          " AND am.agr_conceptos = " + @vch_agr_10 + @vvc_cond_2                                            
        EXECUTE (@vvc_sql)                                    
        IF @@ERROR <> 0 RAISERROR ('Error al insertar Novedad C11B Licencia de Maternidad', 16, 1)
        
        /* NOVEDAD I: INCAPACIDADES POR LICENCIA DE MATERNIDAD COLUMNA C:RIESGOS*/
        IF @vbi_arp_iss = 1
        BEGIN
            SET @vvc_sql =
            " UPDATE " + @vsy_tabla +
            " SET C11C = CASE WHEN " + @vch_acum_imp + " <> 0 THEN 'I' ELSE C11C END," +
                " C11E = " + @vch_acum_tie +
            " FROM acumulados_mensuales am" +
            " WHERE CIA = am.compania" +
              " AND COD_TRAB = am.trabajador" +
              " AND COD_EPS <> 'ISS'" +
              " AND CIA = '" + @pch_compania + "'" +
              " AND am.anio = " + RTrim(@psi_anio) + 
              " AND am.agr_conceptos = " + @vch_agr_10  + @vvc_cond_2
        
            EXECUTE (@vvc_sql)
            IF @@ERROR <> 0 RAISERROR ('Error al insertar Novedad C11C Licencia de Maternidad', 16, 1)
        END
        
        /* NOVEDAD I: INCAPACIDADES POR ENFERMEDAD GENERAL */
        SET @vvc_sql =
        " UPDATE " + @vsy_tabla +
        " SET C11B = CASE WHEN " + @vch_acum_imp + " <> 0 THEN 'I' END," +
            " C11E = IsNull(C11E,0) + IsNull(" + @vch_acum_tie + ",0)" +
        " FROM acumulados_mensuales am" +
        " WHERE CIA = AM.compania" +
          " AND COD_TRAB = AM.trabajador" +
          " AND COD_EPS = 'ISS'" +
          " AND CIA = '" + @pch_compania + "'" +
          " AND AM.anio = "+ Convert(char,@psi_anio) + 
          " AND AM.agr_conceptos = " + @vch_agr_11  + @vvc_cond_2
        EXECUTE (@vvc_sql)
        IF @@ERROR <> 0 RAISERROR ('Error al insertar Novedad C11B Incapacidad Salud', 16, 1)
        
        /* NOVEDAD I: INCAPACIDADES POR ENFERMEDAD GENERAL. COLUMNA C:RIESGOS */                            
        IF @vbi_arp_iss = 1
        BEGIN
            SET
            @vvc_sql =
            " UPDATE " + @vsy_tabla +
            " SET C11C = CASE WHEN " + @vch_acum_imp + " <> 0 THEN 'I' ELSE C11C END," +  /* JAS - 20040811 */
                " C11E = IsNull(C11E,0) + IsNull(" + @vch_acum_tie + ",0)" +
            " FROM acumulados_mensuales am" +
            " WHERE CIA = AM.compania" +
              " AND COD_TRAB = am.trabajador" +
              " AND COD_EPS <> 'ISS'" +
              " AND CIA = '" + @pch_compania + "'" +
              " AND am.anio = " + RTrim(@psi_anio) + 
              " AND am.agr_conceptos = " + @vch_agr_11  + @vvc_cond_2
            EXECUTE (@vvc_sql)
            IF @@ERROR <> 0 RAISERROR ('Error al insertar Novedad C11C Incapacidad Salud', 16, 1)            
        END
        
        /* NOVEDAD I: INCAPACIDADES POR ACCIDENTES DE TRABAJO */
        IF @vbi_arp_iss = 1
        BEGIN
            SET @vvc_sql =
            " UPDATE " + @vsy_tabla +
            " SET C11C = CASE WHEN " + @vch_acum_imp + " <> 0 THEN 'I' ELSE C11C END," +   /* JAS - 20040811 */
                " C11E = IsNull(C11E,0) + " + RTrim(@vch_acum_tie) +
            " FROM acumulados_mensuales am" +
            " WHERE CIA = am.compania" +
              " AND COD_TRAB = am.trabajador" +
              " AND CIA = '" + @pch_compania + "'" +
              " AND am.anio = " + RTrim(@psi_anio) +
              " AND am.agr_conceptos = " + @vch_agr_12
            EXECUTE (@vvc_sql)
            IF @@ERROR <> 0 RAISERROR ('Error al insertar Novedad C11C Incapacidad Riesgos', 16, 1)
        END
        
        /* NOVEDAD L: Licencia NO REMUNERADA Y SANCIONES. COLUMNA A: PENSION */
        SET @vvc_sql =
        " UPDATE " + @vsy_tabla +
        " SET C11A = CASE " + @vch_acum_imp + " WHEN 0 THEN ' ' ELSE 'L' END," +
            " C11E = IsNull(C11E,0) + " + Convert(char,@vch_acum_tie) +
        " FROM acumulados_mensuales am" +
        " WHERE CIA = am.compania" +
          " AND COD_TRAB = am.trabajador" +
          " AND COD_FPO = 'ISS'" +
          " AND CIA ='" + @pch_compania + "'" +
          " AND am.anio = " + RTrim(@psi_anio) + 
          " AND am.agr_conceptos =" + @vch_agr_18  + @vvc_cond_2
        EXECUTE (@vvc_sql)
        IF @@ERROR <> 0 RAISERROR ('Error al insertar Novedad C11A Sanciones (Update)', 16, 1)
        
        /* NOVEDAD L: Licencia NO REMUNERADA Y SANCIONES  COLUMNA B: SALUD */                                    
        SET @vvc_sql =                             
        " UPDATE " + @vsy_tabla +                            
        " SET C11B = CASE " + @vch_acum_imp + "  WHEN 0 THEN ' ' ELSE 'L' END," +
            " C11E = IsNull(C11E,0) + " + Convert(char,@vch_acum_tie) +
        " FROM acumulados_mensuales am" +
        " WHERE CIA = am.compania" +
          " AND COD_TRAB = am.trabajador" +
          " AND COD_EPS = 'ISS'" +
          " AND COD_FPO <> 'ISS'" +
          " AND CIA = '" + @pch_compania + "'" +
          " AND am.ANIO = " + RTrim(@psi_anio) + 
          " AND am.agr_conceptos ="+ @vch_agr_18
        EXECUTE (@vvc_sql)
        IF @@ERROR <> 0 RAISERROR ('Error al insertar Novedad C11B Sanciones', 16, 1)
        
        /* NOVEDAD L: Licencia NO REMUNERADA Y SANCIONES  COLUMNA C: RIESGOS */
        IF @vbi_arp_iss = 1
        BEGIN
            SET @vvc_sql =
            " UPDATE " + @vsy_tabla +
            " SET C11C = CASE WHEN " + @vch_acum_imp + " <> 0 THEN 'L' ELSE C11C END, " +  /* JAS - 20040811 */
                " C11E = IsNull(C11E,0) + " + @vch_acum_tie +
            " FROM acumulados_mensuales am" +
            " WHERE CIA = am.compania" +
              " AND COD_TRAB = am.trabajador" +
              " AND COD_EPS <> 'ISS'" +
              " AND COD_FPO <> 'ISS'" +
              " AND CIA ='" + @pch_compania + "'" +
              " AND am.ANIO = "+ Convert(char,@psi_anio) +
              " AND am.agr_conceptos ="+ @vch_agr_18 + @vvc_cond_2
            EXECUTE (@vvc_sql)
            IF @@ERROR <> 0 RAISERROR ('Error al insertar Novedad C11C Sanciones', 16, 1)
        END
        
        /* NOVEDAD V: VACACIONES */
        IF @vbi_arp_iss = 1
        BEGIN
            SET @vvc_sql =
            " UPDATE " + @vsy_tabla +
            " SET C11C = CASE WHEN " + @vch_acum_imp + " <> 0 THEN 'V' ELSE C11C END," +  /* JAS - 20040811 */
                " C11E = IsNull(C11E,0) + IsNull(" + @vch_acum_tie + ",0)" +
            " FROM acumulados_mensuales am" +
            " WHERE CIA = am.compania" +
              " AND COD_TRAB = am.trabajador" +
              " AND CIA = '" + @pch_compania + "'" +
              " AND am.anio = " + RTrim(@psi_anio) +
              " AND am.agr_conceptos = " + @vch_agr_08  + @vvc_cond_2
              EXECUTE (@vvc_sql)                            
              IF @@ERROR <> 0 RAISERROR ('Error al insertar Novedad C11C Vacaciones', 16, 1)
        END            
        ELSE                            
        BEGIN
            IF IsNull(@vbi_vac_salud, 0) = 1
            Begin
                SET @vvc_sql =
                " UPDATE " + @vsy_tabla +
                " SET C11C = CASE WHEN " + @vch_acum_imp + " <> 0 THEN 'V' ELSE C11C END," +  /* JAS - 20040811 */
                    " C11E = IsNull(C11E,0) + IsNull(" + @vch_acum_tie + ",0)" +
                " FROM acumulados_mensuales am" +
                " WHERE CIA = am.compania" +
                  " AND COD_TRAB = am.trabajador" +
                  " AND COD_EPS = 'ISS'" +
                  " AND CIA = '" + @pch_compania + "'" +
                  " AND am.ANIO = "+ RTrim(@psi_anio) + 
                  " AND am.agr_conceptos = " + @vch_agr_08  + @vvc_cond_2
                EXECUTE (@vvc_sql)
                IF @@ERROR <> 0 RAISERROR ('Error al insertar Novedad C11C Vacaciones', 16, 1)
            End
            IF IsNull(@vbi_vac_todos, 0) = 1 -- Esto corresponde al modelo DORIA
            Begin
                SET @vvc_sql =
                " UPDATE " + @vsy_tabla +
                " SET C11E = IsNull(C11E, 0) + IsNull(" + @vch_acum_imp + ",0), " +
                    " C11D = IsNull((SELECT Day(fecha_ini_per_vac) - 1 FROM programacion_vacaciones x" +
                                   " WHERE x.compania = am.compania and x.trabajador = am.trabajador " +
                                     " AND Year(x.fecha_ini_per_vac) = " + RTrim(@psi_anio) + 
                                     " AND Month(x.fecha_ini_per_vac) = " + RTrim(@pti_mes) + "),0) " +
                " FROM acumulados_mensuales am" +
                " WHERE CIA = am.compania" +
                  " AND COD_TRAB = am.trabajador" +
                  " AND CIA ='" + @pch_compania + "'" +
                  " AND am.anio = "+ RTrim(@psi_anio) +
                  " AND am.agr_conceptos = " + @vch_agr_08
                EXECUTE (@vvc_sql)
                
                IF @pti_mes = 1
                    SELECT @vti_mes_ant = 12, @vsi_anio_ant = @psi_anio - 1  
                ELSE  
                    SELECT @vti_mes_ant = @pti_mes - 1, @vsi_anio_ant = @psi_anio
                
                /* SI LA NOVEDAD DE VACACIONES VIENE DEL MES ANTERIOR - JAS - 20050721 */  
                SET @vvc_sql =
                " UPDATE " + @vsy_tabla +
                " SET C11D = IsNull((SELECT Day(fecha_ini_per_vac) - 1 FROM programacion_vacaciones " +
                                   " WHERE compania = am.compania and trabajador = am.trabajador " +
                                     " AND Year(fecha_ini_per_vac) = " + RTRIM(@vsi_anio_ant) +
                                     " AND Month(fecha_ini_per_vac) = " + RTRIM(@vti_mes_ant) +
                                     " AND Year(fecha_fin_per_vac) >= " + RTRIM(@vsi_anio_ant) +
                                     " AND Month(fecha_fin_per_vac) > " + RTRIM(@vti_mes_ant) + "), 0) " +
                " FROM acumulados_mensuales am" +
                " WHERE CIA = am.compania" +
                  " AND COD_TRAB = am.trabajador" +
                  " AND IsNull(C11E, 0) <> 0" +
                  " AND IsNull(C11D, 0) = 0" +
                  " AND CIA = '" + @pch_compania + "'" +
                  " AND am.anio = "+ RTrim(@psi_anio) + 
                  " AND am.agr_conceptos = " + @vch_agr_08                
                EXECUTE (@vvc_sql)              
                
                /* ACTUALIZAR EL NUMERO DE DIAS TRABAJADOS REAL - JAS - 20050721 */  
                SET @vvc_sql =               
                " UPDATE " + @vsy_tabla +
                " SET C12A = C12A - C11E, C11D = IsNull(C11D,0) " +
                " WHERE IsNull(C11E, 0) <> 0" +
                  " AND CIA ='" + @pch_compania + "'"              
                EXECUTE (@vvc_sql)              
                IF @@ERROR <> 0 RAISERROR ('Error al insertar Novedad C11C Vacaciones', 16, 1)                
            End
        END
                   
        /* NOVEDAD T: TRASLADO DEL ISS A OTRA EPS */                            
        SET
        @vvc_sql =
        " UPDATE " + @vsy_tabla +
        " SET C11B = 'T'" +
        " WHERE C13A <> 0" +
          " AND COD_EPS = 'ISS'" +
          " AND CIA = '" + @pch_compania + "'" +
          " AND EXISTS (SELECT 1 FROM fco_traslados_eps t" +
                      " WHERE t.compania = CIA" +
                        " AND t.trabajador = COD_TRAB" +
                        " AND Convert(char(6), t.fecha_cambio, 112) = " + @vch_periodo + ")" + @vvc_cond_2        
        EXECUTE (@vvc_sql)
        IF @@ERROR <> 0 RAISERROR ('Error al insertar Novedad C11B Traslado EPS', 16, 1)
        
        /* NOVEDAD T: TRASLADO DEL ISS A OTRO FONDO DE PENSIONES */
        SELECT
            @vvc_sql =
            " UPDATE " + @vsy_tabla +
            " SET C11A = 'T'" +
            " WHERE C13A <> 0" +
              " AND COD_FPO = 'ISS'" +
              " AND CIA = '" + @pch_compania + "'" +
              " AND EXISTS (SELECT 1 FROM fco_traslados_fpo t" +
                          " WHERE t.compania = CIA" +
                            " AND t.trabajador = COD_TRAB" +
                            " AND Convert(char(6), t.fecha_cambio, 112) =" + @vch_periodo + ")"  + @vvc_cond_2
        EXECUTE (@vvc_sql)
        IF @@ERROR <> 0 RAISERROR ('Error al insertar Novedad C11A Traslado FPO', 16, 1)
                                            
        /* RETIRO DE LA COMPANIA COLUMNA A: PENSION */
        SELECT
            @vvc_sql =
            " UPDATE " + @vsy_tabla +
            " SET C11A = 'R', C11D = DAY(tg.fecha_baja)" +
            " FROM trabajadores_grales tg" +
            " WHERE C13A <> 0" +
              " AND CIA = tg.compania" +
              " AND COD_TRAB = tg.trabajador" +
              " AND COD_FPO = 'ISS'" +
              " AND CIA = '" + @pch_compania + "'" +
              " AND Convert(char(6), tg.fecha_baja, 112) = " + @vch_periodo  + @vvc_cond_2
        EXECUTE (@vvc_sql)    
        IF @@ERROR <> 0 RAISERROR ('Error al insertar Novedad C11A Retiro CIA', 16, 1)
                                        
        /*** RET: RETIROS EN EL MISMO PERIODO. CASO AVIDESA ***/
        SELECT
            @vvc_sql =
            " UPDATE " + @vsy_tabla +
            " SET C11A = 'R', C11D = DAY(hl.valor_03)" +
            " FROM historial_laboral hl" +
            " WHERE C13A <> 0" +
              " AND CIA = hl.compania" +
              " AND COD_TRAB = hl.trabajador" +
              " AND COD_FPO = 'ISS'" +
              " AND CIA = '" + @pch_compania + "'" +
              " AND hl.codigo_incidencia = '0000000002'" +
              " AND Convert(char(6), hl.valor_03,112) = " + @vch_periodo + @vvc_cond_2
        EXECUTE (@vvc_sql)                            
        IF @@ERROR <> 0 RAISERROR ('Error al insertar retiros de personas que reingresaron en el periodo (Pension)', 16, 1)
                                            
        /* NOVEDAD R: RETIRO DE LA COMPANIA COLUMNA B: SALUD */
        SELECT
            @vvc_sql =
            " UPDATE " + @vsy_tabla +
            " SET C11B = 'R', C11D = DATEPART(DAY, tg.fecha_baja)" +
            " FROM trabajadores_grales tg" +
            " WHERE C13A <> 0" +
              " AND CIA = tg.compania" +
              " AND COD_TRAB = tg.trabajador" +
              " AND COD_EPS = 'ISS'" +
              " AND CIA = '" + @pch_compania + "'" +
              " AND Convert(char(6), tg.fecha_baja, 112) = " + @vch_periodo  + @vvc_cond_2
        EXECUTE (@vvc_sql)
        IF @@ERROR <> 0 RAISERROR ('Error al insertar Novedad C11B Retiro CIA', 16, 1)
        
        /*** NOVEDAD R: RETIROS EN EL MISMO PERIODO. CASO AVIDESA ***/                            
        SELECT                             
            @vvc_sql =
            " UPDATE " + @vsy_tabla +
            " SET C11B = 'R', C11D = DAY(hl.valor_03)" +
            " FROM historial_laboral hl" +
            " WHERE C13A <> 0" +
              " AND CIA = hl.compania" +
              " AND COD_TRAB = hl.trabajador" +
              " AND COD_EPS = 'ISS'" +
              " AND CIA = '" + @pch_compania + "'" +
              " AND hl.codigo_incidenCIA = '0000000002'" +
              " AND Convert(char(6), hl.valor_03, 112) = " + @vch_periodo  + @vvc_cond_2
        EXECUTE (@vvc_sql)
        IF @@ERROR <> 0 RAISERROR ('Error al insertar retiros de personas que reingresaron en el periodo (Salud)', 16, 1)        
        
        /* NOVEDAD R: RETIRO DE LA COMPANIA COLUMNA C: RIESGOS */                            
        IF @vbi_arp_iss = 1                            
        BEGIN                            
            SELECT
                @vvc_sql =
                " UPDATE " + @vsy_tabla +
                " SET C11C = 'R', C11D = DATEPART(DAY, tg.fecha_baja)" +
                " FROM trabajadores_grales tg" +
                " WHERE C13A <> 0" +
                  " AND CIA = tg.compania" +
                  " AND COD_TRAB = tg.trabajador" +
                  " AND CIA = '" + @pch_compania + "'" +
                  " AND Convert(char(6), tg.fecha_baja, 112) = " + @vch_periodo  + @vvc_cond_2
            EXECUTE (@vvc_sql)                            
            IF @@ERROR <> 0 RAISERROR ('Error al insertar Novedad C11C Retiro CIA', 16, 1)
        END                            
                            
        /* INSERCION DE LAS AUTORIZACIONES POR EG EN LA TABLA DEL PIE DE REPORTE */                            
        DECLARE cr_autorizaciones_eg CURSOR FOR                     
        SELECT sucursal, Max(numero) NRO, Sum(valor_total) VLR
        FROM fco_autorizaciones
        WHERE compania = @pch_compania
          AND periodo = Substring(@vch_periodo,2,6)
          AND RTrim(cod_entidad) = @vvc_iss_eps
          AND tipo_incapacidad = 0
        GROUP BY sucursal
        HAVING Sum(valor_total) > 0
        
        OPEN cr_autorizaciones_eg                            
        FETCH NEXT FROM cr_autorizaciones_eg INTO @cch_sucursal, @cvc_nro_aut, @cdc_vlr_aut                            
        WHILE @@fetch_status = 0                                      
        BEGIN                  
            IF @vbi_existe = 0 OR (@pbi_regenerar = 1 AND Rtrim(@cch_sucursal) = RTrim(@pvc_suc))                            
                EXECUTE sp_fco_cambio_ala_datos_adi @vsy_tabla, 0, @vvc_iss_eps, @cch_sucursal, 0, @cvc_nro_aut, null, @cdc_vlr_aut, null
            
            FETCH NEXT FROM cr_autorizaciones_eg INTO @cch_sucursal, @cvc_nro_aut, @cdc_vlr_aut
        END
        CLOSE cr_autorizaciones_eg
        DEALLOCATE cr_autorizaciones_eg
        
        /* INSERCION DE LAS AUTORIZACIONES POR LM EN LA TABLA DEL PIE DE REPORTE */                            
        DECLARE cr_autorizaciones_lm CURSOR FOR                            
        SELECT                             
            sucursal, Max(numero) NRO, Sum(valor_total) VLR                            
        FROM                             
            fco_autorizaciones                            
        WHERE                             
            compania = @pch_compania And                            
            periodo = Substring(@vch_periodo,2,6) And                             
            RTrim(cod_entidad) = @vvc_iss_afp And                            
            tipo_incapacidad = 1                            
        GROUP BY                             
            sucursal                            
        HAVING Sum(valor_total) > 0                            
                            
        OPEN cr_autorizaciones_lm                      
        FETCH NEXT FROM cr_autorizaciones_lm INTO @cch_sucursal, @cvc_nro_aut, @cdc_vlr_aut                            
        WHILE @@fetch_status = 0                                      
        BEGIN                            
            IF @vbi_existe = 0 OR (@pbi_regenerar = 1 AND Rtrim(@cch_sucursal) = RTrim(@pvc_suc))
                EXECUTE sp_fco_cambio_ala_datos_adi @vsy_tabla, 0, @vvc_iss_afp, @cch_sucursal, 1, @cvc_nro_aut, null, @cdc_vlr_aut, null
            
            FETCH NEXT FROM cr_autorizaciones_lm INTO @cch_sucursal, @cvc_nro_aut, @cdc_vlr_aut
        END
        CLOSE cr_autorizaciones_lm
        DEALLOCATE cr_autorizaciones_lm
        
        /* INSERCION DE LOS APORTES A LA ARP EN LA TABLA DEL PIE DE REPORTE */                            
        IF @vbi_arp_iss = 1                            
        BEGIN                                    
            SET @vch_random = SubString(Convert(varchar, Convert(float, GetDate(), 109), 2),13,4)                            

            DELETE fco_vw_iss_arp WHERE clave = Convert(smallint, @vch_random)

            SET @vvc_sql =
            " INSERT INTO fco_vw_iss_arp (clave, sucursal, valor)" +
            " SELECT " + @vch_random + ", suc.dato, Sum(" + @vch_acum_imp + ")" +
            " FROM trabajadores t" +
            " INNER JOIN trabajadores_grales tg ON tg.trabajador = t.trabajador" +
            " INNER JOIN vw_div_trabajador suc ON tg.compania = suc.compania AND tg.trabajador = suc.trabajador" + 
            " INNER JOIN acumulados_mensuales am ON tg.compania = am.compania AND tg.trabajador = am.trabajador" +
            " WHERE am.compania ='" + @pch_compania + "'" +
              " AND am.anio = " + RTrim(@psi_anio) + 
              " AND am.agr_conceptos = " + @vch_agr_09 + @vvc_cond_1 +
            " GROUP BY suc.dato"
  
            EXECUTE (@vvc_sql)                            
            IF @@ERROR <> 0 RAISERROR ('Error al insertar los aportes a Riesgos Profesionales', 16, 1)

            IF IsNull(RTrim(LTrim(@pvc_suc)), '') = ''
            Begin
                SELECT @vdc_total_suc = Sum(IsNull(valor, 0))
                FROM fco_vw_iss_arp
                WHERE clave = Convert(smallint, @vch_random)
    
                EXECUTE sp_fco_cambio_ala_datos_adi @vsy_tabla, 0, 'ISS', @pvc_suc, 8, '', null, null, @vdc_total_suc
            End
            Else
            Begin                            
                DECLARE cr_aportes_riesgos CURSOR FOR
                SELECT sucursal, valor FROM fco_vw_iss_arp
                WHERE clave = Convert(smallint, @vch_random)
                
                OPEN cr_aportes_riesgos
                FETCH NEXT FROM cr_aportes_riesgos INTO @cch_sucursal, @cdc_ap_riesgos
                WHILE @@fetch_status = 0
                BEGIN
                    IF @vbi_existe = 0 OR (@pbi_regenerar = 1 AND Rtrim(@cch_sucursal) = RTrim(@pvc_suc))
                        EXECUTE sp_fco_cambio_ala_datos_adi @vsy_tabla, 0, 'ISS', @cch_sucursal, 8, '', null, null, @cdc_ap_riesgos
                    
                    FETCH NEXT FROM cr_aportes_riesgos INTO @cch_sucursal, @cdc_ap_riesgos
                END
                
                CLOSE cr_aportes_riesgos
                DEALLOCATE cr_aportes_riesgos
            End
            
            DELETE fco_vw_iss_arp WHERE clave = Convert(smallint, @vch_random)
        END
        
        /* INSERCION DE LAS INCAPACIDADES POR ARP EN LA TABLA DEL PIE DE REPORTE */                            
        IF @vbi_arp_iss = 1                            
        BEGIN                                    
            DELETE fco_vw_iss_arp WHERE clave = Convert(smallint, @vch_random)

            SET @vvc_sql =
            " INSERT INTO fco_vw_iss_arp " +
            " SELECT " + @vch_random + ", SUC.dato, Sum(" + @vch_acum_imp + ")" +
            " FROM trabajadores t" +
            " INNER JOIN trabajadores_grales tg ON t.trabajador = tg.trabajador" +
            " INNER JOIN vw_div_trabajador suc ON suc.compania = tg.compania AND suc.trabajador = tg.trabajador" +
            " INNER JOIN acumulados_mensuales am ON am.compania = tg.compania AND am.trabajador = tg.trabajador" +
            " WHERE am.compania ='" + @pch_compania + "'" +
              " AND am.anio = " + RTrim(@psi_anio) +
              " AND am.agr_conceptos = " + @vch_agr_12 + @vvc_cond_1 +
            " GROUP BY suc.dato"
            EXECUTE (@vvc_sql)
            IF @@ERROR <> 0 RAISERROR ('Error al insertar las incapacidades por Riesgos Profesionales', 16, 1)
            
            IF IsNull(RTrim(LTrim(@pvc_suc)), '') = ''
            Begin
                SELECT @vdc_total_suc = Sum(IsNull(valor, 0))
                FROM fco_vw_iss_arp
                WHERE clave = Convert(smallint, @vch_random)

                SET @vdc_total_suc = IsNull(@vdc_total_suc, 0)
    
                EXECUTE sp_fco_cambio_ala_datos_adi @vsy_tabla, 0, 'ISS', @pvc_suc, 0, '', null, null, @vdc_total_suc
            End
            Else
            Begin                            
                DECLARE cr_incapacidades_riesgos CURSOR FOR
                SELECT sucursal, valor FROM fco_vw_iss_arp
                WHERE clave = Convert(smallint, @vch_random)
                
                OPEN cr_incapacidades_riesgos
                FETCH NEXT FROM cr_incapacidades_riesgos INTO @cch_sucursal, @cdc_ap_riesgos
                WHILE @@fetch_status = 0
                BEGIN
                    IF @vbi_existe = 0 OR (@pbi_regenerar = 1 AND Rtrim(@cch_sucursal) = RTrim(@pvc_suc))
                        EXECUTE sp_fco_cambio_ala_datos_adi @vsy_tabla, 0, 'ISS', @cch_sucursal, 0, '', null, null, @cdc_ap_riesgos
                    
                    FETCH NEXT FROM cr_incapacidades_riesgos INTO @cch_sucursal, @cdc_ap_riesgos
                END
                CLOSE cr_incapacidades_riesgos
                DEALLOCATE cr_incapacidades_riesgos
            End
            
            DELETE fco_vw_iss_arp WHERE clave = Convert(smallint, @vch_random)
        END        
        
        SET @vvc_sql = 
            "DELETE FROM " + @vsy_tabla + 
            " WHERE C11A = ' ' AND C11B = ' ' AND C11C = ' ' AND C11D = 0 AND C11E = 0 AND C12A = 0 AND C13A = 0 AND C14A = 0 AND C14B = 0 " +                          
              " AND C14C = 0 AND C25N = ' ' AND C25C = 0 AND C26N = ' ' AND C26C = 0 "                          
        EXECUTE (@vvc_sql)                          
                            
        /*** INCORPORACION DE APORTANTES FANTASMAS ***/
        IF OBJECT_ID('dbo.fco_fantasmas') IS NOT NULL
        BEGIN
            SET @vvc_sql = 
            " INSERT INTO " + @vsy_tabla +
            " SELECT compania, trabajador, eps, afp, sucursal, Left(tipo_id, 1) C10A, trabajador C04," +
                " Replace(IsNull(primer_apellido, '') + '/' + IsNull(segundo_apellido, '') + '/' + IsNull(primer_nombre, '') + ' ' + IsNull(segundo_nombre,  ''), '  ', ' ') C10C," + 
                " '' C11A, '' C11B, '' C11C, 0 C11D, 0 C11E," +
                " C12A = Case When eps = '" + @vvc_iss_eps + "' Then dias_eps Else dias_afp End, " +
                " C13A = Case When eps = '" + @vvc_iss_eps + "' Then ibc_eps Else ibc_afp End, " + 
                " C14A = Case When afp = '" + @vvc_iss_afp + "' Then aporte_afp Else 0 End, " + 
                " C14B = Case When afp = '" + @vvc_iss_afp + "' Then aporte_fsp_sol + aporte_fsp_sub Else 0 End, " + 
                " C14C = Case When eps = '" + @vvc_iss_eps + "' Then aporte_eps Else 0 End, " + 
                " '' C25C, 0 C25C, '' C26N, 0 C26C" +
            " FROM fco_fantasmas" +
            " WHERE compania ='" + @pch_compania + "'" +
              " AND (eps = '" + @vvc_iss_eps + "' OR afp = '" + @vvc_iss_afp + "')" +
              " AND RTrim(anio) + Right('00' + RTrim(mes), 2) >= " + RTrim(@psi_anio) + Right('00' + RTrim(@pti_mes), 2)
            EXECUTE (@vvc_sql)
            IF @@ERROR <> 0 RAISERROR ('Error al incorporar aportantes fantasmas', 16, 1)
        END                                    

        /* REVISION JCL NOVIEMBRE 6 DE 2003. QUITAR LOS RETIRADOS DE HACE TIEMPO  */              
        /* SE COLOCO IsNull - JAS - 20041014 */              
        SET @vvc_sql = 
            " DELETE FROM " + @vsy_tabla + 
            " WHERE cod_trab IN (SELECT trabajador FROM trabajadores_grales" +
                               " WHERE compania = '" + @pch_compania + "'" + 
                                 " AND fecha_baja < '" + RTRIM(@psi_anio) +  RIGHT('00' + RTRIM(@pti_mes),2) + "01')"  +                    
              " AND IsNull(C11A, ' ') = ' ' AND IsNull(C11B, ' ') = ' ' AND IsNull(C11C, ' ') = ' ' AND IsNull(C11D, 0) = 0 " +              
              " AND IsNull(C11E, 0) = 0 AND IsNull(C12A, 0) = 0 AND IsNull(C13A, 0) = 0 AND IsNull(C14A, 0) = 0 " +              
              " AND IsNull(C14B, 0) = 0 AND IsNull(C14C, 0) = 0 AND IsNull(C25N, ' ') = ' ' AND IsNull(C25C, 0) = 0 " +              
              " AND IsNull(C26N, ' ') = ' ' AND IsNull(C26C, 0) = 0"
        EXECUTE (@vvc_sql)
    END /* IF @vbi_existe = 0 */                                                    
    
    IF @pbi_generar_mm = 1
        EXEC sp_fco_crea_autolx_mm @pch_compania, @psi_anio, @pti_mes, @pvc_suc
END                        
GO
GRANT EXECUTE ON dbo.sp_fco_crea_vw_iss TO PUBLIC
GO
