IF NOT OBJECT_ID('TRG_PDV_VD_TEF_CARTAO_CONV') IS NULL
	BEGIN
		DROP TRIGGER TRG_PDV_VD_TEF_CARTAO_CONV
	END

GO
  
CREATE TRIGGER [TRG_PDV_VD_TEF_CARTAO_CONV] ON [dbo].[PDV_VD_TEF] FOR INSERT AS  
	BEGIN  
		-----------------------------------------------------------------------------
		--TRIGGER PARA MAPEAR OS TEF DO CARTAO CONVCARD.			
		-----------------------------------------------------------------------------
		UPDATE PDV_TEF SET 
			CD_ADM_CARTAO = C_PDV_CARTAO.CD_ADM_CARTAO
		FROM 
			INSERTED I
			INNER JOIN PDV_VD_TEF PDV_TEF ON 
				I.CD_EMP				= PDV_TEF.CD_EMP
				AND I.CD_FILIAL			= PDV_TEF.CD_FILIAL
				AND I.CD_VD				= PDV_TEF.CD_VD
				AND I.CD_CTR			= PDV_TEF.CD_CTR
			INNER JOIN RC_ADM_CARTAO_INTERFACEPDV C_PDV ON
				PDV_TEF.NM_OPERADORA_PDV		= C_PDV.NOME
			INNER JOIN RC_ADM_CARTAO C_PDV_CARTAO ON 
				C_PDV.CD_ADM_CARTAO		= C_PDV_CARTAO.CD_ADM_CARTAO					
			INNER JOIN RC_ADM_CARTAO TEF_CARTAO ON 
				PDV_TEF.CD_EMP					= TEF_CARTAO.CD_EMP
				AND PDV_TEF.CD_ADM_CARTAO		= TEF_CARTAO.CD_ADM_CARTAO							
			
		WHERE
			C_PDV_CARTAO.TP_ADM = 4 --CONVENIO
			AND TEF_CARTAO.FLAG_OPERADORA_NAO_DEFINIDA = 1
			 	
	END

