create or replace
PROCEDURE CONSUMO_PLANEJADO_PLA
  (P_FILIAL       IN VARCHAR2,
   P_SERVICOS     IN CLOB,
   P_DTINICIAL    IN DATE,
   P_DTFINAL      IN DATE,
   P_IDARQUIVO    IN VARCHAR2,
   P_IDPRODUTOS   IN VARCHAR2,
   P_IDSEPPRATO   IN VARCHAR2,
   P_PEACRESCIMO  IN NUMBER,
   P_IDARREDOND   IN VARCHAR2,
   P_NRCONSPLAN   IN VARCHAR2,
   P_CDPRODUTOINI IN VARCHAR2,
   P_CDPRODUTOFIN IN VARCHAR2,
   P_CDOPERADOR   IN VARCHAR2,
   P_STDTINICIAL  IN VARCHAR2 DEFAULT NULL,
   P_STDTFINAL    IN VARCHAR2 DEFAULT NULL) IS
   V_DTINICIAL    DATE;
   V_DTFINAL      DATE;
   V_COUNTSERVICO NUMBER;
   V_ARRED        NUMBER;
   V_IDCARDAPIO   VARCHAR2(1);
   V_IDCONSFIXO   VARCHAR2(1);
   V_IDCONSPERC   VARCHAR2(1);
   V_DTLIMITE     DATE;
   V_HRLIMITE     VARCHAR2(4);
   V_CDARVPROD    VARCHAR2(20);
   V_TAM_MASCARA  NUMBER;
   V_CDFAPRATCPLA CONSPLAN.CDFAPRATCPLA%TYPE;
   V_CDPRATCONSPL CONSPLAN.CDPRATCONSPL%TYPE;
   V_CDSETOCONSPL CONSPLAN.CDSETOCONSPL%TYPE;
   V_CDTPPRATCPLA CONSPLAN.CDTPPRATCPLA%TYPE;
   V_SGUNIDAD     CONSPLAN.SGCONSUNIADIC%TYPE;
   V_IDUTILFATCOR PARAMPLANEJA.IDUTILFATCOR%TYPE;
   V_IDUTILFATCOC PARAMPLANEJA.IDUTILFATCOC%TYPE;
   V_IDTIPCALCFAT PARAMPLANEJA.IDTIPCALCFAT%TYPE;
   V_IDUTRETIPRAT SERVICO.IDUTRETIPRAT%TYPE;
   V_CDSETCONFIXO PARAMPLANEJA.CDSETCONFIXO%TYPE;
   V_CDSETCONPERC PARAMPLANEJA.CDSETCONPERC%TYPE;
   V_QTCOMESTICAR CARDADIA.QTCOMESTICAR%TYPE;
   V_QTESTITURNO1 CARDADIA.QTESTITURNO1%TYPE;
   V_QTESTITURNO2 CARDADIA.QTESTITURNO2%TYPE;
   V_QTESTITURNO3 CARDADIA.QTESTITURNO3%TYPE;
   V_QTESTITURNO4 CARDADIA.QTESTITURNO4%TYPE;
   V_QTDEAUX      NUMBER;
   V_QTPRATO      NUMBER;
   V_FERIADO      NUMBER;
   V_CONTTURNO    NUMBER;
   V_CONTTURNCARD NUMBER;
   V_QTPREV       NUMBER;
   V_QTPREV1      NUMBER;
   V_QTPREV2      NUMBER;
   V_QTPREV3      NUMBER;
   V_QTPREV4      NUMBER;
   V_CDTPRCONSFIX CONSPLAN.CDTPPRATCPLA%TYPE;
   V_CDFILCONSFIX CONSPLAN.CDFAPRATCPLA%TYPE;
   V_CDPRACONSFIX CONSPLAN.CDPRATCONSPL%TYPE;
   V_CDTPRCONSPER CONSPLAN.CDTPPRATCPLA%TYPE;
   V_CDFILCONSPER CONSPLAN.CDFAPRATCPLA%TYPE;
   V_CDPRACONSPER CONSPLAN.CDPRATCONSPL%TYPE;
   V_NRPRPREVRETI VARCHAR2(4);
   V_SERV_ATUAL   VARCHAR2(5);
   V_TODOS_SERV   VARCHAR2(1);
   /* ---------- VARIAVEIS QUE CONTERAO OS CURSORES E OS REGISTROS --------------- */
   CUR_CONSPLAN   UTIL_CONSPLAN_PLA.CONSPLAN_CURTYPE;
   REC_CONSPLAN   UTIL_CONSPLAN_PLA.CONSPLAN_RECTYPE;
   CUR_COMPCARD   UTIL_CONSPLAN_PLA.COMPCARD_CURTYPE;
   REC_COMPCARD   UTIL_CONSPLAN_PLA.COMPCARD_RECTYPE;
   CUR_PRODPRAT   UTIL_CONSPLAN_PLA.PRODPRAT_CURTYPE;
   REC_PRODPRAT   UTIL_CONSPLAN_PLA.PRODPRAT_RECTYPE;
   CUR_CONSSERV   UTIL_CONSPLAN_PLA.CONSSERV_CURTYPE;
   REC_CONSSERV   UTIL_CONSPLAN_PLA.CONSSERV_RECTYPE;
   CUR_ARREDOND   UTIL_CONSPLAN_PLA.ARREDOND_CURTYPE;
   REC_ARREDOND   UTIL_CONSPLAN_PLA.ARREDOND_RECTYPE;
   V_FILIAL_INATIVA VARCHAR2(1);
BEGIN
   /* --------------------- IDENTIFICADORES DE CALCULO ---------------------- */
   V_IDCARDAPIO   := SUBSTR(P_IDPRODUTOS, 1, 1);         /* CARDAPIO          */
   V_IDCONSFIXO   := SUBSTR(P_IDPRODUTOS, 2, 1);         /* CONSUMO FIXO      */
   V_IDCONSPERC   := SUBSTR(P_IDPRODUTOS, 3, 1);         /* CONSUMO PERCAPITA */
   V_IDUTRETIPRAT := 'N';                       /* UTILIZA RETIRADA POR PRATO */
   
   /*SE FILIAL COM CONTRATO RESCINDIDO ENTAO ABORTA O CALCULO*/
   SELECT NVL(IDCNTRRESC,'N')
     INTO V_FILIAL_INATIVA
     FROM FILIAL
    WHERE CDFILIAL = P_FILIAL; 
   
   IF V_FILIAL_INATIVA = 'S' THEN
      RETURN; 
   END IF;
   
   /*Comando criado por que campos do tipo CLOB nao podem ser comparados a valor em consultas SQL*/
   IF P_SERVICOS = 'T' THEN
      V_TODOS_SERV := 'T';
   END IF;   
      
   /* - INICIALIZACAO DO ARQUIVO PREVRETI E CONSPLANSE ------------------- */
   SELECT MAX(CDTPRCONSFIX), MAX(CDFILCONSFIX), MAX(CDPRACONSFIX),
          MAX(CDTPRCONSPER), MAX(CDFILCONSPER), MAX(CDPRACONSPER)
   INTO V_CDTPRCONSFIX, V_CDFILCONSFIX, V_CDPRACONSFIX,
        V_CDTPRCONSPER, V_CDFILCONSPER, V_CDPRACONSPER
   FROM PARAMPLANEJA
   WHERE CDFILIAL = P_FILIAL;
   SELECT LENGTH(CDPICTPRAT) - (A.NIVEL - 1) INTO V_TAM_MASCARA
   FROM PARAMGERAL, (SELECT MAX(CDNVPRATO) AS NIVEL FROM PRATO) A;
   V_COUNTSERVICO := 2;
   FOR CUR_SERVICO IN
      (SELECT
          CDFILIAL,
          CDSERVFILI
       FROM
          SERVICO
       WHERE
          (CDFILIAL = P_FILIAL) AND
          ((INSTR(P_SERVICOS, CDSERVFILI) > 0) OR (V_TODOS_SERV = 'T'))
       ORDER BY
           CDSERVFILI)
   LOOP
      IF P_STDTINICIAL IS NULL THEN
         V_DTINICIAL    := P_DTINICIAL;
         V_DTFINAL      := P_DTFINAL;
      ELSE
         V_DTINICIAL    := TO_DATE(SUBSTR(P_STDTINICIAL, V_COUNTSERVICO, 8), 'YYYYMMDD');
         V_DTFINAL      := TO_DATE(SUBSTR(P_STDTFINAL, V_COUNTSERVICO, 8), 'YYYYMMDD');
        IF LENGTH(P_STDTINICIAL) > 10 THEN
           V_COUNTSERVICO := V_COUNTSERVICO + 9;
        END IF;
      END IF;
/*      BEGIN
         INSERT INTO CONSPLANSE
            (NRCONSPLANSE,
             CDFILICPLASE,
             CDSERVCPLASE,
             DTINICCPLASE,
             DTFINACPLASE)
         VALUES
            (P_NRCONSPLAN,
             CUR_SERVICO.CDFILIAL,
             CUR_SERVICO.CDSERVFILI,
             V_DTINICIAL,
             V_DTFINAL);
      EXCEPTION
         WHEN DUP_VAL_ON_INDEX THEN BEGIN
            UPDATE CONSPLANSE
               SET DTINICCPLASE = V_DTINICIAL,
                   DTFINACPLASE = V_DTFINAL
            WHERE
               NRCONSPLANSE = P_NRCONSPLAN AND
               CDFILICPLASE = CUR_SERVICO.CDFILIAL AND
               CDSERVCPLASE = CUR_SERVICO.CDSERVFILI;
         END;
      END;*/
      IF (P_IDARQUIVO IN ('P', 'A')) AND (V_IDCARDAPIO = 'C') THEN
         DELETE FROM PREVRETI
         WHERE
            (CDFILIAL   = CUR_SERVICO.CDFILIAL)            AND
            (CDSERVFILI = CUR_SERVICO.CDSERVFILI)          AND
            (DTPREVRETI BETWEEN P_DTINICIAL AND P_DTFINAL) AND
            (NRREQUEXTR = '0000000000')                    AND
            (IDPREVRETI = 'C');
         COMMIT; /* SOLIC 28995*/   
      END IF;
   END LOOP;
   /* ----------------- CALCULO DOS PRODUTOS DOS CARDAPIOS ------------------ */
   IF (V_IDCARDAPIO = 'C') THEN
      SELECT
         MAX(IDUTILFATCOR),
         MAX(IDUTILFATCOC),
         MAX(IDTIPCALCFAT)
      INTO
         V_IDUTILFATCOR,
         V_IDUTILFATCOC,
         V_IDTIPCALCFAT
      FROM
         PARAMPLANEJA
      WHERE
         CDFILIAL = P_FILIAL;
      IF V_IDUTILFATCOC IS NULL THEN
         V_IDUTILFATCOC := 'S';
      END IF;
      IF V_IDUTILFATCOR IS NULL THEN
         V_IDUTILFATCOR := 'S';
      END IF;
      IF V_IDTIPCALCFAT IS NULL THEN
         V_IDTIPCALCFAT := 'T';
      END IF;
      UTIL_CONSPLAN_PLA.OPEN_CONSPLAN_CURSOR(CUR_CONSPLAN,
                                         P_FILIAL,
                                         P_DTINICIAL,
                                         P_DTFINAL,
                                         P_SERVICOS,
                                         'C',
                                         P_NRCONSPLAN);
      /* ------------------- LOOP PARA CALCULO DOS CARDAPIOS ---------------- */
      LOOP
         FETCH CUR_CONSPLAN INTO REC_CONSPLAN;
         EXIT WHEN CUR_CONSPLAN%NOTFOUND;
         IF (REC_CONSPLAN.CDCLIENTE IS NULL) OR (REC_CONSPLAN.CDCLIENTE = 'XXX') THEN
            /* ---------------- AJUSTE DOS TURNOS DOS CARDAPIOS ---------------- */
            V_CONTTURNCARD := 0;
            IF NOT REC_CONSPLAN.QTESTITURNO1 IS NULL THEN
               V_CONTTURNCARD := V_CONTTURNCARD + 1;
            END IF;
            IF NOT REC_CONSPLAN.QTESTITURNO2 IS NULL THEN
               V_CONTTURNCARD := V_CONTTURNCARD + 1;
            END IF;
            IF NOT REC_CONSPLAN.QTESTITURNO3 IS NULL THEN
               V_CONTTURNCARD := V_CONTTURNCARD + 1;
            END IF;
            IF NOT REC_CONSPLAN.QTESTITURNO4 IS NULL THEN
               V_CONTTURNCARD := V_CONTTURNCARD + 1;
            END IF;
            SELECT COUNT(*)
            INTO V_CONTTURNO
            FROM TURNOSERV
            WHERE
               (CDFILIAL   = P_FILIAL) AND
               (CDSERVFILI = REC_CONSPLAN.CDSERVFILI);
            IF V_CONTTURNO = 0 THEN
               REC_CONSPLAN.QTESTITURNO1 := NULL;
               REC_CONSPLAN.QTESTITURNO2 := NULL;
               REC_CONSPLAN.QTESTITURNO3 := NULL;
               REC_CONSPLAN.QTESTITURNO4 := NULL;
               UPDATE
                  CARDADIA
               SET
                  QTCOMESTICAR = REC_CONSPLAN.QTCOMESTICAR,
                  QTESTITURNO1 = NULL,
                  QTESTITURNO2 = NULL,
                  QTESTITURNO3 = NULL,
                  QTESTITURNO4 = NULL
               WHERE
                  (CDFILIAL   = P_FILIAL) AND
                  (NRCARDAPIO = REC_CONSPLAN.NRCARDAPIO);
            ELSE
               SELECT COUNT(*) INTO V_CONTTURNO FROM TURNOSERV
               WHERE CDFILIAL = P_FILIAL AND
                     CDSERVFILI = REC_CONSPLAN.CDSERVFILI AND
                     CDTURNO = '1';
               IF (V_CONTTURNO = 0) THEN REC_CONSPLAN.QTESTITURNO1 := NULL; END IF;
               SELECT COUNT(*) INTO V_CONTTURNO FROM TURNOSERV
               WHERE CDFILIAL = P_FILIAL AND
                     CDSERVFILI = REC_CONSPLAN.CDSERVFILI AND
                     CDTURNO = '2';
               IF (V_CONTTURNO = 0) THEN REC_CONSPLAN.QTESTITURNO2 := NULL; END IF;
               SELECT COUNT(*) INTO V_CONTTURNO FROM TURNOSERV
               WHERE CDFILIAL = P_FILIAL AND
                     CDSERVFILI = REC_CONSPLAN.CDSERVFILI AND
                     CDTURNO = '3';
               IF (V_CONTTURNO = 0) THEN REC_CONSPLAN.QTESTITURNO3 := NULL; END IF;
               SELECT COUNT(*) INTO V_CONTTURNO FROM TURNOSERV
               WHERE CDFILIAL = P_FILIAL AND
                     CDSERVFILI = REC_CONSPLAN.CDSERVFILI AND
                     CDTURNO = '4';
               IF (V_CONTTURNO = 0) THEN REC_CONSPLAN.QTESTITURNO4 := NULL; END IF;
               UPDATE
                  CARDADIA
               SET
                  QTCOMESTICAR = REC_CONSPLAN.QTCOMESTICAR,
                  QTESTITURNO1 = REC_CONSPLAN.QTESTITURNO1,
                  QTESTITURNO2 = REC_CONSPLAN.QTESTITURNO2,
                  QTESTITURNO3 = REC_CONSPLAN.QTESTITURNO3,
                  QTESTITURNO4 = REC_CONSPLAN.QTESTITURNO4
               WHERE
                  (CDFILIAL   = P_FILIAL) AND
                  (NRCARDAPIO = REC_CONSPLAN.NRCARDAPIO);
            END IF;
            IF REC_CONSPLAN.QTESTITURNO1 IS NULL THEN REC_CONSPLAN.QTESTITURNO1 := 0; END IF;
            IF REC_CONSPLAN.QTESTITURNO2 IS NULL THEN REC_CONSPLAN.QTESTITURNO2 := 0; END IF;
            IF REC_CONSPLAN.QTESTITURNO3 IS NULL THEN REC_CONSPLAN.QTESTITURNO3 := 0; END IF;
            IF REC_CONSPLAN.QTESTITURNO4 IS NULL THEN REC_CONSPLAN.QTESTITURNO4 := 0; END IF;
         END IF;
         /* EDERSON - NAO CALCULA PRATOS PARA COMENSAIS IGUAL A ZERO */
         IF REC_CONSPLAN.QTCOMESTICAR <> 0 THEN
            /* ----------------- LOOP DA COMPOSICAO DO CARDAPIO ---------------- */
            UTIL_CONSPLAN_PLA.OPEN_COMPCARD_CURSOR(CUR_COMPCARD,
                                               P_FILIAL,
                                               REC_CONSPLAN.NRCARDAPIO);
            LOOP
               FETCH CUR_COMPCARD INTO REC_COMPCARD;
               EXIT WHEN CUR_COMPCARD%NOTFOUND;
               IF REC_COMPCARD.VRPECONSCARD <> 0 THEN
                  V_QTPRATO := (REC_COMPCARD.VRPECONSCARD * REC_CONSPLAN.QTCOMESTICAR)/ 100 ;
                  /* ---------------- LOOP DA COMPOSICAO DOS PRATOS --------------- */
                  UTIL_CONSPLAN_PLA.OPEN_PRODPRAT_CURSOR(CUR_PRODPRAT,
                                                         P_FILIAL,
                                                         REC_CONSPLAN.CDSERVFILI,
                                                         REC_COMPCARD.CDFILAUXPRAT,
                                                         REC_COMPCARD.CDPRATO,
                                                         V_QTPRATO,
                                                         P_CDPRODUTOINI,
                                                         P_CDPRODUTOFIN,
                                                         V_IDUTILFATCOR,
                                                         V_IDUTILFATCOC,
                                                         V_IDTIPCALCFAT
                                                         );
                  LOOP
                     FETCH CUR_PRODPRAT INTO REC_PRODPRAT;
                     EXIT WHEN CUR_PRODPRAT%NOTFOUND;
                     REC_PRODPRAT.QTPRODUTO := REC_PRODPRAT.QTPRODUTO * V_QTPRATO;
                     REC_PRODPRAT.QTBRUTAUNR := REC_PRODPRAT.QTBRUTAUNR * V_QTPRATO; 
                     V_DTLIMITE := TO_DATE(TO_CHAR(REC_CONSPLAN.DTDATA, 'DD/MM/YYYY')||REC_CONSPLAN.HRINICATEN, 'DD/MM/YYYYHH24MI');
                     SUBTHTOD_PLA(V_DTLIMITE,REC_PRODPRAT.HRTEMPPREP,V_DTLIMITE);
                     V_HRLIMITE := TO_CHAR(V_DTLIMITE, 'HH24MI');
                     /* ---------------- EVITA DIVISAO POR ZERO ------------------- */
                     V_QTDEAUX := REC_CONSPLAN.QTCOMESTICAR;
                     IF (V_QTDEAUX = 0) OR (V_QTDEAUX IS NULL) THEN
                        V_QTDEAUX := 1;
                     END IF;
                     SELECT NVL(IDUTRETIPRAT,'N')
                     INTO V_IDUTRETIPRAT
                     FROM SERVICO
                     WHERE
                        CDFILIAL = P_FILIAL AND
                        CDSERVFILI = REC_CONSPLAN.CDSERVFILI;
                     /* --------------------- GRAVA NA PREVRETI ------------------- */
                     IF P_IDARQUIVO = 'P' THEN
                        UTIL_CONSPLAN_PLA.ATUALIZA_PREVRETI(
                           P_FILIAL,
                           REC_CONSPLAN,
                           'C',
                           REC_PRODPRAT.CDPRODUTO,
                           V_DTLIMITE,
                           V_HRLIMITE,
                           (REC_PRODPRAT.QTPRODUTO) * (1 + (P_PEACRESCIMO / 100)),
                           (REC_PRODPRAT.QTPRODUTO * REC_CONSPLAN.QTESTITURNO1 / V_QTDEAUX) * (1 + (P_PEACRESCIMO / 100)),
                           (REC_PRODPRAT.QTPRODUTO * REC_CONSPLAN.QTESTITURNO2 / V_QTDEAUX) * (1 + (P_PEACRESCIMO / 100)),
                           (REC_PRODPRAT.QTPRODUTO * REC_CONSPLAN.QTESTITURNO3 / V_QTDEAUX) * (1 + (P_PEACRESCIMO / 100)),
                           (REC_PRODPRAT.QTPRODUTO * REC_CONSPLAN.QTESTITURNO4 / V_QTDEAUX) * (1 + (P_PEACRESCIMO / 100)),
                           'S',
                           P_CDOPERADOR,
                           V_IDUTRETIPRAT,
                           REC_COMPCARD.CDTIPOPRAT,
                           REC_COMPCARD.CDFILAUXPRAT,
                           REC_COMPCARD.CDPRATO,
                           REC_PRODPRAT.VRPEFATCOR,
                           REC_PRODPRAT.QTBRUTAUNR,
                           REC_PRODPRAT.SGUNIDADE
                           );
                     /* --------------------- GRAVA NA CONSPLAN ------------------- */
                     ELSE
                        V_CDSETOCONSPL := '  ';
                        V_CDFAPRATCPLA := ' ';
                        V_CDPRATCONSPL := '          ';
                        V_CDTPPRATCPLA := '  ';
                        V_SGUNIDAD   := NULL;
                        IF P_IDSEPPRATO = 'P' OR P_IDSEPPRATO = 'S' THEN
                           /* Por Setor */
                           IF P_IDSEPPRATO = 'S' THEN
                              SELECT
                                 CDARVPROD
                              INTO
                                 V_CDARVPROD
                              FROM
                                 PRODUTO
                              WHERE
                                 CDPRODUTO = REC_PRODPRAT.CDPRODUTO;
                              SELECT
                                 MAX(S.CDSETORCOZ)
                              INTO
                                 V_CDSETOCONSPL
                              FROM
                                 SETOPRODCARD S,
                                 PRODUTO P
                              WHERE
                                 (S.CDFILIAL   = P_FILIAL)                AND
                                 (S.CDTIPOPRAT = REC_COMPCARD.CDTIPOPRAT) AND
                                 (S.CDPRODUTO  = P.CDPRODUTO) AND
                                 (INSTR(V_CDARVPROD, P.CDARVPROD) = 1);
                              IF V_CDSETOCONSPL IS NULL THEN
                                 IF NOT REC_COMPCARD.CDSETORCOZ IS NULL THEN
                                    V_CDSETOCONSPL := REC_COMPCARD.CDSETORCOZ;
                                 ELSE
                                    V_CDSETOCONSPL := '  ';
                                 END IF;
                              END IF;
                           END IF;
                           V_CDFAPRATCPLA := REC_COMPCARD.CDFILAUXPRAT;
                           V_CDPRATCONSPL := REC_COMPCARD.CDPRATO;
                           V_CDTPPRATCPLA := REC_COMPCARD.CDTIPOPRAT;
                           V_SGUNIDAD     := REC_PRODPRAT.SGUNIDADE;
                        END IF;
                        UTIL_CONSPLAN_PLA.ATUALIZA_CONSPLAN(
                           P_NRCONSPLAN,
                           P_FILIAL,
                           REC_CONSPLAN,
                           'C',
                           V_CDFAPRATCPLA,
                           V_CDPRATCONSPL,
                           REC_PRODPRAT.CDAPRERECE,
                           V_CDSETOCONSPL,
                           REC_PRODPRAT.CDPRODUTO,
                           V_DTLIMITE,
                           V_HRLIMITE,
                           V_CDTPPRATCPLA,
                           (REC_PRODPRAT.QTPRODUTO) * (1 + (P_PEACRESCIMO / 100)),
                           (REC_PRODPRAT.QTPRODUTO * REC_CONSPLAN.QTESTITURNO1 / V_QTDEAUX) * (1 + (P_PEACRESCIMO / 100)),
                           (REC_PRODPRAT.QTPRODUTO * REC_CONSPLAN.QTESTITURNO2 / V_QTDEAUX) * (1 + (P_PEACRESCIMO / 100)),
                           (REC_PRODPRAT.QTPRODUTO * REC_CONSPLAN.QTESTITURNO3 / V_QTDEAUX) * (1 + (P_PEACRESCIMO / 100)),
                           (REC_PRODPRAT.QTPRODUTO * REC_CONSPLAN.QTESTITURNO4 / V_QTDEAUX) * (1 + (P_PEACRESCIMO / 100)),
                           REC_COMPCARD.VRPECONSCARD,
                           V_QTPRATO, /* REC_CONSPLAN.QTCOMESTICAR, */
                           'S',
                           REC_PRODPRAT.QTCOMPLPROD,
                           REC_PRODPRAT.VRPEFATCOR,
                           REC_PRODPRAT.QTBRUTAUNR,
                           V_SGUNIDAD,
                           REC_PRODPRAT.IDTPREQPROD
                           );
                     END IF; /* GRAVACAO NA CONSPLAN OU PREVRETI */
                  END LOOP; /* LOOP DA COMPOSICAO DOS PRATOS */
                  CLOSE CUR_PRODPRAT;
               END IF;
            END LOOP; /* LOOP DA COMPOSICAO DO CARDAPIO */
            CLOSE CUR_COMPCARD;
         END IF; /* EDERSON - NAO CALCULA PRATOS PARA COMENSAIS IGUAL A ZERO */
      END LOOP; /* LOOP PARA CALCULO DOS CARDAPIOS */
      CLOSE CUR_CONSPLAN;
      /* GRAVA NUMERO DE COMENSAIS POR PRATO (DIETA) */
      IF P_IDSEPPRATO = 'S' THEN
         FOR CUR_CONSPLANSE IN
         (SELECT
            CA.CDFILIAL,
            CA.CDSERVFILI,
            CA.DTCARDAPIO,
            CC.CDFILAUXPRAT,
            CC.CDTIPOPRAT,
            CC.CDPRATO,
            SUM((CC.VRPECONSCARD*CA.QTCOMESTICAR)/100) QTCOMESTDIE,
            SUM(CA.QTCOMESTICAR) QTCOMESTICAR
         FROM
            COMPCARD CC,
            CARDADIA CA
         WHERE
            CA.CDFILIAL = P_FILIAL      AND
            ((INSTR(P_SERVICOS, CA.CDSERVFILI) > 0) OR (V_TODOS_SERV = 'T')) AND
            (CA.DTCARDAPIO BETWEEN P_DTINICIAL AND P_DTFINAL) AND
            (NOT (CA.CDDIETPAD IS NULL))  AND
            CC.CDFILIAL     = CA.CDFILIAL AND
            CC.NRCARDAPIO   = CA.NRCARDAPIO
         GROUP BY
            CA.CDFILIAL,
            CA.CDSERVFILI,
            CA.DTCARDAPIO,
            CC.CDFILAUXPRAT,
            CC.CDTIPOPRAT,
            CC.CDPRATO     )
         LOOP
            UPDATE
               CONSPLAN
            SET
               QTCOMESTCPLA = CUR_CONSPLANSE.QTCOMESTICAR,
               QTCOMESTDIE  = CUR_CONSPLANSE.QTCOMESTDIE
            WHERE
               (CDFILICPLA   = P_FILIAL)                    AND
               (NRCONSPLAN   = P_NRCONSPLAN)                AND
               (CDSERVCPLA   = CUR_CONSPLANSE.CDSERVFILI)   AND
               (DTCONSPLAN   = CUR_CONSPLANSE.DTCARDAPIO)   AND
               (CDFAPRATCPLA = CUR_CONSPLANSE.CDFILAUXPRAT) AND
               (CDTPPRATCPLA = CUR_CONSPLANSE.CDTIPOPRAT)   AND
               (CDPRATCONSPL = CUR_CONSPLANSE.CDPRATO)      AND
               (IDCONSPLAN   = 'C');
         END LOOP;
      ELSE
         FOR CUR_CONSPLANSE IN
         (SELECT
            CA.CDFILIAL,
            CA.CDSERVFILI,
            CA.DTCARDAPIO,
            CC.CDFILAUXPRAT,
            CC.CDPRATO,
            SUM((CC.VRPECONSCARD*CA.QTCOMESTICAR)/100) QTCOMESTDIE,
            SUM(CA.QTCOMESTICAR) QTCOMESTICAR
         FROM
            COMPCARD CC,
            CARDADIA CA
         WHERE
            CA.CDFILIAL = P_FILIAL      AND
            ((INSTR(P_SERVICOS, CA.CDSERVFILI) > 0) OR (V_TODOS_SERV = 'T')) AND
            (CA.DTCARDAPIO BETWEEN P_DTINICIAL AND P_DTFINAL) AND
            (NOT (CA.CDDIETPAD IS NULL))  AND
            CC.CDFILIAL     = CA.CDFILIAL AND
            CC.NRCARDAPIO   = CA.NRCARDAPIO
         GROUP BY
            CA.CDFILIAL,
            CA.CDSERVFILI,
            CA.DTCARDAPIO,
            CC.CDFILAUXPRAT,
            CC.CDPRATO     )
         LOOP
            UPDATE
               CONSPLAN
            SET
               QTCOMESTCPLA = CUR_CONSPLANSE.QTCOMESTICAR,
               QTCOMESTDIE  = CUR_CONSPLANSE.QTCOMESTDIE
            WHERE
               (CDFILICPLA   = P_FILIAL)                    AND
               (NRCONSPLAN   = P_NRCONSPLAN)                AND
               (CDSERVCPLA   = CUR_CONSPLANSE.CDSERVFILI)   AND
               (DTCONSPLAN   = CUR_CONSPLANSE.DTCARDAPIO)   AND
               (CDFAPRATCPLA = CUR_CONSPLANSE.CDFILAUXPRAT) AND
               (CDPRATCONSPL = CUR_CONSPLANSE.CDPRATO)      AND
               (IDCONSPLAN   = 'C');
         END LOOP;
      END IF;
   END IF; /* CALCULO DOS PRODUTOS DOS CARDAPIOS */
   /* ----------------------- UTILIZACAO DA CONSSERV ------------------------ */
   IF (V_IDCONSFIXO = 'F') OR (V_IDCONSPERC = 'P') THEN
      SELECT
         MAX(CDSETCONFIXO),
         MAX(CDSETCONPERC)
      INTO
         V_CDSETCONFIXO,
         V_CDSETCONPERC
      FROM
         PARAMPLANEJA
      WHERE
         CDFILIAL = P_FILIAL;
      /* ABERTURA DO CURSOR */
      IF (V_IDCONSPERC = 'P') THEN
         UTIL_CONSPLAN_PLA.OPEN_CONSPLAN_CURSOR(CUR_CONSPLAN,
                                            P_FILIAL,
                                            P_DTINICIAL,
                                            P_DTFINAL,
                                            P_SERVICOS,
                                            'P',
                                            P_NRCONSPLAN);
         /* -------------------- LOOP PARA CALCULO DA CONSSERV ----------------- */
         LOOP
            FETCH CUR_CONSPLAN INTO REC_CONSPLAN;
            EXIT WHEN CUR_CONSPLAN%NOTFOUND;
            SELECT NVL(IDUTRETIPRAT,'N')
            INTO V_IDUTRETIPRAT
            FROM SERVICO
            WHERE
               CDFILIAL = P_FILIAL AND
               CDSERVFILI = REC_CONSPLAN.CDSERVFILI;
            IF REC_CONSPLAN.QTESTITURNO1 IS NULL THEN REC_CONSPLAN.QTESTITURNO1 := 0; END IF;
            IF REC_CONSPLAN.QTESTITURNO2 IS NULL THEN REC_CONSPLAN.QTESTITURNO2 := 0; END IF;
            IF REC_CONSPLAN.QTESTITURNO3 IS NULL THEN REC_CONSPLAN.QTESTITURNO3 := 0; END IF;
            IF REC_CONSPLAN.QTESTITURNO4 IS NULL THEN REC_CONSPLAN.QTESTITURNO4 := 0; END IF;
            /* VERIFICA SE E FERIADO */
            SELECT
               COUNT(*)
            INTO
               V_FERIADO
            FROM
               FERIFILI
            WHERE
               (CDFILIAL   = P_FILIAL) AND
               (DTFERIFILI = REC_CONSPLAN.DTDATA);
            /* --------------- INICIALIZACAO DO ARQUIVO PREVRETI --------------- */
            IF (P_IDARQUIVO IN ('P','A')) THEN
               DELETE FROM PREVRETI
                WHERE (CDFILIAL   = P_FILIAL)                AND
                      (CDSERVFILI = REC_CONSPLAN.CDSERVFILI) AND
                      (DTPREVRETI = REC_CONSPLAN.DTDATA)     AND
                      (NRREQUEXTR = '0000000000')            AND
                      (IDPREVRETI = 'P');
            END IF;
            /* ---------------- LOOP PARA COMPOSICAO DA CONSSERV --------------- */
            UTIL_CONSPLAN_PLA.OPEN_CONSSERV_CURSOR(CUR_CONSSERV,
                                               P_FILIAL,
                                               REC_CONSPLAN.CDSERVFILI,
                                               REC_CONSPLAN.DTDATA,
                                               V_FERIADO,
                                               P_CDPRODUTOINI,
                                               P_CDPRODUTOFIN,
                                               'P');
            LOOP
               FETCH CUR_CONSSERV INTO REC_CONSSERV;
               EXIT WHEN CUR_CONSSERV%NOTFOUND;
               /* TESTE PARA IMPEDIR DE SE CADASTRAR CONSUMO PERCAPITA PARA DIAS */
               /* ----------------------- SEM CARDAPIO ------------------------- */
               IF((V_IDCONSPERC = 'P') AND (NOT REC_CONSPLAN.NRCARDAPIO IS NULL)) THEN
               /* TESTE PARA CALCULO DAS QUANTIDADES POR TURNO */
                  V_QTCOMESTICAR := (REC_CONSSERV.QTCONSSERV * REC_CONSPLAN.QTCOMESTICAR) * (1 + (P_PEACRESCIMO / 100));
                  V_QTESTITURNO1 := (REC_CONSSERV.QTCONSSERV * REC_CONSPLAN.QTESTITURNO1) * (1 + (P_PEACRESCIMO / 100));
                  V_QTESTITURNO2 := (REC_CONSSERV.QTCONSSERV * REC_CONSPLAN.QTESTITURNO2) * (1 + (P_PEACRESCIMO / 100));
                  V_QTESTITURNO3 := (REC_CONSSERV.QTCONSSERV * REC_CONSPLAN.QTESTITURNO3) * (1 + (P_PEACRESCIMO / 100));
                  V_QTESTITURNO4 := (REC_CONSSERV.QTCONSSERV * REC_CONSPLAN.QTESTITURNO4) * (1 + (P_PEACRESCIMO / 100));

                  IF(V_IDCONSPERC = 'P' AND REC_CONSSERV.IDQTDECONS = 'P') THEN
                     IF P_IDARQUIVO = 'P' THEN
                        UTIL_CONSPLAN_PLA.ATUALIZA_PREVRETI(
                           P_FILIAL,
                           REC_CONSPLAN,
                           REC_CONSSERV.IDQTDECONS,
                           REC_CONSSERV.CDPRODUTO,
                           REC_CONSPLAN.DTDATA,
                           REC_CONSPLAN.HRINICATEN,
                           V_QTCOMESTICAR,
                           V_QTESTITURNO1,
                           V_QTESTITURNO2,
                           V_QTESTITURNO3,
                           V_QTESTITURNO4,
                           'N',
                           P_CDOPERADOR,
                           'N',
                           NULL,
                           NULL,
                           NULL,
                           0,
                           0,
                           NULL
                           );
                     /* ----------------------- GRAVA NA CONSPLAN -------------------- */
                     ELSE
                        V_CDFAPRATCPLA := ' ';
                        IF V_IDUTRETIPRAT = 'S' THEN
                           V_CDTPPRATCPLA := V_CDTPRCONSPER;
                        ELSE
                           V_CDTPPRATCPLA := 'PP';
                        END IF;
                        V_CDSETOCONSPL := '  ';
                        V_CDPRATCONSPL := '          ';
                        IF P_IDSEPPRATO = 'S' OR P_IDSEPPRATO = 'P' THEN
                           IF P_IDSEPPRATO = 'S' THEN
                              SELECT
                                 CDARVPROD
                              INTO
                                 V_CDARVPROD
                              FROM
                                 PRODUTO
                              WHERE
                                 CDPRODUTO = REC_CONSSERV.CDPRODUTO;
                              SELECT
                                 MAX(S.CDSETORCOZ)
                              INTO
                                 V_CDSETOCONSPL
                              FROM
                                 SETOPRODCONS S,
                                 PRODUTO P
                              WHERE
                                 (S.CDFILIAL     = P_FILIAL)                AND
                                 (S.IDQTPRODCONS = REC_CONSSERV.IDQTDECONS) AND
                                 (S.CDPRODUTO    = P.CDPRODUTO)             AND
                                 (INSTR(V_CDARVPROD, P.CDARVPROD) = 1);
                              IF V_CDSETOCONSPL IS NULL THEN
                                 V_CDSETOCONSPL := V_CDSETCONPERC;
                                 IF V_CDSETOCONSPL IS NULL THEN
                                    V_CDSETOCONSPL := '  ';
                                 END IF;
                              END IF;
                           END IF;
                           IF V_IDUTRETIPRAT = 'S' THEN
                              V_CDPRATCONSPL := V_CDPRACONSPER;
                              V_CDFAPRATCPLA := V_CDFILCONSPER;
                           ELSE
                              V_CDPRATCONSPL := RPAD('P', V_TAM_MASCARA, 'P') /*'PPPPPPPPPP'*/;
                              V_CDFAPRATCPLA := 'P';
                           END IF;
                        END IF;
                        UTIL_CONSPLAN_PLA.ATUALIZA_CONSPLAN(
                           P_NRCONSPLAN,
                           P_FILIAL,
                           REC_CONSPLAN,
                           REC_CONSSERV.IDQTDECONS,
                           V_CDFAPRATCPLA,
                           V_CDPRATCONSPL,
                           REC_PRODPRAT.CDAPRERECE,
                           V_CDSETOCONSPL,
                           REC_CONSSERV.CDPRODUTO,
                           REC_CONSPLAN.DTDATA,
                           REC_CONSPLAN.HRINICATEN,
                           V_CDTPPRATCPLA,
                           V_QTCOMESTICAR,
                           V_QTESTITURNO1,
                           V_QTESTITURNO2,
                           V_QTESTITURNO3,
                           V_QTESTITURNO4,
                           100,
                           REC_CONSPLAN.QTCOMESTICAR,
                           'N',
                           0,
                           0,
                           0,
                           NULL,
                           REC_CONSSERV.IDTPREQPROD);
                     END IF; /* GRAVACAO NA CONSPLAN OU PREVRETI */
                  END IF; /* TESTE DE CONSUMO PERCAPITA PARA DIAS SEM CARDAPIO */
               END IF;
            END LOOP; /* LOOP DA COMPOSICAO DA CONSSERV */
            CLOSE CUR_CONSSERV;
         END LOOP; /* LOOP PARA CALCULO DA CONSERV */
         CLOSE CUR_CONSPLAN;
      END IF;

      IF (V_IDCONSFIXO = 'F') THEN
         UTIL_CONSPLAN_PLA.OPEN_CONSPLAN_CURSOR(CUR_CONSPLAN,
                                                P_FILIAL,
                                                P_DTINICIAL,
                                                P_DTFINAL,
                                                P_SERVICOS,
                                                'F',
                                                P_NRCONSPLAN);

         /* -------------------- LOOP PARA CALCULO DA CONSSERV ----------------- */
         LOOP
            FETCH CUR_CONSPLAN INTO REC_CONSPLAN;
            EXIT WHEN CUR_CONSPLAN%NOTFOUND;
            SELECT NVL(IDUTRETIPRAT,'N')
            INTO V_IDUTRETIPRAT
            FROM SERVICO
            WHERE
               CDFILIAL = P_FILIAL AND
               CDSERVFILI = REC_CONSPLAN.CDSERVFILI;
            IF REC_CONSPLAN.QTESTITURNO1 IS NULL THEN REC_CONSPLAN.QTESTITURNO1 := 0; END IF;
            IF REC_CONSPLAN.QTESTITURNO2 IS NULL THEN REC_CONSPLAN.QTESTITURNO2 := 0; END IF;
            IF REC_CONSPLAN.QTESTITURNO3 IS NULL THEN REC_CONSPLAN.QTESTITURNO3 := 0; END IF;
            IF REC_CONSPLAN.QTESTITURNO4 IS NULL THEN REC_CONSPLAN.QTESTITURNO4 := 0; END IF;
            /* VERIFICA SE E FERIADO */
            SELECT
               COUNT(*)
            INTO
               V_FERIADO
            FROM
               FERIFILI
            WHERE
               (CDFILIAL   = P_FILIAL) AND
               (DTFERIFILI = REC_CONSPLAN.DTDATA);
            /* --------------- INICIALIZACAO DO ARQUIVO PREVRETI --------------- */
            IF (P_IDARQUIVO IN ('P','A')) THEN
               DELETE FROM PREVRETI
                WHERE(CDFILIAL   = P_FILIAL)                AND
                     (CDSERVFILI = REC_CONSPLAN.CDSERVFILI) AND
                     (DTPREVRETI = REC_CONSPLAN.DTDATA)     AND
                     (NRREQUEXTR = '0000000000')            AND
                     (IDPREVRETI = 'F');
            END IF;
            /* ---------------- LOOP PARA COMPOSICAO DA CONSSERV --------------- */
            UTIL_CONSPLAN_PLA.OPEN_CONSSERV_CURSOR(CUR_CONSSERV,
                                                   P_FILIAL,
                                                   REC_CONSPLAN.CDSERVFILI,
                                                   REC_CONSPLAN.DTDATA,
                                                   V_FERIADO,
                                                   P_CDPRODUTOINI,
                                                   P_CDPRODUTOFIN,
                                                   'F');
            LOOP
               FETCH CUR_CONSSERV INTO REC_CONSSERV;
               EXIT WHEN CUR_CONSSERV%NOTFOUND;
               /* TESTE PARA IMPEDIR DE SE CADASTRAR CONSUMO PERCAPITA PARA DIAS */
               /* ----------------------- SEM CARDAPIO ------------------------- */
               IF ((V_IDCONSFIXO = 'F') AND (REC_CONSSERV.IDQTDECONS = 'F')) THEN
                  /* TESTE PARA CALCULO DAS QUANTIDADES POR TURNO */
                  IF REC_CONSSERV.IDQTDECONS = 'F' THEN
                     V_QTCOMESTICAR := (REC_CONSSERV.QTCONSSERV) * (1 + (P_PEACRESCIMO / 100));
                     V_QTESTITURNO1 := 0;
                     V_QTESTITURNO2 := 0;
                     V_QTESTITURNO3 := 0;
                     V_QTESTITURNO4 := 0;
                  END IF;
                  /* ----------------------- GRAVA NA PREVRETI -------------------- */
                  IF (V_IDCONSFIXO = 'F' AND REC_CONSSERV.IDQTDECONS = 'F') THEN
                     IF P_IDARQUIVO = 'P' THEN
                        UTIL_CONSPLAN_PLA.ATUALIZA_PREVRETI(P_FILIAL,
                                                            REC_CONSPLAN,
                                                            REC_CONSSERV.IDQTDECONS,
                                                            REC_CONSSERV.CDPRODUTO,
                                                            REC_CONSPLAN.DTDATA,
                                                            REC_CONSPLAN.HRINICATEN,
                                                            V_QTCOMESTICAR,
                                                            V_QTESTITURNO1,
                                                            V_QTESTITURNO2,
                                                            V_QTESTITURNO3,
                                                            V_QTESTITURNO4,
                                                            'N',
                                                            P_CDOPERADOR,
                                                            'N',
                                                            NULL,
                                                            NULL,
                                                            NULL,
                                                            0,
                                                            0,
                                                            NULL
                                                            );
                     /* ----------------------- GRAVA NA CONSPLAN -------------------- */
                     ELSE
                        V_CDFAPRATCPLA := ' ';
                        IF V_IDUTRETIPRAT = 'S' THEN
                           V_CDTPPRATCPLA := V_CDTPRCONSFIX;
                        ELSE
                           V_CDTPPRATCPLA := 'FF';
                        END IF;
                        V_CDSETOCONSPL := '  ';
                        V_CDPRATCONSPL := '          ';
                        IF P_IDSEPPRATO = 'S' OR P_IDSEPPRATO = 'P' THEN
                           IF P_IDSEPPRATO = 'S' THEN
                              SELECT
                                 CDARVPROD
                              INTO
                                 V_CDARVPROD
                              FROM
                                 PRODUTO
                              WHERE
                                 CDPRODUTO = REC_CONSSERV.CDPRODUTO;
                              SELECT
                                 MAX(S.CDSETORCOZ)
                              INTO
                                 V_CDSETOCONSPL
                              FROM
                                 SETOPRODCONS S,
                                 PRODUTO P
                              WHERE
                                 (S.CDFILIAL     = P_FILIAL)                AND
                                 (S.IDQTPRODCONS = REC_CONSSERV.IDQTDECONS) AND
                                 (S.CDPRODUTO    = P.CDPRODUTO)             AND
                                 (INSTR(V_CDARVPROD, P.CDARVPROD) = 1);
                              IF V_CDSETOCONSPL IS NULL THEN
                                 V_CDSETOCONSPL := V_CDSETCONFIXO;
                                 IF V_CDSETOCONSPL IS NULL THEN
                                    V_CDSETOCONSPL := '  ';
                                 END IF;
                              END IF;
                           END IF;
                           IF V_IDUTRETIPRAT = 'S' THEN
                              V_CDPRATCONSPL := V_CDPRACONSFIX;
                              V_CDFAPRATCPLA := V_CDFILCONSFIX;
                           ELSE
                              V_CDPRATCONSPL := RPAD('F', V_TAM_MASCARA, 'F') /*'FFFFFFFFFF'*/;
                              V_CDFAPRATCPLA := 'P';
                           END IF;
                        END IF;
                        UTIL_CONSPLAN_PLA.ATUALIZA_CONSPLAN(
                           P_NRCONSPLAN,
                           P_FILIAL,
                           REC_CONSPLAN,
                           REC_CONSSERV.IDQTDECONS,
                           V_CDFAPRATCPLA,
                           V_CDPRATCONSPL,
                           REC_PRODPRAT.CDAPRERECE,
                           V_CDSETOCONSPL,
                           REC_CONSSERV.CDPRODUTO,
                           REC_CONSPLAN.DTDATA,
                           REC_CONSPLAN.HRINICATEN,
                           V_CDTPPRATCPLA,
                           V_QTCOMESTICAR,
                           V_QTESTITURNO1,
                           V_QTESTITURNO2,
                           V_QTESTITURNO3,
                           V_QTESTITURNO4,
                           100,
                           REC_CONSPLAN.QTCOMESTICAR,
                           'N',
                           0,
                           0,
                           0,
                           NULL,
                           REC_CONSSERV.IDTPREQPROD);
                     END IF; /* GRAVACAO NA CONSPLAN OU PREVRETI */



                  END IF; /* TESTE DE CONSUMO PERCAPITA PARA DIAS SEM CARDAPIO */
               END IF;
            END LOOP; /* LOOP DA COMPOSICAO DA CONSSERV */
            CLOSE CUR_CONSSERV;
         END LOOP; /* LOOP PARA CALCULO DA CONSERV */
         CLOSE CUR_CONSPLAN;


         UTIL_CONSPLAN_PLA.OPEN_CONSPLAN_CURSOR(CUR_CONSPLAN,
                                                 P_FILIAL,
                                                 P_DTINICIAL,
                                                 P_DTFINAL,
                                                 P_SERVICOS,
                                                 'E',
                                                 P_NRCONSPLAN);

          /* -------------------- LOOP PARA CALCULO DA CONSSERV ----------------- */
          LOOP
             FETCH CUR_CONSPLAN INTO REC_CONSPLAN;
             EXIT WHEN CUR_CONSPLAN%NOTFOUND;
             SELECT NVL(IDUTRETIPRAT,'N')
             INTO V_IDUTRETIPRAT
             FROM SERVICO
             WHERE
                CDFILIAL = P_FILIAL AND
                CDSERVFILI = REC_CONSPLAN.CDSERVFILI;
             IF REC_CONSPLAN.QTESTITURNO1 IS NULL THEN REC_CONSPLAN.QTESTITURNO1 := 0; END IF;
             IF REC_CONSPLAN.QTESTITURNO2 IS NULL THEN REC_CONSPLAN.QTESTITURNO2 := 0; END IF;
             IF REC_CONSPLAN.QTESTITURNO3 IS NULL THEN REC_CONSPLAN.QTESTITURNO3 := 0; END IF;
             IF REC_CONSPLAN.QTESTITURNO4 IS NULL THEN REC_CONSPLAN.QTESTITURNO4 := 0; END IF;
             /* VERIFICA SE E FERIADO */
             SELECT
                COUNT(*)
             INTO
                V_FERIADO
             FROM
                FERIFILI
             WHERE
                (CDFILIAL   = P_FILIAL) AND
                (DTFERIFILI = REC_CONSPLAN.DTDATA);
              --------------- INICIALIZACAO DO ARQUIVO PREVRETI --------------- 
             IF (P_IDARQUIVO IN ('P','A')) THEN
                DELETE FROM PREVRETI
                 WHERE(CDFILIAL   = P_FILIAL)                AND
                      (CDSERVFILI = REC_CONSPLAN.CDSERVFILI) AND
                      (DTPREVRETI = REC_CONSPLAN.DTDATA)     AND
                      (NRREQUEXTR = '0000000000')            AND
                      (IDPREVRETI = 'F');
             END IF;
             /* ---------------- LOOP PARA COMPOSICAO DA CONSSERV --------------- */
             UTIL_CONSPLAN_PLA.OPEN_CONSSERV_CURSOR(CUR_CONSSERV,
                                                    P_FILIAL,
                                                    REC_CONSPLAN.CDSERVFILI,
                                                    REC_CONSPLAN.DTDATA,
                                                    V_FERIADO,
                                                    P_CDPRODUTOINI,
                                                    P_CDPRODUTOFIN,
                                                    'E');
             LOOP
                FETCH CUR_CONSSERV INTO REC_CONSSERV;
                EXIT WHEN CUR_CONSSERV%NOTFOUND;
                /* TESTE PARA IMPEDIR DE SE CADASTRAR CONSUMO PERCAPITA PARA DIAS */
                /* ----------------------- SEM CARDAPIO ------------------------- */
                IF ((V_IDCONSFIXO = 'F') AND (REC_CONSSERV.IDQTDECONS = 'F')) THEN
                   /* TESTE PARA CALCULO DAS QUANTIDADES POR TURNO */
                   IF REC_CONSSERV.IDQTDECONS = 'F' THEN
                      V_QTCOMESTICAR := (REC_CONSSERV.QTCONSSERV) * (1 + (P_PEACRESCIMO / 100));
                      V_QTESTITURNO1 := 0;
                      V_QTESTITURNO2 := 0;
                      V_QTESTITURNO3 := 0;
                      V_QTESTITURNO4 := 0;
                   END IF;
                   /* ----------------------- GRAVA NA PREVRETI -------------------- */
                   IF (V_IDCONSFIXO = 'F' AND REC_CONSSERV.IDQTDECONS = 'F') THEN
                      IF P_IDARQUIVO = 'P' THEN
                         UTIL_CONSPLAN_PLA.ATUALIZA_PREVRETI(P_FILIAL,
                                                             REC_CONSPLAN,
                                                             REC_CONSSERV.IDQTDECONS,
                                                             REC_CONSSERV.CDPRODUTO,
                                                             REC_CONSPLAN.DTDATA,
                                                             REC_CONSPLAN.HRINICATEN,
                                                             V_QTCOMESTICAR,
                                                             V_QTESTITURNO1,
                                                             V_QTESTITURNO2,
                                                             V_QTESTITURNO3,
                                                             V_QTESTITURNO4,
                                                             'N',
                                                             P_CDOPERADOR,
                                                             'N',
                                                             NULL,
                                                             NULL,
                                                             NULL,
                                                             0,
                                                             0,
                                                             NULL
                                                             );
                      /* ----------------------- GRAVA NA CONSPLAN -------------------- */
                      ELSE
                         V_CDFAPRATCPLA := ' ';
                         IF V_IDUTRETIPRAT = 'S' THEN
                            V_CDTPPRATCPLA := V_CDTPRCONSFIX;
                         ELSE
                            V_CDTPPRATCPLA := 'FF';
                         END IF;
                         V_CDSETOCONSPL := '  ';
                         V_CDPRATCONSPL := '          ';
                         IF P_IDSEPPRATO = 'S' OR P_IDSEPPRATO = 'P' THEN
                            IF P_IDSEPPRATO = 'S' THEN
                               SELECT
                                  CDARVPROD
                               INTO
                                  V_CDARVPROD
                               FROM
                                  PRODUTO
                               WHERE
                                  CDPRODUTO = REC_CONSSERV.CDPRODUTO;
                               SELECT
                                  MAX(S.CDSETORCOZ)
                               INTO
                                  V_CDSETOCONSPL
                               FROM
                                  SETOPRODCONS S,
                                  PRODUTO P
                               WHERE
                                  (S.CDFILIAL     = P_FILIAL)                AND
                                  (S.IDQTPRODCONS = REC_CONSSERV.IDQTDECONS) AND
                                  (S.CDPRODUTO    = P.CDPRODUTO)             AND
                                  (INSTR(V_CDARVPROD, P.CDARVPROD) = 1);
                               IF V_CDSETOCONSPL IS NULL THEN
                                  V_CDSETOCONSPL := V_CDSETCONFIXO;
                                  IF V_CDSETOCONSPL IS NULL THEN
                                     V_CDSETOCONSPL := '  ';
                                  END IF;
                               END IF;
                            END IF;
                            IF V_IDUTRETIPRAT = 'S' THEN
                               V_CDPRATCONSPL := V_CDPRACONSFIX;
                               V_CDFAPRATCPLA := V_CDFILCONSFIX;
                            ELSE
                               V_CDPRATCONSPL := RPAD('F', V_TAM_MASCARA, 'F') /*'FFFFFFFFFF'*/;
                               V_CDFAPRATCPLA := 'P';
                            END IF;
                         END IF;
                         UTIL_CONSPLAN_PLA.ATUALIZA_CONSPLAN(
                            P_NRCONSPLAN,
                            P_FILIAL,
                            REC_CONSPLAN,
                            REC_CONSSERV.IDQTDECONS,
                            V_CDFAPRATCPLA,
                            V_CDPRATCONSPL,
                            REC_PRODPRAT.CDAPRERECE,
                            V_CDSETOCONSPL,
                            REC_CONSSERV.CDPRODUTO,
                            REC_CONSPLAN.DTDATA,
                            REC_CONSPLAN.HRINICATEN,
                            V_CDTPPRATCPLA,
                            V_QTCOMESTICAR,
                            V_QTESTITURNO1,
                            V_QTESTITURNO2,
                            V_QTESTITURNO3,
                            V_QTESTITURNO4,
                            100,
                            REC_CONSPLAN.QTCOMESTICAR,
                            'N',
                            0,
                            0,
                            0,
                            NULL,
                            REC_CONSSERV.IDTPREQPROD);
                      END IF; /* GRAVACAO NA CONSPLAN OU PREVRETI */



                   END IF; /* TESTE DE CONSUMO PERCAPITA PARA DIAS SEM CARDAPIO */
                END IF;
             END LOOP; /* LOOP DA COMPOSICAO DA CONSSERV */
             CLOSE CUR_CONSSERV;
          END LOOP; /* LOOP PARA CALCULO DA CONSERV */
          CLOSE CUR_CONSPLAN;
         
   END IF;



   END IF; /* UTILIZACAO DA CONSSERV */
   IF (P_IDARREDOND = 'S') OR (P_IDARREDOND = 'C') THEN
      /* S - ARREDONDAMENTO DE REQUISICAO */
      /* N - NAO ARREDONDAR               */
      /* C - ARREDONDAMENTO DE COMPRAS    */
      IF P_IDARQUIVO = 'P' THEN
         UTIL_CONSPLAN_PLA.OPEN_ARREDOND_CURSOR(CUR_ARREDOND,
                                            'P',
                                            P_FILIAL,
                                            P_SERVICOS,
                                            P_DTINICIAL,
                                            P_DTFINAL,
                                            '0000000000',
                                            P_NRCONSPLAN);
      ELSE
         UTIL_CONSPLAN_PLA.OPEN_ARREDOND_CURSOR(CUR_ARREDOND,
                                            'C',
                                            P_FILIAL,
                                            P_SERVICOS,
                                            P_DTINICIAL,
                                            P_DTFINAL,
                                            P_NRCONSPLAN,
                                            P_NRCONSPLAN);
      END IF;
      LOOP
         FETCH CUR_ARREDOND INTO REC_ARREDOND;
         EXIT WHEN CUR_ARREDOND%NOTFOUND;
         IF (P_IDARREDOND = 'S') THEN
            V_ARRED := REC_ARREDOND.VRARREDREQ;
         ELSE
            V_ARRED := REC_ARREDOND.VRARREDCMP;
         END IF;
         IF NOT (V_ARRED IS NULL) AND (V_ARRED <> 0) THEN
            IF (REC_ARREDOND.QTCOMENST1 > 0) AND (MOD(REC_ARREDOND.QTCOMENST1, V_ARRED) > 0) THEN
               V_QTPREV1 := (FLOOR(REC_ARREDOND.QTCOMENST1/V_ARRED) + 1) * V_ARRED;
            ELSE
               V_QTPREV1 := REC_ARREDOND.QTCOMENST1;
            END IF;
            IF (REC_ARREDOND.QTCOMENST2 > 0) AND (MOD(REC_ARREDOND.QTCOMENST2, V_ARRED) > 0) THEN
               V_QTPREV2 := (FLOOR(REC_ARREDOND.QTCOMENST2/V_ARRED) + 1) * V_ARRED;
            ELSE
               V_QTPREV2 := REC_ARREDOND.QTCOMENST2;
            END IF;
            IF (REC_ARREDOND.QTCOMENST3 > 0) AND (MOD(REC_ARREDOND.QTCOMENST3, V_ARRED) > 0) THEN
               V_QTPREV3 := (FLOOR(REC_ARREDOND.QTCOMENST3/V_ARRED) + 1) * V_ARRED;
            ELSE
               V_QTPREV3 := REC_ARREDOND.QTCOMENST3;
            END IF;
            IF (REC_ARREDOND.QTCOMENST4 > 0) AND (MOD(REC_ARREDOND.QTCOMENST4,V_ARRED) > 0) THEN
               V_QTPREV4 := (FLOOR(REC_ARREDOND.QTCOMENST4/V_ARRED) + 1) * V_ARRED;
            ELSE
               V_QTPREV4 := REC_ARREDOND.QTCOMENST4;
            END IF;
            IF (V_QTPREV1 = 0) AND (V_QTPREV2 = 0) AND (V_QTPREV3 = 0) AND (V_QTPREV4 = 0) THEN
               IF (REC_ARREDOND.QTCOMENS > 0) AND (MOD(REC_ARREDOND.QTCOMENS, V_ARRED) > 0) THEN
                  V_QTPREV := (FLOOR(REC_ARREDOND.QTCOMENS/V_ARRED) + 1) * V_ARRED;
               ELSE
                  V_QTPREV := REC_ARREDOND.QTCOMENS;
               END IF;
            ELSE
               V_QTPREV := V_QTPREV1 + V_QTPREV2 + V_QTPREV3 + V_QTPREV4 ;
            END IF;
            IF P_IDARQUIVO = 'P' THEN
               UPDATE
                  PREVRETI
               SET
                  QTPREVRETI = V_QTPREV,
                  QTPRERETT1 = V_QTPREV1,
                  QTPRERETT2 = V_QTPREV2,
                  QTPRERETT3 = V_QTPREV3,
                  QTPRERETT4 = V_QTPREV4,
                  QTPREVRETISA = REC_ARREDOND.QTCOMENS,
                  QTPRERETT1SA = REC_ARREDOND.QTCOMENST1,
                  QTPRERETT2SA = REC_ARREDOND.QTCOMENST2,
                  QTPRERETT3SA = REC_ARREDOND.QTCOMENST3,
                  QTPRERETT4SA = REC_ARREDOND.QTCOMENST4
               WHERE
                  (CDFILIAL     = REC_ARREDOND.CDFILIAL)     AND
                  (CDSERVFILI   = REC_ARREDOND.CDSERVFILI)   AND
                  (DTPREVRETI   = REC_ARREDOND.DTDATA)       AND
                  (NRREQUEXTR   = REC_ARREDOND.NRREQUCONS)   AND
                  (IDPREVRETI   = REC_ARREDOND.IDTIPOCONS)   AND
                  (CDPRODUTO    = REC_ARREDOND.CDPRODUTO)    AND
                  (DTLIMITERETI = REC_ARREDOND.DTLIMITE)     AND
                  (HRLIMITERETI = REC_ARREDOND.HRLIMITE)     AND
                  (CDTIPOPRAT   = REC_ARREDOND.CDTIPOPRAT)   AND
                  (CDFILAUXPRAT = REC_ARREDOND.CDFILAUXPRAT) AND
                  (CDPRATO      = REC_ARREDOND.CDPRATO);
            ELSE
               UPDATE
                  CONSPLAN
               SET
                  QTCONSPLAN   = V_QTPREV,
                  QTCONSPLANT1 = V_QTPREV1,
                  QTCONSPLANT2 = V_QTPREV2,
                  QTCONSPLANT3 = V_QTPREV3,
                  QTCONSPLANT4 = V_QTPREV4,
                  QTCONSPLANSA = REC_ARREDOND.QTCOMENS,
                  QTCONPLAT1SA = REC_ARREDOND.QTCOMENST1,
                  QTCONPLAT2SA = REC_ARREDOND.QTCOMENST2,
                  QTCONPLAT3SA = REC_ARREDOND.QTCOMENST3,
                  QTCONPLAT4SA = REC_ARREDOND.QTCOMENST4
               WHERE
                  (CDFILICPLA   = REC_ARREDOND.CDFILIAL)     AND
                  (NRCONSPLAN   = REC_ARREDOND.NRREQUCONS)   AND
                  (CDSERVCPLA   = REC_ARREDOND.CDSERVFILI)   AND
                  (DTCONSPLAN   = REC_ARREDOND.DTDATA)       AND
                  (IDCONSPLAN   = REC_ARREDOND.IDTIPOCONS)   AND
                  (CDCLIECPLA   = REC_ARREDOND.CDCLIENTE)    AND
                  (CDDESTFICPLA = REC_ARREDOND.CDDESTFILI)   AND
                  (CDFAPRATCPLA = REC_ARREDOND.CDFILAUXPRAT) AND
                  (CDPRATCONSPL = REC_ARREDOND.CDPRATO)      AND
                  (CDPRODCPLA   = REC_ARREDOND.CDPRODUTO)    AND
                  (DTLIMITECPLA = REC_ARREDOND.DTLIMITE)     AND
                  (HRLIMITECPLA = REC_ARREDOND.HRLIMITE)     AND
                  (CDTPPRATCPLA = REC_ARREDOND.CDTIPOPRAT);
            END IF;
         END IF; /* SE HOUVER FATOR DE ARREDONDAMENTO DO PRODUTO*/
      END LOOP;
      CLOSE CUR_ARREDOND;
   END IF; /* ARREDONDAMENTO DE REQUISICAO */
   IF (P_IDARQUIVO = 'A') THEN
      V_SERV_ATUAL := ' ';
      FOR C_PREVRETI IN (
          SELECT
             CDFILICPLA, CDSERVCPLA, DTCONSPLAN,
             '0000000000', IDCONSPLAN, CDPRODCPLA,
             DTLIMITECPLA, HRLIMITECPLA, SUM(QTCONSPLAN) AS QTCONSPLAN,
             SUM(QTCONSPLANT1) AS QTCONSPLANT1, SUM(QTCONSPLANT2) AS QTCONSPLANT2, SUM(QTCONSPLANT3) AS QTCONSPLANT3,
             SUM(QTCONSPLANT4) AS QTCONSPLANT4, SUM(QTCONSPLANSA) AS QTCONSPLANSA, SUM(QTCONPLAT1SA) AS QTCONPLAT1SA,
             SUM(QTCONPLAT2SA) AS QTCONPLAT2SA, SUM(QTCONPLAT3SA) AS QTCONPLAT3SA, SUM(QTCONPLAT4SA) AS QTCONPLAT4SA,
             CDTPPRATCPLA, CDFAPRATCPLA, CDPRATCONSPL, SUM(QTCONSBRUTAUNR) AS QTCONSBRUTAUNR, SGCONSUNIADIC
          FROM
             SERVICO S,
             CONSPLAN C
          WHERE
             (CDFILICPLA = P_FILIAL)     AND
             (NRCONSPLAN = P_NRCONSPLAN) AND
             ((INSTR(P_SERVICOS, CDSERVCPLA) > 0) OR (V_TODOS_SERV = 'T')) AND
             (IDCONSPLAN IN (V_IDCARDAPIO,V_IDCONSFIXO,V_IDCONSPERC)) AND
             (S.CDFILIAL = C.CDFILICPLA) AND
             (S.CDSERVFILI = C.CDSERVCPLA) AND
             (S.IDUTRETIPRAT = 'S')
          group by  CDFILICPLA, CDSERVCPLA, DTCONSPLAN, IDCONSPLAN, CDPRODCPLA,
             DTLIMITECPLA, HRLIMITECPLA, CDFAPRATCPLA, CDPRATCONSPL, CDTPPRATCPLA, SGCONSUNIADIC
          ORDER BY
             CDFILICPLA, CDSERVCPLA, DTCONSPLAN) LOOP
         IF V_SERV_ATUAL <> C_PREVRETI.CDSERVCPLA THEN
            V_SERV_ATUAL := C_PREVRETI.CDSERVCPLA;
            SELECT
               NVL(MAX(PR.NRPRPREVRETI), 0) INTO V_NRPRPREVRETI
            FROM
               PREVRETI PR
            WHERE
               (PR.CDFILIAL     = C_PREVRETI.CDFILICPLA)   AND
               (PR.CDSERVFILI   = C_PREVRETI.CDSERVCPLA)   AND
               (PR.DTPREVRETI   = C_PREVRETI.DTCONSPLAN)   AND
               (PR.IDPREVRETI   = C_PREVRETI.IDCONSPLAN)   AND
               (PR.NRREQUEXTR   = '0000000000');
         END IF;
         V_NRPRPREVRETI := LPAD(TO_CHAR(TO_NUMBER(V_NRPRPREVRETI) + 1), 4, 0);
         INSERT INTO PREVRETI
            (CDFILIAL,     CDSERVFILI,   DTPREVRETI,   NRREQUEXTR,
             IDPREVRETI,   CDPRODUTO,    DTLIMITERETI, HRLIMITERETI,
             QTPREVRETI,   QTPRERETT1,   QTPRERETT2,   QTPRERETT3,
             QTPRERETT4,   QTPREVRETISA, QTPRERETT1SA, QTPRERETT2SA,
             QTPRERETT3SA, QTPRERETT4SA, CDTIPOPRAT,   CDFILAUXPRAT,
             CDPRATO,      IDPREVTPPRAT, NRPRPREVRETI, CDOPERADOR, QTPREVBRUTAUNR, SGPREVUNIADIC)
         VALUES(
          C_PREVRETI.CDFILICPLA, C_PREVRETI.CDSERVCPLA, C_PREVRETI.DTCONSPLAN,
          '0000000000', C_PREVRETI.IDCONSPLAN, C_PREVRETI.CDPRODCPLA,
          C_PREVRETI.DTLIMITECPLA, C_PREVRETI.HRLIMITECPLA, C_PREVRETI.QTCONSPLAN,
          C_PREVRETI.QTCONSPLANT1, C_PREVRETI.QTCONSPLANT2, C_PREVRETI.QTCONSPLANT3,
          C_PREVRETI.QTCONSPLANT4, C_PREVRETI.QTCONSPLANSA, C_PREVRETI.QTCONPLAT1SA,
          C_PREVRETI.QTCONPLAT2SA, C_PREVRETI.QTCONPLAT3SA, C_PREVRETI.QTCONPLAT4SA,
          C_PREVRETI.CDTPPRATCPLA, C_PREVRETI.CDFAPRATCPLA, C_PREVRETI.CDPRATCONSPL,
          'S', V_NRPRPREVRETI, P_CDOPERADOR, C_PREVRETI.QTCONSBRUTAUNR, C_PREVRETI.SGCONSUNIADIC);
      END LOOP;
      INSERT INTO PREVRETI
         (CDFILIAL,     CDSERVFILI,   DTPREVRETI,   NRREQUEXTR,
          IDPREVRETI,   CDPRODUTO,    DTLIMITERETI, HRLIMITERETI,
          QTPREVRETI,   QTPRERETT1,   QTPRERETT2,   QTPRERETT3,
          QTPRERETT4,   QTPREVRETISA, QTPRERETT1SA, QTPRERETT2SA,
          QTPRERETT3SA, QTPRERETT4SA, IDPREVTPPRAT, CDOPERADOR, QTPREVBRUTAUNR)
      (SELECT
          CDFILICPLA, CDSERVCPLA, DTCONSPLAN,
          '0000000000', IDCONSPLAN, CDPRODCPLA,
          DTLIMITECPLA, HRLIMITECPLA,
          DECODE(P.VRARRED,
                 0, SUM(QTCONSPLANSA),
                 NULL, SUM(QTCONSPLANSA),
                 DECODE(MOD(SUM(QTCONSPLANSA), P.VRARRED),
                        0, SUM(QTCONSPLANSA),
                        (FLOOR(SUM(QTCONSPLANSA) / P.VRARRED) + 1) * P.VRARRED)
          ) QTCONSPLAN, /*SUM(QTCONSPLAN),*/
          SUM(QTCONSPLANT1), SUM(QTCONSPLANT2), SUM(QTCONSPLANT3),
          SUM(QTCONSPLANT4), SUM(QTCONSPLANSA), SUM(QTCONPLAT1SA),
          SUM(QTCONPLAT2SA), SUM(QTCONPLAT3SA), SUM(QTCONPLAT4SA),
          'N', P_CDOPERADOR, SUM(QTCONSBRUTAUNR)
       FROM
          SERVICO S,
          CONSPLAN C,
          (SELECT
              CDPRODUTO,
              DECODE(P_IDARREDOND,
                     'S', VRARREDREQ,
                     'C', VRARREDCMP,
                     0) AS VRARRED
           FROM PRODUTO) P
       WHERE
          (CDFILICPLA = P_FILIAL)     AND
          (NRCONSPLAN = P_NRCONSPLAN) AND
          (IDCONSPLAN IN (V_IDCARDAPIO,V_IDCONSFIXO,V_IDCONSPERC)) AND
          ((INSTR(P_SERVICOS, CDSERVCPLA) > 0) OR (V_TODOS_SERV = 'T')) AND
          (S.CDFILIAL = C.CDFILICPLA) AND
          (S.CDSERVFILI = C.CDSERVCPLA) AND
          (S.IDUTRETIPRAT <> 'S') AND
          (C.CDPRODCPLA = P.CDPRODUTO)
       GROUP BY
          CDFILICPLA, CDSERVCPLA, DTCONSPLAN, IDCONSPLAN, CDPRODCPLA,
          DTLIMITECPLA, HRLIMITECPLA, P.VRARRED);
   END IF;
   /*
   DELETE FROM CONSPLANSE
   WHERE
      (CDFILICPLASE = P_FILIAL) AND
      (NRCONSPLANSE = P_NRCONSPLAN);
   */
   COMMIT;
END;
/