SET TERM ^ ;
CREATE OR ALTER TRIGGER TTBPDV_CFE_FORMA_PAG_B01U FOR TBPDV_CFE_FORMA_PAGAMENTO
ACTIVE BEFORE UPDATE POSITION 9
AS
BEGIN
  IF(OLD.VALOR <> NEW.VALOR)THEN
  BEGIN
     EXECUTE PROCEDURE SPI_GERAR_COMISSAO(NEW.PDV_CFE_ID,'CFE');
  END

  IF(OLD.PAGAMENTO_FORMA_ID <> NEW.PAGAMENTO_FORMA_ID)THEN
  BEGIN
     NEW.ADQUIRENTE_ID = COALESCE((SELECT FIRST 1 PDV_POS.ADQUIRENTE_ID
                                        FROM TBPAGAMENTO_FORMA B, TBPDV_ADQUIRENTE PDV_ADQ, TBPDV_POS PDV_POS
                                        WHERE B.CODIGO = NEW.PAGAMENTO_FORMA_ID AND B.NFE_TIPPAG IN ('03','04')
                                        AND B.EMPRESA_ID > 0 AND B.STATUS = '1' AND B.EMPRESA_ID = PDV_ADQ.EMPRESA_ID
                                        AND PDV_POS.ADQUIRENTE_ID = PDV_ADQ.ID),0);

     IF(NEW.ADQUIRENTE_ID = 0)THEN
     BEGIN
        NEW.NSU                = '';
        NEW.CODIGO_AUTORIZACAO = '';
        NEW.BANDEIRA_CARTAO    = '';
     END
  END
END
^ SET TERM ; ^