IF EXISTS (SELECT 1 FROM SYS.VIEWS WHERE OBJECT_ID = OBJECT_ID(N'[V_EST_NF_EVENTOS]'))
	DROP VIEW [V_EST_NF_EVENTOS]
GO 

CREATE  VIEW [V_EST_NF_EVENTOS]
AS
 --  SELECT DISTINCT COD_EVENTO , DS_EVENTO FROM V_EST_NF_EVENTOS
 --  SELECT * FROM V_EST_NF_EVENTOS WHERE CD_NF  =  14161

 SELECT '112110' AS COD_EVENTO
		,'Pagamento integral para liberar crédito presumido do adquirente' AS DS_EVENTO
		,'Pagamento integral para liberar crédito' AS DS_EVENTO_SIMP
		, EST_NF_ENT.CD_EMP
		, EST_NF_ENT.CD_FILIAL
		, EST_NF_ENT.CD_NF
		, EST_NF_ENT.NR_NF
		, CASE WHEN ISNULL(EST_NF_ENT_EF_NF_CTR_NF.CD_NF_ENT, 0) > 0 THEN 1
		  ELSE 0
		  END NF_EMISSAO_PROPRIA
		, EST_NF_ENT.CD_NF_SERIE
		, ISNULL(EST_NF_SERIE.SERIE, '') AS SERIE
		, ISNULL(EST_NF_SERIE.MODELO, '') AS MODELO
		, EST_NF_ENT.CD_FORN AS CD_ORIG
		, V_GLB_PESSOA.RAZAO_SOCIAL_PESSOA AS NM_ORIG
		, EST_NF_ENT.DT_ENT
		, EST_NF_ENT.DT_EMI_NF
		,'ENTRADA' AS TIPO_NF  
		, EST_NF_ENT.VLR_NF
		, ISNULL(NFE_PGTO_INTEG.NR_AUTORIZADOR,'') AS NR_AUTORIZADOR
		, ISNULL(NFE_PGTO_INTEG.NR_PROTOCOLO,'') AS NR_PROTOCOLO
		, ISNULL(NFE_PGTO_INTEG.STS_IMP,0) AS STS_IMP
		, ISNULL(NFE_PGTO_INTEG.DT_RECBTO,'') AS DT_RECBTO
		, PGTO_INTEG.DT_HR_EVENTO AS DT_ARQUIVO
		, ISNULL(NFE_NFE_PGTOINTEG.DS_MOTIVO, '') AS DS_MOTIVO
		, GLB_USU.NM_USU
		, GLB_MOV_OP.DS_MOV_OP
		, PGTO_INTEG.CD_CTR_INF_EFET_PGTO_INTEG AS NFE_CTR
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ
		, GLB_MOV_OP.CD_MOV_OP
		
		--Itens 
		, 0 AS SEQ_ITEM 
		, 0 AS  CD_PROD
		, '' AS DS_PROD
		, 0 AS VLR_IBS_PEREC  
		, 0 AS VLR_CBS_PEREC  
		, 0 AS QTD_PEREC  
		, '' AS UNID_PEREC  
		, 0 AS VLR_IBS  
		, 0 AS VLR_CBS
  
	 
		FROM  DBO.EST_NF_ENT
		 INNER JOIN PRC_FILIAL FL ON DBO.EST_NF_ENT.CD_EMP = FL.CD_EMP
									 AND DBO.EST_NF_ENT.CD_FILIAL = FL.CD_FILIAL
  
	     INNER JOIN DBO.GLB_MOV_OP ON DBO.EST_NF_ENT.CD_NT_MOV = DBO.GLB_MOV_OP.CD_MOV_OP
		  
		 OUTER APPLY UFD_GLB_PESSOA(EST_NF_ENT.CD_FORN, GLB_MOV_OP.DEST_ORIG) V_GLB_PESSOA
 
		 INNER JOIN DBO.EST_NF_SERIE ON DBO.EST_NF_ENT.CD_EMP = DBO.EST_NF_SERIE.CD_EMP
										AND DBO.EST_NF_ENT.CD_NF_SERIE = DBO.EST_NF_SERIE.CD_NF_SERIE

		 INNER JOIN EST_NFE_INF_EFET_PGTO_INTEG_EST_NF_ENT PGTO_INTEG_ENT ON 
		  PGTO_INTEG_ENT.CD_EMP = EST_NF_ENT.CD_EMP
		  AND PGTO_INTEG_ENT.CD_FILIAL =  EST_NF_ENT.CD_FILIAL
		  AND PGTO_INTEG_ENT.CD_NF = EST_NF_ENT.CD_NF

		  INNER JOIN EST_NFE_INF_EFET_PGTO_INTEG PGTO_INTEG ON 
		  PGTO_INTEG.CD_CTR_INF_EFET_PGTO_INTEG  = PGTO_INTEG_ENT.CD_CTR_INF_EFET_PGTO_INTEG

		 INNER JOIN GLB_USU ON  
         PGTO_INTEG.CD_USU = GLB_USU.CD_USU 
          
		 LEFT OUTER JOIN EST_NF_ENT_EF_NF_CTR_NF ON EST_NF_ENT_EF_NF_CTR_NF.CD_EMP = EST_NF_ENT.CD_EMP
													AND EST_NF_ENT_EF_NF_CTR_NF.CD_FILIAL = EST_NF_ENT.CD_FILIAL
													AND EST_NF_ENT_EF_NF_CTR_NF.CD_NF_ENT = EST_NF_ENT.CD_NF
	    LEFT OUTER JOIN EST_NFE_INF_EFET_PGTO_INTEG_VALIDADA NFE_PGTO_INTEG ON  
         PGTO_INTEG.CD_CTR_INF_EFET_PGTO_INTEG = NFE_PGTO_INTEG.CD_CTR_INF_EFET_PGTO_INTEG
		 
		 LEFT JOIN EST_NFE_INF_EFET_PGTO_INTEG_ARQ_DANFE ARQ_DANFE ON 
		 ARQ_DANFE.CD_CTR_INF_EFET_PGTO_INTEG = PGTO_INTEG.CD_CTR_INF_EFET_PGTO_INTEG
		 
	    LEFT JOIN NFE..EST_NFE_INFEFETPGTOINTEG NFE_NFE_PGTOINTEG ON 
		NFE_NFE_PGTOINTEG.NRO = PGTO_INTEG.CD_CTR_INF_EFET_PGTO_INTEG

		

UNION ALL

         SELECT '112130' AS COD_EVENTO
		,'Notas de Entrada Compras para perecimento, perda, roubo ou furto durante o transporte fornecedor' AS DS_EVENTO
		,'Perecimento, perda, roubo,furto durante transporte' AS DS_EVENTO_SIMP
		, EST_NF_ENT.CD_EMP
		, EST_NF_ENT.CD_FILIAL
		, EST_NF_ENT.CD_NF
		, EST_NF_ENT.NR_NF
		, CASE WHEN ISNULL(EST_NF_ENT_EF_NF_CTR_NF.CD_NF_ENT, 0) > 0 THEN 1
		  ELSE 0
		  END NF_EMISSAO_PROPRIA
		, EST_NF_ENT.CD_NF_SERIE
		, ISNULL(EST_NF_SERIE.SERIE, '') AS SERIE
		, ISNULL(EST_NF_SERIE.MODELO, '') AS MODELO
		, EST_NF_ENT.CD_FORN AS CD_ORIG
		, V_GLB_PESSOA.RAZAO_SOCIAL_PESSOA AS NM_ORIG
		, EST_NF_ENT.DT_ENT
		, EST_NF_ENT.DT_EMI_NF
		,'ENTRADA' AS TIPO_NF  
		, EST_NF_ENT.VLR_NF
		, ISNULL(NFE_PERDA_ROUBO_TRANSP.NR_AUTORIZADOR,'') AS NR_AUTORIZADOR
		, ISNULL(NFE_PERDA_ROUBO_TRANSP.NR_PROTOCOLO,'') AS NR_PROTOCOLO
		, ISNULL(NFE_PERDA_ROUBO_TRANSP.STS_IMP,0) AS STS_IMP
		, ISNULL(NFE_PERDA_ROUBO_TRANSP.DT_RECBTO,'') AS DT_RECBTO
		, PERDA_ROUBO_TRANSP.DT_HR_EVENTO AS DT_ARQUIVO
		, ISNULL(NFE_NFE_PERDAROUBOTRANSP.DS_MOTIVO, '') AS DS_MOTIVO
		, GLB_USU.NM_USU
		, GLB_MOV_OP.DS_MOV_OP
		, PERDA_ROUBO_TRANSP.CD_CTR_PERDA_ROUBO_TRANSP_FORN AS NFE_CTR
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ
		, GLB_MOV_OP.CD_MOV_OP
		--Itens 
		, PERDA_ROUBO_TRANSP_IT.SEQ_ITEM 
		, PERDA_ROUBO_TRANSP_IT.CD_PROD
		, EST_PROD.DS_PROD
		, PERDA_ROUBO_TRANSP_IT.VLR_IBS_PEREC  
		, PERDA_ROUBO_TRANSP_IT.VLR_CBS_PEREC  
		, PERDA_ROUBO_TRANSP_IT.QTD_PEREC  
		, PERDA_ROUBO_TRANSP_IT.UNID_PEREC  
		, PERDA_ROUBO_TRANSP_IT.VLR_IBS  
		, PERDA_ROUBO_TRANSP_IT.VLR_CBS
	 
	 
		FROM  DBO.EST_NF_ENT  --  SELECT TOP 1 * FROM EST_PROD
		 INNER JOIN PRC_FILIAL FL ON DBO.EST_NF_ENT.CD_EMP = FL.CD_EMP
									 AND DBO.EST_NF_ENT.CD_FILIAL = FL.CD_FILIAL
  
	     INNER JOIN DBO.GLB_MOV_OP ON DBO.EST_NF_ENT.CD_NT_MOV = DBO.GLB_MOV_OP.CD_MOV_OP
		  
		 OUTER APPLY UFD_GLB_PESSOA(EST_NF_ENT.CD_FORN, GLB_MOV_OP.DEST_ORIG) V_GLB_PESSOA
 
		 INNER JOIN DBO.EST_NF_SERIE ON DBO.EST_NF_ENT.CD_EMP = DBO.EST_NF_SERIE.CD_EMP
										AND DBO.EST_NF_ENT.CD_NF_SERIE = DBO.EST_NF_SERIE.CD_NF_SERIE

		 INNER JOIN EST_NFE_PEREC_PERDA_ROUBO_TRANSP_FORN_EST_NF_ENT PERDA_ROUBO_TRANSP_ENT ON 
		  PERDA_ROUBO_TRANSP_ENT.CD_EMP = EST_NF_ENT.CD_EMP
		  AND PERDA_ROUBO_TRANSP_ENT.CD_FILIAL =  EST_NF_ENT.CD_FILIAL
		  AND PERDA_ROUBO_TRANSP_ENT.CD_NF = EST_NF_ENT.CD_NF
		   
		  INNER JOIN EST_NFE_PEREC_PERDA_ROUBO_TRANSP_FORN PERDA_ROUBO_TRANSP ON 
		  PERDA_ROUBO_TRANSP.CD_CTR_PERDA_ROUBO_TRANSP_FORN   = PERDA_ROUBO_TRANSP_ENT.CD_CTR_PERDA_ROUBO_TRANSP_FORN
		  
		  INNER JOIN EST_NFE_PEREC_PERDA_ROUBO_TRANSP_FORN_IT PERDA_ROUBO_TRANSP_IT ON 
		  PERDA_ROUBO_TRANSP.CD_CTR_PERDA_ROUBO_TRANSP_FORN = PERDA_ROUBO_TRANSP_IT.CD_CTR_PERDA_ROUBO_TRANSP_FORN
       
		  INNER JOIN EST_PROD ON EST_PROD.CD_PROD =  PERDA_ROUBO_TRANSP_IT.CD_PROD
		  
		 INNER JOIN GLB_USU ON  
         PERDA_ROUBO_TRANSP.CD_USU = GLB_USU.CD_USU 
          
		 LEFT OUTER JOIN EST_NF_ENT_EF_NF_CTR_NF ON EST_NF_ENT_EF_NF_CTR_NF.CD_EMP = EST_NF_ENT.CD_EMP
													AND EST_NF_ENT_EF_NF_CTR_NF.CD_FILIAL = EST_NF_ENT.CD_FILIAL
													AND EST_NF_ENT_EF_NF_CTR_NF.CD_NF_ENT = EST_NF_ENT.CD_NF
	    
		LEFT OUTER JOIN EST_NFE_PEREC_PERDA_ROUBO_TRANSP_FORN_VALIDADA NFE_PERDA_ROUBO_TRANSP ON  
        PERDA_ROUBO_TRANSP.CD_CTR_PERDA_ROUBO_TRANSP_FORN = NFE_PERDA_ROUBO_TRANSP.CD_CTR_PERDA_ROUBO_TRANSP_FORN 
		 
		 LEFT JOIN EST_NFE_PEREC_PERDA_ROUBO_TRANSP_FORN_ARQ_DANFE  ARQ_DANFE ON 
		 ARQ_DANFE.CD_CTR_PEREC_PERDA_ROUBO_TRANSP_FORN  = PERDA_ROUBO_TRANSP.CD_CTR_PERDA_ROUBO_TRANSP_FORN
		 
	    LEFT JOIN NFE..EST_NFE_PERECPERDAROUBOTRANSPFORN NFE_NFE_PERDAROUBOTRANSP ON 
		NFE_NFE_PERDAROUBOTRANSP.NRO = PERDA_ROUBO_TRANSP.CD_CTR_PERDA_ROUBO_TRANSP_FORN

		LEFT JOIN  NFE..EST_NFE_PERECPERDAROUBOTRANSPFORNIT NFE_NFE_PERDAROUBOTRANSPIT ON 
		NFE_NFE_PERDAROUBOTRANSPIT.ID = NFE_NFE_PERDAROUBOTRANSP.ID
		AND PERDA_ROUBO_TRANSP_IT.SEQ_ITEM = NFE_NFE_PERDAROUBOTRANSPIT.SEQ_ITEM


                                  
 




