SET TERM ^ ;

CREATE OR ALTER procedure SPD_NF (
    NF_ID integer,
    ORIGEM varchar(3),
    ESTABELECIMENTO integer)
as
BEGIN

IF (ORIGEM = 'NFE') THEN
   BEGIN
       DELETE FROM TBNFE_ITEM_SALDO     WHERE NFE_CONTROLE = :NF_ID;
       DELETE FROM TBNFE_ITEM           WHERE NFE_CONTROLE = :NF_ID;
       DELETE FROM TBNFE_PARCELAMENTO   WHERE NFE_CONTROLE = :NF_ID;
       DELETE FROM TBNFE_TRANSPORTADORA WHERE NFE_CONTROLE = :NF_ID;
       DELETE FROM TBNFE_OBSERVACAO     WHERE NFE_CONTROLE = :NF_ID;
       DELETE FROM TBNFE_TOTAIS         WHERE NFE_CONTROLE = :NF_ID;
       DELETE FROM TBNFE_REFERENCIADA   WHERE NFE_CONTROLE = :NF_ID;
       DELETE FROM TBNF_REFERENCIADA    WHERE ID = :NF_ID;
       DELETE FROM TBNF_LOTE
        WHERE ESTABELECIMENTO_CODIGO = :ESTABELECIMENTO
          AND NFE_CONTROLE = :NF_ID AND NFS_CONTROLE =  0;
       DELETE FROM TBNF_VINCULO_CONTABIL  
        WHERE ESTABELECIMENTO_ID = :ESTABELECIMENTO
          AND NFE_ID = :NF_ID AND NFS_ID =  0;
   END ELSE
   IF(ORIGEM = 'NFS') THEN
   BEGIN
       DELETE FROM TBNFS_ITEM_SALDO                 WHERE NFS_CONTROLE = :NF_ID;
       DELETE FROM TBNFS_ITEM                       WHERE NFS_CONTROLE = :NF_ID;
       DELETE FROM TBNFS_ITEM_TALAO_SALDO           WHERE NFS_ID = :NF_ID;
       DELETE FROM TBNFS_PARCELAMENTO               WHERE NFS_CONTROLE = :NF_ID;
       DELETE FROM TBNFS_TRANSPORTADORA             WHERE NFS_CONTROLE = :NF_ID;
       DELETE FROM TBNFS_OBSERVACAO                 WHERE NFS_CONTROLE = :NF_ID;
       DELETE FROM TBNFS_TOTAIS                     WHERE NFS_CONTROLE = :NF_ID;
       DELETE FROM TBNFS_REFERENCIADA               WHERE NFS_CONTROLE = :NF_ID;
       DELETE FROM TBNF_REFERENCIADA                WHERE ID = :NF_ID;
       DELETE FROM TBNF_LOTE
        WHERE ESTABELECIMENTO_CODIGO = :ESTABELECIMENTO
          AND NFS_CONTROLE = :NF_ID AND NFS_CONTROLE =  0;
       DELETE FROM TBNF_VINCULO_CONTABIL  
        WHERE ESTABELECIMENTO_ID = :ESTABELECIMENTO
          AND NFE_ID = 0 AND NFS_ID = :NF_ID;
   END

END
^

SET TERM ; ^


SET TERM ^ ;

CREATE OR ALTER trigger ttbnfs_item_b05d for tbnfs_item
active before delete position 5
AS
BEGIN

    DELETE FROM TBNFS_ITEM_SALDO WHERE NFS_ITEM_CONTROLE = OLD.CONTROLE;

    DELETE FROM TBNFS_ITEM_TALAO_SALDO WHERE NFS_ITEM_ID = OLD.CONTROLE;

    IF(OLD.GC_WEB = 1) THEN
    BEGIN
      IF(OLD.NF_NATUREZA = 2) THEN
      BEGIN
         -- DELETAR ESTOQUE PARA NATUREZA LANÇAMENTO
          EXECUTE PROCEDURE SP_NF_ITEM_ESTOQUE('NFS', OLD.CONTROLE, OLD.NFS_CONTROLE, 2);

          -- DESFAZER BAIXA DE PEDIDO
          EXECUTE PROCEDURE SPD_NFS_PEDIDO(OLD.NFS_CONTROLE, OLD.CONTROLE);

           -- DESFAZER BAIXA DE PRE PEDIDO
          EXECUTE PROCEDURE SPD_NFS_PRE_PEDIDO(OLD.NFS_CONTROLE, OLD.CONTROLE);
      END

      UPDATE TBLOTE_PECA_ITEM T SET T.NFS_ITEM_ID = NULL WHERE T.NFS_ITEM_ID = OLD.CONTROLE;

      IF(COALESCE(OLD.PDV_CONFERENCIA_ITEM_ID, 0) > 0) THEN
      BEGIN
          EXECUTE PROCEDURE SP_NFS_ITEM_PRE_PEDIDO(OLD.PDV_CONFERENCIA_ITEM_ID, 2);
      END
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbnf_lote_b05u for tbnf_lote
active before update position 5
AS

DECLARE VARIABLE GC_WEB INTEGER;

BEGIN
    IF(OLD.XML = '07' AND NEW.XML = '08') THEN
    BEGIN

       IF(NEW.NFS_CONTROLE > 0 ) THEN
       BEGIN
            SELECT T.GC_WEB FROM TBNFS T WHERE T.CONTROLE = NEW.NFS_CONTROLE INTO :GC_WEB;

            IF(:GC_WEB = 1) THEN
            BEGIN
                UPDATE TBNFS_ITEM T SET T.NF_SITUACAO = 3, T.SITUACAO = 3 WHERE T.NFS_CONTROLE = NEW.NFS_CONTROLE;

                UPDATE TBNFS_PARCELAMENTO T SET T.NF_SITUACAO = 3, T.SITUACAO = 3  WHERE T.NFS_CONTROLE = NEW.NFS_CONTROLE;

                DELETE FROM TBNFS_ITEM_TALAO_SALDO           WHERE NFS_ID = NEW.NFS_CONTROLE;
            END
       END ELSE
       IF(NEW.NFE_CONTROLE > 0) THEN
       BEGIN
            SELECT T.GC_WEB FROM TBNFE T WHERE T.CONTROLE = NEW.NFE_CONTROLE INTO :GC_WEB;

            IF(:GC_WEB = 1) THEN
            BEGIN
                UPDATE TBNFE_ITEM T SET T.NF_SITUACAO = 3, T.SITUACAO = 3  WHERE T.NFE_CONTROLE = NEW.NFE_CONTROLE;
    
                UPDATE TBNFE_PARCELAMENTO T SET T.NF_SITUACAO = 3, T.SITUACAO = 3  WHERE T.NFE_CONTROLE = NEW.NFE_CONTROLE;
            END
       END
    END

    if ((TRIM(OLD.XML) <> '10' OR TRIM(OLD.XML) = '10') AND TRIM(NEW.XML) = '10') THEN
    BEGIN


       IF(NEW.NFS_CONTROLE > 0 ) THEN
       BEGIN
            SELECT T.GC_WEB FROM TBNFS T WHERE T.CONTROLE = NEW.NFS_CONTROLE INTO :GC_WEB;

            IF(:GC_WEB = 1) THEN
            BEGIN
                UPDATE TBNFS T SET T.SITUACAO = '3' WHERE T.CONTROLE = NEW.NFS_CONTROLE;

                UPDATE TBNFS_ITEM T SET T.NF_SITUACAO = 3, T.SITUACAO = 3 WHERE T.NFS_CONTROLE = NEW.NFS_CONTROLE;

                UPDATE TBNFS_PARCELAMENTO T SET T.NF_SITUACAO = 3, T.SITUACAO = 3  WHERE T.NFS_CONTROLE = NEW.NFS_CONTROLE;

                EXECUTE PROCEDURE SPD_MOVCONTABIL(NEW.NFS_CONTROLE, 'NFS');

            END
       END ELSE

       IF(NEW.NFE_CONTROLE > 0) THEN
       BEGIN
            SELECT T.GC_WEB FROM TBNFE T WHERE T.CONTROLE = NEW.NFE_CONTROLE INTO :GC_WEB;

            IF(:GC_WEB = 1) THEN
            BEGIN
                 UPDATE TBNFE T SET T.SITUACAO = '3' WHERE T.CONTROLE = NEW.NFE_CONTROLE;

                 UPDATE TBNFE_ITEM T SET T.NF_SITUACAO = 3, T.SITUACAO = 3  WHERE T.NFE_CONTROLE = NEW.NFE_CONTROLE;
    
                 UPDATE TBNFE_PARCELAMENTO T SET T.NF_SITUACAO = 3, T.SITUACAO = 3  WHERE T.NFE_CONTROLE = NEW.NFE_CONTROLE;

                 EXECUTE PROCEDURE SPD_MOVCONTABIL(NEW.NFE_CONTROLE, 'NFE');
            END
       END
        


    END

END
^

SET TERM ; ^

