SET TERM ^ ;

CREATE OR ALTER trigger ttbpdv_cfe_b06i for tbpdv_cfe
active before insert position 6
AS
BEGIN
  IF(COALESCE(NEW.COLABORADOR_ID, 0) > 0) THEN
  BEGIN
    NEW.CLIENTE_ID = FN_PARAMETRO('SISTEMA.COLABORADOR_PADRAO_PDV');
  END

  IF(COALESCE(NEW.CLIENTE_ID, 0) = 0) THEN
  BEGIN
    NEW.CLIENTE_ID = COALESCE((SELECT FIRST 1 T.CLIENTE_ID FROM TBPDV T WHERE T.ID = NEW.PDV_ID), 0);

  END
END
^

SET TERM ; ^

