SET TERM ^ ;
CREATE OR ALTER TRIGGER TTBCONCIL_CARTAO_RECEBER_B01I FOR TBCONCILIACAO_CARTAO_RECEBER BEFORE INSERT
as
begin
  if (new.id is null) THEN
  BEGIN
    new.id = gen_id(gtbconciliacao_cartao_receber,1);
  END
  
  if(COALESCE(NEW.valor_pago,0.00) = 0)THEN
  BEGIN
  	 NEW.VALOR_PAGO = COALESCE(CAST(NEW.VALOR AS DOUBLE PRECISION),0.00)-COALESCE(CAST(NEW.DESCONTO AS DOUBLE PRECISION),0.00);
  	
  	 NEW.VALOR_PAGO = CAST(NEW.VALOR_PAGO AS NUMERIC(15,2)); 
  END
  
end
^ SET TERM ; ^