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 nr_nf,  * FROM V_EST_NF_EVENTOS WHERE EVENTO_CANCELADO = 1 
 --  SELECT nr_nf,  * FROM V_EST_NF_EVENTOS WHERE cod_EVENTO = '212120'

 SELECT '112110' AS COD_EVENTO
        ,1 AS CD_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
		, 0 AS CTR_ORIG_CANCEL
		, '' AS COD_EVENTO_ORIG_CANCEL
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ_ARQ_DANFE
		, GLB_MOV_OP.CD_MOV_OP
		, CASE WHEN ISNULL (CANCEL_VALIDADA.STS_IMP ,0) = 1 THEN 1 ELSE 0 END AS EVENTO_CANCELADO
		,'NÃO SE APLICA' AS IND_ACEITACAO
		
		--Itens geral : para este evento não contém itens 
		, 0  AS SEQ_ITEM 
		, 0  AS CD_PROD 
		, '' AS DS_PROD
		, '' AS UNID
		, 0  AS QTDE_NF
		, 0  AS QTDE
		, 0  AS VLR_IBS
		, 0  AS VLR_CBS
		
		--Itens Perecimento, perda, roubo,furto durante transporte. Para este evento não contém itens 

		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO  
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO  
		
      --Itens Fornecimento não realizado com pagamento antecipado
		,0 AS VLR_IBS_NAO_FORNECIDA  
		,0 AS VLR_CBS_NAO_FORNECIDA
		,'' AS DT_PREVISAO_ENTREGA 
	
	-- Itens Apropriação de crédito presumido
		,0 AS C_CRED_PRES_IBS
		,0 AS P_CRED_PRES_IBS
		,0 AS V_CRED_PRES_IBS
		,0 AS C_CRED_PRES_CBS
		,0 AS P_CRED_PRES_CBS
		,0 AS V_CRED_PRES_CBS

		 -- Itens consumo pessoal Entrada
		,0 AS  VLR_IBS_CONS_PESSOAL_ENT
		,0 AS VLR_CBS_CONS_PESSOAL_ENT  
		,'' AS CH_ACESSO_DFE_REFERENCIA_ENT  
		,0 AS SEQ_ITEM_DFE_REFERENCIA_ENT  

		-- Itens consumo pessoal Saída
		,0 AS VLR_IBS_CONS_PESSOAL_SAI
		,0 AS VLR_CBS_CONS_PESSOAL_SAI
		,'' AS CH_ACESSO_DFE_REFERENCIA_SAI
		,0 AS SEQ_ITEM_DFE_REFERENCIA_SAI

        --Itens Perecimento, perda, roubo,furto
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO_DEST 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO_DEST

		--Itens imobilização de item 
		, 0 as VLR_IBS_IMOBILIZ_ITEM
		, 0 as VLR_CBS_IMOBILIZ_ITEM

		--Itens credito combustivel
		,0 AS VLR_IBS_CRED_COMBUST
		,0 AS VLR_CBS_CRED_COMBUST
				  
		-- Itens Apropriação de crédito bens serviços
		,0 AS VLR_CRED_IBS_BENS_SERV
		,0  AS VLR_CRED_CBS_BENS_SERV
 
		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

		LEFT JOIN EST_NFE_CANCEL_EVENTO CANCEL_EVENTO ON  
		CANCEL_EVENTO.CD_CTR_EVENTO = PGTO_INTEG.CD_CTR_INF_EFET_PGTO_INTEG
		AND CANCEL_EVENTO.TIPO_EVENTO = '112110'
		LEFT JOIN  EST_NFE_CANCEL_EVENTO_VALIDADA CANCEL_VALIDADA ON 
		CANCEL_VALIDADA.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO


 UNION ALL
 

  SELECT  '112120' AS COD_EVENTO
		,2 AS CD_EVENTO
		,'Importação em ALC/ZFM não convertida em isenção' AS DS_EVENTO
		,'Importação em ALC/ZFM não convertida em isenção' 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_ZFM_NAO_CONV.NR_AUTORIZADOR,'') AS NR_AUTORIZADOR
		, ISNULL(NFE_ZFM_NAO_CONV.NR_PROTOCOLO,'') AS NR_PROTOCOLO
		, ISNULL(NFE_ZFM_NAO_CONV.STS_IMP,0) AS STS_IMP
		, ISNULL(NFE_ZFM_NAO_CONV.DT_RECBTO,'') AS DT_RECBTO
		, ZFM_NAO_CONV.DT_HR_EVENTO AS DT_ARQUIVO
		, ISNULL(NFE_NFE_ZFMNAOCONV.DS_MOTIVO, '') AS DS_MOTIVO
		, GLB_USU.NM_USU
		, GLB_MOV_OP.DS_MOV_OP
		, NFE_ZFM_NAO_CONV.CD_CTR_IMP_ALC_ZFM_NAO_CONV AS NFE_CTR
		, 0 AS CTR_ORIG_CANCEL
		, '' AS COD_EVENTO_ORIG_CANCEL
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ_ARQ_DANFE
		, GLB_MOV_OP.CD_MOV_OP
		, CASE WHEN ISNULL (CANCEL_VALIDADA.STS_IMP ,0) = 1 THEN 1 ELSE 0 END AS EVENTO_CANCELADO
		,'NÃO SE APLICA' AS IND_ACEITACAO
	
	    --Itens geral 
		, ZFM_NAO_CONV_IT.SEQ_ITEM  
		, ZFM_NAO_CONV_IT.CD_PROD 
		, EST_PROD.DS_PROD AS DS_PROD 
		, ZFM_NAO_CONV_IT.UNID AS UNID 
		, ZFM_NAO_CONV_IT.QTD AS QTDE
		, IT.QT_IT AS  QTDE_NF
	    , ZFM_NAO_CONV_IT.VLR_IBS 
		, ZFM_NAO_CONV_IT.VLR_CBS

		--Itens Perecimento, perda, roubo,furto durante transporte
		, 0  AS VLR_IBS_PEREC_PERDA_ROUBO
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO
		
		 --Itens transporte Fornecimento não realizado com pagamento antecipado
        ,0 AS VLR_IBS_NAO_FORNECIDA  
        ,0 AS VLR_CBS_NAO_FORNECIDA
		,'' AS DT_PREVISAO_ENTREGA
		
	   -- Itens Apropriação de crédito presumido
		,0 AS C_CRED_PRES_IBS
		,0 AS P_CRED_PRES_IBS
		,0 AS V_CRED_PRES_IBS
		,0 AS C_CRED_PRES_CBS
		,0 AS P_CRED_PRES_CBS
		,0 AS V_CRED_PRES_CBS
 
 		 -- Itens consumo pessoal Entrada
		,0 AS  VLR_IBS_CONS_PESSOAL_ENT
		,0 AS VLR_CBS_CONS_PESSOAL_ENT  
		,'' AS CH_ACESSO_DFE_REFERENCIA_ENT  
		,0 AS SEQ_ITEM_DFE_REFERENCIA_ENT  

		-- Itens consumo pessoal Saída
		,0 AS VLR_IBS_CONS_PESSOAL_SAI
		,0 AS VLR_CBS_CONS_PESSOAL_SAI
		,'' AS CH_ACESSO_DFE_REFERENCIA_SAI
		,0 AS SEQ_ITEM_DFE_REFERENCIA_SAI

        --Itens Perecimento, perda, roubo,furto
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO_DEST 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO_DEST

		--Itens imobilização de item 
		, 0 as VLR_IBS_IMOBILIZ_ITEM
		, 0 as VLR_CBS_IMOBILIZ_ITEM

		--Itens credito combustivel
		,0 AS VLR_IBS_CRED_COMBUST
		,0 AS VLR_CBS_CRED_COMBUST
				  
		-- Itens Apropriação de crédito bens serviços
		,0 AS VLR_CRED_IBS_BENS_SERV
		,0  AS VLR_CRED_CBS_BENS_SERV


 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_IMP_ALC_ZFM_NAO_CONV_EST_NF_ENT ZFM_NAO_CONV_ENT ON 
		  ZFM_NAO_CONV_ENT.CD_EMP = EST_NF_ENT.CD_EMP
		  AND ZFM_NAO_CONV_ENT.CD_FILIAL =  EST_NF_ENT.CD_FILIAL
		  AND ZFM_NAO_CONV_ENT.CD_NF = EST_NF_ENT.CD_NF
		   
		  INNER JOIN EST_NFE_IMP_ALC_ZFM_NAO_CONV ZFM_NAO_CONV ON 
		  ZFM_NAO_CONV.CD_CTR_IMP_ALC_ZFM_NAO_CONV   = ZFM_NAO_CONV_ENT.CD_CTR_IMP_ALC_ZFM_NAO_CONV 
		  
		  INNER JOIN EST_NFE_IMP_ALC_ZFM_NAO_CONV_IT ZFM_NAO_CONV_IT ON 
		  ZFM_NAO_CONV.CD_CTR_IMP_ALC_ZFM_NAO_CONV = ZFM_NAO_CONV_IT.CD_CTR_IMP_ALC_ZFM_NAO_CONV
		  
		  INNER JOIN EST_NF_ENT_IT IT  ON
		         EST_NF_ENT.CD_EMP = IT.CD_EMP
		  AND EST_NF_ENT.CD_FILIAL = IT.CD_FILIAL
		  AND EST_NF_ENT.CD_NF = IT.CD_NF
		  AND ZFM_NAO_CONV_IT.SEQ_ITEM = IT.SEQ_ITEM 
		  AND ZFM_NAO_CONV_IT.CD_PROD = IT.CD_PROD
       
		  INNER JOIN EST_PROD ON EST_PROD.CD_PROD =  ZFM_NAO_CONV_IT.CD_PROD
		  
		 INNER JOIN GLB_USU ON  
         ZFM_NAO_CONV.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_IMP_ALC_ZFM_NAO_CONV_VALIDADA NFE_ZFM_NAO_CONV ON  
        NFE_ZFM_NAO_CONV.CD_CTR_IMP_ALC_ZFM_NAO_CONV = ZFM_NAO_CONV.CD_CTR_IMP_ALC_ZFM_NAO_CONV 
		 
		LEFT JOIN EST_NFE_IMP_ALC_ZFM_NAO_CONV_ARQ_DANFE ARQ_DANFE ON 
		ARQ_DANFE.CD_CTR_IMP_ALC_ZFM_NAO_CONV = ZFM_NAO_CONV.CD_CTR_IMP_ALC_ZFM_NAO_CONV 
		 
	    LEFT JOIN NFE..EST_NFE_IMPALCZFMNAOCONV NFE_NFE_ZFMNAOCONV  ON 
		NFE_NFE_ZFMNAOCONV.NRO = ZFM_NAO_CONV.CD_CTR_IMP_ALC_ZFM_NAO_CONV

		LEFT JOIN  NFE..EST_NFE_PERECPERDAROUBOTRANSPFORNIT NFE_NFE_ZFMNAOCONVIT ON 
		NFE_NFE_ZFMNAOCONVIT.ID = NFE_NFE_ZFMNAOCONV.ID
		AND NFE_NFE_ZFMNAOCONVIT.SEQ_ITEM = NFE_NFE_ZFMNAOCONV.ID

		LEFT JOIN EST_NFE_CANCEL_EVENTO CANCEL_EVENTO ON  
		CANCEL_EVENTO.CD_CTR_EVENTO = ZFM_NAO_CONV.CD_CTR_IMP_ALC_ZFM_NAO_CONV
		AND CANCEL_EVENTO.TIPO_EVENTO = '112120'
		LEFT JOIN  EST_NFE_CANCEL_EVENTO_VALIDADA CANCEL_VALIDADA ON 
		CANCEL_VALIDADA.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO

 UNION ALL

         SELECT '112130' AS COD_EVENTO
		, 3 AS CD_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
		, 0 AS CTR_ORIG_CANCEL
		, '' AS COD_EVENTO_ORIG_CANCEL
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ_ARQ_DANFE
		, GLB_MOV_OP.CD_MOV_OP
		, CASE WHEN ISNULL (CANCEL_VALIDADA.STS_IMP ,0) = 1 THEN 1 ELSE 0 END AS EVENTO_CANCELADO
		,'NÃO SE APLICA' AS IND_ACEITACAO
	
	    --Itens geral 
		, PERDA_ROUBO_TRANSP_IT.SEQ_ITEM  
		, PERDA_ROUBO_TRANSP_IT.CD_PROD 
		, EST_PROD.DS_PROD AS DS_PROD 
		, PERDA_ROUBO_TRANSP_IT.UNID_PEREC AS UNID 
		, PERDA_ROUBO_TRANSP_IT.QTD_PEREC  AS QTDE
		, IT.QT_IT AS  QTDE_NF
	    , PERDA_ROUBO_TRANSP_IT.VLR_IBS 
		, PERDA_ROUBO_TRANSP_IT.VLR_CBS

		--Itens Perecimento, perda, roubo,furto durante transporte
		, PERDA_ROUBO_TRANSP_IT.VLR_IBS_PEREC  AS VLR_IBS_PEREC_PERDA_ROUBO
		, PERDA_ROUBO_TRANSP_IT.VLR_CBS_PEREC  AS VLR_CBS_PEREC_PERDA_ROUBO
		
		 --Itens transporte Fornecimento não realizado com pagamento antecipado
        ,0 AS VLR_IBS_NAO_FORNECIDA  
        ,0 AS VLR_CBS_NAO_FORNECIDA
		,'' AS DT_PREVISAO_ENTREGA
		
	    -- Itens Apropriação de crédito presumido
		,0 AS C_CRED_PRES_IBS
		,0 AS P_CRED_PRES_IBS
		,0 AS V_CRED_PRES_IBS
		,0 AS C_CRED_PRES_CBS
		,0 AS P_CRED_PRES_CBS
		,0 AS V_CRED_PRES_CBS
 
 		 -- Itens consumo pessoal Entrada
		,0 AS  VLR_IBS_CONS_PESSOAL_ENT
		,0 AS VLR_CBS_CONS_PESSOAL_ENT  
		,'' AS CH_ACESSO_DFE_REFERENCIA_ENT  
		,0 AS SEQ_ITEM_DFE_REFERENCIA_ENT  

		-- Itens consumo pessoal Saída
		,0 AS VLR_IBS_CONS_PESSOAL_SAI
		,0 AS VLR_CBS_CONS_PESSOAL_SAI
		,'' AS CH_ACESSO_DFE_REFERENCIA_SAI
		,0 AS SEQ_ITEM_DFE_REFERENCIA_SAI

        --Itens Perecimento, perda, roubo,furto
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO_DEST 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO_DEST

		--Itens imobilização de item 
		, 0 as VLR_IBS_IMOBILIZ_ITEM
		, 0 as VLR_CBS_IMOBILIZ_ITEM

		--Itens credito combustivel
		,0 AS VLR_IBS_CRED_COMBUST
		,0 AS VLR_CBS_CRED_COMBUST
				  
		-- Itens Apropriação de crédito bens serviços
		,0 AS VLR_CRED_IBS_BENS_SERV
		,0  AS VLR_CRED_CBS_BENS_SERV

		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_NF_ENT_IT IT  ON
		         EST_NF_ENT.CD_EMP = IT.CD_EMP
		  AND EST_NF_ENT.CD_FILIAL = IT.CD_FILIAL
		  AND EST_NF_ENT.CD_NF = IT.CD_NF
		  AND PERDA_ROUBO_TRANSP_IT.SEQ_ITEM = IT.SEQ_ITEM 
		  AND PERDA_ROUBO_TRANSP_IT.CD_PROD = IT.CD_PROD
       
		  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
		
		LEFT JOIN EST_NFE_CANCEL_EVENTO CANCEL_EVENTO ON  
		CANCEL_EVENTO.CD_CTR_EVENTO = PERDA_ROUBO_TRANSP.CD_CTR_PERDA_ROUBO_TRANSP_FORN
		AND CANCEL_EVENTO.TIPO_EVENTO = '112130'
		LEFT JOIN  EST_NFE_CANCEL_EVENTO_VALIDADA CANCEL_VALIDADA ON 
		CANCEL_VALIDADA.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO


UNION ALL
		
	     SELECT '112140' AS COD_EVENTO
		, 4 AS CD_EVENTO
		, 'Fornecimento não realizado com pagamento antecipado' AS DS_EVENTO
		, 'Fornecimento não realizado com pagamento antecipado' 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_ANTECIP.NR_AUTORIZADOR,'') AS NR_AUTORIZADOR
		, ISNULL(NFE_PGTO_ANTECIP.NR_PROTOCOLO,'') AS NR_PROTOCOLO
		, ISNULL(NFE_PGTO_ANTECIP.STS_IMP,0) AS STS_IMP
		, ISNULL(NFE_PGTO_ANTECIP.DT_RECBTO,'') AS DT_RECBTO
		, PGTO_ANTECIP.DT_HR_EVENTO AS DT_ARQUIVO
		, ISNULL(NFE_NFE_PGTO_ANTECIP.DS_MOTIVO, '') AS DS_MOTIVO
		, GLB_USU.NM_USU
		, GLB_MOV_OP.DS_MOV_OP
		, PGTO_ANTECIP.CD_CTR_FORN_NAO_REALIZ_COM_PGTO_ANTECIP AS NFE_CTR
		, 0 AS CTR_ORIG_CANCEL
		, '' AS COD_EVENTO_ORIG_CANCEL
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ_ARQ_DANFE
		, GLB_MOV_OP.CD_MOV_OP
		, CASE WHEN ISNULL (CANCEL_VALIDADA.STS_IMP ,0) = 1 THEN 1 ELSE 0 END AS EVENTO_CANCELADO
		,'NÃO SE APLICA' AS IND_ACEITACAO
		
		--Itens geral 
        , PGTO_ANTECIP_IT.SEQ_ITEM 
		, PGTO_ANTECIP_IT.CD_PROD
		, EST_PROD.DS_PROD
		, PGTO_ANTECIP_IT.UNID_NAO_FORNECIDA as UNID
		, PGTO_ANTECIP_IT.QTD_NAO_FORNECIDA AS QTDE
		, IT.QT_IT AS  QTDE_NF
		, IT_IBSCBS.VLR_IBS  
		, IT_IBSCBS.VLR_CBS

		--Itens Perecimento, perda, roubo,furto durante transporte
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO 

		--Itens transporte Fornecimento não realizado com pagamento antecipado
        ,PGTO_ANTECIP_IT.VLR_IBS_NAO_FORNECIDA  
        ,PGTO_ANTECIP_IT.VLR_CBS_NAO_FORNECIDA
		,'' AS DT_PREVISAO_ENTREGA 

	    -- Itens Apropriação de crédito presumido
		,0 AS C_CRED_PRES_IBS
		,0 AS P_CRED_PRES_IBS
		,0 AS V_CRED_PRES_IBS
		,0 AS C_CRED_PRES_CBS
		,0 AS P_CRED_PRES_CBS
		,0 AS V_CRED_PRES_CBS

		 -- Itens consumo pessoal Entrada
		,0 AS  VLR_IBS_CONS_PESSOAL_ENT
		,0 AS VLR_CBS_CONS_PESSOAL_ENT  
		,'' AS CH_ACESSO_DFE_REFERENCIA_ENT  
		,0 AS SEQ_ITEM_DFE_REFERENCIA_ENT  

		-- Itens consumo pessoal Saída
		,0 AS VLR_IBS_CONS_PESSOAL_SAI
		,0 AS VLR_CBS_CONS_PESSOAL_SAI
		,'' AS CH_ACESSO_DFE_REFERENCIA_SAI
		,0 AS SEQ_ITEM_DFE_REFERENCIA_SAI	 

        --Itens Perecimento, perda, roubo,furto
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO_DEST 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO_DEST

		--Itens imobilização de item 
		, 0 as VLR_IBS_IMOBILIZ_ITEM
		, 0 as VLR_CBS_IMOBILIZ_ITEM

		--Itens credito combustivel
		,0 AS VLR_IBS_CRED_COMBUST
		,0 AS VLR_CBS_CRED_COMBUST
				  
		-- Itens Apropriação de crédito bens serviços
		,0 AS VLR_CRED_IBS_BENS_SERV
		,0  AS VLR_CRED_CBS_BENS_SERV
		
	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_FORN_NAO_REALIZ_COM_PGTO_ANTECIP_EST_NF_ENT PGTO_ANTECIP_ENT ON 
		  PGTO_ANTECIP_ENT.CD_EMP = EST_NF_ENT.CD_EMP
		  AND PGTO_ANTECIP_ENT.CD_FILIAL =  EST_NF_ENT.CD_FILIAL
		  AND PGTO_ANTECIP_ENT.CD_NF = EST_NF_ENT.CD_NF

		  INNER JOIN EST_NFE_FORN_NAO_REALIZ_COM_PGTO_ANTECIP PGTO_ANTECIP ON 
		  PGTO_ANTECIP.CD_CTR_FORN_NAO_REALIZ_COM_PGTO_ANTECIP  = PGTO_ANTECIP_ENT.CD_CTR_FORN_NAO_REALIZ_COM_PGTO_ANTECIP

		  INNER JOIN EST_NFE_FORN_NAO_REALIZ_COM_PGTO_ANTECIP_IT PGTO_ANTECIP_IT ON 
		  PGTO_ANTECIP.CD_CTR_FORN_NAO_REALIZ_COM_PGTO_ANTECIP = PGTO_ANTECIP_IT.CD_CTR_FORN_NAO_REALIZ_COM_PGTO_ANTECIP

		  INNER JOIN  EST_NF_ENT_IT_IBS_CBS IT_IBSCBS ON
		         IT_IBSCBS.CD_EMP = PGTO_ANTECIP_ENT.CD_EMP
		  AND IT_IBSCBS.CD_FILIAL = PGTO_ANTECIP_ENT.CD_FILIAL
		  AND IT_IBSCBS.CD_NF = PGTO_ANTECIP_ENT.CD_NF
		  AND IT_IBSCBS.CD_IT = PGTO_ANTECIP_IT.SEQ_ITEM 
		  AND IT_IBSCBS.CD_PROD = PGTO_ANTECIP_IT.CD_PROD
		  
		  INNER JOIN EST_NF_ENT_IT IT  ON
		         IT_IBSCBS.CD_EMP = IT.CD_EMP
		  AND IT_IBSCBS.CD_FILIAL = IT.CD_FILIAL
		  AND IT_IBSCBS.CD_NF = IT.CD_NF
		  AND IT_IBSCBS.CD_IT = IT.SEQ_ITEM 
		  AND IT_IBSCBS.CD_PROD = IT.CD_PROD
       
		  INNER JOIN EST_PROD ON EST_PROD.CD_PROD =  PGTO_ANTECIP_IT.CD_PROD

		 INNER JOIN GLB_USU ON  
         PGTO_ANTECIP.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_FORN_NAO_REALIZ_COM_PGTO_ANTECIP_VALIDADA NFE_PGTO_ANTECIP ON  
         PGTO_ANTECIP.CD_CTR_FORN_NAO_REALIZ_COM_PGTO_ANTECIP = NFE_PGTO_ANTECIP.CD_CTR_FORN_NAO_REALIZ_COM_PGTO_ANTECIP
		 
		 LEFT JOIN EST_NFE_FORN_NAO_REALIZ_COM_PGTO_ANTECIP_ARQ_DANFE ARQ_DANFE ON 
		 ARQ_DANFE.CD_CTR_FORN_NAO_REALIZ_COM_PGTO_ANTECIP = PGTO_ANTECIP.CD_CTR_FORN_NAO_REALIZ_COM_PGTO_ANTECIP
		 
	    LEFT JOIN NFE..EST_NFE_FORNNAOREALIZCOMPGTOANTECIP NFE_NFE_PGTO_ANTECIP ON 
		NFE_NFE_PGTO_ANTECIP.NRO = PGTO_ANTECIP.CD_CTR_FORN_NAO_REALIZ_COM_PGTO_ANTECIP

		LEFT JOIN  NFE..EST_NFE_FORNNAOREALIZCOMPGTOANTECIPIT AS  NFE_NFE_FORNNAOREALIZCOMPGTOANTECIPIT ON 
		NFE_NFE_FORNNAOREALIZCOMPGTOANTECIPIT.ID = NFE_NFE_PGTO_ANTECIP.ID
		AND PGTO_ANTECIP_IT.SEQ_ITEM = NFE_NFE_FORNNAOREALIZCOMPGTOANTECIPIT.SEQ_ITEM

		LEFT JOIN EST_NFE_CANCEL_EVENTO CANCEL_EVENTO ON  
		CANCEL_EVENTO.CD_CTR_EVENTO = PGTO_ANTECIP.CD_CTR_FORN_NAO_REALIZ_COM_PGTO_ANTECIP 
		AND CANCEL_EVENTO.TIPO_EVENTO = '112140'
		LEFT JOIN  EST_NFE_CANCEL_EVENTO_VALIDADA CANCEL_VALIDADA ON 
		CANCEL_VALIDADA.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO

UNION ALL
		
	     SELECT '112150' AS COD_EVENTO
		, 5 AS CD_EVENTO
		, 'Atualização da data de previsão de entrega' AS DS_EVENTO
		, 'Atualização da data de previsão de entrega' 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_PREV_ENTREGA.NR_AUTORIZADOR,'') AS NR_AUTORIZADOR
		, ISNULL(NFE_PREV_ENTREGA.NR_PROTOCOLO,'') AS NR_PROTOCOLO
		, ISNULL(NFE_PREV_ENTREGA.STS_IMP,0) AS STS_IMP
		, ISNULL(NFE_PREV_ENTREGA.DT_RECBTO,'') AS DT_RECBTO
		, PREV_ENTREGA.DT_HR_EVENTO AS DT_ARQUIVO
		, ISNULL(NFE_NFE_PREV_ENTREGA.DS_MOTIVO, '') AS DS_MOTIVO
		, GLB_USU.NM_USU
		, GLB_MOV_OP.DS_MOV_OP
		, PREV_ENTREGA.CD_CTR_ATUALIZA_DATA_PREVISAO_ENTREGA  AS NFE_CTR
		, 0 AS CTR_ORIG_CANCEL
		, '' AS COD_EVENTO_ORIG_CANCEL
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ_ARQ_DANFE
		, GLB_MOV_OP.CD_MOV_OP
		, CASE WHEN ISNULL (CANCEL_VALIDADA.STS_IMP ,0) = 1 THEN 1 ELSE 0 END AS EVENTO_CANCELADO
		,'NÃO SE APLICA' AS IND_ACEITACAO
		
		--Itens geral : para este evento não contém itens 
		, 0  AS SEQ_ITEM 
		, 0  AS CD_PROD 
		, '' AS DS_PROD
		, '' AS UNID
		, 0  AS QTDE_NF
		, 0  AS QTDE
		, 0  AS VLR_IBS
		, 0  AS VLR_CBS
		
		--Itens Perecimento, perda, roubo,furto durante transporte. Para este evento não contém itens 

		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO  
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO  
		
      --Itens Fornecimento não realizado com pagamento antecipado
		,0 AS VLR_IBS_NAO_FORNECIDA  
		,0 AS VLR_CBS_NAO_FORNECIDA
		,  DT_PREVISAO_ENTREGA 

		-- Itens Apropriação de crédito presumido
		,0 AS C_CRED_PRES_IBS
		,0 AS P_CRED_PRES_IBS
		,0 AS V_CRED_PRES_IBS
		,0 AS C_CRED_PRES_CBS
		,0 AS P_CRED_PRES_CBS
		,0 AS V_CRED_PRES_CBS

		 -- Itens consumo pessoal Entrada
		,0 AS  VLR_IBS_CONS_PESSOAL_ENT
		,0 AS VLR_CBS_CONS_PESSOAL_ENT  
		,'' AS CH_ACESSO_DFE_REFERENCIA_ENT  
		,0 AS SEQ_ITEM_DFE_REFERENCIA_ENT  

		-- Itens consumo pessoal Saída
		,0 AS VLR_IBS_CONS_PESSOAL_SAI
		,0 AS VLR_CBS_CONS_PESSOAL_SAI
		,'' AS CH_ACESSO_DFE_REFERENCIA_SAI
		,0 AS SEQ_ITEM_DFE_REFERENCIA_SAI	
		

        --Itens Perecimento, perda, roubo,furto
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO_DEST 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO_DEST

		--Itens imobilização de item 
		, 0 as VLR_IBS_IMOBILIZ_ITEM
		, 0 as VLR_CBS_IMOBILIZ_ITEM

		--Itens credito combustivel
		,0 AS VLR_IBS_CRED_COMBUST
		,0 AS VLR_CBS_CRED_COMBUST
				  
		-- Itens Apropriação de crédito bens serviços
		,0 AS VLR_CRED_IBS_BENS_SERV
		,0  AS VLR_CRED_CBS_BENS_SERV 


	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_ATUALIZA_DATA_PREVISAO_ENTREGA_EST_NF_ENT PREV_ENTREGA_ENT ON 
		  PREV_ENTREGA_ENT.CD_EMP = EST_NF_ENT.CD_EMP
		  AND PREV_ENTREGA_ENT.CD_FILIAL =  EST_NF_ENT.CD_FILIAL
		  AND PREV_ENTREGA_ENT.CD_NF = EST_NF_ENT.CD_NF

		  INNER JOIN EST_NFE_ATUALIZA_DATA_PREVISAO_ENTREGA PREV_ENTREGA ON 
		  PREV_ENTREGA.CD_CTR_ATUALIZA_DATA_PREVISAO_ENTREGA   = PREV_ENTREGA_ENT.CD_CTR_ATUALIZA_DATA_PREVISAO_ENTREGA 
		 

		 INNER JOIN GLB_USU ON  
         PREV_ENTREGA.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_ATUALIZA_DATA_PREVISAO_ENTREGA_VALIDADA NFE_PREV_ENTREGA  ON  
         PREV_ENTREGA.CD_CTR_ATUALIZA_DATA_PREVISAO_ENTREGA  = NFE_PREV_ENTREGA.CD_CTR_ATUALIZA_DATA_PREVISAO_ENTREGA 
		 
		 LEFT JOIN EST_NFE_ATUALIZA_DATA_PREVISAO_ENTREGA_ARQ_DANFE  ARQ_DANFE ON 
		 ARQ_DANFE.CD_CTR_ATUALIZA_DATA_PREVISAO_ENTREGA  = PREV_ENTREGA.CD_CTR_ATUALIZA_DATA_PREVISAO_ENTREGA 
		 
	    LEFT JOIN NFE..EST_NFE_ATUALIZADATAPREVISAOENTREGA NFE_NFE_PREV_ENTREGA ON 
		NFE_NFE_PREV_ENTREGA.NRO = PREV_ENTREGA.CD_CTR_ATUALIZA_DATA_PREVISAO_ENTREGA

        LEFT JOIN EST_NFE_CANCEL_EVENTO CANCEL_EVENTO ON  
		   CANCEL_EVENTO.CD_CTR_EVENTO = PREV_ENTREGA.CD_CTR_ATUALIZA_DATA_PREVISAO_ENTREGA
		   AND CANCEL_EVENTO.TIPO_EVENTO = '112150'
		LEFT JOIN  EST_NFE_CANCEL_EVENTO_VALIDADA CANCEL_VALIDADA ON 
		   CANCEL_VALIDADA.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO


 
UNION ALL
		
	    SELECT '211110' AS COD_EVENTO
		, 6 AS CD_EVENTO
		, 'Solicitação de Apropriação de crédito presumido' AS DS_EVENTO
		, 'Solicitação de Apropriação de crédito presumido' AS DS_EVENTO_SIMP
		, EST_NF_SAI.CD_EMP
		, EST_NF_SAI.CD_FILIAL
		, EST_NF_SAI.CD_NF
		, EST_NF_SAI.NF_NF AS NR_NF
		, 1 AS NF_EMISSAO_PROPRIA
		, EST_NF_SAI.CD_NF_SERIE
		, ISNULL(EST_NF_SERIE.SERIE, '') AS SERIE
		, ISNULL(EST_NF_SERIE.MODELO, '') AS MODELO
		, EST_NF_SAI.CD_CLI AS CD_ORIG
		, V_GLB_PESSOA.RAZAO_SOCIAL_PESSOA AS NM_ORIG
		, EST_NF_SAI.DT_EMI_NF AS  DT_ENT 
		, EST_NF_SAI.DT_EMI_NF
		,'SAÍDA' AS TIPO_NF  
		, EST_NF_SAI.VLR_NF
		, ISNULL(NFE_APROP_CRED_PRESUMIDO.NR_AUTORIZADOR,'') AS NR_AUTORIZADOR
		, ISNULL(NFE_APROP_CRED_PRESUMIDO.NR_PROTOCOLO,'') AS NR_PROTOCOLO
		, ISNULL(NFE_APROP_CRED_PRESUMIDO.STS_IMP,0) AS STS_IMP
		, ISNULL(NFE_APROP_CRED_PRESUMIDO.DT_RECBTO,'') AS DT_RECBTO
		, APROP_CRED_PRESUMIDO.DT_HR_EVENTO AS DT_ARQUIVO
		, ISNULL(NFE_NFE_APROP_CRED_PRESUMIDO.DS_MOTIVO, '') AS DS_MOTIVO
		, GLB_USU.NM_USU
		, GLB_MOV_OP.DS_MOV_OP
		, NFE_APROP_CRED_PRESUMIDO.CD_CTR_SOLICIT_APROP_CRED_PRESUMIDO  AS NFE_CTR
		, 0 AS CTR_ORIG_CANCEL
		, '' AS COD_EVENTO_ORIG_CANCEL
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ_ARQ_DANFE
		, GLB_MOV_OP.CD_MOV_OP
		, CASE WHEN ISNULL (CANCEL_VALIDADA.STS_IMP ,0) = 1 THEN 1 ELSE 0 END AS EVENTO_CANCELADO
		,'NÃO SE APLICA' AS IND_ACEITACAO
		
		--Itens geral : para este evento não contém itens 
        , APROP_CRED_PRESUMIDO_IT.SEQ_ITEM 
		, APROP_CRED_PRESUMIDO_IT.CD_PROD
		, EST_PROD.DS_PROD
		, '' AS  UNID
		, 0 AS QTDE
		, IT.QT_IT AS  QTDE_NF
		, IT_IBSCBS.VLR_IBS  
		, IT_IBSCBS.VLR_CBS
		
		--Itens Perecimento, perda, roubo,furto durante transporte. Para este evento não contém itens 

		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO  
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO  
		
      --Itens Fornecimento não realizado com pagamento antecipado
		,0 AS VLR_IBS_NAO_FORNECIDA  
		,0 AS VLR_CBS_NAO_FORNECIDA
		,'' AS  DT_PREVISAO_ENTREGA
		
		-- Itens Apropriação de crédito presumido

		,APROP_CRED_PRESUMIDO_IT.C_CRED_PRES_IBS
		,APROP_CRED_PRESUMIDO_IT.P_CRED_PRES_IBS
		,APROP_CRED_PRESUMIDO_IT.V_CRED_PRES_IBS
		,APROP_CRED_PRESUMIDO_IT.C_CRED_PRES_CBS
		,APROP_CRED_PRESUMIDO_IT.P_CRED_PRES_CBS
		,APROP_CRED_PRESUMIDO_IT.V_CRED_PRES_CBS

		 -- Itens consumo pessoal Entrada
		,0 AS  VLR_IBS_CONS_PESSOAL_ENT
		,0 AS VLR_CBS_CONS_PESSOAL_ENT  
		,'' AS CH_ACESSO_DFE_REFERENCIA_ENT  
		,0 AS SEQ_ITEM_DFE_REFERENCIA_ENT  

		-- Itens consumo pessoal Saída
		,0 AS VLR_IBS_CONS_PESSOAL_SAI
		,0 AS VLR_CBS_CONS_PESSOAL_SAI
		,'' AS CH_ACESSO_DFE_REFERENCIA_SAI
		,0 AS SEQ_ITEM_DFE_REFERENCIA_SAI

	  
        --Itens Perecimento, perda, roubo,furto
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO_DEST 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO_DEST

		--Itens imobilização de item 
		, 0 as VLR_IBS_IMOBILIZ_ITEM
		, 0 as VLR_CBS_IMOBILIZ_ITEM

		--Itens credito combustivel
		,0 AS VLR_IBS_CRED_COMBUST
		,0 AS VLR_CBS_CRED_COMBUST
				  
		-- Itens Apropriação de crédito bens serviços
		,0 AS VLR_CRED_IBS_BENS_SERV
		,0  AS VLR_CRED_CBS_BENS_SERV 
		
	 
	FROM  DBO.EST_NF_SAI
		 INNER JOIN PRC_FILIAL FL ON DBO.EST_NF_SAI.CD_EMP = FL.CD_EMP
									 AND DBO.EST_NF_SAI.CD_FILIAL = FL.CD_FILIAL
  
	     INNER JOIN DBO.GLB_MOV_OP ON DBO.EST_NF_SAI.CD_NT_MOV = DBO.GLB_MOV_OP.CD_MOV_OP
		  
		 OUTER APPLY UFD_GLB_PESSOA(EST_NF_SAI.CD_CLI, GLB_MOV_OP.DEST_ORIG) V_GLB_PESSOA
 
		 INNER JOIN DBO.EST_NF_SERIE ON DBO.EST_NF_SAI.CD_EMP = DBO.EST_NF_SERIE.CD_EMP
										AND DBO.EST_NF_SAI.CD_NF_SERIE = DBO.EST_NF_SERIE.CD_NF_SERIE

		 INNER JOIN EST_NFE_SOLICIT_APROP_CRED_PRESUMIDO_EST_NF_SAI APROP_CRED_PRESUMIDO_SAI ON 
		  APROP_CRED_PRESUMIDO_SAI.CD_EMP = EST_NF_SAI.CD_EMP
		  AND APROP_CRED_PRESUMIDO_SAI.CD_FILIAL =  EST_NF_SAI.CD_FILIAL
		  AND APROP_CRED_PRESUMIDO_SAI.CD_NF = EST_NF_SAI.CD_NF

		  INNER JOIN EST_NFE_SOLICIT_APROP_CRED_PRESUMIDO APROP_CRED_PRESUMIDO ON 
		  APROP_CRED_PRESUMIDO.CD_CTR_SOLICIT_APROP_CRED_PRESUMIDO    = APROP_CRED_PRESUMIDO_SAI.CD_CTR_SOLICIT_APROP_CRED_PRESUMIDO  
		 
		 INNER JOIN EST_NFE_SOLICIT_APROP_CRED_PRESUMIDO_IT APROP_CRED_PRESUMIDO_IT ON 
		 APROP_CRED_PRESUMIDO.CD_CTR_SOLICIT_APROP_CRED_PRESUMIDO = APROP_CRED_PRESUMIDO_IT.CD_CTR_SOLICIT_APROP_CRED_PRESUMIDO

		  left JOIN  EST_NF_SAI_IT_IBS_CBS IT_IBSCBS ON
	      IT_IBSCBS.CD_EMP = APROP_CRED_PRESUMIDO_SAI.CD_EMP
		  AND IT_IBSCBS.CD_FILIAL = APROP_CRED_PRESUMIDO_SAI.CD_FILIAL
		  AND IT_IBSCBS.CD_NF = APROP_CRED_PRESUMIDO_SAI.CD_NF
		  AND IT_IBSCBS.CD_IT = APROP_CRED_PRESUMIDO_IT.SEQ_ITEM 
		  AND IT_IBSCBS.CD_PROD = APROP_CRED_PRESUMIDO_IT.CD_PROD
		  
		  left JOIN EST_NF_SAI_IT IT  ON
		         IT_IBSCBS.CD_EMP = IT.CD_EMP
		  AND IT_IBSCBS.CD_FILIAL = IT.CD_FILIAL
		  AND IT_IBSCBS.CD_NF = IT.CD_NF
		  AND IT_IBSCBS.CD_IT = IT.CD_IT
		  AND IT_IBSCBS.CD_PROD = IT.CD_PROD
       
		  INNER JOIN EST_PROD ON EST_PROD.CD_PROD =  APROP_CRED_PRESUMIDO_IT.CD_PROD

		 INNER JOIN GLB_USU ON  
         APROP_CRED_PRESUMIDO.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_SAI.CD_EMP
													AND EST_NF_ENT_EF_NF_CTR_NF.CD_FILIAL = EST_NF_SAI.CD_FILIAL
													AND EST_NF_ENT_EF_NF_CTR_NF.CD_NF_ENT = EST_NF_SAI.CD_NF
	   
	     LEFT OUTER JOIN EST_NFE_SOLICIT_APROP_CRED_PRESUMIDO_VALIDADA NFE_APROP_CRED_PRESUMIDO  ON  
         APROP_CRED_PRESUMIDO.CD_CTR_SOLICIT_APROP_CRED_PRESUMIDO  = NFE_APROP_CRED_PRESUMIDO.CD_CTR_SOLICIT_APROP_CRED_PRESUMIDO 
		 

		 LEFT JOIN EST_NFE_SOLICIT_APROP_CRED_PRESUMIDO_ARQ_DANFE  ARQ_DANFE ON 
		 ARQ_DANFE.CD_CTR_SOLICIT_APROP_CRED_PRESUMIDO  = APROP_CRED_PRESUMIDO.CD_CTR_SOLICIT_APROP_CRED_PRESUMIDO 
		 
	    LEFT JOIN NFE..EST_NFE_SOLICITAPROPCREDPRESUMIDO NFE_NFE_APROP_CRED_PRESUMIDO  ON 
		NFE_NFE_APROP_CRED_PRESUMIDO.NRO = APROP_CRED_PRESUMIDO.CD_CTR_SOLICIT_APROP_CRED_PRESUMIDO

        LEFT JOIN EST_NFE_CANCEL_EVENTO CANCEL_EVENTO ON  
		   CANCEL_EVENTO.CD_CTR_EVENTO = APROP_CRED_PRESUMIDO.CD_CTR_SOLICIT_APROP_CRED_PRESUMIDO
		   AND CANCEL_EVENTO.TIPO_EVENTO = '211110'
		LEFT JOIN  EST_NFE_CANCEL_EVENTO_VALIDADA CANCEL_VALIDADA ON 
		CANCEL_VALIDADA.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO

 
UNION ALL
		
	     SELECT '211120' AS COD_EVENTO
		, 7 AS CD_EVENTO
		, 'Destinação de item para consumo pessoal - Entrada' AS DS_EVENTO
		, 'Destinação para consumo pessoal - Entrada' 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_DEST_CONS_PESSOAL.NR_AUTORIZADOR,'') AS NR_AUTORIZADOR
		, ISNULL(NFE_DEST_CONS_PESSOAL.NR_PROTOCOLO,'') AS NR_PROTOCOLO
		, ISNULL(NFE_DEST_CONS_PESSOAL.STS_IMP,0) AS STS_IMP
		, ISNULL(NFE_DEST_CONS_PESSOAL.DT_RECBTO,'') AS DT_RECBTO
		, DEST_CONS_PESSOAL.DT_HR_EVENTO AS DT_ARQUIVO
		, ISNULL(NFE_NFE_DEST_CONS_PESSOAL.DS_MOTIVO, '') AS DS_MOTIVO
		, GLB_USU.NM_USU
		, GLB_MOV_OP.DS_MOV_OP
		, DEST_CONS_PESSOAL.CD_CTR_DEST_ITEM_CONS_PESSOAL AS NFE_CTR
		, 0 AS CTR_ORIG_CANCEL
		, '' AS COD_EVENTO_ORIG_CANCEL
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ_ARQ_DANFE
		, GLB_MOV_OP.CD_MOV_OP
		, CASE WHEN ISNULL (CANCEL_VALIDADA.STS_IMP ,0) = 1 THEN 1 ELSE 0 END AS EVENTO_CANCELADO
		,'NÃO SE APLICA' AS IND_ACEITACAO
		
		--Itens geral 
        , DEST_CONS_PESSOAL_IT.SEQ_ITEM 
		, DEST_CONS_PESSOAL_IT.CD_PROD
		, EST_PROD.DS_PROD
		, DEST_CONS_PESSOAL_IT.UNID_CONSUMO AS UNID
		, DEST_CONS_PESSOAL_IT.QTD_CONSUMO AS QTDE
		, IT.QT_IT AS  QTDE_NF
		, IT_IBSCBS.VLR_IBS  
		, IT_IBSCBS.VLR_CBS

		--Itens Perecimento, perda, roubo,furto durante transporte
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO 

		--Itens transporte Fornecimento não realizado com pagamento antecipado
        ,0 AS VLR_IBS_NAO_FORNECIDA  
        ,0 AS VLR_CBS_NAO_FORNECIDA
		,'' AS DT_PREVISAO_ENTREGA 

	    -- Itens Apropriação de crédito presumido
		,0 AS C_CRED_PRES_IBS
		,0 AS P_CRED_PRES_IBS
		,0 AS V_CRED_PRES_IBS
		,0 AS C_CRED_PRES_CBS
		,0 AS P_CRED_PRES_CBS
		,0 AS V_CRED_PRES_CBS

		-- Itens consumo pessoal Entrada
		,DEST_CONS_PESSOAL_IT.VLR_IBS AS  VLR_IBS_CONS_PESSOAL_ENT
		,DEST_CONS_PESSOAL_IT.VLR_CBS AS VLR_CBS_CONS_PESSOAL_ENT  
		,DEST_CONS_PESSOAL_IT.CH_ACESSO_DFE_REFERENCIA  AS CH_ACESSO_DFE_REFERENCIA_ENT  
		,DEST_CONS_PESSOAL_IT.SEQ_ITEM_DFE_REFERENCIA AS SEQ_ITEM_DFE_REFERENCIA_ENT  

		-- Itens consumo pessoal Saída
		,0 AS VLR_IBS_CONS_PESSOAL_SAI
		,0 AS VLR_CBS_CONS_PESSOAL_SAI
		,'' AS CH_ACESSO_DFE_REFERENCIA_SAI
		,0 AS SEQ_ITEM_DFE_REFERENCIA_SAI
		
	 
	   --Itens Perecimento, perda, roubo,furto
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO_DEST 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO_DEST

		--Itens imobilização de item 
		, 0 as VLR_IBS_IMOBILIZ_ITEM
		, 0 as VLR_CBS_IMOBILIZ_ITEM

		--Itens credito combustivel
		,0 AS VLR_IBS_CRED_COMBUST
		,0 AS VLR_CBS_CRED_COMBUST
				  
		-- Itens Apropriação de crédito bens serviços
		,0 AS VLR_CRED_IBS_BENS_SERV
		,0  AS VLR_CRED_CBS_BENS_SERV 
		
	 
	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_DEST_ITEM_CONS_PESSOAL_EST_NF_ENT DEST_CONS_PESSOAL_ENT ON 
		  DEST_CONS_PESSOAL_ENT.CD_EMP = EST_NF_ENT.CD_EMP
		  AND DEST_CONS_PESSOAL_ENT.CD_FILIAL =  EST_NF_ENT.CD_FILIAL
		  AND DEST_CONS_PESSOAL_ENT.CD_NF = EST_NF_ENT.CD_NF

		  INNER JOIN EST_NFE_DEST_ITEM_CONS_PESSOAL DEST_CONS_PESSOAL ON 
		  DEST_CONS_PESSOAL.CD_CTR_DEST_ITEM_CONS_PESSOAL  = DEST_CONS_PESSOAL_ENT.CD_CTR_DEST_ITEM_CONS_PESSOAL

		  INNER JOIN EST_NFE_DEST_ITEM_CONS_PESSOAL_IT DEST_CONS_PESSOAL_IT ON 
		  DEST_CONS_PESSOAL.CD_CTR_DEST_ITEM_CONS_PESSOAL = DEST_CONS_PESSOAL_IT.CD_CTR_DEST_ITEM_CONS_PESSOAL

		  INNER JOIN  EST_NF_ENT_IT_IBS_CBS IT_IBSCBS ON
		         IT_IBSCBS.CD_EMP = DEST_CONS_PESSOAL_ENT.CD_EMP
		  AND IT_IBSCBS.CD_FILIAL = DEST_CONS_PESSOAL_ENT.CD_FILIAL
		  AND IT_IBSCBS.CD_NF = DEST_CONS_PESSOAL_ENT.CD_NF
		  AND IT_IBSCBS.CD_IT = DEST_CONS_PESSOAL_IT.SEQ_ITEM 
		  AND IT_IBSCBS.CD_PROD = DEST_CONS_PESSOAL_IT.CD_PROD
		  
		  INNER JOIN EST_NF_ENT_IT IT  ON
		         IT_IBSCBS.CD_EMP = IT.CD_EMP
		  AND IT_IBSCBS.CD_FILIAL = IT.CD_FILIAL
		  AND IT_IBSCBS.CD_NF = IT.CD_NF
		  AND IT_IBSCBS.CD_IT = IT.SEQ_ITEM 
		  AND IT_IBSCBS.CD_PROD = IT.CD_PROD
       
		  INNER JOIN EST_PROD ON EST_PROD.CD_PROD =  DEST_CONS_PESSOAL_IT.CD_PROD

		 INNER JOIN GLB_USU ON  
         DEST_CONS_PESSOAL.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_DEST_ITEM_CONS_PESSOAL_VALIDADA NFE_DEST_CONS_PESSOAL  ON  
         DEST_CONS_PESSOAL.CD_CTR_DEST_ITEM_CONS_PESSOAL = NFE_DEST_CONS_PESSOAL.CD_CTR_DEST_ITEM_CONS_PESSOAL
		 
		 LEFT JOIN EST_NFE_DEST_ITEM_CONS_PESSOAL_ARQ_DANFE ARQ_DANFE ON 
		 ARQ_DANFE.CD_CTR_DEST_ITEM_CONS_PESSOAL = DEST_CONS_PESSOAL.CD_CTR_DEST_ITEM_CONS_PESSOAL
		 
	    LEFT JOIN NFE..EST_NFE_DESTITEMCONSPESSOAL NFE_NFE_DEST_CONS_PESSOAL  ON 
		NFE_NFE_DEST_CONS_PESSOAL.NRO = DEST_CONS_PESSOAL.CD_CTR_DEST_ITEM_CONS_PESSOAL

		LEFT JOIN  NFE..EST_NFE_DESTITEMCONSPESSOALIT AS NFE_NFE_DESTITEMCONSPESSOALIT ON 
		NFE_NFE_DESTITEMCONSPESSOALIT.ID = NFE_NFE_DEST_CONS_PESSOAL.ID
		AND DEST_CONS_PESSOAL_IT.SEQ_ITEM = NFE_NFE_DESTITEMCONSPESSOALIT.SEQ_ITEM

		LEFT JOIN EST_NFE_CANCEL_EVENTO CANCEL_EVENTO ON  
		CANCEL_EVENTO.CD_CTR_EVENTO = DEST_CONS_PESSOAL.CD_CTR_DEST_ITEM_CONS_PESSOAL 
		AND CANCEL_EVENTO.TIPO_EVENTO = '211120'
		LEFT JOIN  EST_NFE_CANCEL_EVENTO_VALIDADA CANCEL_VALIDADA ON 
		CANCEL_VALIDADA.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO


UNION ALL 


	SELECT '211120' AS COD_EVENTO
		, 7 AS CD_EVENTO
		, 'Destinação de item para consumo pessoal - Saída' AS DS_EVENTO
		, 'Destinação para consumo pessoal - Saída' AS DS_EVENTO_SIMP
		, EST_NF_SAI.CD_EMP
		, EST_NF_SAI.CD_FILIAL
		, EST_NF_SAI.CD_NF
		, EST_NF_SAI.NF_NF AS NR_NF
		, 1 AS NF_EMISSAO_PROPRIA
		, EST_NF_SAI.CD_NF_SERIE
		, ISNULL(EST_NF_SERIE.SERIE, '') AS SERIE
		, ISNULL(EST_NF_SERIE.MODELO, '') AS MODELO
		, EST_NF_SAI.CD_CLI AS CD_ORIG
		, V_GLB_PESSOA.RAZAO_SOCIAL_PESSOA AS NM_ORIG
		, EST_NF_SAI.DT_EMI_NF AS DT_ENT
		, EST_NF_SAI.DT_EMI_NF
		,'SAÍDA' AS TIPO_NF  
		, EST_NF_SAI.VLR_NF
		, ISNULL(NFE_DEST_CONS_PESSOAL.NR_AUTORIZADOR,'') AS NR_AUTORIZADOR
		, ISNULL(NFE_DEST_CONS_PESSOAL.NR_PROTOCOLO,'') AS NR_PROTOCOLO
		, ISNULL(NFE_DEST_CONS_PESSOAL.STS_IMP,0) AS STS_IMP
		, ISNULL(NFE_DEST_CONS_PESSOAL.DT_RECBTO,'') AS DT_RECBTO
		, DEST_CONS_PESSOAL.DT_HR_EVENTO AS DT_ARQUIVO
		, ISNULL(NFE_NFE_DEST_CONS_PESSOAL.DS_MOTIVO, '') AS DS_MOTIVO
		, GLB_USU.NM_USU
		, GLB_MOV_OP.DS_MOV_OP
		, DEST_CONS_PESSOAL.CD_CTR_DEST_ITEM_CONS_PESSOAL AS NFE_CTR
		, 0 AS CTR_ORIG_CANCEL
		, '' AS COD_EVENTO_ORIG_CANCEL
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ_ARQ_DANFE
		, GLB_MOV_OP.CD_MOV_OP
		, CASE WHEN ISNULL (CANCEL_VALIDADA.STS_IMP ,0) = 1 THEN 1 ELSE 0 END AS EVENTO_CANCELADO
		,'NÃO SE APLICA' AS IND_ACEITACAO
		
		--Itens geral 
        , DEST_CONS_PESSOAL_IT.SEQ_ITEM 
		, DEST_CONS_PESSOAL_IT.CD_PROD
		, EST_PROD.DS_PROD
		, DEST_CONS_PESSOAL_IT.UNID_CONSUMO AS UNID
		, DEST_CONS_PESSOAL_IT.QTD_CONSUMO AS QTDE
		, IT.QT_IT AS  QTDE_NF
		, IT_IBSCBS.VLR_IBS  
		, IT_IBSCBS.VLR_CBS

		--Itens Perecimento, perda, roubo,furto durante transporte
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO 

		--Itens transporte Fornecimento não realizado com pagamento antecipado
        ,0 AS VLR_IBS_NAO_FORNECIDA  
        ,0 AS VLR_CBS_NAO_FORNECIDA
		,'' AS DT_PREVISAO_ENTREGA 

	    -- Itens Apropriação de crédito presumido
		,0 AS C_CRED_PRES_IBS
		,0 AS P_CRED_PRES_IBS
		,0 AS V_CRED_PRES_IBS
		,0 AS C_CRED_PRES_CBS
		,0 AS P_CRED_PRES_CBS
		,0 AS V_CRED_PRES_CBS

		-- Itens consumo pessoal Entrada
		,0 AS VLR_IBS_CONS_PESSOAL_ENT
		,0 AS VLR_CBS_CONS_PESSOAL_ENT
		,'' AS CH_ACESSO_DFE_REFERENCIA_ENT
		,0 AS SEQ_ITEM_DFE_REFERENCIA_ENT  

		-- Itens consumo pessoal Saída
		,DEST_CONS_PESSOAL_IT.VLR_IBS AS  VLR_IBS_CONS_PESSOAL_SAI
		,DEST_CONS_PESSOAL_IT.VLR_CBS AS VLR_CBS_CONS_PESSOAL_SAI  
		,DEST_CONS_PESSOAL_IT.CH_ACESSO_DFE_REFERENCIA  AS CH_ACESSO_DFE_REFERENCIA_SAI  
		,DEST_CONS_PESSOAL_IT.SEQ_ITEM_DFE_REFERENCIA AS SEQ_ITEM_DFE_REFERENCIA_SAI

		--Itens Perecimento, perda, roubo,furto
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO_DEST 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO_DEST

		--Itens imobilização de item 
		, 0 as VLR_IBS_IMOBILIZ_ITEM
		, 0 as VLR_CBS_IMOBILIZ_ITEM

		--Itens credito combustivel
		,0 AS VLR_IBS_CRED_COMBUST
		,0 AS VLR_CBS_CRED_COMBUST
				  
		-- Itens Apropriação de crédito bens serviços
		,0 AS VLR_CRED_IBS_BENS_SERV
		,0  AS VLR_CRED_CBS_BENS_SERV 
		
	 
	FROM  DBO.EST_NF_SAI
		 INNER JOIN PRC_FILIAL FL ON DBO.EST_NF_SAI.CD_EMP = FL.CD_EMP
									 AND DBO.EST_NF_SAI.CD_FILIAL = FL.CD_FILIAL
  
	     INNER JOIN DBO.GLB_MOV_OP ON DBO.EST_NF_SAI.CD_NT_MOV = DBO.GLB_MOV_OP.CD_MOV_OP
		  
		 OUTER APPLY UFD_GLB_PESSOA(EST_NF_SAI.CD_CLI, GLB_MOV_OP.DEST_ORIG) V_GLB_PESSOA
 
		 INNER JOIN DBO.EST_NF_SERIE ON DBO.EST_NF_SAI.CD_EMP = DBO.EST_NF_SERIE.CD_EMP
										AND DBO.EST_NF_SAI.CD_NF_SERIE = DBO.EST_NF_SERIE.CD_NF_SERIE

		 INNER JOIN EST_NFE_DEST_ITEM_CONS_PESSOAL_EST_NF_SAI DEST_CONS_PESSOAL_SAI ON 
		  DEST_CONS_PESSOAL_SAI.CD_EMP = EST_NF_SAI.CD_EMP
		  AND DEST_CONS_PESSOAL_SAI.CD_FILIAL =  EST_NF_SAI.CD_FILIAL
		  AND DEST_CONS_PESSOAL_SAI.CD_NF = EST_NF_SAI.CD_NF

		  INNER JOIN EST_NFE_DEST_ITEM_CONS_PESSOAL DEST_CONS_PESSOAL ON 
		  DEST_CONS_PESSOAL.CD_CTR_DEST_ITEM_CONS_PESSOAL  = DEST_CONS_PESSOAL_SAI.CD_CTR_DEST_ITEM_CONS_PESSOAL

		  INNER JOIN EST_NFE_DEST_ITEM_CONS_PESSOAL_IT DEST_CONS_PESSOAL_IT ON 
		  DEST_CONS_PESSOAL.CD_CTR_DEST_ITEM_CONS_PESSOAL = DEST_CONS_PESSOAL_IT.CD_CTR_DEST_ITEM_CONS_PESSOAL

		  LEFT JOIN  EST_NF_SAI_IT_IBS_CBS IT_IBSCBS ON
		         IT_IBSCBS.CD_EMP = DEST_CONS_PESSOAL_SAI.CD_EMP
		  AND IT_IBSCBS.CD_FILIAL = DEST_CONS_PESSOAL_SAI.CD_FILIAL
		  AND IT_IBSCBS.CD_NF = DEST_CONS_PESSOAL_SAI.CD_NF
		  AND IT_IBSCBS.CD_IT = DEST_CONS_PESSOAL_IT.SEQ_ITEM 
		  AND IT_IBSCBS.CD_PROD = DEST_CONS_PESSOAL_IT.CD_PROD
		  
		  LEFT JOIN EST_NF_SAI_IT IT  ON
		         IT_IBSCBS.CD_EMP = IT.CD_EMP
		  AND IT_IBSCBS.CD_FILIAL = IT.CD_FILIAL
		  AND IT_IBSCBS.CD_NF = IT.CD_NF
		  AND IT_IBSCBS.CD_IT = IT.CD_IT
		  AND IT_IBSCBS.CD_PROD = IT.CD_PROD
       
		  INNER JOIN EST_PROD ON EST_PROD.CD_PROD =  DEST_CONS_PESSOAL_IT.CD_PROD

		 INNER JOIN GLB_USU ON  
         DEST_CONS_PESSOAL.CD_USU = GLB_USU.CD_USU 
       
	     LEFT OUTER JOIN EST_NFE_DEST_ITEM_CONS_PESSOAL_VALIDADA NFE_DEST_CONS_PESSOAL  ON  
         DEST_CONS_PESSOAL.CD_CTR_DEST_ITEM_CONS_PESSOAL = NFE_DEST_CONS_PESSOAL.CD_CTR_DEST_ITEM_CONS_PESSOAL
		 
		 LEFT JOIN EST_NFE_DEST_ITEM_CONS_PESSOAL_ARQ_DANFE ARQ_DANFE ON 
		 ARQ_DANFE.CD_CTR_DEST_ITEM_CONS_PESSOAL = DEST_CONS_PESSOAL.CD_CTR_DEST_ITEM_CONS_PESSOAL
		 
	    LEFT JOIN NFE..EST_NFE_DESTITEMCONSPESSOAL NFE_NFE_DEST_CONS_PESSOAL  ON 
		NFE_NFE_DEST_CONS_PESSOAL.NRO = DEST_CONS_PESSOAL.CD_CTR_DEST_ITEM_CONS_PESSOAL

		LEFT JOIN  NFE..EST_NFE_DESTITEMCONSPESSOALIT AS NFE_NFE_DESTITEMCONSPESSOALIT ON 
		NFE_NFE_DESTITEMCONSPESSOALIT.ID = NFE_NFE_DEST_CONS_PESSOAL.ID
		AND DEST_CONS_PESSOAL_IT.SEQ_ITEM = NFE_NFE_DESTITEMCONSPESSOALIT.SEQ_ITEM

		LEFT JOIN EST_NFE_CANCEL_EVENTO CANCEL_EVENTO ON  
		CANCEL_EVENTO.CD_CTR_EVENTO = DEST_CONS_PESSOAL.CD_CTR_DEST_ITEM_CONS_PESSOAL 
		AND CANCEL_EVENTO.TIPO_EVENTO = '211120'
		LEFT JOIN  EST_NFE_CANCEL_EVENTO_VALIDADA CANCEL_VALIDADA ON 
		CANCEL_VALIDADA.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO

UNION ALL

SELECT '211124' AS COD_EVENTO
		, 8 AS CD_EVENTO
		, 'Perecimento, perda, roubo ou furto' AS DS_EVENTO
		, 'Perecimento, perda, roubo ou furto' AS DS_EVENTO_SIMP
		, EST_NF_SAI.CD_EMP
		, EST_NF_SAI.CD_FILIAL
		, EST_NF_SAI.CD_NF
		, EST_NF_SAI.NF_NF AS NR_NF
		, 1 AS NF_EMISSAO_PROPRIA
		, EST_NF_SAI.CD_NF_SERIE
		, ISNULL(EST_NF_SERIE.SERIE, '') AS SERIE
		, ISNULL(EST_NF_SERIE.MODELO, '') AS MODELO
		, EST_NF_SAI.CD_CLI AS CD_ORIG
		, V_GLB_PESSOA.RAZAO_SOCIAL_PESSOA AS NM_ORIG
		, EST_NF_SAI.DT_EMI_NF AS DT_ENT
		, EST_NF_SAI.DT_EMI_NF
		,'SAÍDA' AS TIPO_NF  
		, EST_NF_SAI.VLR_NF
		, ISNULL(NFE_PERDA_ROUBO_DEST.NR_AUTORIZADOR,'') AS NR_AUTORIZADOR
		, ISNULL(NFE_PERDA_ROUBO_DEST.NR_PROTOCOLO,'') AS NR_PROTOCOLO
		, ISNULL(NFE_PERDA_ROUBO_DEST.STS_IMP,0) AS STS_IMP
		, ISNULL(NFE_PERDA_ROUBO_DEST.DT_RECBTO,'') AS DT_RECBTO
		, PERDA_ROUBO_DEST.DT_HR_EVENTO AS DT_ARQUIVO
		, ISNULL(NFE_NFE_PERDA_ROUBO_DEST.DS_MOTIVO, '') AS DS_MOTIVO
		, GLB_USU.NM_USU
		, GLB_MOV_OP.DS_MOV_OP
		, PERDA_ROUBO_DEST.CD_CTR_PERDA_ROUBO_TRANSP_DEST AS NFE_CTR
		, 0 AS CTR_ORIG_CANCEL
		, '' AS COD_EVENTO_ORIG_CANCEL
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ_ARQ_DANFE
		, GLB_MOV_OP.CD_MOV_OP
		, CASE WHEN ISNULL (CANCEL_VALIDADA.STS_IMP ,0) = 1 THEN 1 ELSE 0 END AS EVENTO_CANCELADO
		,'NÃO SE APLICA' AS IND_ACEITACAO
		
		--Itens geral 
        , PERDA_ROUBO_DEST_IT.SEQ_ITEM 
		, PERDA_ROUBO_DEST_IT.CD_PROD
		, EST_PROD.DS_PROD
		, PERDA_ROUBO_DEST_IT.UNID_PEREC AS UNID
		, PERDA_ROUBO_DEST_IT.QTD_PEREC AS QTDE
		, IT.QT_IT AS  QTDE_NF
		, IT_IBSCBS.VLR_IBS  
		, IT_IBSCBS.VLR_CBS

		--Itens Perecimento, perda, roubo,furto durante transporte
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO 

		--Itens transporte Fornecimento não realizado com pagamento antecipado
        ,0 AS VLR_IBS_NAO_FORNECIDA  
        ,0 AS VLR_CBS_NAO_FORNECIDA
		,'' AS DT_PREVISAO_ENTREGA 

	    -- Itens Apropriação de crédito presumido
		,0 AS C_CRED_PRES_IBS
		,0 AS P_CRED_PRES_IBS
		,0 AS V_CRED_PRES_IBS
		,0 AS C_CRED_PRES_CBS
		,0 AS P_CRED_PRES_CBS
		,0 AS V_CRED_PRES_CBS

		-- Itens consumo pessoal Entrada
		,0 AS VLR_IBS_CONS_PESSOAL_ENT
		,0 AS VLR_CBS_CONS_PESSOAL_ENT
		,'' AS CH_ACESSO_DFE_REFERENCIA_ENT
		,0 AS SEQ_ITEM_DFE_REFERENCIA_ENT  

		-- Itens consumo pessoal Saída
		,0 AS  VLR_IBS_CONS_PESSOAL_SAI
		,0 AS VLR_CBS_CONS_PESSOAL_SAI  
		,'' AS CH_ACESSO_DFE_REFERENCIA_SAI  
		,0 AS SEQ_ITEM_DFE_REFERENCIA_SAI

		--Itens Perecimento, perda, roubo,furto
		, PERDA_ROUBO_DEST_IT.VLR_IBS_PEREC AS VLR_IBS_PEREC_PERDA_ROUBO_DEST 
		, PERDA_ROUBO_DEST_IT.VLR_CBS_PEREC AS VLR_CBS_PEREC_PERDA_ROUBO_DEST

		--Itens imobilização de item 
		, 0 as VLR_IBS_IMOBILIZ_ITEM
		, 0 as VLR_CBS_IMOBILIZ_ITEM

		--Itens credito combustivel
		,0 AS VLR_IBS_CRED_COMBUST
		,0 AS VLR_CBS_CRED_COMBUST
				  
		-- Itens Apropriação de crédito bens serviços
		,0 AS VLR_CRED_IBS_BENS_SERV
		,0  AS VLR_CRED_CBS_BENS_SERV 
	 
	FROM  DBO.EST_NF_SAI
		 INNER JOIN PRC_FILIAL FL ON DBO.EST_NF_SAI.CD_EMP = FL.CD_EMP
									 AND DBO.EST_NF_SAI.CD_FILIAL = FL.CD_FILIAL
  
	     INNER JOIN DBO.GLB_MOV_OP ON DBO.EST_NF_SAI.CD_NT_MOV = DBO.GLB_MOV_OP.CD_MOV_OP
		  
		 OUTER APPLY UFD_GLB_PESSOA(EST_NF_SAI.CD_CLI, GLB_MOV_OP.DEST_ORIG) V_GLB_PESSOA
 
		 INNER JOIN DBO.EST_NF_SERIE ON DBO.EST_NF_SAI.CD_EMP = DBO.EST_NF_SERIE.CD_EMP
										AND DBO.EST_NF_SAI.CD_NF_SERIE = DBO.EST_NF_SERIE.CD_NF_SERIE

		 INNER JOIN EST_NFE_PEREC_PERDA_ROUBO_TRANSP_DEST_EST_NF_SAI PERDA_ROUBO_DEST_SAI ON 
		  PERDA_ROUBO_DEST_SAI.CD_EMP = EST_NF_SAI.CD_EMP
		  AND PERDA_ROUBO_DEST_SAI.CD_FILIAL =  EST_NF_SAI.CD_FILIAL
		  AND PERDA_ROUBO_DEST_SAI.CD_NF = EST_NF_SAI.CD_NF

		  INNER JOIN EST_NFE_PEREC_PERDA_ROUBO_TRANSP_DEST PERDA_ROUBO_DEST ON 
		  PERDA_ROUBO_DEST.CD_CTR_PERDA_ROUBO_TRANSP_DEST  = PERDA_ROUBO_DEST_SAI.CD_CTR_PERDA_ROUBO_TRANSP_DEST

		  INNER JOIN EST_NFE_PEREC_PERDA_ROUBO_TRANSP_DEST_IT PERDA_ROUBO_DEST_IT ON 
		  PERDA_ROUBO_DEST.CD_CTR_PERDA_ROUBO_TRANSP_DEST = PERDA_ROUBO_DEST_IT.CD_CTR_PERDA_ROUBO_TRANSP_DEST

		  LEFT JOIN  EST_NF_SAI_IT_IBS_CBS IT_IBSCBS ON
		         IT_IBSCBS.CD_EMP = PERDA_ROUBO_DEST_SAI.CD_EMP
		  AND IT_IBSCBS.CD_FILIAL = PERDA_ROUBO_DEST_SAI.CD_FILIAL
		  AND IT_IBSCBS.CD_NF = PERDA_ROUBO_DEST_SAI.CD_NF
		  AND IT_IBSCBS.CD_IT = PERDA_ROUBO_DEST_IT.SEQ_ITEM 
		  AND IT_IBSCBS.CD_PROD = PERDA_ROUBO_DEST_IT.CD_PROD
		  
		  LEFT JOIN EST_NF_SAI_IT IT  ON
		         IT_IBSCBS.CD_EMP = IT.CD_EMP
		  AND IT_IBSCBS.CD_FILIAL = IT.CD_FILIAL
		  AND IT_IBSCBS.CD_NF = IT.CD_NF
		  AND IT_IBSCBS.CD_IT = IT.CD_IT
		  AND IT_IBSCBS.CD_PROD = IT.CD_PROD
       
		  INNER JOIN EST_PROD ON EST_PROD.CD_PROD =  PERDA_ROUBO_DEST_IT.CD_PROD

		 INNER JOIN GLB_USU ON  
         PERDA_ROUBO_DEST.CD_USU = GLB_USU.CD_USU 
       
	     LEFT OUTER JOIN EST_NFE_PEREC_PERDA_ROUBO_TRANSP_DEST_VALIDADA NFE_PERDA_ROUBO_DEST  ON  
         PERDA_ROUBO_DEST.CD_CTR_PERDA_ROUBO_TRANSP_DEST = NFE_PERDA_ROUBO_DEST.CD_CTR_PERDA_ROUBO_TRANSP_DEST
		 
		 LEFT JOIN EST_NFE_PEREC_PERDA_ROUBO_TRANSP_DEST_ARQ_DANFE ARQ_DANFE ON 
		 ARQ_DANFE.CD_CTR_PEREC_PERDA_ROUBO_TRANSP_DEST  = PERDA_ROUBO_DEST.CD_CTR_PERDA_ROUBO_TRANSP_DEST
		 
	    LEFT JOIN NFE..EST_NFE_PERECPERDAROUBOTRANSPDEST NFE_NFE_PERDA_ROUBO_DEST   ON 
		NFE_NFE_PERDA_ROUBO_DEST.NRO = PERDA_ROUBO_DEST.CD_CTR_PERDA_ROUBO_TRANSP_DEST

		LEFT JOIN  NFE..EST_NFE_PERECPERDAROUBOTRANSPDESTIT AS NFE_NFE_PERDA_ROUBO_DEST_IT ON 
		   NFE_NFE_PERDA_ROUBO_DEST_IT.ID = NFE_NFE_PERDA_ROUBO_DEST.ID
		AND PERDA_ROUBO_DEST_IT.SEQ_ITEM = NFE_NFE_PERDA_ROUBO_DEST_IT.SEQ_ITEM

		LEFT JOIN EST_NFE_CANCEL_EVENTO CANCEL_EVENTO ON  
		CANCEL_EVENTO.CD_CTR_EVENTO = PERDA_ROUBO_DEST.CD_CTR_PERDA_ROUBO_TRANSP_DEST 
		AND CANCEL_EVENTO.TIPO_EVENTO = '211124'
		LEFT JOIN  EST_NFE_CANCEL_EVENTO_VALIDADA CANCEL_VALIDADA ON 
		CANCEL_VALIDADA.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO

UNION ALL


	SELECT '211128' AS COD_EVENTO
		, 9 AS CD_EVENTO --ACEITE DE DÉBITO NA APURAÇÃO POR EMISSÃO DE NOTA DE CRÉDITO
		, 'Aceite de debito na apuração por emissão de nota de credito' AS DS_EVENTO
		, 'Aceite de debito na apuração por emissão de nota de credito' AS DS_EVENTO_SIMP
		, EST_NF_SAI.CD_EMP
		, EST_NF_SAI.CD_FILIAL
		, EST_NF_SAI.CD_NF
		, EST_NF_SAI.NF_NF AS NR_NF
		, 1 AS NF_EMISSAO_PROPRIA
		, EST_NF_SAI.CD_NF_SERIE
		, ISNULL(EST_NF_SERIE.SERIE, '') AS SERIE
		, ISNULL(EST_NF_SERIE.MODELO, '') AS MODELO
		, EST_NF_SAI.CD_CLI AS CD_ORIG
		, V_GLB_PESSOA.RAZAO_SOCIAL_PESSOA AS NM_ORIG
		, EST_NF_SAI.DT_EMI_NF AS DT_ENT
		, EST_NF_SAI.DT_EMI_NF
		,'SAÍDA' AS TIPO_NF  
		, EST_NF_SAI.VLR_NF
		, ISNULL(NFE_ACEITE_DEB_NF_CRED.NR_AUTORIZADOR,'') AS NR_AUTORIZADOR
		, ISNULL(NFE_ACEITE_DEB_NF_CRED.NR_PROTOCOLO,'') AS NR_PROTOCOLO
		, ISNULL(NFE_ACEITE_DEB_NF_CRED.STS_IMP,0) AS STS_IMP
		, ISNULL(NFE_ACEITE_DEB_NF_CRED.DT_RECBTO,'') AS DT_RECBTO
		, ACEITE_DEB_NF_CRED.DT_HR_EVENTO AS DT_ARQUIVO
		, ISNULL(NFE_NFE_PERDA_ROUBO_DEST.DS_MOTIVO, '') AS DS_MOTIVO
		, GLB_USU.NM_USU
		, GLB_MOV_OP.DS_MOV_OP
		, ACEITE_DEB_NF_CRED.CD_CTR_ACEITE_DEB_NOTA_CREDITO AS NFE_CTR
		, 0 AS CTR_ORIG_CANCEL
		, '' AS COD_EVENTO_ORIG_CANCEL
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ_ARQ_DANFE
		, GLB_MOV_OP.CD_MOV_OP
		, CASE WHEN ISNULL (CANCEL_VALIDADA.STS_IMP ,0) = 1 THEN 1 ELSE 0 END AS EVENTO_CANCELADO
		, CASE WHEN ACEITE_DEB_NF_CRED.IND_ACEITACAO = 1 THEN  'SIM'  ELSE 'NÃO' END AS IND_ACEITACAO
		
		--Itens geral : para este evento não contém itens 
		, 0  AS SEQ_ITEM 
		, 0  AS CD_PROD 
		, '' AS DS_PROD
		, '' AS UNID
		, 0  AS QTDE_NF
		, 0  AS QTDE
		, 0  AS VLR_IBS
		, 0  AS VLR_CBS
		
		--Itens Perecimento, perda, roubo,furto durante transporte. Para este evento não contém itens 

		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO  
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO  
		
       --Itens Fornecimento não realizado com pagamento antecipado
		,0 AS VLR_IBS_NAO_FORNECIDA  
		,0 AS VLR_CBS_NAO_FORNECIDA
		,'' AS DT_PREVISAO_ENTREGA 
	
	   -- Itens Apropriação de crédito presumido
		,0 AS C_CRED_PRES_IBS
		,0 AS P_CRED_PRES_IBS
		,0 AS V_CRED_PRES_IBS
		,0 AS C_CRED_PRES_CBS
		,0 AS P_CRED_PRES_CBS
		,0 AS V_CRED_PRES_CBS

		 -- Itens consumo pessoal Entrada
		,0 AS  VLR_IBS_CONS_PESSOAL_ENT
		,0 AS VLR_CBS_CONS_PESSOAL_ENT  
		,'' AS CH_ACESSO_DFE_REFERENCIA_ENT  
		,0 AS SEQ_ITEM_DFE_REFERENCIA_ENT  

		-- Itens consumo pessoal Saída
		,0 AS VLR_IBS_CONS_PESSOAL_SAI
		,0 AS VLR_CBS_CONS_PESSOAL_SAI
		,'' AS CH_ACESSO_DFE_REFERENCIA_SAI
		,0 AS SEQ_ITEM_DFE_REFERENCIA_SAI

        --Itens Perecimento, perda, roubo,furto
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO_DEST 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO_DEST

		--Itens imobilização de item 
		, 0 as VLR_IBS_IMOBILIZ_ITEM
		, 0 as VLR_CBS_IMOBILIZ_ITEM

		--Itens credito combustivel
		,0 AS VLR_IBS_CRED_COMBUST
		,0 AS VLR_CBS_CRED_COMBUST
				  
		-- Itens Apropriação de crédito bens serviços
		,0 AS VLR_CRED_IBS_BENS_SERV
		,0  AS VLR_CRED_CBS_BENS_SERV

			 
	FROM  DBO.EST_NF_SAI
		 INNER JOIN PRC_FILIAL FL ON DBO.EST_NF_SAI.CD_EMP = FL.CD_EMP
									 AND DBO.EST_NF_SAI.CD_FILIAL = FL.CD_FILIAL
  
	     INNER JOIN DBO.GLB_MOV_OP ON DBO.EST_NF_SAI.CD_NT_MOV = DBO.GLB_MOV_OP.CD_MOV_OP
		  
		 OUTER APPLY UFD_GLB_PESSOA(EST_NF_SAI.CD_CLI, GLB_MOV_OP.DEST_ORIG) V_GLB_PESSOA
 
		 INNER JOIN DBO.EST_NF_SERIE ON DBO.EST_NF_SAI.CD_EMP = DBO.EST_NF_SERIE.CD_EMP
										AND DBO.EST_NF_SAI.CD_NF_SERIE = DBO.EST_NF_SERIE.CD_NF_SERIE

		 INNER JOIN EST_NFE_EST_NFE_ACEITE_DEB_NOTA_CREDITO_EST_NF_SAI ACEITE_DEB_NF_CRED_SAI ON 
		  ACEITE_DEB_NF_CRED_SAI.CD_EMP = EST_NF_SAI.CD_EMP
		  AND ACEITE_DEB_NF_CRED_SAI.CD_FILIAL =  EST_NF_SAI.CD_FILIAL
		  AND ACEITE_DEB_NF_CRED_SAI.CD_NF = EST_NF_SAI.CD_NF

		  INNER JOIN EST_NFE_ACEITE_DEB_NOTA_CREDITO ACEITE_DEB_NF_CRED ON 
		  ACEITE_DEB_NF_CRED.CD_CTR_ACEITE_DEB_NOTA_CREDITO  = ACEITE_DEB_NF_CRED_SAI.CD_CTR_ACEITE_DEB_NOTA_CREDITO
        
		 INNER JOIN GLB_USU ON  
         ACEITE_DEB_NF_CRED.CD_USU = GLB_USU.CD_USU 
       
	     LEFT OUTER JOIN EST_NFE_ACEITE_DEB_NOTA_CREDITO_VALIDADA NFE_ACEITE_DEB_NF_CRED  ON  
         ACEITE_DEB_NF_CRED.CD_CTR_ACEITE_DEB_NOTA_CREDITO = NFE_ACEITE_DEB_NF_CRED.CD_CTR_ACEITE_DEB_NOTA_CREDITO
		 
		 LEFT JOIN EST_NFE_ACEITE_DEB_NOTA_CREDITO_ARQ_DANFE ARQ_DANFE ON 
		 ARQ_DANFE.CD_CTR_ACEITE_DEB_NOTA_CREDITO  = ACEITE_DEB_NF_CRED.CD_CTR_ACEITE_DEB_NOTA_CREDITO
		 
	    LEFT JOIN NFE..EST_NFE_ACEITEDEBNOTACREDITO NFE_NFE_PERDA_ROUBO_DEST   ON 
		NFE_NFE_PERDA_ROUBO_DEST.NRO = ACEITE_DEB_NF_CRED.CD_CTR_ACEITE_DEB_NOTA_CREDITO

		LEFT JOIN EST_NFE_CANCEL_EVENTO CANCEL_EVENTO ON  
		CANCEL_EVENTO.CD_CTR_EVENTO = ACEITE_DEB_NF_CRED.CD_CTR_ACEITE_DEB_NOTA_CREDITO 
		AND CANCEL_EVENTO.TIPO_EVENTO = '211128'
		LEFT JOIN  EST_NFE_CANCEL_EVENTO_VALIDADA CANCEL_VALIDADA ON 
		CANCEL_VALIDADA.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO

UNION ALL

SELECT '211130' AS COD_EVENTO
		, 10 AS CD_EVENTO
		, 'Imobilização de item' AS DS_EVENTO
		, 'Imobilização de item' AS DS_EVENTO_SIMP
		, EST_NF_SAI.CD_EMP
		, EST_NF_SAI.CD_FILIAL
		, EST_NF_SAI.CD_NF
		, EST_NF_SAI.NF_NF AS NR_NF
		, 1 AS NF_EMISSAO_PROPRIA
		, EST_NF_SAI.CD_NF_SERIE
		, ISNULL(EST_NF_SERIE.SERIE, '') AS SERIE
		, ISNULL(EST_NF_SERIE.MODELO, '') AS MODELO
		, EST_NF_SAI.CD_CLI AS CD_ORIG
		, V_GLB_PESSOA.RAZAO_SOCIAL_PESSOA AS NM_ORIG
		, EST_NF_SAI.DT_EMI_NF AS DT_ENT
		, EST_NF_SAI.DT_EMI_NF
		,'SAÍDA' AS TIPO_NF  
		, EST_NF_SAI.VLR_NF
		, ISNULL(NFE_IMOBILIZA_ITEM.NR_AUTORIZADOR,'') AS NR_AUTORIZADOR
		, ISNULL(NFE_IMOBILIZA_ITEM.NR_PROTOCOLO,'') AS NR_PROTOCOLO
		, ISNULL(NFE_IMOBILIZA_ITEM.STS_IMP,0) AS STS_IMP
		, ISNULL(NFE_IMOBILIZA_ITEM.DT_RECBTO,'') AS DT_RECBTO
		, IMOBILIZA_ITEM.DT_HR_EVENTO AS DT_ARQUIVO
		, ISNULL(NFE_NFE_IMOBILIZA_ITEM.DS_MOTIVO, '') AS DS_MOTIVO
		, GLB_USU.NM_USU
		, GLB_MOV_OP.DS_MOV_OP
		, IMOBILIZA_ITEM.CD_CTR_IMOBILIZA_ITEM AS NFE_CTR
		, 0 AS CTR_ORIG_CANCEL
		, '' AS COD_EVENTO_ORIG_CANCEL
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ_ARQ_DANFE
		, GLB_MOV_OP.CD_MOV_OP
		, CASE WHEN ISNULL (CANCEL_VALIDADA.STS_IMP ,0) = 1 THEN 1 ELSE 0 END AS EVENTO_CANCELADO
		,'NÃO SE APLICA' AS IND_ACEITACAO
		
		--Itens geral 
        , IMOBILIZA_ITEM_IT.SEQ_ITEM 
		, IMOBILIZA_ITEM_IT.CD_PROD
		, EST_PROD.DS_PROD
		, IMOBILIZA_ITEM_IT.UNID_IMOB AS UNID
		, IMOBILIZA_ITEM_IT.QTD_IMOB AS QTDE
		, IT.QT_IT AS  QTDE_NF
		, IT_IBSCBS.VLR_IBS  
		, IT_IBSCBS.VLR_CBS

		--Itens Perecimento, perda, roubo,furto durante transporte
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO 

		--Itens transporte Fornecimento não realizado com pagamento antecipado
        ,0 AS VLR_IBS_NAO_FORNECIDA  
        ,0 AS VLR_CBS_NAO_FORNECIDA
		,'' AS DT_PREVISAO_ENTREGA 

	    -- Itens Apropriação de crédito presumido
		,0 AS C_CRED_PRES_IBS
		,0 AS P_CRED_PRES_IBS
		,0 AS V_CRED_PRES_IBS
		,0 AS C_CRED_PRES_CBS
		,0 AS P_CRED_PRES_CBS
		,0 AS V_CRED_PRES_CBS

		-- Itens consumo pessoal Entrada
		,0 AS VLR_IBS_CONS_PESSOAL_ENT
		,0 AS VLR_CBS_CONS_PESSOAL_ENT
		,'' AS CH_ACESSO_DFE_REFERENCIA_ENT
		,0 AS SEQ_ITEM_DFE_REFERENCIA_ENT  

		-- Itens consumo pessoal Saída
		,0 AS  VLR_IBS_CONS_PESSOAL_SAI
		,0 AS VLR_CBS_CONS_PESSOAL_SAI  
		,'' AS CH_ACESSO_DFE_REFERENCIA_SAI  
		,0 AS SEQ_ITEM_DFE_REFERENCIA_SAI

		--Itens Perecimento, perda, roubo,furto
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO_DEST 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO_DEST 

		--Itens imobilização de item 
		, IMOBILIZA_ITEM_IT.VLR_IBS as VLR_IBS_IMOBILIZ_ITEM
		, IMOBILIZA_ITEM_IT.VLR_CBS as VLR_CBS_IMOBILIZ_ITEM

		--Itens credito combustivel
		,0 AS VLR_IBS_CRED_COMBUST
		,0 AS VLR_CBS_CRED_COMBUST
				  
		-- Itens Apropriação de crédito bens serviços
		,0 AS VLR_CRED_IBS_BENS_SERV
		,0  AS VLR_CRED_CBS_BENS_SERV
	 
	FROM  DBO.EST_NF_SAI
		 INNER JOIN PRC_FILIAL FL ON DBO.EST_NF_SAI.CD_EMP = FL.CD_EMP
									 AND DBO.EST_NF_SAI.CD_FILIAL = FL.CD_FILIAL
  
	     INNER JOIN DBO.GLB_MOV_OP ON DBO.EST_NF_SAI.CD_NT_MOV = DBO.GLB_MOV_OP.CD_MOV_OP
		  
		 OUTER APPLY UFD_GLB_PESSOA(EST_NF_SAI.CD_CLI, GLB_MOV_OP.DEST_ORIG) V_GLB_PESSOA
 
		 INNER JOIN DBO.EST_NF_SERIE ON DBO.EST_NF_SAI.CD_EMP = DBO.EST_NF_SERIE.CD_EMP
										AND DBO.EST_NF_SAI.CD_NF_SERIE = DBO.EST_NF_SERIE.CD_NF_SERIE

		 INNER JOIN EST_NFE_IMOBILIZA_ITEM_EST_NF_SAI IMOBILIZA_ITEM_SAI ON 
		  IMOBILIZA_ITEM_SAI.CD_EMP = EST_NF_SAI.CD_EMP
		  AND IMOBILIZA_ITEM_SAI.CD_FILIAL =  EST_NF_SAI.CD_FILIAL
		  AND IMOBILIZA_ITEM_SAI.CD_NF = EST_NF_SAI.CD_NF

		  INNER JOIN EST_NFE_IMOBILIZA_ITEM IMOBILIZA_ITEM ON 
		  IMOBILIZA_ITEM.CD_CTR_IMOBILIZA_ITEM  = IMOBILIZA_ITEM_SAI.CD_CTR_IMOBILIZA_ITEM

		  INNER JOIN EST_NFE_IMOBILIZA_ITEM_IT IMOBILIZA_ITEM_IT ON 
		  IMOBILIZA_ITEM.CD_CTR_IMOBILIZA_ITEM = IMOBILIZA_ITEM_IT.CD_CTR_IMOBILIZA_ITEM

		  LEFT JOIN  EST_NF_SAI_IT_IBS_CBS IT_IBSCBS ON
		         IT_IBSCBS.CD_EMP = IMOBILIZA_ITEM_SAI.CD_EMP
		  AND IT_IBSCBS.CD_FILIAL = IMOBILIZA_ITEM_SAI.CD_FILIAL
		  AND IT_IBSCBS.CD_NF = IMOBILIZA_ITEM_SAI.CD_NF
		  AND IT_IBSCBS.CD_IT = IMOBILIZA_ITEM_IT.SEQ_ITEM 
		  AND IT_IBSCBS.CD_PROD = IMOBILIZA_ITEM_IT.CD_PROD
		  
		  LEFT JOIN EST_NF_SAI_IT IT  ON
		         IT_IBSCBS.CD_EMP = IT.CD_EMP
		  AND IT_IBSCBS.CD_FILIAL = IT.CD_FILIAL
		  AND IT_IBSCBS.CD_NF = IT.CD_NF
		  AND IT_IBSCBS.CD_IT = IT.CD_IT
		  AND IT_IBSCBS.CD_PROD = IT.CD_PROD
       
		  INNER JOIN EST_PROD ON EST_PROD.CD_PROD =  IMOBILIZA_ITEM_IT.CD_PROD

		 INNER JOIN GLB_USU ON  
         IMOBILIZA_ITEM.CD_USU = GLB_USU.CD_USU 
       
	     LEFT OUTER JOIN EST_NFE_IMOBILIZA_ITEM_VALIDADA NFE_IMOBILIZA_ITEM  ON  
         IMOBILIZA_ITEM.CD_CTR_IMOBILIZA_ITEM = NFE_IMOBILIZA_ITEM.CD_CTR_IMOBILIZA_ITEM
		 
		 LEFT JOIN EST_NFE_IMOBILIZA_ITEM_ARQ_DANFE ARQ_DANFE ON 
		 ARQ_DANFE.CD_CTR_IMOBILIZA_ITEM  = IMOBILIZA_ITEM.CD_CTR_IMOBILIZA_ITEM
		 
	    LEFT JOIN NFE..EST_NFE_IMOBILIZAITEM NFE_NFE_IMOBILIZA_ITEM   ON 
		NFE_NFE_IMOBILIZA_ITEM.NRO = IMOBILIZA_ITEM.CD_CTR_IMOBILIZA_ITEM
		
		LEFT JOIN EST_NFE_CANCEL_EVENTO CANCEL_EVENTO ON  
		CANCEL_EVENTO.CD_CTR_EVENTO = IMOBILIZA_ITEM.CD_CTR_IMOBILIZA_ITEM 
		AND CANCEL_EVENTO.TIPO_EVENTO = '211130'
		LEFT JOIN  EST_NFE_CANCEL_EVENTO_VALIDADA CANCEL_VALIDADA ON 
		CANCEL_VALIDADA.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO


UNION ALL

SELECT '211140' AS COD_EVENTO
		, 11 AS CD_EVENTO
		, 'Solicitação de Apropriação de crédito de combustivel' AS DS_EVENTO
		, 'Apropriação de crédito de combustivel' AS DS_EVENTO_SIMP
		, EST_NF_SAI.CD_EMP
		, EST_NF_SAI.CD_FILIAL
		, EST_NF_SAI.CD_NF
		, EST_NF_SAI.NF_NF AS NR_NF
		, 1 AS NF_EMISSAO_PROPRIA
		, EST_NF_SAI.CD_NF_SERIE
		, ISNULL(EST_NF_SERIE.SERIE, '') AS SERIE
		, ISNULL(EST_NF_SERIE.MODELO, '') AS MODELO
		, EST_NF_SAI.CD_CLI AS CD_ORIG
		, V_GLB_PESSOA.RAZAO_SOCIAL_PESSOA AS NM_ORIG
		, EST_NF_SAI.DT_EMI_NF AS  DT_ENT 
		, EST_NF_SAI.DT_EMI_NF
		,'SAÍDA' AS TIPO_NF  
		, EST_NF_SAI.VLR_NF
		, ISNULL(NFE_APROP_CRED_COMBUST.NR_AUTORIZADOR,'') AS NR_AUTORIZADOR
		, ISNULL(NFE_APROP_CRED_COMBUST.NR_PROTOCOLO,'') AS NR_PROTOCOLO
		, ISNULL(NFE_APROP_CRED_COMBUST.STS_IMP,0) AS STS_IMP
		, ISNULL(NFE_APROP_CRED_COMBUST.DT_RECBTO,'') AS DT_RECBTO
		, APROP_CRED_COMBUST.DT_HR_EVENTO AS DT_ARQUIVO
		, ISNULL(NFE_NFE_APROP_CRED_COMBUST.DS_MOTIVO, '') AS DS_MOTIVO
		, GLB_USU.NM_USU
		, GLB_MOV_OP.DS_MOV_OP
		, NFE_APROP_CRED_COMBUST.CD_CTR_SOLIC_APROP_CRED_COMB  AS NFE_CTR
		, 0 AS CTR_ORIG_CANCEL
		, '' AS COD_EVENTO_ORIG_CANCEL
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ_ARQ_DANFE
		, GLB_MOV_OP.CD_MOV_OP
		, CASE WHEN ISNULL (CANCEL_VALIDADA.STS_IMP ,0) = 1 THEN 1 ELSE 0 END AS EVENTO_CANCELADO
		,'NÃO SE APLICA' AS IND_ACEITACAO
		
		--Itens geral 
        , APROP_CRED_COMBUST_IT.SEQ_ITEM 
		, APROP_CRED_COMBUST_IT.CD_PROD
		, EST_PROD.DS_PROD
		, APROP_CRED_COMBUST_IT.UNID_COMB AS UNID
		, QTD_COMB AS QTDE
		, IT.QT_IT AS  QTDE_NF
		, IT_IBSCBS.VLR_IBS  
		, IT_IBSCBS.VLR_CBS
		
		--Itens Perecimento, perda, roubo,furto durante transporte. Para este evento não contém itens 

		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO  
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO  
		
      --Itens Fornecimento não realizado com pagamento antecipado
		,0 AS VLR_IBS_NAO_FORNECIDA  
		,0 AS VLR_CBS_NAO_FORNECIDA
		,'' AS  DT_PREVISAO_ENTREGA
		
		-- Itens Apropriação de crédito presumido
		,0 AS C_CRED_PRES_IBS
		,0 AS P_CRED_PRES_IBS
		,0 AS V_CRED_PRES_IBS
		,0 AS C_CRED_PRES_CBS
		,0 AS P_CRED_PRES_CBS
		,0 AS V_CRED_PRES_CBS

		 -- Itens consumo pessoal Entrada
		,0 AS  VLR_IBS_CONS_PESSOAL_ENT
		,0 AS VLR_CBS_CONS_PESSOAL_ENT  
		,'' AS CH_ACESSO_DFE_REFERENCIA_ENT  
		,0 AS SEQ_ITEM_DFE_REFERENCIA_ENT  

		-- Itens consumo pessoal Saída
		,0 AS VLR_IBS_CONS_PESSOAL_SAI
		,0 AS VLR_CBS_CONS_PESSOAL_SAI
		,'' AS CH_ACESSO_DFE_REFERENCIA_SAI
		,0 AS SEQ_ITEM_DFE_REFERENCIA_SAI

	  
        --Itens Perecimento, perda, roubo,furto
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO_DEST 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO_DEST

		--Itens imobilização de item 
		, 0 as VLR_IBS_IMOBILIZ_ITEM
		, 0 as VLR_CBS_IMOBILIZ_ITEM 

		--Itens credito combustivel
		,APROP_CRED_COMBUST_IT.VLR_IBS AS VLR_IBS_CRED_COMBUST
		,APROP_CRED_COMBUST_IT.VLR_CBS AS VLR_CBS_CRED_COMBUST
				  
		-- Itens Apropriação de crédito bens serviços
		,0 AS VLR_CRED_IBS_BENS_SERV
		,0  AS VLR_CRED_CBS_BENS_SERV
 
  FROM  DBO.EST_NF_SAI
		 INNER JOIN PRC_FILIAL FL ON DBO.EST_NF_SAI.CD_EMP = FL.CD_EMP
									 AND DBO.EST_NF_SAI.CD_FILIAL = FL.CD_FILIAL
  
	     INNER JOIN DBO.GLB_MOV_OP ON DBO.EST_NF_SAI.CD_NT_MOV = DBO.GLB_MOV_OP.CD_MOV_OP
		  
		 OUTER APPLY UFD_GLB_PESSOA(EST_NF_SAI.CD_CLI, GLB_MOV_OP.DEST_ORIG) V_GLB_PESSOA
 
		 INNER JOIN DBO.EST_NF_SERIE ON DBO.EST_NF_SAI.CD_EMP = DBO.EST_NF_SERIE.CD_EMP
										AND DBO.EST_NF_SAI.CD_NF_SERIE = DBO.EST_NF_SERIE.CD_NF_SERIE

		 INNER JOIN EST_NFE_SOLIC_APROP_CRED_COMB_EST_NF_SAI AS APROP_CRED_COMBUST_SAI ON 
		  APROP_CRED_COMBUST_SAI.CD_EMP = EST_NF_SAI.CD_EMP
		  AND APROP_CRED_COMBUST_SAI.CD_FILIAL =  EST_NF_SAI.CD_FILIAL
		  AND APROP_CRED_COMBUST_SAI.CD_NF = EST_NF_SAI.CD_NF

		  INNER JOIN EST_NFE_SOLIC_APROP_CRED_COMB APROP_CRED_COMBUST ON 
		  APROP_CRED_COMBUST.CD_CTR_SOLIC_APROP_CRED_COMB    = APROP_CRED_COMBUST_SAI.CD_CTR_SOLIC_APROP_CRED_COMB  
		 
		 INNER JOIN EST_NFE_SOLIC_APROP_CRED_COMB_IT APROP_CRED_COMBUST_IT ON 
		 APROP_CRED_COMBUST.CD_CTR_SOLIC_APROP_CRED_COMB = APROP_CRED_COMBUST_IT.CD_CTR_SOLIC_APROP_CRED_COMB

		  LEFT JOIN  EST_NF_SAI_IT_IBS_CBS IT_IBSCBS ON
	      IT_IBSCBS.CD_EMP = APROP_CRED_COMBUST_SAI.CD_EMP
		  AND IT_IBSCBS.CD_FILIAL = APROP_CRED_COMBUST_SAI.CD_FILIAL
		  AND IT_IBSCBS.CD_NF = APROP_CRED_COMBUST_SAI.CD_NF
		  AND IT_IBSCBS.CD_IT = APROP_CRED_COMBUST_IT.SEQ_ITEM 
		  AND IT_IBSCBS.CD_PROD = APROP_CRED_COMBUST_IT.CD_PROD
		  
		  LEFT JOIN EST_NF_SAI_IT IT  ON
		         IT_IBSCBS.CD_EMP = IT.CD_EMP
		  AND IT_IBSCBS.CD_FILIAL = IT.CD_FILIAL
		  AND IT_IBSCBS.CD_NF = IT.CD_NF
		  AND IT_IBSCBS.CD_IT = IT.CD_IT
		  AND IT_IBSCBS.CD_PROD = IT.CD_PROD
       
		  INNER JOIN EST_PROD ON EST_PROD.CD_PROD =  APROP_CRED_COMBUST_IT.CD_PROD

		 INNER JOIN GLB_USU ON  
         APROP_CRED_COMBUST.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_SAI.CD_EMP
													AND EST_NF_ENT_EF_NF_CTR_NF.CD_FILIAL = EST_NF_SAI.CD_FILIAL
													AND EST_NF_ENT_EF_NF_CTR_NF.CD_NF_ENT = EST_NF_SAI.CD_NF
	   
	     LEFT OUTER JOIN EST_NFE_SOLIC_APROP_CRED_COMB_VALIDADA NFE_APROP_CRED_COMBUST   ON  
         APROP_CRED_COMBUST.CD_CTR_SOLIC_APROP_CRED_COMB  = NFE_APROP_CRED_COMBUST.CD_CTR_SOLIC_APROP_CRED_COMB 
		 

		 LEFT JOIN EST_NFE_SOLIC_APROP_CRED_COMB_ARQ_DANFE  ARQ_DANFE ON 
		 ARQ_DANFE.CD_CTR_SOLIC_APROP_CRED_COMB  = APROP_CRED_COMBUST.CD_CTR_SOLIC_APROP_CRED_COMB 
		 
	    LEFT JOIN NFE..EST_NFE_SOLICAPROPCREDCOMB NFE_NFE_APROP_CRED_COMBUST  ON 
		NFE_NFE_APROP_CRED_COMBUST.NRO =  APROP_CRED_COMBUST.CD_CTR_SOLIC_APROP_CRED_COMB

        LEFT JOIN EST_NFE_CANCEL_EVENTO CANCEL_EVENTO ON  
		   CANCEL_EVENTO.CD_CTR_EVENTO = APROP_CRED_COMBUST.CD_CTR_SOLIC_APROP_CRED_COMB
		   AND CANCEL_EVENTO.TIPO_EVENTO = '211140'
		LEFT JOIN  EST_NFE_CANCEL_EVENTO_VALIDADA CANCEL_VALIDADA ON 
		   CANCEL_VALIDADA.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO
UNION ALL

  SELECT '211150' AS COD_EVENTO
		, 12 AS CD_EVENTO
		, 'Solicitação de Apropriação de crédito para bens e serviços que dependem de atividade do adquirente' AS DS_EVENTO
		, 'Apropriação de crédito de bens e serviços de atividade do adquirente' AS DS_EVENTO_SIMP
		, EST_NF_SAI.CD_EMP
		, EST_NF_SAI.CD_FILIAL
		, EST_NF_SAI.CD_NF
		, EST_NF_SAI.NF_NF AS NR_NF
		, 1 AS NF_EMISSAO_PROPRIA
		, EST_NF_SAI.CD_NF_SERIE
		, ISNULL(EST_NF_SERIE.SERIE, '') AS SERIE
		, ISNULL(EST_NF_SERIE.MODELO, '') AS MODELO
		, EST_NF_SAI.CD_CLI AS CD_ORIG
		, V_GLB_PESSOA.RAZAO_SOCIAL_PESSOA AS NM_ORIG
		, EST_NF_SAI.DT_EMI_NF AS DT_ENT
		, EST_NF_SAI.DT_EMI_NF
		,'SAÍDA' AS TIPO_NF  
		, EST_NF_SAI.VLR_NF
		, ISNULL(NFE_APROP_BENS_SERVS.NR_AUTORIZADOR,'') AS NR_AUTORIZADOR
		, ISNULL(NFE_APROP_BENS_SERVS.NR_PROTOCOLO,'') AS NR_PROTOCOLO
		, ISNULL(NFE_APROP_BENS_SERVS.STS_IMP,0) AS STS_IMP
		, ISNULL(NFE_APROP_BENS_SERVS.DT_RECBTO,'') AS DT_RECBTO
		, APROP_BENS_SERVS.DT_HR_EVENTO AS DT_ARQUIVO
		, ISNULL(NFE_NFE_SOLICAPROPCREDBENSSERVS.DS_MOTIVO, '') AS DS_MOTIVO
		, GLB_USU.NM_USU
		, GLB_MOV_OP.DS_MOV_OP
		, APROP_BENS_SERVS.CD_CTR_SOLIC_APROP_CRED_BENS_SERVS AS NFE_CTR
		, 0 AS CTR_ORIG_CANCEL
		, '' AS COD_EVENTO_ORIG_CANCEL
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ_ARQ_DANFE
		, GLB_MOV_OP.CD_MOV_OP
		, CASE WHEN ISNULL (CANCEL_VALIDADA.STS_IMP ,0) = 1 THEN 1 ELSE 0 END AS EVENTO_CANCELADO
		,'NÃO SE APLICA' AS IND_ACEITACAO
		
		--Itens geral 
        , APROP_BENS_SERVS_IT.SEQ_ITEM 
		, APROP_BENS_SERVS_IT.CD_PROD
		, EST_PROD.DS_PROD
		, '' AS UNID
		, 0 AS QTDE
		, IT.QT_IT AS  QTDE_NF
		, IT_IBSCBS.VLR_IBS  
		, IT_IBSCBS.VLR_CBS

		--Itens Perecimento, perda, roubo,furto durante transporte
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO 

		--Itens transporte Fornecimento não realizado com pagamento antecipado
        ,0 AS VLR_IBS_NAO_FORNECIDA  
        ,0 AS VLR_CBS_NAO_FORNECIDA
		,'' AS DT_PREVISAO_ENTREGA 

	    -- Itens Apropriação de crédito presumido
		,0 AS C_CRED_PRES_IBS
		,0 AS P_CRED_PRES_IBS
		,0 AS V_CRED_PRES_IBS
		,0 AS C_CRED_PRES_CBS
		,0 AS P_CRED_PRES_CBS
		,0 AS V_CRED_PRES_CBS

		-- Itens consumo pessoal Entrada
		,0 AS VLR_IBS_CONS_PESSOAL_ENT
		,0 AS VLR_CBS_CONS_PESSOAL_ENT
		,'' AS CH_ACESSO_DFE_REFERENCIA_ENT
		,0 AS SEQ_ITEM_DFE_REFERENCIA_ENT  

		-- Itens consumo pessoal Saída
		,0 AS  VLR_IBS_CONS_PESSOAL_SAI
		,0 AS VLR_CBS_CONS_PESSOAL_SAI  
		,'' AS CH_ACESSO_DFE_REFERENCIA_SAI  
		,0 AS SEQ_ITEM_DFE_REFERENCIA_SAI

		--Itens Perecimento, perda, roubo,furto
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO_DEST 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO_DEST 

		--Itens imobilização de item 
		, 0 AS VLR_IBS_IMOBILIZ_ITEM
		, 0 AS VLR_CBS_IMOBILIZ_ITEM

		--Itens credito combustivel
		,0 AS VLR_IBS_CRED_COMBUST
		,0 AS VLR_CBS_CRED_COMBUST
  
		-- Itens Apropriação de crédito bens serviços
		,APROP_BENS_SERVS_IT.VLR_CRED_IBS AS VLR_CRED_IBS_BENS_SERV
		,APROP_BENS_SERVS_IT.VLR_CRED_CBS AS VLR_CRED_CBS_BENS_SERV
	 
 	FROM  DBO.EST_NF_SAI
		 INNER JOIN PRC_FILIAL FL ON DBO.EST_NF_SAI.CD_EMP = FL.CD_EMP
									 AND DBO.EST_NF_SAI.CD_FILIAL = FL.CD_FILIAL
  
	     INNER JOIN DBO.GLB_MOV_OP ON DBO.EST_NF_SAI.CD_NT_MOV = DBO.GLB_MOV_OP.CD_MOV_OP
		  
		 OUTER APPLY UFD_GLB_PESSOA(EST_NF_SAI.CD_CLI, GLB_MOV_OP.DEST_ORIG) V_GLB_PESSOA
 
		 INNER JOIN DBO.EST_NF_SERIE ON DBO.EST_NF_SAI.CD_EMP = DBO.EST_NF_SERIE.CD_EMP
										AND DBO.EST_NF_SAI.CD_NF_SERIE = DBO.EST_NF_SERIE.CD_NF_SERIE

		 INNER JOIN EST_NFE_SOLIC_APROP_CRED_BENS_SERVS_EST_NF_SAI AS APROP_BENS_SERVS_SAI ON 
		  APROP_BENS_SERVS_SAI.CD_EMP = EST_NF_SAI.CD_EMP
		  AND APROP_BENS_SERVS_SAI.CD_FILIAL =  EST_NF_SAI.CD_FILIAL
		  AND APROP_BENS_SERVS_SAI.CD_NF = EST_NF_SAI.CD_NF

		  INNER JOIN EST_NFE_SOLIC_APROP_CRED_BENS_SERVS APROP_BENS_SERVS ON 
		  APROP_BENS_SERVS.CD_CTR_SOLIC_APROP_CRED_BENS_SERVS = APROP_BENS_SERVS_SAI.CD_CTR_SOLIC_APROP_CRED_BENS_SERVS

		  INNER JOIN EST_NFE_SOLIC_APROP_CRED_BENS_SERVS_IT AS APROP_BENS_SERVS_IT ON 
		  APROP_BENS_SERVS.CD_CTR_SOLIC_APROP_CRED_BENS_SERVS  = APROP_BENS_SERVS_IT.CD_CTR_SOLIC_APROP_CRED_BENS_SERVS

		  LEFT JOIN  EST_NF_SAI_IT_IBS_CBS IT_IBSCBS ON
		         IT_IBSCBS.CD_EMP = APROP_BENS_SERVS_SAI.CD_EMP
		  AND IT_IBSCBS.CD_FILIAL = APROP_BENS_SERVS_SAI.CD_FILIAL
		  AND IT_IBSCBS.CD_NF = APROP_BENS_SERVS_SAI.CD_NF
		  AND IT_IBSCBS.CD_IT = APROP_BENS_SERVS_IT.SEQ_ITEM 
		  AND IT_IBSCBS.CD_PROD = APROP_BENS_SERVS_IT.CD_PROD
		  
		  LEFT JOIN EST_NF_SAI_IT IT  ON
		         IT_IBSCBS.CD_EMP = IT.CD_EMP
		  AND IT_IBSCBS.CD_FILIAL = IT.CD_FILIAL
		  AND IT_IBSCBS.CD_NF = IT.CD_NF
		  AND IT_IBSCBS.CD_IT = IT.CD_IT
		  AND IT_IBSCBS.CD_PROD = IT.CD_PROD
       
		  INNER JOIN EST_PROD ON EST_PROD.CD_PROD =  APROP_BENS_SERVS_IT.CD_PROD

		 INNER JOIN GLB_USU ON  
         APROP_BENS_SERVS.CD_USU = GLB_USU.CD_USU 
       
	     LEFT OUTER JOIN EST_NFE_SOLIC_APROP_CRED_BENS_SERVS_VALIDADA NFE_APROP_BENS_SERVS   ON  
         APROP_BENS_SERVS.CD_CTR_SOLIC_APROP_CRED_BENS_SERVS = NFE_APROP_BENS_SERVS.CD_CTR_SOLIC_APROP_CRED_BENS_SERVS
		 
		 LEFT JOIN EST_NFE_SOLIC_APROP_CRED_BENS_SERVS_ARQ_DANFE ARQ_DANFE ON 
		 ARQ_DANFE.CD_CTR_SOLIC_APROP_CRED_BENS_SERVS  = APROP_BENS_SERVS.CD_CTR_SOLIC_APROP_CRED_BENS_SERVS
		 
	    LEFT JOIN NFE..EST_NFE_SOLICAPROPCREDBENSSERVS NFE_NFE_SOLICAPROPCREDBENSSERVS   ON 
		NFE_NFE_SOLICAPROPCREDBENSSERVS.NRO = APROP_BENS_SERVS.CD_CTR_SOLIC_APROP_CRED_BENS_SERVS
		
		LEFT JOIN EST_NFE_CANCEL_EVENTO CANCEL_EVENTO ON  
		CANCEL_EVENTO.CD_CTR_EVENTO = APROP_BENS_SERVS.CD_CTR_SOLIC_APROP_CRED_BENS_SERVS 
		AND CANCEL_EVENTO.TIPO_EVENTO = '211130'
		LEFT JOIN  EST_NFE_CANCEL_EVENTO_VALIDADA CANCEL_VALIDADA ON 
		CANCEL_VALIDADA.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO

	UNION ALL 

	SELECT '212110' AS COD_EVENTO
		, 13 AS CD_EVENTO
		, 'Manifestação sobre pedido de transferência de crédito de IBS em operações de sucessão' AS DS_EVENTO
		, 'Manifestação sobre pedido de transferência de crédito de IBS em operações de sucessão' AS DS_EVENTO_SIMP
		, EST_NF_SAI.CD_EMP
		, EST_NF_SAI.CD_FILIAL
		, EST_NF_SAI.CD_NF
		, EST_NF_SAI.NF_NF AS NR_NF
		, 1 AS NF_EMISSAO_PROPRIA
		, EST_NF_SAI.CD_NF_SERIE
		, ISNULL(EST_NF_SERIE.SERIE, '') AS SERIE
		, ISNULL(EST_NF_SERIE.MODELO, '') AS MODELO
		, EST_NF_SAI.CD_CLI AS CD_ORIG
		, V_GLB_PESSOA.RAZAO_SOCIAL_PESSOA AS NM_ORIG
		, EST_NF_SAI.DT_EMI_NF AS DT_ENT
		, EST_NF_SAI.DT_EMI_NF
		,'SAÍDA' AS TIPO_NF  
		, EST_NF_SAI.VLR_NF
		, ISNULL(NFE_PED_TRANSF_CRED_IBS.NR_AUTORIZADOR,'') AS NR_AUTORIZADOR
		, ISNULL(NFE_PED_TRANSF_CRED_IBS.NR_PROTOCOLO,'') AS NR_PROTOCOLO
		, ISNULL(NFE_PED_TRANSF_CRED_IBS.STS_IMP,0) AS STS_IMP
		, ISNULL(NFE_PED_TRANSF_CRED_IBS.DT_RECBTO,'') AS DT_RECBTO
		, PED_TRANSF_CRED_IBS.DT_HR_EVENTO AS DT_ARQUIVO
		, ISNULL(NFE_NFE_MANPEDTRANSFCREDIBS.DS_MOTIVO, '') AS DS_MOTIVO
		, GLB_USU.NM_USU
		, GLB_MOV_OP.DS_MOV_OP
		, PED_TRANSF_CRED_IBS.CD_CTR_MAN_PED_TRANSF_CRED_IBS AS NFE_CTR
		, 0 AS CTR_ORIG_CANCEL
		, '' AS COD_EVENTO_ORIG_CANCEL
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ_ARQ_DANFE
		, GLB_MOV_OP.CD_MOV_OP
		, CASE WHEN ISNULL (CANCEL_VALIDADA.STS_IMP ,0) = 1 THEN 1 ELSE 0 END AS EVENTO_CANCELADO
		, CASE WHEN PED_TRANSF_CRED_IBS.IND_ACEITACAO = 1 THEN  'SIM'  ELSE 'NÃO' END AS IND_ACEITACAO
		
        --Itens geral : para este evento não contém itens 
		, 0  AS SEQ_ITEM 
		, 0  AS CD_PROD 
		, '' AS DS_PROD
		, '' AS UNID
		, 0  AS QTDE_NF
		, 0  AS QTDE
		, 0  AS VLR_IBS
		, 0  AS VLR_CBS
		
		--Itens Perecimento, perda, roubo,furto durante transporte. Para este evento não contém itens 

		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO  
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO  
		
      --Itens Fornecimento não realizado com pagamento antecipado
		,0 AS VLR_IBS_NAO_FORNECIDA  
		,0 AS VLR_CBS_NAO_FORNECIDA
		,'' AS DT_PREVISAO_ENTREGA 
	
	-- Itens Apropriação de crédito presumido
		,0 AS C_CRED_PRES_IBS
		,0 AS P_CRED_PRES_IBS
		,0 AS V_CRED_PRES_IBS
		,0 AS C_CRED_PRES_CBS
		,0 AS P_CRED_PRES_CBS
		,0 AS V_CRED_PRES_CBS

		 -- Itens consumo pessoal Entrada
		,0 AS  VLR_IBS_CONS_PESSOAL_ENT
		,0 AS VLR_CBS_CONS_PESSOAL_ENT  
		,'' AS CH_ACESSO_DFE_REFERENCIA_ENT  
		,0 AS SEQ_ITEM_DFE_REFERENCIA_ENT  

		-- Itens consumo pessoal Saída
		,0 AS VLR_IBS_CONS_PESSOAL_SAI
		,0 AS VLR_CBS_CONS_PESSOAL_SAI
		,'' AS CH_ACESSO_DFE_REFERENCIA_SAI
		,0 AS SEQ_ITEM_DFE_REFERENCIA_SAI

        --Itens Perecimento, perda, roubo,furto
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO_DEST 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO_DEST

		--Itens imobilização de item 
		, 0 as VLR_IBS_IMOBILIZ_ITEM
		, 0 as VLR_CBS_IMOBILIZ_ITEM

		--Itens credito combustivel
		,0 AS VLR_IBS_CRED_COMBUST
		,0 AS VLR_CBS_CRED_COMBUST
				  
		-- Itens Apropriação de crédito bens serviços
		,0 AS VLR_CRED_IBS_BENS_SERV
		,0  AS VLR_CRED_CBS_BENS_SERV
	 
   FROM  DBO.EST_NF_SAI
		 INNER JOIN PRC_FILIAL FL ON DBO.EST_NF_SAI.CD_EMP = FL.CD_EMP
									 AND DBO.EST_NF_SAI.CD_FILIAL = FL.CD_FILIAL
  
	     INNER JOIN DBO.GLB_MOV_OP ON DBO.EST_NF_SAI.CD_NT_MOV = DBO.GLB_MOV_OP.CD_MOV_OP
		  
		 OUTER APPLY UFD_GLB_PESSOA(EST_NF_SAI.CD_CLI, GLB_MOV_OP.DEST_ORIG) V_GLB_PESSOA
 
		 INNER JOIN DBO.EST_NF_SERIE ON DBO.EST_NF_SAI.CD_EMP = DBO.EST_NF_SERIE.CD_EMP
										AND DBO.EST_NF_SAI.CD_NF_SERIE = DBO.EST_NF_SERIE.CD_NF_SERIE

		 INNER JOIN EST_NFE_MAN_PED_TRANSF_CRED_IBS_EST_NF_SAI AS PED_TRANSF_CRED_IBS_SAI ON 
		  PED_TRANSF_CRED_IBS_SAI.CD_EMP = EST_NF_SAI.CD_EMP
		  AND PED_TRANSF_CRED_IBS_SAI.CD_FILIAL =  EST_NF_SAI.CD_FILIAL
		  AND PED_TRANSF_CRED_IBS_SAI.CD_NF = EST_NF_SAI.CD_NF

		  INNER JOIN EST_NFE_MAN_PED_TRANSF_CRED_IBS AS PED_TRANSF_CRED_IBS  ON 
		  PED_TRANSF_CRED_IBS.CD_CTR_MAN_PED_TRANSF_CRED_IBS = PED_TRANSF_CRED_IBS_SAI.CD_CTR_MAN_PED_TRANSF_CRED_IBS
  
		 INNER JOIN GLB_USU ON  
         PED_TRANSF_CRED_IBS.CD_USU = GLB_USU.CD_USU 
       
	     LEFT OUTER JOIN EST_NFE_MAN_PED_TRANSF_CRED_IBS_VALIDADA NFE_PED_TRANSF_CRED_IBS   ON  
         PED_TRANSF_CRED_IBS.CD_CTR_MAN_PED_TRANSF_CRED_IBS = NFE_PED_TRANSF_CRED_IBS.CD_CTR_MAN_PED_TRANSF_CRED_IBS
		 
		 LEFT JOIN EST_NFE_MAN_PED_TRANSF_CRED_IBS_ARQ_DANFE ARQ_DANFE ON 
		 ARQ_DANFE.CD_CTR_MAN_PED_TRANSF_CRED_IBS  = PED_TRANSF_CRED_IBS.CD_CTR_MAN_PED_TRANSF_CRED_IBS
		 
	    LEFT JOIN NFE..EST_NFE_MANPEDTRANSFCREDIBS NFE_NFE_MANPEDTRANSFCREDIBS   ON 
		NFE_NFE_MANPEDTRANSFCREDIBS.NRO = PED_TRANSF_CRED_IBS.CD_CTR_MAN_PED_TRANSF_CRED_IBS
		
		LEFT JOIN EST_NFE_CANCEL_EVENTO CANCEL_EVENTO ON  
		CANCEL_EVENTO.CD_CTR_EVENTO = PED_TRANSF_CRED_IBS.CD_CTR_MAN_PED_TRANSF_CRED_IBS 
		AND CANCEL_EVENTO.TIPO_EVENTO = '212110'
		LEFT JOIN  EST_NFE_CANCEL_EVENTO_VALIDADA CANCEL_VALIDADA ON 
		CANCEL_VALIDADA.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO

 

	union all

	SELECT '212110' AS COD_EVENTO
		, 13 AS CD_EVENTO
		, 'Manifestação sobre pedido de transferência de crédito de IBS em operações de sucessão' AS DS_EVENTO
		, 'Manifestação sobre pedido de transferência de crédito de IBS em operações de sucessão' 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_PED_TRANSF_CRED_IBS.NR_AUTORIZADOR,'') AS NR_AUTORIZADOR
		, ISNULL(NFE_PED_TRANSF_CRED_IBS.NR_PROTOCOLO,'') AS NR_PROTOCOLO
		, ISNULL(NFE_PED_TRANSF_CRED_IBS.STS_IMP,0) AS STS_IMP
		, ISNULL(NFE_PED_TRANSF_CRED_IBS.DT_RECBTO,'') AS DT_RECBTO
		, PED_TRANSF_CRED_IBS.DT_HR_EVENTO AS DT_ARQUIVO
		, ISNULL(NFE_NFE_MANPEDTRANSFCREDIBS.DS_MOTIVO, '') AS DS_MOTIVO
		, GLB_USU.NM_USU
		, GLB_MOV_OP.DS_MOV_OP
		, PED_TRANSF_CRED_IBS.CD_CTR_MAN_PED_TRANSF_CRED_IBS AS NFE_CTR
		, 0 AS CTR_ORIG_CANCEL
		, '' AS COD_EVENTO_ORIG_CANCEL
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ_ARQ_DANFE
		, GLB_MOV_OP.CD_MOV_OP
		, CASE WHEN ISNULL (CANCEL_VALIDADA.STS_IMP ,0) = 1 THEN 1 ELSE 0 END AS EVENTO_CANCELADO
		, CASE WHEN PED_TRANSF_CRED_IBS.IND_ACEITACAO = 1 THEN  'SIM'  ELSE 'NÃO' END AS IND_ACEITACAO
		
        --Itens geral : para este evento não contém itens 
		, 0  AS SEQ_ITEM 
		, 0  AS CD_PROD 
		, '' AS DS_PROD
		, '' AS UNID
		, 0  AS QTDE_NF
		, 0  AS QTDE
		, 0  AS VLR_IBS
		, 0  AS VLR_IBS
		
		--Itens Perecimento, perda, roubo,furto durante transporte. Para este evento não contém itens 

		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO  
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO  
		
      --Itens Fornecimento não realizado com pagamento antecipado
		,0 AS VLR_IBS_NAO_FORNECIDA  
		,0 AS VLR_IBS_NAO_FORNECIDA
		,'' AS DT_PREVISAO_ENTREGA 
	
	-- Itens Apropriação de crédito presumido
		,0 AS C_CRED_PRES_IBS
		,0 AS P_CRED_PRES_IBS
		,0 AS V_CRED_PRES_IBS
		,0 AS C_CRED_PRES_IBS
		,0 AS P_CRED_PRES_IBS
		,0 AS V_CRED_PRES_IBS

		 -- Itens consumo pessoal Entrada
		,0 AS  VLR_IBS_CONS_PESSOAL_ENT
		,0 AS VLR_IBS_CONS_PESSOAL_ENT  
		,'' AS CH_ACESSO_DFE_REFERENCIA_ENT  
		,0 AS SEQ_ITEM_DFE_REFERENCIA_ENT  

		-- Itens consumo pessoal Saída
		,0 AS VLR_IBS_CONS_PESSOAL_SAI
		,0 AS VLR_IBS_CONS_PESSOAL_SAI
		,'' AS CH_ACESSO_DFE_REFERENCIA_SAI
		,0 AS SEQ_ITEM_DFE_REFERENCIA_SAI

        --Itens Perecimento, perda, roubo,furto
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO_DEST 
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO_DEST

		--Itens imobilização de item 
		, 0 as VLR_IBS_IMOBILIZ_ITEM
		, 0 as VLR_IBS_IMOBILIZ_ITEM

		--Itens credito combustivel
		,0 AS VLR_IBS_CRED_COMBUST
		,0 AS VLR_IBS_CRED_COMBUST
				  
		-- Itens Apropriação de crédito bens serviços
		,0 AS VLR_CRED_IBS_BENS_SERV
		,0  AS VLR_CRED_IBS_BENS_SERV
	 
 
		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_MAN_PED_TRANSF_CRED_IBS_EST_NF_ENT AS PED_TRANSF_CRED_IBS_ENT ON 
		  PED_TRANSF_CRED_IBS_ENT.CD_EMP = EST_NF_ENT.CD_EMP
		  AND PED_TRANSF_CRED_IBS_ENT.CD_FILIAL =  EST_NF_ENT.CD_FILIAL
		  AND PED_TRANSF_CRED_IBS_ENT.CD_NF = EST_NF_ENT.CD_NF

		  INNER JOIN EST_NFE_MAN_PED_TRANSF_CRED_IBS AS PED_TRANSF_CRED_IBS  ON 
		  PED_TRANSF_CRED_IBS.CD_CTR_MAN_PED_TRANSF_CRED_IBS = PED_TRANSF_CRED_IBS_ENT.CD_CTR_MAN_PED_TRANSF_CRED_IBS
  
		 INNER JOIN GLB_USU ON  
         PED_TRANSF_CRED_IBS.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_MAN_PED_TRANSF_CRED_IBS_VALIDADA NFE_PED_TRANSF_CRED_IBS   ON  
         PED_TRANSF_CRED_IBS.CD_CTR_MAN_PED_TRANSF_CRED_IBS = NFE_PED_TRANSF_CRED_IBS.CD_CTR_MAN_PED_TRANSF_CRED_IBS
		 
		 LEFT JOIN EST_NFE_MAN_PED_TRANSF_CRED_IBS_ARQ_DANFE ARQ_DANFE ON 
		 ARQ_DANFE.CD_CTR_MAN_PED_TRANSF_CRED_IBS  = PED_TRANSF_CRED_IBS.CD_CTR_MAN_PED_TRANSF_CRED_IBS
		 
	    LEFT JOIN NFE..EST_NFE_MANPEDTRANSFCREDIBS NFE_NFE_MANPEDTRANSFCREDIBS   ON 
		NFE_NFE_MANPEDTRANSFCREDIBS.NRO = PED_TRANSF_CRED_IBS.CD_CTR_MAN_PED_TRANSF_CRED_IBS
		
		LEFT JOIN EST_NFE_CANCEL_EVENTO CANCEL_EVENTO ON  
		CANCEL_EVENTO.CD_CTR_EVENTO = PED_TRANSF_CRED_IBS.CD_CTR_MAN_PED_TRANSF_CRED_IBS 
		AND CANCEL_EVENTO.TIPO_EVENTO = '212110'
		LEFT JOIN  EST_NFE_CANCEL_EVENTO_VALIDADA CANCEL_VALIDADA ON 
		CANCEL_VALIDADA.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO


		---------------------------------------

	UNION ALL 

	SELECT '212120' AS COD_EVENTO
		, 14 AS CD_EVENTO
		, 'Manifestação sobre pedido de transferência de crédito de CBS em operações de sucessão' AS DS_EVENTO
		, 'Manifestação sobre pedido de transferência de crédito de CBS em operações de sucessão' AS DS_EVENTO_SIMP
		, EST_NF_SAI.CD_EMP
		, EST_NF_SAI.CD_FILIAL
		, EST_NF_SAI.CD_NF
		, EST_NF_SAI.NF_NF AS NR_NF
		, 1 AS NF_EMISSAO_PROPRIA
		, EST_NF_SAI.CD_NF_SERIE
		, ISNULL(EST_NF_SERIE.SERIE, '') AS SERIE
		, ISNULL(EST_NF_SERIE.MODELO, '') AS MODELO
		, EST_NF_SAI.CD_CLI AS CD_ORIG
		, V_GLB_PESSOA.RAZAO_SOCIAL_PESSOA AS NM_ORIG
		, EST_NF_SAI.DT_EMI_NF AS DT_ENT
		, EST_NF_SAI.DT_EMI_NF
		,'SAÍDA' AS TIPO_NF  
		, EST_NF_SAI.VLR_NF
		, ISNULL(NFE_PED_TRANSF_CRED_CBS.NR_AUTORIZADOR,'') AS NR_AUTORIZADOR
		, ISNULL(NFE_PED_TRANSF_CRED_CBS.NR_PROTOCOLO,'') AS NR_PROTOCOLO
		, ISNULL(NFE_PED_TRANSF_CRED_CBS.STS_IMP,0) AS STS_IMP
		, ISNULL(NFE_PED_TRANSF_CRED_CBS.DT_RECBTO,'') AS DT_RECBTO
		, PED_TRANSF_CRED_CBS.DT_HR_EVENTO AS DT_ARQUIVO
		, ISNULL(NFE_NFE_MANPEDTRANSFCREDCBS.DS_MOTIVO, '') AS DS_MOTIVO
		, GLB_USU.NM_USU
		, GLB_MOV_OP.DS_MOV_OP
		, PED_TRANSF_CRED_CBS.CD_CTR_MAN_PED_TRANSF_CRED_CBS AS NFE_CTR
		, 0 AS CTR_ORIG_CANCEL
		, '' AS COD_EVENTO_ORIG_CANCEL
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ_ARQ_DANFE
		, GLB_MOV_OP.CD_MOV_OP
		, CASE WHEN ISNULL (CANCEL_VALIDADA.STS_IMP ,0) = 1 THEN 1 ELSE 0 END AS EVENTO_CANCELADO
		, CASE WHEN PED_TRANSF_CRED_CBS.IND_ACEITACAO = 1 THEN  'SIM'  ELSE 'NÃO' END AS IND_ACEITACAO
		
        --Itens geral : para este evento não contém itens 
		, 0  AS SEQ_ITEM 
		, 0  AS CD_PROD 
		, '' AS DS_PROD
		, '' AS UNID
		, 0  AS QTDE_NF
		, 0  AS QTDE
		, 0  AS VLR_CBS
		, 0  AS VLR_CBS
		
		--Itens Perecimento, perda, roubo,furto durante transporte. Para este evento não contém itens 

		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO  
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO  
		
      --Itens Fornecimento não realizado com pagamento antecipado
		,0 AS VLR_CBS_NAO_FORNECIDA  
		,0 AS VLR_CBS_NAO_FORNECIDA
		,'' AS DT_PREVISAO_ENTREGA 
	
	-- Itens Apropriação de crédito presumido
		,0 AS C_CRED_PRES_CBS
		,0 AS P_CRED_PRES_CBS
		,0 AS V_CRED_PRES_CBS
		,0 AS C_CRED_PRES_CBS
		,0 AS P_CRED_PRES_CBS
		,0 AS V_CRED_PRES_CBS

		 -- Itens consumo pessoal Entrada
		,0 AS  VLR_CBS_CONS_PESSOAL_ENT
		,0 AS VLR_CBS_CONS_PESSOAL_ENT  
		,'' AS CH_ACESSO_DFE_REFERENCIA_ENT  
		,0 AS SEQ_ITEM_DFE_REFERENCIA_ENT  

		-- Itens consumo pessoal Saída
		,0 AS VLR_CBS_CONS_PESSOAL_SAI
		,0 AS VLR_CBS_CONS_PESSOAL_SAI
		,'' AS CH_ACESSO_DFE_REFERENCIA_SAI
		,0 AS SEQ_ITEM_DFE_REFERENCIA_SAI

        --Itens Perecimento, perda, roubo,furto
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO_DEST 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO_DEST

		--Itens imobilização de item 
		, 0 as VLR_CBS_IMOBILIZ_ITEM
		, 0 as VLR_CBS_IMOBILIZ_ITEM

		--Itens credito combustivel
		,0 AS VLR_CBS_CRED_COMBUST
		,0 AS VLR_CBS_CRED_COMBUST
				  
		-- Itens Apropriação de crédito bens serviços
		,0 AS VLR_CRED_CBS_BENS_SERV
		,0  AS VLR_CRED_CBS_BENS_SERV
	 
   FROM  DBO.EST_NF_SAI
		 INNER JOIN PRC_FILIAL FL ON DBO.EST_NF_SAI.CD_EMP = FL.CD_EMP
									 AND DBO.EST_NF_SAI.CD_FILIAL = FL.CD_FILIAL
  
	     INNER JOIN DBO.GLB_MOV_OP ON DBO.EST_NF_SAI.CD_NT_MOV = DBO.GLB_MOV_OP.CD_MOV_OP
		  
		 OUTER APPLY UFD_GLB_PESSOA(EST_NF_SAI.CD_CLI, GLB_MOV_OP.DEST_ORIG) V_GLB_PESSOA
 
		 INNER JOIN DBO.EST_NF_SERIE ON DBO.EST_NF_SAI.CD_EMP = DBO.EST_NF_SERIE.CD_EMP
										AND DBO.EST_NF_SAI.CD_NF_SERIE = DBO.EST_NF_SERIE.CD_NF_SERIE

		 INNER JOIN EST_NFE_MAN_PED_TRANSF_CRED_CBS_EST_NF_SAI AS PED_TRANSF_CRED_CBS_SAI ON 
		  PED_TRANSF_CRED_CBS_SAI.CD_EMP = EST_NF_SAI.CD_EMP
		  AND PED_TRANSF_CRED_CBS_SAI.CD_FILIAL =  EST_NF_SAI.CD_FILIAL
		  AND PED_TRANSF_CRED_CBS_SAI.CD_NF = EST_NF_SAI.CD_NF

		  INNER JOIN EST_NFE_MAN_PED_TRANSF_CRED_CBS AS PED_TRANSF_CRED_CBS  ON 
		  PED_TRANSF_CRED_CBS.CD_CTR_MAN_PED_TRANSF_CRED_CBS = PED_TRANSF_CRED_CBS_SAI.CD_CTR_MAN_PED_TRANSF_CRED_CBS
  
		 INNER JOIN GLB_USU ON  
         PED_TRANSF_CRED_CBS.CD_USU = GLB_USU.CD_USU 
       
	     LEFT OUTER JOIN EST_NFE_MAN_PED_TRANSF_CRED_CBS_VALIDADA NFE_PED_TRANSF_CRED_CBS   ON  
         PED_TRANSF_CRED_CBS.CD_CTR_MAN_PED_TRANSF_CRED_CBS = NFE_PED_TRANSF_CRED_CBS.CD_CTR_MAN_PED_TRANSF_CRED_CBS
		 
		 LEFT JOIN EST_NFE_MAN_PED_TRANSF_CRED_CBS_ARQ_DANFE ARQ_DANFE ON 
		 ARQ_DANFE.CD_CTR_MAN_PED_TRANSF_CRED_CBS  = PED_TRANSF_CRED_CBS.CD_CTR_MAN_PED_TRANSF_CRED_CBS
		 
	    LEFT JOIN NFE..EST_NFE_MANPEDTRANSFCREDCBS NFE_NFE_MANPEDTRANSFCREDCBS   ON 
		NFE_NFE_MANPEDTRANSFCREDCBS.NRO = PED_TRANSF_CRED_CBS.CD_CTR_MAN_PED_TRANSF_CRED_CBS
		
		LEFT JOIN EST_NFE_CANCEL_EVENTO CANCEL_EVENTO ON  
		CANCEL_EVENTO.CD_CTR_EVENTO = PED_TRANSF_CRED_CBS.CD_CTR_MAN_PED_TRANSF_CRED_CBS 
		AND CANCEL_EVENTO.TIPO_EVENTO = '212120'
		LEFT JOIN  EST_NFE_CANCEL_EVENTO_VALIDADA CANCEL_VALIDADA ON 
		CANCEL_VALIDADA.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO

 

	union all

	SELECT '212120' AS COD_EVENTO
		, 14 AS CD_EVENTO
		, 'Manifestação sobre pedido de transferência de crédito de CBS em operações de sucessão' AS DS_EVENTO
		, 'Manifestação sobre pedido de transferência de crédito de CBS em operações de sucessão' 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_PED_TRANSF_CRED_CBS.NR_AUTORIZADOR,'') AS NR_AUTORIZADOR
		, ISNULL(NFE_PED_TRANSF_CRED_CBS.NR_PROTOCOLO,'') AS NR_PROTOCOLO
		, ISNULL(NFE_PED_TRANSF_CRED_CBS.STS_IMP,0) AS STS_IMP
		, ISNULL(NFE_PED_TRANSF_CRED_CBS.DT_RECBTO,'') AS DT_RECBTO
		, PED_TRANSF_CRED_CBS.DT_HR_EVENTO AS DT_ARQUIVO
		, ISNULL(NFE_NFE_MANPEDTRANSFCREDCBS.DS_MOTIVO, '') AS DS_MOTIVO
		, GLB_USU.NM_USU
		, GLB_MOV_OP.DS_MOV_OP
		, PED_TRANSF_CRED_CBS.CD_CTR_MAN_PED_TRANSF_CRED_CBS AS NFE_CTR
		, 0 AS CTR_ORIG_CANCEL
		, '' AS COD_EVENTO_ORIG_CANCEL
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ_ARQ_DANFE
		, GLB_MOV_OP.CD_MOV_OP
		, CASE WHEN ISNULL (CANCEL_VALIDADA.STS_IMP ,0) = 1 THEN 1 ELSE 0 END AS EVENTO_CANCELADO
		, CASE WHEN PED_TRANSF_CRED_CBS.IND_ACEITACAO = 1 THEN  'SIM'  ELSE 'NÃO' END AS IND_ACEITACAO
		
        --Itens geral : para este evento não contém itens 
		, 0  AS SEQ_ITEM 
		, 0  AS CD_PROD 
		, '' AS DS_PROD
		, '' AS UNID
		, 0  AS QTDE_NF
		, 0  AS QTDE
		, 0  AS VLR_CBS
		, 0  AS VLR_CBS
		
		--Itens Perecimento, perda, roubo,furto durante transporte. Para este evento não contém itens 

		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO  
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO  
		
      --Itens Fornecimento não realizado com pagamento antecipado
		,0 AS VLR_CBS_NAO_FORNECIDA  
		,0 AS VLR_CBS_NAO_FORNECIDA
		,'' AS DT_PREVISAO_ENTREGA 
	
	-- Itens Apropriação de crédito presumido
		,0 AS C_CRED_PRES_CBS
		,0 AS P_CRED_PRES_CBS
		,0 AS V_CRED_PRES_CBS
		,0 AS C_CRED_PRES_CBS
		,0 AS P_CRED_PRES_CBS
		,0 AS V_CRED_PRES_CBS

		 -- Itens consumo pessoal Entrada
		,0 AS  VLR_CBS_CONS_PESSOAL_ENT
		,0 AS VLR_CBS_CONS_PESSOAL_ENT  
		,'' AS CH_ACESSO_DFE_REFERENCIA_ENT  
		,0 AS SEQ_ITEM_DFE_REFERENCIA_ENT  

		-- Itens consumo pessoal Saída
		,0 AS VLR_CBS_CONS_PESSOAL_SAI
		,0 AS VLR_CBS_CONS_PESSOAL_SAI
		,'' AS CH_ACESSO_DFE_REFERENCIA_SAI
		,0 AS SEQ_ITEM_DFE_REFERENCIA_SAI

        --Itens Perecimento, perda, roubo,furto
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO_DEST 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO_DEST

		--Itens imobilização de item 
		, 0 as VLR_CBS_IMOBILIZ_ITEM
		, 0 as VLR_CBS_IMOBILIZ_ITEM

		--Itens credito combustivel
		,0 AS VLR_CBS_CRED_COMBUST
		,0 AS VLR_CBS_CRED_COMBUST
				  
		-- Itens Apropriação de crédito bens serviços
		,0 AS VLR_CRED_CBS_BENS_SERV
		,0  AS VLR_CRED_CBS_BENS_SERV
	 
 
		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_MAN_PED_TRANSF_CRED_CBS_EST_NF_ENT AS PED_TRANSF_CRED_CBS_ENT ON 
		  PED_TRANSF_CRED_CBS_ENT.CD_EMP = EST_NF_ENT.CD_EMP
		  AND PED_TRANSF_CRED_CBS_ENT.CD_FILIAL =  EST_NF_ENT.CD_FILIAL
		  AND PED_TRANSF_CRED_CBS_ENT.CD_NF = EST_NF_ENT.CD_NF

		  INNER JOIN EST_NFE_MAN_PED_TRANSF_CRED_CBS AS PED_TRANSF_CRED_CBS  ON 
		  PED_TRANSF_CRED_CBS.CD_CTR_MAN_PED_TRANSF_CRED_CBS = PED_TRANSF_CRED_CBS_ENT.CD_CTR_MAN_PED_TRANSF_CRED_CBS
  
		 INNER JOIN GLB_USU ON  
         PED_TRANSF_CRED_CBS.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_MAN_PED_TRANSF_CRED_CBS_VALIDADA NFE_PED_TRANSF_CRED_CBS   ON  
         PED_TRANSF_CRED_CBS.CD_CTR_MAN_PED_TRANSF_CRED_CBS = NFE_PED_TRANSF_CRED_CBS.CD_CTR_MAN_PED_TRANSF_CRED_CBS
		 
		 LEFT JOIN EST_NFE_MAN_PED_TRANSF_CRED_CBS_ARQ_DANFE ARQ_DANFE ON 
		 ARQ_DANFE.CD_CTR_MAN_PED_TRANSF_CRED_CBS  = PED_TRANSF_CRED_CBS.CD_CTR_MAN_PED_TRANSF_CRED_CBS
		 
	    LEFT JOIN NFE..EST_NFE_MANPEDTRANSFCREDCBS NFE_NFE_MANPEDTRANSFCREDCBS   ON 
		NFE_NFE_MANPEDTRANSFCREDCBS.NRO = PED_TRANSF_CRED_CBS.CD_CTR_MAN_PED_TRANSF_CRED_CBS
		
		LEFT JOIN EST_NFE_CANCEL_EVENTO CANCEL_EVENTO ON  
		CANCEL_EVENTO.CD_CTR_EVENTO = PED_TRANSF_CRED_CBS.CD_CTR_MAN_PED_TRANSF_CRED_CBS 
		AND CANCEL_EVENTO.TIPO_EVENTO = '212120'
		LEFT JOIN  EST_NFE_CANCEL_EVENTO_VALIDADA CANCEL_VALIDADA ON 
		CANCEL_VALIDADA.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO

UNION ALL

SELECT '110001' AS COD_EVENTO
        ,15 AS CD_EVENTO
		,'Cancelamento de evento' AS DS_EVENTO
		,'Cancelamento de evento' AS DS_EVENTO_SIMP
		, 1 AS CD_EMP
		, 0 AS CD_FILIAL
		, 0 AS CD_NF
		, 0 AS NR_NF
		, 0 AS NF_EMISSAO_PROPRIA
		, 0 AS CD_NF_SERIE
		, ''  AS SERIE
		,'' AS MODELO
		, 0 AS CD_ORIG
		, '' AS NM_ORIG
		, '' DT_ENT
		, '' DT_EMI_NF
		,'CANCEL EVENTO - ' + CANCEL_EVENTO.TIPO_EVENTO  AS TIPO_NF  
		, 0 AS VLR_NF
		, ISNULL(NFE_CANCEL_EVENTO.NR_AUTORIZADOR,'') AS NR_AUTORIZADOR
		, ISNULL(NFE_CANCEL_EVENTO.NR_PROTOCOLO,'') AS NR_PROTOCOLO
		, ISNULL(NFE_CANCEL_EVENTO.STS_IMP,0) AS STS_IMP
		, ISNULL(NFE_CANCEL_EVENTO.DT_RECBTO,'') AS DT_RECBTO
		, CANCEL_EVENTO.DT_HR_EVENTO AS DT_ARQUIVO
		, ISNULL(NFE_NFE_CANCEL_EVENTO.DS_MOTIVO, '') AS DS_MOTIVO
		, GLB_USU.NM_USU
		, '' DS_MOV_OP
		, CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO AS NFE_CTR
		, CANCEL_EVENTO.CD_CTR_EVENTO AS CTR_ORIG_CANCEL
		, CANCEL_EVENTO.TIPO_EVENTO AS COD_EVENTO_ORIG_CANCEL
		, ISNULL(ARQ_DANFE.CD_CTR_ARQ,0) AS CD_CTR_ARQ_ARQ_DANFE
		, 0 AS CD_MOV_OP
		, 0 AS EVENTO_CANCELADO
		,'NÃO SE APLICA' AS IND_ACEITACAO
		
		--Itens geral : para este evento não contém itens 
		, 0  AS SEQ_ITEM 
		, 0  AS CD_PROD 
		, '' AS DS_PROD
		, '' AS UNID
		, 0  AS QTDE_NF
		, 0  AS QTDE
		, 0  AS VLR_IBS
		, 0  AS VLR_CBS
		
		--Itens Perecimento, perda, roubo,furto durante transporte. Para este evento não contém itens 

		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO  
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO  
		
      --Itens Fornecimento não realizado com pagamento antecipado
		,0 AS VLR_IBS_NAO_FORNECIDA  
		,0 AS VLR_CBS_NAO_FORNECIDA
		,'' AS DT_PREVISAO_ENTREGA 
	
	-- Itens Apropriação de crédito presumido
		,0 AS C_CRED_PRES_IBS
		,0 AS P_CRED_PRES_IBS
		,0 AS V_CRED_PRES_IBS
		,0 AS C_CRED_PRES_CBS
		,0 AS P_CRED_PRES_CBS
		,0 AS V_CRED_PRES_CBS

		 -- Itens consumo pessoal Entrada
		,0 AS  VLR_IBS_CONS_PESSOAL_ENT
		,0 AS VLR_CBS_CONS_PESSOAL_ENT  
		,'' AS CH_ACESSO_DFE_REFERENCIA_ENT  
		,0 AS SEQ_ITEM_DFE_REFERENCIA_ENT  

		-- Itens consumo pessoal Saída
		,0 AS VLR_IBS_CONS_PESSOAL_SAI
		,0 AS VLR_CBS_CONS_PESSOAL_SAI
		,'' AS CH_ACESSO_DFE_REFERENCIA_SAI
		,0 AS SEQ_ITEM_DFE_REFERENCIA_SAI

        --Itens Perecimento, perda, roubo,furto
		, 0 AS VLR_IBS_PEREC_PERDA_ROUBO_DEST 
		, 0 AS VLR_CBS_PEREC_PERDA_ROUBO_DEST

		--Itens imobilização de item 
		, 0 as VLR_IBS_IMOBILIZ_ITEM
		, 0 as VLR_CBS_IMOBILIZ_ITEM

		--Itens credito combustivel
		,0 AS VLR_IBS_CRED_COMBUST
		,0 AS VLR_CBS_CRED_COMBUST
				  
		-- Itens Apropriação de crédito bens serviços
		,0 AS VLR_CRED_IBS_BENS_SERV
		,0  AS VLR_CRED_CBS_BENS_SERV
 
		FROM  EST_NFE_CANCEL_EVENTO AS CANCEL_EVENTO
 
		 INNER JOIN GLB_USU ON  
         CANCEL_EVENTO.CD_USU = GLB_USU.CD_USU 
 
		LEFT OUTER JOIN EST_NFE_CANCEL_EVENTO_VALIDADA NFE_CANCEL_EVENTO ON  
         NFE_CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO
		 
		 LEFT JOIN EST_NFE_CANCEL_EVENTO_ARQ_DANFE ARQ_DANFE ON 
		 ARQ_DANFE.CD_CTR_CANCEL_EVENTO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO
		 
	    LEFT JOIN NFE..EST_NFE_CANCELEVENTO NFE_NFE_CANCEL_EVENTO ON 
		NFE_NFE_CANCEL_EVENTO.NRO = CANCEL_EVENTO.CD_CTR_CANCEL_EVENTO





