SET TERM ^ ;

CREATE OR ALTER trigger tbpdv_cfe_b13i for tbpdv_cfe
active before insert position 13
AS
DECLARE VARIABLE CANAL_VENDAS_ID_PDV INTEGER;
BEGIN
    /* DEFINIR CANAL DE VENDAS PARA IMPORTACAO DE DOCUMENTOS FISCAIS */
    IF(NEW.SITUACAO = 2 AND NEW.IMPORTACAO = 1) THEN
    BEGIN
        :CANAL_VENDAS_ID_PDV        = COALESCE((SELECT FIRST 1 COALESCE(T.CANAL_VENDAS_ID, 0) FROM TBPDV T WHERE T.ID = NEW.PDV_ID), 0);
    
        NEW.CANAL_VENDAS_ID         = :CANAL_VENDAS_ID_PDV;
    END

END
^

SET TERM ; ^

