CREATE OR ALTER TRIGGER TTBCONCILIACAO_CARTAO_B01U FOR TBCONCILIACAO_CARTAO
ACTIVE BEFORE UPDATE POSITION 1
AS
BEGIN
  IF(NEW.CODIGO_AUTORIZACAO = '0' OR NEW.CODIGO_AUTORIZACAO IS NULL) THEN
  BEGIN
     NEW.CODIGO_AUTORIZACAO = NEW.NSU;
  END

  IF(NEW.CONCILIACAO = 1 AND NEW.CONCILIACAO <> OLD.CONCILIACAO)THEN
  BEGIN
     UPDATE TBCONCILIACAO_CARTAO_PARCELAS B
       SET B.CONCILIACAO = 1
     WHERE B.NSU         = NEW.NSU
     AND B.ADQUIRENTE_ID = NEW.ADQUIRENTE_ID
     AND B.MERCHANT_ID   = NEW.MERCHANT_ID
     AND B.DATA_VENDA    = NEW.DATA_VENDA;
  END

  IF(NEW.CONCILIACAO = 0 AND NEW.CONCILIACAO <> OLD.CONCILIACAO)THEN
  BEGIN
     UPDATE TBCONCILIACAO_CARTAO_PARCELAS B
       SET B.CONCILIACAO = 0
     WHERE B.NSU         = NEW.NSU
     AND B.ADQUIRENTE_ID = NEW.ADQUIRENTE_ID
     AND B.MERCHANT_ID   = NEW.MERCHANT_ID
     AND B.DATA_VENDA    = NEW.DATA_VENDA;
  END
END;