/****** Object:  Trigger [TRG_EST_NF_ENT_EST_NF_SAI_TRANSF_INCLUI_ARQ_XML]    Script Date: 08/08/2012 10:00:52 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[TRG_EST_NF_ENT_EST_NF_SAI_TRANSF_INCLUI_ARQ_XML]'))
DROP TRIGGER [dbo].[TRG_EST_NF_ENT_EST_NF_SAI_TRANSF_INCLUI_ARQ_XML]
GO
GO
-----------------------------------INSERÇÃO EM EST_NF_ENT_EST_NF_SAI_TRANSF-----------------------------------
CREATE TRIGGER [dbo].[TRG_EST_NF_ENT_EST_NF_SAI_TRANSF_INCLUI_ARQ_XML] ON [dbo].[EST_NF_ENT_EST_NF_SAI_TRANSF] FOR INSERT AS
--Inclui o arquivo xml gerado pela filial de origem das notas fiscais
INSERT INTO EST_NF_ENT_ARQ_DANFE(CD_CTR_DANFE_PROV,CD_EMP,CD_FILIAL,CD_NF,ARQUIVO_DANFE)
SELECT
    0,		
    INSERTED.CD_EMP,
    INSERTED.CD_FIL_DEST, 
    INSERTED.CD_NF_ENT,
    EST_NF_SAI_ARQ_DANFE.ARQUIVO_DANFE 
FROM         
    INSERTED 
    INNER JOIN EST_NF_SAI_ARQ_DANFE ON 
    INSERTED.CD_EMP = EST_NF_SAI_ARQ_DANFE.CD_EMP AND 
    INSERTED.CD_FIL_ORIG = EST_NF_SAI_ARQ_DANFE.CD_FILIAL AND 
    INSERTED.CD_NF_SAI = EST_NF_SAI_ARQ_DANFE.CD_NF


--Pedencia de arquivo DANFE para validacao
INSERT INTO EST_NF_ENT_NFE_FORN_VALID
(CD_EMP, 
 CD_FILIAL, 
 CD_FORN, 
 NR_NF, 
 DT_EMI_NF, 
 NR_AUTORIZADOR, 
 NR_PROTOCOLO, 
 DIG_VALIDACAO, 
 DT_VALIDACAO, 
 VALIDACAO, 
 CD_USU, 
 DT_CAD, 
 ERR_VALIDACAO, 
 CANCELADA_SEFAZ)
SELECT		
	DISTINCT INSERTED.CD_EMP,
	INSERTED.CD_FIL_DEST, 
	PRC_FILIAL_PG_FORN.CD_FORN,
	EST_NF_SAI.NF_NF,
	EST_NF_SAI.DT_EMI_NF,
	EST_NF_SAI_NFE.NR_AUTORIZADOR,
	EST_NF_SAI_NFE.NR_PROTOCOLO,
	EST_NF_SAI_NFE.DG_VALIDADOR,
	EST_NF_SAI_NFE.DT_RECBTO,
	0,
	1,
	CONVERT(CHAR(10),GETDATE(),112),
	'',
	0  
FROM         
	INSERTED INNER JOIN EST_NF_SAI_NFE ON 
	INSERTED.CD_EMP = EST_NF_SAI_NFE.CD_EMP AND 
    INSERTED.CD_FIL_ORIG = EST_NF_SAI_NFE.CD_FILIAL AND 
    INSERTED.CD_NF_SAI = EST_NF_SAI_NFE.CD_NF
	INNER JOIN EST_NF_SAI ON 
	INSERTED.CD_EMP = EST_NF_SAI.CD_EMP AND 
    INSERTED.CD_FIL_ORIG = EST_NF_SAI.CD_FILIAL AND 
    INSERTED.CD_NF_SAI = EST_NF_SAI.CD_NF
	INNER JOIN PRC_FILIAL_PG_FORN ON
	INSERTED.CD_EMP = PRC_FILIAL_PG_FORN.CD_EMP AND
	INSERTED.CD_FIL_ORIG = PRC_FILIAL_PG_FORN.CD_FILIAL    
    LEFT OUTER JOIN EST_NF_ENT_NFE_FORN_VALID ON
    EST_NF_SAI.CD_EMP = EST_NF_ENT_NFE_FORN_VALID.CD_EMP AND
    PRC_FILIAL_PG_FORN.CD_FORN  = EST_NF_ENT_NFE_FORN_VALID.CD_FORN  AND
    EST_NF_SAI.NF_NF    = EST_NF_ENT_NFE_FORN_VALID.NR_NF AND
    EST_NF_SAI.DT_EMI_NF     = EST_NF_ENT_NFE_FORN_VALID.DT_EMI_NF 
WHERE
	EST_NF_ENT_NFE_FORN_VALID.NR_NF  IS NULL

--Vinculando com a entrada
INSERT INTO EST_NF_ENT_EST_NF_ENT_NFE_FORN_VALID
SELECT 
	INSERTED.CD_EMP,
	INSERTED.CD_FIL_DEST,
	INSERTED.CD_NF_ENT,
	EST_NF_ENT_NFE_FORN_VALID.CD_CTR 
FROM         
	EST_NF_ENT_NFE_FORN_VALID INNER JOIN EST_NF_ENT ON 
	EST_NF_ENT_NFE_FORN_VALID.CD_EMP = EST_NF_ENT.CD_EMP AND 
    EST_NF_ENT_NFE_FORN_VALID.CD_FILIAL = EST_NF_ENT.CD_FILIAL AND 
    EST_NF_ENT_NFE_FORN_VALID.NR_NF = EST_NF_ENT.NR_NF AND 
    EST_NF_ENT_NFE_FORN_VALID.DT_EMI_NF = EST_NF_ENT.DT_EMI_NF
	INNER JOIN INSERTED ON 
	EST_NF_ENT.CD_EMP = INSERTED.CD_EMP AND 
    EST_NF_ENT.CD_FILIAL = INSERTED.CD_FIL_DEST  AND 
    EST_NF_ENT.CD_NF = INSERTED.CD_NF_ENT   
    LEFT OUTER JOIN EST_NF_ENT_EST_NF_ENT_NFE_FORN_VALID   ON
    EST_NF_ENT_NFE_FORN_VALID.CD_CTR = EST_NF_ENT_EST_NF_ENT_NFE_FORN_VALID.CD_CTR 
WHERE
	EST_NF_ENT_EST_NF_ENT_NFE_FORN_VALID.CD_CTR IS NULL
GO
