SET TERM ^ ;
ALTER TABLE TBNFS
ADD REPROCESSAR_PARCELAS SMALLINT
DEFAULT 0^

CREATE OR ALTER TRIGGER TTBNFS_PARCELAMENTO_A01D FOR TBNFS_PARCELAMENTO
ACTIVE AFTER DELETE POSITION 9
AS
DECLARE VARIABLE PDV_CFE_ID INTEGER;
DECLARE VARIABLE NFS_CONTROLE INTEGER;
BEGIN
  IF(OLD.AVULSO = '1')THEN
  BEGIN
     DELETE FROM TBCOMISSAO WHERE TABELA_ID = OLD.CONTROLE AND ORIGEM = 'AVU';
  END

  /*VERIFICAR SE PARCELAS ESTAO SENDO REPROCESSADAS PELA CONCILIACAO DE CARTOES*/

  :PDV_CFE_ID = COALESCE((SELECT FIRST 1 CFE_PAG.PDV_CFE_ID FROM TBPDV_CFE_FORMA_PAGAMENTO CFE_PAG, TBPDV_CFE CFE
                    WHERE CFE.ID = CFE_PAG.PDV_CFE_ID AND CFE_PAG.ID = OLD.PDV_CFE_FORMA_PAGAMENTO_ID AND COALESCE(CFE.REPROCESSAR_PARCELAS,0) = 0),0);

  :NFS_CONTROLE = COALESCE((SELECT FIRST 1 NFS.CONTROLE FROM TBNFS NFS
                    WHERE NFS.CONTROLE = OLD.NFS_CONTROLE
                    AND COALESCE(NFS.REPROCESSAR_PARCELAS,0) = 0
                    AND OLD.NFS_CONTROLE > 0),0);

  IF(:PDV_CFE_ID > 0)THEN
  BEGIN
    EXECUTE PROCEDURE SPI_CFE_GERAR_COMISSAO(:PDV_CFE_ID);
    EXECUTE PROCEDURE SPI_GERAR_COMISSAO(:PDV_CFE_ID,'CFE');
  END

  IF(:NFS_CONTROLE > 0)THEN
  BEGIN
     EXECUTE PROCEDURE SPI_GERAR_COMISSAO(:NFS_CONTROLE,'NFS');
  END
END^

CREATE OR ALTER TRIGGER TTBNFS_PARCELAMENTO_A01I FOR TBNFS_PARCELAMENTO
ACTIVE AFTER INSERT POSITION 9
AS
DECLARE VARIABLE PDV_CFE_ID INTEGER;
DECLARE VARIABLE NFS_CONTROLE INTEGER;
BEGIN
  :PDV_CFE_ID = COALESCE((SELECT FIRST 1 CFE_PAG.PDV_CFE_ID FROM TBPDV_CFE_FORMA_PAGAMENTO CFE_PAG, TBPDV_CFE CFE
                    WHERE CFE_PAG.PDV_CFE_ID = CFE.ID AND CFE_PAG.ID = NEW.PDV_CFE_FORMA_PAGAMENTO_ID AND COALESCE(CFE.REPROCESSAR_PARCELAS,0) = 0),0);

  :NFS_CONTROLE = COALESCE((SELECT FIRST 1 NFS.CONTROLE FROM TBNFS NFS
                    WHERE NFS.CONTROLE = NEW.NFS_CONTROLE
                    AND COALESCE(NFS.REPROCESSAR_PARCELAS,0) = 0
                    AND NEW.NFS_CONTROLE > 0),0);

  IF(:PDV_CFE_ID > 0)THEN
  BEGIN
     EXECUTE PROCEDURE SPI_CFE_GERAR_COMISSAO(:PDV_CFE_ID);

     EXECUTE PROCEDURE SPI_GERAR_COMISSAO(:PDV_CFE_ID,'CFE');
  END

  IF(NEW.AVULSO = '1') THEN
  BEGIN
     EXECUTE PROCEDURE SPI_GERAR_COMISSAO(NEW.CONTROLE,'AVU');
  END

  IF(:NFS_CONTROLE > 0)THEN
  BEGIN
     EXECUTE PROCEDURE SPI_GERAR_COMISSAO(:NFS_CONTROLE,'NFS');
  END

END^

CREATE OR ALTER TRIGGER TTBNFS_PARCELAMENTO_A01U FOR TBNFS_PARCELAMENTO
ACTIVE AFTER UPDATE POSITION 1
AS
DECLARE VARIABLE PDV_CFE_ID INTEGER;
BEGIN
  IF(( (NEW.VALOR_SALDO                <> OLD.VALOR_SALDO)
    OR (NEW.PAGAMENTO_FORMA            <> OLD.PAGAMENTO_FORMA))
    AND COALESCE(NEW.COLABORADOR_ID, 0) > 0) THEN
  BEGIN
       EXECUTE PROCEDURE SPU_SALDO_COLABORADOR(NEW.COLABORADOR_ID);
  END

  IF(NEW.PAGAMENTO_FORMA <> OLD.PAGAMENTO_FORMA
  OR NEW.VALOR           <> OLD.VALOR
  OR NEW.REPRESENTANTE_COMISSAO <> OLD.REPRESENTANTE_COMISSAO) THEN
  BEGIN
     :PDV_CFE_ID = COALESCE((SELECT FIRST 1 CFE_PAG.PDV_CFE_ID FROM TBPDV_CFE_FORMA_PAGAMENTO CFE_PAG WHERE CFE_PAG.ID = NEW.PDV_CFE_FORMA_PAGAMENTO_ID),0);

     IF(:PDV_CFE_ID > 0)THEN
     BEGIN
        EXECUTE PROCEDURE SPI_CFE_GERAR_COMISSAO(:PDV_CFE_ID);

        EXECUTE PROCEDURE SPI_GERAR_COMISSAO(:PDV_CFE_ID,'CFE');
     END

     IF(NEW.AVULSO = '1')THEN
     BEGIN
       EXECUTE PROCEDURE SPI_GERAR_COMISSAO(NEW.CONTROLE,'AVU');
     END
     ELSE
     BEGIN
       EXECUTE PROCEDURE SPI_GERAR_COMISSAO(NEW.NFS_CONTROLE,'NFS');
     END
  END
END^

CREATE OR ALTER TRIGGER TTBNFS_PARCELAMENTO_A01U FOR TBNFS_PARCELAMENTO
ACTIVE AFTER UPDATE POSITION 1
AS
DECLARE VARIABLE PDV_CFE_ID INTEGER;
BEGIN
  IF(( (NEW.VALOR_SALDO                <> OLD.VALOR_SALDO)
    OR (NEW.PAGAMENTO_FORMA            <> OLD.PAGAMENTO_FORMA))
    AND COALESCE(NEW.COLABORADOR_ID, 0) > 0) THEN
  BEGIN
       EXECUTE PROCEDURE SPU_SALDO_COLABORADOR(NEW.COLABORADOR_ID);
  END

  IF(NEW.PAGAMENTO_FORMA        <> OLD.PAGAMENTO_FORMA
  OR NEW.VALOR                  <> OLD.VALOR
  OR NEW.REPRESENTANTE_COMISSAO <> OLD.REPRESENTANTE_COMISSAO
  OR NEW.REPRESENTANTE_ID       <> OLD.REPRESENTANTE_ID ) THEN
  BEGIN
     :PDV_CFE_ID = COALESCE((SELECT FIRST 1 CFE_PAG.PDV_CFE_ID FROM TBPDV_CFE_FORMA_PAGAMENTO CFE_PAG WHERE CFE_PAG.ID = NEW.PDV_CFE_FORMA_PAGAMENTO_ID),0);

     IF(:PDV_CFE_ID > 0)THEN
     BEGIN
        EXECUTE PROCEDURE SPI_CFE_GERAR_COMISSAO(:PDV_CFE_ID);

        EXECUTE PROCEDURE SPI_GERAR_COMISSAO(:PDV_CFE_ID,'CFE');
     END
  END

  IF(NEW.VALOR                  <> OLD.VALOR
  OR NEW.REPRESENTANTE_COMISSAO <> OLD.REPRESENTANTE_COMISSAO
  OR NEW.REPRESENTANTE_ID       <> OLD.REPRESENTANTE_ID)THEN
  BEGIN
     IF(NEW.AVULSO = '1')THEN
     BEGIN
       EXECUTE PROCEDURE SPI_GERAR_COMISSAO(NEW.CONTROLE,'AVU');
     END
     ELSE
     BEGIN
       EXECUTE PROCEDURE SPI_GERAR_COMISSAO(NEW.NFS_CONTROLE,'NFS');
     END
  END
END^

^ SET TERM ; ^