IF OBJECT_ID('dbo.vw_fco_pi_pens_proc') IS NOT NULL
    DROP VIEW dbo.vw_fco_pi_pens_proc
GO
CREATE VIEW dbo.vw_fco_pi_pens_proc
AS
SELECT a.compania, a.anio, a.mes, a.sucursal, ISNULL(suc.descripcion, '(Consolidado)') suc_des, 
CONVERT(tinyint,procesar) procesar, sit_periodo = 
ISNULL((SELECT MAX(sit_periodo) FROM fco_integrada_cab x
        WHERE x.compania = a.compania AND x.anio = a.anio AND x.mes = a.mes), 1)
FROM fco_pi_pens_proc a
LEFT JOIN vw_fco_sucursales suc ON suc.compania = a.compania AND suc.sucursal = a.sucursal
AND
(   (a.sucursal = '') 
 OR (a.sucursal IN 
     (SELECT sucursal FROM vw_fco_sucursales_cia 
      WHERE (   (clase_nomina = '--')
             OR (clase_nomina IN (SELECT clase_nomina FROM rel_usu_clase_nom
                                  WHERE usuario = SYSTEM_USER))
            ) 
      )
     )
)
GO
GRANT SELECT ON dbo.vw_fco_pi_pens_proc TO PUBLIC
GO
