create or replace FUNCTION MHA_EXPORTA_NOTAFISCAL RETURN XML_ARQ PIPELINED
IS
   V_CLIDEPOS   VARCHAR2(6);
   V_N          VARCHAR2(2);
   V_CDFILIAL   VARCHAR2(4);
   V_NRLANCTONF VARCHAR2(10);
   V_F          VARCHAR2(2);
   V_1          VARCHAR2(2);
   /* NOTAFISCAL */
   V_EMPRESA         VARCHAR2(2);
   V_FILIAL          VARCHAR2(10);
   V_CODAGENTE       VARCHAR2(21);
   V_TIPODOCUMENTO   VARCHAR2(3);
   V_CODTRANSACAOERP VARCHAR2(14);
   V_NFENTRADA       VARCHAR2(30);
   V_NFSERIE         VARCHAR2(10);
   V_DTENTPREV       DATE;
   --V_DTENTPREV       VARCHAR2(20);
   V_VALORTOTALNF    FLOAT;
   /* ITEMPRODUTO */
   V_SEQITEM    INTEGER;
   V_CODPRODUTO VARCHAR2(20);
   V_QUANTIDADE FLOAT;
   V_CODUNIDADE VARCHAR2(10);
   V_VALORUNITARIO FLOAT;
   V_VALORTOTALITEM FLOAT;
   /* XML */
   L_XMLTYPE XMLTYPE;
   L_DOMDOC DBMS_XMLDOM.DOMDOCUMENT;
   L_ROOT_NODE DBMS_XMLDOM.DOMNODE;
   
   L_NOTAS_ELEMENT DBMS_XMLDOM.DOMELEMENT;
   L_NOTAS_NODE DBMS_XMLDOM.DOMNODE;
   
   L_NFINF_ELEMENT DBMS_XMLDOM.DOMELEMENT;
   L_NFINF_NODE DBMS_XMLDOM.DOMNODE;
   
   L_ITEMPROD_ELEMENT DBMS_XMLDOM.DOMELEMENT;
   L_ITEMPROD_NODE DBMS_XMLDOM.DOMNODE;
   
   L_ITENSNF_ELEMENT DBMS_XMLDOM.DOMELEMENT;
   L_ITENSNF_NODE DBMS_XMLDOM.DOMNODE;
   
   L_NAME_ELEMENT DBMS_XMLDOM.DOMELEMENT;
   L_NAME_NODE DBMS_XMLDOM.DOMNODE;
   L_NAME_TEXT DBMS_XMLDOM.DOMTEXT;
   L_NAME_TEXTNODE DBMS_XMLDOM.DOMNODE;
   
   OUT_XML_ARQ XML := XML(NULL);
   
   CURSOR C_NOTAFISCAL IS
      SELECT NF.CDFILIAL, NF.NRLANCTONF, NF.CDEMPRESA, PF.CDFILWMSMHA, NVL(NF.NRINSJURNF, REGEXP_REPLACE(NF.CDFORNECED, '\D')),
             DECODE(IDNFDEVOOL, 'S', 'DEV','F') AS TIPODOCUMENTO, NF.CDFILIAL||NF.NRLANCTONF, NF.NRNOTAFISC,
             NF.CDSERIEENTR, TO_DATE(NF.DTENTRSAID), NF.VRNOTAFISC, 'F' AS TIPAGENTE
        FROM NOTAFISCAL NF JOIN FILIAL F
                             ON NF.CDFILIRECE = F.CDFILIAL
                           JOIN PARAMFILIAL PF
                             ON PF.CDFILIAL = F.CDFILIAL
                           JOIN INTG_NOTAFISCAL I  
                             ON I.NRACESSONFE = NF.NRACESSONFE
       WHERE NF.IDENTRSAIDOP = 'E'
         AND F.IDFUNCAOFILI  = 'O'
         AND NF.IDSTATUS     IN ('I', 'L')
         AND NVL(NF.IDORIGDESTNF, 'F') = 'F'
         AND NVL(NF.IDSISTEMA, 'DFE') = 'DFE'
         AND PF.CDFILWMSMHA IS NOT NULL
         AND NF.USR_DTEXPXML IS NULL
         AND I.IDSTATUS = 'L'
         AND EXISTS(SELECT I.CDFILIAL
                      FROM ITEMPRODUTO I
                     WHERE I.CDFILIAL   = NF.CDFILIAL
                       AND I.NRLANCTONF = NF.NRLANCTONF)
      UNION ALL
      SELECT NF.CDFILIAL, NF.NRLANCTONF, NF.CDEMPRESA, PF.CDFILWMSMHA, CDFILIEMIT,
             'TRN' AS TIPODOCUMENTO, NF.CDFILIAL||NF.NRLANCTONF, NF.NRNOTAFISC,
             NF.CDSERIEENTR, TO_DATE(NF.DTENTRSAID), NF.VRNOTAFISC, 'A' AS TIPAGENTE
        FROM NOTAFISCAL NF JOIN FILIAL F
                             ON NF.CDFILIRECE = F.CDFILIAL
                           JOIN PARAMFILIAL PF
                             ON PF.CDFILIAL = F.CDFILIAL
       WHERE NF.IDENTRSAIDOP = 'E'
         AND F.IDFUNCAOFILI  = 'O'
         AND NF.IDSTATUS     IN ('I', 'L')
         AND NF.IDORIGDESTNF = 'L'
         AND NVL(NF.IDSISTEMA, 'DFE') = 'DFE'
         AND PF.CDFILWMSMHA IS NOT NULL
         AND NF.USR_DTEXPXML IS NULL
         AND EXISTS(SELECT I.CDFILIAL
                      FROM ITEMPRODUTO I
                     WHERE I.CDFILIAL   = NF.CDFILIAL
                       AND I.NRLANCTONF = NF.NRLANCTONF)
      UNION ALL
      SELECT NF.CDFILIAL, NF.NRLANCTONF, NF.CDEMPRESA, PF.CDFILWMSMHA, CDFILIEMIT,
             'TRN' AS TIPODOCUMENTO, NF.CDFILIAL||NF.NRLANCTONF, NF.NRNOTAFISC,
             NF.CDSERIEENTR, TO_DATE(NF.DTENTRSAID), NF.VRNOTAFISC, 'A' AS TIPAGENTE
        FROM NOTAFISCAL NF JOIN FILIAL F
                             ON NF.CDFILIRECE = F.CDFILIAL
                           JOIN PARAMFILIAL PF
                             ON PF.CDFILIAL = F.CDFILIAL
                           JOIN FILIAL FE --FILIAL DE EMISSAO
                             ON FE.CDFILIAL = NF.CDFILIEMIT
       WHERE NF.IDENTRSAIDOP = 'E'
         AND F.IDFUNCAOFILI  = 'O'
         AND NF.IDSTATUS     = 'P'
         AND NF.IDORIGDESTNF = 'L'
         AND FE.IDFUNCAOFILI = 'O'
         AND NVL(NF.IDSISTEMA, 'DFE') = 'DFE'
         AND PF.CDFILWMSMHA IS NOT NULL
         AND NF.USR_DTEXPXML IS NULL
         AND EXISTS(SELECT I.CDFILIAL
                      FROM ITEMPRODUTO I
                     WHERE I.CDFILIAL   = NF.CDFILIAL
                       AND I.NRLANCTONF = NF.NRLANCTONF);

   CURSOR C_ITEMPRODUTO(C_CDFILIAL IN VARCHAR2, C_NRLANCTONF IN VARCHAR2) IS
      SELECT TO_NUMBER(I.NRSEQITPROD), P.CDARVPROD, ROUND(I.QTITEMPROD, 7), P.SGUNIDADE, ROUND(I.VRUNITITEM, 7), ROUND(I.VRTOTAITEM, 7)
        FROM ITEMPRODUTO I, PRODUTO P
       WHERE I.CDPRODUTO  = P.CDPRODUTO
         AND I.CDFILIAL   = C_CDFILIAL
         AND I.NRLANCTONF = C_NRLANCTONF
       ORDER BY I.NRSEQITPROD;
BEGIN
   V_CLIDEPOS := '006';
   V_N        := 'N';
   V_1        := '1';
   
   OPEN C_NOTAFISCAL;
   LOOP
      FETCH C_NOTAFISCAL INTO V_CDFILIAL, V_NRLANCTONF, V_EMPRESA, V_FILIAL, V_CODAGENTE, 
                              V_TIPODOCUMENTO, V_CODTRANSACAOERP, V_NFENTRADA, 
                              V_NFSERIE, V_DTENTPREV, V_VALORTOTALNF, V_F;
      EXIT WHEN C_NOTAFISCAL%NOTFOUND;
      
      L_DOMDOC    := DBMS_XMLDOM.NEWDOMDOCUMENT;
      L_ROOT_NODE := DBMS_XMLDOM.MAKENODE(L_DOMDOC);
      
      L_NFINF_ELEMENT := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'Recebimento');
      DBMS_XMLDOM.SETATTRIBUTE(L_NFINF_ELEMENT, 'CODIGO', V_CODTRANSACAOERP);
      L_NFINF_NODE    := DBMS_XMLDOM.APPENDCHILD(L_ROOT_NODE, DBMS_XMLDOM.MAKENODE(L_NFINF_ELEMENT));
      
      /* VALORES FIXOS */
      L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'TIPOGERADOR');
      L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_NFINF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
      L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, 'ERP');
      L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
      
      L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'TIPOINTERFACE');
      L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_NFINF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
      L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, 'INC');
      L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
      /* *** */
      
      L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'EMPRESA');
      L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_NFINF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
      L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_EMPRESA);
      L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
      
      L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'FILIAL');
      L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_NFINF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
      L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_FILIAL);
      L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
      
      /* VALORES FIXOS */
      L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'CLIENTEDEPOSITANTE');
      L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_NFINF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
      L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_CLIDEPOS);
      L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));

      L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'CODDEPOSITO');
      L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_NFINF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
      L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_1);
      L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
      /* *** */
      
      L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'CODAGENTE');
      L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_NFINF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
      L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_CODAGENTE);
      L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
      
      /* VALOR FIXO */
      L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'TIPAGENTE');
      L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_NFINF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
      L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_F);
      L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
      /* *** */
      
      L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'TIPDOCUMENTO');
      L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_NFINF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
      L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_TIPODOCUMENTO);
      L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
      
      L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'CODTRANSACAOERP');
      L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_NFINF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
      L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_CODTRANSACAOERP);
      L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
      
      L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'CODPEDIDOCOMPRA');
      L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_NFINF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
      L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_CODTRANSACAOERP);
      L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
      
      L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'NFENTRADA');
      L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_NFINF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
      L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_NFENTRADA);
      L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
      
      L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'NFSERIE');
      L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_NFINF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
      L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_NFSERIE);
      L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
      
      L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'DATENTREGAPREVISTA');
      L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_NFINF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
      L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, TO_CHAR(V_DTENTPREV, 'DD/MM/YYYY'));
      L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
      
      L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'ENVIOARMAZENAGEM');
      L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_NFINF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
      L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_N);
      L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
      
      L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'VALORTOTALNF');
      L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_NFINF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
      L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_VALORTOTALNF);
      L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
      
      L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'DATAPROCESSAMENTOERP');
      L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_NFINF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
      L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, TO_CHAR(V_DTENTPREV, 'DD/MM/YYYY'));
      L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
      
      L_ITEMPROD_ELEMENT := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'itensNF');
      L_ITEMPROD_NODE    := DBMS_XMLDOM.APPENDCHILD(L_NFINF_NODE, DBMS_XMLDOM.MAKENODE(L_ITEMPROD_ELEMENT));
      
      OPEN C_ITEMPRODUTO(V_CDFILIAL, V_NRLANCTONF);
      LOOP
         FETCH C_ITEMPRODUTO INTO V_SEQITEM, V_CODPRODUTO, V_QUANTIDADE, V_CODUNIDADE, V_VALORUNITARIO, V_VALORTOTALITEM;
         EXIT WHEN C_ITEMPRODUTO%NOTFOUND;
         
         L_ITENSNF_ELEMENT := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'NotaFiscalItem');
         L_ITENSNF_NODE    := DBMS_XMLDOM.APPENDCHILD(L_ITEMPROD_NODE, DBMS_XMLDOM.MAKENODE(L_ITENSNF_ELEMENT));
         
         L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'SEQITEM');
         L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_ITENSNF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
         L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_SEQITEM);
         L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
         
         L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'CODPRODUTO');
         L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_ITENSNF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
         L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_CODPRODUTO);
         L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
         
         L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'QUANTIDADE');
         L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_ITENSNF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
         L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_QUANTIDADE);
         L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
         
         L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'CODUNIDADE');
         L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_ITENSNF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
         L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_CODUNIDADE);
         L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
         
         L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'VALORUNITARIO');
         L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_ITENSNF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
         L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_VALORUNITARIO);
         L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
         
         L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'VALORTOTALITEM');
         L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_ITENSNF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
         L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, V_VALORTOTALITEM);
         L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
         
         L_NAME_ELEMENT  := DBMS_XMLDOM.CREATEELEMENT(L_DOMDOC, 'QTDCAIXA');
         L_NAME_NODE     := DBMS_XMLDOM.APPENDCHILD(L_ITENSNF_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_ELEMENT));
         L_NAME_TEXT     := DBMS_XMLDOM.CREATETEXTNODE(L_DOMDOC, 0);
         L_NAME_TEXTNODE := DBMS_XMLDOM.APPENDCHILD(L_NAME_NODE,DBMS_XMLDOM.MAKENODE(L_NAME_TEXT));
      END LOOP;
      CLOSE C_ITEMPRODUTO;

      L_XMLTYPE := DBMS_XMLDOM.GETXMLTYPE(L_DOMDOC);
      DBMS_XMLDOM.FREEDOCUMENT(L_DOMDOC);
      OUT_XML_ARQ.XMLRETORNO := L_XMLTYPE;
      
      PIPE ROW(OUT_XML_ARQ);
   END LOOP;
   CLOSE C_NOTAFISCAL;
   
   RETURN;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('MHA_EXPORTA_NOTAFISCAL','5.87.XXX'); END;
/