CREATE OR REPLACE PROCEDURE EXPORTA_CARDAPIO_COMPLETA
(
P_NRLISTAORIDEST IN VARCHAR2, /* Numero da lista no formato FilialOri|ServOri|CnfOriLista_FilialDest|ServDest */
P_NRLISTAPERIODO IN VARCHAR2, /* Numero da lista no formato DtOri(YYYYMMDD)_DtDest(YYYYMMDD) */
P_MANTEMCONFIG IN VARCHAR2,
P_IDCOPCOMENS  IN VARCHAR2,
P_IDSOBREPOR   IN VARCHAR2,
P_CDOPERADOR   IN VARCHAR2,
P_MODOPROC     IN VARCHAR2,
P_NRLOGINTCARD OUT VARCHAR2,
P_IDGERASUBST  IN VARCHAR DEFAULT 'S'
)
AS
   V_TIPOCARDAPIO VARCHAR2(1);
   V_IDUTRPADCA VARCHAR2(1);
   V_IDMANCORCARDALT VARCHAR2(1);

   /*Variavel de controle para nao efetuar as pesquisas de filial mais de uma vez*/
   V_CDFILIAL_ATUAL VARCHAR2(4);

   V_IDCARDIAUTIL VARCHAR2(1);
   V_IDCUSPADCAR VARCHAR2(1);

   V_IDTIPOSERV VARCHAR2(1);

   V_IDCARDDESCENTR VARCHAR2(1);
   V_IDSERVTRAN VARCHAR2(1);

   /*Variavel de controle para nao efetuar as pesquisas do  filial mais de uma vez*/
   V_MES_ATUAL DATE;

   V_VRCUSTPADR FLOAT;

   V_NRLOGINTCARD VARCHAR2(20);

   V_AUXESP VARCHAR2(1);

   V_PRATOESPE VARCHAR2(1);

   V_DTLIBECARDMATR DATE;
   V_DTAPROCARDFILI DATE;
   V_DTAPROCARDMATR DATE;
   V_DTLIMAPROVFILI DATE;

   V_MAXNRSEQDEST NUMBER;
   V_MAXNRORDDEST NUMBER;
   V_ORD NUMBER;

   V_EXICNFCAMES NUMBER;

   V_CONTFERIFILI NUMBER;

   V_CONTFERICARD NUMBER;

   V_DIASEMANA NUMBER;

   V_CONTCARDDEST VARCHAR2(20);

   V_CDDIETDEST VARCHAR2(4);

   V_NRCARDAPIO VARCHAR2(20);

   V_CDSERVDEST VARCHAR2(5);
   V_CDFILIDEST VARCHAR2(4);

   C_DATAS_DTORIGEM   DATE;
   C_COMENSSERV_QTCOM FLOAT(126);
   C_COMENSSERV_QTCLI FLOAT(126);
   C_COMENSSERV_QTFUN FLOAT(126);
   C_COMENSSERV_QTDOA FLOAT(126);
   C_TURNOSERV_QTT1   FLOAT(126);
   C_TURNOSERV_QTT2   FLOAT(126);
   C_TURNOSERV_QTT3   FLOAT(126);
   C_TURNOSERV_QTT4   FLOAT(126);

   CURSOR C_COMENSSERV IS
    SELECT NVL(DECODE(V_DIASEMANA,2,QTCOMESTISEG,3,QTCOMESTITER,4,QTCOMESTIQUA,5,QTCOMESTIQUI,6,QTCOMESTISEX,7,QTCOMESTISAB,1,QTCOMESTIDOM,8,QTCOMESTIFER),0),
           NVL(DECODE(V_DIASEMANA,2,QTCLIESTISEG,3,QTCLIESTITER,4,QTCLIESTIQUA,5,QTCLIESTIQUI,6,QTCLIESTISEX,7,QTCLIESTISAB,1,QTCLIESTIDOM,8,QTCLIESTIFER),0),
           NVL(DECODE(V_DIASEMANA,2,QTFUNESTISEG,3,QTFUNESTITER,4,QTFUNESTIQUA,5,QTFUNESTIQUI,6,QTFUNESTISEX,7,QTFUNESTISAB,1,QTFUNESTIDOM,8,QTFUNESTIFER),0),
           NVL(DECODE(V_DIASEMANA,2,QTDOAESTISEG,3,QTDOAESTITER,4,QTDOAESTIQUA,5,QTDOAESTIQUI,6,QTDOAESTISEX,7,QTDOAESTISAB,1,QTDOAESTIDOM,8,QTDOAESTIFER),0)
    FROM   SERVICO
    WHERE  CDFILIAL = V_CDFILIDEST AND
           CDSERVFILI = V_CDSERVDEST;

   CURSOR C_TURNOSERV IS
   SELECT SUM(T1),SUM(T2),SUM(T3),SUM(T4)
   FROM   (SELECT NVL(DECODE(V_DIASEMANA,2,QTCOMESTTSEG,3,QTCOMESTTTER,4,QTCOMESTTQUA,5,QTCOMESTTQUI,6,QTCOMESTTSEX,7,QTCOMESTTSAB,1,QTCOMESTTDOM,8,QTCOMESTTFER), 0) T1,
                0 T2, 0 T3, 0 T4
         FROM   TURNOSERV
         WHERE  CDFILIAL = V_CDFILIDEST AND
                CDSERVFILI = V_CDSERVDEST AND
                CDTURNO = '1'
         UNION
         SELECT 0 T1,
                NVL(DECODE(V_DIASEMANA,2,QTCOMESTTSEG,3,QTCOMESTTTER,4,QTCOMESTTQUA,5,QTCOMESTTQUI,6,QTCOMESTTSEX,7,QTCOMESTTSAB,1,QTCOMESTTDOM,8,QTCOMESTTFER), 0) T2,
                0 T3, 0 T4
         FROM   TURNOSERV
         WHERE  CDFILIAL = V_CDFILIDEST AND
                CDSERVFILI = V_CDSERVDEST AND
                CDTURNO = '2'
         UNION
         SELECT 0 T1, 0 T2,
                NVL(DECODE(V_DIASEMANA,2,QTCOMESTTSEG,3,QTCOMESTTTER,4,QTCOMESTTQUA,5,QTCOMESTTQUI,6,QTCOMESTTSEX,7,QTCOMESTTSAB,1,QTCOMESTTDOM,8,QTCOMESTTFER), 0) T3,
                0 T4
         FROM   TURNOSERV
         WHERE  CDFILIAL = V_CDFILIDEST AND
                CDSERVFILI = V_CDSERVDEST AND
                CDTURNO = '3'
         UNION
         SELECT 0 T1, 0 T2, 0 T3,
                NVL(DECODE(V_DIASEMANA,2,QTCOMESTTSEG,3,QTCOMESTTTER,4,QTCOMESTTQUA,5,QTCOMESTTQUI,6,QTCOMESTTSEX,7,QTCOMESTTSAB,1,QTCOMESTTDOM,8,QTCOMESTTFER), 0) T4
         FROM   TURNOSERV
         WHERE  CDFILIAL = V_CDFILIDEST AND
                CDSERVFILI = V_CDSERVDEST AND
                CDTURNO = '4');

   CURSOR C_DIETSERV IS
   SELECT NVL(DECODE(V_DIASEMANA,2,QTCOMDIETSEG,3,QTCOMDIETTER,4,QTCOMDIETQUA,5,QTCOMDIETQUI,6,QTCOMDIETSEX,7,QTCOMDIETSAB,1,QTCOMDIETDOM,8,QTCOMDIETFER),0),
         NVL(DECODE(V_DIASEMANA,2,QTCLIDIETSEG,3,QTCLIDIETTER,4,QTCLIDIETQUA,5,QTCLIDIETQUI,6,QTCLIDIETSEX,7,QTCLIDIETSAB,1,QTCLIDIETDOM,8,QTCLIDIETFER),0),
         NVL(DECODE(V_DIASEMANA,2,QTFUNDIETSEG,3,QTFUNDIETTER,4,QTFUNDIETQUA,5,QTFUNDIETQUI,6,QTFUNDIETSEX,7,QTFUNDIETSAB,1,QTFUNDIETDOM,8,QTFUNDIETFER),0),
         NVL(DECODE(V_DIASEMANA,2,QTDOADIETSEG,3,QTDOADIETTER,4,QTDOADIETQUA,5,QTDOADIETQUI,6,QTDOADIETSEX,7,QTDOADIETSAB,1,QTDOADIETDOM,8,QTDOADIETFER),0)
   FROM   DIETSERV S, DIETPADR P
   WHERE  CDFILIAL     = V_CDFILIDEST AND
         CDSERVFILI   = V_CDSERVDEST AND
         S.CDDIETPAD  = V_CDDIETDEST AND
         P.CDDIETPAD  = S.CDDIETPAD  AND
         P.IDTIPODIET = 'S';

   V_EXISCARDACLI NUMBER;

   V_PTOENTREGA NUMBER;

   V_CDFILAUXDEST VARCHAR2(4);

   V_CONTPRAT NUMBER;

   V_EXISTPRAT NUMBER;

   V_CONTPRODRECE NUMBER;

   V_TIPORECE VARCHAR2(1);

   V_CDNVPRODUTO VARCHAR2(1);

   V_PRODINATIVO BOOLEAN := FALSE;

   V_CDARVPRODINAT VARCHAR2(20);

   V_VALOR_A_MAIS FLOAT;

   V_CDPRATODEL VARCHAR2(10);

   V_CDFILAUXPRATDEL VARCHAR2(4);

   V_NRORDCNFMES NUMBER;

   V_NRSEQCNFMES NUMBER;

   V_EXISTDIST NUMBER;

   V_NRCENARIOCARD NUMBER;

   V_IDATIVO VARCHAR2(1);

   V_IDTIPORESTRICAO VARCHAR2(30);

   V_VRPECONSCARD FLOAT(126);

   V_NRGRUPOPRATOSERV NUMBER;

   V_EXIST_CARDAMES NUMBER;

   V_NRVIGENCIA NUMBER;
BEGIN
   /*Parametrizacao geral do cardapio (Valido para todas as filiais)*/
   BEGIN
      SELECT IDUTCARDPADR, IDUTRPADCA, IDMANCORCARDALT
      INTO   V_TIPOCARDAPIO, V_IDUTRPADCA, V_IDMANCORCARDALT
      FROM   PARAMGERAL
      WHERE  CDPARGERAL = '0001';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_TIPOCARDAPIO := 'N';
         V_IDUTRPADCA := 'N';
         V_IDMANCORCARDALT := 'N';
   END;

   V_CDFILIAL_ATUAL := NULL;
   /* ===== ETAPA 1 - Percorre cada servico de destino (e sua origem) */
   FOR CUR_ORIDEST IN
   (
      SELECT CASE WHEN INSTR(L.ORIGEM, '|') > 0 THEN 
                   SUBSTR(L.ORIGEM, 1, INSTR(L.ORIGEM, '|') - 1)
                   ELSE NULL
             END AS CDFILIORIG,       
             CASE WHEN (INSTR(L.ORIGEM, '|') > 0 AND INSTR(L.ORIGEM, '|', 1, 2) > 0) THEN 
                  SUBSTR(L.ORIGEM, INSTR(L.ORIGEM, '|') + 1, INSTR(L.ORIGEM, '|', 1, 2) - INSTR(L.ORIGEM, '|') - 1)
                  ELSE NULL
             END AS CDSERVORIG,
             CASE WHEN INSTR(L.ORIGEM, '|', 2) > 0 THEN 
                   SUBSTR(L.ORIGEM, INSTR(L.ORIGEM, '|', 1, 2) + 1, LENGTH(L.ORIGEM))
                  ELSE NULL
             END AS NRLISTACNF,
             CASE WHEN INSTR(L.DESTINO, '|') > 0 THEN 
                   SUBSTR(L.DESTINO, 1, INSTR(L.DESTINO, '|') - 1)
                   ELSE NULL
             END AS CDFILIDEST,
             CASE WHEN INSTR(L.DESTINO, '|') > 0 THEN 
                   SUBSTR(L.DESTINO, INSTR(L.DESTINO, '|') + 1, LENGTH(L.DESTINO))
                  ELSE NULL
             END AS CDSERVDEST
        FROM
      (
      SELECT CASE WHEN INSTR(CDLISTA, '_') > 0 THEN 
                   SUBSTR(CDLISTA, 1, INSTR(CDLISTA, '_') - 1)
                   ELSE NULL
             END AS ORIGEM,

             CASE WHEN INSTR(CDLISTA, '_') > 0 THEN 
                     SUBSTR(CDLISTA, INSTR(CDLISTA, '_') + 1, LENGTH(CDLISTA))
                  ELSE NULL
             END AS DESTINO
           FROM LISTA
          WHERE NRLISTA = P_NRLISTAORIDEST
      ) L
      ORDER BY CDFILIDEST
   ) LOOP
      /*Garantir que os dados da lista de origem e destino sejam validos*/      
      IF CUR_ORIDEST.CDFILIORIG IS NULL OR
         CUR_ORIDEST.CDSERVORIG IS NULL OR
         CUR_ORIDEST.NRLISTACNF IS NULL OR
         CUR_ORIDEST.CDFILIDEST IS NULL OR
         CUR_ORIDEST.CDSERVDEST IS NULL THEN
         CONTINUE;
      END IF;

      V_CDSERVDEST := CUR_ORIDEST.CDSERVDEST;
      V_CDFILIDEST := CUR_ORIDEST.CDFILIDEST;

      /*Busca as informacoes por filial apenas se for a primeira filial ou quando muda a filial*/
      IF V_CDFILIAL_ATUAL IS NULL OR 
         V_CDFILIAL_ATUAL <> CUR_ORIDEST.CDFILIDEST THEN
         /* Verifica se o parametro "Verifica custo padrao na Montagem do Cardapio" esta marcado */
         SELECT MAX(IDCUSPADCAR), NVL(MAX(IDCARDIAUTIL), 'N')
           INTO V_IDCUSPADCAR, V_IDCARDIAUTIL
           FROM PARAMPLANEJA
          WHERE CDFILIAL = CUR_ORIDEST.CDFILIDEST;

         V_CDFILIAL_ATUAL := CUR_ORIDEST.CDFILIDEST;
      END IF;

      /*Tipo de servico de origem*/
      SELECT MAX(IDTIPOSERV)
        INTO V_IDTIPOSERV
        FROM SERVICO
       WHERE CDFILIAL   = CUR_ORIDEST.CDFILIORIG AND
             CDSERVFILI = CUR_ORIDEST.CDSERVORIG;

      /*Dados do servico de destino*/
      SELECT NVL(MAX(IDCARDDESCENTR), 'N'), NVL(MAX(IDSERVTRAN),'N')
      INTO   V_IDCARDDESCENTR, V_IDSERVTRAN
      FROM   SERVICO
      WHERE  CDFILIAL   = CUR_ORIDEST.CDFILIDEST AND
             CDSERVFILI = CUR_ORIDEST.CDSERVDEST;

      V_MES_ATUAL := NULL;
      /* ===== ETAPA 2 - Percorre o periodo de origem e destino */
      FOR CUR_PERIODO IN
      (
         SELECT TO_DATE(ORIGEM, 'YYYYMMDD') AS DTORIG,
                TO_DATE(DESTINO, 'YYYYMMDD') AS DTDEST,
                TRUNC(TO_DATE(ORIGEM, 'YYYYMMDD'), 'MM') AS DTMESORIG,
                TRUNC(TO_DATE(DESTINO, 'YYYYMMDD'), 'MM') AS DTMESDEST
           FROM
         (
            SELECT CASE WHEN INSTR(CDLISTA, '_') > 0 THEN 
                         SUBSTR(CDLISTA, 1, INSTR(CDLISTA, '_') - 1)
                         ELSE NULL
                   END AS ORIGEM,

                   CASE WHEN INSTR(CDLISTA, '_') > 0 THEN 
                           SUBSTR(CDLISTA, INSTR(CDLISTA, '_') + 1, LENGTH(CDLISTA))
                        ELSE NULL
                   END AS DESTINO
                 FROM LISTA
                WHERE NRLISTA = P_NRLISTAPERIODO
         ) L
      ) LOOP
         /*Rotinas rodadas para o cardapio do mes como um todo*/
         IF V_MES_ATUAL IS NULL THEN

            /*BUSCA O NRO DO CENARIO DE ORIGEM DA FILIAL E SERVICO DE ORIGEM, SE ESTA PREENCHIDO ENTAO VAI FAZER AS VALIDACOES DO OTIMIZADOR*/
            SELECT MAX(NRCENARIOCARD)
              INTO V_NRCENARIOCARD
              FROM CARDAMES
             WHERE CDFILIAL = CUR_ORIDEST.CDFILIORIG AND
                   CDSERVFILI = CUR_ORIDEST.CDSERVORIG AND
                   DTMESCARDAPIO = CUR_PERIODO.DTMESORIG;
            /*BUSCA O TIPO DE RESTRICAO USADO NO CENARIO DE ORIGEM (CONTRATO OU PLANEJADO)*/
            IF V_NRCENARIOCARD IS NOT NULL THEN
               SELECT IDTIPORESTRICAO
                 INTO V_IDTIPORESTRICAO
                 FROM CENARIOCARD 
                WHERE NRCENARIOCARD = V_NRCENARIOCARD;
            END IF;
            /* Verifica se existe custo padrao para o servico de destino */
            IF V_IDCUSPADCAR = 'S' THEN               
               SELECT MAX(VRCUSTPADR) AS VRCUSTPADR
                 INTO V_VRCUSTPADR
                 FROM CUSTPADR
                WHERE (CDFILIAL   = CUR_ORIDEST.CDFILIDEST) 
                  AND (CDSERVFILI = CUR_ORIDEST.CDSERVDEST)
                  AND (DTREFCUSPADR = CUR_PERIODO.DTMESDEST);    

               IF V_VRCUSTPADR IS NULL THEN
                  IF V_NRLOGINTCARD IS NULL THEN
                     NOVO_CODIGO('LOGINTCARD_ASSISTENTE', V_NRLOGINTCARD, 1);
                     V_NRLOGINTCARD := 'A'|| SUBSTR(V_NRLOGINTCARD,12,9);
                  END IF;
                  GRAVA_LOGINTCARD(V_NRLOGINTCARD, CUR_ORIDEST.CDFILIORIG, CUR_ORIDEST.CDFILIDEST, CUR_ORIDEST.CDSERVDEST, CUR_PERIODO.DTMESDEST, ' ', ' ', ' ', '4');
               END IF;       
            END IF;

            /* Cardapio DESCENTRALIZADO*/
            IF (V_TIPOCARDAPIO = 'D') AND (V_IDCARDDESCENTR = 'S') THEN
               /*Modo Matriz*/
               IF (P_MODOPROC = 'M') THEN
                  V_AUXESP := NULL;
                  BEGIN
                     SELECT 'S'
                       INTO V_AUXESP
                       FROM CTRLAPROVCARD
                      WHERE CDFILIAL = CUR_ORIDEST.CDFILIDEST
                        AND CDSERVFILI = CUR_ORIDEST.CDSERVDEST
                        AND DTMESCARDAPROV = CUR_PERIODO.DTMESDEST
                        AND DTLIBECARDMATR IS NOT NULL;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_PRATOESPE := 'N';
                  END;
               END IF;

               /*Modo Filial*/
               IF (P_MODOPROC = 'F') THEN
                  /*Verifica se o servico ja esta LIBERADO pela matriz, APROVADO pela filial,
                    APROVADO pela matriz ou se a data de LIMITE de aprovacao pela filial expirou*/
                  BEGIN
                     SELECT CTRL.DTLIBECARDMATR, CTRL.DTAPROCARDFILI, CTRL.DTAPROCARDMATR,
                            CALE.DTLIMAPROVFILI + NVL(QTDIAPRORAPROV , 0) AS DTLIMAPROVFILI
                     INTO   V_DTLIBECARDMATR, V_DTAPROCARDFILI, V_DTAPROCARDMATR, V_DTLIMAPROVFILI
                     FROM   CTRLAPROVCARD CTRL, CALENCARD CALE
                     WHERE  CTRL.CDFILIAL       = CUR_ORIDEST.CDFILIDEST AND
                            CTRL.CDSERVFILI     = CUR_ORIDEST.CDSERVDEST AND
                            CTRL.DTMESCARDAPROV = CUR_PERIODO.DTMESDEST  AND
                            CTRL.CDFILIAL       = CALE.CDFILIAL AND
                            CTRL.DTMESCARDAPROV = CALE.DTMESCALENCARD;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_DTLIBECARDMATR := NULL;
                        V_DTAPROCARDFILI := NULL;
                        V_DTAPROCARDMATR := NULL;
                        V_DTLIMAPROVFILI := NULL;
                  END;
                  /*Cardapio NAO liberado pela MATRIZ*/
                  IF V_DTLIBECARDMATR IS NULL THEN
                     RETURN;
                  END IF;
                  /*Cardapio Ja aprovado pela FILIAL*/
                  IF NOT V_DTAPROCARDFILI IS NULL THEN
                     RETURN;
                  END IF;
                  /*Cardapio Ja aprovado pela MATRIZ*/
                  IF NOT V_DTAPROCARDMATR IS NULL THEN
                     RETURN;
                  END IF;
                  /*Data limite de aprovacao pela FILIAL expirada*/
                  IF TRUNC(SYSDATE) > TRUNC(V_DTLIMAPROVFILI) THEN
                     RETURN;
                  END IF;
               END IF;
            END IF;


            /* ===== ETAPA 3 - Ajuste da configuracao caso NAO mantenha configuracao do destino */
            V_MAXNRSEQDEST := 0;
            V_MAXNRORDDEST := 0;
            
            /*Apaga compcard do mes que n�o aponta para colunas existentes no cardapio de destino*/
            DELETE FROM COMPCARD A
               WHERE EXISTS (SELECT 1 FROM CARDADIA B WHERE  B.CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                                       B.CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND
                                       B.DTMESCARD  = CUR_PERIODO.DTMESDEST AND
                                       A.CDFILIAL   = B.CDFILIAL AND
                                       A.NRCARDAPIO = B.NRCARDAPIO)
                 AND NOT EXISTS (SELECT 1 FROM CNFCAMES B
                                 WHERE B.CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                                       B.CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND
                                       B.DTREFECNFMES = CUR_PERIODO.DTMESDEST AND
                                       A.CDFILIAL = B.CDFILIAL AND
                                       A.NRSEQPRATCAR = B.NRSEQCNFMES);
            IF P_MANTEMCONFIG <> 'S' THEN               

               IF P_IDSOBREPOR = 'S' THEN
                  DELETE FROM CNFCAMES
                   WHERE CDFILIAL     = CUR_ORIDEST.CDFILIDEST AND
                         CDSERVFILI   = CUR_ORIDEST.CDSERVDEST AND
                         DTREFECNFMES = CUR_PERIODO.DTMESDEST;
               ELSE                  
                  /* Rotina para reordenar o sequencial da CNFCAMES e COMPCARD a partir de 1 */
                  V_ORD := 0;

                  FOR C_SEQ IN
                    (SELECT NRSEQCNFMES, CDTIPOPRAT
                     FROM   CNFCAMES
                     WHERE  CDFILIAL     = CUR_ORIDEST.CDFILIDEST AND
                            CDSERVFILI   = CUR_ORIDEST.CDSERVDEST AND
                            DTREFECNFMES = CUR_PERIODO.DTMESDEST
                     ORDER BY NRORDCNFMES)
                  LOOP
                    V_ORD := V_ORD + 1;

                    UPDATE CNFCAMES
                    SET    NRORDCNFMES = V_ORD,
                           NRSEQCNFMES = -1 * V_ORD
                    WHERE  CDFILIAL     = CUR_ORIDEST.CDFILIDEST AND
                           CDSERVFILI   = CUR_ORIDEST.CDSERVDEST AND
                           DTREFECNFMES = CUR_PERIODO.DTMESDEST  AND
                           NRSEQCNFMES  = C_SEQ.NRSEQCNFMES;

                    UPDATE COMPCARD A
                    SET    NRSEQPRATCAR = -1 * V_ORD,
                           NRORDPRATCAR = V_ORD,
                           CDTIPOPRAT = C_SEQ.CDTIPOPRAT
                    WHERE  NRSEQPRATCAR = C_SEQ.NRSEQCNFMES
                      AND EXISTS (SELECT 1 FROM CARDADIA B WHERE  B.CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                                           B.CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND
                                           B.DTMESCARD  = CUR_PERIODO.DTMESDEST AND
                                           A.CDFILIAL   = B.CDFILIAL AND
                                           A.NRCARDAPIO = B.NRCARDAPIO);
                  END LOOP;

                  UPDATE CNFCAMES SET NRSEQCNFMES = ABS(NRSEQCNFMES)
                    WHERE  CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                           CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND
                           DTREFECNFMES = CUR_PERIODO.DTMESDEST;                 

                  UPDATE COMPCARD A
                    SET    NRSEQPRATCAR = ABS(NRSEQPRATCAR)
                    WHERE EXISTS (SELECT 1 FROM CARDADIA B WHERE  B.CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                                           B.CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND
                                           B.DTMESCARD  = CUR_PERIODO.DTMESDEST AND
                                           A.CDFILIAL   = B.CDFILIAL AND
                                           A.NRCARDAPIO = B.NRCARDAPIO);

                  SELECT NVL(MAX(NRSEQCNFMES),0), NVL(MAX(NRORDCNFMES),0)
                    INTO V_MAXNRSEQDEST, V_MAXNRORDDEST
                    FROM CNFCAMES
                    WHERE CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                          CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND
                          DTREFECNFMES = CUR_PERIODO.DTMESDEST;
               END IF;

               INSERT INTO CNFCAMES
               (
               CDFILIAL, CDSERVFILI, NRSEQCNFMES, DTREFECNFMES,
               CDTIPOPRAT, IDOPCCNFMES, IDIMPCNFMES, NRPAGCNFMES,
               VRPERCOMMES, NRORDCNFMES, DSCOMPLTPMES, IDCONCNFMES,
               IDTPPRATSEGM, IDTPPRATTERM, IDTPPRATQUAM, IDTPPRATQUIM,
               IDTPPRATSEXM, IDTPPRATSABM, IDTPPRATDOMM, IDTPPRATFERM,
               IDRESERVAMYMENU
               )
               SELECT 
               CDFILIDEST, CDSERVDEST, V_MAXNRSEQDEST+ROWNUM, DTMESDEST,
               CDTIPOPRAT, IDOPCCNFMES, IDIMPCNFMES, NRPAGCNFMES,
               VRPERCOMMES, V_MAXNRORDDEST + ROWNUM, DSCOMPLTPMES, IDCONCNFMES,
               IDTPPRATSEGM, IDTPPRATTERM, IDTPPRATQUAM, IDTPPRATQUIM,
               IDTPPRATSEXM, IDTPPRATSABM, IDTPPRATDOMM, IDTPPRATFERM,
               IDRESERVAMYMENU
                 FROM   
               (SELECT CUR_ORIDEST.CDFILIDEST CDFILIDEST, CUR_ORIDEST.CDSERVDEST CDSERVDEST,
                       NRSEQCNFMES, CUR_PERIODO.DTMESDEST DTMESDEST,
                       CDTIPOPRAT, IDOPCCNFMES, IDIMPCNFMES, NVL(NRPAGCNFMES,1) NRPAGCNFMES,
                       NVL(VRPERCOMMES,0) VRPERCOMMES, NRORDCNFMES, DSCOMPLTPMES, IDCONCNFMES,
                       IDTPPRATSEGM, IDTPPRATTERM, IDTPPRATQUAM, IDTPPRATQUIM,
                       IDTPPRATSEXM, IDTPPRATSABM, IDTPPRATDOMM, IDTPPRATFERM,
                       IDRESERVAMYMENU
                   FROM CNFCAMES
                        INNER JOIN LISTA L ON L.CDLISTA = LPAD(NRSEQCNFMES, 3, '0')
                   WHERE (CDFILIAL     = CUR_ORIDEST.CDFILIORIG) AND
                         (CDSERVFILI   = CUR_ORIDEST.CDSERVORIG) AND
                         (DTREFECNFMES = CUR_PERIODO.DTMESORIG ) AND
                         (L.NRLISTA    = CUR_ORIDEST.NRLISTACNF)                         
                   ORDER BY NRORDCNFMES);

            END IF;

            /*Verifica se existe configuracao de cardapio para o mes*/
            SELECT COUNT(*) 
              INTO V_EXICNFCAMES
              FROM CNFCAMES
             WHERE CDFILIAL     = CUR_ORIDEST.CDFILIDEST AND
                   CDSERVFILI   = CUR_ORIDEST.CDSERVDEST AND
                   DTREFECNFMES = CUR_PERIODO.DTMESDEST;

            /*Se nao existe insere a configuracao padrao do servico na configuracao do cardapio do mes*/
            IF V_EXICNFCAMES = 0 THEN
              INSERT INTO CNFCAMES 
                (CDFILIAL, CDSERVFILI, NRSEQCNFMES, DTREFECNFMES,
                CDTIPOPRAT, IDOPCCNFMES, IDIMPCNFMES, NRPAGCNFMES,
                VRPERCOMMES, NRORDCNFMES, DSCOMPLTPMES, IDCONCNFMES,
                IDTPPRATSEGM, IDTPPRATTERM, IDTPPRATQUAM, IDTPPRATQUIM,
                IDTPPRATSEXM, IDTPPRATSABM, IDTPPRATDOMM, IDTPPRATFERM,
                IDRESERVAMYMENU)  
              (SELECT 
                CDFILIAL, CDSERVFILI, NRSEQCNFCARD, CUR_PERIODO.DTMESDEST,
                CDTIPOPRAT, IDOPCCNFCARD, IDIMPCNFCARD, NRPAGCNFCARD,
                VRPERCOMCARD, NRORDCNFCARD, DSCOMPLTP, IDCONCNFCARD,
                IDTPPRATSEGP, IDTPPRATTERP, IDTPPRATQUAP, IDTPPRATQUIP,
                IDTPPRATSEXP, IDTPPRATSABP, IDTPPRATDOMP, IDTPPRATFERP,
                IDRESERVAMYMENU
                FROM CONFCARD
              WHERE CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                    CDSERVFILI = CUR_ORIDEST.CDSERVDEST);
            END IF;

            -- Limpa (se existir) as configura��es de valida��o de card�pio do destino
            UPDATE CARDAMES
               SET IDVALIDADO = 'N',
                   CDOPERVALIDACAO = NULL,
                   DTVALIDACAO = NULL
             WHERE CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                   CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND
                   DTMESCARDAPIO = CUR_PERIODO.DTMESDEST;

            /*FIM - ETAPA 3 ===== */

            V_MES_ATUAL := CUR_PERIODO.DTMESDEST;
         END IF;

         SELECT COUNT(*)
           INTO V_CONTFERIFILI
           FROM FERIFILI
          WHERE CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                DTFERIFILI = CUR_PERIODO.DTDEST;

         SELECT COUNT(*)
           INTO V_CONTFERICARD
           FROM FERICARD
          WHERE CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                DTFERICARD = CUR_PERIODO.DTDEST;

         /* Grava o numero correspondente ao dia da semana */
         IF V_CONTFERICARD = 0 THEN
            V_DIASEMANA := TO_CHAR(CUR_PERIODO.DTDEST,'D');
         ELSE
            V_DIASEMANA := 8;
         END IF;

         /* ===== ETAPA 4 - Processamento para cada data do periodo
            Permite inclusao quando: 
            V_IDCARDIAUTIL <> 'S' (NAO BLOQUEIA INCLUSAO EM DIA NAO UTIL) OR
            V_IDCARDIAUTIL = 'S' AND V_CONTFERIFILI = 0 (SE BLOQUEIA, e O DIA E NORMAL) */
         IF (V_IDCARDIAUTIL <> 'S') OR ((V_IDCARDIAUTIL = 'S') AND (V_CONTFERIFILI = 0)) THEN

            FOR C_CARDORIG IN
            (
               SELECT CDFILIAL, CDSERVFILI, DTCARDAPIO, NRCARDAPIO,
                      CDDIETPAD, DTMESCARD, DSCARDAPIO,
                      QTCOMESTICAR, QTCLIESTICAR, QTFUNESTICAR, QTDOAESTICAR,
                      QTESTITURNO1, QTESTITURNO2, QTESTITURNO3, QTESTITURNO4
                 FROM CARDADIA
                WHERE CDFILIAL   = CUR_ORIDEST.CDFILIORIG AND
                      CDSERVFILI = CUR_ORIDEST.CDSERVORIG AND
                      DTCARDAPIO = CUR_PERIODO.DTORIG
                ORDER BY CDFILIAL, CDSERVFILI, DTCARDAPIO, CDDIETPAD
            )
            LOOP
               IF P_IDSOBREPOR = 'S' THEN

                  /* Verifica se existe distribuicao de cardapio transportado no dia*/  
                  BEGIN
                     SELECT COUNT(*) 
                       INTO V_EXISTDIST
                       FROM DISTCARD D
                      WHERE D.CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                            D.NRCARDAPIO = (SELECT NRCARDAPIO 
                                             FROM CARDADIA C 
                                            WHERE C.CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                                                  C.CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND
                                                  C.DTCARDAPIO = CUR_PERIODO.DTDEST);
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_EXISTDIST := 0;
                  END;   

                  IF (V_EXISTDIST > 0) THEN
                     IF V_NRLOGINTCARD IS NULL THEN
                        NOVO_CODIGO('LOGINTCARD_ASSISTENTE', V_NRLOGINTCARD, 1);
                        V_NRLOGINTCARD := 'A'|| SUBSTR(V_NRLOGINTCARD,12,9);
                     END IF;
                     GRAVA_LOGINTCARD(V_NRLOGINTCARD, CUR_ORIDEST.CDFILIORIG, CUR_ORIDEST.CDFILIDEST, CUR_ORIDEST.CDSERVDEST, CUR_PERIODO.DTMESDEST, ' ', ' ', ' ', '9');
                     CONTINUE;
                  END IF;

                  /*Se cardapio DESCENTRALIZADO*/
                  IF (V_TIPOCARDAPIO = 'D') AND (V_IDCARDDESCENTR = 'S') THEN
                     /*Insere na COMPCARDDEL todos os sequenciais de pratos que serao sobrepostos*/
                     IF P_MODOPROC = 'M' THEN
                        /*Verifica se o cardapio esta liberado pela Matriz*/
                        IF V_AUXESP = 'S' THEN
                           IF V_IDMANCORCARDALT = 'S' THEN
                              /*Com o parametro marcado insere todos os pratos que estao no cardapio na compcarddel*/
                              INSERT INTO COMPCARDDEL
                                 (CDFILIAL, CDSERVFILI, DTPRATEXCL, NRSEQPRATEXCL, IDLOCALEXCL, CDPRATO, CDFILAUXPRAT)
                              (SELECT CDFILIAL, CDSERVFILI, DTCARDAPIO, NRSEQPRATCAR, IDLOCALEXCL, CDPRATO, CDFILAUXPRAT
                                 FROM
                                 (SELECT CA.CDFILIAL, CA.CDSERVFILI, CA.DTCARDAPIO,
                                         C.NRSEQPRATCAR, P_MODOPROC AS IDLOCALEXCL, C.CDPRATO, C.CDFILAUXPRAT
                                    FROM COMPCARD C
                                         INNER JOIN CARDADIA CA ON CA.CDFILIAL = C.CDFILIAL AND 
                                                                   CA.NRCARDAPIO = C.NRCARDAPIO
                                   WHERE (C.CDPRATOMAT IS NULL)
                                     AND (C.CDFILAUXPRATMAT IS NULL)
                                     AND (C.IDPRATOESPE = 'N' )
                                     AND CA.CDFILIAL = CUR_ORIDEST.CDFILIDEST
                                     AND CA.CDSERVFILI = CUR_ORIDEST.CDSERVDEST
                                     AND CA.DTCARDAPIO = CUR_PERIODO.DTDEST
                                   UNION
                                  SELECT CA.CDFILIAL, CA.CDSERVFILI, CA.DTCARDAPIO,
                                         C.NRSEQPRATCAR, P_MODOPROC, C.CDPRATOMAT, C.CDFILAUXPRATMAT
                                    FROM COMPCARD C
                                         INNER JOIN CARDADIA CA ON CA.CDFILIAL = C.CDFILIAL AND 
                                                                   CA.NRCARDAPIO = C.NRCARDAPIO
                                   WHERE (C.IDPRATOESPE <> 'N' )
                                     AND CA.CDFILIAL = CUR_ORIDEST.CDFILIDEST
                                     AND CA.CDSERVFILI = CUR_ORIDEST.CDSERVDEST
                                     AND CA.DTCARDAPIO = CUR_PERIODO.DTDEST
                                 ) PRATOS
                                WHERE NOT EXISTS (SELECT CDFILIAL FROM COMPCARDDEL
                                                   WHERE COMPCARDDEL.CDFILIAL = PRATOS.CDFILIAL
                                                     AND COMPCARDDEL.CDSERVFILI = PRATOS.CDSERVFILI
                                                     AND COMPCARDDEL.DTPRATEXCL = PRATOS.DTCARDAPIO
                                                     AND COMPCARDDEL.NRSEQPRATEXCL = PRATOS.NRSEQPRATCAR));
                           ELSE
                              INSERT INTO COMPCARDDEL
                                 (CDFILIAL, CDSERVFILI, DTPRATEXCL, NRSEQPRATEXCL, IDLOCALEXCL, CDPRATO, CDFILAUXPRAT)
                              (SELECT CDFILIAL, CDSERVFILI, DTCARDAPIO, NRSEQPRATCAR, IDLOCALEXCL, CDPRATO, CDFILAUXPRAT
                                 FROM
                                 (SELECT CA.CDFILIAL, CA.CDSERVFILI, CA.DTCARDAPIO,
                                         C.NRSEQPRATCAR, P_MODOPROC AS IDLOCALEXCL, C.CDPRATO, C.CDFILAUXPRAT
                                    FROM COMPCARD C
                                         INNER JOIN CARDADIA CA ON CA.CDFILIAL = C.CDFILIAL AND 
                                                                   CA.NRCARDAPIO = C.NRCARDAPIO
                                   WHERE (C.CDPRATOMAT IS NULL)
                                     AND (C.CDFILAUXPRATMAT IS NULL)
                                     AND (C.IDPRATOESPE = 'N' )
                                     AND CA.CDFILIAL = CUR_ORIDEST.CDFILIDEST
                                     AND CA.CDSERVFILI = CUR_ORIDEST.CDSERVDEST
                                     AND CA.DTCARDAPIO = CUR_PERIODO.DTDEST
                                   UNION
                                  SELECT CA.CDFILIAL, CA.CDSERVFILI, CA.DTCARDAPIO,
                                         C.NRSEQPRATCAR, P_MODOPROC, C.CDPRATOMAT, C.CDFILAUXPRATMAT
                                    FROM COMPCARD C
                                         INNER JOIN CARDADIA CA ON CA.CDFILIAL = C.CDFILIAL AND 
                                                                   CA.NRCARDAPIO = C.NRCARDAPIO
                                   WHERE NOT(C.CDPRATOMAT IS NULL)
                                     AND NOT(C.CDFILAUXPRATMAT IS NULL)
                                     AND CA.CDFILIAL = CUR_ORIDEST.CDFILIDEST
                                     AND CA.CDSERVFILI = CUR_ORIDEST.CDSERVDEST
                                     AND CA.DTCARDAPIO = CUR_PERIODO.DTDEST ) PRATOS
                                WHERE NOT EXISTS (SELECT CDFILIAL FROM COMPCARDDEL
                                                   WHERE COMPCARDDEL.CDFILIAL = PRATOS.CDFILIAL
                                                     AND COMPCARDDEL.CDSERVFILI = PRATOS.CDSERVFILI
                                                     AND COMPCARDDEL.DTPRATEXCL = PRATOS.DTCARDAPIO
                                                     AND COMPCARDDEL.NRSEQPRATEXCL = PRATOS.NRSEQPRATCAR));
                           END IF;
                        END IF;
                     /*modo FILIAL*/
                     ELSE
                        INSERT INTO COMPCARDDEL
                           (CDFILIAL, CDSERVFILI, DTPRATEXCL, NRSEQPRATEXCL, IDLOCALEXCL, CDPRATO, CDFILAUXPRAT)
                        (SELECT CA.CDFILIAL, CA.CDSERVFILI, CA.DTCARDAPIO,
                                C.NRSEQPRATCAR, P_MODOPROC, C.CDPRATO, C.CDFILAUXPRAT
                           FROM COMPCARD C
                                INNER JOIN CARDADIA CA ON CA.CDFILIAL = C.CDFILIAL AND 
                                                          CA.NRCARDAPIO = C.NRCARDAPIO
                          WHERE (C.CDPRATOMAT IS NULL)
                            AND (C.CDFILAUXPRATMAT IS NULL)
                            AND (C.IDPRATOESPE = 'N' )                            
                            AND CA.CDFILIAL = CUR_ORIDEST.CDFILIDEST
                            AND CA.CDSERVFILI = CUR_ORIDEST.CDSERVDEST
                            AND CA.DTCARDAPIO = CUR_PERIODO.DTDEST
                            AND NOT EXISTS (SELECT CD.NRSEQPRATEXCL
                                              FROM COMPCARDDEL CD
                                             WHERE CD.CDFILIAL = CUR_ORIDEST.CDFILIDEST
                                               AND CD.CDSERVFILI = CUR_ORIDEST.CDSERVDEST
                                               AND CD.DTPRATEXCL = CUR_PERIODO.DTDEST
                                               AND CD.CDFILIAL = C.CDFILIAL
                                               AND CD.NRSEQPRATEXCL = C.NRSEQPRATCAR)
                         UNION
                         SELECT CA.CDFILIAL, CA.CDSERVFILI, CA.DTCARDAPIO,
                                C.NRSEQPRATCAR, P_MODOPROC, C.CDPRATOMAT, C.CDFILAUXPRATMAT
                           FROM COMPCARD C
                                INNER JOIN CARDADIA CA ON CA.CDFILIAL = C.CDFILIAL AND 
                                                                   CA.NRCARDAPIO = C.NRCARDAPIO
                          WHERE NOT(C.CDPRATOMAT IS NULL)
                            AND NOT(C.CDFILAUXPRATMAT IS NULL)
                            AND CA.CDFILIAL = CUR_ORIDEST.CDFILIDEST
                            AND CA.CDSERVFILI = CUR_ORIDEST.CDSERVDEST
                            AND CA.DTCARDAPIO = CUR_PERIODO.DTDEST
                            AND NOT EXISTS (SELECT CD.NRSEQPRATEXCL
                                              FROM COMPCARDDEL CD
                                             WHERE CD.CDFILIAL = CUR_ORIDEST.CDFILIDEST
                                               AND CD.CDSERVFILI = CUR_ORIDEST.CDSERVDEST
                                               AND CD.DTPRATEXCL = CUR_PERIODO.DTDEST
                                               AND CD.CDFILIAL = C.CDFILIAL
                                               AND CD.NRSEQPRATEXCL = C.NRSEQPRATCAR));
                     END IF;

                  END IF;



                  DELETE FROM COMPCARD
                   WHERE EXISTS (SELECT 1
                                   FROM CARDADIA
                                  WHERE CDFILIAL = COMPCARD.CDFILIAL AND
                                        NRCARDAPIO = COMPCARD.NRCARDAPIO AND
                                        CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                                        CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND
                                        DTCARDAPIO = CUR_PERIODO.DTDEST);

                  DELETE FROM CARDADIA
                   WHERE CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                         CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND
                         DTCARDAPIO = CUR_PERIODO.DTDEST AND
                         QTCOMEFETCAR = 0 AND  /*NAO EXCLUI O CARDAPIO SE POSSUIR EFETIVOS MESMO QUE SO DO PLANEJAMENTO LANCADOS*/
                         NOT EXISTS (SELECT CDFILIAL, NRCARDAPIO
                                       FROM RETIRADA
                                      WHERE CDFILIAL = CARDADIA.CDFILIAL AND
                                            NRCARDAPIO = CARDADIA.NRCARDAPIO);
               END IF;

               IF V_IDTIPOSERV = 'N' THEN
                  SELECT MAX(NRCARDAPIO), SUM(QTCOMESTICAR)
                  INTO   V_CONTCARDDEST, C_COMENSSERV_QTCOM
                  FROM   CARDADIA
                  WHERE  CDFILIAL   = CUR_ORIDEST.CDFILIDEST AND
                         CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND
                         DTCARDAPIO = CUR_PERIODO.DTDEST;
               END IF;
               V_CDDIETDEST := 'XXXX';

               IF V_IDTIPOSERV = 'D' THEN
                  SELECT MAX(NRCARDAPIO), SUM(QTCOMESTICAR)
                  INTO   V_CONTCARDDEST, C_COMENSSERV_QTCOM
                  FROM   CARDADIA
                  WHERE  CDFILIAL   = CUR_ORIDEST.CDFILIDEST AND
                         CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND
                         DTCARDAPIO = CUR_PERIODO.DTDEST AND
                         CDDIETPAD  = C_CARDORIG.CDDIETPAD;

                  /* Verifica se a dieta de origem esta cadastrada no servico destino. */
                  SELECT NVL(MAX(S.CDDIETPAD),'XXXX')
                  INTO   V_CDDIETDEST
                  FROM   DIETSERV S, DIETPADR P
                  WHERE  CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                         CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND
                         S.CDDIETPAD = C_CARDORIG.CDDIETPAD AND
                         P.CDDIETPAD = S.CDDIETPAD AND
                         P.IDTIPODIET = 'S';
               END IF;

               IF (V_IDTIPOSERV = 'N') OR (V_IDTIPOSERV = 'D' AND (V_CDDIETDEST <> 'XXXX')) THEN

                  /* ===== ETAPA 5 - Inclusao cardadia e copia de comensais */
                  /* Gera novo numero de cardapio se nao existir */
                  IF V_CONTCARDDEST IS NULL THEN
                     V_NRCARDAPIO := NULL;
                     NOVO_CODIGO('CARDAPIO'||V_CDFILIDEST, V_NRCARDAPIO, 1);
                     V_NRCARDAPIO := SUBSTR(V_NRCARDAPIO,11,10);
                  ELSE
                     V_NRCARDAPIO := V_CONTCARDDEST;
                  END IF;
                  
                  IF V_CONTCARDDEST IS NULL OR P_IDSOBREPOR = 'S' THEN /*JA TENHO CARDAPIO MAS QUERO SOBREPOR (SEM EXCLUIR CARDADIA) OU NAO TENHO CARDAPIO ENTAO VAI ATUALIZAR/INSERIR CARDADIA*/
                     IF P_IDCOPCOMENS = 'S' THEN
                     /* Insere o novo cardapio copiando os comensais da origem */
                        C_COMENSSERV_QTCOM := C_CARDORIG.QTCOMESTICAR;
                        C_COMENSSERV_QTCLI := C_CARDORIG.QTCLIESTICAR;
                        C_COMENSSERV_QTFUN := C_CARDORIG.QTFUNESTICAR;
                        C_COMENSSERV_QTDOA := C_CARDORIG.QTDOAESTICAR;
                        C_TURNOSERV_QTT1   := C_CARDORIG.QTESTITURNO1;
                        C_TURNOSERV_QTT2   := C_CARDORIG.QTESTITURNO2;
                        C_TURNOSERV_QTT3   := C_CARDORIG.QTESTITURNO3;
                        C_TURNOSERV_QTT4   := C_CARDORIG.QTESTITURNO4;
                     ELSE
                     /* Insere o novo cardapio com os comensais do servico ou dieta destino */
                        IF V_IDTIPOSERV = 'N' THEN
                           OPEN C_COMENSSERV;
                           FETCH C_COMENSSERV INTO
                              C_COMENSSERV_QTCOM,
                              C_COMENSSERV_QTCLI,
                              C_COMENSSERV_QTFUN,
                              C_COMENSSERV_QTDOA;
                           IF C_COMENSSERV%NOTFOUND THEN
                              C_COMENSSERV_QTCOM := 0;
                              C_COMENSSERV_QTCLI := 0;
                              C_COMENSSERV_QTFUN := 0;
                              C_COMENSSERV_QTDOA := 0;
                           END IF;
                           CLOSE C_COMENSSERV;
                        END IF;
                        IF V_IDTIPOSERV = 'D' THEN
                           OPEN C_DIETSERV;
                           FETCH C_DIETSERV INTO
                              C_COMENSSERV_QTCOM,
                              C_COMENSSERV_QTCLI,
                              C_COMENSSERV_QTFUN,
                              C_COMENSSERV_QTDOA;
                           IF C_DIETSERV%NOTFOUND THEN
                              C_COMENSSERV_QTCOM := 0;
                              C_COMENSSERV_QTCLI := 0;
                              C_COMENSSERV_QTFUN := 0;
                              C_COMENSSERV_QTDOA := 0;
                           END IF;
                           CLOSE C_DIETSERV;
                        END IF;

                        OPEN C_TURNOSERV;
                        FETCH C_TURNOSERV INTO
                           C_TURNOSERV_QTT1,
                           C_TURNOSERV_QTT2,
                           C_TURNOSERV_QTT3,
                           C_TURNOSERV_QTT4;
                        IF C_TURNOSERV%NOTFOUND THEN
                           C_TURNOSERV_QTT1 := 0;
                           C_TURNOSERV_QTT2 := 0;
                           C_TURNOSERV_QTT3 := 0;
                           C_TURNOSERV_QTT4 := 0;
                        END IF;
                        CLOSE C_TURNOSERV;

                     END IF;
                     IF (C_COMENSSERV_QTCOM <> 0) AND (NOT C_COMENSSERV_QTCOM IS NULL) THEN
                        IF V_CONTCARDDEST IS NULL THEN  /*SE NAO EXISTIA CARDAPIO VAI INSERIR NA CARDADIA*/
                           INSERT INTO CARDADIA
                            (CDFILIAL, CDSERVFILI, DTCARDAPIO, NRCARDAPIO,
                             CDDIETPAD, DTMESCARD, CDOPERADOR, DSCARDAPIO, DTULTATUACAR,
                             QTCOMESTICAR, QTCLIESTICAR, QTFUNESTICAR, QTDOAESTICAR,
                             QTESTITURNO1, QTESTITURNO2, QTESTITURNO3, QTESTITURNO4)
                           VALUES
                             (V_CDFILIDEST, V_CDSERVDEST, CUR_PERIODO.DTDEST, V_NRCARDAPIO,
                              C_CARDORIG.CDDIETPAD, TRUNC(CUR_PERIODO.DTDEST,'MM'), P_CDOPERADOR, C_CARDORIG.DSCARDAPIO,
                              SYSDATE,
                              C_COMENSSERV_QTCOM, C_COMENSSERV_QTCLI, C_COMENSSERV_QTFUN, C_COMENSSERV_QTDOA,
                              C_TURNOSERV_QTT1, C_TURNOSERV_QTT2, C_TURNOSERV_QTT3, C_TURNOSERV_QTT4);
                        END IF;
                        IF V_CONTCARDDEST IS NOT NULL AND P_IDSOBREPOR = 'S' THEN /*SE O CARDAPIO JA EXISTIA MAS MARQUEI SOBREPOR, ATUALIZA OS COMENSAIS NA MESMA CARDADIA*/
                           UPDATE CARDADIA
                              SET QTCOMESTICAR = C_COMENSSERV_QTCOM,
                                  QTCLIESTICAR = C_COMENSSERV_QTCLI,
                                  QTFUNESTICAR = C_COMENSSERV_QTFUN,
                                  QTDOAESTICAR = C_COMENSSERV_QTDOA,
                                  QTESTITURNO1 = C_TURNOSERV_QTT1,
                                  QTESTITURNO2 = C_TURNOSERV_QTT2,
                                  QTESTITURNO3 = C_TURNOSERV_QTT3,
                                  QTESTITURNO4 = C_TURNOSERV_QTT4
                            WHERE CDFILIAL = V_CDFILIDEST AND
                                  NRCARDAPIO = V_NRCARDAPIO;
                        END IF;
                        /*verifica se servico e transportado e se existe ponto de entrega e insere na cardacli*/
                        IF V_IDSERVTRAN = 'S' THEN
                           SELECT COUNT(*)
                             INTO V_EXISCARDACLI 
                             FROM CARDACLI 
                            WHERE (CDFILIAL   = V_CDFILIDEST)   AND
                                 (NRCARDAPIO = V_NRCARDAPIO);
                           IF V_EXISCARDACLI = 0 THEN
                              /*se copia comensais do servico de origem, entao insere cardacli do origem*/
                              IF P_IDCOPCOMENS = 'S' THEN
                                 /*verifica se todos os pontos de entrega do servico origem e igual ao servico destino*/
                                 SELECT COUNT(*) IGUAIS
                                  INTO V_PTOENTREGA
                                  FROM
                                 (SELECT COUNT(B.CDDESTFILI) COUNT_IGUAIS   
                                   FROM DESTSRVCLI A, CARDACLI B
                                  WHERE (A.CDFILIAL   = V_CDFILIDEST)         AND
                                        (A.CDSERVFILI = V_CDSERVDEST)         AND 
                                        (B.CDFILIAL   = C_CARDORIG.CDFILIAL)   AND
                                        (B.NRCARDAPIO = C_CARDORIG.NRCARDAPIO)          AND
                                        (A.CDDESTFILI =  B.CDDESTFILI)         AND
                                        (A.CDCLIENTE  = B.CDCLIENTE))IGUAIS,

                                   (SELECT COUNT(CDDESTFILI) COUNT_ORIGEM
                                    FROM CARDACLI 
                                  WHERE (CDFILIAL   = C_CARDORIG.CDFILIAL)   AND
                                        (NRCARDAPIO = C_CARDORIG.NRCARDAPIO)) ORIGEM,

                                   (SELECT COUNT(CDDESTFILI) COUNT_DESTINO
                                      FROM DESTSRVCLI 
                                     WHERE (CDFILIAL   = V_CDFILIDEST)   AND
                                           (CDSERVFILI = V_CDSERVDEST)) DESTINO

                                 WHERE IGUAIS.COUNT_IGUAIS = ORIGEM.COUNT_ORIGEM AND
                                       ORIGEM.COUNT_ORIGEM = DESTINO.COUNT_DESTINO;
                                 /*se pontos de entrega forem iguais importa a cardacli para o servico destino*/       
                                 IF V_PTOENTREGA > 0 THEN
                                    INSERT INTO CARDACLI (CDFILIAL, NRCARDAPIO, CDCLIENTE, CDDESTFILI, QTCOMESTICLI)
                                          (SELECT V_CDFILIDEST,
                                                  V_NRCARDAPIO,
                                                     CDCLIENTE,
                                                    CDDESTFILI,
                                                  QTCOMESTICLI
                                            FROM CARDACLI
                                           WHERE (CDFILIAL   = C_CARDORIG.CDFILIAL)   AND
                                                 (NRCARDAPIO = C_CARDORIG.NRCARDAPIO));  
                                 ELSE
                                    IF V_NRLOGINTCARD IS NULL THEN
                                       NOVO_CODIGO('LOGINTCARD_ASSISTENTE', V_NRLOGINTCARD, 1);
                                       V_NRLOGINTCARD := 'A'|| SUBSTR(V_NRLOGINTCARD,12,9);
                                    END IF;
                                    /* 6o Caso: Pontos de entrega para o servico nao foi importado*/
                                    GRAVA_LOGINTCARD(V_NRLOGINTCARD, CUR_ORIDEST.CDFILIORIG, V_CDFILIDEST, V_CDSERVDEST, CUR_PERIODO.DTDEST, ' ', ' ', ' ',  '6');                                 
                                 END IF;

                              ELSE /*insere na cardacli os pontos de entrega do servico de destino*/
                                 INSERT INTO CARDACLI (CDFILIAL, NRCARDAPIO, CDCLIENTE, CDDESTFILI, QTCOMESTICLI)
                                 (SELECT V_CDFILIDEST,
                                         V_NRCARDAPIO,
                                         CDCLIENTE,
                                         CDDESTFILI,
                                         DECODE(NVL(V_DIASEMANA, '8'),
                                                     '1',QTCOESTCSDOM,
                                                     '2',QTCOESTCSSEG,
                                                     '3',QTCOESTCSTER,
                                                     '4',QTCOESTCSQUA,
                                                     '5',QTCOESTCSQUI,
                                                     '6',QTCOESTCSSEX,
                                                     '7',QTCOESTCSSAB,
                                                         QTCOESTCSFER) QTCOESTCS
                                    FROM DESTSRVCLI
                                   WHERE (CDFILIAL   = V_CDFILIDEST)   AND
                                        (CDSERVFILI = V_CDSERVDEST));  
                              END IF;
                           END IF;   
                        END IF;
                     ELSE
                        IF V_NRLOGINTCARD IS NULL THEN
                           NOVO_CODIGO('LOGINTCARD_ASSISTENTE', V_NRLOGINTCARD, 1);
                           V_NRLOGINTCARD := 'A'|| SUBSTR(V_NRLOGINTCARD,12,9);
                        END IF;
                        /* 1o Caso: Cardapio seria gravado com comensais = 0 */
                        GRAVA_LOGINTCARD(V_NRLOGINTCARD, CUR_ORIDEST.CDFILIORIG, V_CDFILIDEST, V_CDSERVDEST, CUR_PERIODO.DTDEST, ' ', ' ', ' ',  '0');
                     END IF;
                  END IF; /* V_CONTCARDEST = 0 */
                  /*FIM - ETAPA 5 ===== */

                  /* ===== ETAPA 6 - Processamento dos pratos do cardapio de origem */
                  IF (C_COMENSSERV_QTCOM <> 0) AND (NOT C_COMENSSERV_QTCOM IS NULL) THEN
                     FOR C_PRATOSORIG IN
                        (SELECT F.CDFILIAL, C.NRCARDAPIO, F.NRSEQCNFMES NRSEQPRATCAR,
                                NVL(LIS.CDFILAUXPRATDEST, C.CDFILAUXPRAT) AS CDFILAUXPRAT,
                                NVL(NVL(LIS.CDPRATODEST, C.CDPRATO), '�?¿') AS CDPRATO, C.VRPECONSCARD,
                                F.CDTIPOPRAT, F.NRORDCNFMES NRORDPRATCAR, P.NMPRATO
                         FROM   PRATO P, COMPCARD C, CNFCAMES F, LISTA L, (SELECT L.CDFILIAL, L.CDSERVFILI, L.CDFILAUXPRATORIG, L.CDPRATOORIG,
                                                                                    MAX(I.CDFILAUXPRATDEST) AS CDFILAUXPRATDEST, MAX(I.CDPRATODEST) AS CDPRATODEST
                                                                             FROM LISTASUBSPRATO L , ITLISTASUBSPRATO I
                                                                             WHERE L.NRLISTASUBSPRATO = I.NRLISTASUBSPRATO(+)
                                                                               AND L.CDFILIAL   = V_CDFILIDEST
                                                                               AND L.CDSERVFILI = V_CDSERVDEST
                                                                               AND P_IDGERASUBST = 'S'
                                                                             GROUP BY L.CDFILIAL, L.CDSERVFILI, L.CDFILAUXPRATORIG, L.CDPRATOORIG
                                                                             ) LIS
                         WHERE  F.CDFILIAL     = CUR_ORIDEST.CDFILIORIG AND
                                F.CDSERVFILI   = CUR_ORIDEST.CDSERVORIG AND
                                F.DTREFECNFMES = CUR_PERIODO.DTMESORIG  AND
                                L.NRLISTA      = CUR_ORIDEST.NRLISTACNF AND
                                L.CDLISTA      = LPAD(F.NRSEQCNFMES, 3, '0') AND
                                C.CDFILIAL     (+) = F.CDFILIAL AND
                                C.NRCARDAPIO   (+) = C_CARDORIG.NRCARDAPIO AND
                                C.NRSEQPRATCAR (+) = F.NRSEQCNFMES AND
                                P.CDPRATO      (+) = C.CDPRATO     AND
                                P.CDFILAUXPRAT (+) = C.CDFILAUXPRAT AND

                                LIS.CDFILAUXPRATORIG (+) = C.CDFILAUXPRAT AND
                                LIS.CDPRATOORIG (+) = C.CDPRATO AND 
                                ((LIS.CDFILAUXPRATORIG IS NULL) OR (LIS.CDFILAUXPRATORIG IS NOT NULL AND LIS.CDFILAUXPRATDEST IS NOT NULL))


                          ORDER BY NRORDCNFMES)
                     LOOP
                        IF C_PRATOSORIG.CDPRATO <> '�?¿' THEN
                           V_CDFILAUXDEST := C_PRATOSORIG.CDFILAUXPRAT;
                           IF C_PRATOSORIG.CDFILAUXPRAT <> 'P' THEN
                              V_CDFILAUXDEST := V_CDFILIDEST;
                           END IF;

                           V_CONTPRAT  := 1;
                           V_EXISTPRAT := 1;
                           IF CUR_ORIDEST.CDFILIDEST <> CUR_ORIDEST.CDFILIORIG THEN
                              V_EXISTPRAT := 0;
                              SELECT COUNT(*)
                                INTO   V_EXISTPRAT
                                FROM   PRATO
                               WHERE  CDFILAUXPRAT = V_CDFILAUXDEST  AND
                                      CDPRATO = C_PRATOSORIG.CDPRATO;

                              IF P_IDGERASUBST <> 'S' THEN
                                 V_CONTPRAT := 0;
                                 SELECT COUNT(*)
                                   INTO   V_CONTPRAT
                                   FROM   PRATO
                                   WHERE  CDFILAUXPRAT = V_CDFILAUXDEST  AND
                                          CDPRATO = C_PRATOSORIG.CDPRATO AND
                                          TRANSLATE(UPPER(NMPRATO), '�?�?�?��?�?�??�?š�?�?��?�?�?�?�?�??�?�?��?�??�?� �?Ž�?�?��?�?��?�??�?�?��?�?�?"�?�"�?�?�?�?��?�??�?�?�', 'AEIOUAEIOUAEIOUAEIOUAONC') =
                                          TRANSLATE(UPPER(C_PRATOSORIG.NMPRATO), '�?�?�?��?�?�??�?š�?�?��?�?�?�?�?�??�?�?��?�??�?� �?Ž�?�?��?�?��?�??�?�?��?�?�?"�?�"�?�?�?�?��?�??�?�?�', 'AEIOUAEIOUAEIOUAEIOUAONC');
                              END IF;
                           END IF;

                           /* IDUTRPADCA = 'N' --> NAO PERMITE RECEITA PADRAO NO CARDAPIO */
                           V_CONTPRODRECE := 0;
                           IF V_IDUTRPADCA = 'N' THEN
                              SELECT SUM(NUM)
                              INTO V_CONTPRODRECE
                              FROM  (SELECT COUNT(*) NUM
                                     FROM   RECEFILI
                                     WHERE  CDFILAUXPRAT = V_CDFILAUXDEST  AND
                                            CDPRATO = C_PRATOSORIG.CDPRATO AND
                                            CDFILIAL = V_CDFILIDEST
                                     UNION ALL
                                     SELECT COUNT(*) NUM
                                     FROM   RECESERV
                                     WHERE  CDFILAUXPRAT = V_CDFILAUXDEST  AND
                                            CDPRATO = C_PRATOSORIG.CDPRATO AND
                                            CDFILIAL = V_CDFILIDEST AND
                                            CDSERVFILI = V_CDSERVDEST);
                           ELSE
                              SELECT SUM(NUM)
                              INTO V_CONTPRODRECE
                              FROM (SELECT COUNT(*) NUM
                                    FROM   RECEPADR
                                    WHERE  CDFILAUXPRAT = V_CDFILAUXDEST AND
                                           CDPRATO = C_PRATOSORIG.CDPRATO
                                    UNION ALL
                                    SELECT COUNT(*) NUM
                                    FROM   RECEFILI
                                    WHERE  CDFILAUXPRAT = V_CDFILAUXDEST AND
                                           CDPRATO = C_PRATOSORIG.CDPRATO AND
                                           CDFILIAL = V_CDFILIDEST
                                    UNION ALL
                                    SELECT COUNT(*) NUM
                                    FROM   RECESERV
                                    WHERE  CDFILAUXPRAT = V_CDFILAUXDEST AND
                                           CDPRATO = C_PRATOSORIG.CDPRATO AND
                                           CDFILIAL = V_CDFILIDEST AND
                                           CDSERVFILI = V_CDSERVDEST);
                           END IF;

                           /* VERIFICAR SE HA PRODUTO INATIVO NO PRATO */
                           /* verificar a receita utilizada pelo prato destino */
                           V_TIPORECE := 'N';
                           SELECT NVL(MAX(TIPO), 'N')
                           INTO   V_TIPORECE
                           FROM  (SELECT 'S' AS TIPO
                                  FROM   RECESERV
                                  WHERE  (CDFILIAL     = V_CDFILIDEST)         AND
                                         (CDSERVFILI   = V_CDSERVDEST)         AND
                                         (CDPRATO      = C_PRATOSORIG.CDPRATO) AND
                                         (CDFILAUXPRAT = V_CDFILAUXDEST)
                                  UNION ALL
                                  SELECT 'F' AS TIPO
                                  FROM   RECEFILI F
                                  WHERE  (CDFILIAL     = V_CDFILIDEST)         AND
                                         (CDPRATO      = C_PRATOSORIG.CDPRATO) AND
                                         (CDFILAUXPRAT = V_CDFILAUXDEST)       AND
                                         NOT EXISTS (SELECT CDPRATO
                                                     FROM   RECESERV
                                                     WHERE  (CDFILIAL     = F.CDFILIAL)     AND
                                                            (CDSERVFILI   = V_CDSERVDEST)   AND
                                                            (CDPRATO      = F.CDPRATO)      AND
                                                            (CDFILAUXPRAT = F.CDFILAUXPRAT))
                                  UNION ALL
                                  SELECT 'P' AS TIPO
                                  FROM   RECEPADR P
                                  WHERE  (CDPRATO      = C_PRATOSORIG.CDPRATO) AND
                                         (CDFILAUXPRAT = V_CDFILAUXDEST)       AND
                                         NOT EXISTS (SELECT CDPRATO
                                                     FROM   RECEFILI
                                                     WHERE  (CDFILIAL     = V_CDFILIDEST)    AND
                                                            (CDPRATO      = P.CDPRATO)       AND
                                                            (CDFILAUXPRAT = P.CDFILAUXPRAT)) AND
                                         NOT EXISTS (SELECT CDPRATO
                                                     FROM   RECESERV
                                                     WHERE  (CDFILIAL     = V_CDFILIDEST) AND
                                                            (CDSERVFILI   = V_CDSERVDEST) AND
                                                            (CDPRATO      = P.CDPRATO)           AND
                                                            (CDFILAUXPRAT = P.CDFILAUXPRAT))) A;
                            /************************/
                           /*Parametro para selecionar apenas produto de nivel completo*/
                           SELECT MAX(CDNVPRODUTO)
                           INTO V_CDNVPRODUTO
                           FROM PRODUTO;
                           V_PRODINATIVO   := FALSE;
                           V_CDARVPRODINAT :='';
                           IF (V_TIPORECE = 'S') THEN  /*RECEITA POR SERVICO COM PRODUTO INATIVO*/
                             FOR C_RECESERV IN (
                                 SELECT  P.CDARVPROD, P.IDPRODATIVPR, NVL(PF.IDPRODATIV, 'A') AS IDPRODATIV
                                  FROM   PRODUTO P, PRODFILI PF, RECESERV RS
                                  WHERE  PF.CDPRODUTO(+) = P.CDPRODUTO            AND
                                        PF.CDFILIAL (+) = V_CDFILIDEST           AND
                                        P.CDNVPRODUTO   = V_CDNVPRODUTO          AND
                                        RS.CDFILIAL     = V_CDFILIDEST           AND
                                        RS.CDSERVFILI   = V_CDSERVDEST           AND
                                        RS.CDPRATO      = C_PRATOSORIG.CDPRATO   AND
                                        RS.CDFILAUXPRAT = V_CDFILAUXDEST         AND
                                        P.CDPRODUTO     = RS.CDPRODUTO)
                               LOOP
                                  IF (C_RECESERV.IDPRODATIV = 'I') OR (C_RECESERV.IDPRODATIVPR = 'N') THEN     /*O CAMPO CDPRATLOG (TABELA:LOGINTCARD) GUARDARA� OS PRODUTO INATIVOS*/
                                     V_PRODINATIVO := TRUE;
                                     V_CDARVPRODINAT := C_RECESERV.CDARVPROD;
                                  END IF;
                               END LOOP;

                            ELSE
                               IF (V_TIPORECE = 'F') THEN  /*RECEITA LOCAL COM PRODUTO INATIVO*/

                                  FOR C_RECEFILI IN (
                                      SELECT P.CDARVPROD, P.IDPRODATIVPR, NVL(PF.IDPRODATIV, 'A') AS IDPRODATIV
                                      FROM   PRODUTO P, PRODFILI PF, RECEFILI RF
                                      WHERE  PF.CDPRODUTO(+) = P.CDPRODUTO          AND
                                             PF.CDFILIAL (+) = V_CDFILIDEST         AND
                                             P.CDNVPRODUTO   = V_CDNVPRODUTO        AND
                                             RF.CDFILIAL     = V_CDFILIDEST         AND
                                             RF.CDPRATO      = C_PRATOSORIG.CDPRATO AND
                                             RF.CDFILAUXPRAT = V_CDFILAUXDEST       AND
                                             P.CDPRODUTO     = RF.CDPRODUTO)
                                  LOOP
                                     IF (C_RECEFILI.IDPRODATIV = 'I') OR (C_RECEFILI.IDPRODATIVPR = 'N') THEN     /*O CAMPO CDPRATLOG (TABELA:LOGINTCARD) GUARDARA OS PRODUTO INATIVOS*/
                                        V_PRODINATIVO := TRUE;
                                        V_CDARVPRODINAT := C_RECEFILI.CDARVPROD;
                                     END IF;
                                  END LOOP;

                               ELSE
                                  IF (V_TIPORECE ='P') THEN   /*RECEITA PADR�?�?O COM PRODUTO INATIVO*/

                                     FOR C_RECEPADR IN (
                                        SELECT P.CDARVPROD, P.IDPRODATIVPR, NVL(PF.IDPRODATIV, 'A') AS IDPRODATIV
                                        FROM   PRODUTO  P, PRODFILI PF, RECEPADR RP
                                        WHERE  P.CDNVPRODUTO        = V_CDNVPRODUTO        AND
                                              V_CDFILIDEST         = PF.CDFILIAL(+)       AND
                                              P.CDPRODUTO          = PF.CDPRODUTO(+)      AND
                                              RP.CDPRATO           = C_PRATOSORIG.CDPRATO AND
                                              RP.CDFILAUXPRAT      = V_CDFILAUXDEST       AND
                                              RP.CDPRODUTO         = P.CDPRODUTO)
                                     LOOP
                                        IF (C_RECEPADR.IDPRODATIV = 'I') OR (C_RECEPADR.IDPRODATIVPR = 'N') THEN /*O CAMPO CDPRATLOG (TABELA:LOGINTCARD) GUARDARA OS PRODUTO INATIVOS*/
                                           V_PRODINATIVO   := TRUE;
                                           V_CDARVPRODINAT := C_RECEPADR.CDARVPROD;
                                        END IF;
                                     END LOOP;

                                  END IF; /*RECEITA PADR�O COM PRODUTO INATIVO*/
                               END IF;  /*RECEITA LOCAL COM PRODUTO INATIVO geovane*/
                            END IF; /*RECEITA POR SERVICO COM PRODUTO INATIVO geovane*/

                            /***********************/
                            /* FIM - VERIFICAR SE H� PRODUTO INATIVO NO PRATO */
                            V_IDATIVO := PRATO_RECEITA_ATIVA(C_PRATOSORIG.CDPRATO, V_CDFILAUXDEST, V_CDFILIDEST, V_CDSERVDEST, V_TIPORECE); /* VALIDANDO PRATO RECEITA UTILZIADA ATIVA */
                            IF (V_EXISTPRAT <> 0) AND (V_CONTPRAT <> 0) AND (V_CONTPRODRECE <> 0) AND (V_PRODINATIVO <> TRUE) AND (V_IDATIVO <> 'N') THEN
                               SELECT MAX(DECODE(B.QTTPPRATOS,
                                                0, 0,
                                                ROUND((A.VALOR / B.QTTPPRATOS), 6)))
                               INTO   V_VALOR_A_MAIS
                               FROM   (SELECT C.CDTIPOPRAT, C.NRSEQPRATCAR,
                                             SUM(C.VRPECONSCARD) VALOR
                                      FROM   COMPCARD C
                                      WHERE  C.CDFILIAL   = CUR_ORIDEST.CDFILIORIG AND
                                             C.NRCARDAPIO = C_CARDORIG.NRCARDAPIO AND
                                             NOT EXISTS (SELECT L.CDLISTA
                                                         FROM   LISTA L
                                                         WHERE  L.NRLISTA = CUR_ORIDEST.NRLISTACNF AND
                                                                L.CDLISTA = C.NRSEQPRATCAR)
                                      GROUP BY C.CDTIPOPRAT, C.NRSEQPRATCAR) A,
                                     (SELECT C.CDTIPOPRAT, COUNT(C.CDTIPOPRAT) AS QTTPPRATOS, C.NRSEQCNFMES
                                      FROM   CNFCAMES C, LISTA L
                                      WHERE  C.CDFILIAL     = CUR_ORIDEST.CDFILIORIG AND
                                             C.CDSERVFILI   = CUR_ORIDEST.CDSERVORIG AND
                                             C.DTREFECNFMES = CUR_PERIODO.DTMESORIG  AND
                                             L.NRLISTA      = CUR_ORIDEST.NRLISTACNF AND
                                             L.CDLISTA      = C.NRSEQCNFMES
                                      GROUP BY C.CDTIPOPRAT, C.NRSEQCNFMES) B
                               WHERE   A.CDTIPOPRAT = B.CDTIPOPRAT AND
                                      A.NRSEQPRATCAR = C_PRATOSORIG.NRSEQPRATCAR;

                               IF V_VALOR_A_MAIS IS NULL THEN
                                 V_VALOR_A_MAIS := 0;
                               END IF;

                               SELECT MIN(NRORDCNFMES)
                                 INTO V_NRORDCNFMES
                                 FROM CNFCAMES CF
                                WHERE CF.CDFILIAL     = CUR_ORIDEST.CDFILIDEST
                                  AND CF.CDSERVFILI   = CUR_ORIDEST.CDSERVDEST
                                  AND CF.DTREFECNFMES = CUR_PERIODO.DTMESDEST
                                  AND CF.CDTIPOPRAT   = C_PRATOSORIG.CDTIPOPRAT
                                  /* Garante que quanto for Acrescentar e Nao manter configuracao (adicionando colunas a mais) altera apenas as colunas adicionais */
                                  AND CF.NRSEQCNFMES > V_MAXNRSEQDEST
                                  AND NOT EXISTS (SELECT NRORDPRATCAR FROM COMPCARD
                                                   WHERE CDFILIAL     = CUR_ORIDEST.CDFILIDEST
                                                     AND CDTIPOPRAT   = C_PRATOSORIG.CDTIPOPRAT
                                                     AND NRCARDAPIO   = V_NRCARDAPIO
                                                     AND NRORDPRATCAR = CF.NRORDCNFMES);
                               IF (NOT (V_NRORDCNFMES) IS NULL) THEN 
                                  SELECT MIN(NRSEQCNFMES) 
                                    INTO V_NRSEQCNFMES
                                    FROM CNFCAMES
                                   WHERE CDFILIAL     = CUR_ORIDEST.CDFILIDEST
                                     AND CDSERVFILI   = CUR_ORIDEST.CDSERVDEST
                                     AND DTREFECNFMES = CUR_PERIODO.DTMESDEST
                                     AND CDTIPOPRAT   = C_PRATOSORIG.CDTIPOPRAT
                                     AND NRORDCNFMES  = V_NRORDCNFMES
                                     /* Garante que quanto for Acrescentar e Nao manter configuracao (adicionando colunas a mais) altera apenas as colunas adicionais */
                                     AND NRSEQCNFMES > V_MAXNRSEQDEST;
                               ELSE
                                  V_NRSEQCNFMES := NULL;
                               END IF;

                               IF NOT V_NRSEQCNFMES IS NULL THEN    

                                  IF (P_MODOPROC = 'M') AND (V_IDCARDDESCENTR = 'S') THEN
                                     IF V_AUXESP = 'S' THEN
                                        IF (P_IDSOBREPOR = 'S') THEN
                                           BEGIN
                                              SELECT CDPRATO, CDFILAUXPRAT
                                              INTO V_CDPRATODEL, V_CDFILAUXPRATDEL
                                              FROM COMPCARDDEL
                                              WHERE CDFILIAL = CUR_ORIDEST.CDFILIDEST
                                              AND CDSERVFILI = CUR_ORIDEST.CDSERVDEST
                                              AND DTPRATEXCL = CUR_PERIODO.DTDEST
                                              AND NRSEQPRATEXCL = V_NRSEQCNFMES;
                                           EXCEPTION
                                              WHEN NO_DATA_FOUND THEN
                                              V_CDPRATODEL := '';
                                              V_CDFILAUXPRATDEL := '';
                                           END;

                                           IF V_IDMANCORCARDALT = 'S' THEN
                                              V_PRATOESPE := 'R';
                                           ELSE
                                              IF ((V_CDPRATODEL = C_PRATOSORIG.CDPRATO) AND (V_CDFILAUXPRATDEL = V_CDFILAUXDEST)) THEN
                                                 V_PRATOESPE := 'N';                                          
                                                 V_CDPRATODEL := '';
                                                 V_CDFILAUXPRATDEL := '';
                                              ELSE
                                                 V_PRATOESPE := 'R';
                                              END IF;
                                           END IF;
                                        ELSE
                                           V_CDPRATODEL := '';
                                           V_CDFILAUXPRATDEL := '';
                                           V_PRATOESPE := 'R';
                                        END IF;
                                     ELSE
                                        V_PRATOESPE := 'N';
                                        V_CDPRATODEL := '';
                                        V_CDFILAUXPRATDEL := '';
                                     END IF;
                                  END IF;

                                  IF (P_MODOPROC = 'F') AND (V_IDCARDDESCENTR = 'S') THEN
                                     IF (P_IDSOBREPOR = 'S') THEN
                                        BEGIN
                                           SELECT CDPRATO, CDFILAUXPRAT
                                           INTO V_CDPRATODEL, V_CDFILAUXPRATDEL
                                           FROM COMPCARDDEL
                                           WHERE CDFILIAL = CUR_ORIDEST.CDFILIDEST
                                           AND CDSERVFILI = CUR_ORIDEST.CDSERVDEST
                                           AND DTPRATEXCL = CUR_PERIODO.DTDEST
                                           AND NRSEQPRATEXCL = V_NRSEQCNFMES;
                                        EXCEPTION
                                           WHEN NO_DATA_FOUND THEN
                                              V_CDPRATODEL := '';
                                              V_CDFILAUXPRATDEL := '';
                                        END;
                                        IF V_IDMANCORCARDALT = 'S' THEN
                                           V_PRATOESPE := 'S';
                                        ELSE
                                           IF ((V_CDPRATODEL = C_PRATOSORIG.CDPRATO) AND (V_CDFILAUXPRATDEL = V_CDFILAUXDEST)) THEN
                                              V_PRATOESPE := 'N';
                                              V_CDPRATODEL := '';
                                              V_CDFILAUXPRATDEL := '';
                                           ELSE
                                              V_PRATOESPE := 'S';
                                           END IF;
                                        END IF;
                                     ELSE
                                        V_CDPRATODEL := '';
                                        V_CDFILAUXPRATDEL := '';
                                        V_PRATOESPE := 'S';
                                     END IF;
                                  END IF; 

                                  /*VALIDA HISTORICO DE CONSUMO DO PRATO E EXISTENCIA DE GRUPO DE PRATO - SOMENTE SE O CARDAPIO DE ORIGEM VEIO DO OTIMIZADOR*/  
                                  IF V_NRCENARIOCARD IS NOT NULL THEN
                                     SELECT MAX(VRPECONSCARD)
                                       INTO V_VRPECONSCARD
                                       FROM(SELECT ROUND(AVG(CP.VRPECONSCARD),2) AS VRPECONSCARD 
                                              FROM COMPCARD CP INNER JOIN CARDADIA C ON C.CDFILIAL = CP.CDFILIAL AND C.NRCARDAPIO = CP.NRCARDAPIO 
                                                               INNER JOIN SERVICO S ON S.CDFILIAL = C.CDFILIAL AND S.CDSERVFILI = C.CDSERVFILI 
                                             WHERE C.CDFILIAL = CUR_ORIDEST.CDFILIDEST AND C.CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND 
                                                   (C.DTMESCARD BETWEEN ADD_MONTHS(TO_DATE(CUR_PERIODO.DTMESDEST),(NVL(S.NRMESHISTCONS,6)*-1)) AND TO_DATE(CUR_PERIODO.DTMESDEST)) AND
                                                   CP.CDPRATO = C_PRATOSORIG.CDPRATO AND CP.CDFILAUXPRAT = V_CDFILAUXDEST AND
                                                   NOT EXISTS (SELECT H.CDPRATO 
                                                                 FROM HISTCONSUMOPRATO H 
                                                               WHERE H.CDFILIAL = CUR_ORIDEST.CDFILIDEST AND H.CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND H.CDFILAUXPRAT = CP.CDFILAUXPRAT AND H.CDPRATO = CP.CDPRATO
                                                               ) 
                                            UNION ALL 
                                            SELECT H.VRPECONSCARD 
                                              FROM HISTCONSUMOPRATO H 
                                             WHERE H.CDFILIAL = CUR_ORIDEST.CDFILIDEST AND H.CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND H.CDFILAUXPRAT = V_CDFILAUXDEST AND H.CDPRATO = C_PRATOSORIG.CDPRATO
                                           ) H;

                                    /*SE NAO TEM HISTORICO ENTAO VAI GRAVAR O PRATO COM O HISTORICO DA FILIAL DE ORIGEM POREM VAI GERAR LOG COM ESTA INFORMACAO*/
                                    IF V_VRPECONSCARD IS NULL THEN
                                       V_VRPECONSCARD := C_PRATOSORIG.VRPECONSCARD + V_VALOR_A_MAIS;
                                       /*GRAVA LOG DE PRATO SEM HISTORICO NO DESTINO*/
                                       IF V_NRLOGINTCARD IS NULL THEN
                                          NOVO_CODIGO('LOGINTCARD_ASSISTENTE', V_NRLOGINTCARD, 1);
                                          V_NRLOGINTCARD := 'A'|| SUBSTR(V_NRLOGINTCARD,12,9);
                                       END IF;
                                       GRAVA_LOGINTCARD(V_NRLOGINTCARD, CUR_ORIDEST.CDFILIORIG, V_CDFILIDEST, V_CDSERVDEST, CUR_PERIODO.DTDEST, C_PRATOSORIG.CDPRATO, C_PRATOSORIG.CDFILAUXPRAT, V_CDFILAUXDEST, 'A');
                                    END IF;

                                    /*VALIDA SE O PRATO POSSUI GRUPO DE PRATO NO DESTINO, SE NAO EXISTE EXPORTA O PRATO ASSIM MESMO MAS GRAVA NO LOG*/
                                    SELECT MAX(IT.NRGRUPOPRATOSERV)
                                      INTO V_NRGRUPOPRATOSERV
                                      FROM ITGRUPOPRATOSERV IT INNER JOIN GRUPOPRATOSERV GS ON GS.NRGRUPOPRATOSERV = IT.NRGRUPOPRATOSERV
                                                               INNER JOIN GRUPOPRATO G ON G.NRGRUPOPRATO = GS.NRGRUPOPRATO
                                     WHERE IT.CDFILAUXPRAT = V_CDFILAUXDEST AND
                                           IT.CDPRATO = C_PRATOSORIG.CDPRATO AND
                                           G.CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                                           GS.CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND
                                           G.IDTIPORESTRICAO = V_IDTIPORESTRICAO;
                                    /*GRAVA LOG DE PRATO SEM GRUPO DE PRATO NO DESTINO*/
                                    IF V_NRGRUPOPRATOSERV IS NULL THEN
                                       IF V_NRLOGINTCARD IS NULL THEN
                                          NOVO_CODIGO('LOGINTCARD_ASSISTENTE', V_NRLOGINTCARD, 1);
                                          V_NRLOGINTCARD := 'A'|| SUBSTR(V_NRLOGINTCARD,12,9);
                                       END IF;
                                       GRAVA_LOGINTCARD(V_NRLOGINTCARD, CUR_ORIDEST.CDFILIORIG, V_CDFILIDEST, V_CDSERVDEST, CUR_PERIODO.DTDEST, C_PRATOSORIG.CDPRATO, C_PRATOSORIG.CDFILAUXPRAT, V_CDFILAUXDEST, 'B');
                                    END IF;
                                 ELSE
                                    V_VRPECONSCARD := C_PRATOSORIG.VRPECONSCARD + V_VALOR_A_MAIS;
                                 END IF;
                                 /*INSERE O PRATO ORIGEM NO PRIMEIRO TIPO DE PRATO DESTINO CORRESPONDENTE*/
                                 INSERT INTO COMPCARD (CDFILIAL, NRCARDAPIO, NRSEQPRATCAR, CDFILAUXPRAT, CDPRATO,
                                                        VRPECONSCARD, CDTIPOPRAT, NRORDPRATCAR, IDPRATOESPE,
                                                        CDPRATOMAT, CDFILAUXPRATMAT)
                                 VALUES (CUR_ORIDEST.CDFILIDEST, V_NRCARDAPIO, V_NRSEQCNFMES, V_CDFILAUXDEST, C_PRATOSORIG.CDPRATO,
                                          V_VRPECONSCARD, C_PRATOSORIG.CDTIPOPRAT, V_NRORDCNFMES, V_PRATOESPE,
                                          V_CDPRATODEL, V_CDFILAUXPRATDEL);  
                              END IF;
                           ELSE
                           /* LOG DE RESTRI��ES DA EXPORTA��O */
                              IF V_NRLOGINTCARD IS NULL THEN
                                 NOVO_CODIGO('LOGINTCARD_ASSISTENTE', V_NRLOGINTCARD, 1);
                                 V_NRLOGINTCARD := 'A'|| SUBSTR(V_NRLOGINTCARD,12,9);
                              END IF;
                              /* 1o Caso: Prato n�o existe no destino */
                              IF V_EXISTPRAT = 0 THEN
                                 GRAVA_LOGINTCARD(V_NRLOGINTCARD, CUR_ORIDEST.CDFILIORIG, V_CDFILIDEST, V_CDSERVDEST, CUR_PERIODO.DTDEST, C_PRATOSORIG.CDPRATO, C_PRATOSORIG.CDFILAUXPRAT, V_CDFILAUXDEST, '1');
                              ELSIF
                              /* 2o Caso: Prato com nome diferente no destino */
                              V_CONTPRAT = 0 THEN
                                 GRAVA_LOGINTCARD(V_NRLOGINTCARD, CUR_ORIDEST.CDFILIORIG, V_CDFILIDEST, V_CDSERVDEST, CUR_PERIODO.DTDEST, C_PRATOSORIG.CDPRATO, C_PRATOSORIG.CDFILAUXPRAT, V_CDFILAUXDEST, '2');
                              ELSIF
                              /* 3o Caso: Prato no destino mudaria para status padr�o ou sem receita */
                              V_CONTPRODRECE = 0 THEN
                                 GRAVA_LOGINTCARD(V_NRLOGINTCARD, CUR_ORIDEST.CDFILIORIG, V_CDFILIDEST, V_CDSERVDEST, CUR_PERIODO.DTDEST, C_PRATOSORIG.CDPRATO, C_PRATOSORIG.CDFILAUXPRAT, V_CDFILAUXDEST, '3');
                              ELSIF
                              /* 4o Caso: Prato no destino contem produto inativo */
                              V_PRODINATIVO = TRUE THEN
                                 GRAVA_LOGINTCARD(V_NRLOGINTCARD, CUR_ORIDEST.CDFILIORIG, V_CDFILIDEST, V_CDSERVDEST, CUR_PERIODO.DTDEST, C_PRATOSORIG.CDPRATO, C_PRATOSORIG.CDFILAUXPRAT, NULL, '5');
                              ELSIF
                              /* 5 Caso: Prato ou receita utilizada inativa */
                              V_IDATIVO = 'N' THEN
                                 GRAVA_LOGINTCARD(V_NRLOGINTCARD, CUR_ORIDEST.CDFILIORIG, V_CDFILIDEST, V_CDSERVDEST, CUR_PERIODO.DTDEST, C_PRATOSORIG.CDPRATO, C_PRATOSORIG.CDFILAUXPRAT, NULL, '7');
                              END IF;
                           END IF;
                        END IF;
                     END LOOP;
                  ELSE
                     IF V_NRLOGINTCARD IS NULL THEN
                        NOVO_CODIGO('LOGINTCARD_ASSISTENTE', V_NRLOGINTCARD, 1);
                        V_NRLOGINTCARD := 'A'|| SUBSTR(V_NRLOGINTCARD,12,9);
                     END IF;
                     /* 1o Caso: Cardapio seria gravado com comensais = 0 */
                     GRAVA_LOGINTCARD(V_NRLOGINTCARD, CUR_ORIDEST.CDFILIORIG, V_CDFILIDEST, V_CDSERVDEST, CUR_PERIODO.DTDEST, ' ', ' ', ' ', '0');
                  END IF;
                  /*FIM - ETAPA 6 ===== */

               END IF;

            END LOOP; -- Fim loop cardadia de origem

         END IF;
         /*FIM - ETAPA 4 ===== */

      END LOOP;
      /*FIM - ETAPA 2 ===== */

      /*VALIDACOES DE FREQUENCIAS DE GRUPO DE PRATO*/
      IF V_NRCENARIOCARD IS NOT NULL THEN
         /*BUSCA VIGENCIA DE FREQUENCIA DE GRUPO DE PRATO VALENDO PARA O MES DA UNIDADE DESTINO*/ 
         SELECT MAX(NRVIGENCIA)
           INTO V_NRVIGENCIA
           FROM(
         SELECT V.NRVIGENCIA, 
                CASE WHEN (V.DTVIGENCIAFIM IS NOT NULL AND V.DTVIGENCIAFIM >= V_MES_ATUAL AND V.DTVIGENCIA <= V_MES_ATUAL) THEN 0
                     WHEN (V.DTVIGENCIAFIM IS NOT NULL AND V.DTVIGENCIAFIM <= V_MES_ATUAL) THEN 2 
                     WHEN V.DTVIGENCIA <= V_MES_ATUAL THEN 1 ELSE 2 END AS ORDEM 
           FROM VIGFREQGRPRATMES V INNER JOIN  FREQGRPRATMES F ON V.NRVIGENCIA = F.NRVIGENCIA
          WHERE V.CDFILIAL = CUR_ORIDEST.CDFILIDEST AND V.IDTIPORESTRICAO = V_IDTIPORESTRICAO AND F.CDSERVFILI = CUR_ORIDEST.CDSERVDEST
         ORDER BY ORDEM, V.DTVIGENCIA DESC)
         WHERE ROWNUM = 1;

         IF V_NRVIGENCIA IS NULL THEN
            /* LOG DE RESTRI��ES DA EXPORTA��O */
            IF V_NRLOGINTCARD IS NULL THEN
               NOVO_CODIGO('LOGINTCARD_ASSISTENTE', V_NRLOGINTCARD, 1);
               V_NRLOGINTCARD := 'A'|| SUBSTR(V_NRLOGINTCARD,12,9);
            END IF;
            /*ERRO NAO EXISTE VIGENCIA CADASTRADA PARA FREQUENCIA NO DESTINO*/
            GRAVA_LOGINTCARD(V_NRLOGINTCARD, CUR_ORIDEST.CDFILIORIG, V_CDFILIDEST, V_CDSERVDEST, V_MES_ATUAL, 'V', NULL, NULL, 'C');
         ELSE
            /*FAZ UM LOOP PEGANDO TODOS OS GRUPOS DE PRATO QUE ESTEJAM FORA DO LIMITE DE FREQUENCIA ACIMA DO MAXIMO OU ABAIXO DO MINIMO DO MES, OU ACIMA DO MAXIMO DA SEMANA OU NAO TENHA FREQUENCIA OU TENHA FREQUENCIA E NAO FOI USADO*/
            FOR C_FREQ IN (SELECT DISTINCT CDGRUPOPRATO, 
                                  CASE WHEN QTGRUPOMES IS NULL THEN 'D' 
                                       WHEN QTGRUPOMES > NRMAXFREQGRPRATMES OR QTGRUPOMES < NRMINFREQGRPRATMES THEN 'E' 
                                       WHEN QTGRUPOSEMANA > NRMAXFREQGRPRATSEM THEN 'F' 
                                       WHEN NRMINFREQGRPRATMES IS NULL THEN 'G' END AS ERROS
                             FROM (SELECT CASE WHEN FREQ.CDGRUPOPRATO IS NULL THEN CARD.CDGRUPOPRATO ELSE FREQ.CDGRUPOPRATO END AS CDGRUPOPRATO, NRMINFREQGRPRATMES, NRMAXFREQGRPRATMES, NRMAXFREQGRPRATSEM, QTGRUPOMES, QTGRUPOSEMANA
                                     FROM (SELECT G.CDGRUPOPRATO, IT.NRMINFREQGRPRATMES, IT.NRMAXFREQGRPRATMES, IT.NRMAXFREQGRPRATSEM
                                             FROM FREQGRPRATMES F INNER JOIN ITFREQGRPRATMES IT ON F.NRFREQGRPRATMES = IT.NRFREQGRPRATMES
                                             INNER JOIN GRUPOPRATOSERV GS ON GS.NRGRUPOPRATOSERV = IT.NRGRUPOPRATOSERV AND GS.CDSERVFILI = F.CDSERVFILI
                                             INNER JOIN GRUPOPRATO G ON G.NRGRUPOPRATO = GS.NRGRUPOPRATO AND G.CDFILIAL = F.CDFILIAL AND G.IDTIPORESTRICAO = V_IDTIPORESTRICAO
                                            WHERE F.NRVIGENCIA = V_NRVIGENCIA AND F.CDFILIAL = CUR_ORIDEST.CDFILIDEST AND F.CDSERVFILI = CUR_ORIDEST.CDSERVDEST
                                          ) FREQ FULL OUTER JOIN
                                          (SELECT DISTINCT G.CDGRUPOPRATO, COUNT(G.CDGRUPOPRATO) OVER (PARTITION BY G.CDGRUPOPRATO) AS QTGRUPOMES, 
                                                  COUNT(IT.NRGRUPOPRATOSERV) OVER (PARTITION BY IT.NRGRUPOPRATOSERV, TO_CHAR(C.DTCARDAPIO,'WW')) AS QTGRUPOSEMANA
                                             FROM CARDADIA C INNER JOIN COMPCARD CP ON C.CDFILIAL = CP.CDFILIAL AND C.NRCARDAPIO = CP.NRCARDAPIO
                                                             INNER JOIN ITGRUPOPRATOSERV IT ON IT.CDFILAUXPRAT = CP.CDFILAUXPRAT AND IT.CDPRATO = CP.CDPRATO
                                                             INNER JOIN GRUPOPRATOSERV GS ON GS.NRGRUPOPRATOSERV = IT.NRGRUPOPRATOSERV AND GS.CDSERVFILI = C.CDSERVFILI
                                                             INNER JOIN GRUPOPRATO G ON G.NRGRUPOPRATO = GS.NRGRUPOPRATO AND G.CDFILIAL = C.CDFILIAL AND G.IDTIPORESTRICAO = V_IDTIPORESTRICAO
                                            WHERE C.CDFILIAL = CUR_ORIDEST.CDFILIDEST AND C.CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND C.DTMESCARD = V_MES_ATUAL
                                          )CARD ON FREQ.CDGRUPOPRATO = CARD.CDGRUPOPRATO
                                    WHERE CASE WHEN QTGRUPOMES IS NULL THEN 'D' 
                                               WHEN QTGRUPOMES > NRMAXFREQGRPRATMES OR QTGRUPOMES < NRMINFREQGRPRATMES THEN 'E' 
                                               WHEN QTGRUPOSEMANA > NRMAXFREQGRPRATSEM THEN 'F' 
                                               WHEN NRMINFREQGRPRATMES IS NULL THEN 'G' END IS NOT NULL
                                  )
            ) LOOP
               /* LOG DE RESTRI��ES DA EXPORTA��O */
               IF V_NRLOGINTCARD IS NULL THEN
                  NOVO_CODIGO('LOGINTCARD_ASSISTENTE', V_NRLOGINTCARD, 1);
                  V_NRLOGINTCARD := 'A'|| SUBSTR(V_NRLOGINTCARD,12,9);
               END IF;
               BEGIN 
                  INSERT INTO LOGINTCARD (NRLOGINTCARD,CDFILDESTLOG,CDSERDESTLOG,DTCARDESTLOG,CDPRATLOG,CDFAPRAORIGL,CDFAPRADESTL,CDFILORIGLOG,IDNUMERRO)
                                  VALUES (V_NRLOGINTCARD,V_CDFILIDEST,V_CDSERVDEST,V_MES_ATUAL,C_FREQ.CDGRUPOPRATO,NULL,NULL,CUR_ORIDEST.CDFILIORIG,C_FREQ.ERROS);
               EXCEPTION
                  WHEN DUP_VAL_ON_INDEX THEN BEGIN
                     UPDATE LOGINTCARD
                     SET IDNUMERRO = IDNUMERRO
                     WHERE
                        NRLOGINTCARD = V_NRLOGINTCARD AND
                        CDFILDESTLOG = V_CDFILIDEST AND
                        CDSERDESTLOG = V_CDSERVDEST AND
                        DTCARDESTLOG = V_MES_ATUAL AND
                        CDPRATLOG    = ' ';
                  END;
               END;
            END LOOP;
         END IF;


         /*ATUALIZA O NUMERO DO CENARIO NA CARDAMES DE DESTINO ASSOCIADO AO CENARIO DA FILIAL DE ORIGEM*/
         SELECT COUNT(1)
           INTO V_EXIST_CARDAMES
           FROM CARDAMES 
          WHERE CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND
                TRUNC(DTMESCARDAPIO) = V_MES_ATUAL;
         IF V_EXIST_CARDAMES > 0 THEN
            UPDATE CARDAMES SET NRCENARIOCARD = V_NRCENARIOCARD
             WHERE CDFILIAL = CUR_ORIDEST.CDFILIDEST AND CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND TRUNC(DTMESCARDAPIO) = V_MES_ATUAL;
         ELSE
            INSERT INTO CARDAMES(CDFILIAL, CDSERVFILI, DTMESCARDAPIO, IDCARDLIBCENT, IDCARDLIBAGRE, IDCARDCICLO, CDOPERINCLUSAO, NRCENARIOCARD) 
                 VALUES (CUR_ORIDEST.CDFILIDEST, CUR_ORIDEST.CDSERVDEST, V_MES_ATUAL, 'N', 'N', 'N', P_CDOPERADOR, V_NRCENARIOCARD);
         END IF;         

      END IF;

      IF (V_TIPOCARDAPIO = 'D') AND (V_IDCARDDESCENTR = 'S') THEN
         /*Atualiza CARDADIA para especifico se nela existir algum prato especifico ou excluido*/
         UPDATE CARDADIA C
           SET C.IDCARDIAESPE = 'S'
         WHERE C.CDFILIAL   = CUR_ORIDEST.CDFILIDEST AND
               C.CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND
               C.DTMESCARD  = V_MES_ATUAL  AND
               EXISTS (SELECT CC.CDFILIAL
                         FROM COMPCARD CC
                        WHERE CC.CDFILIAL   = C.CDFILIAL   AND
                              CC.NRCARDAPIO = C.NRCARDAPIO AND
                              CC.IDPRATOESPE IN ('S', 'R')
                        UNION
                       SELECT CCD.CDFILIAL
                         FROM COMPCARDDEL CCD
                        WHERE CCD.CDFILIAL   = C.CDFILIAL   AND
                              CCD.CDSERVFILI = C.CDSERVFILI AND
                              CCD.DTPRATEXCL = C.DTCARDAPIO);

         /*Deleta da cardadia os dias sem prato*/
         DELETE FROM CARDADIA
         WHERE CDFILIAL = CUR_ORIDEST.CDFILIDEST
           AND CDSERVFILI = CUR_ORIDEST.CDSERVDEST
           AND DTMESCARD = V_MES_ATUAL
           AND NOT EXISTS (SELECT NRCARDAPIO
                             FROM COMPCARD
                            WHERE COMPCARD.CDFILIAL = CARDADIA.CDFILIAL
                              AND COMPCARD.NRCARDAPIO = CARDADIA.NRCARDAPIO);

         IF V_AUXESP = 'N' THEN
           DELETE FROM COMPCARDDEL
            WHERE CDFILIAL = CUR_ORIDEST.CDFILIDEST
              AND CDSERVFILI = CUR_ORIDEST.CDSERVDEST
              AND TO_CHAR(DTPRATEXCL, 'MMYYYY') = TO_CHAR(V_MES_ATUAL, 'MMYYYY');
         ELSE      
           /*Deleta da COMPCARDDEL todos os pratos que nao fazer parte da configuracao do cardapio e que nao guardam informacao do prato liberado pela matriz*/
           DELETE FROM COMPCARDDEL                                
            WHERE CDFILIAL = CUR_ORIDEST.CDFILIDEST
              AND CDSERVFILI = CUR_ORIDEST.CDSERVDEST
              AND TO_CHAR(DTPRATEXCL, 'MMYYYY') = TO_CHAR(V_MES_ATUAL, 'MMYYYY')
              AND CDFILAUXPRAT IS NULL
              AND CDPRATO IS NULL
              AND NOT EXISTS (SELECT NRSEQCNFMES
                                FROM CNFCAMES
                               WHERE CNFCAMES.CDFILIAL = COMPCARDDEL.CDFILIAL
                                 AND CNFCAMES.CDSERVFILI = COMPCARDDEL.CDSERVFILI
                                 AND CNFCAMES.NRSEQCNFMES = COMPCARDDEL.NRSEQPRATEXCL
                                 AND CNFCAMES.DTREFECNFMES = LAST_DAY(ADD_MONTHS(COMPCARDDEL.DTPRATEXCL, -1))+1);
            /*Exclui da COMPCARDDEL todos os sequenciais de pratos que foram incluidos na COMPCARD*/
            DELETE FROM COMPCARDDEL
            WHERE EXISTS (SELECT COMPCARD.CDFILIAL, CARDADIA.CDSERVFILI, CARDADIA.DTCARDAPIO, COMPCARD.NRSEQPRATCAR
                            FROM COMPCARD, CARDADIA
                           WHERE COMPCARD.CDFILIAL = CARDADIA.CDFILIAL
                             AND COMPCARD.NRCARDAPIO = CARDADIA.NRCARDAPIO
                             AND COMPCARD.CDFILIAL = CUR_ORIDEST.CDFILIDEST
                             AND CARDADIA.CDSERVFILI = CUR_ORIDEST.CDSERVDEST
                             AND CARDADIA.DTMESCARD = V_MES_ATUAL
                             AND COMPCARDDEL.CDFILIAL = CARDADIA.CDFILIAL
                             AND COMPCARDDEL.CDSERVFILI = CARDADIA.CDSERVFILI
                             AND COMPCARDDEL.DTPRATEXCL = CARDADIA.DTCARDAPIO
                             AND COMPCARDDEL.NRSEQPRATEXCL = COMPCARD.NRSEQPRATCAR) ;
         END IF;                                 

      END IF;

      /*Apaga lista da configuracao de cardapio do servico de origem*/
      DELETE FROM LISTA
       WHERE NRLISTA = CUR_ORIDEST.NRLISTACNF;

      COMMIT;      
   END LOOP; 
   /*FIM - ETAPA 1 ========================================= */

   /*Apaga lista de origem e destino*/
   DELETE FROM LISTA
       WHERE NRLISTA = P_NRLISTAORIDEST;

   /*Apaga lista do periodo*/
   DELETE FROM LISTA
       WHERE NRLISTA = P_NRLISTAPERIODO;

   P_NRLOGINTCARD := NVL(V_NRLOGINTCARD, '');
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('EXPORTA_CARDAPIO_COMPLETA','6.49.007'); END;
/
