/****** Object:  View [dbo].[V_EST_NF_ENT_DANFE_PROVISORIO_HISTORICO_BLOQUEIOS]    Script Date: 07/04/2013 16:59:30 ******/
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[V_EST_NF_ENT_DANFE_PROVISORIO_HISTORICO_BLOQUEIOS]'))
DROP VIEW [dbo].[V_EST_NF_ENT_DANFE_PROVISORIO_HISTORICO_BLOQUEIOS]
GO
GO
--SELECT * FROM V_EST_NF_ENT_DANFE_PROVISORIO_HISTORICO_BLOQUEIOS
CREATE VIEW V_EST_NF_ENT_DANFE_PROVISORIO_HISTORICO_BLOQUEIOS WITH ENCRYPTION AS
SELECT     
	V_EST_NF_ENT_CAB.CD_EMP, 
	V_EST_NF_ENT_CAB.CD_FILIAL, 
	V_EST_NF_ENT_CAB.CD_FORN, 
	V_EST_NF_ENT_CAB.NM_ORIG,
	V_EST_NF_ENT_CAB.NR_NF, 
	V_EST_NF_ENT_CAB.DS_MOV_OP,
	ISNULL(EST_PED_CP.CD_PED,0) AS CD_PED,
	EST_PED_CP.DT_EMI AS DT_EMI_PEDIDO, 
	V_EST_NF_ENT_CAB.DT_EMI_NF, 
	V_EST_NF_ENT_CAB.DT_ENT, 
	V_EST_NF_ENT_CAB.VLR_NF, 
    EST_NF_ENT_DANFE_PROVISORIO.DT_CAD AS DT_CAD_NFE,
	EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA.DT_TRANSACAO AS DT_CHECKIN,
	TIPO_BLQ_LIB.DS_TIPO_BLQ AS DS_TIPO_BLQ_LIB,
	GLB_USU.NM_USU AS NM_USU_LIBEROU,
	LIB.DT_LIBERACAO,
	ISNULL(TIPO_BLQ_BLQ.DS_TIPO_BLQ,'') AS DS_TIPO_BLQ,
	ISNULL(GLB_USU_BLQ.NM_USU,'') AS NM_USU_BLOQUEOU,
	EST_NF_ENT_DANFE_PROVISORIO_BLOQUEIO.DT_LIB AS DT_BLOQUEIO
FROM            
	V_EST_NF_ENT_CAB LEFT OUTER JOIN EST_NF_ENT_PED_CP ON 
	V_EST_NF_ENT_CAB.CD_EMP = EST_NF_ENT_PED_CP.CD_EMP AND 
	V_EST_NF_ENT_CAB.CD_FILIAL = EST_NF_ENT_PED_CP.CD_FILIAL AND 
    V_EST_NF_ENT_CAB.CD_NF = EST_NF_ENT_PED_CP.CD_NF 
	LEFT OUTER JOIN EST_PED_CP ON 
	EST_NF_ENT_PED_CP.CD_EMP = EST_PED_CP.CD_EMP AND 
	EST_NF_ENT_PED_CP.CD_FILIAL = EST_PED_CP.CD_FILIAL AND 
    EST_NF_ENT_PED_CP.CD_PED = EST_PED_CP.CD_PED 
	LEFT OUTER JOIN EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT ON 
	V_EST_NF_ENT_CAB.CD_EMP = EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT.CD_EMP AND 
    V_EST_NF_ENT_CAB.CD_FILIAL = EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT.CD_FILIAL AND 
	V_EST_NF_ENT_CAB.CD_NF = EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT.CD_NF 
	LEFT OUTER JOIN EST_NF_ENT_DANFE_PROVISORIO ON 
	EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT.CD_CTR = EST_NF_ENT_DANFE_PROVISORIO.CD_CTR
	LEFT OUTER JOIN EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA ON
	EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT.CD_CTR = EST_NF_ENT_DANFE_PROVISORIO_CHECKIN_ENTRADA.CD_CTR
	LEFT OUTER JOIN EST_NF_ENT_DANFE_PROVISORIO_LIBERACAO_TIPO_BLQ LIB ON 
	EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT.CD_CTR = LIB.CD_CTR 
	INNER JOIN V_EST_NF_ENT_DANFE_PROVISORIO_TIPO_BLOQUEIO TIPO_BLQ_LIB ON
	LIB.TIPO_BLQ = TIPO_BLQ_LIB.TIPO_BLQ  
	LEFT OUTER JOIN GLB_USU ON
	LIB.CD_USU_LIB = GLB_USU.CD_USU
	LEFT OUTER JOIN EST_NF_ENT_DANFE_PROVISORIO_BLOQUEIO ON
	EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT.CD_CTR = EST_NF_ENT_DANFE_PROVISORIO_BLOQUEIO.CD_CTR 
	LEFT OUTER JOIN V_EST_NF_ENT_DANFE_PROVISORIO_TIPO_BLOQUEIO TIPO_BLQ_BLQ ON
	EST_NF_ENT_DANFE_PROVISORIO_BLOQUEIO.TIPO_BLQ = TIPO_BLQ_BLQ.TIPO_BLQ  
	LEFT OUTER JOIN GLB_USU GLB_USU_BLQ ON
	EST_NF_ENT_DANFE_PROVISORIO_BLOQUEIO.CD_USU = GLB_USU_BLQ.CD_USU