SET TERM ^ ;
CREATE OR ALTER trigger ttbnfs_b10d for tbnfs
active before delete position 5
AS
BEGIN
   if (OLD.GC_WEB = 1) then
       begin

            IF(OLD.PDV_DEVOLUCAO_ID > 0)THEN
            BEGIN
               --EXECUTE PROCEDURE SPUI_DEVOLUCAO_TROCA_NF(OLD.PDV_DEVOLUCAO_ID,COALESCE((SELECT FIRST 1 B.VALOR_TOTAL_NF FROM TBNFS_TOTAIS B WHERE B.NFS_CONTROLE = OLD.CONTROLE),0),1);

               UPDATE tbpdv_devolucao_troca A SET A.nfe_devolucao = 0 WHERE ID = OLD.PDV_DEVOLUCAO_ID;
            END

            EXECUTE PROCEDURE SPD_NF(OLD.CONTROLE, 'NFS', OLD.ESTABELECIMENTO_CODIGO);
            /*APAGAR AGRUPAMENTOS DE FORMAS DE PAGAMENTO*/

            DELETE FROM TBNFS_FORMA_PAGAMENTO WHERE DOCUMENTO_ID = OLD.CONTROLE AND DOCUMENTO_TIPO = 'NFS';
       end 

END
^ SET TERM ; ^