
CREATE OR ALTER TRIGGER TTBCAIXABANCO_LANC_CLASS_999 FOR TBCAIXABANCO_LANCAMENTO_CLASS
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 999
AS
BEGIN
  IF (INSERTING) THEN
      BEGIN
      IF (EXTRACT(MONTH FROM NEW.DATA)||EXTRACT(YEAR FROM NEW.DATA) <> EXTRACT(MONTH FROM CURRENT_DATE)||EXTRACT(YEAR FROM CURRENT_DATE)) THEN
          BEGIN
          UPDATE OR INSERT INTO TBREPLICA_TABELA (TABELA, ANO, MES, STATUS)
           VALUES ('TBCAIXABANCO_LANCAMENTO_CLASS',
                   CAST( EXTRACT (YEAR FROM NEW.DATA) AS INTEGER),
                   CAST( EXTRACT (MONTH FROM NEW.DATA) AS INTEGER),
                   '0')
           MATCHING (TABELA, ANO, MES);
          END
      END
  IF (DELETING) THEN
      BEGIN
      IF (EXTRACT(MONTH FROM OLD.DATA)||EXTRACT(YEAR FROM OLD.DATA) <> EXTRACT(MONTH FROM CURRENT_DATE)||EXTRACT(YEAR FROM CURRENT_DATE)) THEN
          BEGIN
          UPDATE TBREPLICA_TABELA SET STATUS = '0'
          WHERE TABELA = 'TBCAIXABANCO_LANCAMENTO_CLASS'
            AND ANO = CAST( EXTRACT (YEAR FROM OLD.DATA) AS INTEGER)
            AND MES = CAST( EXTRACT (MONTH FROM OLD.DATA) AS INTEGER)
            AND STATUS = '1';
          END
      END
  IF (UPDATING) THEN
      BEGIN
      IF (EXTRACT(MONTH FROM OLD.DATA)||EXTRACT(YEAR FROM OLD.DATA) <> EXTRACT(MONTH FROM CURRENT_DATE)||EXTRACT(YEAR FROM CURRENT_DATE)) THEN
          BEGIN
          UPDATE TBREPLICA_TABELA SET STATUS = '0'
           WHERE TABELA = 'TBCAIXABANCO_LANCAMENTO_CLASS'
             AND ANO = CAST( EXTRACT (YEAR FROM OLD.DATA) AS INTEGER)
             AND MES = CAST( EXTRACT (MONTH FROM OLD.DATA) AS INTEGER)
             AND STATUS = '1';
          END

      IF (EXTRACT(MONTH FROM NEW.DATA)||EXTRACT(YEAR FROM NEW.DATA) <> EXTRACT(MONTH FROM CURRENT_DATE)||EXTRACT(YEAR FROM CURRENT_DATE)) THEN
          BEGIN
          IF (EXTRACT (YEAR FROM OLD.DATA) <> EXTRACT (YEAR FROM NEW.DATA) OR
              EXTRACT (MONTH FROM OLD.DATA) <> EXTRACT (MONTH FROM NEW.DATA)) THEN
              BEGIN
              UPDATE OR INSERT INTO TBREPLICA_TABELA (TABELA, ANO, MES, STATUS)
               VALUES ('TBCAIXABANCO_LANCAMENTO_CLASS',
                   CAST( EXTRACT (YEAR FROM NEW.DATA) AS INTEGER),
                   CAST( EXTRACT (MONTH FROM NEW.DATA) AS INTEGER),
                   '0')
               MATCHING (TABELA, ANO, MES);
              END
          END
      END                         
END;

CREATE OR ALTER TRIGGER TTBCXABCO_LANC_CLASS_SALDO_999 FOR TBCAIXABANCO_LANC_CLASS_SALDO
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 999
AS
DECLARE VARIABLE DATA DATE;
BEGIN

  IF (INSERTING) THEN
      BEGIN
      DATA = COALESCE((SELECT FIRST 1 A.DATA FROM TBCAIXABANCO_LANCAMENTO_CLASS A WHERE A.CONTROLE = NEW.LANC_CLASS_ID),CURRENT_DATE);

      IF (EXTRACT(MONTH FROM :DATA)||EXTRACT(YEAR FROM :DATA) <> EXTRACT(MONTH FROM CURRENT_DATE)||EXTRACT(YEAR FROM CURRENT_DATE)) THEN
          BEGIN
          UPDATE OR INSERT INTO TBREPLICA_TABELA (TABELA, ANO, MES, STATUS)
           VALUES ('TBCAIXABANCO_LANCAMENTO_CLASS_SALDO',
                   CAST( EXTRACT (YEAR FROM :DATA) AS INTEGER),
                   CAST( EXTRACT (MONTH FROM :DATA) AS INTEGER),
                   '0')
           MATCHING (TABELA, ANO, MES);
          END
      END

  IF (DELETING) THEN
      BEGIN
      DATA = COALESCE((SELECT FIRST 1 A.DATA FROM TBCAIXABANCO_LANCAMENTO_CLASS A WHERE A.CONTROLE = OLD.LANC_CLASS_ID),CURRENT_DATE);

      IF (EXTRACT(MONTH FROM :DATA)||EXTRACT(YEAR FROM :DATA) <> EXTRACT(MONTH FROM CURRENT_DATE)||EXTRACT(YEAR FROM CURRENT_DATE)) THEN
          BEGIN
          UPDATE TBREPLICA_TABELA SET STATUS = '0'
          WHERE TABELA = 'TBCAIXABANCO_LANCAMENTO_CLASS_SALDO'
            AND ANO = CAST( EXTRACT (YEAR FROM :DATA) AS INTEGER)
            AND MES = CAST( EXTRACT (MONTH FROM :DATA) AS INTEGER)
            AND STATUS = '1';
          END
      END

  IF (UPDATING) THEN
      BEGIN
      DATA = COALESCE((SELECT FIRST 1 A.DATA FROM TBCAIXABANCO_LANCAMENTO_CLASS A WHERE A.CONTROLE = NEW.LANC_CLASS_ID),CURRENT_DATE);

      IF (EXTRACT(MONTH FROM :DATA)||EXTRACT(YEAR FROM :DATA) <> EXTRACT(MONTH FROM CURRENT_DATE)||EXTRACT(YEAR FROM CURRENT_DATE)) THEN
          BEGIN
          UPDATE TBREPLICA_TABELA SET STATUS = '0'
           WHERE TABELA = 'TBCAIXABANCO_LANCAMENTO_CLASS_SALDO'
             AND ANO = CAST( EXTRACT (YEAR FROM :DATA) AS INTEGER)
             AND MES = CAST( EXTRACT (MONTH FROM :DATA) AS INTEGER)
             AND STATUS = '1';
          END

      END
END;
