/*

Criado em  : 07/2020
       Por : Jefferson Gouvea

Descricao:

       Funcao responsavel por retornar do demonstrativo de resultado forsale.

       P_CDFILIAL  Filial
       P_DATAINI   Data inicial
       P_DATAFIN   Data final

*/

DROP FUNCTION RESUMO_RFS
/

DROP TYPE RESUMO_RFS_TABLE
/

DROP TYPE RESUMO_RFS_TYPE
/

CREATE TYPE RESUMO_RFS_TYPE AS
OBJECT (NRRESUMO_RFS        NUMBER,
        DSFILIAL            VARCHAR2(40),
        NRTOPICO            VARCHAR2(03),
        DSDESCRICAOTOP      VARCHAR2(50),
        VRREAL              FLOAT(126),
        VRPERREAL           FLOAT(126),
        VRPROJECAO          FLOAT(126),
        VRPERPROJECAO       FLOAT(126),
        VRPROJMANUAL        FLOAT(126),
        VRPERPROJMANUAL     FLOAT(126),
        VRDIFREALPROJMANU   FLOAT(126),
        DSPRICINDICCOL1     VARCHAR2(30),
        VRPRINCINDICCOL1    FLOAT(126),
        DSPRICINDICCOL2     VARCHAR2(30),
        VRPRINCINDICCOL2    FLOAT(126),
        DSPRICINDICCOL3     VARCHAR2(30),
        VRPRINCINDICCOL3    FLOAT(126),
        DSPRICINDICCOL4     VARCHAR2(30),
        VRPRINCINDICCOL4    FLOAT(126),
        PRINCINDICADORES    VARCHAR2(01),
        NRORDEM             NUMBER,
        UTILPROJMANUAL      VARCHAR2(01),
        NRORG               NUMBER)
/

CREATE TYPE RESUMO_RFS_TABLE AS
TABLE OF RESUMO_RFS_TYPE
/

CREATE OR REPLACE FUNCTION RESUMO_RFS
 (P_CDFILIAL     IN VARCHAR2,
  P_DATAINI      IN DATE,
  P_DATAFIN      IN DATE) RETURN RESUMO_RFS_TABLE PIPELINED
IS
  V_DIAS_REALIZADOS      NUMBER;
  V_DIASCORRIDOS         NUMBER;
  V_DIASUTEISMES         NUMBER;
  V_DSFILIAL             VARCHAR2(100);
  V_AUX                  FLOAT;
  V_AUX_P                FLOAT;
  V_NRORG                NUMBER;
  V_IDPERFIXIMPDRF       VARCHAR2(01);
  V_IDEXIBEDESPESAMOBRA  VARCHAR2(30);
  V_NRMETROFILIAL        NUMBER;

  V_PROJ_VRFATBRUTODRF   FLOAT;
  V_PROJ_VRIMPOSTODRF    FLOAT;
  V_PROJ_VRFATLIQDRF     FLOAT;
  V_PROJ_VRCONSUMODRF    FLOAT;
  V_PROJ_VRCMVDRF        FLOAT;
  V_PROJ_VRRETEXTRADRF   FLOAT;
  V_PROJ_VRDESGERALDRF   FLOAT;
  V_PROJ_VRDESMAOOBRADRF FLOAT;
  V_PROJ_VRDESINVESTDRF  FLOAT;
  V_PROJ_VRRESULTADODRF  FLOAT;
  V_UTIPROJMANUAL        VARCHAR2(01);

  V_VRFATURAMENTO        FLOAT;
  V_VRFATURAMENTO_PROJ   FLOAT;
  V_VRTOTIMPOSTO         FLOAT;
  V_QTVENDIDA            FLOAT;
  V_QTPESSOAS            FLOAT;
  V_CUSTOVENDA           FLOAT;
  V_QTDECUPONS           FLOAT;

  V_VRIMPOSTO            FLOAT;
  V_LIQUIDO              FLOAT;
  V_LIQUIDO_PROJ         FLOAT;
  V_RETCONSOLID          FLOAT;
  V_RETNAOCONSOLID       FLOAT;
  V_DESPESA              FLOAT;

  V_RESULTADO            FLOAT;
  V_RESULTADO_PROJ       FLOAT;

  V_VRVENDREDUZ          FLOAT;
  V_VRSANGRIA            FLOAT;
  V_VRCONTRAVALE         FLOAT;
  V_SUPRIMENTOS          FLOAT;
  V_VRITEMCANCELADO      FLOAT;
  V_VRVENDACANC          FLOAT;
  V_QTVRDESITVEND        FLOAT;
  V_QTITEMCANC           FLOAT;
  V_QTVENDACANC          FLOAT;
  V_VRACRITVEND          FLOAT;
  V_VRDESITVEND          FLOAT;
  V_VRIMPDESTAC          FLOAT;
  V_DREALIZADOS          NUMBER;

  OUT_RECORD RESUMO_RFS_TYPE := RESUMO_RFS_TYPE(NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                                                NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                                                NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                                                NULL, NULL);
BEGIN
   V_RESULTADO := 0;
   V_VRIMPDESTAC := 0;

   SELECT MAX(NMFILIAL), MAX(NRORG)
     INTO V_DSFILIAL, V_NRORG
     FROM FILIAL
    WHERE CDFILIAL = P_CDFILIAL;

   SELECT COUNT(DISTINCT DTENTRVENDA), NVL(MAX(P_DATAFIN - P_DATAINI + 1),1)
     INTO V_DIAS_REALIZADOS, V_DIASCORRIDOS
     FROM VENDA
    WHERE CDFILIAL = P_CDFILIAL
      AND DTENTRVENDA BETWEEN P_DATAINI AND P_DATAFIN;

   V_DREALIZADOS := CASE WHEN V_DIAS_REALIZADOS = 0 THEN 1 ELSE V_DIAS_REALIZADOS END; 

   SELECT NVL(MAX(P.IDPERFIXIMPDRF),'N'), MAX(P.IDEXIBEDESPESAMOBRA), MAX(NRMETROFILIAL)
     INTO V_IDPERFIXIMPDRF, V_IDEXIBEDESPESAMOBRA, V_NRMETROFILIAL
     FROM PARFILIALROP P
    WHERE P.CDFILIAL = TIPOPARAM_DRO(P_CDFILIAL);

   SELECT MAX(P_DATAFIN - P_DATAINI + 1) - COUNT(F.CDFILIAL)
     INTO V_DIASUTEISMES
     FROM FERIFILI F, PARAMFILIAL
    WHERE PARAMFILIAL.CDFILIAL = P_CDFILIAL
      AND PARAMFILIAL.CDFILIAL = F.CDFILIAL(+)
      AND F.DTFERIFILI(+) BETWEEN P_DATAINI AND P_DATAFIN
      AND NOT EXISTS (SELECT L.NRLANCESTQ
                        FROM LANCTOESTOQ L
                       WHERE L.CDFILIMOVI   = P_CDFILIAL
                         AND L.DTLANCESTQ   = F.DTFERIFILI
                         AND L.IDTPLANCTO  IN ('0','1','2','E')
                         AND PARAMFILIAL.IDFDIAENTREST = 'S'
                       UNION
                      SELECT L.NRLANCESTQ
                        FROM LANCTOESTOQ L
                       WHERE L.CDFILIMOVI   = P_CDFILIAL
                         AND L.DTLANCESTQ   = F.DTFERIFILI
                         AND L.IDTPLANCTO  IN ('0','1','4','5')
                         AND PARAMFILIAL.IDFDIASAIDEST = 'S' );

   BEGIN
      SELECT NVL(VRFATBRUTODRF,0), NVL(VRIMPOSTODRF,0), NVL(VRFATLIQDRF,0), NVL(VRCONSUMODRF,0),
             NVL(VRCMVDRF,0), NVL(VRRETEXTRADRF,0), NVL(VRDESGERALDRF,0), NVL(VRDESMAOOBRADRF,0), NVL(VRDESINVESTDRF,0), NVL(VRRESULTADODRF,0),
             'S'
        INTO V_PROJ_VRFATBRUTODRF, V_PROJ_VRIMPOSTODRF, V_PROJ_VRFATLIQDRF, V_PROJ_VRCONSUMODRF,
             V_PROJ_VRCMVDRF, V_PROJ_VRRETEXTRADRF, V_PROJ_VRDESGERALDRF, V_PROJ_VRDESMAOOBRADRF, V_PROJ_VRDESINVESTDRF, V_PROJ_VRRESULTADODRF,
             V_UTIPROJMANUAL
        FROM PROJMANUALDRF
       WHERE CDFILIAL  = P_CDFILIAL
         AND DTINICIAL = FIRST_DAY(P_DATAINI)
         AND DTFINAL   = LAST_DAY(P_DATAINI);
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_PROJ_VRFATBRUTODRF := NULL; V_PROJ_VRIMPOSTODRF := NULL; V_PROJ_VRFATLIQDRF := NULL; V_PROJ_VRCONSUMODRF := NULL;
         V_PROJ_VRCMVDRF := NULL; V_PROJ_VRRETEXTRADRF := NULL; V_PROJ_VRDESGERALDRF := NULL;
         V_PROJ_VRDESMAOOBRADRF := NULL; V_PROJ_VRDESINVESTDRF := NULL; V_PROJ_VRRESULTADODRF := NULL;
         V_UTIPROJMANUAL := 'N';
      END;
   END;

   SELECT NVL(SUM(VRFATURAMENTO),0) VRFATURAMENTO,
          NVL(SUM(VRTOTIMPOSTO)*(-1),0) VRTOTIMPOSTO,
          NVL(SUM(QTFATURAMENTO),0) QTVENDIDA,
          NVL(SUM(QTPESSOAS),0) QTPESSOAS,
          NVL(SUM(CUSTOVENDA),0) CUSTOVENDA,
          NVL(SUM(QTDECUPONS),0) QTDECUPONS,
          NVL(SUM(VRACRITVEND),0) VRACRITVEND,
          NVL(SUM(VRDESITVEND),0) VRDESITVEND,
          COUNT(CASE WHEN NVL(VRDESITVEND,0) > 0 THEN VRDESITVEND ELSE NULL END) QTVRDESITVEND
     INTO V_VRFATURAMENTO,
          V_VRTOTIMPOSTO,
          V_QTVENDIDA,
          V_QTPESSOAS,
          V_CUSTOVENDA,
          V_QTDECUPONS,
          V_VRACRITVEND,
          V_VRDESITVEND,
          V_QTVRDESITVEND
     FROM FATURAMENTOROP
    WHERE IDTPLANCROP = 'FAT_FORSALE'
      AND CDFILIAL = P_CDFILIAL
      AND IDSTATUS = 'B'
      AND DTENTRSAID BETWEEN P_DATAINI AND P_DATAFIN;

   V_AUX_P := V_VRFATURAMENTO / V_DREALIZADOS * V_DIASCORRIDOS;
   V_VRFATURAMENTO_PROJ  := V_AUX_P;                   
                                                                   
   V_RESULTADO := NVL(V_VRFATURAMENTO,0);
   V_RESULTADO_PROJ := NVL(V_AUX_P,0);

   OUT_RECORD.NRRESUMO_RFS      := 0;
   OUT_RECORD.DSFILIAL          := V_DSFILIAL;
   OUT_RECORD.NRTOPICO          := '1.';
   OUT_RECORD.DSDESCRICAOTOP    := 'FATRFS';
   OUT_RECORD.VRREAL            := V_VRFATURAMENTO;
   OUT_RECORD.VRPERREAL         := CASE WHEN V_VRFATURAMENTO > 0
                                        THEN 1
                                        ELSE NULL
                                   END;
   OUT_RECORD.VRPROJECAO        := V_AUX_P;
   OUT_RECORD.VRPERPROJECAO     := CASE WHEN V_AUX_P > 0
                                        THEN CASE WHEN V_VRFATURAMENTO > 0
                                                  THEN 1
                                                  ELSE NULL
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJMANUAL      := V_PROJ_VRFATBRUTODRF;
   OUT_RECORD.VRPERPROJMANUAL   := CASE WHEN V_PROJ_VRFATBRUTODRF > 0
                                        THEN 1
                                        ELSE NULL
                                   END;
   OUT_RECORD.VRDIFREALPROJMANU := V_VRFATURAMENTO - V_PROJ_VRFATBRUTODRF;
   OUT_RECORD.NRORDEM           := 1;
   OUT_RECORD.NRORG             := V_NRORG;
   OUT_RECORD.UTILPROJMANUAL    := V_UTIPROJMANUAL;
   OUT_RECORD.VRPRINCINDICCOL1  := NULL;
   OUT_RECORD.DSPRICINDICCOL1   := NULL;
   OUT_RECORD.VRPRINCINDICCOL2  := NULL;
   OUT_RECORD.DSPRICINDICCOL2   := NULL;
   OUT_RECORD.VRPRINCINDICCOL3  := NULL;
   OUT_RECORD.DSPRICINDICCOL3   := NULL;
   OUT_RECORD.VRPRINCINDICCOL4  := NULL;
   OUT_RECORD.DSPRICINDICCOL4   := NULL;
   OUT_RECORD.PRINCINDICADORES  := 'N';
   PIPE ROW (OUT_RECORD);

   SELECT NVL(SUM(DECODE(R.IDTRATIMPROP, 'A', 1, DECODE(R.IDTRATIMPROP, 'D', -1, 0)) * D.VRDEDUCAO),0)
     INTO V_VRIMPOSTO
     FROM DEDUCAOROP D, IMPOSTOROP R
    WHERE D.CDFILIAL = P_CDFILIAL
      AND DTREFERENC BETWEEN P_DATAINI AND P_DATAFIN
      AND R.CDFILIAL = TIPOPARAM_DRO(P_CDFILIAL)
      AND R.CDIMPOSTO = D.CDIMPOSTO
      AND R.IDTPIMPOSTOROP = 'FN';

   V_AUX := CASE WHEN V_IDPERFIXIMPDRF = 'S' THEN  V_VRIMPOSTO ELSE V_VRTOTIMPOSTO END;
   V_VRIMPOSTO := V_AUX;

   V_AUX_P := V_AUX / V_DREALIZADOS * V_DIASCORRIDOS;                   
                                                                   
   V_RESULTADO := V_RESULTADO + NVL(V_AUX,0);
   V_RESULTADO_PROJ := V_RESULTADO_PROJ + NVL(V_AUX_P,0);

   OUT_RECORD.NRRESUMO_RFS      := 1;
   OUT_RECORD.DSFILIAL          := V_DSFILIAL;
   OUT_RECORD.NRTOPICO          := '*';
   OUT_RECORD.DSDESCRICAOTOP    := CASE WHEN  V_IDPERFIXIMPDRF = 'S' THEN 'IMPOSRFS' ELSE 'IMPORFS' END;
   OUT_RECORD.VRREAL            := V_AUX;
   OUT_RECORD.VRPERREAL         := CASE WHEN V_VRFATURAMENTO > 0
                                        THEN CASE WHEN ABS(V_AUX) > 0
                                                  THEN V_AUX / V_VRFATURAMENTO
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJECAO        := V_AUX_P;
   OUT_RECORD.VRPERPROJECAO     := CASE WHEN V_AUX_P <> 0
                                        THEN CASE WHEN ABS(V_AUX_P) > 0
                                                  THEN V_AUX_P / V_VRFATURAMENTO_PROJ
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJMANUAL      := V_PROJ_VRIMPOSTODRF;
   OUT_RECORD.VRPERPROJMANUAL   := CASE WHEN V_PROJ_VRFATBRUTODRF <> 0
                                        THEN CASE WHEN ABS(V_PROJ_VRIMPOSTODRF) > 0
                                                  THEN V_PROJ_VRIMPOSTODRF / V_PROJ_VRFATBRUTODRF
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRDIFREALPROJMANU := V_AUX - V_PROJ_VRIMPOSTODRF;
   OUT_RECORD.NRORDEM           := 2;
   OUT_RECORD.NRORG             := V_NRORG;
   OUT_RECORD.UTILPROJMANUAL    := V_UTIPROJMANUAL;
   OUT_RECORD.VRPRINCINDICCOL1  := NULL;
   OUT_RECORD.DSPRICINDICCOL1   := NULL;
   OUT_RECORD.VRPRINCINDICCOL2  := NULL;
   OUT_RECORD.DSPRICINDICCOL2   := NULL;
   OUT_RECORD.VRPRINCINDICCOL3  := NULL;
   OUT_RECORD.DSPRICINDICCOL3   := NULL;
   OUT_RECORD.VRPRINCINDICCOL4  := NULL;
   OUT_RECORD.DSPRICINDICCOL4   := NULL;
   OUT_RECORD.PRINCINDICADORES  := 'N';
   PIPE ROW (OUT_RECORD);

   V_AUX := V_VRFATURAMENTO + V_VRIMPOSTO;
   V_LIQUIDO := V_AUX;

   V_AUX_P := V_AUX / V_DREALIZADOS * V_DIASCORRIDOS;
   V_LIQUIDO_PROJ := V_AUX_P;

   OUT_RECORD.NRRESUMO_RFS      := 2;
   OUT_RECORD.DSFILIAL          := V_DSFILIAL;
   OUT_RECORD.NRTOPICO          := ' ';
   OUT_RECORD.DSDESCRICAOTOP    := 'FATLIQRFS';
   OUT_RECORD.VRREAL            := V_AUX;
   OUT_RECORD.VRPERREAL         := CASE WHEN V_VRFATURAMENTO > 0
                                        THEN CASE WHEN ABS(V_AUX) > 0
                                                  THEN V_AUX / V_VRFATURAMENTO
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJECAO        := V_AUX_P;
   OUT_RECORD.VRPERPROJECAO     := CASE WHEN V_AUX_P <> 0
                                        THEN CASE WHEN ABS(V_AUX_P) > 0
                                                  THEN V_AUX_P / V_VRFATURAMENTO_PROJ
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJMANUAL      := V_PROJ_VRFATLIQDRF;
   OUT_RECORD.VRPERPROJMANUAL   := CASE WHEN V_PROJ_VRFATBRUTODRF <> 0
                                        THEN CASE WHEN ABS(V_PROJ_VRFATLIQDRF) > 0
                                                  THEN V_PROJ_VRFATLIQDRF / V_PROJ_VRFATBRUTODRF
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRDIFREALPROJMANU := V_AUX - V_PROJ_VRFATLIQDRF;
   OUT_RECORD.NRORDEM           := 3;
   OUT_RECORD.NRORG             := V_NRORG;
   OUT_RECORD.UTILPROJMANUAL    := V_UTIPROJMANUAL;
   OUT_RECORD.VRPRINCINDICCOL1  := NULL;
   OUT_RECORD.DSPRICINDICCOL1   := NULL;
   OUT_RECORD.VRPRINCINDICCOL2  := NULL;
   OUT_RECORD.DSPRICINDICCOL2   := NULL;
   OUT_RECORD.VRPRINCINDICCOL3  := NULL;
   OUT_RECORD.DSPRICINDICCOL3   := NULL;
   OUT_RECORD.VRPRINCINDICCOL4  := NULL;
   OUT_RECORD.DSPRICINDICCOL4   := NULL;
   OUT_RECORD.PRINCINDICADORES  := 'N';
   PIPE ROW (OUT_RECORD);

   SELECT NVL(SUM(R.VRLANCTOESTQ),0) VRTOTRETIRADA
     INTO V_RETCONSOLID
     FROM RETIRADADRO R INNER JOIN FILIALROP F ON R.NRFILIALROP = F.NRFILIALROP
    WHERE F.CDFILIAL = P_CDFILIAL
      AND F.DTFILIALROP BETWEEN P_DATAINI AND P_DATAFIN
      AND R.IDTPLANCROP = 'RETCONSOLID';

   SELECT NVL(SUM(R.VRLANCTOESTQ),0) VRTOTRETIRADA
     INTO V_RETNAOCONSOLID
     FROM RETIRADADRO R INNER JOIN FILIALROP F ON R.NRFILIALROP = F.NRFILIALROP
    WHERE F.CDFILIAL = P_CDFILIAL
      AND F.DTFILIALROP BETWEEN P_DATAINI AND P_DATAFIN
      AND R.IDTPLANCROP = 'RETNAOCONSOLID';

   V_AUX := V_RETCONSOLID + V_RETNAOCONSOLID;

   V_AUX_P := V_AUX / V_DREALIZADOS * V_DIASCORRIDOS;                   
                                                                   
   V_RESULTADO := V_RESULTADO - NVL(V_AUX,0);
   V_RESULTADO_PROJ := V_RESULTADO_PROJ - NVL(V_AUX_P,0);

   OUT_RECORD.NRRESUMO_RFS      := 3;
   OUT_RECORD.DSFILIAL          := V_DSFILIAL;
   OUT_RECORD.NRTOPICO          := '2.';
   OUT_RECORD.DSDESCRICAOTOP    := 'CONSURFS';
   OUT_RECORD.VRREAL            := V_AUX;
   OUT_RECORD.VRPERREAL         := CASE WHEN V_VRFATURAMENTO > 0
                                        THEN CASE WHEN ABS(V_AUX) > 0
                                                  THEN V_AUX / V_LIQUIDO
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJECAO        := V_AUX_P;
   OUT_RECORD.VRPERPROJECAO     := CASE WHEN V_AUX_P <> 0
                                        THEN CASE WHEN ABS(V_AUX_P) > 0
                                                  THEN V_AUX_P / V_LIQUIDO_PROJ
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJMANUAL      := V_PROJ_VRCONSUMODRF;
   OUT_RECORD.VRPERPROJMANUAL   := CASE WHEN V_PROJ_VRFATLIQDRF <> 0
                                        THEN CASE WHEN ABS(V_PROJ_VRCONSUMODRF) > 0
                                                  THEN V_PROJ_VRCONSUMODRF / V_PROJ_VRFATLIQDRF
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRDIFREALPROJMANU := V_AUX - V_PROJ_VRCONSUMODRF;
   OUT_RECORD.NRORDEM           := 4;
   OUT_RECORD.NRORG             := V_NRORG;
   OUT_RECORD.UTILPROJMANUAL    := V_UTIPROJMANUAL;
   OUT_RECORD.VRPRINCINDICCOL1  := NULL;
   OUT_RECORD.DSPRICINDICCOL1   := NULL;
   OUT_RECORD.VRPRINCINDICCOL2  := NULL;
   OUT_RECORD.DSPRICINDICCOL2   := NULL;
   OUT_RECORD.VRPRINCINDICCOL3  := NULL;
   OUT_RECORD.DSPRICINDICCOL3   := NULL;
   OUT_RECORD.VRPRINCINDICCOL4  := NULL;
   OUT_RECORD.DSPRICINDICCOL4   := NULL;
   OUT_RECORD.PRINCINDICADORES  := 'N';
   PIPE ROW (OUT_RECORD);

   V_AUX := V_RETCONSOLID;

   V_AUX_P := V_AUX / V_DREALIZADOS * V_DIASCORRIDOS;

   OUT_RECORD.NRRESUMO_RFS      := 4;
   OUT_RECORD.DSFILIAL          := V_DSFILIAL;
   OUT_RECORD.NRTOPICO          := ' ';
   OUT_RECORD.DSDESCRICAOTOP    := 'CMVRFS';
   OUT_RECORD.VRREAL            := V_AUX;
   OUT_RECORD.VRPERREAL         := CASE WHEN V_VRFATURAMENTO > 0
                                        THEN CASE WHEN ABS(V_AUX) > 0
                                                  THEN V_AUX / V_LIQUIDO
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJECAO        := V_AUX_P;
   OUT_RECORD.VRPERPROJECAO     := CASE WHEN V_AUX_P <> 0
                                        THEN CASE WHEN ABS(V_AUX_P) > 0
                                                  THEN V_AUX_P / V_LIQUIDO_PROJ
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJMANUAL      := V_PROJ_VRCMVDRF;
   OUT_RECORD.VRPERPROJMANUAL   := CASE WHEN V_PROJ_VRFATLIQDRF <> 0
                                        THEN CASE WHEN ABS(V_PROJ_VRCMVDRF) > 0
                                                  THEN V_PROJ_VRCMVDRF / V_PROJ_VRFATLIQDRF
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRDIFREALPROJMANU := V_AUX - V_PROJ_VRCMVDRF;
   OUT_RECORD.NRORDEM           := 5;
   OUT_RECORD.NRORG             := V_NRORG;
   OUT_RECORD.UTILPROJMANUAL    := V_UTIPROJMANUAL;
   OUT_RECORD.VRPRINCINDICCOL1  := NULL;
   OUT_RECORD.DSPRICINDICCOL1   := NULL;
   OUT_RECORD.VRPRINCINDICCOL2  := NULL;
   OUT_RECORD.DSPRICINDICCOL2   := NULL;
   OUT_RECORD.VRPRINCINDICCOL3  := NULL;
   OUT_RECORD.DSPRICINDICCOL3   := NULL;
   OUT_RECORD.VRPRINCINDICCOL4  := NULL;
   OUT_RECORD.DSPRICINDICCOL4   := NULL;
   OUT_RECORD.PRINCINDICADORES  := 'N';
   PIPE ROW (OUT_RECORD);

   V_AUX := V_RETNAOCONSOLID;

   V_AUX_P := V_AUX / V_DREALIZADOS * V_DIASCORRIDOS;

   OUT_RECORD.NRRESUMO_RFS      := 5;
   OUT_RECORD.DSFILIAL          := V_DSFILIAL;
   OUT_RECORD.NRTOPICO          := ' ';
   OUT_RECORD.DSDESCRICAOTOP    := 'RETEXTRARFS';
   OUT_RECORD.VRREAL            := V_AUX;
   OUT_RECORD.VRPERREAL         := CASE WHEN V_VRFATURAMENTO > 0
                                        THEN CASE WHEN ABS(V_AUX) > 0
                                                  THEN V_AUX / V_LIQUIDO
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJECAO        := V_AUX_P;
   OUT_RECORD.VRPERPROJECAO     := CASE WHEN V_AUX_P <> 0
                                        THEN CASE WHEN ABS(V_AUX_P) > 0
                                                  THEN V_AUX_P / V_LIQUIDO_PROJ
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJMANUAL      := V_PROJ_VRRETEXTRADRF;
   OUT_RECORD.VRPERPROJMANUAL   := CASE WHEN V_PROJ_VRFATLIQDRF <> 0
                                        THEN CASE WHEN ABS(V_PROJ_VRRETEXTRADRF) > 0
                                                  THEN V_PROJ_VRRETEXTRADRF / V_PROJ_VRFATLIQDRF
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRDIFREALPROJMANU := V_AUX - V_PROJ_VRRETEXTRADRF;
   OUT_RECORD.NRORDEM           := 6;
   OUT_RECORD.NRORG             := V_NRORG;
   OUT_RECORD.UTILPROJMANUAL    := V_UTIPROJMANUAL;
   OUT_RECORD.VRPRINCINDICCOL1  := NULL;
   OUT_RECORD.DSPRICINDICCOL1   := NULL;
   OUT_RECORD.VRPRINCINDICCOL2  := NULL;
   OUT_RECORD.DSPRICINDICCOL2   := NULL;
   OUT_RECORD.VRPRINCINDICCOL3  := NULL;
   OUT_RECORD.DSPRICINDICCOL3   := NULL;
   OUT_RECORD.VRPRINCINDICCOL4  := NULL;
   OUT_RECORD.DSPRICINDICCOL4   := NULL;
   OUT_RECORD.PRINCINDICADORES  := 'N';
   PIPE ROW (OUT_RECORD);

   SELECT NVL(SUM(D.VRDESPESA),0)
     INTO V_DESPESA
     FROM DESPESASROP D INNER JOIN FILIALROP F ON D.NRFILIALROP = F.NRFILIALROP
    WHERE D.IDTPLANCROP = 'DESPESA'
      AND NVL(D.IDTIPODESP,'LANC_ENTRADA') IN ('LANC_ENTRADA','CONTA_PAGAR')
      AND F.CDFILIAL = P_CDFILIAL
      AND D.DTDESPESA = F.DTFILIALROP
      AND F.DTFILIALROP BETWEEN P_DATAINI AND P_DATAFIN;

   V_AUX := V_DESPESA;

   V_AUX_P := V_AUX / V_DREALIZADOS * V_DIASCORRIDOS;

   V_RESULTADO := V_RESULTADO - NVL(V_AUX,0);
   V_RESULTADO_PROJ := V_RESULTADO_PROJ - NVL(V_AUX_P,0);

   OUT_RECORD.NRRESUMO_RFS      := 6;
   OUT_RECORD.DSFILIAL          := V_DSFILIAL;
   OUT_RECORD.NRTOPICO          := '3.';
   OUT_RECORD.DSDESCRICAOTOP    := 'DESPESARFS';
   OUT_RECORD.VRREAL            := V_AUX;
   OUT_RECORD.VRPERREAL         := CASE WHEN V_VRFATURAMENTO > 0
                                        THEN CASE WHEN ABS(V_AUX) > 0
                                                  THEN V_AUX / V_LIQUIDO
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJECAO        := V_AUX_P;
   OUT_RECORD.VRPERPROJECAO     := CASE WHEN V_AUX_P <> 0
                                        THEN CASE WHEN ABS(V_AUX_P) > 0
                                                  THEN V_AUX_P / V_LIQUIDO_PROJ
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJMANUAL      := V_PROJ_VRDESGERALDRF;
   OUT_RECORD.VRPERPROJMANUAL   := CASE WHEN V_PROJ_VRFATLIQDRF <> 0
                                        THEN CASE WHEN ABS(V_PROJ_VRDESGERALDRF) > 0
                                                  THEN V_PROJ_VRDESGERALDRF / V_PROJ_VRFATLIQDRF
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRDIFREALPROJMANU := V_AUX - V_PROJ_VRDESGERALDRF;
   OUT_RECORD.NRORDEM           := 7;
   OUT_RECORD.NRORG             := V_NRORG;
   OUT_RECORD.UTILPROJMANUAL    := V_UTIPROJMANUAL;
   OUT_RECORD.VRPRINCINDICCOL1  := NULL;
   OUT_RECORD.DSPRICINDICCOL1   := NULL;
   OUT_RECORD.VRPRINCINDICCOL2  := NULL;
   OUT_RECORD.DSPRICINDICCOL2   := NULL;
   OUT_RECORD.VRPRINCINDICCOL3  := NULL;
   OUT_RECORD.DSPRICINDICCOL3   := NULL;
   OUT_RECORD.VRPRINCINDICCOL4  := NULL;
   OUT_RECORD.DSPRICINDICCOL4   := NULL;
   OUT_RECORD.PRINCINDICADORES  := 'N';
   PIPE ROW (OUT_RECORD);

   SELECT CASE WHEN V_IDEXIBEDESPESAMOBRA = 'TOTAL_PERIODO'
               THEN NVL(SUM(D.VRDESPESA),0)
               ELSE (NVL(SUM(D.VRDESPESA),0) / V_DIASCORRIDOS) * V_DIAS_REALIZADOS
          END
     INTO V_AUX
     FROM DESPESASROP D INNER JOIN FILIALROP F ON D.NRFILIALROP = F.NRFILIALROP
    WHERE D.IDTPLANCROP = 'MAOOBRA'
      AND F.CDFILIAL = P_CDFILIAL
      AND D.DTDESPESA = F.DTFILIALROP
      AND F.DTFILIALROP BETWEEN P_DATAINI AND P_DATAFIN;

   V_AUX_P := V_AUX / V_DREALIZADOS * V_DIASCORRIDOS;

   V_RESULTADO := V_RESULTADO + NVL(V_AUX,0);
   V_RESULTADO_PROJ := V_RESULTADO_PROJ + NVL(V_AUX_P,0);

   OUT_RECORD.NRRESUMO_RFS      := 7;
   OUT_RECORD.DSFILIAL          := V_DSFILIAL;
   OUT_RECORD.NRTOPICO          := '4.';
   OUT_RECORD.DSDESCRICAOTOP    := 'DESPESAMORFS';
   OUT_RECORD.VRREAL            := ABS(V_AUX);
   OUT_RECORD.VRPERREAL         := CASE WHEN V_VRFATURAMENTO > 0
                                        THEN CASE WHEN ABS(V_AUX) > 0
                                                  THEN ABS(V_AUX) / V_LIQUIDO
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJECAO        := ABS(V_AUX_P);
   OUT_RECORD.VRPERPROJECAO     := CASE WHEN V_AUX_P <> 0
                                        THEN CASE WHEN ABS(V_AUX_P) > 0
                                                  THEN ABS(V_AUX_P) / V_LIQUIDO_PROJ
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJMANUAL      := ABS(V_PROJ_VRDESMAOOBRADRF);
   OUT_RECORD.VRPERPROJMANUAL   := CASE WHEN V_PROJ_VRFATLIQDRF <> 0
                                        THEN CASE WHEN ABS(V_PROJ_VRDESMAOOBRADRF) > 0
                                                  THEN ABS(V_PROJ_VRDESMAOOBRADRF) / V_PROJ_VRFATLIQDRF
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRDIFREALPROJMANU := V_AUX - V_PROJ_VRDESMAOOBRADRF;
   OUT_RECORD.NRORDEM           := 8;
   OUT_RECORD.NRORG             := V_NRORG;
   OUT_RECORD.UTILPROJMANUAL    := V_UTIPROJMANUAL;
   OUT_RECORD.VRPRINCINDICCOL1  := NULL;
   OUT_RECORD.DSPRICINDICCOL1   := NULL;
   OUT_RECORD.VRPRINCINDICCOL2  := NULL;
   OUT_RECORD.DSPRICINDICCOL2   := NULL;
   OUT_RECORD.VRPRINCINDICCOL3  := NULL;
   OUT_RECORD.DSPRICINDICCOL3   := NULL;
   OUT_RECORD.VRPRINCINDICCOL4  := NULL;
   OUT_RECORD.DSPRICINDICCOL4   := NULL;
   OUT_RECORD.PRINCINDICADORES  := 'N';
   PIPE ROW (OUT_RECORD);

   SELECT NVL(SUM(I.VRINVEST),0)
     INTO V_AUX
     FROM INVESTIMENTOROP I INNER JOIN FILIALROP F ON I.NRFILIALROP = F.NRFILIALROP
    WHERE I.IDTPLANCROP = 'INVESTIMENTO'
      AND F.CDFILIAL = P_CDFILIAL
      AND F.DTFILIALROP = I.DTINVESTIMENTO
      AND F.DTFILIALROP BETWEEN P_DATAINI AND P_DATAFIN;

   V_AUX_P := V_AUX / V_DREALIZADOS * V_DIASCORRIDOS;

   V_RESULTADO := V_RESULTADO - NVL(V_AUX,0);
   V_RESULTADO_PROJ := V_RESULTADO_PROJ - NVL(V_AUX_P,0);

   OUT_RECORD.NRRESUMO_RFS      := 8;
   OUT_RECORD.DSFILIAL          := V_DSFILIAL;
   OUT_RECORD.NRTOPICO          := '5.';
   OUT_RECORD.DSDESCRICAOTOP    := 'INVESTRFS';
   OUT_RECORD.VRREAL            := V_AUX;
   OUT_RECORD.VRPERREAL         := CASE WHEN V_VRFATURAMENTO > 0
                                        THEN CASE WHEN ABS(V_AUX) > 0
                                                  THEN V_AUX / V_LIQUIDO
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJECAO        := V_AUX_P;
   OUT_RECORD.VRPERPROJECAO     := CASE WHEN V_AUX_P <> 0
                                        THEN CASE WHEN ABS(V_AUX_P) > 0
                                                  THEN V_AUX_P / V_LIQUIDO_PROJ
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJMANUAL      := V_PROJ_VRDESINVESTDRF;
   OUT_RECORD.VRPERPROJMANUAL   := CASE WHEN V_PROJ_VRFATLIQDRF <> 0
                                        THEN CASE WHEN ABS(V_PROJ_VRDESINVESTDRF) > 0
                                                  THEN V_PROJ_VRDESINVESTDRF / V_PROJ_VRFATLIQDRF
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRDIFREALPROJMANU := V_AUX - V_PROJ_VRDESINVESTDRF;
   OUT_RECORD.NRORDEM           := 9;
   OUT_RECORD.NRORG             := V_NRORG;
   OUT_RECORD.UTILPROJMANUAL    := V_UTIPROJMANUAL;
   OUT_RECORD.VRPRINCINDICCOL1  := NULL;
   OUT_RECORD.DSPRICINDICCOL1   := NULL;
   OUT_RECORD.VRPRINCINDICCOL2  := NULL;
   OUT_RECORD.DSPRICINDICCOL2   := NULL;
   OUT_RECORD.VRPRINCINDICCOL3  := NULL;
   OUT_RECORD.DSPRICINDICCOL3   := NULL;
   OUT_RECORD.VRPRINCINDICCOL4  := NULL;
   OUT_RECORD.DSPRICINDICCOL4   := NULL;
   OUT_RECORD.PRINCINDICADORES  := 'N';
   PIPE ROW (OUT_RECORD);

   V_AUX := V_RESULTADO;
   V_AUX_P := V_RESULTADO_PROJ;

   OUT_RECORD.NRRESUMO_RFS      := 9;
   OUT_RECORD.DSFILIAL          := V_DSFILIAL;
   OUT_RECORD.NRTOPICO          := ' ';
   OUT_RECORD.DSDESCRICAOTOP    := 'RESULTADORFS';
   OUT_RECORD.VRREAL            := V_AUX;
   OUT_RECORD.VRPERREAL         := CASE WHEN V_VRFATURAMENTO > 0
                                        THEN CASE WHEN ABS(V_AUX) > 0
                                                  THEN V_AUX / V_LIQUIDO
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJECAO        := V_AUX_P;
   OUT_RECORD.VRPERPROJECAO     := CASE WHEN V_AUX_P <> 0
                                        THEN CASE WHEN ABS(V_AUX_P) > 0
                                                  THEN V_AUX_P / V_LIQUIDO_PROJ
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRPROJMANUAL      := V_PROJ_VRRESULTADODRF;
   OUT_RECORD.VRPERPROJMANUAL   := CASE WHEN V_PROJ_VRFATLIQDRF <> 0
                                        THEN CASE WHEN ABS(V_PROJ_VRRESULTADODRF) > 0
                                                  THEN V_PROJ_VRRESULTADODRF / V_PROJ_VRFATLIQDRF
                                                  ELSE 0
                                             END
                                        ELSE 0
                                   END;
   OUT_RECORD.VRDIFREALPROJMANU := V_AUX - V_PROJ_VRRESULTADODRF;
   OUT_RECORD.NRORDEM           := 10;
   OUT_RECORD.NRORG             := V_NRORG;
   OUT_RECORD.UTILPROJMANUAL    := V_UTIPROJMANUAL;
   OUT_RECORD.VRPRINCINDICCOL1  := NULL;
   OUT_RECORD.DSPRICINDICCOL1   := NULL;
   OUT_RECORD.VRPRINCINDICCOL2  := NULL;
   OUT_RECORD.DSPRICINDICCOL2   := NULL;
   OUT_RECORD.VRPRINCINDICCOL3  := NULL;
   OUT_RECORD.DSPRICINDICCOL3   := NULL;
   OUT_RECORD.VRPRINCINDICCOL4  := NULL;
   OUT_RECORD.DSPRICINDICCOL4   := NULL;
   OUT_RECORD.PRINCINDICADORES  := 'N';
   PIPE ROW (OUT_RECORD);

   /* Indicadores */

   OUT_RECORD.NRRESUMO_RFS      := 10;
   OUT_RECORD.DSFILIAL          := V_DSFILIAL;
   OUT_RECORD.NRTOPICO          := ' ';
   OUT_RECORD.DSDESCRICAOTOP    := 'RESULTADORFS';
   OUT_RECORD.VRREAL            := NULL;
   OUT_RECORD.VRPERREAL         := NULL;
   OUT_RECORD.VRPROJECAO        := NULL;
   OUT_RECORD.VRPERPROJECAO     := NULL;
   OUT_RECORD.VRPROJMANUAL      := NULL;
   OUT_RECORD.VRPERPROJMANUAL   := NULL;
   OUT_RECORD.VRDIFREALPROJMANU := NULL;
   OUT_RECORD.NRORDEM           := 11;
   OUT_RECORD.NRORG             := V_NRORG;
   OUT_RECORD.UTILPROJMANUAL    := V_UTIPROJMANUAL;
   OUT_RECORD.VRPRINCINDICCOL1  := V_VRFATURAMENTO;
   OUT_RECORD.DSPRICINDICCOL1   := 'INDFATBRUT';
   OUT_RECORD.VRPRINCINDICCOL2  := V_VRACRITVEND;
   OUT_RECORD.DSPRICINDICCOL2   := 'INDTXSERAC';
   OUT_RECORD.VRPRINCINDICCOL3  := V_QTVENDIDA/CASE WHEN NVL(V_QTDECUPONS,0) = 0 THEN 1 ELSE V_QTDECUPONS END;
   OUT_RECORD.DSPRICINDICCOL3   := 'INDTICMEDQ';
   OUT_RECORD.VRPRINCINDICCOL4  := V_QTPESSOAS;
   OUT_RECORD.DSPRICINDICCOL4   := 'INDQTDECON';
   OUT_RECORD.PRINCINDICADORES  := 'S';
   PIPE ROW (OUT_RECORD);

   SELECT NVL(SUM(RZ.VRVENBRUREDZ-RZ.VRTOTCANREDZ- RZ.VRTOTDESREDZ),0) AS VRVENDAREDZ
     INTO V_VRVENDREDUZ
     FROM REDUCAOZ RZ
    WHERE RZ.CDFILIAL = P_CDFILIAL
      AND RZ.DTREDUCAOZ BETWEEN P_DATAINI AND P_DATAFIN;

   OUT_RECORD.NRRESUMO_RFS      := 11;
   OUT_RECORD.DSFILIAL          := V_DSFILIAL;
   OUT_RECORD.NRTOPICO          := ' ';
   OUT_RECORD.DSDESCRICAOTOP    := 'RESULTADORFS';
   OUT_RECORD.VRREAL            := NULL;
   OUT_RECORD.VRPERREAL         := NULL;
   OUT_RECORD.VRPROJECAO        := NULL;
   OUT_RECORD.VRPERPROJECAO     := NULL;
   OUT_RECORD.VRPROJMANUAL      := NULL;
   OUT_RECORD.VRPERPROJMANUAL   := NULL;
   OUT_RECORD.VRDIFREALPROJMANU := NULL;
   OUT_RECORD.NRORDEM           := 12;
   OUT_RECORD.NRORG             := V_NRORG;
   OUT_RECORD.UTILPROJMANUAL    := V_UTIPROJMANUAL;
   OUT_RECORD.VRPRINCINDICCOL1  := V_VRVENDREDUZ;
   OUT_RECORD.DSPRICINDICCOL1   := 'INDFATBRRZ';
   OUT_RECORD.VRPRINCINDICCOL2  := 0;
   OUT_RECORD.DSPRICINDICCOL2   := 'INDCOUVART';
   OUT_RECORD.VRPRINCINDICCOL3  := V_VRFATURAMENTO/CASE WHEN NVL(V_QTVENDIDA,0) = 0 THEN 1 ELSE V_QTVENDIDA END;
   OUT_RECORD.DSPRICINDICCOL3   := 'INDPRECMED';
   OUT_RECORD.VRPRINCINDICCOL4  := V_VRFATURAMENTO/CASE WHEN V_QTPESSOAS = 0 THEN 1 ELSE V_QTPESSOAS END;
   OUT_RECORD.DSPRICINDICCOL4   := 'INDFBPCONS';
   OUT_RECORD.PRINCINDICADORES  := 'S';
   PIPE ROW (OUT_RECORD);

   SELECT NVL(SUM(VRFATURAMENTO),0) VRFATURAMENTO
     INTO V_VRSANGRIA
     FROM FATURAMENTOROP
    WHERE IDTPLANCROP = 'FAT_SANGRIA'
      AND CDFILIAL = P_CDFILIAL
      AND DTENTRSAID BETWEEN P_DATAINI AND P_DATAFIN;

   SELECT NVL(SUM(VRFATURAMENTO),0) VRFATURAMENTO
     INTO V_VRCONTRAVALE
     FROM FATURAMENTOROP
    WHERE IDTPLANCROP = 'FAT_CONTRAVALE'
      AND CDFILIAL = P_CDFILIAL
      AND DTENTRSAID BETWEEN P_DATAINI AND P_DATAFIN;

   SELECT NVL(SUM(VRFATURAMENTO),0) VRFATURAMENTO
     INTO V_SUPRIMENTOS
     FROM FATURAMENTOROP
    WHERE IDTPLANCROP = 'FAT_SUPRIMENTOS'
      AND CDFILIAL = P_CDFILIAL
      AND DTENTRSAID BETWEEN P_DATAINI AND P_DATAFIN;

   OUT_RECORD.NRRESUMO_RFS      := 12;
   OUT_RECORD.DSFILIAL          := V_DSFILIAL;
   OUT_RECORD.NRTOPICO          := ' ';
   OUT_RECORD.DSDESCRICAOTOP    := 'RESULTADORFS';
   OUT_RECORD.VRREAL            := NULL;
   OUT_RECORD.VRPERREAL         := NULL;
   OUT_RECORD.VRPROJECAO        := NULL;
   OUT_RECORD.VRPERPROJECAO     := NULL;
   OUT_RECORD.VRPROJMANUAL      := NULL;
   OUT_RECORD.VRPERPROJMANUAL   := NULL;
   OUT_RECORD.VRDIFREALPROJMANU := NULL;
   OUT_RECORD.NRORDEM           := 13;
   OUT_RECORD.NRORG             := V_NRORG;
   OUT_RECORD.UTILPROJMANUAL    := V_UTIPROJMANUAL;
   OUT_RECORD.VRPRINCINDICCOL1  := V_VRFATURAMENTO-V_VRVENDREDUZ;
   OUT_RECORD.DSPRICINDICCOL1   := 'INDDIFFBRZ';
   OUT_RECORD.VRPRINCINDICCOL2  := V_VRDESITVEND;
   OUT_RECORD.DSPRICINDICCOL2   := 'INDDESCONT';
   OUT_RECORD.VRPRINCINDICCOL3  := CASE WHEN V_VRFATURAMENTO > (V_VRSANGRIA - V_VRCONTRAVALE - V_SUPRIMENTOS)
                                        THEN V_VRFATURAMENTO - (V_VRSANGRIA - V_VRCONTRAVALE - V_SUPRIMENTOS)
                                        ELSE 0
                                   END;
   OUT_RECORD.DSPRICINDICCOL3   := 'INDQUEBRA';
   OUT_RECORD.VRPRINCINDICCOL4  := V_VRFATURAMENTO/CASE WHEN NVL(V_NRMETROFILIAL,0) = 0 THEN 1 ELSE V_NRMETROFILIAL END;
   OUT_RECORD.DSPRICINDICCOL4   := 'INDFATBM2';
   OUT_RECORD.PRINCINDICADORES  := 'S';
   PIPE ROW (OUT_RECORD);

   OUT_RECORD.NRRESUMO_RFS      := 13;
   OUT_RECORD.DSFILIAL          := V_DSFILIAL;
   OUT_RECORD.NRTOPICO          := ' ';
   OUT_RECORD.DSDESCRICAOTOP    := 'RESULTADORFS';
   OUT_RECORD.VRREAL            := NULL;
   OUT_RECORD.VRPERREAL         := NULL;
   OUT_RECORD.VRPROJECAO        := NULL;
   OUT_RECORD.VRPERPROJECAO     := NULL;
   OUT_RECORD.VRPROJMANUAL      := NULL;
   OUT_RECORD.VRPERPROJMANUAL   := NULL;
   OUT_RECORD.VRDIFREALPROJMANU := NULL;
   OUT_RECORD.NRORDEM           := 14;
   OUT_RECORD.NRORG             := V_NRORG;
   OUT_RECORD.UTILPROJMANUAL    := V_UTIPROJMANUAL;
   OUT_RECORD.VRPRINCINDICCOL1  := V_VRFATURAMENTO/V_DREALIZADOS;
   OUT_RECORD.DSPRICINDICCOL1   := 'INDFTBDIAR';
   OUT_RECORD.VRPRINCINDICCOL2  := 0;
   OUT_RECORD.DSPRICINDICCOL2   := 'INDCONSMIN';
   OUT_RECORD.VRPRINCINDICCOL3  := CASE WHEN (V_VRSANGRIA - V_VRCONTRAVALE - V_SUPRIMENTOS) > V_VRFATURAMENTO
                                        THEN (V_VRSANGRIA - V_VRCONTRAVALE - V_SUPRIMENTOS) - V_VRFATURAMENTO
                                        ELSE 0
                                   END;
   OUT_RECORD.DSPRICINDICCOL3   := 'INDSOBRA';
   OUT_RECORD.VRPRINCINDICCOL4  := V_DIASUTEISMES;
   OUT_RECORD.DSPRICINDICCOL4   := 'INDDIAUTM';
   OUT_RECORD.PRINCINDICADORES  := 'S';
   PIPE ROW (OUT_RECORD);

   SELECT NVL(SUM(VRFATURAMENTO),0) VRFATURAMENTO,
          NVL(SUM(QTFATURAMENTO),0) QTVENDIDA
     INTO V_VRITEMCANCELADO, V_QTITEMCANC
     FROM FATURAMENTOROP
    WHERE IDTPLANCROP = 'FAT_FORSALE'
      AND CDFILIAL = P_CDFILIAL
      AND IDSTATUS = 'I'
      AND DTENTRSAID BETWEEN P_DATAINI AND P_DATAFIN;

   SELECT NVL(SUM(VRFATURAMENTO),0) VRFATURAMENTO,
          NVL(SUM(QTDECUPONS),0) QTDECUPONS
     INTO V_VRVENDACANC, V_QTVENDACANC
     FROM FATURAMENTOROP
    WHERE IDTPLANCROP = 'FAT_FORSALE'
      AND CDFILIAL = P_CDFILIAL
      AND IDSTATUS = 'V'
      AND DTENTRSAID BETWEEN P_DATAINI AND P_DATAFIN;

   OUT_RECORD.NRRESUMO_RFS      := 14;
   OUT_RECORD.DSFILIAL          := V_DSFILIAL;
   OUT_RECORD.NRTOPICO          := ' ';
   OUT_RECORD.DSDESCRICAOTOP    := 'RESULTADORFS';
   OUT_RECORD.VRREAL            := NULL;
   OUT_RECORD.VRPERREAL         := NULL;
   OUT_RECORD.VRPROJECAO        := NULL;
   OUT_RECORD.VRPERPROJECAO     := NULL;
   OUT_RECORD.VRPROJMANUAL      := NULL;
   OUT_RECORD.VRPERPROJMANUAL   := NULL;
   OUT_RECORD.VRDIFREALPROJMANU := NULL;
   OUT_RECORD.NRORDEM           := 15;
   OUT_RECORD.NRORG             := V_NRORG;
   OUT_RECORD.UTILPROJMANUAL    := V_UTIPROJMANUAL;
   IF (V_IDPERFIXIMPDRF = 'S') AND (V_VRIMPDESTAC > 0) THEN
      OUT_RECORD.VRPRINCINDICCOL1  := 0;
      OUT_RECORD.DSPRICINDICCOL1   := 'INDIMPDEST';
   ELSE
      OUT_RECORD.VRPRINCINDICCOL1  := NULL;
      OUT_RECORD.DSPRICINDICCOL1   := NULL;
   END IF;
   OUT_RECORD.VRPRINCINDICCOL2  := V_QTDECUPONS;
   OUT_RECORD.DSPRICINDICCOL2   := 'INDQTDECUP';
   OUT_RECORD.VRPRINCINDICCOL3  := V_VRITEMCANCELADO+V_VRVENDACANC;
   OUT_RECORD.DSPRICINDICCOL3   := 'INDCANCELA';
   OUT_RECORD.VRPRINCINDICCOL4  := (P_DATAFIN - P_DATAINI) + 1;
   OUT_RECORD.DSPRICINDICCOL4   := 'INDDIACORR';
   OUT_RECORD.PRINCINDICADORES  := 'S';
   PIPE ROW (OUT_RECORD);

   OUT_RECORD.NRRESUMO_RFS      := 15;
   OUT_RECORD.DSFILIAL          := V_DSFILIAL;
   OUT_RECORD.NRTOPICO          := ' ';
   OUT_RECORD.DSDESCRICAOTOP    := 'RESULTADORFS';
   OUT_RECORD.VRREAL            := NULL;
   OUT_RECORD.VRPERREAL         := NULL;
   OUT_RECORD.VRPROJECAO        := NULL;
   OUT_RECORD.VRPERPROJECAO     := NULL;
   OUT_RECORD.VRPROJMANUAL      := NULL;
   OUT_RECORD.VRPERPROJMANUAL   := NULL;
   OUT_RECORD.VRDIFREALPROJMANU := NULL;
   OUT_RECORD.NRORDEM           := 16;
   OUT_RECORD.NRORG             := V_NRORG;
   OUT_RECORD.UTILPROJMANUAL    := V_UTIPROJMANUAL;
   OUT_RECORD.VRPRINCINDICCOL1  := NULL;
   OUT_RECORD.DSPRICINDICCOL1   := NULL;
   OUT_RECORD.VRPRINCINDICCOL2  := V_QTVRDESITVEND;
   OUT_RECORD.DSPRICINDICCOL2   := 'INDQTCUPDES';
   OUT_RECORD.VRPRINCINDICCOL3  := V_QTITEMCANC;
   OUT_RECORD.DSPRICINDICCOL3   := 'INDQTPRODCAN';
   OUT_RECORD.VRPRINCINDICCOL4  := V_DIAS_REALIZADOS;
   OUT_RECORD.DSPRICINDICCOL4   := 'INDDIAREALI';
   OUT_RECORD.PRINCINDICADORES  := 'S';
   PIPE ROW (OUT_RECORD);   

   OUT_RECORD.NRRESUMO_RFS      := 16;
   OUT_RECORD.DSFILIAL          := V_DSFILIAL;
   OUT_RECORD.NRTOPICO          := ' ';
   OUT_RECORD.DSDESCRICAOTOP    := 'RESULTADORFS';
   OUT_RECORD.VRREAL            := NULL;
   OUT_RECORD.VRPERREAL         := NULL;
   OUT_RECORD.VRPROJECAO        := NULL;
   OUT_RECORD.VRPERPROJECAO     := NULL;
   OUT_RECORD.VRPROJMANUAL      := NULL;
   OUT_RECORD.VRPERPROJMANUAL   := NULL;
   OUT_RECORD.VRDIFREALPROJMANU := NULL;
   OUT_RECORD.NRORDEM           := 17;
   OUT_RECORD.NRORG             := V_NRORG;
   OUT_RECORD.UTILPROJMANUAL    := V_UTIPROJMANUAL;
   OUT_RECORD.VRPRINCINDICCOL1  := NULL;
   OUT_RECORD.DSPRICINDICCOL1   := NULL;
   OUT_RECORD.VRPRINCINDICCOL2  := V_QTVENDIDA;
   OUT_RECORD.DSPRICINDICCOL2   := 'INDQTDEPROD';
   OUT_RECORD.VRPRINCINDICCOL3  := V_QTVENDACANC;
   OUT_RECORD.DSPRICINDICCOL3   := 'INDQTCUPCAN';
   OUT_RECORD.VRPRINCINDICCOL4  := NULL;
   OUT_RECORD.DSPRICINDICCOL4   := NULL;
   OUT_RECORD.PRINCINDICADORES  := 'S';
   PIPE ROW (OUT_RECORD);                                  

   OUT_RECORD.NRRESUMO_RFS      := 17;
   OUT_RECORD.DSFILIAL          := V_DSFILIAL;
   OUT_RECORD.NRTOPICO          := ' ';
   OUT_RECORD.DSDESCRICAOTOP    := 'RESULTADORFS';
   OUT_RECORD.VRREAL            := NULL;
   OUT_RECORD.VRPERREAL         := NULL;
   OUT_RECORD.VRPROJECAO        := NULL;
   OUT_RECORD.VRPERPROJECAO     := NULL;
   OUT_RECORD.VRPROJMANUAL      := NULL;
   OUT_RECORD.VRPERPROJMANUAL   := NULL;
   OUT_RECORD.VRDIFREALPROJMANU := NULL;
   OUT_RECORD.NRORDEM           := 18;
   OUT_RECORD.NRORG             := V_NRORG;
   OUT_RECORD.UTILPROJMANUAL    := V_UTIPROJMANUAL;
   OUT_RECORD.VRPRINCINDICCOL1  := NULL;
   OUT_RECORD.DSPRICINDICCOL1   := NULL;
   OUT_RECORD.VRPRINCINDICCOL2  := V_VRFATURAMENTO/CASE WHEN NVL(V_QTDECUPONS,0) = 0 THEN 1 ELSE V_QTDECUPONS END;
   OUT_RECORD.DSPRICINDICCOL2   := 'INDVRTICMED';
   OUT_RECORD.VRPRINCINDICCOL3  := NULL;
   OUT_RECORD.DSPRICINDICCOL3   := NULL;
   OUT_RECORD.VRPRINCINDICCOL4  := NULL;
   OUT_RECORD.DSPRICINDICCOL4   := NULL;
   OUT_RECORD.PRINCINDICADORES  := 'S';
   PIPE ROW (OUT_RECORD);
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('RESUMO_RFS','6.35.xxx'); END;
/
