/****** Object:  View [dbo].[V_PAINEL_NFE_EST_NF_ENT_PRE_ENTRADA_PENDENTE_ANALITICO]    Script Date: 03/10/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_PENDENTE_ANALITICO]')) 
  DROP VIEW [dbo].[V_PAINEL_NFE_EST_NF_ENT_PRE_ENTRADA_PENDENTE_ANALITICO] 

GO 

--SELECT * FROM V_PAINEL_NFE_EST_NF_ENT_PRE_ENTRADA_PENDENTE_ANALITICO 
CREATE VIEW V_PAINEL_NFE_EST_NF_ENT_PRE_ENTRADA_PENDENTE_ANALITICO  
AS
SELECT EST_NF_ENT_PRE_ENTRADA.CD_CTR_PRE_ENTRADA AS codigoControle,
       EST_NF_ENT_PRE_ENTRADA.DT_CAD AS dataCadastro,
       EST_NF_ENT_PRE_ENTRADA.NR_NF AS numeroNfe,
       PG_FORN.CD_FORN AS fornecedorId,
       PG_FORN.RZ_FORN AS fornecedorNome,
       SUM (EST_NF_ENT_PRE_ENTRADA_CPL.QT_IT * EST_NF_ENT_PRE_ENTRADA_CPL.VLR_IT - EST_NF_ENT_PRE_ENTRADA_CPL.VLR_DESC) AS valor,
           ISNULL(WMS.CD_PED_WMS, 0) AS codigoWms,
		    DS_TP as ClassificacaoFornecedor
FROM EST_NF_ENT_PRE_ENTRADA
INNER JOIN EST_NF_ENT_PRE_ENTRADA_CPL ON EST_NF_ENT_PRE_ENTRADA.CD_EMP = EST_NF_ENT_PRE_ENTRADA_CPL.CD_EMP
AND EST_NF_ENT_PRE_ENTRADA.CD_CTR_PRE_ENTRADA = EST_NF_ENT_PRE_ENTRADA_CPL.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
INNER JOIN EST_PED_CP ON EST_NF_ENT_PRE_ENTRADA.CD_EMP = EST_PED_CP.CD_EMP
AND EST_NF_ENT_PRE_ENTRADA.CD_FILIAL = EST_PED_CP.CD_FILIAL
AND EST_NF_ENT_PRE_ENTRADA.CD_PED = EST_PED_CP.CD_PED
INNER JOIN PG_FORN ON PG_FORN.CD_EMP = EST_PED_CP.CD_EMP
AND PG_FORN.CD_FORN = EST_PED_CP.CD_FORN
LEFT OUTER JOIN EST_NF_ENT_PRE_ENTRADA_EST_WMS_ENT WMS ON EST_NF_ENT_PRE_ENTRADA.CD_EMP = WMS.CD_EMP
AND EST_NF_ENT_PRE_ENTRADA.CD_CTR_PRE_ENTRADA = WMS.CD_CTR_PRE_ENTRADA
INNER JOIN PG_TP_FORN ON
PG_FORN.CD_TP_FORN = PG_TP_FORN.CD_TP_FORN

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)
GROUP BY EST_NF_ENT_PRE_ENTRADA.CD_CTR_PRE_ENTRADA,
         EST_NF_ENT_PRE_ENTRADA.NR_NF,
         PG_FORN.Cd_Forn,
         PG_FORN.RZ_FORN,
         WMS.CD_PED_WMS,
         EST_NF_ENT_PRE_ENTRADA.DT_CAD,
		  DS_TP 