create or replace PROCEDURE FCT_SUB_MNE_DOC(
    P_NRSEQCONTR    IN INTEGER,
    P_NRSEQDOCCONT  IN VARCHAR2,
    P_NRORG         IN INTEGER,
    P_IDTIPODOC     IN VARCHAR2,
    P_SUBMNEDOC     IN VARCHAR2 DEFAULT 'S')
  AS
  V_TXDOCUMENT  CLOB;        /* Texto do documento */
  V_PARCCONTR   NUMBER;      /* Parceiro do Contrato*/
  V_FILIAL      VARCHAR2(4); /* Filial do Contrato*/
  V_INTERLCON   NUMBER;      /* C¢d. Interlocutor do Contrato*/
  V_MODPAGORC   NUMBER;      /* C¢d. Modalidade de Pagamento do Contrato*/
  V_RECURS      NUMBER;      /* Verifica exist‚ncia de recursos no item*/
  V_RESULT      CHAR;
  V_RESULTREC   CHAR;
  V_TSDADOS     NUMBER;
  V_NRSEQCONTR  NUMBER;
  V_NRSEQDOCCONT  VARCHAR2(400);
  V_NRORG         NUMBER;


  /* ---- Cria‡Æo da linha dos itens ---- */
  V_TESTA1      NUMBER;
  V_TESTA2      NUMBER;
  V_POSINI      NUMBER;
  V_POSFIN      NUMBER;
  V_CLOBLINHA   CLOB;
  V_CLOBLINHA1  CLOB;
  V_CLOBFINAL   CLOB;
  V_CLOBINICIAL CLOB;
  V_INIPER      NUMBER;
  V_FINPER      NUMBER;
  V_IND         NUMBER;
  
  /* INICIO ADICIONAIS */
  
  V_EXISTE_ADICIONAL NUMBER;
  V_TESTA1AD      NUMBER;
  V_TESTA2AD      NUMBER;
  V_RESULTAD      CHAR;
  V_POSINIAD      NUMBER;
  V_POSFINAD      NUMBER;
  V_CLOBFINALAD   CLOB;
  V_CLOBINICIALAD CLOB;
  V_CLOBLINHAAD   CLOB;
  V_TSDADOSAD     NUMBER;
  V_CLOBLINHA1AD  CLOB;
  V_CLOBLINHAAUXAD CLOB;
  V_TOTITEMAD FLOAT;
  V_TOTITEMAD2  VARCHAR2(20);
  V_TOTGERAD FLOAT;
  V_TOTGERAD2 VARCHAR2(20);
  
  /* MNEMÔNICOS ADICIONAIS
    [INDAD] ----> Índice
    [DESCAD] ---> Descrição
    [QNTAD]  ---> Quantidade
    [VRUNITAD] -> Valor Unitário 
    [MEDIDAAD] -> Medida
    [QTMEDAD] -> Quantidade de Medida
    [VRTOTAD] --> Valor Total
    [TOTITEMAD] -> Valor Total de todos os adicionais do ITEM
    [TOTGERAD] -> Valor Total de todos os adicionais da PROPOSTA
  */
  
  /* FIM ADICIONAIS */

  /* ---- Cria‡Æo da linha dos recursos ---- */
  V_TESTARC1    NUMBER;
  V_TESTARC2    NUMBER;
  V_POSINIREC   NUMBER;
  V_POSFINREC   NUMBER;
  V_CLOBLINREC  CLOB;
  V_CLOBLINREC1 CLOB;
  V_CLOBFINREC  CLOB;
  V_CLOBINIREC  CLOB;

  /* ---- Mnem“nicos - Dados do Comprador ---- */
  V_CPARC     VARCHAR2(20);   /* [CPARC] -> Inscri‡Æo do Comprador (CNPJ OU CPF)*/
  V_CNMPARC   VARCHAR2(100);  /* [CNMPARC] -> Nome do Comprador*/
  V_CIE       VARCHAR2(20);   /* [CIE] -> Inscri‡Æo Estadual Comprador*/
  V_CENDE     VARCHAR2(400);  /* [CENDE] -> Endere‡o principal do Comprador (COMPLETO)*/
  V_CDSEND    VARCHAR2(100);  /* [CDSEND] -> Descri‡Æo do Endere‡o do Comprador (ex: Rua X, 112) */
  V_CMUNIC    VARCHAR2(30);   /* [CMUNIC]  -> Munic¡pio do Comprador*/
  V_CBAIRRO   VARCHAR2(100);  /* [CBAIRRO] -> Bairro do Comprador*/
  V_CESTADO   VARCHAR2(2);    /* [CESTADO] -> Estado do Comprador*/
  V_CCEP      VARCHAR2(15);   /* [CCEP] -> CEP do Comprador*/
  /* -- Endere‡o de Entrega --*/
  V_CENDE2     VARCHAR2(400);  /* [CENDE2] -> Endere‡o DE ENTREGA do OR€AMENBTO (COMPLETO)*/
  V_CDSEND2    VARCHAR2(100);  /* [CDSEND2] -> Descri‡Æo do Endere‡o do Comprador (ex: Rua X, 112) */
  V_CMUNIC2    VARCHAR2(30);   /* [CMUNIC2]  -> Munic¡pio do Comprador*/
  V_CBAIRRO2   VARCHAR2(100);  /* [CBAIRRO2] -> Bairro do Comprador*/
  V_CESTADO2   VARCHAR2(2);    /* [CESTADO2] -> Estado do Comprador*/
  V_CCEP2      VARCHAR2(15);   /* [CCEP2] -> CEP do Comprador*/

  /* ---- Mnem“nicos - Dados do Prestador ---- */
  V_PPARC     VARCHAR2(20);   /* [PPARC] -> Inscri‡Æo do Prestador (CNPJ OU CPF)*/
  V_PNMPARC   VARCHAR2(100);  /* [PNMPARC] -> Nome do Prestador*/
  V_PIE       VARCHAR2(20);   /* [PIE] -> Inscri‡Æo Estadual Prestador*/
  V_PENDE     VARCHAR2(400);  /* [PENDE] -> Endere‡o principal do Prestador (COMPLETO)*/
  V_PDSEND    VARCHAR2(100);  /* [PDSEND] -> Descri‡Æo do Endere‡o do Comprador (ex: Rua X, 112) */
  V_PMUNIC    VARCHAR2(30);   /* [PMUNIC]  -> Munic¡pio do Comprador*/
  V_PBAIRRO   VARCHAR2(100);  /* [PBAIRRO] -> Bairro do Comprador*/
  V_PESTADO   VARCHAR2(2);    /* [PESTADO] -> Estado do Comprador*/
  V_PCEP      VARCHAR2(15);   /* [PCEP] -> CEP do Comprador*/

  /* ---- Mnem“nicos - Dados do Servi‡o/Produto ---- */
  /*[IND] -> Öndice do Item*/
  V_COD       NUMBER;         /* [COD] -> C¢digo do Item */
  V_QNT       INTEGER;        /* [QNT] -> Quantidade do Item*/
  V_DESC      VARCHAR2(400);  /* [DESC] -> Descri‡Æo do Item*/
  V_QTMED     INTEGER;        /* [QTMED] -> Quantidade da Medida do Item*/
  V_MEDIDA    VARCHAR2(400);  /* [MEDIDA] -> Medida do Item*/
  V_TPSERV    VARCHAR2(30);   /* [TPSERV] -> Unidade. Tempo do Servi‡o*/
  V_QTTPSERV  INTEGER;        /* [QTTPSERV] -> Quantidade Tempo do Servi‡o*/
  V_VRUNIT    FLOAT;          /* [VRUNIT] -> Valor Unit rio*/
  V_VRTOT     FLOAT;          /* [VRTOT] -> Valor Total*/
  V_TOTPER    FLOAT;          /* [TOTPER] -> Valor Total do Per¡odo*/
  V_TOTITEM   FLOAT;          /* [TOTITEM] -> Valor Total de TODOS OS ITENS*/
  V_TOTPERIT  FLOAT;          /* [TOTPERIT] -> Valor Total do Per¡odo para TODOS OS ITENS*/
  V_MODPAG    VARCHAR2(100);  /* [MODPAG] -> Modalidade de Pagamento do Item*/

  /*== Apura‡Æo de Faturamento ==*/
  /*[AIND] -> Öndice do Item*/
  V_ACOD      NUMBER;         /* [ACOD] -> C¢digo do Item */
  V_AQNT      INTEGER;        /* [AQNT] -> Quantidade do Item*/
  V_ADESC     VARCHAR2(400);  /* [ADESC] -> Descri‡Æo do Item*/
  V_AQTMED    INTEGER;        /* [AQTMED] -> Quantidade da Medida do Item*/
  V_AMEDIDA   VARCHAR2(400);  /* [AMEDIDA] -> Medida do Item*/
  V_ATPSERV   VARCHAR2(30);   /* [ATPSERV] -> Unidade. Tempo do Servi‡o*/
  V_AQTTPSERV INTEGER;        /* [AQTTPSERV] -> Quantidade Tempo do Servi‡o*/
  V_AVRUNIT   FLOAT;          /* [AVRUNIT] -> Valor Unit rio*/
  V_AVRTOT    FLOAT;          /* [AVRTOT] -> Valor Total*/
  V_ATOTPER   FLOAT;          /* [ATOTPER] -> Valor Total do Per¡odo*/
  V_TOTITEM_A  FLOAT;         /* [ATOTITEM] -> Valor Total de TODOS OS ITENS*/
  V_TOTPERIT_A FLOAT;         /* [ATOTPERIT] -> Valor Total do Per¡odo para TODOS OS ITENS*/
  V_AMODPAG   VARCHAR2(100);  /* [AMODPAG] -> Modalidade de Pagamento do Item*/

  /*== Modalidade de Pagamento == */
  /*[OIND] -> Öndice do Item*/
  V_OCOD      NUMBER;         /* [OCOD] -> C¢digo do Item */
  V_OQNT      INTEGER;        /* [OQNT] -> Quantidade do Item*/
  V_ODESC     VARCHAR2(400);  /* [ODESC] -> Descri‡Æo do Item*/
  V_OQTMED    INTEGER;        /* [OQTMED] -> Quantidade da Medida do Item*/
  V_OMEDIDA   VARCHAR2(400);  /* [OMEDIDA] -> Medida do Item*/
  V_OTPSERV   VARCHAR2(30);   /* [OTPSERV] -> Unidade. Tempo do Servi‡o*/
  V_OQTTPSERV INTEGER;        /* [OQTTPSERV] -> Quantidade Tempo do Servi‡o*/
  V_OVRUNIT   FLOAT;          /* [OVRUNIT] -> Valor Unit rio*/
  V_OVRTOT    FLOAT;          /* [OVRTOT] -> Valor Total*/
  V_OTOTPER   FLOAT;          /* [OTOTPER] -> Valor Total do Per¡odo*/
  V_TOTITEM_O  FLOAT;         /* [OTOTITEM] -> Valor Total de TODOS OS ITENS*/
  V_TOTPERIT_O FLOAT;         /* [OTOTPERIT] -> Valor Total do Per¡odo para TODOS OS ITENS*/
  V_OMODPAG   VARCHAR2(100);  /* [OMODPAG] -> Modalidade de Pagamento do Item*/

  /* ---- Mnem“nicos - Dados do Contrato ---- */
  V_CDCONT    VARCHAR2(50);   /* [CDCONT] - C¢digo do Contrato */
  V_DTINI     DATE;           /* [DTINI] - Data Inicial do Contrato*/
  V_DTFIN     DATE;           /* [DTFIN] - Data Final do Contrato*/
  V_PRAZO     INTEGER;        /* [PRAZO] - Prazo do Contrato*/
  V_INDFIN    VARCHAR2(30);   /* [INDFIN] - Öndice Financeiro do Contrato*/
  V_DTREA     DATE;           /* [DTREA] - Data Reajuste do Contrato*/
  V_DTVENPARC DATE;           /* [DTVENPARC] - Data de Vencimento das Parcelas*/
  V_QTPOST    INTEGER;        /* [QTPOST] - Quantidade de Postos de Trabalho*/
  V_DTPRORAUT DATE;           /* [DTPRORAUT] - Data Prorroga‡Æo Autom tica */
  V_INTCON    VARCHAR2(250);  /* [INTCON] - Interlocutor do Contrato*/
  V_MOPGCON   VARCHAR2(100);  /* [MOPGCON] - Modalidade de Pagamento do Contrato*/
  /*[DTHOJE] - DATA ATUAL  (DD/MM/AAAA)*/
  /*[DIAAT]  - DIA ATUAL */
  /*[MESAT]  - MÒS ATUAL */
  V_NMMESAT   VARCHAR2(30); /*[NMMESAT] - MÒS ATUAL (NOME)*/
  /*[ANOAT]  - ANO ATUAL */

  /* ---- Mnem“nicos - Recursos do Contrato ---- */

          /*-- POR ITEM DO CONTRATO --*/
  V_CDRECI     NUMBER;         /* [CDRECI] - C¢digo do Recurso do Contrato */
  V_NMRECI     VARCHAR2(250);  /* [NMRECI] - Nome do Recurso do Contrato */
  V_VRPRRECI   FLOAT;          /* [VRPRREC] - Valor Percentual do Recurso do Contrato */
  V_VRRECI     FLOAT;          /* [VRREC] - Valor do Recurso do Contrato */
  V_PATRECI    VARCHAR2(30);   /* [PATRECI] - C¢digo de Patrim“nio do Recurso (Ativo) */
  V_SERECI     VARCHAR2(5);    /* [SERECI] - S‚rie do recusto (Ativo) */

  /*  Itens de Apura‡Æo de Faturamento*/

  V_ACDRECI     NUMBER;         /* [ACDRECI] - C¢digo do Recurso do Contrato */
  V_ANMRECI     VARCHAR2(250);  /* [ANMRECI] - Nome do Recurso do Contrato */
  V_AVRPRRECI   FLOAT;          /* [AVRPRREC] - Valor Percentual do Recurso do Contrato */
  V_AVRRECI     FLOAT;          /* [AVRREC] - Valor do Recurso do Contrato */
  V_APATRECI    VARCHAR2(30);   /* [APATRECI] - C¢digo de Patrim“nio do Recurso (Ativo) */
  V_ASERECI     VARCHAR2(5);    /* [ASERECI] - S‚rie do recusto (Ativo) */

  /* Itens de Modalidade de Pagamento*/

  V_OCDRECI     NUMBER;         /* [OCDRECI] - C¢digo do Recurso do Contrato */
  V_ONMRECI     VARCHAR2(250);  /* [ONMRECI] - Nome do Recurso do Contrato */
  V_OVRPRRECI   FLOAT;          /* [OVRPRREC] - Valor Percentual do Recurso do Contrato */
  V_OVRRECI     FLOAT;          /* [OVRREC] - Valor do Recurso do Contrato */
  V_OPATRECI    VARCHAR2(30);   /* [OPATRECI] - C¢digo de Patrim“nio do Recurso (Ativo) */
  V_OSERECI     VARCHAR2(5);    /* [OSERECI] - S‚rie do recusto (Ativo) */

           /*-- TODOS DO CONTRATO --*/
  V_CDRECT     NUMBER;         /* [CDRECT] - C¢digo do Recurso do Contrato */
  V_NMRECT     VARCHAR2(250);  /* [NMRECT] - Nome do Recurso do Contrato */
  V_VRPRRECT   FLOAT;          /* [VRPRREC] - Valor Percentual do Recurso do Contrato */
  V_VRRECT     FLOAT;          /* [VRREC] - Valor do Recurso do Contrato */
  V_PATRECT    VARCHAR2(30);   /* [PATRECT] - C¢digo de Patrim“nio do Recurso (Ativo) */
  V_SERECT     VARCHAR2(5);    /* [SERECT] - S‚rie do recusto (Ativo) */
  V_REFERENCIA VARCHAR2(250);
  V_NOTAEXP   VARCHAR2(20);

  /*================= OR€AMENTO ===============*/

  /*-- Dados do Or‡amento --*/
  V_ORCLIE    VARCHAR2(100);  /* [ORCLIE] - Cliente do Or‡amento */
  V_ORFILI    VARCHAR2(100);  /* [ORFILI] - Filial do Or‡amento */
  V_ORCOD     VARCHAR2(100);  /* [ORCOD] - C¢digo do Or‡amento */
  V_ORVERS    VARCHAR2(100);  /* [ORVERS] - VersÆo do Or‡amento */
  V_ORDTREF   DATE;           /* [ORDTREF] - Data do Or‡amento */
  V_ORPRECB   VARCHAR2(100);  /* [ORPRECB] - Pre‡o Base do Or‡amento */
  V_ORFATUR   VARCHAR2(100);  /* [ORFATUR] - Tipo de Faturamento do Or‡amento */
  V_ORMODPAG  VARCHAR2(100);  /* [ORMODPAG] - Modalidade de Pagamento do Or‡amento */
  V_ORINTER   VARCHAR2(200);  /* [ORINTER] - Interlocutor do Or‡amento */
  V_ORREFE    VARCHAR2(400);  /* [ORREFE] - Referˆncia do Or‡amento */
  V_ORDIASVD  NUMBER;         /* [ORDIASVD] - Prazo do Or‡amento */
  V_ORDTVAL   DATE;           /* [ORDTVAL] - Data de Validade do Or‡amento */
  V_QTDIASCT  NUMBER;         /* [QTDIASCT] - Prazo do Contrato no Or‡amento */
  V_ORDTINICT DATE;           /* [ORDTINICT] - Data de In¡cio do Contrato no Or‡amento */
  V_ORDTFINCT DATE;           /* [ORDTFINCT] - Data de T‚rmino do Contrato no Or‡amento */
  V_OREMAIL   VARCHAR2(200);  /* [OREMAIL] - E-MAIL do Cliente do Or‡amento */
  V_ORTEL     VARCHAR2(45);   /* [ORTEL] - Telefone do Cliente do Or‡amento */
  V_OROBS     VARCHAR2(400);  /* [OROBS] - Observa‡äes do Or‡amento */
  V_ORVRORC   FLOAT;          /* [ORVRORC] - Valor do Or‡amento */

  /*-- Dados do Cliente do Or‡amento --*/
  V_ORPARC     VARCHAR2(50);   /* [ORPARC] -> Inscri‡Æo do Cliente do Or‡amento (CNPJ OU CPF)*/
  V_ORNMPARC   VARCHAR2(400);  /* [ORNMPARC] -> Nome do Cliente do Or‡amento */
  V_ORIE       VARCHAR2(50);   /* [ORIE] -> Inscri‡Æo Estadual Cliente do Or‡amento */
  V_ORENDE     VARCHAR2(400);  /* [ORENDE] -> Endere‡o principal do Cliente do Or‡amento (COMPLETO)*/
  V_ORDSEND    VARCHAR2(400);  /* [ORDSEND] -> Descri‡Æo do Endere‡o do Cliente do Or‡amento (ex: Rua X, 112) */
  V_ORMUNIC    VARCHAR2(50);   /* [ORMUNIC]  -> Munic¡pio do Cliente do Or‡amento */
  V_ORBAIRRO   VARCHAR2(200);  /* [ORBAIRRO] -> Bairro do Cliente do Or‡amento */
  V_ORESTADO   VARCHAR2(20);    /* [ORESTADO] -> Estado do Cliente do Or‡amento */
  V_ORCEP      VARCHAR2(20);   /* [ORCEP] -> CEP do Cliente do Or‡amento */
  /* -- Endere‡o de Entrega --*/
  V_ORENDE2    VARCHAR2(400);  /* [ORENDE2] -> Endere‡o principal do Cliente do Or‡amento (COMPLETO)*/
  V_ORDSEND2   VARCHAR2(400);  /* [ORDSEND2] -> Descri‡Æo do Endere‡o do Cliente do Or‡amento (ex: Rua X, 112) */
  V_ORMUNIC2   VARCHAR2(50);   /* [ORMUNIC2]  -> Munic¡pio do Cliente do Or‡amento */
  V_ORBAIRRO2  VARCHAR2(200);  /* [ORBAIRRO2] -> Bairro do Cliente do Or‡amento */
  V_ORESTADO2  VARCHAR2(20);   /* [ORESTADO2] -> Estado do Cliente do Or‡amento */
  V_ORCEP2     VARCHAR2(20);   /* [ORCEP2] -> CEP do Cliente do Or‡amento */

  /*-- Vendedor Principal do Or‡amento --*/
  V_NMVENDEDOR VARCHAR2(400);  /*[NMVENDPRI] -> Nome do vendedor principal do Or‡emnto  */
  V_NMGERENTE  VARCHAR2(400);  /*[NMGERENTPRI] -> Gerente do vendedor principal do Or‡amento*/
  V_PARTVENDA  FLOAT;          /*[PARTVENDPRI] -> Participa‡Æo do Vendedor principal (%)*/

  /*-- Auxiliares para o or‡amento --*/
  V_TOTITEM2 VARCHAR2(20);
  V_TOTPERIT2 VARCHAR2(20);
  V_TOTITEM_A2 VARCHAR2(20);
  V_TOTPERIT_A2 VARCHAR2(20);
  V_TOTITEM_O2 VARCHAR2(20);
  V_TOTPERIT_O2 VARCHAR2(20);
  V_TOTORCA2 VARCHAR2(20);
  V_TOTITEMRC  FLOAT;
  V_TOTORCA    FLOAT;
  V_PRECBOR   VARCHAR2(100);
  V_MODPAGORCA VARCHAR2(100);
  V_CLIEORC   VARCHAR2(100);
  V_INTEROR   VARCHAR2(100);

  V_NRSEQMODDOCFCT NUMBER; /* SEQUENCIAL DO MODELO DE DOCUMENTO PARA CACARREGAR E GERAR OS MNEMONICOS*/

  V_VRTOTRECURSO NUMBER;
  --V_IDUTILIZAHIERSERV VARCHAR2(3);
  V_IDUTILIZAHIERSERV VARCHAR2(3);/*IDENTIFICADOR, SE O MODELO DE PROPOSTA UTILIZA OU NÇO HIERARQUIA*/
  V_TIPOHIERARQUIA NUMBER; /*VALOR PRESENTE NOS MNEMONICOS [TPHIERQ:<VALOR>] OU [TPHIERQINI:<VALOR>] PARA DEFINIR QUAIS ITENS SERAO RETONADOS NO DOCUMENTO*/
  V_TPHIERARQ NUMBER; /*VALOR PARA IDENTIFICAR SE O MNEMONICO UTILIZADO E O [TPHIERQ:<VALOR>] OU [TPHIERQINI:<VALOR>]*/
  V_MNEHIERARQ VARCHAR2(50); /*VARIAVER QUE VAI CONTER O CODIGO DO MNEMONICO INSERIDO NO DOCUMENTO EX. [TPHIERAR:02]*/

  BEGIN
    V_EXISTE_ADICIONAL := 0;
    
     V_NRSEQCONTR := TO_NUMBER(P_NRSEQCONTR);
     V_NRSEQDOCCONT := P_NRSEQDOCCONT;
     V_NRORG := TO_NUMBER(P_NRORG);

     IF P_IDTIPODOC = 'C' THEN

       IF P_SUBMNEDOC = 'N' THEN

           FOR C_DOCUMENTO IN ( SELECT DOC.NRSEQMODDOCFCT, DOC.IDIDIOMA, DOC.TXMODELODOCORCA
                                  FROM FCT_MODELOCONTRATO MC, FCT_MODELODOCCONTR MD, FCT_CONTRATO C, FCT_MODELODOC DOC
                                 WHERE MC.NRORG = V_NRORG
                                   AND MC.NRSEQMODCONTRA = MD.NRSEQMODCONTRA
                                   AND MC.NRORG = MD.NRORG
                                   AND MC.NRSEQMODCONTRA = C.NRSEQMODCONTRA
                                   AND MC.NRORG = C.NRORG
                                   AND C.NRSEQCONTRATO   = V_NRSEQCONTR
                                   --AND MD.IDMODPADRAO    = 'S'
                                   AND MD.NRSEQMODDOCFCT = DOC.NRSEQMODDOCFCT
                                   AND MD.NRORG = DOC.NRORG) LOOP
                NOVO_CODIGO_NT('NRSEQCONTDOC', V_NRSEQDOCCONT);
                INSERT INTO FCT_CONTRATODOC(NRORG,TXMODELODOCORCA, IDIDIOMA, NRSEQCONTRATO, NRSEQCONTDOC, DTINCLUSAO, IDATIVO, NRSEQMODDOCFCT )
                                   VALUES (V_NRORG,C_DOCUMENTO.TXMODELODOCORCA, C_DOCUMENTO.IDIDIOMA,V_NRSEQCONTR, V_NRSEQDOCCONT, SYSDATE, 'S', C_DOCUMENTO.NRSEQMODDOCFCT);


          END LOOP;
       ELSE

          FOR C_DOC IN (SELECT TXMODELODOCORCA, NRSEQMODDOCFCT, NRSEQCONTDOC
                          FROM FCT_CONTRATODOC
                         WHERE (INSTR('_'||V_NRSEQDOCCONT||'_', '_'||NRSEQCONTDOC||'_')>0)
                           AND (NRSEQCONTRATO = V_NRSEQCONTR)
                           AND (NRORG = V_NRORG)) LOOP

                      BEGIN
                           SELECT SUM(VRRECUCONT)
                             INTO V_VRTOTRECURSO
                             FROM FCT_RECURSOSCONTR
                            WHERE NRSEQCONTRATO = V_NRSEQCONTR
                              AND IDRECURATIV = 'S'
                              AND NRORG = V_NRORG;
                        EXCEPTION
                             WHEN NO_DATA_FOUND THEN
                               V_VRTOTRECURSO := NULL;
                        END;

                  /* CASO O DOCUMENTO POSSUA UM MODELO A*/
                   IF C_DOC.NRSEQMODDOCFCT IS NOT NULL THEN
                      BEGIN
                          SELECT TXMODELODOCORCA
                            INTO V_TXDOCUMENT
                            FROM FCT_MODELODOC
                           WHERE NRSEQMODDOCFCT = C_DOC.NRSEQMODDOCFCT
                             AND NRORG = V_NRORG;
                        EXCEPTION
                          WHEN NO_DATA_FOUND THEN
                             V_TXDOCUMENT := NULL;
                        END;
                   ELSE
                      V_TXDOCUMENT := C_DOC.TXMODELODOCORCA;
                   END IF;

                   IF V_TXDOCUMENT IS NOT NULL THEN
                   /* ==== Busca dados principais do contrato ==== */
                      BEGIN
                         SELECT CT.NRCONTRATOFCT, CT.DTINICIOCONTRATO, CT.DTFIMCONTRATO, CT.NRDIASPRAZOCONT, CT.NRPARCNEGOCIO,
                                CT.CDINDFINCONT, CT.DTPADREAJCONT, CT.QTPOSTOTRABCONT, CT.DTPADRPRORGCONT, CT.CDFILIALORCA,
                                OC.CDMODPGTO, OC.NRPARCINTERLOCUTOR, CT.DSREFERENCIACONT
                           INTO V_CDCONT, V_DTINI, V_DTFIN, V_PRAZO, V_PARCCONTR,
                                V_INDFIN, V_DTREA, V_QTPOST, V_DTPRORAUT, V_FILIAL,
                                V_MODPAGORC, V_INTERLCON, V_REFERENCIA
                           FROM FCT_CONTRATO CT, FCT_REFEORCACONT RF, FCT_ORCAMENTO OC
                          WHERE CT.NRORG = V_NRORG
                            AND CT.NRSEQCONTRATO = V_NRSEQCONTR
                            AND RF.NRSEQCONTRATO(+) = CT.NRSEQCONTRATO
                            AND RF.NRORG(+) = CT.NRORG
                            AND OC.NRSEQORCAMENTO(+) = RF.NRSEQORCAMENTO
                            AND OC.NRORG(+) = RF.NRORG;
                      EXCEPTION
                         WHEN NO_DATA_FOUND THEN BEGIN
                             V_CDCONT := NULL;
                             V_DTINI  := NULL;
                             V_DTFIN  := NULL;
                             V_PRAZO  := NULL;
                             V_PARCCONTR := NULL;
                             V_INDFIN := NULL;
                             V_DTREA := NULL;
                             V_QTPOST := NULL;
                             V_DTPRORAUT := NULL;
                             V_MODPAGORC := NULL;
                             V_INTERLCON := NULL;
                         END;
                      END;

                      IF V_MODPAGORC IS NOT NULL THEN
                         BEGIN
                            SELECT DSMODPGTO
                              INTO V_MOPGCON
                              FROM MODPGTO
                             WHERE CDMODPGTO = V_MODPAGORC
                               AND NRORG = V_NRORG;
                         EXCEPTION
                            WHEN NO_DATA_FOUND THEN
                               V_MOPGCON := NULL;
                         END;
                      END IF;

                      IF V_INTERLCON IS NOT NULL THEN
                         BEGIN
                            SELECT NMPRINCIPALPARC
                              INTO V_INTCON
                              FROM PARCNEGOCIO
                             WHERE NRPARCNEGOCIO = V_INTERLCON
                               AND NRORG = V_NRORG;
                         EXCEPTION
                            WHEN NO_DATA_FOUND THEN
                               V_INTCON := NULL;
                         END;
                      END IF;

                      BEGIN
                        SELECT MAX(NF.NRNOTAFISC) AS NRNOTAFISC
                         INTO V_NOTAEXP
                          FROM FCT_RECURSOSCONTR REC, ITEMPRODUTO IPD, NOTAFISCAL NF
                         WHERE REC.NRORG             = V_NRORG
                           AND REC.NRSEQCONTRATO     = V_NRSEQCONTR
                           AND REC.NRPEDIDO          = IPD.NRPEDIDO
                           AND REC.NRITEMPED         = IPD.NRITEMPED
                           AND REC.NRORG             = IPD.NRORG
                           AND NF.CDFILIAL           = IPD.CDFILIAL
                           AND NF.NRLANCTONF         = IPD.NRLANCTONF
                           AND NF.NRORG              = IPD.NRORG
                           AND NF.NRNOTAFISC      IS NOT NULL;
                      EXCEPTION
                         WHEN NO_DATA_FOUND THEN
                             V_NOTAEXP := NULL;
                      END;

                   /* ==== Replace dados principais do contrato ==== */

                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CDCONT]', NVL(V_CDCONT, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[DTINI]', NVL(TO_CHAR(V_DTINI, 'DD/MM/YYYY'), ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[DTFIN]', NVL(TO_CHAR(V_DTFIN, 'DD/MM/YYYY'), ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[PRAZO]', NVL(TO_CHAR(V_PRAZO), ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[INDFIN]', NVL(V_INDFIN, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[DTREA]', NVL(TO_CHAR(V_DTREA, 'DD/MM/YYYY'), ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[QTPOST]', NVL(TO_CHAR(V_QTPOST), ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[DTPRORAUT]', NVL(TO_CHAR(V_DTPRORAUT, 'DD/MM/YYYY'), ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[INTCON]', TO_CHAR(NVL(V_INTCON, '')));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[MOPGCON]', NVL(TO_CHAR(V_MOPGCON), ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[DTVENPARC]', TO_CHAR(NVL(V_DTVENPARC, '')));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[DTHOJE]', TO_CHAR(SYSDATE,'DD/MM/YYYY'));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[DIAAT]', TO_CHAR(SYSDATE,'DD'));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[MESAT]', TO_CHAR(SYSDATE,'MM'));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ANOAT]', TO_CHAR(SYSDATE,'YYYY'));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[REFCONT]', TO_CHAR(NVL(V_REFERENCIA,'')));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[TOTREC]', TO_CHAR(NVL(V_VRTOTRECURSO,'0')));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[NFEXP]', TO_CHAR(NVL(V_NOTAEXP,'')));

                      CASE TO_CHAR(SYSDATE,'MM')
                         WHEN '01' THEN V_NMMESAT := 'Janeiro';
                         WHEN '02' THEN V_NMMESAT := 'Fevereiro';
                         WHEN '03' THEN V_NMMESAT := 'Mar‡o';
                         WHEN '04' THEN V_NMMESAT := 'Abril';
                         WHEN '05' THEN V_NMMESAT := 'Maio';
                         WHEN '06' THEN V_NMMESAT := 'Junho';
                         WHEN '07' THEN V_NMMESAT := 'Julho';
                         WHEN '08' THEN V_NMMESAT := 'Agosto';
                         WHEN '09' THEN V_NMMESAT := 'Setembro';
                         WHEN '10' THEN V_NMMESAT := 'Outubro';
                         WHEN '11' THEN V_NMMESAT := 'Novembro';
                         WHEN '12' THEN V_NMMESAT := 'Dezembro';
                         ELSE V_NMMESAT := '';
                      END CASE;

                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[NMMESAT]', V_NMMESAT);

                   /* ==== Busca dados do Comprador ==== */

                      IF V_PARCCONTR IS NOT NULL THEN
                         BEGIN
                            SELECT PCN.NRINSCRICAOPARC, PCN.NMPRINCIPALPARC, CLI.NRINSESTCLIE  AS INSCMUNI
                              INTO V_CPARC, V_CNMPARC, V_CIE
                              FROM PARCNEGOCIO PCN, CLIENTE CLI
                             WHERE PCN.NRORG = V_NRORG
                               AND PCN.NRPARCNEGOCIO = V_PARCCONTR
                               AND CLI.NRPARCNEGOCIO = PCN.NRPARCNEGOCIO
                               AND CLI.NRORG = PCN.NRORG;
                         EXCEPTION
                            WHEN NO_DATA_FOUND THEN BEGIN
                               V_CPARC := NULL;
                               V_CNMPARC := NULL;
                               V_CIE := NULL;
                            END;
                         END;

                         BEGIN
                            SELECT DISTINCT (ENP.DSENDERECO||' - '||ENP.NMBAIRROENDERECO||' - '||MN.NMMUNICIPIO||' - '||ENP.SGESTADO) AS ENDERECO,
                                   ENP.DSENDERECO, MN.NMMUNICIPIO, ENP.NMBAIRROENDERECO, ENP.SGESTADO, ENP.NRCEPENDERECO
                              INTO V_CENDE, V_CDSEND, V_CMUNIC, V_CBAIRRO, V_CESTADO, V_CCEP
                              FROM ENDERECOPARC ENP, MUNICIPIO MN
                             WHERE ENP.NRORG = V_NRORG
                               AND ENP. NRPARCNEGOCIO = V_PARCCONTR
                               AND MN.CDMUNICIPIO = ENP.CDMUNICIPIO
                               AND MN.CDPAIS  = ENP.CDPAIS
                               AND MN.SGESTADO = ENP.SGESTADO
                               AND ENP.CDTIPOENDERECO = 'PRINCIPAL';
                         EXCEPTION
                            WHEN NO_DATA_FOUND THEN BEGIN
                               V_CENDE := NULL;
                               V_CDSEND := NULL;
                               V_CMUNIC := NULL;
                               V_CBAIRRO := NULL;
                               V_CESTADO := NULL;
                               V_CCEP := NULL;
                            END;
                         END;
                          /* -- Endere‡o de Entrega (do Or‡amento) --*/
                          /* LAERCIO */
                         BEGIN
                            SELECT DISTINCT (ENP.DSENDERECO||' - '||ENP.NMBAIRROENDERECO||' - '||MN.NMMUNICIPIO||' - '||ENP.SGESTADO) AS ENDERECO,
                                   ENP.DSENDERECO, MN.NMMUNICIPIO, ENP.NMBAIRROENDERECO, ENP.SGESTADO, ENP.NRCEPENDERECO
                              INTO V_CENDE2, V_CDSEND2, V_CMUNIC2, V_CBAIRRO2, V_CESTADO2, V_CCEP2
                              FROM FCT_ENDERECOORCA ENP, MUNICIPIO MN, FCT_REFEORCACONT RF, FCT_CONTRATO CT
                             WHERE CT.NRORG = V_NRORG
                               AND CT.NRSEQCONTRATO = V_NRSEQCONTR
                               AND RF.NRSEQCONTRATO = CT.NRSEQCONTRATO
                               AND RF.NRORG = CT.NRORG
                               AND ENP.NRSEQORCAMENTO = RF.NRSEQORCAMENTO
                               AND ENP.NRORG = RF.NRORG
                               AND MN.CDMUNICIPIO(+) = ENP.CDMUNICIPIO
                               AND MN.CDPAIS(+)  = ENP.CDPAIS
                               AND MN.SGESTADO(+) = ENP.SGESTADO
                               AND ENP.CDTIPOENDERECO = 'ENTREGA';
                         EXCEPTION
                            WHEN NO_DATA_FOUND THEN BEGIN
                               V_ORENDE2 := NULL;
                               V_ORDSEND2 := NULL;
                               V_ORMUNIC2 := NULL;
                               V_ORBAIRRO2 := NULL;
                               V_ORESTADO2 := NULL;
                               V_ORCEP2 := NULL;
                            END;
                         END;
                  END IF;


                      /* ==== Replace dados Comprador ==== */

                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CPARC]', NVL(V_CPARC, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CNMPARC]', NVL(V_CNMPARC, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CIE]', NVL(V_CIE, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CENDE]', NVL(V_CENDE, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CDSEND]', NVL(V_CDSEND, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CMUNIC]', NVL(V_CMUNIC, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CBAIRRO]', NVL(V_CBAIRRO, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CESTADO]', NVL(V_CESTADO, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CCEP]', NVL(V_CCEP, ''));

                      /* -- Endere‡o de Entrega (do Or‡amento)--*/

                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CENDE2]', NVL(V_CENDE2, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CDSEND2]', NVL(V_CDSEND2, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CMUNIC2]', NVL(V_CMUNIC2, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CBAIRRO2]', NVL(V_CBAIRRO2, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CESTADO2]', NVL(V_CESTADO2, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CCEP2]', NVL(V_CCEP2, ''));

                      /* ==== Busca dados do Prestador ==== */

                      IF V_FILIAL IS NOT NULL THEN
                         BEGIN
                            SELECT NRINSJURFILI, NMRAZSOCFILI, CDINSCESTA
                              INTO V_PPARC, V_PNMPARC, V_PIE
                              FROM FILIAL
                             WHERE CDFILIAL = V_FILIAL
                               AND NRORG = V_NRORG;
                         EXCEPTION
                            WHEN NO_DATA_FOUND THEN BEGIN
                               V_PPARC := NULL;
                               V_PNMPARC := NULL;
                               V_PIE := NULL;
                            END;
                         END;

                         BEGIN
                            SELECT DISTINCT (EDF.DSENDEFILI||' - '||EDF.NMBAIRFILI||' - '||MN.NMMUNICIPIO||' - '||EDF.SGESTADO) AS ENDERECO,
                                   EDF.DSENDEFILI, MN.NMMUNICIPIO, EDF.NMBAIRFILI, EDF.SGESTADO, NRCEPFILI
                              INTO V_PENDE, V_PDSEND, V_PMUNIC, V_PBAIRRO, V_PESTADO, V_PCEP
                              FROM ENDEFILI EDF, MUNICIPIO MN
                             WHERE EDF.CDFILIAL = V_FILIAL
                               AND EDF.NRORG = V_NRORG
                               AND MN.CDMUNICIPIO = EDF.CDMUNICIPIO
                               AND MN.CDPAIS = EDF.CDPAIS
                               AND MN.SGESTADO = EDF.SGESTADO;
                         EXCEPTION
                            WHEN NO_DATA_FOUND THEN BEGIN
                               V_PENDE := NULL;
                               V_PDSEND := NULL;
                               V_PMUNIC := NULL;
                               V_PBAIRRO := NULL;
                               V_PESTADO := NULL;
                               V_PCEP := NULL;
                            END;
                         END;
                      END IF;

                      /* ==== Replace dados Prestador ==== */

                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[PPARC]', NVL(V_PPARC, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[PNMPARC]', NVL(V_PNMPARC, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[PIE]', NVL(V_PIE, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[PENDE]', NVL(V_PENDE, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[PDSEND]', NVL(V_PDSEND, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[PMUNIC]', NVL(V_PMUNIC, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[PBAIRRO]', NVL(V_PBAIRRO, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[PESTADO]', NVL(V_PESTADO, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[PCEP]', NVL(V_PCEP, ''));

                      /* ==== Replace dados Item ==== */

                      V_TESTA1 := INSTR(V_TXDOCUMENT,'<tr',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</tr>', INSTR(V_TXDOCUMENT, '[IND]',1)))*-1);
                      V_TESTA2 := INSTR(V_TXDOCUMENT,'<p',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</p>', INSTR(V_TXDOCUMENT, '[IND]',1)))*-1);

                      IF V_TESTA1 <> 0 THEN
                         IF V_TESTA1 < V_TESTA2 THEN
                            V_RESULT := 'C';  /* Coluna */
                            V_POSINI := INSTR(V_TXDOCUMENT,'<tr',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</tr>', INSTR(V_TXDOCUMENT, '[IND]',1)))*-1);
                            V_POSFIN := INSTR(V_TXDOCUMENT,'</tr>',INSTR(V_TXDOCUMENT,'[IND]',1));
                         ELSE
                            V_RESULT := 'L'; /* Linha */
                            V_POSINI := INSTR(V_TXDOCUMENT,'<p',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</p>', INSTR(V_TXDOCUMENT, '[IND]',1)))*-1);
                            V_POSFIN := INSTR(V_TXDOCUMENT,'</p>',INSTR(V_TXDOCUMENT,'[IND]',1));
                         END IF;
                      ELSE
                           V_RESULT := 'L'; /* Linha */
                            V_POSINI := INSTR(V_TXDOCUMENT,'[LN]',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'[/LN]', INSTR(V_TXDOCUMENT, '[IND]',1)))*-1);
                            V_POSFIN := INSTR(V_TXDOCUMENT,'[/LN]',INSTR(V_TXDOCUMENT,'[IND]',1));
                      END IF;

                         V_CLOBINICIAL := SUBSTR(V_TXDOCUMENT,1,V_POSFIN-1);
                         V_CLOBLINHA :=  SUBSTR(V_TXDOCUMENT,V_POSINI,V_POSFIN-V_POSINI);
                         V_CLOBFINAL :=  SUBSTR(V_TXDOCUMENT,V_POSFIN,length(V_TXDOCUMENT));

                         V_TSDADOS := 0;
                         FOR C_LINE IN (SELECT TO_CHAR(ROWNUM) AS IND, NRITEMOCONTRATO
                                            FROM FCT_ITEMCONTRATO
                                           WHERE NRSEQCONTRATO = V_NRSEQCONTR
                                             AND NRORG = V_NRORG
                                             AND IDITEMATIVO = 'S'
                                             AND IDADICITEMCONT = 'N') LOOP
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA,'[IND]','[IND'||C_LINE.IND||']');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[COD]','[COD'||C_LINE.IND||']');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[QNT]', '[QNT'||C_LINE.IND||']');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[DESC]', '[DESC'||C_LINE.IND||']');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[QTMED]', '[QTMED'||C_LINE.IND||']');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[MEDIDA]', '[MEDIDA'||C_LINE.IND||']');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[TPSERV]', '[TPSERV'||C_LINE.IND||']');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[QTTPSERV]', '[QTTPSERV'||C_LINE.IND||']');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[MODPAG]', '[MODPAG'||C_LINE.IND||']');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[VRUNIT]', '[VRUNIT'||C_LINE.IND||']');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[VRTOT]', '[VRTOT'||C_LINE.IND||']');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[TOTPER]', '[TOTPER'||C_LINE.IND||']');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[VRPRRECI]', '[VRPRRECI'||C_LINE.IND||C_LINE.NRITEMOCONTRATO||']');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[VRRECI]', '[VRRECI'||C_LINE.IND||C_LINE.NRITEMOCONTRATO||']');

                            IF V_RESULT = 'C' THEN
                               V_CLOBINICIAL := V_CLOBINICIAL||V_CLOBLINHA1||'</tr>';
                            ELSE
                               V_CLOBINICIAL := V_CLOBINICIAL||V_CLOBLINHA1;
                            END IF;

                            V_TSDADOS := V_TSDADOS+1;
                         END LOOP;
                         IF V_TSDADOS = 0 THEN
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA,'[IND]','');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[COD]','');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[QNT]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[DESC]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[QTMED]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[MEDIDA]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[TPSERV]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[QTTPSERV]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[MODPAG]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[VRUNIT]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[VRTOT]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[TOTPER]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[VRPRRECI]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[VRRECI]', '');

                            V_TXDOCUMENT := V_CLOBINICIAL||V_CLOBFINAL;
                            V_TXDOCUMENT := REPLACE(V_TXDOCUMENT, V_CLOBLINHA, '');
                         ELSE
                            V_TXDOCUMENT := V_CLOBINICIAL||V_CLOBFINAL;
                            V_TXDOCUMENT := REPLACE(V_TXDOCUMENT, V_CLOBLINHA, '');
                         END IF;

                     /* o c¢digo abaixo foi retirado do loop acima */


                        V_TESTARC1 := INSTR(V_TXDOCUMENT,'<tr',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</tr>', INSTR(V_TXDOCUMENT, '[INDRECI]',1)))*-1);
                         V_TESTARC2 := INSTR(V_TXDOCUMENT,'<p',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</p>', INSTR(V_TXDOCUMENT, '[INDRECI]',1)))*-1);
                         IF  V_TESTARC1<>0 THEN
                               IF V_TESTARC1 < V_TESTARC2 THEN
                                  V_RESULTREC := 'C';
                                  V_POSINIREC := INSTR(V_TXDOCUMENT,'<tr',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</tr>', INSTR(V_TXDOCUMENT, '[INDRECI]',1)))*-1);
                                  V_POSFINREC := INSTR(V_TXDOCUMENT,'</tr>',INSTR(V_TXDOCUMENT,'[INDRECI]',1));
                               ELSE
                                  V_RESULTREC := 'L';
                                  V_POSINIREC := INSTR(V_TXDOCUMENT,'<p',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</p>', INSTR(V_TXDOCUMENT, '[INDRECI]',1)))*-1);
                                  V_POSFINREC := INSTR(V_TXDOCUMENT,'</p>',INSTR(V_TXDOCUMENT,'[INDRECI]',1));
                               END IF;
                         ELSE
                                  V_RESULTREC := 'L';
                                  V_POSINIREC := INSTR(V_TXDOCUMENT,'[LN]',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'[/LN]', INSTR(V_TXDOCUMENT, '[INDRECI]',1)))*-1);
                                  V_POSFINREC := INSTR(V_TXDOCUMENT,'[/LN]',INSTR(V_TXDOCUMENT,'[INDRECI]',1));
                         END IF;

                         V_CLOBINIREC := SUBSTR(V_TXDOCUMENT,1,V_POSFINREC-1);
                         V_CLOBLINREC := SUBSTR(V_TXDOCUMENT,V_POSINIREC,V_POSFINREC-V_POSINIREC);
                         V_CLOBFINREC := SUBSTR(V_TXDOCUMENT,V_POSFINREC,length(V_TXDOCUMENT));

                   --   IF  V_TESTARC2 <> 0 THEN
                        FOR C_LINE IN (SELECT TO_CHAR(ROWNUM) AS IND, NRITEMOCONTRATO
                                            FROM FCT_ITEMCONTRATO
                                           WHERE NRSEQCONTRATO = V_NRSEQCONTR
                                             AND NRORG = V_NRORG
                                             AND IDITEMATIVO = 'S'
                                             AND IDADICITEMCONT = 'N') LOOP

                               SELECT COUNT(*)
                                  INTO V_RECURS
                                  FROM FCT_RECURSOSCONTR
                                 WHERE NRSEQCONTRATO = V_NRSEQCONTR
                                   AND NRORG = V_NRORG
                                   AND NRITEMOCONTRATO = C_LINE.NRITEMOCONTRATO
                                   AND IDRECURATIV = 'S';

                               IF V_RECURS > 0 THEN
                                  FOR C_LINERC IN (SELECT TO_CHAR(ROWNUM) AS IND, NRSEQRECUCONT
                                                     FROM FCT_RECURSOSCONTR
                                                    WHERE NRSEQCONTRATO = V_NRSEQCONTR
                                                      AND NRORG = V_NRORG
                                                      AND NRITEMOCONTRATO = C_LINE.NRITEMOCONTRATO
                                                      AND IDRECURATIV = 'S') LOOP

                                     V_CLOBLINREC1 := REPLACE(V_CLOBLINREC,'[INDRECI]','[INDRECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                     V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[CDRECI]','[CDRECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                     V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[NMRECI]', '[NMRECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                     V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[VRPRRECI]', '[VRPRRECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                     V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[VRRECI]', '[VRRECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                     V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[PATRECI]', '[PATRECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                     V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[SERECI]', '[SERECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                     V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[MODREC]', '[MODREC'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                     V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[FABREC]', '[FABREC'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                     V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[GRUPOREC]', '[GRUPOREC'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                     V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[CLASREC]', '[CLASREC'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                     IF V_RESULTREC = 'C' THEN
                                        V_CLOBINIREC := V_CLOBINIREC||V_CLOBLINREC1||'</tr>';
                                     ELSE
                                        V_CLOBINIREC := V_CLOBINIREC||V_CLOBLINREC1||'<br>';
                                     END IF;
                                  END LOOP;
                               END IF;
                          end loop;
                          V_TXDOCUMENT := V_CLOBINIREC||V_CLOBFINREC;
                          V_TXDOCUMENT := REPLACE(V_TXDOCUMENT, V_CLOBLINREC, '');

                        --END IF;


                     /* fim do loop de recursos */


                     /*---------------------------------------------*/

                      /* Rotina para cirar linhas novas em caso de Apura‡Æo de faturamento */


                      V_TESTA1 := INSTR(V_TXDOCUMENT,'<tr',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</tr>', INSTR(V_TXDOCUMENT, '[AIND]',1)))*-1);
                      V_TESTA2 := INSTR(V_TXDOCUMENT,'<p',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</p>', INSTR(V_TXDOCUMENT, '[AIND]',1)))*-1);
                      IF V_TESTA1 <> 0 THEN
                         IF V_TESTA1 < V_TESTA2 THEN
                            V_RESULT := 'C';  /* Coluna */
                            V_POSINI := INSTR(V_TXDOCUMENT,'<tr',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</tr>', INSTR(V_TXDOCUMENT, '[AIND]',1)))*-1);
                            V_POSFIN := INSTR(V_TXDOCUMENT,'</tr>',INSTR(V_TXDOCUMENT,'[AIND]',1));
                         ELSE
                            V_RESULT := 'L';  /* Linha */
                            V_POSINI := INSTR(V_TXDOCUMENT,'<p',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</p>', INSTR(V_TXDOCUMENT, '[AIND]',1)))*-1);
                            V_POSFIN := INSTR(V_TXDOCUMENT,'</p>',INSTR(V_TXDOCUMENT,'[AIND]',1));
                         END IF;
                     ELSE

                           V_RESULT := 'L'; /* Linha */
                            V_POSINI := INSTR(V_TXDOCUMENT,'[LN]',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'[/LN]', INSTR(V_TXDOCUMENT, '[AIND]',1)))*-1);
                            V_POSFIN := INSTR(V_TXDOCUMENT,'[/LN]',INSTR(V_TXDOCUMENT,'[AIND]',1));

                     END IF;
                         V_CLOBINICIAL := SUBSTR(V_TXDOCUMENT,1,V_POSFIN-1);
                         V_CLOBLINHA :=  SUBSTR(V_TXDOCUMENT,V_POSINI,V_POSFIN-V_POSINI);
                         V_CLOBFINAL :=  SUBSTR(V_TXDOCUMENT,V_POSFIN,length(V_TXDOCUMENT));

                         V_TSDADOS := 0;
                         FOR C_LINE IN (SELECT TO_CHAR(ROWNUM) AS IND, NRITEMOCONTRATO
                                          FROM FCT_ITEMCONTRATO
                                         WHERE NRSEQCONTRATO = V_NRSEQCONTR
                                           AND NRORG = V_NRORG
                                           AND IDITEMATIVO = 'S'
                                           AND IDFATURACONTR = 'A'
                                           AND IDADICITEMCONT = 'N') LOOP

                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA,'[AIND]','[AIND'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[ACOD]','[ACOD'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[AQNT]', '[AQNT'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[ADESC]', '[ADESC'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[AQTMED]', '[AQTMED'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[AMEDIDA]', '[AMEDIDA'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[ATPSERV]', '[ATPSERV'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[AQTTPSERV]', '[AQTTPSERV'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[AMODPAG]', '[AMODPAG'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[AVRUNIT]', '[AVRUNIT'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[AVRTOT]', '[AVRTOT'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[ATOTPER]', '[ATOTPER'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[AVRPRRECI]', '[AVRPRRECI'||C_LINE.IND||C_LINE.NRITEMOCONTRATO||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[AVRRECI]', '[AVRRECI'||C_LINE.IND||C_LINE.NRITEMOCONTRATO||']');
                             IF V_RESULT = 'C' THEN
                                 V_CLOBINICIAL := V_CLOBINICIAL||V_CLOBLINHA1||'</tr>';
                             ELSE
                                 V_CLOBINICIAL := V_CLOBINICIAL||V_CLOBLINHA1;
                             END IF;

                            V_TSDADOS := V_TSDADOS+1;
                         END LOOP;
                         IF V_TSDADOS = 0 THEN
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA,'[AIND]','');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[ACOD]','');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[AQNT]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[ADESC]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[AQTMED]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[AMEDIDA]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[ATPSERV]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[AQTTPSERV]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[AMODPAG]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[AVRUNIT]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[AVRTOT]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[ATOTPER]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[AVRPRRECI]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[AVRRECI]', '');

                            V_TXDOCUMENT := V_CLOBINICIAL||V_CLOBFINAL;
                            V_TXDOCUMENT := REPLACE(V_TXDOCUMENT, V_CLOBLINHA, '');
                         ELSE
                            V_TXDOCUMENT := V_CLOBINICIAL||V_CLOBFINAL;
                            V_TXDOCUMENT := REPLACE(V_TXDOCUMENT, V_CLOBLINHA, '');
                         END IF;
                    --  END IF;

                      /* codigo retirado do loop acima*/

                      V_TESTARC1 := INSTR(V_TXDOCUMENT,'<tr',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</tr>', INSTR(V_TXDOCUMENT, '[AINDRECI]',1)))*-1);
                         V_TESTARC2 := INSTR(V_TXDOCUMENT,'<p',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</p>', INSTR(V_TXDOCUMENT, '[AINDRECI]',1)))*-1);

                      IF  V_TESTARC1<>0 THEN
                               IF V_TESTARC1 < V_TESTARC2 THEN
                                  V_RESULTREC := 'C';
                                  V_POSINIREC := INSTR(V_TXDOCUMENT,'<tr',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</tr>', INSTR(V_TXDOCUMENT, '[AINDRECI]',1)))*-1);
                                  V_POSFINREC := INSTR(V_TXDOCUMENT,'</tr>',INSTR(V_TXDOCUMENT,'[AINDRECI]',1));
                               ELSE
                                  V_RESULTREC := 'L';
                                  V_POSINIREC := INSTR(V_TXDOCUMENT,'<p',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</p>', INSTR(V_TXDOCUMENT, '[AINDRECI]',1)))*-1);
                                  V_POSFINREC := INSTR(V_TXDOCUMENT,'</p>',INSTR(V_TXDOCUMENT,'[AINDRECI]',1));
                               END IF;
                         ELSE
                                  V_RESULTREC := 'L';
                                  V_POSINIREC := INSTR(V_TXDOCUMENT,'<span',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</span>', INSTR(V_TXDOCUMENT, '[AINDRECI]',1)))*-1);
                                  V_POSFINREC := INSTR(V_TXDOCUMENT,'</span>',INSTR(V_TXDOCUMENT,'[AINDRECI]',1));
                         END IF;




                         V_CLOBINIREC := SUBSTR(V_TXDOCUMENT,1,V_POSFINREC-1);
                         V_CLOBLINREC :=  SUBSTR(V_TXDOCUMENT,V_POSINIREC,V_POSFINREC-V_POSINIREC);
                         V_CLOBFINREC :=  SUBSTR(V_TXDOCUMENT,V_POSFINREC,length(V_TXDOCUMENT));

                    --     IF V_TESTARC1 <> 0 THEN
                               FOR C_LINE IN (SELECT TO_CHAR(ROWNUM) AS IND, NRITEMOCONTRATO
                                                FROM FCT_ITEMCONTRATO
                                               WHERE NRSEQCONTRATO = V_NRSEQCONTR
                                               AND NRORG = V_NRORG
                                                 AND IDITEMATIVO = 'S'
                                                 AND IDFATURACONTR = 'A'
                                                 AND IDADICITEMCONT = 'N') LOOP
                                          SELECT COUNT(*)
                                            INTO V_RECURS
                                            FROM FCT_RECURSOSCONTR
                                           WHERE NRSEQCONTRATO = V_NRSEQCONTR
                                             AND NRORG = V_NRORG
                                             AND NRITEMOCONTRATO = C_LINE.NRITEMOCONTRATO
                                             AND IDRECURATIV = 'S';

                                         IF V_RECURS > 0 THEN
                                            FOR C_LINERC IN (SELECT TO_CHAR(ROWNUM) AS IND, NRSEQRECUCONT
                                                               FROM FCT_RECURSOSCONTR
                                                              WHERE NRSEQCONTRATO = V_NRSEQCONTR
                                                                AND NRORG = V_NRORG
                                                                AND NRITEMOCONTRATO = C_LINE.NRITEMOCONTRATO
                                                                AND IDRECURATIV = 'S') LOOP

                                               V_CLOBLINREC1 := REPLACE(V_CLOBLINREC,'[AINDRECI]','[AINDRECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                               V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[ACDRECI]','[ACDRECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                               V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[ANMRECI]', '[ANMRECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                               V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[AVRPRRECI]', '[AVRPRRECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                               V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[AVRRECI]', '[AVRRECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                               V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[APATRECI]', '[APATRECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                               V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[ASERECI]', '[ASERECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                               V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[MODREC]', '[MODREC'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                               V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[FABREC]', '[FABREC'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                               V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[GRUPOREC]', '[GRUPOREC'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                               V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[CLASREC]', '[CLASREC'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                               IF V_RESULT = 'C' THEN
                                                  V_CLOBINIREC := V_CLOBINIREC||V_CLOBLINREC1||'</tr>';
                                               ELSE
                                                  V_CLOBINIREC := V_CLOBINIREC||V_CLOBLINREC1||'<br>';
                                               END IF;
                                         END LOOP;
                                  END IF;
                              END LOOP;
                              V_TXDOCUMENT := V_CLOBINIREC||V_CLOBFINREC;
                              V_TXDOCUMENT := REPLACE(V_TXDOCUMENT, V_CLOBLINREC, '');

                   --   END IF;

                     /*---------------------------------------------*/

                      /* Rotina para cirar linhas novas em caso de Modalidade de Pagamento */

                      V_TESTA1 := INSTR(V_TXDOCUMENT,'<tr',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</tr>', INSTR(V_TXDOCUMENT, '[OIND]',1)))*-1);
                      V_TESTA2 := INSTR(V_TXDOCUMENT,'<p',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</p>', INSTR(V_TXDOCUMENT, '[OIND]',1)))*-1);
                      IF V_TESTA1 <> 0 THEN
                         IF V_TESTA1 < V_TESTA2 THEN
                            V_RESULT := 'C';  /* Coluna */
                            V_POSINI := INSTR(V_TXDOCUMENT,'<tr',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</tr>', INSTR(V_TXDOCUMENT, '[OIND]',1)))*-1);
                            V_POSFIN := INSTR(V_TXDOCUMENT,'</tr>',INSTR(V_TXDOCUMENT,'[OIND]',1));
                         ELSE
                            V_RESULT := 'L';  /* Linha */
                            V_POSINI := INSTR(V_TXDOCUMENT,'<p',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</p>', INSTR(V_TXDOCUMENT, '[OIND]',1)))*-1);
                            V_POSFIN := INSTR(V_TXDOCUMENT,'</p>',INSTR(V_TXDOCUMENT,'[OIND]',1));
                         END IF;
                     ELSE
                           V_RESULT := 'L'; /* Linha */
                            V_POSINI := INSTR(V_TXDOCUMENT,'[LN]',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'[/LN]', INSTR(V_TXDOCUMENT, '[OIND]',1)))*-1);
                            V_POSFIN := INSTR(V_TXDOCUMENT,'[/LN]',INSTR(V_TXDOCUMENT,'[OIND]',1));
                      END IF;
                         V_CLOBINICIAL := SUBSTR(V_TXDOCUMENT,1,V_POSFIN-1);
                         V_CLOBLINHA :=  SUBSTR(V_TXDOCUMENT,V_POSINI,V_POSFIN-V_POSINI);
                         V_CLOBFINAL :=  SUBSTR(V_TXDOCUMENT,V_POSFIN,length(V_TXDOCUMENT));

                         V_TSDADOS := 0;
                         FOR C_LINE IN (SELECT TO_CHAR(ROWNUM) AS IND, NRITEMOCONTRATO
                                           FROM FCT_ITEMCONTRATO
                                           WHERE NRSEQCONTRATO = V_NRSEQCONTR
                                             AND NRORG = V_NRORG
                                             AND IDITEMATIVO = 'S'
                                             AND IDADICITEMCONT = 'N'
                                             AND IDFATURACONTR = 'O') LOOP

                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA,'[OIND]','[OIND'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OCOD]','[OCOD'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OQNT]', '[OQNT'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[ODESC]', '[ODESC'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OQTMED]', '[OQTMED'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OMEDIDA]', '[OMEDIDA'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OTPSERV]', '[OTPSERV'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OQTTPSERV]', '[OQTTPSERV'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OMODPAG]', '[OMODPAG'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OVRUNIT]', '[OVRUNIT'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OVRTOT]', '[OVRTOT'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OTOTPER]', '[OTOTPER'||C_LINE.IND||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OVRPRRECI]', '[OVRPRRECI'||C_LINE.IND||C_LINE.NRITEMOCONTRATO||']');
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OVRRECI]', '[OVRRECI'||C_LINE.IND||C_LINE.NRITEMOCONTRATO||']');
                             IF V_RESULT = 'C' THEN
                                V_CLOBINICIAL := V_CLOBINICIAL||V_CLOBLINHA1||'</tr>';
                             ELSE
                                V_CLOBINICIAL := V_CLOBINICIAL||V_CLOBLINHA1;
                             END IF;

                             V_TSDADOS := V_TSDADOS+1;
                         END LOOP;
                         IF V_TSDADOS <> 0 THEN
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA,'[OIND]','');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OCOD]','');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OQNT]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[ODESC]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OQTMED]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OMEDIDA]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OTPSERV]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OQTTPSERV]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OMODPAG]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OVRUNIT]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OVRTOT]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OTOTPER]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OVRPRRECI]', '');
                            V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OVRRECI]', '');

                            V_TXDOCUMENT := V_CLOBINICIAL||V_CLOBFINAL;
                            V_TXDOCUMENT := REPLACE(V_TXDOCUMENT, V_CLOBLINHA, '');
                         ELSE
                            V_TXDOCUMENT := V_CLOBINICIAL||V_CLOBFINAL;
                            V_TXDOCUMENT := REPLACE(V_TXDOCUMENT, V_CLOBLINHA, '');
                         END IF;
                    --END IF;
                         /*CODIGO RETIRADO DO LOOP ACIMA*/

                         V_TESTARC1 := INSTR(V_TXDOCUMENT,'<tr',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</tr>', INSTR(V_TXDOCUMENT, '[OINDRECI]',1)))*-1);
                         V_TESTARC2 := INSTR(V_TXDOCUMENT,'<p',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</p>', INSTR(V_TXDOCUMENT, '[OINDRECI]',1)))*-1);


                          IF  V_TESTARC1<>0 THEN
                               IF V_TESTARC1 < V_TESTARC2 THEN
                                  V_RESULTREC := 'C';
                                  V_POSINIREC := INSTR(V_TXDOCUMENT,'<tr',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</tr>', INSTR(V_TXDOCUMENT, '[OINDRECI]',1)))*-1);
                                  V_POSFINREC := INSTR(V_TXDOCUMENT,'</tr>',INSTR(V_TXDOCUMENT,'[OINDRECI]',1));
                               ELSE
                                  V_RESULTREC := 'L';
                                  V_POSINIREC := INSTR(V_TXDOCUMENT,'<p',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</p>', INSTR(V_TXDOCUMENT, '[OINDRECI]',1)))*-1);
                                  V_POSFINREC := INSTR(V_TXDOCUMENT,'</p>',INSTR(V_TXDOCUMENT,'[OINDRECI]',1));
                               END IF;
                         ELSE
                                  V_RESULTREC := 'L';
                                  V_POSINIREC := INSTR(V_TXDOCUMENT,'<span',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</span>', INSTR(V_TXDOCUMENT, '[OINDRECI]',1)))*-1);
                                  V_POSFINREC := INSTR(V_TXDOCUMENT,'</span>',INSTR(V_TXDOCUMENT,'[OINDRECI]',1));
                         END IF;

                         V_CLOBINIREC := SUBSTR(V_TXDOCUMENT,1,V_POSFINREC-1);
                         V_CLOBLINREC :=  SUBSTR(V_TXDOCUMENT,V_POSINIREC,V_POSFINREC-V_POSINIREC);
                         V_CLOBFINREC :=  SUBSTR(V_TXDOCUMENT,V_POSFINREC,length(V_TXDOCUMENT));

                    --     IF V_TESTARC1 <> 0 THEN

                               FOR C_LINE IN (SELECT TO_CHAR(ROWNUM) AS IND, NRITEMOCONTRATO
                                           FROM FCT_ITEMCONTRATO
                                           WHERE NRSEQCONTRATO = V_NRSEQCONTR
                                             AND NRORG = V_NRORG
                                             AND IDITEMATIVO = 'S'
                                             AND IDADICITEMCONT = 'N'
                                             AND IDFATURACONTR = 'O') LOOP
                                    SELECT COUNT(*)
                                       INTO V_RECURS
                                       FROM FCT_RECURSOSCONTR
                                      WHERE NRSEQCONTRATO = V_NRSEQCONTR
                                        AND NRORG = V_NRORG
                                        AND NRITEMOCONTRATO = C_LINE.NRITEMOCONTRATO
                                        AND IDRECURATIV = 'S';

                                    IF V_RECURS > 0 THEN
                                       FOR C_LINERC IN (SELECT TO_CHAR(ROWNUM) AS IND, NRSEQRECUCONT
                                                          FROM FCT_RECURSOSCONTR
                                                         WHERE NRSEQCONTRATO = V_NRSEQCONTR
                                                           AND NRORG = V_NRORG
                                                           AND NRITEMOCONTRATO = C_LINE.NRITEMOCONTRATO
                                                           AND IDRECURATIV = 'S') LOOP

                                          V_CLOBLINREC1 := REPLACE(V_CLOBLINREC,'[OINDRECI]','[OINDRECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                          V_CLOBLINREC1 := REPLACE(V_CLOBLINREC,'[OCDRECI]','[OCDRECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                          V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[ONMRECI]', '[ONMRECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                          V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[OVRPRRECI]', '[OVRPRRECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                          V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[OVRRECI]', '[OVRRECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                          V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[OPATRECI]', '[OPATRECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                          V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[OSERECI]', '[OSERECI'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                          V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[MODREC]', '[MODREC'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                          V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[FABREC]', '[FABREC'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                          V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[GRUPOREC]', '[GRUPOREC'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                          V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[CLASREC]', '[CLASREC'||C_LINERC.IND||C_LINE.NRITEMOCONTRATO||']');
                                          IF V_RESULTREC = 'C' THEN
                                             V_CLOBINIREC := V_CLOBINIREC||V_CLOBLINREC1||'</tr>';
                                          ELSE
                                             V_CLOBINIREC := V_CLOBINIREC||V_CLOBLINREC1;
                                          END IF;
                                       END LOOP;
                                    END IF;
                                END LOOP;
                                V_TXDOCUMENT := V_CLOBINIREC||V_CLOBFINREC;
                                V_TXDOCUMENT := REPLACE(V_TXDOCUMENT, V_CLOBLINREC, '');
                       --      END IF;

                      V_TOTITEM := 0;
                      V_TOTPERIT := 0;
                      V_TOTITEM_A := 0;
                      V_TOTPERIT_A := 0;
                      V_TOTITEM_O := 0;
                      V_TOTPERIT_O := 0;

                      FOR C_ITENS IN (SELECT TO_CHAR(ROWNUM) AS IND, DECODE(IT.CDSERVORCA, NULL, IT.CDPRODUTO, IT.CDSERVORCA) AS CODITEM, NVL(IT.QTITEMCONTRATO,0) AS QTITEMCONTRATO, IT.IDFATURACONTR,
                                             IT.NRITEMOCONTRATO, IT.NMITEMCONTRATO, NVL(IT.QTMEDIDACONTR,0) AS QTMEDIDACONTR, TM.DSTPMEDIDASERV, IT.IDUNIDTEMPOSERV, NVL(IT.QTTEMPOSERV, 0) AS QTTEMPOSERV,
                                             NVL(IT.VRUNITITEMCONT,0) AS VRUNITITEMCONT, NVL(IT.VRTOTITEMCONT,0) AS VRTOTITEMCONT, NVL((IT.VRTOTITEMCONT),0) TOTPERIODO,
                                             NVL(LTRIM(TO_CHAR(IT.VRUNITITEMCONT,'999G999G999G990D99')),0) AS VRUNITITEMCONT2, NVL(LTRIM(TO_CHAR(IT.VRTOTITEMCONT,'999G999G999G990D99')),0) AS VRTOTITEMCONT2,
                                             NVL((LTRIM(TO_CHAR(IT.VRTOTITEMCONT,'999G999G999G990D99'))),0) TOTPERIODO2, MP.DSMODPGTO
                                        FROM FCT_ITEMCONTRATO IT, FCT_TIPOMEDIDASERV TM, MODPGTO MP
                                       WHERE IT.NRSEQCONTRATO = P_NRSEQCONTR
                                         AND IT.NRORG = V_NRORG
                                         AND IT.IDITEMATIVO = 'S'
                                         AND IT.IDADICITEMCONT = 'N'
                                         AND TM.CDTPMEDIDASERV(+) = IT.CDTPMEDIDASERV
                                         AND TM.NRORG(+) = IT.NRORG
                                         AND MP.CDMODPGTO(+) = IT.CDMODPGTO
                                         AND MP.NRORG(+) = IT.NRORG) LOOP

                          V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[IND'||C_ITENS.IND||']', NVL(C_ITENS.IND, ''));
                          V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[COD'||C_ITENS.IND||']', NVL(C_ITENS.CODITEM, ''));
                          V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[QNT'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.QTITEMCONTRATO, 0)));
                          V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[DESC'||C_ITENS.IND||']', NVL(C_ITENS.NMITEMCONTRATO, ''));
                          V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[QTMED'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.QTMEDIDACONTR, 0)));
                          V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[MEDIDA'||C_ITENS.IND||']', NVL(C_ITENS.DSTPMEDIDASERV, ''));
                          V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[TPSERV'||C_ITENS.IND||']', NVL(C_ITENS.IDUNIDTEMPOSERV, ''));
                          V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[QTTPSERV'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.QTTEMPOSERV, 0)));
                          V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[MODPAG'||C_ITENS.IND||']', NVL(C_ITENS.DSMODPGTO, ''));
                          V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[VRUNIT'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.VRUNITITEMCONT2, 0)));
                          V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[VRTOT'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.VRTOTITEMCONT2, 0)));
                          V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[TOTPER'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.TOTPERIODO2, 0)));

                          V_TOTITEM := V_TOTITEM + NVL(C_ITENS.VRTOTITEMCONT,0);
                          V_TOTPERIT := V_TOTPERIT + NVL(C_ITENS.TOTPERIODO,0);

                          IF C_ITENS.IDFATURACONTR = 'A' THEN
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[AIND'||C_ITENS.IND||']', NVL(C_ITENS.IND, ''));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ACOD'||C_ITENS.IND||']', NVL(C_ITENS.CODITEM, ''));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[AQNT'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.QTITEMCONTRATO, 0)));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ADESC'||C_ITENS.IND||']', NVL(C_ITENS.NMITEMCONTRATO, ''));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[AQTMED'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.QTMEDIDACONTR, 0)));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[AMEDIDA'||C_ITENS.IND||']', NVL(C_ITENS.DSTPMEDIDASERV, ''));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ATPSERV'||C_ITENS.IND||']', NVL(C_ITENS.IDUNIDTEMPOSERV, ''));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[AQTTPSERV'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.QTTEMPOSERV, 0)));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[AMODPAG'||C_ITENS.IND||']', NVL(C_ITENS.DSMODPGTO, ''));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[AVRUNIT'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.VRUNITITEMCONT2, 0)));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[AVRTOT'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.VRTOTITEMCONT2, 0)));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ATOTPER'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.TOTPERIODO2, 0)));

                             V_TOTITEM_A := V_TOTITEM_A+ NVL(C_ITENS.VRTOTITEMCONT,0);
                             V_TOTPERIT_A := V_TOTPERIT_A + NVL(C_ITENS.TOTPERIODO,0);
                          END IF;

                          IF C_ITENS.IDFATURACONTR = 'O' THEN
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OIND'||C_ITENS.IND||']', NVL(C_ITENS.IND, ''));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OCOD'||C_ITENS.IND||']', NVL(C_ITENS.CODITEM, ''));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OQNT'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.QTITEMCONTRATO, 0)));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ODESC'||C_ITENS.IND||']', NVL(C_ITENS.NMITEMCONTRATO, ''));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OQTMED'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.QTMEDIDACONTR, 0)));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OMEDIDA'||C_ITENS.IND||']', NVL(C_ITENS.DSTPMEDIDASERV, ''));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OTPSERV'||C_ITENS.IND||']', NVL(C_ITENS.IDUNIDTEMPOSERV, ''));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OQTTPSERV'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.QTTEMPOSERV, 0)));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OMODPAG'||C_ITENS.IND||']', NVL(C_ITENS.DSMODPGTO, ''));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OVRUNIT'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.VRUNITITEMCONT2, 0)));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OVRTOT'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.VRTOTITEMCONT2, 0)));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OTOTPER'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.TOTPERIODO2, 0)));

                             V_TOTITEM_O  := V_TOTITEM_O + NVL(C_ITENS.VRTOTITEMCONT,0);
                             V_TOTPERIT_O := V_TOTPERIT_O + NVL(C_ITENS.TOTPERIODO,0);
                          END IF;

                          /* Verifica se existem recursos no contrato */

                          SELECT COUNT(*)
                            INTO V_RECURS
                            FROM FCT_RECURSOSCONTR
                           WHERE NRSEQCONTRATO = V_NRSEQCONTR
                             AND NRORG = V_NRORG
                             AND NRITEMOCONTRATO = C_ITENS.NRITEMOCONTRATO
                             AND IDRECURATIV = 'S';

                          IF V_RECURS > 0 THEN

                             /* Substitui Mnemonicos recursos do contrato POR ITEM */
                             FOR C_RECUR IN (SELECT TO_CHAR(ROWNUM) AS IND, NVL(LTRIM(to_char(RC.VRPERCRECUCONT,'999G999G999G990D99')),0) AS VRPERCRECUCONT,
                                                    NVL(LTRIM(to_char(RC.VRRECUCONT,'999G999G999G990D99')),0) AS VRRECUCONT, BA.CDPATRIBEMATIVO, BA.CDSERIEENTRBEM,
                                                    DECODE(IDTPRECURSO, 'P', RC.CDPRODUTO, DECODE(IDTPRECURSO, 'F', RC.NRPARCNEGOCIO, DECODE(IDTPRECURSO, 'A', RC.CDBEMATIVO , NULL))) AS CODRECUR,
                                                    DECODE(IDTPRECURSO, 'P', PD.NMPRODUTO, DECODE(IDTPRECURSO, 'F', PN.NMPRINCIPALPARC, DECODE(IDTPRECURSO, 'A', BA.NMBEMATIVO , NULL))) AS NMRECUR,
                                                    M.NMMODELO, FA.NMFABRIC, GB.NMGRUPOBEM, CL.NMCLASSIFICABEM
                                               FROM FCT_RECURSOSCONTR RC, PRODUTO PD, PARCNEGOCIO PN, BEMATIVO BA, MODELO M, FABRICANTE FA, GRUPOBEM GB, CLASSIFICABEM CL
                                              WHERE RC.NRSEQCONTRATO = V_NRSEQCONTR
                                                AND RC.NRORG = V_NRORG
                                                AND RC.NRITEMOCONTRATO = C_ITENS.NRITEMOCONTRATO
                                                AND RC.IDRECURATIV = 'S'
                                                AND PD.CDPRODUTO(+) = RC.CDPRODUTO
                                                AND PD.NRORG(+) = RC.NRORG
                                                AND PN.NRPARCNEGOCIO(+) = RC.NRPARCNEGOCIO
                                                AND PN.NRORG(+) = RC.NRORG
                                                AND BA.CDBEMATIVO(+) = RC.CDBEMATIVO
                                                AND BA.CDEMPRESA(+) = RC.CDEMPRESA
                                                AND BA.NRORG(+) = RC.NRORG
                                                AND BA.CDMODELO = M.CDMODELO(+)
                                                AND BA.CDFABRIC = FA.CDFABRIC(+)
                                                AND BA.CDGRUPOBEM = GB.CDGRUPOBEM(+)
                                                AND BA.CDEMPRESA = GB.CDEMPRESA(+)
                                                AND BA.CDCLASSIFICABEM = CL.CDCLASSIFICABEM(+)
                                                AND BA.CDEMPRESA = CL.CDEMPRESA(+)
                                                AND BA.CDGRUPOBEM = CL.CDGRUPOBEM(+)) LOOP

                                V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[INDRECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.IND, ''));

                                V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[MODREC'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.NMMODELO, ''));
                                V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[FABREC'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.NMFABRIC, ''));
                                V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CLASREC'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.NMCLASSIFICABEM, ''));
                                V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[GRUPOREC'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.NMGRUPOBEM, ''));

                                V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CDRECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.CODRECUR, ''));
                                V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[NMRECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.NMRECUR, ''));
                                V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[VRPRRECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', TO_CHAR(NVL(C_RECUR.VRPERCRECUCONT, 0)));
                                V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[VRRECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', TO_CHAR(NVL(C_RECUR.VRRECUCONT, 0)));
                                V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[PATRECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.CDPATRIBEMATIVO, ''));
                                V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[SERECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.CDSERIEENTRBEM, ''));

                                V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[VRPRRECI'||C_ITENS.IND||C_ITENS.NRITEMOCONTRATO||']', TO_CHAR(NVL(C_RECUR.VRPERCRECUCONT, 0)));
                                V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[VRRECI'||C_ITENS.IND||C_ITENS.NRITEMOCONTRATO||']', TO_CHAR(NVL(C_RECUR.VRRECUCONT, 0)));

                                IF C_ITENS.IDFATURACONTR = 'A' THEN
                                   V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[AINDRECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.IND, ''));
                                   V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ACDRECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.CODRECUR, ''));
                                   V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ANMRECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.NMRECUR, ''));
                                   V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[AVRPRRECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', TO_CHAR(NVL(C_RECUR.VRPERCRECUCONT, 0)));
                                   V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[AVRRECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', TO_CHAR(NVL(C_RECUR.VRRECUCONT, 0)));
                                   V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[APATRECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.CDPATRIBEMATIVO, ''));
                                   V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ASERECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.CDSERIEENTRBEM, ''));

                                   V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[AVRPRRECI'||C_ITENS.IND||C_ITENS.NRITEMOCONTRATO||']', TO_CHAR(NVL(C_RECUR.VRPERCRECUCONT, 0)));
                                   V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[AVRRECI'||C_ITENS.IND||C_ITENS.NRITEMOCONTRATO||']', TO_CHAR(NVL(C_RECUR.VRRECUCONT, 0)));

                                END IF;

                                IF C_ITENS.IDFATURACONTR = 'O' THEN
                                   V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OINDRECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.IND, ''));
                                   V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OCDRECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.CODRECUR, ''));
                                   V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ONMRECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.NMRECUR, ''));
                                   V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OVRPRRECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', TO_CHAR(NVL(C_RECUR.VRPERCRECUCONT, 0)));
                                   V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OVRRECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', TO_CHAR(NVL(C_RECUR.VRRECUCONT, 0)));
                                   V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OPATRECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.CDPATRIBEMATIVO, ''));
                                   V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OSERECI'||C_RECUR.IND||C_ITENS.NRITEMOCONTRATO||']', NVL(C_RECUR.CDSERIEENTRBEM, ''));

                                   V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OVRPRRECI'||C_ITENS.IND||C_ITENS.NRITEMOCONTRATO||']', TO_CHAR(NVL(C_RECUR.VRPERCRECUCONT, 0)));
                                   V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OVRRECI'||C_ITENS.IND||C_ITENS.NRITEMOCONTRATO||']', TO_CHAR(NVL(C_RECUR.VRRECUCONT, 0)));

                                END IF;
                             END LOOP;
                          ELSE
                          /* Caso nÆo hajam recursos no item, recebe 0*/

                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[VRPRRECI'||C_ITENS.IND||C_ITENS.NRITEMOCONTRATO||']', 0);
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[VRRECI'||C_ITENS.IND||C_ITENS.NRITEMOCONTRATO||']', 0);

                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[AVRPRRECI'||C_ITENS.IND||C_ITENS.NRITEMOCONTRATO||']', 0);
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[AVRRECI'||C_ITENS.IND||C_ITENS.NRITEMOCONTRATO||']', 0);

                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OVRPRRECI'||C_ITENS.IND||C_ITENS.NRITEMOCONTRATO||']', 0);
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OVRRECI'||C_ITENS.IND||C_ITENS.NRITEMOCONTRATO||']', 0);
                          END IF;

                      END LOOP;

                      V_TOTITEM2 := NVL(LTRIM(to_char(V_TOTITEM,'999G999G999G990D99')),0);
                      V_TOTPERIT2 := NVL(LTRIM(to_char(V_TOTPERIT,'999G999G999G990D99')),0);
                      V_TOTITEM_A2 := NVL(LTRIM(to_char(V_TOTITEM_A,'999G999G999G990D99')),0);
                      V_TOTPERIT_A2 := NVL(LTRIM(to_char(V_TOTPERIT_A,'999G999G999G990D99')),0);
                      V_TOTITEM_O2 := NVL(LTRIM(to_char(V_TOTITEM_O,'999G999G999G990D99')),0);
                      V_TOTPERIT_O2 := NVL(LTRIM(to_char(V_TOTPERIT_O,'999G999G999G990D99')),0);

                      /*Totais Item (Todos)*/
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[TOTITEM]', V_TOTITEM2);
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[TOTPERIT]', V_TOTPERIT2);
                      /*Totais Item (Apura‡Æo de Faturamento)*/
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ATOTITEM]', V_TOTITEM_A2);
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ATOTPERIT]', V_TOTPERIT_A2);
                      /*Totais Item (Modalidade de Pagamento)*/
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OTOTITEM]', V_TOTITEM_O2);
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OTOTPERIT]', V_TOTPERIT_O2);
                      /* Substitui Mnemonicos recursos do contrato TODOS DO CONTRATO*/

                      SELECT COUNT(*)
                        INTO V_RECURS
                        FROM FCT_RECURSOSCONTR
                       WHERE NRSEQCONTRATO = V_NRSEQCONTR
                         AND NRORG = V_NRORG
                         AND IDRECURATIV = 'S';

                      IF V_RECURS > 0 THEN
                      /* Caso hajam recursos, eles serÆo replicados*/
                         V_TESTARC1 := INSTR(V_TXDOCUMENT,'<tr',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</tr>', INSTR(V_TXDOCUMENT, '[INDRECT]',1)))*-1);
                         V_TESTARC2 := INSTR(V_TXDOCUMENT,'<p',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</p>', INSTR(V_TXDOCUMENT, '[INDRECT]',1)))*-1);
                         IF V_TESTARC1 <> 0 THEN
                            IF V_TESTARC1 < V_TESTARC2 THEN
                               V_POSINIREC := INSTR(V_TXDOCUMENT,'<tr',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</tr>', INSTR(V_TXDOCUMENT, '[INDRECT]',1)))*-1);
                               V_POSFINREC := INSTR(V_TXDOCUMENT,'</tr>',INSTR(V_TXDOCUMENT,'[INDRECT]',1));
                            ELSE
                               V_POSINIREC := INSTR(V_TXDOCUMENT,'<p',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</p>', INSTR(V_TXDOCUMENT, '[INDRECT]',1)))*-1);
                               V_POSFINREC := INSTR(V_TXDOCUMENT,'</p>',INSTR(V_TXDOCUMENT,'[INDRECT]',1));
                            END IF;
                            V_CLOBINIREC := SUBSTR(V_TXDOCUMENT,1,V_POSFINREC-1);
                            V_CLOBLINREC :=  SUBSTR(V_TXDOCUMENT,V_POSINIREC,V_POSFINREC-V_POSINIREC);
                            V_CLOBFINREC :=  SUBSTR(V_TXDOCUMENT,V_POSFINREC,length(V_TXDOCUMENT));

                            FOR C_LINERC IN (SELECT TO_CHAR(ROWNUM) AS IND,  NRSEQRECUCONT
                                               FROM FCT_RECURSOSCONTR
                                              WHERE NRSEQCONTRATO = V_NRSEQCONTR
                                                AND NRORG = V_NRORG
                                                AND IDRECURATIV = 'S') LOOP

                               V_CLOBLINREC1 := REPLACE(V_CLOBLINREC,'[INDRECT]','[INDRECT'||C_LINERC.IND||']');
                               V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[CDRECT]','[CDRECT'||C_LINERC.IND||']');
                               V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[NMRECT]', '[NMRECT'||C_LINERC.IND||']');
                               V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[VRPRRECT]', '[VRPRRECT'||C_LINERC.IND||']');
                               V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[VRRECT]', '[VRRECT'||C_LINERC.IND||']');
                               V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[PATRECT]', '[PATRECT'||C_LINERC.IND||']');
                               V_CLOBLINREC1 := REPLACE(V_CLOBLINREC1,'[SERECT]', '[SERECT'||C_LINERC.IND||']');
                               V_CLOBINIREC := V_CLOBINIREC||V_CLOBLINREC1;
                            END LOOP;
                            V_TXDOCUMENT := V_CLOBINIREC||V_CLOBFINREC;
                            V_TXDOCUMENT := REPLACE(V_TXDOCUMENT, V_CLOBLINREC, '');

                            FOR C_RECUR IN (SELECT TO_CHAR(ROWNUM) AS IND, NVL(LTRIM(to_char(RC.VRPERCRECUCONT,'999G999G999G990D99')),0) AS VRPERCRECUCONT,
                                                   NVL(LTRIM(to_char(RC.VRRECUCONT,'999G999G999G990D99')),0) AS VRRECUCONT, BA.CDPATRIBEMATIVO, BA.CDSERIEENTRBEM,
                                                   DECODE(IDTPRECURSO, 'P', RC.CDPRODUTO, DECODE(IDTPRECURSO, 'F', RC.NRPARCNEGOCIO, DECODE(IDTPRECURSO, 'A', RC.CDBEMATIVO , NULL))) AS CODRECUR,
                                                   DECODE(IDTPRECURSO, 'P', PD.NMPRODUTO, DECODE(IDTPRECURSO, 'F', PN.NMPRINCIPALPARC, DECODE(IDTPRECURSO, 'A', BA.NMBEMATIVO , NULL))) AS NMRECUR,
                                                   M.NMMODELO, FA.NMFABRIC, GB.NMGRUPOBEM, CL.NMCLASSIFICABEM
                                              FROM FCT_RECURSOSCONTR RC, PRODUTO PD, PARCNEGOCIO PN, BEMATIVO BA, MODELO M, FABRICANTE FA, GRUPOBEM GB, CLASSIFICABEM CL
                                             WHERE RC.NRSEQCONTRATO = V_NRSEQCONTR
                                               AND RC.IDRECURATIV = 'S'
                                               AND RC.NRORG = V_NRORG
                                               AND PD.CDPRODUTO(+) = RC.CDPRODUTO
                                               AND PD.NRORG(+) = RC.NRORG
                                               AND PN.NRPARCNEGOCIO(+) = RC.NRPARCNEGOCIO
                                               AND PN.NRORG(+) = RC.NRORG
                                               AND BA.CDBEMATIVO(+) = RC.CDBEMATIVO
                                               AND BA.CDEMPRESA(+) = RC.CDEMPRESA
                                               AND BA.NRORG(+) = RC.NRORG
                                               AND BA.CDMODELO = M.CDMODELO(+)
                                               AND BA.CDFABRIC = FA.CDFABRIC(+)
                                               AND BA.CDGRUPOBEM = GB.CDGRUPOBEM(+)
                                               AND BA.CDEMPRESA = GB.CDEMPRESA(+)
                                               AND BA.CDCLASSIFICABEM = CL.CDCLASSIFICABEM(+)
                                               AND BA.CDEMPRESA = CL.CDEMPRESA(+)
                                               AND BA.CDGRUPOBEM = CL.CDGRUPOBEM(+)) LOOP

                               V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[INDRECT'||C_RECUR.IND||']', NVL(C_RECUR.IND, ''));
                               V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CDRECT'||C_RECUR.IND||']', NVL(C_RECUR.CODRECUR, ''));
                               V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[NMRECT'||C_RECUR.IND||']', NVL(C_RECUR.NMRECUR, ''));
                               V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[VRPRRECT'||C_RECUR.IND||']', TO_CHAR(NVL(C_RECUR.VRPERCRECUCONT, 0)));
                               V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[VRRECT'||C_RECUR.IND||']', TO_CHAR(NVL(C_RECUR.VRRECUCONT, 0)));
                               V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[PATRECT'||C_RECUR.IND||']', NVL(C_RECUR.CDPATRIBEMATIVO, ''));
                               V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[SERECT'||C_RECUR.IND||']', NVL(C_RECUR.CDSERIEENTRBEM, ''));
                               V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[MODREC'||C_RECUR.IND||']', NVL(C_RECUR.NMMODELO, ''));
                               V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[FABREC'||C_RECUR.IND||']', NVL(C_RECUR.NMFABRIC, ''));
                               V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[CLASREC'||C_RECUR.IND||']', NVL(C_RECUR.NMCLASSIFICABEM, ''));
                                V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[GRUPOREC'||C_RECUR.IND||']', NVL(C_RECUR.NMGRUPOBEM, ''));
                            END LOOP;
                         END IF;
                      END IF;
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[LN]','');
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[/LN]','');
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'<br><br>','');
                      UPDATE FCT_CONTRATODOC
                         SET TXMODELODOCORCA = V_TXDOCUMENT
                       WHERE NRSEQCONTDOC = C_DOC.NRSEQCONTDOC
                         AND NRSEQCONTRATO = V_NRSEQCONTR
                         AND NRORG = V_NRORG;
                   END IF;
           END LOOP;
       END IF;
     ELSE
       /* VERIFICAR SE O OR€AMENTO UTILIZA OU NÇO HIERARQUIA DE SERVI€OS  */

         BEGIN
             SELECT NVL(M.IDUTILIZAHIERSERV,'NAO')
               INTO V_IDUTILIZAHIERSERV
               FROM FCT_ORCAMENTO O, FCT_ORCAMENTOMODELO M
              WHERE O.NRSEQORCAMENTO = V_NRSEQCONTR
                AND O.NRORG = V_NRORG
                AND O.NRORCAMENTOMODELO = M.NRORCAMENTOMODELO
                AND O.NRORG = M.NRORG;
         EXCEPTION
             WHEN NO_DATA_FOUND THEN
                 V_IDUTILIZAHIERSERV := 'NAO';
         END;

         /* PRIMEIRO PEGA O DOCUMENTO E O CODIGO DO MODELO DE DOCUMENTO */
         FOR C_DOC IN (SELECT NRSEQMODDOCFCT, TXMODELOORCADOC, NRSEQORCADOC
                          FROM FCT_ORCAMENTODOC
                         WHERE (INSTR('_'||V_NRSEQDOCCONT||'_','_'||NRSEQORCADOC||'_')>0)
                           AND (NRSEQORCAMENTO = V_NRSEQCONTR)
                           AND (NRORG = V_NRORG)
         )LOOP
               /*
               BEGIN
                SELECT NRSEQMODDOCFCT, TXMODELOORCADOC
                  INTO V_NRSEQMODDOCFCT, V_TXDOCUMENT
                  FROM FCT_ORCAMENTODOC
                 WHERE NRSEQORCADOC = V_NRSEQDOCCONT
                   AND NRSEQORCAMENTO = V_NRSEQCONTR;

              EXCEPTION
                WHEN NO_DATA_FOUND THEN
                   V_NRSEQMODDOCFCT := NULL;
                   V_TXDOCUMENT     := NULL;
              END;
              */

              /* VERIFICA SE EXISTE O MODELO DE DOCUMENTO PARA SUBSTITUIR O TEXTO DO DOCUMENTO */
             IF C_DOC.NRSEQMODDOCFCT IS NOT NULL THEN

                 BEGIN
                     SELECT TXMODELODOCORCA
                       INTO V_TXDOCUMENT
                       FROM FCT_MODELODOC
                      WHERE NRSEQMODDOCFCT = C_DOC.NRSEQMODDOCFCT
                        AND NRORG = V_NRORG;
                 EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                         V_TXDOCUMENT := NULL;
                 END;
             ELSE
                 V_TXDOCUMENT := C_DOC.TXMODELOORCADOC;
             END IF;

             IF V_TXDOCUMENT IS NOT NULL THEN
                 /* ==== Recebe dados principais do Or‡amento ==== */
                 BEGIN
                     SELECT NRPARCNEGOCIO, CDFILIAL, NRORCAMENTO, NRVERSAOORCAMENTO, DTREFORCAMENTO, CDPRECBASESERV,
                            DECODE(IDFATURAORCA, 'A', 'APURA€ÇO DE FATURAMENTO', 'MODALIDADE DE PAGAMENTO') AS IDFATURAORCA,
                            CDMODPGTO, NRPARCINTERLOCUTOR, DSREFERENCIAORCA, QTDIASVALIDADE, DTVALIDADE,
                            QTDIASCONTRATO, DTINICIOCONTRATO, DTFIMCONTRATO, DSEMAIL, NRTELEFONE, DSOBSERVACAOORCA, VRORCAMENTO
                       INTO V_CLIEORC, V_ORFILI, V_ORCOD, V_ORVERS, V_ORDTREF, V_PRECBOR,
                            V_ORFATUR, V_MODPAGORCA, V_INTEROR, V_ORREFE, V_ORDIASVD, V_ORDTVAL,
                            V_QTDIASCT, V_ORDTINICT, V_ORDTFINCT, V_OREMAIL, V_ORTEL, V_OROBS, V_ORVRORC
                       FROM FCT_ORCAMENTO OC
                      WHERE NRSEQORCAMENTO = V_NRSEQCONTR
                        AND NRORG = V_NRORG;
                 EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                         V_CLIEORC   := NULL;
                         V_ORFILI    := NULL;
                         V_ORCOD     := NULL;
                         V_ORVERS    := NULL;
                         V_ORDTREF   := NULL;
                         V_PRECBOR   := NULL;
                         V_ORFATUR   := NULL;
                         V_MODPAGORC := NULL;
                         V_INTEROR   := NULL;
                         V_ORREFE    := NULL;
                         V_ORDIASVD  := NULL;
                         V_ORDTVAL   := NULL;
                         V_QTDIASCT  := NULL;
                         V_ORDTINICT := NULL;
                         V_ORDTFINCT := NULL;
                         V_OREMAIL   := NULL;
                         V_ORTEL     := NULL;
                         V_OROBS     := NULL;
                         V_ORVRORC   := NULL;
                     END;
                 END;

                 /*VERIFICAR QUAL SERµ O MNEMONICO UTILIZADO OU APENAS O TIPO DE HIERARQUIA OU SEUS FILHOS*/
                 IF(V_IDUTILIZAHIERSERV = 'SIM') THEN
                     IF(INSTR(V_TXDOCUMENT, '[TPHIERQ:')>0) THEN
                         V_TPHIERARQ := 2;
                         BEGIN
                             V_MNEHIERARQ := '[TPHIERQ:'||SUBSTR(V_TXDOCUMENT,INSTR(V_TXDOCUMENT,'[TPHIERQ:')+9, INSTR(V_TXDOCUMENT,']',INSTR(V_TXDOCUMENT,'[TPHIERQ:')) - (INSTR(V_TXDOCUMENT,'[TPHIERQ:')+9))||']';
                             V_TIPOHIERARQUIA := TO_NUMBER(SUBSTR(V_TXDOCUMENT,INSTR(V_TXDOCUMENT,'[TPHIERQ:')+9, INSTR(V_TXDOCUMENT,']',INSTR(V_TXDOCUMENT,'[TPHIERQ:')) - (INSTR(V_TXDOCUMENT,'[TPHIERQ:')+9)));
                         EXCEPTION
                             WHEN INVALID_NUMBER THEN
                                 V_TIPOHIERARQUIA := NULL;
                          --   V_IDUTILIZAHIERSERV := 'NAO';
                         END;
                     ELSE
                         IF(INSTR(V_TXDOCUMENT, '[TPHIERQINI:')>0) THEN
                             V_TPHIERARQ := 1;
                             BEGIN
                                 V_MNEHIERARQ := '[TPHIERQINI:'||SUBSTR(V_TXDOCUMENT,INSTR(V_TXDOCUMENT,'[TPHIERQINI:')+12, INSTR(V_TXDOCUMENT,']',INSTR(V_TXDOCUMENT,'[TPHIERQINI:')) - (INSTR(V_TXDOCUMENT,'[TPHIERQINI:')+12))||']';
                                 V_TIPOHIERARQUIA := TO_NUMBER(SUBSTR(V_TXDOCUMENT,INSTR(V_TXDOCUMENT,'[TPHIERQINI:')+12, INSTR(V_TXDOCUMENT,']',INSTR(V_TXDOCUMENT,'[TPHIERQINI:')) - (INSTR(V_TXDOCUMENT,'[TPHIERQINI:')+12)));
                             EXCEPTION
                                 WHEN INVALID_NUMBER THEN
                                     V_TIPOHIERARQUIA := NULL;
                                  --   V_IDUTILIZAHIERSERV := 'NAO';
                             END;
                          -- ELSE
                          --    V_IDUTILIZAHIERSERV := 'NAO';
                         END IF;
                     END IF;

                     IF(V_TIPOHIERARQUIA IS NULL) THEN
                         V_IDUTILIZAHIERSERV := 'NAO';
                     END IF;
                 END IF;

                 IF V_MODPAGORCA IS NOT NULL THEN
                     BEGIN
                         SELECT DSMODPGTO
                           INTO V_ORMODPAG
                           FROM MODPGTO
                          WHERE CDMODPGTO = V_MODPAGORCA
                            AND NRORG = V_NRORG;
                     EXCEPTION
                         WHEN NO_DATA_FOUND THEN
                             V_ORMODPAG := NULL;
                     END;
                 END IF;

                 IF V_CLIEORC IS NOT NULL THEN
                     BEGIN
                         SELECT NMPRINCIPALPARC
                           INTO V_ORCLIE
                           FROM PARCNEGOCIO
                          WHERE NRPARCNEGOCIO = V_CLIEORC
                            AND NRORG = V_NRORG;
                     EXCEPTION
                         WHEN NO_DATA_FOUND THEN
                             V_ORCLIE := NULL;
                     END;
                 END IF;

                 IF V_INTEROR IS NOT NULL THEN
                     BEGIN
                         SELECT NMPRINCIPALPARC
                           INTO V_ORINTER
                           FROM PARCNEGOCIO
                          WHERE NRPARCNEGOCIO = V_INTEROR
                            AND NRORG = V_NRORG;
                     EXCEPTION
                         WHEN NO_DATA_FOUND THEN
                             V_ORINTER := NULL;
                     END;
                 ELSE
                     BEGIN
                         SELECT DSINTERORCA
                           INTO V_ORINTER
                           FROM FCT_ORCAMENTO
                          WHERE NRSEQORCAMENTO = V_NRSEQCONTR
                            AND NRORG = V_NRORG;
                     EXCEPTION
                         WHEN NO_DATA_FOUND THEN
                             V_ORINTER := NULL;
                     END;
                 END IF;

                 IF V_PRECBOR IS NOT NULL THEN
                     BEGIN
                         SELECT DSPRECBASESERV
                           INTO V_ORPRECB
                           FROM FCT_PRECBASESERV
                         WHERE CDPRECBASESERV = V_PRECBOR
                           AND NRORG = V_NRORG;
                     EXCEPTION
                         WHEN NO_DATA_FOUND THEN
                             V_ORPRECB := NULL;
                     END;
                 END IF;

                 /* ==== Replace dados principais do Or‡amento ==== */

                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORCLIE]', NVL(V_ORCLIE, ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORFILI]', NVL(V_ORFILI,''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORCOD]', NVL(V_ORCOD, ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORVERS]', NVL(V_ORVERS, ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORDTREF]', NVL(TO_CHAR(V_ORDTREF, 'DD/MM/YYYY'), ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORPRECBS]', NVL(V_ORPRECB, ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORFATUR]', NVL(V_ORFATUR, ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORMODPAG]', NVL(V_ORMODPAG, ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORINTER]', NVL(V_ORINTER, ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORREFE]', REPLACE(NVL(V_ORREFE, ''),chr(10),'<br>'));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORDIASVD]', NVL(TO_CHAR(V_ORDIASVD), ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORDTVAL]', NVL(TO_CHAR(V_ORDTVAL, 'DD/MM/YYYY'), ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[QTDIASCT]', NVL(TO_CHAR(V_QTDIASCT), ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORDTINICT]', NVL(TO_CHAR(V_ORDTINICT, 'DD/MM/YYYY'), ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORDTFINCT]', NVL(TO_CHAR(V_ORDTFINCT, 'DD/MM/YYYY'), ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OREMAIL]', NVL(V_OREMAIL, ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORTEL]', NVL(V_ORTEL,''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OROBS]', REPLACE(NVL(V_OROBS,''),chr(10),'<br>'));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORVRORC]', NVL(TO_CHAR(V_ORVRORC), ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[DTHOJE]', TO_CHAR(SYSDATE,'DD/MM/YYYY'));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[DIAAT]', TO_CHAR(SYSDATE,'DD'));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[MESAT]', TO_CHAR(SYSDATE,'MM'));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ANOAT]', TO_CHAR(SYSDATE,'YYYY'));

                 CASE TO_CHAR(SYSDATE,'MM')
                    WHEN '01' THEN V_NMMESAT := 'Janeiro';
                    WHEN '02' THEN V_NMMESAT := 'Fevereiro';
                    WHEN '03' THEN V_NMMESAT := 'Mar‡o';
                    WHEN '04' THEN V_NMMESAT := 'Abril';
                    WHEN '05' THEN V_NMMESAT := 'Maio';
                    WHEN '06' THEN V_NMMESAT := 'Junho';
                    WHEN '07' THEN V_NMMESAT := 'Julho';
                    WHEN '08' THEN V_NMMESAT := 'Agosto';
                    WHEN '09' THEN V_NMMESAT := 'Setembro';
                    WHEN '10' THEN V_NMMESAT := 'Outubro';
                    WHEN '11' THEN V_NMMESAT := 'Novembro';
                    WHEN '12' THEN V_NMMESAT := 'Dezembro';
                    ELSE V_NMMESAT := '';
                 END CASE;

                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[NMMESAT]', V_NMMESAT);


                 /* ==== Busca dados do Cliente do Or‡amento ==== */

                 IF V_CLIEORC IS NOT NULL THEN
                    BEGIN
                       SELECT PCN.NRINSCRICAOPARC, PCN.NMPRINCIPALPARC, CLI.NRINSESTCLIE  AS INSCMUNI
                         INTO V_ORPARC, V_ORNMPARC, V_ORIE
                         FROM PARCNEGOCIO PCN, CLIENTE CLI
                        WHERE PCN.NRPARCNEGOCIO = V_CLIEORC
                          AND PCN.NRORG = V_NRORG
                          AND CLI.NRPARCNEGOCIO = PCN.NRPARCNEGOCIO
                          AND CLI.NRORG = PCN.NRORG;
                    EXCEPTION
                       WHEN NO_DATA_FOUND THEN BEGIN
                          V_ORPARC := NULL;
                          V_ORNMPARC := NULL;
                          V_ORIE := NULL;
                       END;
                    END;

                    BEGIN
                       SELECT DISTINCT (ENP.DSENDERECO||' - '||ENP.NMBAIRROENDERECO||' - '||MN.NMMUNICIPIO||' - '||ENP.SGESTADO) AS ENDERECO,
                              ENP.DSENDERECO, MN.NMMUNICIPIO, ENP.NMBAIRROENDERECO, ENP.SGESTADO, ENP.NRCEPENDERECO
                         INTO V_ORENDE, V_ORDSEND, V_ORMUNIC, V_ORBAIRRO, V_ORESTADO, V_ORCEP
                         FROM ENDERECOPARC ENP, MUNICIPIO MN
                        WHERE ENP.NRPARCNEGOCIO = V_CLIEORC
                          AND ENP.NRORG = V_NRORG
                          AND MN.CDMUNICIPIO = ENP.CDMUNICIPIO
                          AND MN.CDPAIS = ENP.CDPAIS
                          AND MN.SGESTADO = ENP.SGESTADO
                          AND ENP.CDTIPOENDERECO = 'PRINCIPAL';
                    EXCEPTION
                       WHEN NO_DATA_FOUND THEN BEGIN
                          V_ORENDE := NULL;
                          V_ORDSEND := NULL;
                          V_ORMUNIC := NULL;
                          V_ORBAIRRO := NULL;
                          V_ORESTADO := NULL;
                          V_ORCEP := NULL;
                       END;
                    END;

                    /* -- Endere‡o de Entrega --*/
                    BEGIN
                       SELECT DISTINCT (ENP.DSENDERECO||' - '||ENP.NMBAIRROENDERECO||' - '||MN.NMMUNICIPIO||' - '||ENP.SGESTADO) AS ENDERECO,
                              ENP.DSENDERECO, MN.NMMUNICIPIO, ENP.NMBAIRROENDERECO, ENP.SGESTADO, ENP.NRCEPENDERECO
                         INTO V_ORENDE2, V_ORDSEND2, V_ORMUNIC2, V_ORBAIRRO2, V_ORESTADO2, V_ORCEP2
                         FROM FCT_ENDERECOORCA ENP, MUNICIPIO MN
                        WHERE ENP.NRSEQORCAMENTO = V_NRSEQCONTR
                          AND ENP.NRORG = V_NRORG
                          AND MN.CDMUNICIPIO(+) = ENP.CDMUNICIPIO
                          AND MN.CDPAIS(+) = ENP.CDPAIS
                          AND MN.SGESTADO(+) = ENP.SGESTADO
                          AND ENP.CDTIPOENDERECO = 'ENTREGA';
                    EXCEPTION
                       WHEN NO_DATA_FOUND THEN BEGIN
                          V_ORENDE2 := NULL;
                          V_ORDSEND2 := NULL;
                          V_ORMUNIC2 := NULL;
                          V_ORBAIRRO2 := NULL;
                          V_ORESTADO2 := NULL;
                          V_ORCEP2 := NULL;
                       END;
                    END;
                 END IF;

                 /* ==== Replace dados do Cliente do Or‡amento ==== */

                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORPARC]', NVL(V_ORPARC, ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORNMPARC]', NVL(V_ORNMPARC, ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORIE]', NVL(V_ORIE, ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORENDE]', NVL(V_ORENDE, ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORDSEND]', NVL(V_ORDSEND, ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORMUNIC]', NVL(V_ORMUNIC, ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORBAIRRO]', NVL(V_ORBAIRRO, ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORESTADO]', NVL(V_ORESTADO, ''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORCEP]', NVL(V_ORCEP, ''));
                 /* -- Endere‡o de Entrega --*/
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORENDE2]', NVL(V_ORENDE2, NVL(V_ORENDE,'')));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORDSEND2]', NVL(V_ORDSEND2, NVL(V_ORDSEND,'')));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORMUNIC2]', NVL(V_ORMUNIC2, NVL(V_ORMUNIC,'')));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORBAIRRO2]', NVL(V_ORBAIRRO2, NVL(V_ORBAIRRO,'')));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORESTADO2]', NVL(V_ORESTADO2, NVL(V_ORESTADO,'')));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[ORCEP2]', NVL(V_ORCEP2, NVL(V_ORCEP,'')));

                 /* ==== Vendedor principal do Or‡amento ==== */

                 BEGIN
                    SELECT PA.NMPRINCIPALPARC, PN.NMPRINCIPALPARC, VE.VRPEPARTICIPAVENDA
                      INTO V_NMVENDEDOR, V_NMGERENTE, V_PARTVENDA
                      FROM FCT_VENDEDORORCA VE, PARCNEGOCIO PA, PARCNEGOCIO PN
                     WHERE VE.NRSEQORCAMENTO =  V_NRSEQCONTR
                       AND VE.NRORG = V_NRORG
                       AND PA.NRPARCNEGOCIO = VE.NRPARCVENDEDOR
                       AND PA.NRORG = VE.NRORG
                       AND PN.NRPARCNEGOCIO(+) = VE.NRPARCGERENTE
                       AND PN.NRORG(+) = VE.NRORG
                       AND VE.IDVENPRIORCA = 'S';
                 EXCEPTION
                    WHEN NO_DATA_FOUND THEN BEGIN
                       V_NMVENDEDOR := NULL;
                       V_NMGERENTE := NULL;
                       V_PARTVENDA := NULL;
                    END;
                 END;

                 /* ==== Replace dados do Vendedor do Or‡amento ==== */
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[NMVENDPRI]', NVL(V_NMVENDEDOR,''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[NMGERENTPRI]', NVL(V_NMGERENTE,''));
                 V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[PARTVENDPRI]', TO_CHAR(NVL(V_PARTVENDA,'')));

                /* ==== Itens do Or‡amento ==== */

                 V_TESTA1 := INSTR(V_TXDOCUMENT,'<tr',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</tr>', INSTR(V_TXDOCUMENT, '[IND]',1)))*-1);
                 V_TESTA2 := INSTR(V_TXDOCUMENT,'<p',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</p>', INSTR(V_TXDOCUMENT, '[IND]',1)))*-1);

                 V_EXISTE_ADICIONAL := INSTR(V_TXDOCUMENT, '[INDAD]');

                 IF V_TESTA1 <> 0 THEN
                         IF V_TESTA1 < V_TESTA2 THEN
                            V_RESULT := 'C';  /* Coluna */
                            
                            IF (V_EXISTE_ADICIONAL <> 0) THEN
                                V_POSINI := INSTR(V_TXDOCUMENT,'<table',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</tr>', INSTR(V_TXDOCUMENT, '[IND]',1)))*-1);
                                V_POSFIN := INSTR(V_TXDOCUMENT,'</table>',INSTR(V_TXDOCUMENT,'[IND]',1));
                            ELSE
                                V_POSINI := INSTR(V_TXDOCUMENT,'<tr',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</tr>', INSTR(V_TXDOCUMENT, '[IND]',1)))*-1);
                                V_POSFIN := INSTR(V_TXDOCUMENT,'</tr>',INSTR(V_TXDOCUMENT,'[IND]',1));
                            END IF;
                         ELSE
                            V_RESULT := 'L'; /* Linha */
                            V_POSINI := INSTR(V_TXDOCUMENT,'<p',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'</p>', INSTR(V_TXDOCUMENT, '[IND]',1)))*-1);
                            V_POSFIN := INSTR(V_TXDOCUMENT,'</p>',INSTR(V_TXDOCUMENT,'[IND]',1));
                         END IF;
                 ELSE
                     V_RESULT := 'L'; /* Linha */
                     V_POSINI := INSTR(V_TXDOCUMENT,'[LN]',(length(V_TXDOCUMENT)-INSTR(V_TXDOCUMENT,'[/LN]', INSTR(V_TXDOCUMENT, '[IND]',1)))*-1);
                     V_POSFIN := INSTR(V_TXDOCUMENT,'[/LN]',INSTR(V_TXDOCUMENT,'[IND]',1));
                 END IF;

                    V_CLOBINICIAL := SUBSTR(V_TXDOCUMENT,1,V_POSFIN-1);
                    V_CLOBLINHA :=  SUBSTR(V_TXDOCUMENT,V_POSINI,V_POSFIN-V_POSINI);
                    V_CLOBFINAL :=  SUBSTR(V_TXDOCUMENT,V_POSFIN,length(V_TXDOCUMENT));

                    V_TSDADOS := 0;

                    FOR C_LINE IN (SELECT TO_CHAR(ROWNUM) AS IND, NRITEMORCAMENTO
                                     FROM FCT_ITEMORCAMENTO LEFT JOIN TRAB_SERVHIERARQUIA
                                       ON FCT_ITEMORCAMENTO.CDSERVORCA = TRAB_SERVHIERARQUIA.CDSERVORCAREL
                                      AND FCT_ITEMORCAMENTO.NRORG = TRAB_SERVHIERARQUIA.NRORG,
                                          (SELECT CDARVORE, CDSERVORCAREL
                                             FROM TRAB_SERVHIERARQUIA
                                            WHERE NRORG = P_NRORG
                                              AND (NRTIPOHIERARQSERV = V_TIPOHIERARQUIA) OR ((V_IDUTILIZAHIERSERV = 'SIM')
                                              AND (V_TIPOHIERARQUIA IS NULL))) TR
                                    WHERE FCT_ITEMORCAMENTO.NRORG = P_NRORG
                                      AND (((TRAB_SERVHIERARQUIA.CDARVORE BETWEEN TR.CDARVORE||'.' AND RPAD(TR.CDARVORE||'.',200,'Z'))
                                      AND (V_IDUTILIZAHIERSERV = 'SIM')
                                      AND (V_TPHIERARQ = '1')
                                      AND (V_TIPOHIERARQUIA IS NOT NULL))
                                       OR ((FCT_ITEMORCAMENTO.NRTIPOHIERARQSERV = V_TIPOHIERARQUIA)
                                      AND (V_IDUTILIZAHIERSERV = 'SIM')
                                      AND (V_TPHIERARQ = '2')
                                      AND (FCT_ITEMORCAMENTO.CDSERVORCA = TR.CDSERVORCAREL))
                                       OR ((V_IDUTILIZAHIERSERV  = 'SIM')
                                      AND (V_TIPOHIERARQUIA IS NULL)
                                      AND (FCT_ITEMORCAMENTO.CDSERVORCA = TR.CDSERVORCAREL)))
                                      AND NRSEQORCAMENTO            = P_NRSEQCONTR
                                      AND ((NRITEMORCAMENTOPAI        IS NULL)
                                      AND (IDADICITEMORC              = 'N'))
                                      AND (FCT_ITEMORCAMENTO.NRORG = V_NRORG)
                                   UNION ALL
                                   SELECT TO_CHAR(ROWNUM) AS IND, NRITEMORCAMENTO
                                     FROM FCT_ITEMORCAMENTO
                                    WHERE V_IDUTILIZAHIERSERV = 'NAO'
                                      AND NRSEQORCAMENTO = P_NRSEQCONTR
                                      AND NRITEMORCAMENTOPAI IS NULL
                                      AND IDADICITEMORC = 'N'
                                      AND NRORG = V_NRORG
                    ) LOOP

                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA,'[IND]','[IND'||C_LINE.IND||']');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[COD]','[COD'||C_LINE.IND||']');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[QNT]', '[QNT'||C_LINE.IND||']');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[DESC]', '[DESC'||C_LINE.IND||']');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[QTMED]', '[QTMED'||C_LINE.IND||']');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[MEDIDA]', '[MEDIDA'||C_LINE.IND||']');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[TPSERV]', '[TPSERV'||C_LINE.IND||']');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[QTTPSERV]', '[QTTPSERV'||C_LINE.IND||']');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[MODPAG]', '[MODPAG'||C_LINE.IND||']');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[VRUNIT]', '[VRUNIT'||C_LINE.IND||']');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[VRTOT]', '[VRTOT'||C_LINE.IND||']');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[TOTPER]', '[TOTPER'||C_LINE.IND||']');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[TPHIERQ]', '[TPHIERQ'||C_LINE.IND||']');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OBSERV]', '[OBSERV'||C_LINE.IND||']');

                      /* INICIO ADICIONAIS */
                      V_TESTA1AD := INSTR(V_CLOBLINHA1,'<tr',(length(V_CLOBLINHA1)-INSTR(V_CLOBLINHA1,'</tr>', INSTR(V_CLOBLINHA1, '[INDAD]',1)))*-1);
                      V_TESTA2AD := INSTR(V_CLOBLINHA1,'<p',(length(V_CLOBLINHA1)-INSTR(V_CLOBLINHA1,'</p>', INSTR(V_CLOBLINHA1, '[INDAD]',1)))*-1);
                      IF V_TESTA1AD <> 0 THEN
                         IF V_TESTA1AD < V_TESTA2AD THEN
                            V_RESULTAD := 'C';  -- Coluna
                            V_POSINIAD := INSTR(V_CLOBLINHA1,'<tr',(length(V_CLOBLINHA1)-INSTR(V_CLOBLINHA1,'</tr>', INSTR(V_CLOBLINHA1, '[INDAD]',1)))*-1);
                            V_POSFINAD := INSTR(V_CLOBLINHA1,'</tr>',INSTR(V_CLOBLINHA1,'[INDAD]',1));
                         ELSE
                            V_RESULTAD := 'L'; -- Linha
                            V_POSINIAD := INSTR(V_CLOBLINHA1,'<p',(length(V_CLOBLINHA1)-INSTR(V_CLOBLINHA1,'</p>', INSTR(V_CLOBLINHA1, '[INDAD]',1)))*-1);
                            V_POSFINAD := INSTR(V_CLOBLINHA1,'</p>',INSTR(V_CLOBLINHA1,'[INDAD]',1));
                         END IF;
                         V_CLOBINICIALAD := SUBSTR(V_CLOBLINHA1,1,V_POSFINAD-1);
                         V_CLOBLINHAAD :=  SUBSTR(V_CLOBLINHA1,V_POSINIAD,V_POSFINAD-V_POSINIAD);
                         V_CLOBFINALAD :=  SUBSTR(V_CLOBLINHA1,V_POSFINAD,length(V_CLOBLINHA1));
    
                         V_TSDADOSAD := 0;
                         FOR C_LINEAD IN (SELECT TO_CHAR(ROWNUM) AS IND, NRITEMORCAMENTO
                                          FROM FCT_ITEMORCAMENTO LEFT JOIN TRAB_SERVHIERARQUIA
                                            ON FCT_ITEMORCAMENTO.CDSERVORCA = TRAB_SERVHIERARQUIA.CDSERVORCAREL,
                                               (SELECT CDARVORE, CDSERVORCAREL
                                                  FROM TRAB_SERVHIERARQUIA
                                                 WHERE (NRTIPOHIERARQSERV = V_TIPOHIERARQUIA)
                                                    OR ((V_IDUTILIZAHIERSERV = 'SIM')
                                                   AND (V_TIPOHIERARQUIA IS NULL))) TR
                                         WHERE (((TRAB_SERVHIERARQUIA.CDARVORE BETWEEN TR.CDARVORE||'.' AND RPAD(TR.CDARVORE||'.',200,'Z'))
                                           AND (V_IDUTILIZAHIERSERV = 'SIM')
                                           AND (V_TPHIERARQ = '1')
                                           AND (V_TIPOHIERARQUIA IS NOT NULL))
                                            OR ((FCT_ITEMORCAMENTO.NRTIPOHIERARQSERV = V_TIPOHIERARQUIA)
                                           AND (V_IDUTILIZAHIERSERV = 'SIM')
                                           AND (V_TPHIERARQ = '2')
                                           AND (FCT_ITEMORCAMENTO.CDSERVORCA = TR.CDSERVORCAREL))
                                            OR ((V_IDUTILIZAHIERSERV  = 'SIM')
                                           AND (V_TIPOHIERARQUIA IS NULL)
                                           AND (FCT_ITEMORCAMENTO.CDSERVORCA = TR.CDSERVORCAREL)))
                                           AND NRSEQORCAMENTO = P_NRSEQCONTR
                                           AND (NRITEMORCAMENTOPAI = C_LINE.NRITEMORCAMENTO)
                                           AND (IDADICITEMORC = 'S')
                                           AND (FCT_ITEMORCAMENTO. NRORG = V_NRORG)
                                        UNION ALL
                                        SELECT TO_CHAR(ROWNUM) AS IND, NRITEMORCAMENTO
                                          FROM FCT_ITEMORCAMENTO
                                         WHERE V_IDUTILIZAHIERSERV = 'NAO'
                                           AND NRSEQORCAMENTO = P_NRSEQCONTR
                                           AND (NRITEMORCAMENTOPAI = C_LINE.NRITEMORCAMENTO)
                                           AND (IDADICITEMORC = 'S')
                                           AND NRORG = V_NRORG
                                        ORDER BY NRITEMORCAMENTO
                         ) LOOP
    
                             V_CLOBLINHA1AD := REPLACE(V_CLOBLINHAAD,'[INDAD]','[INDAD'||C_LINE.NRITEMORCAMENTO||C_LINEAD.IND||']');
                             V_CLOBLINHA1AD := REPLACE(V_CLOBLINHA1AD,'[DESCAD]','[DESCAD'||C_LINE.NRITEMORCAMENTO||C_LINEAD.IND||']');
                             V_CLOBLINHA1AD := REPLACE(V_CLOBLINHA1AD,'[QNTAD]', '[QNTAD'||C_LINE.NRITEMORCAMENTO||C_LINEAD.IND||']');
                             V_CLOBLINHA1AD := REPLACE(V_CLOBLINHA1AD,'[VRUNITAD]', '[VRUNITAD'||C_LINE.NRITEMORCAMENTO||C_LINEAD.IND||']');
                             V_CLOBLINHA1AD := REPLACE(V_CLOBLINHA1AD,'[MEDIDAAD]', '[MEDIDAAD'||C_LINE.NRITEMORCAMENTO||C_LINEAD.IND||']');
                             V_CLOBLINHA1AD := REPLACE(V_CLOBLINHA1AD,'[QTMEDAD]', '[QTMEDAD'||C_LINE.NRITEMORCAMENTO||C_LINEAD.IND||']');
                             V_CLOBLINHA1AD := REPLACE(V_CLOBLINHA1AD,'[VRTOTAD]', '[VRTOTAD'||C_LINE.NRITEMORCAMENTO||C_LINEAD.IND||']');
                             IF V_RESULTAD = 'C' THEN
                                 V_CLOBLINHAAUXAD := V_CLOBLINHA1AD||'</tr>';
                             ELSE
                                 V_CLOBLINHAAUXAD := V_CLOBLINHA1AD;
                             END IF;
                                 
                             V_CLOBINICIALAD := V_CLOBINICIALAD||V_CLOBLINHAAUXAD;
                             V_TSDADOSAD := V_TSDADOSAD+1;
                             
                         END LOOP;

                         IF V_TSDADOSAD <> 0 THEN
                             V_CLOBLINHA1AD := REPLACE(V_CLOBLINHAAD,'[DESCAD]','');
                             V_CLOBLINHA1AD := REPLACE(V_CLOBLINHA1AD,'[QNTAD]', '');
                             V_CLOBLINHA1AD := REPLACE(V_CLOBLINHA1AD,'[VRUNITAD]', '');
                             V_CLOBLINHA1AD := REPLACE(V_CLOBLINHA1AD,'[MEDIDAAD]', '');
                             V_CLOBLINHA1AD := REPLACE(V_CLOBLINHA1AD,'[QTMEDAD]', '');
                             V_CLOBLINHA1AD := REPLACE(V_CLOBLINHA1AD,'[VRTOTAD]', '');
                             V_CLOBLINHA1 := V_CLOBINICIALAD||V_CLOBFINALAD;
                             V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1, V_CLOBLINHAAD, '');
                         ELSE
                              V_CLOBLINHA1 := V_CLOBINICIALAD||V_CLOBFINALAD;
                              V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1, V_CLOBLINHAAD, ''); 
                         END IF;   
                         
                         /* AQUI */
                         V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[TOTITEMAD]', '[TOTITEMAD'||C_LINE.NRITEMORCAMENTO||'1]');              
                         
                      END IF;
                      /* FIM ADICIONAIS */

                      IF V_RESULT = 'C' THEN
                          IF (V_EXISTE_ADICIONAL <> 0) THEN
                              V_CLOBINICIAL := V_CLOBINICIAL||V_CLOBLINHA1||'</table>';
                          ELSE
                              V_CLOBINICIAL := V_CLOBINICIAL||V_CLOBLINHA1||'</tr>';
                          END IF;
                      ELSE
                          V_CLOBINICIAL := V_CLOBINICIAL||V_CLOBLINHA1;
                      END IF;


                      V_TSDADOS := V_TSDADOS+1;
                  END LOOP;

                    IF V_TSDADOS <> 0 THEN
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA,'[IND]','');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[COD]','');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[QNT]', '');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[DESC]', '');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[QTMED]', '');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[MEDIDA]', '');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[TPSERV]', '');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[QTTPSERV]', '');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[MODPAG]', '');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[VRUNIT]', '');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[VRTOT]', '');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[TOTPER]', '');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[TPHIERQ]', '');
                        V_CLOBLINHA1 := REPLACE(V_CLOBLINHA1,'[OBSERV]', '');
                        V_TXDOCUMENT := V_CLOBINICIAL||V_CLOBFINAL;
                        V_TXDOCUMENT := REPLACE(V_TXDOCUMENT, V_CLOBLINHA, '');
                        V_TXDOCUMENT := REPLACE(V_TXDOCUMENT, V_CLOBLINREC, '');
                     ELSE
                        V_TXDOCUMENT := V_CLOBINICIAL||V_CLOBFINAL;
                        V_TXDOCUMENT := REPLACE(V_TXDOCUMENT, V_CLOBLINHA, '');
                        V_TXDOCUMENT := REPLACE(V_TXDOCUMENT, V_CLOBLINREC, '');
                     END IF;

                 END IF;

                 V_TOTITEM := 0;
                 V_TOTPERIT := 0;
                 V_TOTGERAD := 0;
                  FOR C_ITENS IN (SELECT TO_CHAR(ROWNUM) AS IND, DECODE(IT.CDSERVORCA, NULL, IT.CDPRODUTO, IT.CDSERVORCA) AS CODITEM, NVL(IT.QTITEMORCAMENTO,0) AS QTITEMORCAMENTO, IT.IDFATURAORCA,
                                         IT.NRITEMORCAMENTO, IT.NMITEMORCAMENTO, NVL(IT.QTMEDIDAORCA,0) AS QTMEDIDAORCA, TM.DSTPMEDIDASERV, IT.IDUNIDTEMPOLOCACAO, NVL(IT.QTTEMPOLOCACAO, 0) AS QTTEMPOLOCACAO,
                                         NVL(IT.VRUNITITEMORCA,0) AS VRUNITITEMORCA, NVL(IT.VRTOTALITEMORCA,0) AS VRTOTALITEMORCA, NVL((IT.VRTOTALITEMORCA),0) TOTPERIODO,
                                         NVL(LTRIM(to_char(IT.VRUNITITEMORCA,'999G999G999G990D99')),0) VRUNITITEMORCA2, NVL(LTRIM(to_char(IT.VRTOTALITEMORCA,'999G999G999G990D99')),0) VRTOTALITEMORCA2,
                                         NVL(LTRIM(to_char(IT.VRTOTALITEMORCA,'999G999G999G990D99')),0) AS TOTPERIODO2, MP.DSMODPGTO, IT.NRTIPOHIERARQSERV, IT.TXFORMULAITORCA
                                    FROM FCT_ITEMORCAMENTO IT 
                                    LEFT JOIN FCT_TIPOMEDIDASERV TM
                                      ON IT.CDTPMEDIDASERV = TM.CDTPMEDIDASERV
                                     AND IT.NRORG = TM.NRORG 
                                    LEFT JOIN MODPGTO MP
                                      ON IT.CDMODPGTO = MP.CDMODPGTO
                                     AND IT.NRORG = MP.NRORG  
                                    LEFT JOIN TRAB_SERVHIERARQUIA
                                      ON IT.CDSERVORCA = TRAB_SERVHIERARQUIA.CDSERVORCAREL
                                     AND IT.NRORG = TRAB_SERVHIERARQUIA.NRORG ,
                                         (SELECT CDARVORE, CDSERVORCAREL
                                            FROM TRAB_SERVHIERARQUIA
                                           WHERE (NRTIPOHIERARQSERV = V_TIPOHIERARQUIA) OR ((V_IDUTILIZAHIERSERV = 'SIM')
                                             AND (V_TIPOHIERARQUIA IS NULL))) TR
                                   WHERE (((TRAB_SERVHIERARQUIA.CDARVORE BETWEEN TR.CDARVORE||'.' AND RPAD(TR.CDARVORE||'.',200,'Z'))
                                     AND (V_IDUTILIZAHIERSERV = 'SIM')
                                     AND (V_TPHIERARQ = '1')
                                     AND (V_TIPOHIERARQUIA IS NOT NULL))
                                      OR ((IT.NRTIPOHIERARQSERV = V_TIPOHIERARQUIA)
                                     AND (V_IDUTILIZAHIERSERV = 'SIM')
                                     AND (V_TPHIERARQ = '2')
                                     AND (IT.CDSERVORCA = TR.CDSERVORCAREL))
                                      OR ((V_IDUTILIZAHIERSERV  = 'SIM')
                                     AND (V_TIPOHIERARQUIA IS NULL)
                                     AND (IT.CDSERVORCA = TR.CDSERVORCAREL)))
                                     AND IT.NRSEQORCAMENTO = P_NRSEQCONTR
                                     AND ((IT.NRITEMORCAMENTOPAI IS NULL)
                                     AND (IT.IDADICITEMORC = 'N'))
                                     AND (IT.NRORG = V_NRORG)
                                  UNION ALL
                                  SELECT TO_CHAR(ROWNUM) AS IND, DECODE(IT.CDSERVORCA, NULL, IT.CDPRODUTO, IT.CDSERVORCA) AS CODITEM, NVL(IT.QTITEMORCAMENTO,0) AS QTITEMORCAMENTO, IT.IDFATURAORCA,
                                         IT.NRITEMORCAMENTO, IT.NMITEMORCAMENTO, NVL(IT.QTMEDIDAORCA,0) AS QTMEDIDAORCA, TM.DSTPMEDIDASERV, IT.IDUNIDTEMPOLOCACAO, NVL(IT.QTTEMPOLOCACAO, 0) AS QTTEMPOLOCACAO,
                                         NVL(IT.VRUNITITEMORCA,0) AS VRUNITITEMORCA, NVL(IT.VRTOTALITEMORCA,0) AS VRTOTALITEMORCA, NVL((IT.VRTOTALITEMORCA),0) TOTPERIODO,
                                         NVL(LTRIM(to_char(IT.VRUNITITEMORCA,'999G999G999G990D99')),0) VRUNITITEMORCA2, NVL(LTRIM(to_char(IT.VRTOTALITEMORCA,'999G999G999G990D99')),0) VRTOTALITEMORCA2,
                                         NVL(LTRIM(to_char(IT.VRTOTALITEMORCA,'999G999G999G990D99')),0) AS TOTPERIODO2, MP.DSMODPGTO, IT.NRTIPOHIERARQSERV, IT.TXFORMULAITORCA
                                    FROM FCT_ITEMORCAMENTO IT 
                                    LEFT JOIN FCT_TIPOMEDIDASERV TM
                                      ON IT.CDTPMEDIDASERV = TM.CDTPMEDIDASERV
                                     AND IT.NRORG = TM.NRORG 
                                    LEFT JOIN MODPGTO MP
                                      ON IT.CDMODPGTO = MP.CDMODPGTO
                                     AND IT.NRORG = MP.NRORG
                                   WHERE V_IDUTILIZAHIERSERV = 'NAO'
                                     AND NRSEQORCAMENTO = P_NRSEQCONTR
                                     AND NRITEMORCAMENTOPAI IS NULL
                                     AND IDADICITEMORC = 'N'
                                     AND IT.NRORG = V_NRORG
                  ) LOOP

                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[IND'||C_ITENS.IND||']', NVL(C_ITENS.IND, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[COD'||C_ITENS.IND||']', NVL(C_ITENS.CODITEM, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[QNT'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.QTITEMORCAMENTO, 0)));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[DESC'||C_ITENS.IND||']', NVL(C_ITENS.NMITEMORCAMENTO, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[QTMED'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.QTMEDIDAORCA, 0)));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[MEDIDA'||C_ITENS.IND||']', NVL(C_ITENS.DSTPMEDIDASERV, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[TPSERV'||C_ITENS.IND||']', NVL(C_ITENS.IDUNIDTEMPOLOCACAO, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[QTTPSERV'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.QTTEMPOLOCACAO, 0)));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[MODPAG'||C_ITENS.IND||']', NVL(C_ITENS.DSMODPGTO, ''));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[VRUNIT'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.VRUNITITEMORCA2, 0)));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[VRTOT'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.VRTOTALITEMORCA2, 0)));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[TOTPER'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.TOTPERIODO2, 0)));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[TPHIERQ'||C_ITENS.IND||']', TO_CHAR(NVL(C_ITENS.NRTIPOHIERARQSERV, 0)));
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[OBSERV'||C_ITENS.IND||']', REPLACE(NVL(C_ITENS.TXFORMULAITORCA, ''),CHR(10),'<br>'));
                       
                      V_TOTITEMAD := 0; 
                      
                      FOR C_ITENSAD IN (SELECT TO_CHAR(ROWNUM) AS IND, DECODE(IT.CDSERVORCA, NULL, IT.CDPRODUTO, IT.CDSERVORCA) AS CODITEM, NVL(IT.QTITEMORCAMENTO,0) AS QTITEMORCAMENTO,
                                               IT.NMITEMORCAMENTO, NVL(IT.QTMEDIDAORCA,0) AS QTMEDIDAORCA, TM.DSTPMEDIDASERV, NVL(IT.VRUNITITEMORCA,0) AS VRUNITITEMORCA, NVL(IT.VRTOTALITEMORCA,0) AS VRTOTALITEMORCA,
                                               NVL(LTRIM(to_char(IT.VRUNITITEMORCA,'999G999G999G990D99')),0) AS VRUNITITEMORCA2,
                                               NVL(LTRIM(to_char(IT.VRTOTALITEMORCA,'999G999G999G990D99')),0) AS VRTOTALITEMORCA2, IT.IDADICCOBRITEMORC, IT.NRITEMORCAMENTO
                                          FROM FCT_ITEMORCAMENTO IT 
                                          LEFT JOIN FCT_TIPOMEDIDASERV TM
                                               ON IT.CDTPMEDIDASERV = TM.CDTPMEDIDASERV
                                              AND IT.NRORG = TM.NRORG 
                                          LEFT JOIN TRAB_SERVHIERARQUIA
                                               ON IT.CDSERVORCA = TRAB_SERVHIERARQUIA.CDSERVORCAREL
                                              AND IT.NRORG = TRAB_SERVHIERARQUIA.NRORG,
                                               (SELECT CDARVORE, CDSERVORCAREL
                                                  FROM TRAB_SERVHIERARQUIA
                                                 WHERE (NRTIPOHIERARQSERV = V_TIPOHIERARQUIA)
                                                    OR ((V_IDUTILIZAHIERSERV = 'SIM')
                                                   AND (V_TIPOHIERARQUIA IS NULL))) TR
                                         WHERE (((TRAB_SERVHIERARQUIA.CDARVORE BETWEEN TR.CDARVORE||'.' AND RPAD(TR.CDARVORE||'.',200,'Z'))
                                           AND (V_IDUTILIZAHIERSERV = 'SIM')
                                           AND (V_TPHIERARQ = '1')
                                           AND (V_TIPOHIERARQUIA IS NOT NULL))
                                            OR ((IT.NRTIPOHIERARQSERV = V_TIPOHIERARQUIA)
                                           AND (V_IDUTILIZAHIERSERV = 'SIM')
                                           AND (V_TPHIERARQ = '2')
                                           AND (IT.CDSERVORCA = TR.CDSERVORCAREL))
                                            OR ((V_IDUTILIZAHIERSERV  = 'SIM')
                                           AND (V_TIPOHIERARQUIA IS NULL)
                                           AND (IT.CDSERVORCA = TR.CDSERVORCAREL)))
                                           AND NRSEQORCAMENTO = P_NRSEQCONTR
                                           AND (NRITEMORCAMENTOPAI = C_ITENS.NRITEMORCAMENTO)
                                           AND (IDADICITEMORC = 'S')
                                           AND (IT.NRORG = V_NRORG)
                                       UNION ALL
                                       SELECT TO_CHAR(ROWNUM) AS IND, DECODE(IT.CDSERVORCA, NULL, IT.CDPRODUTO, IT.CDSERVORCA) AS CODITEM, NVL(IT.QTITEMORCAMENTO,0) AS QTITEMORCAMENTO,
                                              IT.NMITEMORCAMENTO, NVL(IT.QTMEDIDAORCA,0) AS QTMEDIDAORCA, TM.DSTPMEDIDASERV, NVL(IT.VRUNITITEMORCA,0) AS VRUNITITEMORCA, NVL(IT.VRTOTALITEMORCA,0) AS VRTOTALITEMORCA,
                                              NVL(LTRIM(to_char(IT.VRUNITITEMORCA,'999G999G999G990D99')),0) AS VRUNITITEMORCA2,
                                              NVL(LTRIM(to_char(IT.VRTOTALITEMORCA,'999G999G999G990D99')),0) AS VRTOTALITEMORCA2, IT.IDADICCOBRITEMORC, IT.NRITEMORCAMENTO
                                         FROM FCT_ITEMORCAMENTO IT 
                                         LEFT JOIN FCT_TIPOMEDIDASERV TM
                                               ON IT.CDTPMEDIDASERV = TM.CDTPMEDIDASERV
                                              AND IT.NRORG = TM.NRORG 
                                        WHERE V_IDUTILIZAHIERSERV = 'NAO'
                                          AND NRSEQORCAMENTO = P_NRSEQCONTR
                                          AND (NRITEMORCAMENTOPAI = C_ITENS.NRITEMORCAMENTO)
                                          AND (IDADICITEMORC = 'S')
                                          AND IT.NRORG = V_NRORG
                                      ORDER BY NRITEMORCAMENTO
                         ) LOOP
        
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[INDAD'||C_ITENS.NRITEMORCAMENTO||C_ITENSAD.IND||']', NVL(C_ITENSAD.IND, ''));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[DESCAD'||C_ITENS.NRITEMORCAMENTO||C_ITENSAD.IND||']', NVL(C_ITENSAD.NMITEMORCAMENTO, ''));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[QNTAD'||C_ITENS.NRITEMORCAMENTO||C_ITENSAD.IND||']', TO_CHAR(NVL(C_ITENSAD.QTITEMORCAMENTO, 0)));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[VRUNITAD'||C_ITENS.NRITEMORCAMENTO||C_ITENSAD.IND||']', TO_CHAR(NVL(C_ITENSAD.VRUNITITEMORCA2, 0)));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[MEDIDAAD'||C_ITENS.NRITEMORCAMENTO||C_ITENSAD.IND||']', NVL(C_ITENSAD.DSTPMEDIDASERV, ''));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[QTMEDAD'||C_ITENS.NRITEMORCAMENTO||C_ITENSAD.IND||']', TO_CHAR(NVL(C_ITENSAD.QTMEDIDAORCA, 0)));
                             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[VRTOTAD'||C_ITENS.NRITEMORCAMENTO||C_ITENSAD.IND||']', TO_CHAR(NVL(C_ITENSAD.VRTOTALITEMORCA2, 0)));
        
                             V_TOTITEMAD := V_TOTITEMAD + NVL(C_ITENSAD.VRTOTALITEMORCA,0);
        
                         END LOOP;

                      V_TOTGERAD := V_TOTGERAD + NVL(V_TOTITEMAD,0);

                      V_TOTITEMAD2 := NVL(LTRIM(to_char(V_TOTITEMAD,'999G999G999G990D99')),0);
                      
                      V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[TOTITEMAD'||C_ITENS.NRITEMORCAMENTO||'1]', TO_CHAR(NVL(V_TOTITEMAD2, 0)));

                      V_TOTITEM := V_TOTITEM + NVL(C_ITENS.VRTOTALITEMORCA,0);
                      V_TOTPERIT := V_TOTPERIT + NVL(C_ITENS.TOTPERIODO,0);

                  END LOOP;

                  /*Totais Item (Todos)*/

                  V_TOTGERAD2 := NVL(LTRIM(to_char(V_TOTGERAD,'999G999G999G990D99')),0);
                  V_TOTITEM2 := NVL(LTRIM(to_char(V_TOTITEM,'999G999G999G990D99')),0);
                  V_TOTPERIT2 := NVL(LTRIM(to_char(V_TOTPERIT,'999G999G999G990D99')),0);

                 BEGIN
                     SELECT NVL(M.IDUTILIZAHIERSERV,'NAO')
                       INTO V_IDUTILIZAHIERSERV
                       FROM FCT_ORCAMENTO O, FCT_ORCAMENTOMODELO M
                      WHERE O.NRSEQORCAMENTO = V_NRSEQCONTR
                        AND O.NRORG = V_NRORG
                        AND O.NRORCAMENTOMODELO = M.NRORCAMENTOMODELO
                        AND O.NRORG = M.NRORG;
                 EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                         V_IDUTILIZAHIERSERV := 'NAO';
                 END;

                 IF(V_IDUTILIZAHIERSERV = 'SIM')THEN
                     SELECT NVL(SUM(FCT_ITEMORCAMENTO.VRTOTALITEMORCA),0)
                       INTO V_TOTITEM2
                       FROM FCT_ITEMORCAMENTO, FCT_HIERARQUIASERV
                      WHERE FCT_ITEMORCAMENTO.NRSEQORCAMENTO = P_NRSEQCONTR
                        AND FCT_ITEMORCAMENTO.NRORG = P_NRORG
                        AND FCT_HIERARQUIASERV.CDSERVORCAREL = FCT_ITEMORCAMENTO.CDSERVORCA
                        AND FCT_HIERARQUIASERV.NRORG = FCT_ITEMORCAMENTO.NRORG
                        AND ((FCT_HIERARQUIASERV.NRTIPOHIERARQSERV = FCT_ITEMORCAMENTO.NRTIPOHIERARQSERV
                        AND FCT_ITEMORCAMENTO.NRTIPOHIERARQSERV = V_TIPOHIERARQUIA
                        AND FCT_HIERARQUIASERV.IDSOMAVALORSUP = 'SIM') OR (FCT_HIERARQUIASERV.CDSERVORCASUP IS NULL AND V_TIPOHIERARQUIA IS NULL));

                     V_TOTITEM2 := NVL(LTRIM(to_char(V_TOTITEM2,'999G999G999G990D99')),0);
                     V_TOTPERIT2 := V_TOTITEM2;

                     SELECT NVL(SUM(FCT_ITEMORCAMENTO.VRTOTALITEMORCA),0)
                       INTO V_TOTORCA2
                       FROM FCT_ITEMORCAMENTO
                      WHERE FCT_ITEMORCAMENTO.NRSEQORCAMENTO = P_NRSEQCONTR
                        AND FCT_ITEMORCAMENTO.NRORG = P_NRORG
                        AND FCT_ITEMORCAMENTO.IDADICITEMORC = 'S'
                        AND FCT_ITEMORCAMENTO.IDADICCOBRITEMORC = 'S';

                     V_TOTORCA2 := V_TOTORCA2 + V_TOTITEM2;
                     V_TOTORCA2 := NVL(LTRIM(to_char(V_TOTORCA2,'999G999G999G990D99')),0);

                ELSE
                    SELECT SUM(VRTOTALITEMORCA)
                      INTO V_TOTORCA2
                      FROM FCT_ITEMORCAMENTO
                     WHERE NRSEQORCAMENTO = P_NRSEQCONTR
                       AND NRORG = P_NRORG
                       AND ((IDADICITEMORC = 'N') OR (IDADICCOBRITEMORC = 'S'));

                    V_TOTORCA2 := NVL(LTRIM(to_char(V_TOTORCA2,'999G999G999G990D99')),0);

                 END IF;
             
             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[TOTGERAD]', NVL(TO_CHAR(V_TOTGERAD2), 0));
             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[TOTITEM]', NVL(TO_CHAR(V_TOTITEM2), 0));
             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[TOTPERIT]', NVL(TO_CHAR(V_TOTPERIT2), 0));
             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[TOTORCA]', NVL(TO_CHAR(V_TOTORCA2), 0));
             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[LN]','');
             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,'[/LN]','');
             V_TXDOCUMENT := REPLACE(V_TXDOCUMENT,V_MNEHIERARQ,'');
             UPDATE FCT_ORCAMENTODOC
                SET TXMODELOORCADOC = V_TXDOCUMENT
              WHERE NRSEQORCADOC = C_DOC.NRSEQORCADOC
                AND NRSEQORCAMENTO = V_NRSEQCONTR
                AND NRORG = P_NRORG;
         END LOOP;
     END IF;
  END;
/

-- VERSAO --
BEGIN ATUVEROBJ('FCT_SUB_MNE_DOC','6.10.xxx'); END;
/