CREATE OR REPLACE PROCEDURE APURAC_DM_RESULT_OPERACIONAL(
   P_CDFILIAL     IN  LOTE.CDFILIAL%TYPE,
   P_DTINICIAL    IN  DATE,
   P_DTFINAL      IN  DATE,
   P_NRORG        IN  POSESTATU.CDLOCALESTOQ%TYPE)
AS
   V_CDEMPRESA          VARCHAR2(02);
   V_NMEMPRESA          VARCHAR2(40);
   V_CDAREAGERE         VARCHAR2(03);
   V_NMAREAGERE         VARCHAR2(30);
   V_CDSEGMER           VARCHAR2(05);
   V_NMSEGMER           VARCHAR2(40);
   V_CDSUPERVISOR       VARCHAR2(12);
   V_NMOPERADOR         VARCHAR2(40);
   V_CDFILIAL           VARCHAR2(04);
   V_NMFILIAL           VARCHAR2(30);
   V_CDPAIS             VARCHAR2(04);
   V_NMPAIS             VARCHAR2(30);
   V_SGESTADO           VARCHAR2(02);
   V_NMESTADO           VARCHAR2(30);
   V_CDMUNICIPIO        VARCHAR2(04);
   V_NMMUNICIPIO        VARCHAR2(30);
   V_CDFILIALCENT       VARCHAR2(04);
   V_NMFILIALCENT       VARCHAR2(30);
   V_CDGRUPSERV         VARCHAR2(03);
   V_NMGRUPSERV         VARCHAR2(30);
   V_NMSERVICO          VARCHAR2(40);
   V_NRINSJURCLIE       VARCHAR2(21);
   V_NMCLIENTE          VARCHAR2(100);
   V_CDTIPOOPER         VARCHAR2(02);
   V_NMTIPOOPER         VARCHAR2(30);
   V_IDENTRSAIDOP       VARCHAR2(01);
   V_CDRECEXTRA         VARCHAR2(04);
   V_DESCRECEXTRA       VARCHAR2(200);
   V_CDFATUDIVER        VARCHAR2(02);
   V_DSFATUDIVER        VARCHAR2(40);
   V_NMENCARGO          VARCHAR2(20);
   V_NRINSJURFORN       VARCHAR2(20);
   V_NMFORNECED         VARCHAR2(40);
   V_NMDESPPREV         VARCHAR2(50);
   V_DSTPCTPAGAR        VARCHAR2(40);
   V_CDDESPPREV         VARCHAR2(03);
   V_CDTPCTPAGAR        VARCHAR2(06);
   V_CDTPCTRECEBER      VARCHAR2(06);
   V_DSTPCTRECEBER      VARCHAR2(40);
   V_CDAJUCTPAG         VARCHAR2(04);
   V_DSAJUCTPAG         VARCHAR2(40);
   V_CDAJUCTREC         VARCHAR2(04);
   V_DSAJUCTREC         VARCHAR2(40);
   V_DSTPDEBIBANC       VARCHAR2(30);
   V_DSTPCREDBANC       VARCHAR2(30);
   V_CDARVPROD          VARCHAR2(20);
   V_NMPRODUTO          VARCHAR2(50);
   V_CDINVESTIMENTO     VARCHAR2(10);
   V_NMINVESTIMENTO     VARCHAR2(50);
   V_DSDESPADMFAT       VARCHAR2(40);
   V_CDDESPADMFAT       VARCHAR2(02);

   V_AUX_DEB            FLOAT(126);
   V_AUX_AJ             FLOAT(126);
   V_AUX_CRE            FLOAT(126);
   V_IDTPLANCROP        VARCHAR2(02);
   V_IDDTAPURFAT        VARCHAR2(01);
   
   V_NRDIA_UTIL         NUMBER;
   V_NRDIA_REALIZADO    NUMBER;
   V_CALC_PROJ          VARCHAR2(01);

   V_AUX                FLOAT(126);
BEGIN
   SELECT MAX(F.CDEMPRESA), MAX(E.NMEMPRESA), MAX(F.CDAREAGERE), MAX(A.NMAREAGERE), MAX(F.CDSEGMER), MAX(S.DSSEGMER), MAX(F.CDSUPERVISOR),
          MAX(O.NMOPERADOR), MAX(F.CDFILIAL), MAX(F.NMFILIAL), MAX(EF.CDPAIS), MAX(P.NMPAIS), MAX(T.SGESTADO), MAX(T.SGESTADO), MAX(M.CDMUNICIPIO),
          MAX(M.NMMUNICIPIO), MAX(F.CDFILCENTPME), MAX(C.NMFILIAL), MAX(CASE WHEN PFR.IDFATUROP = 'AB'
                                                                             THEN CASE WHEN PFR.IDDETALHEFATUR = 'M'
                                                                                       THEN 'FE'
                                                                                       ELSE 'FS'
                                                                                  END
                                                                             ELSE CASE WHEN PFR.IDFATUROP = 'FE'
                                                                                       THEN 'FE'
                                                                                       ELSE 'FS'
                                                                                  END
                                                                        END), MAX(PFR.IDDTAPURFAT)
     INTO V_CDEMPRESA, V_NMEMPRESA, V_CDAREAGERE, V_NMAREAGERE, V_CDSEGMER, V_NMSEGMER, V_CDSUPERVISOR,
          V_NMOPERADOR, V_CDFILIAL, V_NMFILIAL, V_CDPAIS, V_NMPAIS, V_SGESTADO, V_NMESTADO, V_CDMUNICIPIO,
          V_NMMUNICIPIO, V_CDFILIALCENT, V_NMFILIALCENT, V_IDTPLANCROP, V_IDDTAPURFAT
     FROM FILIAL F, EMPRESA E, AREAGERE A, SEGMERCADO S, PAIS P, ESTADO T, MUNICIPIO M, FILIAL C, ENDEFILI EF, OPERADOR O, PARFILIALROP PFR
    WHERE F.CDFILIAL = P_CDFILIAL
      AND F.NRORG = P_NRORG
      AND F.CDEMPRESA = E.CDEMPRESA(+)
      AND F.CDAREAGERE = A.CDAREAGERE(+)
      AND F.CDSEGMER = S.CDSEGMER(+)
      AND F.CDFILCENTPME = C.CDFILIAL(+)
      AND F.CDSUPERVISOR = O.CDOPERADOR(+)
      AND F.CDFILIAL = EF.CDFILIAL
      AND PFR.CDFILIAL = TIPOPARAM_DRO(F.CDFILIAL)
      AND EF.IDTPENDEFILI = 'P'
      AND EF.CDPAIS = P.CDPAIS(+)
      AND EF.SGESTADO = T.SGESTADO(+)
      AND EF.CDMUNICIPIO = M.CDMUNICIPIO(+);

   BEGIN
      SELECT NRDIA_UTIL, CASE WHEN NRDIA_REALIZADO = 0 THEN 1 ELSE NRDIA_REALIZADO END NRDIA_REALIZADO,
             DECODE(DTINICOMP, DTINI, DECODE(DTFINALCOMP, DTFINAL,'N','S'),'S') CALC_PROJ
        INTO V_NRDIA_UTIL, V_NRDIA_REALIZADO, V_CALC_PROJ
        FROM (SELECT RETORNA_DIA_UTIL(P_CDFILIAL,P_DTINICIAL,LAST_DAY(P_DTFINAL)) AS NRDIA_UTIL,
                     RETORNA_DIAS_REALIZADOS(P_CDFILIAL,P_DTINICIAL,P_DTFINAL) AS NRDIA_REALIZADO,
                     P_DTFINAL AS DTFINAL, P_DTINICIAL DTINI, LAST_DAY(P_DTFINAL) DTFINALCOMP,
                     FIRST_DAY(P_DTINICIAL) DTINICOMP
                FROM DUAL);
      EXCEPTION
        WHEN NO_DATA_FOUND THEN BEGIN
           V_NRDIA_UTIL  := NULL;
           V_NRDIA_REALIZADO := NULL;
           V_CALC_PROJ := NULL;
      END;
   END;

   DELETE FROM DM_RESULT_OPERACIONAL
         WHERE CDFILIAL = P_CDFILIAL
           AND DTRESUMO BETWEEN P_DTINICIAL AND P_DTFINAL;
   COMMIT;

   V_AUX := 0;

   FOR C_FATUEFET IN (SELECT F.CDFILIAL, F.DTENTRSAID, F.CDCLIENTE, F.DSFATURAMENTO,
                             F.CDSERVFILI, F.IDTPLANCROP, NVL(SUM(F.QTFATURAMENTO),0) QTFATURAMENTO,
                             NVL(SUM(F.VRFATURAMENTO),0) VRFATURAMENTO
                        FROM FATURAMENTOROP F
                       WHERE F.IDTPLANCROP = 'FAT_EFETIVO'
                         AND F.CDFILIAL = P_CDFILIAL
                         AND F.DTENTRSAID BETWEEN P_DTINICIAL AND P_DTFINAL
                       GROUP BY F.CDFILIAL, F.DTENTRSAID, F.CDCLIENTE, F.DSFATURAMENTO,
                             F.CDSERVFILI, F.IDTPLANCROP, F.CDFILIALCENT) LOOP

      SELECT MAX(G.CDGRUPSERV), MAX(G.NMGRUPSERV), MAX(S.NMSERVICO)
        INTO V_CDGRUPSERV, V_NMGRUPSERV, V_NMSERVICO
        FROM SERVICO S, GRUPSERV G
       WHERE S.CDFILIAL = C_FATUEFET.CDFILIAL
         AND S.CDSERVFILI = C_FATUEFET.CDSERVFILI
         AND S.CDGRUPSERV = G.CDGRUPSERV;

      SELECT MAX(C.NRINSJURCLIE), MAX(C.NMFANTCLIE)
        INTO V_NRINSJURCLIE, V_NMCLIENTE
        FROM CLIENTE C
       WHERE C.CDCLIENTE = C_FATUEFET.CDCLIENTE;

      V_AUX := CASE WHEN (V_IDTPLANCROP = 'FS') THEN 0
                    ELSE C_FATUEFET.VRFATURAMENTO
               END;

      INSERT INTO DM_RESULT_OPERACIONAL
             (DTRESUMO, CDEMPRESA, NMEMPRESA, CDREGIONAL, NMREGIONAL, CDSEGMERCADO, NMSEGMERCADO, CDSUPERVISOR, NMSUPERVISOR,
              CDFILIAL, NMFILIAL, CDPAIS, NMPAIS, SGESTADO, NMESTADO, CDMUNICIPIO, NMMUNICIPIO, IDTPLANCROP,
              NRINSJURCLIE, NMCLIENTE, CDSERVICO, NMSERVICO, CDGRUPSERV, NMGRUPSERV, QTSERVICO,
              VRFATUREFET, VRFATUREFETPROJ, VRFATUREFETORCA,
              VRFATBRUTO, VRFATBRUTOPROJ, VRFATBRUTOORCA,
              CDFILIALCENT, NMFILIALCENT, VRFATLIQUIDO,
              VRFATLIQUIDOPROJ, VRFATLIQUIDOORCA,
              VRMARGEMCONTR, VRMARGEMCONTRPROJ, VRMARGEMCONTRORCA,
              VRRESULTPARC, VRRESULTPARCPROJ, VRRESULTPARCORCA, VRRESULTFINAL, VRRESULTFINALPROJ, VRRESULTFINALORCA, NRORG)
           VALUES (C_FATUEFET.DTENTRSAID, V_CDEMPRESA, V_NMEMPRESA, V_CDAREAGERE, V_NMAREAGERE, V_CDSEGMER, V_NMSEGMER, V_CDSUPERVISOR, V_NMOPERADOR,
                   V_CDFILIAL, V_NMFILIAL, V_CDPAIS, V_NMPAIS, V_SGESTADO, V_NMESTADO, V_CDMUNICIPIO, V_NMMUNICIPIO, C_FATUEFET.IDTPLANCROP,
                   V_NRINSJURCLIE, V_NMCLIENTE, C_FATUEFET.CDSERVFILI, V_NMSERVICO, V_CDGRUPSERV, V_NMGRUPSERV, C_FATUEFET.QTFATURAMENTO,
                   V_AUX, CASE WHEN V_CALC_PROJ = 'S'
                               THEN (NVL(V_AUX,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                               ELSE NVL(V_AUX,0)
                          END, 0, V_AUX,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(V_AUX,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(V_AUX,0)
                   END, 0, V_CDFILIALCENT, V_NMFILIALCENT, V_AUX,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(V_AUX,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(V_AUX,0)
                   END, 0, V_AUX,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(V_AUX,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(V_AUX,0)
                   END, 0, V_AUX,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(V_AUX,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(V_AUX,0)
                   END, 0, V_AUX, 
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(V_AUX,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(V_AUX,0)
                   END, 0, P_NRORG);
   END LOOP;

   FOR C_FATUNF IN (SELECT F.CDFILIAL, CASE V_IDDTAPURFAT WHEN 'S' THEN F.DTENTRSAID
                                                          WHEN 'R' THEN F.DTREFERENC
                                                          WHEN 'E' THEN F.DTENTRSAID
                                       END DTENTRSAID, F.CDCLIENTE, F.DSFATURAMENTO,
                           F.CDSERVFILI, F.IDTPLANCROP, NVL(SUM(F.QTFATURAMENTO),0) QTFATURAMENTO,
                           NVL(SUM(F.VRFATURAMENTO),0) VRFATURAMENTO, CDTIPOOPER, IDENTRSAIDOP, NVL(SUM(VRFATDEVCANC),0) VRFATDEVCANC
                      FROM FATURAMENTOROP F
                     WHERE F.IDTPLANCROP = 'FAT_NOTAFISCAL'
                       AND F.CDFILIAL = P_CDFILIAL
                       AND ((('S' = V_IDDTAPURFAT) AND (F.DTENTRSAID BETWEEN P_DTINICIAL AND P_DTFINAL))
                        OR (('R' = V_IDDTAPURFAT) AND (F.DTREFERENC BETWEEN P_DTINICIAL AND P_DTFINAL))
                        OR (('E' = V_IDDTAPURFAT) AND (F.DTENTRSAID BETWEEN P_DTINICIAL AND P_DTFINAL)))
                     GROUP BY F.CDFILIAL, CASE V_IDDTAPURFAT WHEN 'S' THEN F.DTENTRSAID
                                                             WHEN 'R' THEN F.DTREFERENC
                                          END, F.CDCLIENTE, F.DSFATURAMENTO,
                           F.CDSERVFILI, F.IDTPLANCROP, F.CDFILIALCENT, CDTIPOOPER, IDENTRSAIDOP) LOOP

      SELECT MAX(C.NRINSJURCLIE), MAX(C.NMFANTCLIE)
        INTO V_NRINSJURCLIE, V_NMCLIENTE
        FROM CLIENTE C
       WHERE C.CDCLIENTE = C_FATUNF.CDCLIENTE;

      SELECT MAX(CDTIPOOPER), MAX(NMTIPOOPER), MAX(IDENTRSAIDOP)
        INTO V_CDTIPOOPER, V_NMTIPOOPER, V_IDENTRSAIDOP
        FROM TIPOOPERAC
       WHERE CDTIPOOPER = C_FATUNF.CDTIPOOPER
         AND IDENTRSAIDOP = C_FATUNF.IDENTRSAIDOP;

      V_AUX := CASE WHEN (V_IDTPLANCROP = 'FE') THEN 0
                    ELSE C_FATUNF.VRFATURAMENTO
               END;

      INSERT INTO DM_RESULT_OPERACIONAL
             (DTRESUMO, CDEMPRESA, NMEMPRESA, CDREGIONAL, NMREGIONAL, CDSEGMERCADO, NMSEGMERCADO,
              CDSUPERVISOR, NMSUPERVISOR, CDFILIAL, NMFILIAL, CDPAIS, NMPAIS, SGESTADO,
              NMESTADO, CDMUNICIPIO, NMMUNICIPIO, IDTPLANCROP, NRINSJURCLIE, NMCLIENTE, CDSERVICO,
              NMSERVICO, CDGRUPSERV, NMGRUPSERV, QTSERVICO, VRFATURNF, VRFATURNFPROJ, VRFATURNFORCA,
              VRFATBRUTO, VRFATBRUTOPROJ, VRFATBRUTOORCA, CDFILIALCENT,
              NMFILIALCENT, CDTIPOOPER, NMTIPOOPER, IDENTRSAIDOP, VRDEVOLUESTOR,
              VRDEVOLUESTORPROJ, VRDEVOLUESTORORCA,                                     
              VRDEDURECEITA, VRDEDURECEITAPROJ, VRDEDURECEITAORCA,
              VRFATLIQUIDO,
              VRFATLIQUIDOPROJ, VRFATLIQUIDOORCA, VRMARGEMCONTR,
              VRMARGEMCONTRPROJ, VRMARGEMCONTRORCA,
              VRRESULTPARC, VRRESULTPARCPROJ, VRRESULTPARCORCA, VRRESULTFINAL, VRRESULTFINALPROJ, VRRESULTFINALORCA, NRORG)
           VALUES (C_FATUNF.DTENTRSAID, V_CDEMPRESA, V_NMEMPRESA, V_CDAREAGERE, V_NMAREAGERE, V_CDSEGMER, V_NMSEGMER,
                   V_CDSUPERVISOR, V_NMOPERADOR, V_CDFILIAL, V_NMFILIAL, V_CDPAIS, V_NMPAIS, V_SGESTADO,
                   V_NMESTADO, V_CDMUNICIPIO, V_NMMUNICIPIO, C_FATUNF.IDTPLANCROP, V_NRINSJURCLIE, V_NMCLIENTE, NULL,
                   NULL, NULL, NULL, C_FATUNF.QTFATURAMENTO,
                   V_AUX, CASE WHEN V_CALC_PROJ = 'S'
                               THEN (NVL(V_AUX,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                               ELSE NVL(V_AUX,0)
                          END, 0,
                   V_AUX,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(V_AUX,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(V_AUX,0)
                   END, 0,
                   V_CDFILIALCENT, V_NMFILIALCENT, V_CDTIPOOPER, V_NMTIPOOPER, V_IDENTRSAIDOP, C_FATUNF.VRFATDEVCANC,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FATUNF.VRFATDEVCANC,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FATUNF.VRFATDEVCANC,0)
                   END, 0,
                   C_FATUNF.VRFATDEVCANC,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FATUNF.VRFATDEVCANC,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FATUNF.VRFATDEVCANC,0)
                   END, 0,                 
                   V_AUX-C_FATUNF.VRFATDEVCANC,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(V_AUX,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(V_AUX,0)
                   END-C_FATUNF.VRFATDEVCANC, 0,
                   V_AUX-C_FATUNF.VRFATDEVCANC,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(V_AUX,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(V_AUX,0)
                   END-C_FATUNF.VRFATDEVCANC, 0, V_AUX-C_FATUNF.VRFATDEVCANC,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(V_AUX,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(V_AUX,0)
                   END-C_FATUNF.VRFATDEVCANC, 0, V_AUX-C_FATUNF.VRFATDEVCANC,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(V_AUX,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(V_AUX,0)
                   END-C_FATUNF.VRFATDEVCANC, 0, P_NRORG);
   END LOOP;

   FOR C_RECEITA_EXTRA IN (SELECT F.CDFILIAL, F.DTENTRSAID, F.CDCLIENTE, F.DSFATURAMENTO,
                                  F.CDSERVFILI, F.IDTPLANCROP, NVL(SUM(F.QTFATURAMENTO),0) QTFATURAMENTO,
                                  NVL(SUM(F.VRFATURAMENTO),0) VRFATURAMENTO, CDTIPOOPER, IDENTRSAIDOP, NVL(SUM(VRFATDEVCANC),0) VRFATDEVCANC,
                                  NRRECEXTRAFILI
                             FROM FATURAMENTOROP F
                            WHERE F.IDTPLANCROP = 'RECEITA_EXTRA'
                              AND F.CDFILIAL = P_CDFILIAL
                              AND F.DTENTRSAID BETWEEN P_DTINICIAL AND P_DTFINAL
                            GROUP BY F.CDFILIAL, F.DTENTRSAID, F.CDCLIENTE, F.DSFATURAMENTO,
                                  F.CDSERVFILI, F.IDTPLANCROP, F.CDFILIALCENT, CDTIPOOPER, IDENTRSAIDOP, NRRECEXTRAFILI) LOOP

      SELECT MAX(CDRECEXTRA), MAX(DESCRECEXTRA)
        INTO V_CDRECEXTRA, V_DESCRECEXTRA
        FROM RECEXTRA
       WHERE CDRECEXTRA = C_RECEITA_EXTRA.NRRECEXTRAFILI;

      INSERT INTO DM_RESULT_OPERACIONAL
             (DTRESUMO, CDEMPRESA, NMEMPRESA, CDREGIONAL, NMREGIONAL, CDSEGMERCADO, NMSEGMERCADO, CDSUPERVISOR, NMSUPERVISOR,
              CDFILIAL, NMFILIAL, CDPAIS, NMPAIS, SGESTADO, NMESTADO, CDMUNICIPIO, NMMUNICIPIO, IDTPLANCROP,
              NRINSJURCLIE, NMCLIENTE, CDSERVICO, NMSERVICO, CDGRUPSERV, NMGRUPSERV, QTSERVICO,
              VRFATRECEXTRA, VRFATRECEXTRAPROJ, VRFATRECEXTRAORCA,
              VRFATBRUTO, VRFATBRUTOPROJ, VRFATBRUTOORCA,
              CDFILIALCENT, NMFILIALCENT, CDTIPOOPER, NMTIPOOPER, IDENTRSAIDOP, VRDEVOLUESTOR,
              VRDEVOLUESTORPROJ, VRDEVOLUESTORORCA, CDRECEXTRA, NMRECEXTRA,
              VRFATLIQUIDO, VRFATLIQUIDOPROJ, VRFATLIQUIDOORCA, VRMARGEMCONTR, 
              VRMARGEMCONTRPROJ, VRMARGEMCONTRORCA, VRRESULTPARC, VRRESULTPARCPROJ, VRRESULTPARCORCA, VRRESULTFINAL,
              VRRESULTFINALPROJ, VRRESULTFINALORCA, NRORG)
           VALUES (C_RECEITA_EXTRA.DTENTRSAID, V_CDEMPRESA, V_NMEMPRESA, V_CDAREAGERE, V_NMAREAGERE, V_CDSEGMER, V_NMSEGMER, V_CDSUPERVISOR, V_NMOPERADOR,
                   V_CDFILIAL, V_NMFILIAL, V_CDPAIS, V_NMPAIS, V_SGESTADO, V_NMESTADO, V_CDMUNICIPIO, V_NMMUNICIPIO, C_RECEITA_EXTRA.IDTPLANCROP,
                   NULL, NULL, NULL, NULL, NULL, NULL, C_RECEITA_EXTRA.QTFATURAMENTO,
                   C_RECEITA_EXTRA.VRFATURAMENTO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_RECEITA_EXTRA.VRFATURAMENTO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_RECEITA_EXTRA.VRFATURAMENTO,0)
                   END,
                   0,
                   C_RECEITA_EXTRA.VRFATURAMENTO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_RECEITA_EXTRA.VRFATURAMENTO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_RECEITA_EXTRA.VRFATURAMENTO,0)
                   END,
                   0,
                   V_CDFILIALCENT, V_NMFILIALCENT, NULL, NULL, NULL, C_RECEITA_EXTRA.VRFATDEVCANC,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_RECEITA_EXTRA.VRFATDEVCANC,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_RECEITA_EXTRA.VRFATDEVCANC,0)
                   END, 0, V_CDRECEXTRA, V_DESCRECEXTRA,
                   C_RECEITA_EXTRA.VRFATURAMENTO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_RECEITA_EXTRA.VRFATURAMENTO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_RECEITA_EXTRA.VRFATURAMENTO,0)
                   END, 0,
                   C_RECEITA_EXTRA.VRFATURAMENTO, 
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_RECEITA_EXTRA.VRFATURAMENTO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_RECEITA_EXTRA.VRFATURAMENTO,0)
                   END, 0, C_RECEITA_EXTRA.VRFATURAMENTO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_RECEITA_EXTRA.VRFATURAMENTO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_RECEITA_EXTRA.VRFATURAMENTO,0)
                   END, 0, C_RECEITA_EXTRA.VRFATURAMENTO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_RECEITA_EXTRA.VRFATURAMENTO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_RECEITA_EXTRA.VRFATURAMENTO,0)
                   END, 0, P_NRORG);
   END LOOP;

   FOR C_FAT_DIVERSOS IN (SELECT F.CDFILIAL, F.DTENTRSAID, F.CDCLIENTE, F.DSFATURAMENTO,
                                 F.CDSERVFILI, F.IDTPLANCROP, NVL(SUM(F.QTFATURAMENTO),0) QTFATURAMENTO,
                                 NVL(SUM(F.VRFATURAMENTO),0) VRFATURAMENTO, CDTIPOOPER, IDENTRSAIDOP, NVL(SUM(VRFATDEVCANC),0) VRFATDEVCANC,
                                 NRRECEXTRAFILI, CDFATUDIVER
                            FROM FATURAMENTOROP F
                           WHERE F.IDTPLANCROP = 'FAT_DIVERSOS'
                             AND F.CDFILIAL = P_CDFILIAL
                             AND F.DTENTRSAID BETWEEN P_DTINICIAL AND P_DTFINAL
                           GROUP BY F.CDFILIAL, F.DTENTRSAID, F.CDCLIENTE, F.DSFATURAMENTO,
                                 F.CDSERVFILI, F.IDTPLANCROP, F.CDFILIALCENT, CDTIPOOPER, IDENTRSAIDOP, NRRECEXTRAFILI, CDFATUDIVER) LOOP

      SELECT MAX(CDFATUDIVER), MAX(DSFATUDIVER)
        INTO V_CDFATUDIVER, V_DSFATUDIVER
        FROM FATUDIVER
       WHERE CDFATUDIVER = C_FAT_DIVERSOS.CDFATUDIVER;

      INSERT INTO DM_RESULT_OPERACIONAL
             (DTRESUMO, CDEMPRESA, NMEMPRESA, CDREGIONAL, NMREGIONAL, CDSEGMERCADO, NMSEGMERCADO, CDSUPERVISOR, NMSUPERVISOR,
              CDFILIAL, NMFILIAL, CDPAIS, NMPAIS, SGESTADO, NMESTADO, CDMUNICIPIO, NMMUNICIPIO, IDTPLANCROP,
              NRINSJURCLIE, NMCLIENTE, CDSERVICO, NMSERVICO, CDGRUPSERV, NMGRUPSERV, QTSERVICO, VRFATURDIV,
              VRFATURDIVPROJ, VRFATURDIVORCA,
              VRFATBRUTO, VRFATBRUTOPROJ, VRFATBRUTOORCA,
              CDFILIALCENT, NMFILIALCENT, CDTIPOOPER, NMTIPOOPER, IDENTRSAIDOP, VRDEVOLUESTOR,
              VRDEVOLUESTORPROJ, VRDEVOLUESTORORCA, CDRECEXTRA, NMRECEXTRA,
              CDFATUDIVER, NMFATUDIVER, VRFATLIQUIDO,
              VRFATLIQUIDOPROJ, VRFATLIQUIDOORCA, VRMARGEMCONTR, VRMARGEMCONTRPROJ, VRMARGEMCONTRORCA, VRRESULTPARC,
              VRRESULTPARCPROJ, VRRESULTPARCORCA, VRRESULTFINAL,
              VRRESULTFINALPROJ, VRRESULTFINALORCA, NRORG)
           VALUES (C_FAT_DIVERSOS.DTENTRSAID, V_CDEMPRESA, V_NMEMPRESA, V_CDAREAGERE, V_NMAREAGERE, V_CDSEGMER, V_NMSEGMER, V_CDSUPERVISOR, V_NMOPERADOR,
                   V_CDFILIAL, V_NMFILIAL, V_CDPAIS, V_NMPAIS, V_SGESTADO, V_NMESTADO, V_CDMUNICIPIO, V_NMMUNICIPIO, C_FAT_DIVERSOS.IDTPLANCROP,
                   NULL, NULL, NULL, NULL, NULL, NULL, C_FAT_DIVERSOS.QTFATURAMENTO, C_FAT_DIVERSOS.VRFATURAMENTO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FAT_DIVERSOS.VRFATURAMENTO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FAT_DIVERSOS.VRFATURAMENTO,0)
                   END, 0,
                   C_FAT_DIVERSOS.VRFATURAMENTO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FAT_DIVERSOS.VRFATURAMENTO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FAT_DIVERSOS.VRFATURAMENTO,0)
                   END,
                   0,
                   V_CDFILIALCENT, V_NMFILIALCENT, NULL, NULL, NULL, C_FAT_DIVERSOS.VRFATDEVCANC,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FAT_DIVERSOS.VRFATDEVCANC,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FAT_DIVERSOS.VRFATDEVCANC,0)
                   END, 0, NULL, NULL,
                   V_CDFATUDIVER, V_DSFATUDIVER, C_FAT_DIVERSOS.VRFATURAMENTO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FAT_DIVERSOS.VRFATURAMENTO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FAT_DIVERSOS.VRFATURAMENTO,0)
                   END, 0, C_FAT_DIVERSOS.VRFATURAMENTO, 
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FAT_DIVERSOS.VRFATURAMENTO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FAT_DIVERSOS.VRFATURAMENTO,0)
                   END, 0, C_FAT_DIVERSOS.VRFATURAMENTO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FAT_DIVERSOS.VRFATURAMENTO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FAT_DIVERSOS.VRFATURAMENTO,0)
                   END, 0,
                   C_FAT_DIVERSOS.VRFATURAMENTO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FAT_DIVERSOS.VRFATURAMENTO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FAT_DIVERSOS.VRFATURAMENTO,0)
                   END, 0, P_NRORG);
   END LOOP;

   FOR C_FAT_FORSALE IN (SELECT F.CDFILIAL, F.DTENTRSAID, F.CDCLIENTE, F.DSFATURAMENTO,
                                F.CDSERVFILI, F.IDTPLANCROP, NVL(SUM(F.QTFATURAMENTO),0) QTFATURAMENTO,
                                NVL(SUM(CASE WHEN F.IDSTATUS = 'B' THEN F.VRFATURAMENTO ELSE 0 END),0) VRFATURAMENTOB,
                                NVL(SUM(CASE WHEN F.IDSTATUS = 'V' THEN F.VRFATURAMENTO ELSE 0 END),0) VRFATURAMENTOV,
                                NVL(SUM(CASE WHEN F.IDSTATUS = 'D' THEN F.VRFATURAMENTO ELSE 0 END),0) VRFATURAMENTOD,
                                NVL(SUM(CASE WHEN F.IDSTATUS = 'C' THEN F.VRFATURAMENTO ELSE 0 END),0) VRFATURAMENTOC,
                                CDTIPOOPER, IDENTRSAIDOP, NVL(SUM(VRFATDEVCANC),0) VRFATDEVCANC, NRRECEXTRAFILI, CDFATUDIVER
                           FROM FATURAMENTOROP F
                          WHERE F.IDTPLANCROP = 'FAT_FORSALE'
                            AND F.CDFILIAL = P_CDFILIAL
                            AND F.DTENTRSAID BETWEEN P_DTINICIAL AND P_DTFINAL
                          GROUP BY F.CDFILIAL, F.DTENTRSAID, F.CDCLIENTE, F.DSFATURAMENTO,
                                F.CDSERVFILI, F.IDTPLANCROP, F.CDFILIALCENT, CDTIPOOPER, IDENTRSAIDOP, NRRECEXTRAFILI, CDFATUDIVER) LOOP

      INSERT INTO DM_RESULT_OPERACIONAL
             (DTRESUMO, CDEMPRESA, NMEMPRESA, CDREGIONAL, NMREGIONAL, CDSEGMERCADO, NMSEGMERCADO, CDSUPERVISOR, NMSUPERVISOR,
              CDFILIAL, NMFILIAL, CDPAIS, NMPAIS, SGESTADO, NMESTADO, CDMUNICIPIO, NMMUNICIPIO, IDTPLANCROP,
              NRINSJURCLIE, NMCLIENTE, CDSERVICO, NMSERVICO, CDGRUPSERV, NMGRUPSERV, QTSERVICO, VRFATURDIV, VRFATBRUTO,
              VRFATBRUTOPROJ, VRFATBRUTOORCA,
              CDFILIALCENT, NMFILIALCENT, CDTIPOOPER, NMTIPOOPER, IDENTRSAIDOP, VRDEVOLUESTOR,
              VRDEVOLUESTORPROJ, VRDEVOLUESTORORCA, CDRECEXTRA, NMRECEXTRA,
              CDFATUDIVER, NMFATUDIVER, VRFATURFOSCONT, VRFATURFOSBRUT, VRFATURFOSBRUTPROJ, VRFATURFOSBRUTORCA,
              VRFATURFOSCANC, VRFATURFOSDESC, VRFATURFOSDESCPROJ, VRFATURFOSDESCORCA,
              VRDEDUSOBVENDA, VRDEDUSOBVENDAPROJ, VRDEDUSOBVENDAORCA,
              VRDEDURECEITA, VRDEDURECEITAPROJ, VRDEDURECEITAORCA,
              VRFATLIQUIDO, VRFATLIQUIDOPROJ, VRFATLIQUIDOORCA, VRMARGEMCONTR,
              VRMARGEMCONTRPROJ, VRMARGEMCONTRORCA, VRRESULTPARC,
              VRRESULTPARCPROJ, VRRESULTPARCORCA, VRRESULTFINAL,
              VRRESULTFINALPROJ, VRRESULTFINALORCA, NRORG)
           VALUES (C_FAT_FORSALE.DTENTRSAID, V_CDEMPRESA, V_NMEMPRESA, V_CDAREAGERE, V_NMAREAGERE, V_CDSEGMER, V_NMSEGMER, V_CDSUPERVISOR, V_NMOPERADOR,
                   V_CDFILIAL, V_NMFILIAL, V_CDPAIS, V_NMPAIS, V_SGESTADO, V_NMESTADO, V_CDMUNICIPIO, V_NMMUNICIPIO, C_FAT_FORSALE.IDTPLANCROP,
                   NULL, NULL, NULL, NULL, NULL, NULL, C_FAT_FORSALE.QTFATURAMENTO, NULL, C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV,0)
                   END,
                   0,
                   V_CDFILIALCENT, V_NMFILIALCENT, NULL, NULL, NULL, C_FAT_FORSALE.VRFATDEVCANC,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FAT_FORSALE.VRFATDEVCANC,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FAT_FORSALE.VRFATDEVCANC,0)
                   END,0,
                   NULL, C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV,
                   V_CDFATUDIVER, V_DSFATUDIVER, C_FAT_FORSALE.VRFATURAMENTOC, C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV,0)
                   END, 0,
                   C_FAT_FORSALE.VRFATURAMENTOV, C_FAT_FORSALE.VRFATURAMENTOD*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FAT_FORSALE.VRFATURAMENTOD,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FAT_FORSALE.VRFATURAMENTOD,0)
                   END*(-1), 0,
                   C_FAT_FORSALE.VRFATURAMENTOD,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FAT_FORSALE.VRFATURAMENTOD,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FAT_FORSALE.VRFATURAMENTOD,0)
                   END, 0,
                   C_FAT_FORSALE.VRFATURAMENTOD,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FAT_FORSALE.VRFATURAMENTOD,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FAT_FORSALE.VRFATURAMENTOD,0)
                   END, 0,
                   C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV-C_FAT_FORSALE.VRFATURAMENTOD,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV-C_FAT_FORSALE.VRFATURAMENTOD,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV-C_FAT_FORSALE.VRFATURAMENTOD,0)
                   END, 0,
                   C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV-C_FAT_FORSALE.VRFATURAMENTOD,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV-C_FAT_FORSALE.VRFATURAMENTOD,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV-C_FAT_FORSALE.VRFATURAMENTOD,0)
                   END, 0,
                   C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV-C_FAT_FORSALE.VRFATURAMENTOD,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV-C_FAT_FORSALE.VRFATURAMENTOD,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV-C_FAT_FORSALE.VRFATURAMENTOD,0)
                   END, 0,
                   C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV-C_FAT_FORSALE.VRFATURAMENTOD,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV-C_FAT_FORSALE.VRFATURAMENTOD,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_FAT_FORSALE.VRFATURAMENTOB-C_FAT_FORSALE.VRFATURAMENTOV-C_FAT_FORSALE.VRFATURAMENTOD,0)
                   END, 0, P_NRORG);
   END LOOP;

   FOR C_DEDUCAOROPNF IN (SELECT D.CDIMPOSTO, D.DSDEDUCAO, D.CDCLIENTE,
                                 SUM(CASE WHEN D.IDTPLANCROP = 'CONTA_PAGAR'
                                          THEN NVL(D.VRDEDUCAO,0)
                                          ELSE CASE R.IDTRATIMPROP WHEN 'D' THEN NVL(D.VRDEDUCAO,0)*(-1)
                                                                   WHEN 'V' THEN 0
                                                                            ELSE NVL(D.VRDEDUCAO,0)
                                               END
                                 END) VRDEDUCAO, D.DTREFERENC,
                                 D.IDTPLANCROP, D.CDSERVFILI, D.CDTPCTPAGAR
                            FROM DEDUCAOROP D  LEFT JOIN IMPOSTOROP R ON D.CDIMPOSTO = R.CDIMPOSTO
                                                                     AND R.CDFILIAL = TIPOPARAM_DRO(D.CDFILIAL)
                                                                     AND ((D.IDTPLANCROP = 'FAT_NOTAFISCAL' AND R.IDTPIMPOSTOROP IN ('FS')) OR
                                                                          (D.IDTPLANCROP = 'FAT_EFETIVO' AND R.IDTPIMPOSTOROP IN ('FE')))
                                               LEFT JOIN PARFILIALROP P ON P.CDFILIAL = TIPOPARAM_DRO(D.CDFILIAL)
                                                                       AND (R.IDTPIMPOSTOROP = P.IDFATUROP OR P.IDFATUROP = 'AB')
                           WHERE D.IDTPLANCROP IN ('FAT_NOTAFISCAL','FAT_EFETIVO', 'CONTA_PAGAR' /*, 'FAT_FORSALE'*/)
                             AND D.CDFILIAL = P_CDFILIAL
                             AND D.DTREFERENC BETWEEN P_DTINICIAL AND P_DTFINAL
                           GROUP BY D.CDIMPOSTO, D.DSDEDUCAO, D.CDCLIENTE, D.DTREFERENC,
                                    D.IDTPLANCROP, D.CDSERVFILI, D.CDTPCTPAGAR) LOOP

      SELECT MAX(C.NRINSJURCLIE), MAX(C.NMFANTCLIE)
        INTO V_NRINSJURCLIE, V_NMCLIENTE
        FROM CLIENTE C
       WHERE C.CDCLIENTE = C_DEDUCAOROPNF.CDCLIENTE;

      SELECT MAX(T.CDTPCTPAGAR), MAX(T.DSTPCTPAGAR)
        INTO V_CDTPCTPAGAR, V_DSTPCTPAGAR
        FROM TIPOCTPAGAR T
       WHERE T.CDTPCTPAGAR = C_DEDUCAOROPNF.CDTPCTPAGAR;

      SELECT MAX(G.CDGRUPSERV), MAX(G.NMGRUPSERV), MAX(S.NMSERVICO)
        INTO V_CDGRUPSERV, V_NMGRUPSERV, V_NMSERVICO
        FROM SERVICO S, GRUPSERV G
       WHERE S.CDFILIAL = P_CDFILIAL
         AND S.CDSERVFILI = C_DEDUCAOROPNF.CDSERVFILI
         AND S.CDGRUPSERV = G.CDGRUPSERV;

      INSERT INTO DM_RESULT_OPERACIONAL
             (DTRESUMO, CDEMPRESA, NMEMPRESA, CDREGIONAL, NMREGIONAL, CDSEGMERCADO, NMSEGMERCADO, CDSUPERVISOR, NMSUPERVISOR,
              CDFILIAL, NMFILIAL, CDPAIS, NMPAIS, SGESTADO, NMESTADO, CDMUNICIPIO, NMMUNICIPIO, IDTPLANCROP,
              NRINSJURCLIE, NMCLIENTE, CDSERVICO, NMSERVICO, CDGRUPSERV, NMGRUPSERV, QTSERVICO, VRFATURDIV, VRFATBRUTO,
              CDFILIALCENT, NMFILIALCENT, CDTIPOOPER, NMTIPOOPER, IDENTRSAIDOP, VRDEVOLUESTOR, CDRECEXTRA, NMRECEXTRA,
              CDFATUDIVER, NMFATUDIVER, CDIMPOSTO, NMIMPOSTO, VRDEDURECEITA, VRDEDURECEITAPROJ, VRDEDURECEITAORCA, VRIMPOSTO,
              VRIMPOSTOPROJ, VRIMPOSTOORCA,
              CDTPCTPAGAR, NMTPCTPAGAR, VRFATLIQUIDO, VRFATLIQUIDOPROJ, VRFATLIQUIDOORCA, VRMARGEMCONTR,
              VRMARGEMCONTRPROJ, VRMARGEMCONTRORCA, VRRESULTPARC,
              VRRESULTPARCPROJ, VRRESULTPARCORCA, VRRESULTFINAL, VRRESULTFINALPROJ, VRRESULTFINALORCA, NRORG)
           VALUES (C_DEDUCAOROPNF.DTREFERENC, V_CDEMPRESA, V_NMEMPRESA, V_CDAREAGERE, V_NMAREAGERE, V_CDSEGMER, V_NMSEGMER, V_CDSUPERVISOR, V_NMOPERADOR,
                   V_CDFILIAL, V_NMFILIAL, V_CDPAIS, V_NMPAIS, V_SGESTADO, V_NMESTADO, V_CDMUNICIPIO, V_NMMUNICIPIO, C_DEDUCAOROPNF.IDTPLANCROP,
                   V_NRINSJURCLIE, V_NMCLIENTE, C_DEDUCAOROPNF.CDSERVFILI, V_NMSERVICO, V_CDGRUPSERV, V_NMGRUPSERV, 0, 0, 0,
                   V_CDFILIALCENT, V_NMFILIALCENT, NULL, NULL, NULL, NULL, NULL, NULL,
                   V_CDFATUDIVER, V_DSFATUDIVER, C_DEDUCAOROPNF.CDIMPOSTO, C_DEDUCAOROPNF.DSDEDUCAO, C_DEDUCAOROPNF.VRDEDUCAO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPNF.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPNF.VRDEDUCAO,0)
                   END, 0, C_DEDUCAOROPNF.VRDEDUCAO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPNF.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPNF.VRDEDUCAO,0)
                   END, 0,
                   C_DEDUCAOROPNF.CDTPCTPAGAR, V_DSTPCTPAGAR, C_DEDUCAOROPNF.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPNF.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPNF.VRDEDUCAO,0)
                   END*(-1), 0, C_DEDUCAOROPNF.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPNF.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPNF.VRDEDUCAO,0)
                   END*(-1), 0, C_DEDUCAOROPNF.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPNF.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPNF.VRDEDUCAO,0)
                   END*(-1), 0, C_DEDUCAOROPNF.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPNF.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPNF.VRDEDUCAO,0)
                   END*(-1), 0, P_NRORG);
   END LOOP;

   FOR C_DEDUCAOROPFS IN (SELECT D.CDIMPOSTO, D.DSDEDUCAO, D.CDCLIENTE, NVL(SUM(D.VRDEDUCAO),0) VRDEDUCAO, D.DTREFERENC,
                                 D.IDTPLANCROP, D.CDSERVFILI, D.CDTPCTPAGAR
                            FROM DEDUCAOROP D
                           WHERE D.IDTPLANCROP IN ('FAT_FORSALE')
                             AND D.CDFILIAL = P_CDFILIAL
                             AND D.DTREFERENC BETWEEN P_DTINICIAL AND P_DTFINAL
                           GROUP BY D.CDIMPOSTO, D.DSDEDUCAO, D.CDCLIENTE, D.DTREFERENC,
                                    D.IDTPLANCROP, D.CDSERVFILI, D.CDTPCTPAGAR) LOOP

      INSERT INTO DM_RESULT_OPERACIONAL
             (DTRESUMO, CDEMPRESA, NMEMPRESA, CDREGIONAL, NMREGIONAL, CDSEGMERCADO, NMSEGMERCADO, CDSUPERVISOR, NMSUPERVISOR,
              CDFILIAL, NMFILIAL, CDPAIS, NMPAIS, SGESTADO, NMESTADO, CDMUNICIPIO, NMMUNICIPIO, IDTPLANCROP,
              NRINSJURCLIE, NMCLIENTE, CDSERVICO, NMSERVICO, CDGRUPSERV, NMGRUPSERV, QTSERVICO, VRFATURDIV, VRFATBRUTO,
              CDFILIALCENT, NMFILIALCENT, CDTIPOOPER, NMTIPOOPER, IDENTRSAIDOP, VRDEVOLUESTOR, CDRECEXTRA, NMRECEXTRA,
              CDFATUDIVER, NMFATUDIVER, CDIMPOSTO, NMIMPOSTO, VRDEDURECEITA, VRDEDURECEITAPROJ, VRDEDURECEITAORCA, VRIMPOFOS,
              VRIMPOFOSPROJ, VRIMPOFOSORCA,
              CDTPCTPAGAR, NMTPCTPAGAR, VRFATLIQUIDO, VRFATLIQUIDOPROJ, VRFATLIQUIDOORCA, VRMARGEMCONTR,
              VRMARGEMCONTRPROJ, VRMARGEMCONTRORCA, VRRESULTPARC,
              VRRESULTPARCPROJ, VRRESULTPARCORCA, VRRESULTFINAL, VRRESULTFINALPROJ, VRRESULTFINALORCA, NRORG)
           VALUES (C_DEDUCAOROPFS.DTREFERENC, V_CDEMPRESA, V_NMEMPRESA, V_CDAREAGERE, V_NMAREAGERE, V_CDSEGMER, V_NMSEGMER, V_CDSUPERVISOR, V_NMOPERADOR,
                   V_CDFILIAL, V_NMFILIAL, V_CDPAIS, V_NMPAIS, V_SGESTADO, V_NMESTADO, V_CDMUNICIPIO, V_NMMUNICIPIO, C_DEDUCAOROPFS.IDTPLANCROP,
                   V_NRINSJURCLIE, V_NMCLIENTE, C_DEDUCAOROPFS.CDSERVFILI, V_NMSERVICO, V_CDGRUPSERV, V_NMGRUPSERV, 0, 0, 0,
                   V_CDFILIALCENT, V_NMFILIALCENT, NULL, NULL, NULL, NULL, NULL, NULL,
                   V_CDFATUDIVER, V_DSFATUDIVER, C_DEDUCAOROPFS.CDIMPOSTO, C_DEDUCAOROPFS.DSDEDUCAO, C_DEDUCAOROPFS.VRDEDUCAO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPFS.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPFS.VRDEDUCAO,0)
                   END, 0, C_DEDUCAOROPFS.VRDEDUCAO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPFS.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPFS.VRDEDUCAO,0)
                   END, 0,
                   C_DEDUCAOROPFS.CDTPCTPAGAR, V_DSTPCTPAGAR, C_DEDUCAOROPFS.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPFS.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPFS.VRDEDUCAO,0)
                   END*(-1), 0, C_DEDUCAOROPFS.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPFS.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPFS.VRDEDUCAO,0)
                   END*(-1), 0, C_DEDUCAOROPFS.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPFS.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPFS.VRDEDUCAO,0)
                   END*(-1), 0, C_DEDUCAOROPFS.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPFS.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPFS.VRDEDUCAO,0)
                   END*(-1), 0, P_NRORG);
   END LOOP;

   FOR C_DEDUCAOROPCRE IN (SELECT D.DTREFERENC, D.CDCLIENTE, D.CDFORNECED, D.IDTPLANCROP, D.CDIMPOSTO, I.NMIMPOSTO,
                                  NVL(SUM(CASE R.IDTRATIMPROP WHEN 'D' THEN NVL(D.VRDEDUCAO,0)*(-1)
                                                                       ELSE D.VRDEDUCAO
                                          END),0) VRDEDUCAO
                             FROM DEDUCAOROP D INNER JOIN IMPOSTOROP R ON D.CDIMPOSTO = R.CDIMPOSTO
                                                                      AND R.CDFILIAL = TIPOPARAM_DRO(D.CDFILIAL)
                                               INNER JOIN IMPOSTO I ON D.CDIMPOSTO = I.CDIMPOSTO,
                                  PARFILIALROP P
                            WHERE D.CDFILIAL = P_CDFILIAL
                              AND D.DTREFERENC BETWEEN P_DTINICIAL AND P_DTFINAL
                              AND D.IDTPLANCROP = 'CONSUMO_ENTRADA'
                              AND R.IDTPIMPOSTOROP IN ('CC')
                              AND P.CDFILIAL = TIPOPARAM_DRO(D.CDFILIAL)
                              AND P.IDPERCFIXIMPREC <> 'S'
                            GROUP BY D.DTREFERENC, D.CDCLIENTE, D.CDFORNECED, D.IDTPLANCROP, D.CDIMPOSTO, I.NMIMPOSTO
                            UNION ALL
                           SELECT C.DTCONSUMROP DTREFERENC, NULL CDCLIENTE, FO.CDFORNECED, C.IDTPLANCROP, NULL CDIMPOSTO, NULL NMIMPOSTO,
                                  ((NVL(SUM(C.VRITEMLANCROP),0)*NVL(MAX(P.VRPERIMPRECUROP),0))/100) VRDEDUCAO
                             FROM CONSUMOROP C INNER JOIN FILIALROP F ON C.NRFILIALROP = F.NRFILIALROP
                                               INNER JOIN FORNECEDOR FO ON FO.CDFORNECED = C.CDCLIENTEROP,
                                  PARFILIALROP P
                            WHERE C.IDTPLANCROP = 'CONSUMO_ENTRADA'
                              AND F.CDFILIAL = P_CDFILIAL
                              AND F.DTFILIALROP = C.DTCONSUMROP
                              AND F.DTFILIALROP BETWEEN P_DTINICIAL AND P_DTFINAL
                              AND C.CDTIPOOPERROP <> '06'
                              AND P.CDFILIAL = TIPOPARAM_DRO(F.CDFILIAL)
                              AND P.IDPERCFIXIMPREC = 'S'
                            GROUP BY C.DTCONSUMROP, FO.CDFORNECED, C.IDTPLANCROP) LOOP

      SELECT MAX(C.NRINSJURCLIE), MAX(C.NMFANTCLIE)
        INTO V_NRINSJURCLIE, V_NMCLIENTE
        FROM CLIENTE C
       WHERE C.CDCLIENTE = C_DEDUCAOROPCRE.CDCLIENTE;  

      SELECT MAX(F.NRINSJURFORN), MAX(F.NMFANTFORN)
        INTO V_NRINSJURFORN, V_NMFORNECED
        FROM FORNECEDOR F
       WHERE F.CDFORNECED = C_DEDUCAOROPCRE.CDFORNECED;

      INSERT INTO DM_RESULT_OPERACIONAL
             (DTRESUMO, CDEMPRESA, NMEMPRESA, CDREGIONAL, NMREGIONAL, CDSEGMERCADO, NMSEGMERCADO, CDSUPERVISOR, NMSUPERVISOR,
              CDFILIAL, NMFILIAL, CDPAIS, NMPAIS, SGESTADO, NMESTADO, CDMUNICIPIO, NMMUNICIPIO, IDTPLANCROP,
              NRINSJURCLIE, NMCLIENTE, CDSERVICO, NMSERVICO, CDGRUPSERV, NMGRUPSERV, QTSERVICO, VRFATURDIV, VRFATBRUTO,
              CDFILIALCENT, NMFILIALCENT, CDTIPOOPER, NMTIPOOPER, IDENTRSAIDOP, VRDEVOLUESTOR, CDRECEXTRA, NMRECEXTRA,
              CDFATUDIVER, NMFATUDIVER, CDIMPOSTO, NMIMPOSTO, VRDEDURECEITA, VRDEDURECEITAPROJ, VRDEDURECEITAORCA, VRIMPOSTO,
              CDTPCTPAGAR, NMTPCTPAGAR, VRCREDIMPOSTO, VRCREDIMPOSTOPROJ, VRCREDIMPOSTOORCA,
              CDENCARGO, NMENCARGO, NRINSJURFORN, NMFORNECED, VRFATLIQUIDO,
              VRFATLIQUIDOPROJ, VRFATLIQUIDOORCA, VRMARGEMCONTR,
              VRMARGEMCONTRPROJ, VRMARGEMCONTRORCA, VRRESULTPARC,
              VRRESULTPARCPROJ, VRRESULTPARCORCA, VRRESULTFINAL, VRRESULTFINALPROJ, VRRESULTFINALORCA, NRORG)
           VALUES (C_DEDUCAOROPCRE.DTREFERENC, V_CDEMPRESA, V_NMEMPRESA, V_CDAREAGERE, V_NMAREAGERE, V_CDSEGMER, V_NMSEGMER, V_CDSUPERVISOR, V_NMOPERADOR,
                   V_CDFILIAL, V_NMFILIAL, V_CDPAIS, V_NMPAIS, V_SGESTADO, V_NMESTADO, V_CDMUNICIPIO, V_NMMUNICIPIO, C_DEDUCAOROPCRE.IDTPLANCROP,
                   V_NRINSJURCLIE, V_NMCLIENTE, NULL, NULL, NULL, NULL, 0, 0, 0,
                   V_CDFILIALCENT, V_NMFILIALCENT, NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, C_DEDUCAOROPCRE.CDIMPOSTO, C_DEDUCAOROPCRE.NMIMPOSTO, C_DEDUCAOROPCRE.VRDEDUCAO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPCRE.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPCRE.VRDEDUCAO,0)
                   END, 0, NULL,
                   NULL, NULL, C_DEDUCAOROPCRE.VRDEDUCAO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPCRE.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPCRE.VRDEDUCAO,0)
                   END, 0, NULL, NULL, V_NRINSJURFORN, V_NMFORNECED, C_DEDUCAOROPCRE.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPCRE.VRDEDUCAO*(-1),0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPCRE.VRDEDUCAO*(-1),0)
                   END, 0,
                   C_DEDUCAOROPCRE.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPCRE.VRDEDUCAO*(-1),0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPCRE.VRDEDUCAO*(-1),0)
                   END, 0, C_DEDUCAOROPCRE.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPCRE.VRDEDUCAO*(-1),0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPCRE.VRDEDUCAO*(-1),0)
                   END, 0, C_DEDUCAOROPCRE.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPCRE.VRDEDUCAO*(-1),0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPCRE.VRDEDUCAO*(-1),0)
                   END, 0, P_NRORG);
   END LOOP;

   FOR C_DEDUCAOROPDES IN (SELECT F.DTENTRSAID, F.CDCLIENTE, F.IDTPLANCROP,
                                  NVL(SUM(I.VRITEMENCA*(CASE E.IDENCARGO WHEN 'A' THEN (-1)
                                                                         WHEN 'V' THEN 0
                                                                         ELSE 1
                                                        END)),0) VRDEDUCAO, E.CDENCARGO
                             FROM FATURAMENTOROP F INNER JOIN ITEMENCARGO I ON F.CDFILIALLANC = I.CDFILIAL
                                                                           AND F.NRLANCTONF = I.NRLANCTONF
                                                   INNER JOIN DEDURECEITAROP D ON D.CDFILIAL = P_CDFILIAL
                                                                              AND D.CDENCARGO = I.CDENCARGO
                                                   INNER JOIN ENCARGO E ON I.CDENCARGO = E.CDENCARGO
                            WHERE F.IDTPLANCROP = 'FAT_NOTAFISCAL'
                              AND F.CDFILIAL = P_CDFILIAL
                              AND F.DTENTRSAID BETWEEN P_DTINICIAL AND P_DTFINAL
                            GROUP BY F.DTENTRSAID, F.CDCLIENTE, F.IDTPLANCROP, E.CDENCARGO) LOOP

      SELECT MAX(C.NRINSJURCLIE), MAX(C.NMFANTCLIE)
        INTO V_NRINSJURCLIE, V_NMCLIENTE
        FROM CLIENTE C
       WHERE C.CDCLIENTE = C_DEDUCAOROPDES.CDCLIENTE;

      SELECT MAX(NMENCARGO)
        INTO V_NMENCARGO
        FROM ENCARGO
       WHERE CDENCARGO = C_DEDUCAOROPDES.CDENCARGO;

      INSERT INTO DM_RESULT_OPERACIONAL
             (DTRESUMO, CDEMPRESA, NMEMPRESA, CDREGIONAL, NMREGIONAL, CDSEGMERCADO, NMSEGMERCADO, CDSUPERVISOR, NMSUPERVISOR,
              CDFILIAL, NMFILIAL, CDPAIS, NMPAIS, SGESTADO, NMESTADO, CDMUNICIPIO, NMMUNICIPIO, IDTPLANCROP,
              NRINSJURCLIE, NMCLIENTE, CDSERVICO, NMSERVICO, CDGRUPSERV, NMGRUPSERV, QTSERVICO, VRFATURDIV, VRFATBRUTO,
              CDFILIALCENT, NMFILIALCENT, CDTIPOOPER, NMTIPOOPER, IDENTRSAIDOP, VRDEVOLUESTOR, CDRECEXTRA, NMRECEXTRA,
              CDFATUDIVER, NMFATUDIVER, CDIMPOSTO, NMIMPOSTO, VRDEDURECEITA, VRDEDURECEITAPROJ, VRDEDURECEITAORCA, VRIMPOSTO,
              CDTPCTPAGAR, NMTPCTPAGAR, VRDEDUSOBVENDA,
              VRDEDUSOBVENDAPROJ, VRDEDUSOBVENDAORCA, CDENCARGO, NMENCARGO, VRFATLIQUIDO,
              VRFATLIQUIDOPROJ, VRFATLIQUIDOORCA,
              VRMARGEMCONTR, VRMARGEMCONTRPROJ, VRMARGEMCONTRORCA, VRRESULTPARC,
              VRRESULTPARCPROJ, VRRESULTPARCORCA, VRRESULTFINAL, VRRESULTFINALPROJ, VRRESULTFINALORCA, NRORG)
           VALUES (C_DEDUCAOROPDES.DTENTRSAID, V_CDEMPRESA, V_NMEMPRESA, V_CDAREAGERE, V_NMAREAGERE, V_CDSEGMER, V_NMSEGMER, V_CDSUPERVISOR, V_NMOPERADOR,
                   V_CDFILIAL, V_NMFILIAL, V_CDPAIS, V_NMPAIS, V_SGESTADO, V_NMESTADO, V_CDMUNICIPIO, V_NMMUNICIPIO, C_DEDUCAOROPDES.IDTPLANCROP,
                   V_NRINSJURCLIE, V_NMCLIENTE, NULL, NULL, NULL, NULL, 0, 0, 0,
                   V_CDFILIALCENT, V_NMFILIALCENT, NULL, NULL, NULL, NULL, NULL, NULL,
                   V_CDFATUDIVER, V_DSFATUDIVER, NULL, NULL, C_DEDUCAOROPDES.VRDEDUCAO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPDES.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPDES.VRDEDUCAO,0)
                   END, 0, NULL,
                   NULL, NULL, C_DEDUCAOROPDES.VRDEDUCAO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPDES.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPDES.VRDEDUCAO,0)
                   END, 0, C_DEDUCAOROPDES.CDENCARGO, V_NMENCARGO, C_DEDUCAOROPDES.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPDES.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPDES.VRDEDUCAO,0)
                   END*(-1), 0,
                   C_DEDUCAOROPDES.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPDES.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPDES.VRDEDUCAO,0)
                   END*(-1), 0, C_DEDUCAOROPDES.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPDES.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPDES.VRDEDUCAO,0)
                   END*(-1), 0, C_DEDUCAOROPDES.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPDES.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPDES.VRDEDUCAO,0)
                   END*(-1), 0, P_NRORG);
   END LOOP;

   FOR C_DEDUCAOROPENC IN (SELECT F.DTENTRSAID, F.CDCLIENTE, F.IDTPLANCROP,
                                  NVL(SUM(I.VRITEMENCA*(CASE E.IDENCARGO WHEN 'A' THEN (-1)
                                                          WHEN 'V' THEN 0
                                                          ELSE 1
                                         END)),0) VRDEDUCAO, E.CDENCARGO
                             FROM FATURAMENTOROP F INNER JOIN ITEMENCARGO I ON F.CDFILIALLANC = I.CDFILIAL
                                                                           AND F.NRLANCTONF = I.NRLANCTONF
                                                   INNER JOIN ENCARGO E ON I.CDENCARGO = E.CDENCARGO
                            WHERE F.IDTPLANCROP = 'FAT_NOTAFISCAL'
                              AND F.CDFILIAL = V_CDFILIAL
                              AND F.DTENTRSAID BETWEEN P_DTINICIAL AND P_DTFINAL
                              AND NOT EXISTS (SELECT 1
                                                FROM DEDURECEITAROP D
                                               WHERE D.CDFILIAL = P_CDFILIAL
                                                 AND D.CDENCARGO = I.CDENCARGO)
                            GROUP BY F.DTENTRSAID, F.CDCLIENTE, F.IDTPLANCROP, E.CDENCARGO) LOOP

      SELECT MAX(C.NRINSJURCLIE), MAX(C.NMFANTCLIE)
        INTO V_NRINSJURCLIE, V_NMCLIENTE
        FROM CLIENTE C
       WHERE C.CDCLIENTE = C_DEDUCAOROPENC.CDCLIENTE;

      SELECT MAX(NMENCARGO)
        INTO V_NMENCARGO
        FROM ENCARGO
       WHERE CDENCARGO = C_DEDUCAOROPENC.CDENCARGO;

      INSERT INTO DM_RESULT_OPERACIONAL
             (DTRESUMO, CDEMPRESA, NMEMPRESA, CDREGIONAL, NMREGIONAL, CDSEGMERCADO, NMSEGMERCADO, CDSUPERVISOR, NMSUPERVISOR,
              CDFILIAL, NMFILIAL, CDPAIS, NMPAIS, SGESTADO, NMESTADO, CDMUNICIPIO, NMMUNICIPIO, IDTPLANCROP,
              NRINSJURCLIE, NMCLIENTE, CDSERVICO, NMSERVICO, CDGRUPSERV, NMGRUPSERV, QTSERVICO, VRFATURDIV, VRFATBRUTO,
              CDFILIALCENT, NMFILIALCENT, CDTIPOOPER, NMTIPOOPER, IDENTRSAIDOP, VRDEVOLUESTOR, CDRECEXTRA, NMRECEXTRA,
              CDFATUDIVER, NMFATUDIVER, CDIMPOSTO, NMIMPOSTO, VRDEDURECEITA, VRDEDURECEITAPROJ, VRDEDURECEITAORCA, VRIMPOSTO,
              CDTPCTPAGAR, NMTPCTPAGAR, VRENCARGO, VRENCARGOPROJ, VRENCARGOORCA,
              CDENCARGO, NMENCARGO, VRFATLIQUIDO, VRFATLIQUIDOPROJ, VRFATLIQUIDOORCA,
              VRMARGEMCONTR, VRMARGEMCONTRPROJ, VRMARGEMCONTRORCA, VRRESULTPARC,
              VRRESULTPARCPROJ, VRRESULTPARCORCA, VRRESULTFINAL, VRRESULTFINALPROJ, VRRESULTFINALORCA, NRORG)
           VALUES (C_DEDUCAOROPENC.DTENTRSAID, V_CDEMPRESA, V_NMEMPRESA, V_CDAREAGERE, V_NMAREAGERE, V_CDSEGMER, V_NMSEGMER, V_CDSUPERVISOR, V_NMOPERADOR,
                   V_CDFILIAL, V_NMFILIAL, V_CDPAIS, V_NMPAIS, V_SGESTADO, V_NMESTADO, V_CDMUNICIPIO, V_NMMUNICIPIO, C_DEDUCAOROPENC.IDTPLANCROP,
                   V_NRINSJURCLIE, V_NMCLIENTE, NULL, NULL, NULL, NULL, 0, 0, 0,
                   V_CDFILIALCENT, V_NMFILIALCENT, NULL, NULL, NULL, NULL, NULL, NULL,
                   V_CDFATUDIVER, V_DSFATUDIVER, NULL, NULL, C_DEDUCAOROPENC.VRDEDUCAO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPENC.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPENC.VRDEDUCAO,0)
                   END, 0, NULL,
                   NULL, NULL, C_DEDUCAOROPENC.VRDEDUCAO,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPENC.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPENC.VRDEDUCAO,0)
                   END, 0, C_DEDUCAOROPENC.CDENCARGO, V_NMENCARGO, C_DEDUCAOROPENC.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPENC.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPENC.VRDEDUCAO,0)
                   END*(-1), 0,
                   C_DEDUCAOROPENC.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPENC.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPENC.VRDEDUCAO,0)
                   END*(-1), 0, C_DEDUCAOROPENC.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPENC.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPENC.VRDEDUCAO,0)
                   END*(-1), 0, C_DEDUCAOROPENC.VRDEDUCAO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEDUCAOROPENC.VRDEDUCAO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEDUCAOROPENC.VRDEDUCAO,0)
                   END*(-1), 0, P_NRORG);
   END LOOP;

   FOR C_TAXAADM IN (SELECT D.DTDESPESA, D.IDTPLANCROP, NVL(SUM(D.VRDESPESA),0) VRTAXA
                       FROM DESPESASROP D INNER JOIN FILIALROP F ON D.NRFILIALROP = F.NRFILIALROP
                      WHERE D.IDTPLANCROP = 'TAXAADM'
                        AND F.CDFILIAL = V_CDFILIAL
                        AND D.DTDESPESA = F.DTFILIALROP
                        AND F.DTFILIALROP BETWEEN P_DTINICIAL AND P_DTFINAL
                      GROUP BY D.DTDESPESA, D.IDTPLANCROP) LOOP

      INSERT INTO DM_RESULT_OPERACIONAL
             (DTRESUMO, CDEMPRESA, NMEMPRESA, CDREGIONAL, NMREGIONAL, CDSEGMERCADO, NMSEGMERCADO, CDSUPERVISOR, NMSUPERVISOR,
              CDFILIAL, NMFILIAL, CDPAIS, NMPAIS, SGESTADO, NMESTADO, CDMUNICIPIO, NMMUNICIPIO, IDTPLANCROP,
              NRINSJURCLIE, NMCLIENTE, CDSERVICO, NMSERVICO, CDGRUPSERV, NMGRUPSERV, QTSERVICO, VRFATURDIV, VRFATBRUTO,
              CDFILIALCENT, NMFILIALCENT, CDTIPOOPER, NMTIPOOPER, IDENTRSAIDOP, VRDEVOLUESTOR, CDRECEXTRA, NMRECEXTRA,
              CDFATUDIVER, NMFATUDIVER, CDIMPOSTO, NMIMPOSTO, VRDEDURECEITA, VRIMPOSTO,
              CDTPCTPAGAR, NMTPCTPAGAR, VRENCARGO, CDENCARGO, NMENCARGO, VRTAXAADM,
              VRTAXAADMPROJ, VRTAXAADMORCA,
              VRMARGEMCONTR, VRMARGEMCONTRPROJ, VRMARGEMCONTRORCA, VRRESULTPARC,
              VRRESULTPARCPROJ, VRRESULTPARCORCA, VRRESULTFINAL, VRRESULTFINALPROJ, VRRESULTFINALORCA, NRORG)
           VALUES (C_TAXAADM.DTDESPESA, V_CDEMPRESA, V_NMEMPRESA, V_CDAREAGERE, V_NMAREAGERE, V_CDSEGMER, V_NMSEGMER, V_CDSUPERVISOR, V_NMOPERADOR,
                   V_CDFILIAL, V_NMFILIAL, V_CDPAIS, V_NMPAIS, V_SGESTADO, V_NMESTADO, V_CDMUNICIPIO, V_NMMUNICIPIO, C_TAXAADM.IDTPLANCROP,
                   V_NRINSJURCLIE, V_NMCLIENTE, NULL, NULL, NULL, NULL, 0, 0, 0,
                   V_CDFILIALCENT, V_NMFILIALCENT, NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, C_TAXAADM.VRTAXA,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_TAXAADM.VRTAXA,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_TAXAADM.VRTAXA,0)
                   END, 0,
                   C_TAXAADM.VRTAXA*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_TAXAADM.VRTAXA*(-1),0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_TAXAADM.VRTAXA*(-1),0)
                   END, 0, C_TAXAADM.VRTAXA*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_TAXAADM.VRTAXA*(-1),0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_TAXAADM.VRTAXA*(-1),0)
                   END, 0, C_TAXAADM.VRTAXA*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_TAXAADM.VRTAXA*(-1),0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_TAXAADM.VRTAXA*(-1),0)
                   END, 0, P_NRORG);
   END LOOP;

   FOR C_CONSUMO IN (SELECT A.NMSUBGRPROD, NVL(SUM(A.VRESTOQINI),0) VRESTOQINI, NVL(SUM(A.VRIMPLAEST),0) VRIMPLAEST,
                            NVL(SUM(A.VRENTRCOMP),0) VRENTRCOMP, NVL(SUM(A.VROUTRENTR),0) VROUTRENTR, NVL(SUM(A.VRCONTRANS),0) VRCONTRANS,
                            NVL(SUM(A.VRAJUSTES),0) VRAJUSTES, NVL(SUM(A.VRSAIDDEVO),0) VRSAIDDEVO, NVL(SUM(A.VROUTRSAID),0) VROUTRSAID,
                            NVL(SUM(A.VRRETIAJUS),0) VRRETIAJUS, NVL(SUM(A.VRESTOQFIN),0) VRESTOQFIN, NVL(SUM(A.VRCMV),0) VRCMV,
                            NVL(SUM(A.VRCONSUMO),0) VRCONSUMO, NVL(SUM(A.VRBAIXESTO),0) VRBAIXESTO, NVL(SUM(A.VRDIFCMVCONS),0) VRDIFCMVCONS,
                            D.DTDATA, 'CONSUMO' IDTPLANCROP, A.CDGRUPPROD, A.NMGRUPPROD, A.CDSUBGRUPPROD
                       FROM DATA D, TABLE(APURCONS_ROP(P_CDFILIAL, D.DTDATA, D.DTDATA)) A
                      WHERE D.DTDATA BETWEEN P_DTINICIAL AND P_DTFINAL
                        AND UPPER(A.NMSUBGRPROD) <> 'VALOR TOTAL'
                      GROUP BY A.NMSUBGRPROD, D.DTDATA, A.CDGRUPPROD, A.NMGRUPPROD, A.CDSUBGRUPPROD
                      ORDER BY DTDATA) LOOP

      INSERT INTO DM_RESULT_OPERACIONAL
             (DTRESUMO, CDEMPRESA, NMEMPRESA, CDREGIONAL, NMREGIONAL, CDSEGMERCADO, NMSEGMERCADO, CDSUPERVISOR, NMSUPERVISOR,
              CDFILIAL, NMFILIAL, CDPAIS, NMPAIS, SGESTADO, NMESTADO, CDMUNICIPIO, NMMUNICIPIO, IDTPLANCROP,
              NRINSJURCLIE, NMCLIENTE, CDSERVICO, NMSERVICO, CDGRUPSERV, NMGRUPSERV, QTSERVICO, VRFATURDIV, VRFATBRUTO,
              CDFILIALCENT, NMFILIALCENT, CDTIPOOPER, NMTIPOOPER, IDENTRSAIDOP, VRDEVOLUESTOR, CDRECEXTRA, NMRECEXTRA,
              CDFATUDIVER, NMFATUDIVER, CDIMPOSTO, NMIMPOSTO, VRDEDURECEITA, VRIMPOSTO,
              CDTPCTPAGAR, NMTPCTPAGAR, VRENCARGO, CDENCARGO, NMENCARGO, VRTAXAADM, VRESTOQINICIAL,
              VRIMPLANTAEST, VRENTRCOMPRAS, VROUTRASENTR, VRCONHECTRANS, VRAJUSTEEST, VRSAIDDEVOLU, VROUTRASSAID,
              VRRETIRAAJUST, VRESTOQFINAL, VRCMV, VRCONSUMO, VRCONSUMOPROJ, VRCONSUMOORCA, VRDIFCMVCONS, CDGRUPPROD,
              NMGRUPPROD, CDSUBGRUPPROD, NMSUBGRUPPROD, VRMARGEMCONTR, VRMARGEMCONTRPROJ, VRMARGEMCONTRORCA,
              VRRESULTPARC, VRRESULTPARCPROJ, VRRESULTPARCORCA, VRRESULTFINAL, VRRESULTFINALPROJ, VRRESULTFINALORCA, NRORG)
           VALUES (C_CONSUMO.DTDATA, V_CDEMPRESA, V_NMEMPRESA, V_CDAREAGERE, V_NMAREAGERE, V_CDSEGMER, V_NMSEGMER, V_CDSUPERVISOR, V_NMOPERADOR,
                   V_CDFILIAL, V_NMFILIAL, V_CDPAIS, V_NMPAIS, V_SGESTADO, V_NMESTADO, V_CDMUNICIPIO, V_NMMUNICIPIO, C_CONSUMO.IDTPLANCROP,
                   V_NRINSJURCLIE, V_NMCLIENTE, NULL, NULL, NULL, NULL, 0, 0, 0,
                   V_CDFILIALCENT, V_NMFILIALCENT, NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL, C_CONSUMO.VRESTOQINI,
                   C_CONSUMO.VRIMPLAEST, C_CONSUMO.VRENTRCOMP, C_CONSUMO.VROUTRENTR, C_CONSUMO.VRCONTRANS, C_CONSUMO.VRAJUSTES, C_CONSUMO.VRSAIDDEVO, C_CONSUMO.VROUTRSAID,
                   C_CONSUMO.VRRETIAJUS, C_CONSUMO.VRESTOQFIN, C_CONSUMO.VRCMV, C_CONSUMO.VRCONSUMO,                   
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_CONSUMO.VRCONSUMO,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_CONSUMO.VRCONSUMO,0)
                   END, 0, C_CONSUMO.VRDIFCMVCONS, C_CONSUMO.CDGRUPPROD,
                   C_CONSUMO.NMGRUPPROD, C_CONSUMO.CDSUBGRUPPROD, C_CONSUMO.NMSUBGRPROD, C_CONSUMO.VRCONSUMO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_CONSUMO.VRCONSUMO*(-1),0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_CONSUMO.VRCONSUMO*(-1),0)
                   END, 0,
                   C_CONSUMO.VRCONSUMO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_CONSUMO.VRCONSUMO*(-1),0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_CONSUMO.VRCONSUMO*(-1),0)
                   END, 0, C_CONSUMO.VRCONSUMO*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_CONSUMO.VRCONSUMO*(-1),0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_CONSUMO.VRCONSUMO*(-1),0)
                   END, 0, P_NRORG);
   END LOOP;

   FOR C_DESPESA IN (SELECT D.DTDESPESA, D.CDDESPESA, SUM(D.VRDESPESA) VRDESPESA,
                            CASE WHEN D.IDTIPODESP IS NULL
                                 THEN 'DESPESA_DIV'
                                 WHEN D.IDTIPODESP = 'CONTA_PAGAR'
                                 THEN 'DESPESA_PAG'              
                                 WHEN D.IDTIPODESP = 'LANC_ENTRADA'
                                 THEN 'DESPESA_ENT'              
                                 ELSE ''
                            END IDTPLANCROP, D.CDFORNECED, D.CDTIPOOPER
                       FROM DESPESASROP D INNER JOIN FILIALROP F ON D.NRFILIALROP = F.NRFILIALROP
                      WHERE D.IDTPLANCROP = 'DESPESA'
                        AND F.CDFILIAL = V_CDFILIAL
                        AND D.DTDESPESA = F.DTFILIALROP
                        AND D.DTDESPESA BETWEEN P_DTINICIAL AND P_DTFINAL
                      GROUP BY D.DTDESPESA, D.CDDESPESA, CASE WHEN D.IDTIPODESP IS NULL
                                                              THEN 'DESPESA_DIV'
                                                              WHEN D.IDTIPODESP = 'CONTA_PAGAR'
                                                              THEN 'DESPESA_PAG'
                                                              WHEN D.IDTIPODESP = 'LANC_ENTRADA'
                                                              THEN 'DESPESA_ENT'
                                                              ELSE ''
                                                         END, D.CDFORNECED, D.CDTIPOOPER) LOOP

      V_NMDESPPREV := NULL;
      V_DSTPCTPAGAR := NULL;
      V_CDDESPPREV := NULL;
      V_CDTPCTPAGAR := NULL;
      
      IF C_DESPESA.IDTPLANCROP = 'DESPESA_DIV' THEN
         SELECT MAX(D.CDDESPPREV), MAX(D.NMDESPPREV)
           INTO V_CDDESPPREV, V_NMDESPPREV
           FROM DESPPREV D
          WHERE D.CDDESPPREV = C_DESPESA.CDDESPESA;
      ELSE                                 
         SELECT MAX(T.CDTPCTPAGAR), MAX(T.DSTPCTPAGAR)
           INTO V_CDTPCTPAGAR, V_DSTPCTPAGAR
           FROM TIPOCTPAGAR T
          WHERE T.CDTPCTPAGAR = C_DESPESA.CDDESPESA;
      END IF;           

      SELECT MAX(F.NRINSJURFORN), MAX(F.NMFANTFORN)
        INTO V_NRINSJURFORN, V_NMFORNECED
        FROM FORNECEDOR F
       WHERE F.CDFORNECED = C_DESPESA.CDFORNECED;

      SELECT MAX(CDTIPOOPER), MAX(NMTIPOOPER), MAX(IDENTRSAIDOP)
        INTO V_CDTIPOOPER, V_NMTIPOOPER, V_IDENTRSAIDOP
        FROM TIPOOPERAC
       WHERE CDTIPOOPER = C_DESPESA.CDTIPOOPER
         AND IDENTRSAIDOP = 'E';

      INSERT INTO DM_RESULT_OPERACIONAL
             (DTRESUMO, CDEMPRESA, NMEMPRESA, CDREGIONAL, NMREGIONAL, CDSEGMERCADO, NMSEGMERCADO, CDSUPERVISOR, NMSUPERVISOR,
              CDFILIAL, NMFILIAL, CDPAIS, NMPAIS, SGESTADO, NMESTADO, CDMUNICIPIO, NMMUNICIPIO, IDTPLANCROP,
              NRINSJURCLIE, NMCLIENTE, CDSERVICO, NMSERVICO, CDGRUPSERV, NMGRUPSERV, QTSERVICO, VRFATURDIV, VRFATBRUTO,
              CDFILIALCENT, NMFILIALCENT, CDTIPOOPER, NMTIPOOPER, IDENTRSAIDOP, VRDEVOLUESTOR, CDRECEXTRA, NMRECEXTRA,
              CDFATUDIVER, NMFATUDIVER, CDIMPOSTO, NMIMPOSTO, VRDEDURECEITA, VRIMPOSTO,
              VRENCARGO, CDENCARGO, NMENCARGO, VRTAXAADM, VRESTOQINICIAL,
              VRIMPLANTAEST, VRENTRCOMPRAS, VROUTRASENTR, VRCONHECTRANS, VRAJUSTEEST, VRSAIDDEVOLU, VROUTRASSAID,
              VRRETIRAAJUST, VRESTOQFINAL, VRCMV, VRCONSUMO, VRDIFCMVCONS, CDGRUPPROD,
              NMGRUPPROD, CDSUBGRUPPROD, NMSUBGRUPPROD, CDDESPPREV, NMDESPPREV, VRDESPESATOT,
              VRDESPESATOTPROJ, VRDESPESATOTORCA, VRDESPESA,
              VRDESPESAPROJ, VRDESPESAORCA, CDTPCTPAGAR, NMTPCTPAGAR,
              VRRESULTPARC, VRRESULTPARCPROJ, VRRESULTPARCORCA, VRRESULTFINAL, VRRESULTFINALPROJ, VRRESULTFINALORCA, NRORG)
           VALUES (C_DESPESA.DTDESPESA, V_CDEMPRESA, V_NMEMPRESA, V_CDAREAGERE, V_NMAREAGERE, V_CDSEGMER, V_NMSEGMER, V_CDSUPERVISOR, V_NMOPERADOR,
                   V_CDFILIAL, V_NMFILIAL, V_CDPAIS, V_NMPAIS, V_SGESTADO, V_NMESTADO, V_CDMUNICIPIO, V_NMMUNICIPIO, C_DESPESA.IDTPLANCROP,
                   V_NRINSJURCLIE, V_NMCLIENTE, NULL, NULL, NULL, NULL, 0, 0, 0,
                   V_CDFILIALCENT, V_NMFILIALCENT, V_CDTIPOOPER, V_NMTIPOOPER, V_IDENTRSAIDOP, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, V_CDDESPPREV, V_NMDESPPREV, C_DESPESA.VRDESPESA,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DESPESA.VRDESPESA,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DESPESA.VRDESPESA,0)
                   END, 0, C_DESPESA.VRDESPESA,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DESPESA.VRDESPESA,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DESPESA.VRDESPESA,0)
                   END, 0, V_CDTPCTPAGAR, V_DSTPCTPAGAR,
                   C_DESPESA.VRDESPESA*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DESPESA.VRDESPESA*(-1),0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DESPESA.VRDESPESA*(-1),0)
                   END, 0, C_DESPESA.VRDESPESA*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DESPESA.VRDESPESA*(-1),0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DESPESA.VRDESPESA*(-1),0)
                   END, 0, P_NRORG);
   END LOOP;

   FOR C_AJUSTECT IN (SELECT D.DTDESPESA, D.CDDESPESA, SUM(D.VRDESPESA) VRDESPESA,
                             CASE WHEN D.IDTIPODESP = 'CONTA_PAGAR'
                                  THEN 'DESPESA_AJPAG'
                                  WHEN D.IDTIPODESP = 'CONTA_RECEBER'
                                  THEN 'DESPESA_AJREC'    
                                  WHEN D.IDTIPODESP = 'DEBITO'
                                  THEN 'DESPESA_DEBIT'    
                                  WHEN D.IDTIPODESP = 'CREDITO'
                                  THEN 'DESPESA_CREDI'
                                  ELSE ''
                             END IDTPLANCROP, D.CDFORNECED, D.CDTIPOOPER, D.CDAJUSTECT, D.CDCLIENTE,
                             D.CDTPCREDDEBBCO
                        FROM DESPESASROP D INNER JOIN FILIALROP F ON D.NRFILIALROP = F.NRFILIALROP
                       WHERE D.IDTPLANCROP = 'AJUSTECT'
                         AND F.CDFILIAL = V_CDFILIAL
                         AND D.DTDESPESA = F.DTFILIALROP
                         AND D.DTDESPESA BETWEEN P_DTINICIAL AND P_DTFINAL
                       GROUP BY D.DTDESPESA, D.CDDESPESA, D.CDFORNECED, D.CDTIPOOPER, D.CDAJUSTECT, D.CDCLIENTE,
                             D.CDTPCREDDEBBCO, CASE WHEN D.IDTIPODESP = 'CONTA_PAGAR'
                                                    THEN 'DESPESA_AJPAG'
                                                    WHEN D.IDTIPODESP = 'CONTA_RECEBER'
                                                    THEN 'DESPESA_AJREC'
                                                    WHEN D.IDTIPODESP = 'DEBITO'
                                                    THEN 'DESPESA_DEBIT'
                                                    WHEN D.IDTIPODESP = 'CREDITO'
                                                    THEN 'DESPESA_CREDI'
                                                    ELSE ''
                                               END) LOOP

      V_NMDESPPREV := NULL;
      V_DSTPCTPAGAR := NULL;
      V_CDDESPPREV := NULL;
      V_CDTPCTPAGAR := NULL;

      V_AUX_AJ := 0;
      V_AUX_DEB := 0;
      V_AUX_CRE := 0;

      IF C_AJUSTECT.IDTPLANCROP = 'DESPESA_AJPAG' THEN
         SELECT MAX(CDAJUCTPAG), MAX(DSAJUCTPAG)
           INTO V_CDAJUCTPAG, V_DSAJUCTPAG
           FROM TIPOAJPAGAR T
          WHERE T.CDAJUCTPAG = C_AJUSTECT.CDAJUSTECT;

         SELECT MAX(F.NRINSJURFORN), MAX(F.NMFANTFORN)
           INTO V_NRINSJURFORN, V_NMFORNECED
           FROM FORNECEDOR F
          WHERE F.CDFORNECED = C_AJUSTECT.CDFORNECED;

         SELECT MAX(CDTIPOOPER), MAX(NMTIPOOPER), MAX(IDENTRSAIDOP)
           INTO V_CDTIPOOPER, V_NMTIPOOPER, V_IDENTRSAIDOP
           FROM TIPOOPERAC
          WHERE CDTIPOOPER = C_AJUSTECT.CDTIPOOPER
            AND IDENTRSAIDOP = 'E';
            
         SELECT MAX(T.CDTPCTPAGAR), MAX(T.DSTPCTPAGAR)
           INTO V_CDTPCTPAGAR, V_DSTPCTPAGAR
           FROM TIPOCTPAGAR T
          WHERE T.CDTPCTPAGAR = C_AJUSTECT.CDDESPESA;

         V_AUX_AJ := C_AJUSTECT.VRDESPESA;
      ELSIF C_AJUSTECT.IDTPLANCROP = 'DESPESA_AJREC' THEN
         SELECT MAX(CDAJUCTREC), MAX(DSAJUCTREC)
           INTO V_CDAJUCTREC, V_DSAJUCTREC
           FROM TIPOAJRECEBER T
          WHERE T.CDAJUCTREC = C_AJUSTECT.CDAJUSTECT;

         SELECT MAX(C.NRINSJURCLIE), MAX(C.NMFANTCLIE)
           INTO V_NRINSJURCLIE, V_NMCLIENTE
           FROM CLIENTE C
          WHERE C.CDCLIENTE = C_AJUSTECT.CDCLIENTE;
            
         SELECT MAX(T.CDTPCTRECEB), MAX(T.DSTPCTRECEB)
           INTO V_CDTPCTRECEBER, V_DSTPCTRECEBER
           FROM TIPOCTRECEBER T
          WHERE T.CDTPCTRECEB = C_AJUSTECT.CDDESPESA;

         V_AUX_AJ := C_AJUSTECT.VRDESPESA;          
      ELSIF C_AJUSTECT.IDTPLANCROP = 'DESPESA_DEBIT' THEN
         SELECT MAX(F.NRINSJURFORN), MAX(F.NMFANTFORN)
           INTO V_NRINSJURFORN, V_NMFORNECED
           FROM FORNECEDOR F
          WHERE F.CDFORNECED = C_AJUSTECT.CDFORNECED;

         SELECT MAX(TD.DSTPDEBIBANC)
           INTO V_DSTPDEBIBANC
           FROM TPDEBIBANC TD
          WHERE TD.CDTPDEBIBANC = C_AJUSTECT.CDTPCREDDEBBCO;

         V_AUX_DEB := C_AJUSTECT.VRDESPESA;
      ELSE
         SELECT MAX(C.NRINSJURCLIE), MAX(C.NMFANTCLIE)
           INTO V_NRINSJURCLIE, V_NMCLIENTE
           FROM CLIENTE C
          WHERE C.CDCLIENTE = C_AJUSTECT.CDCLIENTE;

         SELECT MAX(TC.DSTPCREDBANC)
           INTO V_DSTPCREDBANC
           FROM TPCREDBANC TC
          WHERE TC.CDTPCREDBANC = C_AJUSTECT.CDTPCREDDEBBCO;

         V_AUX_CRE := C_AJUSTECT.VRDESPESA;
      END IF;

      INSERT INTO DM_RESULT_OPERACIONAL
             (DTRESUMO, CDEMPRESA, NMEMPRESA, CDREGIONAL, NMREGIONAL, CDSEGMERCADO, NMSEGMERCADO, CDSUPERVISOR, NMSUPERVISOR,
              CDFILIAL, NMFILIAL, CDPAIS, NMPAIS, SGESTADO, NMESTADO, CDMUNICIPIO, NMMUNICIPIO, IDTPLANCROP,
              NRINSJURCLIE, NMCLIENTE, CDSERVICO, NMSERVICO, CDGRUPSERV, NMGRUPSERV, QTSERVICO, VRFATURDIV, VRFATBRUTO,
              CDFILIALCENT, NMFILIALCENT, CDTIPOOPER, NMTIPOOPER, IDENTRSAIDOP, VRDEVOLUESTOR, CDRECEXTRA, NMRECEXTRA,
              CDFATUDIVER, NMFATUDIVER, CDIMPOSTO, NMIMPOSTO, VRDEDURECEITA, VRIMPOSTO,
              VRENCARGO, CDENCARGO, NMENCARGO, VRTAXAADM, VRESTOQINICIAL,
              VRIMPLANTAEST, VRENTRCOMPRAS, VROUTRASENTR, VRCONHECTRANS, VRAJUSTEEST, VRSAIDDEVOLU, VROUTRASSAID,
              VRRETIRAAJUST, VRESTOQFINAL, VRCMV, VRCONSUMO, VRDIFCMVCONS, CDGRUPPROD,
              NMGRUPPROD, CDSUBGRUPPROD, NMSUBGRUPPROD, CDDESPPREV, NMDESPPREV, VRDESPESATOT,
              VRDESPESATOTPROJ, VRDESPESATOTORCA, VRDESPESA, CDTPCTPAGAR, NMTPCTPAGAR,
              CDTPCTRECEBER, NMTPCTRECEBER, VRAJUSTE, VRAJUSTEPROJ, VRAJUSTEORCA, VRDEBITOBCO,
              VRDEBITOBCOPROJ, VRDEBITOBCOORCA, VRCREDITOBCO, VRCREDITOBCOPROJ, VRCREDITOBCOORCA, VRRESULTPARC,
              VRRESULTPARCPROJ, VRRESULTPARCORCA, VRRESULTFINAL, VRRESULTFINALPROJ, VRRESULTFINALORCA, NRORG)
           VALUES (C_AJUSTECT.DTDESPESA, V_CDEMPRESA, V_NMEMPRESA, V_CDAREAGERE, V_NMAREAGERE, V_CDSEGMER, V_NMSEGMER, V_CDSUPERVISOR, V_NMOPERADOR,
                   V_CDFILIAL, V_NMFILIAL, V_CDPAIS, V_NMPAIS, V_SGESTADO, V_NMESTADO, V_CDMUNICIPIO, V_NMMUNICIPIO, C_AJUSTECT.IDTPLANCROP,
                   V_NRINSJURCLIE, V_NMCLIENTE, NULL, NULL, NULL, NULL, 0, 0, 0,
                   V_CDFILIALCENT, V_NMFILIALCENT, V_CDTIPOOPER, V_NMTIPOOPER, V_IDENTRSAIDOP, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, V_CDDESPPREV, V_NMDESPPREV, C_AJUSTECT.VRDESPESA,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_AJUSTECT.VRDESPESA,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_AJUSTECT.VRDESPESA,0)
                   END, 0, NULL, V_CDTPCTPAGAR, V_DSTPCTPAGAR,
                   V_CDTPCTRECEBER, V_DSTPCTRECEBER, V_AUX_AJ,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(V_AUX_AJ,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(V_AUX_AJ,0)
                   END, 0, V_AUX_DEB,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(V_AUX_DEB,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(V_AUX_DEB,0)
                   END, 0, V_AUX_CRE,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(V_AUX_CRE,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(V_AUX_CRE,0)
                   END, 0, C_AJUSTECT.VRDESPESA,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_AJUSTECT.VRDESPESA,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_AJUSTECT.VRDESPESA,0)
                   END, 0, C_AJUSTECT.VRDESPESA,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_AJUSTECT.VRDESPESA,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_AJUSTECT.VRDESPESA,0)
                   END, 0, P_NRORG);
   END LOOP;

   FOR C_MAOOBRA IN (SELECT D.DTDESPESA, D.CDDESPESA, SUM(D.VRDESPESA) VRDESPESA,
                            CASE WHEN D.IDTIPODESP = 'MANUAL'
                                 THEN 'MAOOBRA_MAN'              
                                 WHEN D.IDTIPODESP = 'CONTABIL'
                                 THEN 'DESPESA_CON'              
                                 WHEN D.IDTIPODESP IS NULL
                                 THEN 'DESPESA_CTP'       
                                 WHEN D.IDTIPODESP = 'LANC_ENTRADA'
                                 THEN 'DESPESA_LAN'                   
                                 ELSE ''
                            END IDTPLANCROP, D.CDFORNECED, D.CDTIPOOPER, D.CDPRODUTO
                       FROM DESPESASROP D INNER JOIN FILIALROP F ON D.NRFILIALROP = F.NRFILIALROP
                      WHERE D.IDTPLANCROP = 'MAOOBRA'
                        AND F.CDFILIAL = V_CDFILIAL
                        AND D.DTDESPESA = F.DTFILIALROP
                        AND D.DTDESPESA BETWEEN P_DTINICIAL AND P_DTFINAL
                      GROUP BY D.DTDESPESA, D.CDDESPESA, CASE WHEN D.IDTIPODESP = 'MANUAL'
                                                              THEN 'MAOOBRA_MAN'
                                                              WHEN D.IDTIPODESP = 'CONTABIL'
                                                              THEN 'DESPESA_CON'
                                                              WHEN D.IDTIPODESP IS NULL
                                                              THEN 'DESPESA_CTP'
                                                              WHEN D.IDTIPODESP = 'LANC_ENTRADA'
                                                              THEN 'DESPESA_LAN'
                                                              ELSE ''
                                                         END, D.CDFORNECED, D.CDTIPOOPER, D.CDPRODUTO) LOOP

      SELECT MAX(DF.CDDESPADMFAT), MAX(DF.DSDESPADMFAT)
        INTO V_CDDESPADMFAT, V_DSDESPADMFAT
        FROM DESPADMFATUR DF
       WHERE DF.CDDESPADMFAT = C_MAOOBRA.CDDESPESA;

      SELECT MAX(CDTPCTPAGAR), MAX(DSTPCTPAGAR)
        INTO V_CDTPCTPAGAR, V_DSTPCTPAGAR
        FROM TIPOCTPAGAR
       WHERE CDTPCTPAGAR = C_MAOOBRA.CDDESPESA;

      SELECT MAX(F.NRINSJURFORN), MAX(F.NMFANTFORN)
        INTO V_NRINSJURFORN, V_NMFORNECED
        FROM FORNECEDOR F
       WHERE F.CDFORNECED = C_MAOOBRA.CDFORNECED;

      SELECT MAX(CDTIPOOPER), MAX(NMTIPOOPER), MAX(IDENTRSAIDOP)
        INTO V_CDTIPOOPER, V_NMTIPOOPER, V_IDENTRSAIDOP
        FROM TIPOOPERAC
       WHERE CDTIPOOPER = C_MAOOBRA.CDTIPOOPER
         AND IDENTRSAIDOP = 'E';

      SELECT MAX(CDARVPROD), MAX(NMPRODUTO)
        INTO V_CDARVPROD, V_NMPRODUTO
        FROM PRODUTO
       WHERE CDPRODUTO = C_MAOOBRA.CDPRODUTO;

      INSERT INTO DM_RESULT_OPERACIONAL
             (DTRESUMO, CDEMPRESA, NMEMPRESA, CDREGIONAL, NMREGIONAL, CDSEGMERCADO, NMSEGMERCADO, CDSUPERVISOR, NMSUPERVISOR,
              CDFILIAL, NMFILIAL, CDPAIS, NMPAIS, SGESTADO, NMESTADO, CDMUNICIPIO, NMMUNICIPIO, IDTPLANCROP,
              NRINSJURCLIE, NMCLIENTE, CDSERVICO, NMSERVICO, CDGRUPSERV, NMGRUPSERV, QTSERVICO, VRFATURDIV, VRFATBRUTO,
              CDFILIALCENT, NMFILIALCENT, CDTIPOOPER, NMTIPOOPER, IDENTRSAIDOP, VRDEVOLUESTOR, CDRECEXTRA, NMRECEXTRA,
              CDFATUDIVER, NMFATUDIVER, CDIMPOSTO, NMIMPOSTO, VRDEDURECEITA, VRIMPOSTO,
              VRENCARGO, CDENCARGO, NMENCARGO, VRTAXAADM, VRESTOQINICIAL,
              VRIMPLANTAEST, VRENTRCOMPRAS, VROUTRASENTR, VRCONHECTRANS, VRAJUSTEEST, VRSAIDDEVOLU, VROUTRASSAID,
              VRRETIRAAJUST, VRESTOQFINAL, VRCMV, VRCONSUMO, VRDIFCMVCONS, CDGRUPPROD,
              NMGRUPPROD, CDSUBGRUPPROD, NMSUBGRUPPROD, CDDESPPREV, NMDESPPREV, VRDESPESATOT,
              VRDESPESA, CDTPCTPAGAR, NMTPCTPAGAR,
              CDDESPADMFAT, NMDESPADMFAT, VRMAOOBRA,
              VRMAOOBRAPROJ, VRMAOOBRAORCA, NRINSJURFORN, NMFORNECED, CDARVPROD, NMPRODUTO,
              VRRESULTPARC, VRRESULTPARCPROJ, VRRESULTPARCORCA, VRRESULTFINAL, VRRESULTFINALPROJ, VRRESULTFINALORCA, NRORG)
           VALUES (C_MAOOBRA.DTDESPESA, V_CDEMPRESA, V_NMEMPRESA, V_CDAREAGERE, V_NMAREAGERE, V_CDSEGMER, V_NMSEGMER, V_CDSUPERVISOR, V_NMOPERADOR,
                   V_CDFILIAL, V_NMFILIAL, V_CDPAIS, V_NMPAIS, V_SGESTADO, V_NMESTADO, V_CDMUNICIPIO, V_NMMUNICIPIO, C_MAOOBRA.IDTPLANCROP,
                   V_NRINSJURCLIE, V_NMCLIENTE, NULL, NULL, NULL, NULL, 0, 0, 0,
                   V_CDFILIALCENT, V_NMFILIALCENT, V_CDTIPOOPER, V_NMTIPOOPER, V_IDENTRSAIDOP, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL, NULL, V_CDTPCTPAGAR, V_DSTPCTPAGAR,
                   V_CDDESPADMFAT, V_DSDESPADMFAT, C_MAOOBRA.VRDESPESA,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_MAOOBRA.VRDESPESA,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_MAOOBRA.VRDESPESA,0)
                   END, 0, V_NRINSJURFORN, V_NMFORNECED, V_CDARVPROD, V_NMPRODUTO,
                   C_MAOOBRA.VRDESPESA,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_MAOOBRA.VRDESPESA,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_MAOOBRA.VRDESPESA,0)
                   END, 0, C_MAOOBRA.VRDESPESA,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_MAOOBRA.VRDESPESA,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_MAOOBRA.VRDESPESA,0)
                   END, 0, P_NRORG);
   END LOOP;

   FOR C_DEPRECI IN (SELECT D.DTDESPESA, D.CDDESPESA, SUM(D.VRDESPESA) VRDESPESA,
                            D.IDTPLANCROP
                       FROM DESPESASROP D INNER JOIN FILIALROP F ON D.NRFILIALROP = F.NRFILIALROP
                      WHERE D.IDTPLANCROP = 'DEPRECIACAO'
                        AND F.CDFILIAL = V_CDFILIAL
                        AND D.DTDESPESA = F.DTFILIALROP
                        AND D.DTDESPESA BETWEEN P_DTINICIAL AND P_DTFINAL
                      GROUP BY D.DTDESPESA, D.CDDESPESA, D.IDTPLANCROP) LOOP

      INSERT INTO DM_RESULT_OPERACIONAL
             (DTRESUMO, CDEMPRESA, NMEMPRESA, CDREGIONAL, NMREGIONAL, CDSEGMERCADO, NMSEGMERCADO, CDSUPERVISOR, NMSUPERVISOR,
              CDFILIAL, NMFILIAL, CDPAIS, NMPAIS, SGESTADO, NMESTADO, CDMUNICIPIO, NMMUNICIPIO, IDTPLANCROP,
              NRINSJURCLIE, NMCLIENTE, CDSERVICO, NMSERVICO, CDGRUPSERV, NMGRUPSERV, QTSERVICO, VRFATURDIV, VRFATBRUTO,
              CDFILIALCENT, NMFILIALCENT, CDTIPOOPER, NMTIPOOPER, IDENTRSAIDOP, VRDEVOLUESTOR, CDRECEXTRA, NMRECEXTRA,
              CDFATUDIVER, NMFATUDIVER, CDIMPOSTO, NMIMPOSTO, VRDEDURECEITA, VRIMPOSTO,
              VRENCARGO, CDENCARGO, NMENCARGO, VRTAXAADM, VRESTOQINICIAL,
              VRIMPLANTAEST, VRENTRCOMPRAS, VROUTRASENTR, VRCONHECTRANS, VRAJUSTEEST, VRSAIDDEVOLU, VROUTRASSAID,
              VRRETIRAAJUST, VRESTOQFINAL, VRCMV, VRCONSUMO, VRDIFCMVCONS, CDGRUPPROD,
              NMGRUPPROD, CDSUBGRUPPROD, NMSUBGRUPPROD, CDDESPPREV, NMDESPPREV, VRDESPESATOT, VRDESPESA, CDTPCTPAGAR, NMTPCTPAGAR,
              CDDESPADMFAT, NMDESPADMFAT, VRMAOOBRA, NRINSJURFORN, NMFORNECED, CDARVPROD, NMPRODUTO, VRDEPRECIACAO,
              VRDEPRECIACAOPROJ, VRDEPRECIACAOORCA,
              VRRESULTFINAL, VRRESULTFINALPROJ, VRRESULTFINALORCA, NRORG)
           VALUES (C_DEPRECI.DTDESPESA, V_CDEMPRESA, V_NMEMPRESA, V_CDAREAGERE, V_NMAREAGERE, V_CDSEGMER, V_NMSEGMER, V_CDSUPERVISOR, V_NMOPERADOR,
                   V_CDFILIAL, V_NMFILIAL, V_CDPAIS, V_NMPAIS, V_SGESTADO, V_NMESTADO, V_CDMUNICIPIO, V_NMMUNICIPIO, C_DEPRECI.IDTPLANCROP,
                   V_NRINSJURCLIE, V_NMCLIENTE, NULL, NULL, NULL, NULL, 0, 0, 0,
                   V_CDFILIALCENT, V_NMFILIALCENT, NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL, NULL, C_DEPRECI.VRDESPESA,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEPRECI.VRDESPESA,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEPRECI.VRDESPESA,0)
                   END, 0,
                   C_DEPRECI.VRDESPESA*(-1),
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_DEPRECI.VRDESPESA*(-1),0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_DEPRECI.VRDESPESA*(-1),0)
                   END, 0, P_NRORG);
   END LOOP;

   FOR C_INVESTI IN (SELECT I.DTINVESTIMENTO, I.CDINVEST, SUM(I.VRINVEST) VRINVEST,
                            I.IDTPLANCROP, I.CDFILIALNF
                       FROM INVESTIMENTOROP I INNER JOIN FILIALROP F ON I.NRFILIALROP = F.NRFILIALROP
                      WHERE I.IDTPLANCROP = 'INVESTIMENTO'
                        AND F.CDFILIAL = V_CDFILIAL
                        AND I.DTINVESTIMENTO = F.DTFILIALROP
                        AND I.DTINVESTIMENTO BETWEEN P_DTINICIAL AND P_DTFINAL
                      GROUP BY I.DTINVESTIMENTO, I.CDINVEST, I.IDTPLANCROP, I.CDFILIALNF) LOOP

      V_CDINVESTIMENTO := NULL;
      V_NMINVESTIMENTO := NULL;
      V_CDTPCTPAGAR := NULL;
      V_DSTPCTPAGAR := NULL;

      IF C_INVESTI.CDFILIALNF IS NULL THEN
         SELECT MAX(I.CDINVEST), MAX(I.NMINVEST)
           INTO V_CDINVESTIMENTO, V_NMINVESTIMENTO
           FROM INVESTIMENTOS I
          WHERE I.CDINVEST = C_INVESTI.CDINVEST;
      ELSE
         SELECT MAX(T.CDTPCTPAGAR), MAX(T.DSTPCTPAGAR)
           INTO V_CDTPCTPAGAR, V_DSTPCTPAGAR
           FROM TIPOCTPAGAR T
          WHERE T.CDTPCTPAGAR = C_INVESTI.CDINVEST;
      END IF;

      INSERT INTO DM_RESULT_OPERACIONAL
             (DTRESUMO, CDEMPRESA, NMEMPRESA, CDREGIONAL, NMREGIONAL, CDSEGMERCADO, NMSEGMERCADO, CDSUPERVISOR, NMSUPERVISOR,
              CDFILIAL, NMFILIAL, CDPAIS, NMPAIS, SGESTADO, NMESTADO, CDMUNICIPIO, NMMUNICIPIO, IDTPLANCROP,
              NRINSJURCLIE, NMCLIENTE, CDSERVICO, NMSERVICO, CDGRUPSERV, NMGRUPSERV, QTSERVICO, VRFATURDIV, VRFATBRUTO,
              CDFILIALCENT, NMFILIALCENT, CDTIPOOPER, NMTIPOOPER, IDENTRSAIDOP, VRDEVOLUESTOR, CDRECEXTRA, NMRECEXTRA,
              CDFATUDIVER, NMFATUDIVER, CDIMPOSTO, NMIMPOSTO, VRDEDURECEITA, VRIMPOSTO,
              VRENCARGO, CDENCARGO, NMENCARGO, VRTAXAADM, VRESTOQINICIAL,
              VRIMPLANTAEST, VRENTRCOMPRAS, VROUTRASENTR, VRCONHECTRANS, VRAJUSTEEST, VRSAIDDEVOLU, VROUTRASSAID,
              VRRETIRAAJUST, VRESTOQFINAL, VRCMV, VRCONSUMO, VRDIFCMVCONS, CDGRUPPROD,
              NMGRUPPROD, CDSUBGRUPPROD, NMSUBGRUPPROD, CDDESPPREV, NMDESPPREV, VRDESPESATOT, VRDESPESA, CDTPCTPAGAR, NMTPCTPAGAR,
              CDDESPADMFAT, NMDESPADMFAT, VRMAOOBRA, NRINSJURFORN, NMFORNECED, CDARVPROD, NMPRODUTO, VRDEPRECIACAO,
              CDINVESTIMENTO, NMINVESTIMENTO, VRINVESTIMENTO, VRINVESTIMENTOPROJ, VRINVESTIMENTOORCA, VRRESULTFINAL,
              VRRESULTFINALPROJ, VRRESULTFINALORCA, NRORG)
           VALUES (C_INVESTI.DTINVESTIMENTO, V_CDEMPRESA, V_NMEMPRESA, V_CDAREAGERE, V_NMAREAGERE, V_CDSEGMER, V_NMSEGMER, V_CDSUPERVISOR, V_NMOPERADOR,
                   V_CDFILIAL, V_NMFILIAL, V_CDPAIS, V_NMPAIS, V_SGESTADO, V_NMESTADO, V_CDMUNICIPIO, V_NMMUNICIPIO, C_INVESTI.IDTPLANCROP,
                   V_NRINSJURCLIE, V_NMCLIENTE, NULL, NULL, NULL, NULL, 0, 0, 0,
                   V_CDFILIALCENT, V_NMFILIALCENT, NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL, NULL, NULL, NULL, NULL, NULL, NULL, V_CDTPCTPAGAR, V_DSTPCTPAGAR,
                   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                   V_CDINVESTIMENTO, V_NMINVESTIMENTO, C_INVESTI.VRINVEST,
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_INVESTI.VRINVEST,0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_INVESTI.VRINVEST,0)
                   END, 0, C_INVESTI.VRINVEST*(-1), 
                   CASE WHEN V_CALC_PROJ = 'S'
                        THEN (NVL(C_INVESTI.VRINVEST*(-1),0)/V_NRDIA_REALIZADO)*V_NRDIA_UTIL
                        ELSE NVL(C_INVESTI.VRINVEST*(-1),0)
                   END, 0, P_NRORG);
   END LOOP;
   
   COMMIT;
END;
/