/****** Object:  View [dbo].[V_PAINEL_NFE_EST_NF_ENT_PRE_ENTRADA_PENDENCIA_QTDE]    Script Date: 17/11/2018 11:00:00 ******/ 
IF EXISTS (SELECT * 
           FROM   SYS.VIEWS 
           WHERE  OBJECT_ID = 
      Object_id(N'[dbo].[V_PAINEL_NFE_EST_NF_ENT_PRE_ENTRADA_PENDENCIA_QTDE]')) 
  DROP VIEW [dbo].[V_PAINEL_NFE_EST_NF_ENT_PRE_ENTRADA_PENDENCIA_QTDE] 

GO

--SELECT * FROM V_PAINEL_NFE_EST_NF_ENT_PRE_ENTRADA_PENDENCIA_QTDE
CREATE VIEW [dbo].[V_PAINEL_NFE_EST_NF_ENT_PRE_ENTRADA_PENDENCIA_QTDE] AS
SELECT COUNT(EST_NF_ENT_PRE_ENTRADA.CD_CTR_PRE_ENTRADA) AS QTDE_PRE_ENTRADA_PENDENTE
FROM EST_NF_ENT_PRE_ENTRADA
LEFT OUTER JOIN EST_NF_ENT_PRE_ENTRADA_CPL_CONF_LIB ON EST_NF_ENT_PRE_ENTRADA.CD_EMP = EST_NF_ENT_PRE_ENTRADA_CPL_CONF_LIB.CD_EMP
AND EST_NF_ENT_PRE_ENTRADA.CD_CTR_PRE_ENTRADA = EST_NF_ENT_PRE_ENTRADA_CPL_CONF_LIB.CD_CTR_PRE_ENTRADA
INNER JOIN EST_NF_ENT_PRE_ENTRADA_PENDENCIA ON EST_NF_ENT_PRE_ENTRADA.CD_EMP = EST_NF_ENT_PRE_ENTRADA_PENDENCIA.CD_EMP
AND EST_NF_ENT_PRE_ENTRADA.CD_CTR_PRE_ENTRADA = EST_NF_ENT_PRE_ENTRADA_PENDENCIA.CD_CTR_PRE_ENTRADA
WHERE EST_NF_ENT_PRE_ENTRADA.CD_EMP = 1
  AND NOT EXISTS
    (SELECT CD_EMP
     FROM EST_NF_ENT_PRE_ENTRADA_CPL_CONF AS CPL_CONF
     WHERE CPL_CONF.CD_EMP = EST_NF_ENT_PRE_ENTRADA.CD_EMP
       AND CPL_CONF.CD_CTR_PRE_ENTRADA = EST_NF_ENT_PRE_ENTRADA.CD_CTR_PRE_ENTRADA)