SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBNFE_B02D FOR TBNFE
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE CONTROLE INTEGER;
DECLARE VARIABLE DEVOLUCAO_ID INTEGER;
BEGIN

  CONTROLE = COALESCE((SELECT FIRST 1 A.CONTROLE
                FROM TBNFE_PARCELAMENTO A
                WHERE A.VALOR <> A.VALOR_SALDO
                AND A.NFE_CONTROLE = OLD.CONTROLE),0);
  IF(:CONTROLE > 0)THEN
  BEGIN
     EXCEPTION "..."' HÁ PARCELAS COM LANÇAMENTOS VINCULADOS NESTE DOCUMENTO, OPERAÇÃO CANCELADA.';
  END

  IF(OLD.PDV_DEVOLUCAO_ID > 0 AND OLD.NATUREZA = 2)THEN
  BEGIN
     :DEVOLUCAO_ID = COALESCE((SELECT FIRST 1 DEV.ID FROM TBPDV_DEVOLUCAO_TROCA DEV WHERE DEV.ID = OLD.PDV_DEVOLUCAO_ID AND DEV.TIPO_ED = '1'),0);
     IF(:DEVOLUCAO_ID > 0)THEN
     BEGIN

        UPDATE TBPDV_DEVOLUCAO_TROCA DEV
            SET DEV.VALOR_SALDO = CAST(CAST(DEV.VALOR_SALDO AS DOUBLE PRECISION) + CAST(COALESCE((SELECT SUM(NF_ITEM.QUANTIDADE*NF_ITEM.VALOR_UNITARIO) FROM TBNFE_ITEM NF_ITEM WHERE NF_ITEM.NFE_CONTROLE = OLD.CONTROLE),0.00000) AS DOUBLE PRECISION) AS NUMERIC(15,2))
          WHERE DEV.ID = :DEVOLUCAO_ID;

     END
  END
END^

CREATE OR ALTER TRIGGER TBNFE_B10U FOR TBNFE
ACTIVE BEFORE UPDATE POSITION 10
AS
BEGIN
  IF(NEW.SITUACAO <> OLD.SITUACAO) THEN
  BEGIN
     /*SE HOUVER CANCELAMENTO DA NOTA FISCAL A NOTA FISCAL DEVE SER DESVINCULADA DAS DEVOLUCOES/TROCAS*/
     IF(COALESCE(NEW.SITUACAO, 0) = 3 AND COALESCE(NEW.PDV_DEVOLUCAO_ID,0) > 0)THEN
     BEGIN
       IF(OLD.NATUREZA = 1)THEN
       BEGIN
          UPDATE TBPDV_DEVOLUCAO_TROCA A SET A.NFE_DEVOLUCAO = 0 WHERE A.ID = NEW.PDV_DEVOLUCAO_ID;
       END
       IF(OLD.NATUREZA = 2)THEN
       BEGIN
          UPDATE TBPDV_DEVOLUCAO_TROCA A SET A.ROMANEIO_DEVOLUCAO = 0 WHERE A.ID = NEW.PDV_DEVOLUCAO_ID;
       END
       NEW.PDV_DEVOLUCAO_ID = 0;
     END

     /*SE CONCLUIR A NOTA FISCAL DE ENTRADA E A NATUREZA FOR EMISSAO, BAIXAR O SALDO DA DEVOLUCAO*/
     IF(COALESCE(NEW.SITUACAO, 0) = 2 AND COALESCE(NEW.PDV_DEVOLUCAO_ID,0) > 0 AND OLD.NATUREZA = 1)THEN
     BEGIN
        UPDATE TBPDV_DEVOLUCAO_TROCA DEV
            SET DEV.VALOR_SALDO = CAST(CAST(DEV.VALOR_SALDO AS DOUBLE PRECISION) - CAST(COALESCE((SELECT SUM(NF_ITEM.QUANTIDADE*NF_ITEM.VALOR_UNITARIO) FROM TBNFE_ITEM NF_ITEM WHERE NF_ITEM.NFE_CONTROLE = OLD.CONTROLE),0.00000) AS DOUBLE PRECISION) AS NUMERIC(15,2))
          WHERE DEV.ID          = NEW.PDV_DEVOLUCAO_ID
            AND DEV.TIPO_ED     = '1';
     END  
     /*SE CANCELAR A CONCLUSAO DA NOTA FISCAL DE ENTRADA E A NATUREZA FOR EMISSAO, RESTAURAR O SALDO DA DEVOLUCAO*/
     IF(COALESCE(NEW.SITUACAO, 0) = 1 AND COALESCE(OLD.SITUACAO, 0) = 2 AND COALESCE(NEW.PDV_DEVOLUCAO_ID,0) > 0 AND OLD.NATUREZA = 1)THEN
     BEGIN
        UPDATE TBPDV_DEVOLUCAO_TROCA DEV
            SET DEV.VALOR_SALDO = CAST(CAST(DEV.VALOR_SALDO AS DOUBLE PRECISION) + CAST(COALESCE((SELECT SUM(NF_ITEM.QUANTIDADE*NF_ITEM.VALOR_UNITARIO) FROM TBNFE_ITEM NF_ITEM WHERE NF_ITEM.NFE_CONTROLE = OLD.CONTROLE),0.00000) AS DOUBLE PRECISION) AS NUMERIC(15,2))
          WHERE DEV.ID          = NEW.PDV_DEVOLUCAO_ID
            AND DEV.TIPO_ED     = '1';
     END
  END
END^

SET TERM ; ^