CREATE OR REPLACE PROCEDURE EXPORTA_CARDAPIO_COMPLETA
(
P_NRLISTAORIDEST IN VARCHAR2, /* NÃºmero da lista no formato FilialOri|ServOri|CnfOriLista_FilialDest|ServDest */
P_NRLISTAPERIODO IN VARCHAR2, /* NÃºmero 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);

   /*VariÃ¡vel de controle para nÃ£o 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);

   /*VariÃ¡vel de controle para nÃ£o 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 NUMBER;

   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;
BEGIN
   /*ParametrizaÃ§Ã£o geral do cardÃ¡pio (VÃ¡lido 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 serviÃ§o 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 vÃ¡lidos*/      
      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 informaÃ§Ãµes 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 parÃ¢metro "Verifica custo padrÃ£o na Montagem do Cardapio" estÃ¡ 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 serviÃ§o de origem*/
      SELECT MAX(IDTIPOSERV)
        INTO V_IDTIPOSERV
        FROM SERVICO
       WHERE CDFILIAL   = CUR_ORIDEST.CDFILIORIG AND
             CDSERVFILI = CUR_ORIDEST.CDSERVORIG;

      /*Dados do serviÃ§o 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 perÃ­odo 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 cardÃ¡pio do mes como um todo*/
         IF V_MES_ATUAL IS NULL THEN

            /* Verifica se existe custo padrÃ£o para o serviÃ§o 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'||CUR_ORIDEST.CDFILIDEST, V_NRLOGINTCARD, 1);
                     V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD,11,10);
                  END IF;
                  GRAVA_LOGINTCARD(V_NRLOGINTCARD, CUR_ORIDEST.CDFILIORIG, CUR_ORIDEST.CDFILIDEST, CUR_ORIDEST.CDSERVDEST, CUR_PERIODO.DTMESDEST, ' ', ' ', ' ', '4');
               END IF;       
            END IF;

            /* CardÃ¡pio 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 serviÃ§o jÃ¡ estÃ¡ LIBERADO pela matriz, APROVADO pela filial,
                    APROVADO pela matriz ou se a data de LIMITE de aprovaÃ§Ã£o 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;
                  /*CardÃ¡pio NÃƒO liberado pela MATRIZ*/
                  IF V_DTLIBECARDMATR IS NULL THEN
                     RETURN;
                  END IF;
                  /*CardÃ¡pio JÃÂ aprovado pela FILIAL*/
                  IF NOT V_DTAPROCARDFILI IS NULL THEN
                     RETURN;
                  END IF;
                  /*CardÃ¡pio JÃÂ aprovado pela MATRIZ*/
                  IF NOT V_DTAPROCARDMATR IS NULL THEN
                     RETURN;
                  END IF;
                  /*Data limite de aprovaÃ§Ã£o pela FILIAL expirada*/
                  IF TRUNC(SYSDATE) > TRUNC(V_DTLIMAPROVFILI) THEN
                     RETURN;
                  END IF;
               END IF;
            END IF;


            /* ===== ETAPA 3 - Ajuste da configuraÃ§Ã£o caso mantenha configuraÃ§Ã£o do destino */
            V_MAXNRSEQDEST := 0;
            V_MAXNRORDDEST := 0;
            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
                     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
                    WHERE  CDFILIAL     = CUR_ORIDEST.CDFILIDEST AND
                           CDSERVFILI   = CUR_ORIDEST.CDSERVDEST AND
                           DTREFECNFMES = CUR_PERIODO.DTMESDEST  AND
                           NRSEQCNFMES  = C_SEQ.NRSEQCNFMES;

                    UPDATE COMPCARD
                    SET    NRORDPRATCAR = V_ORD
                    WHERE  CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                           NRSEQPRATCAR = C_SEQ.NRSEQCNFMES AND
                           NRCARDAPIO IN (SELECT NRCARDAPIO
                                          FROM   CARDADIA
                                          WHERE  CDFILIAL   = CUR_ORIDEST.CDFILIDEST AND
                                                 CDSERVFILI = CUR_ORIDEST.CDSERVDEST AND
                                                 DTMESCARD  = CUR_PERIODO.DTMESDEST);
                  END LOOP;

                  V_ORD := 0;

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

                    UPDATE CNFCAMES
                    SET    NRSEQCNFMES = V_ORD
                    WHERE  CDFILIAL     = CUR_ORIDEST.CDFILIDEST AND
                           CDSERVFILI   = CUR_ORIDEST.CDSERVDEST AND
                           DTREFECNFMES = CUR_PERIODO.DTMESDEST AND
                           NRORDCNFMES  = C_ORD.NRORDCNFMES;

                    UPDATE COMPCARD A 
                    SET NRSEQPRATCAR = V_ORD
                    WHERE NRORDPRATCAR = C_ORD.NRORDCNFMES
                    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;

                  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
               )
               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
                 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
                   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 configuraÃ§Ã£o de cardapio para o mÃªs*/
            SELECT COUNT(*) 
              INTO V_EXICNFCAMES
              FROM CNFCAMES
             WHERE CDFILIAL     = CUR_ORIDEST.CDFILIDEST AND
                   CDSERVFILI   = CUR_ORIDEST.CDSERVDEST AND
                   DTREFECNFMES = CUR_PERIODO.DTMESDEST;

            /*Se nÃ£o existe insere a configuraÃ§Ã£o padrao do servico na configuraÃ§Ã£o 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)  
              (SELECT 
                CDFILIAL, CDSERVFILI, NRSEQCNFCARD, CUR_PERIODO.DTMESDEST,
                CDTIPOPRAT, IDOPCCNFCARD, IDIMPCNFCARD, NRPAGCNFCARD,
                VRPERCOMCARD, NRORDCNFCARD, DSCOMPLTP, IDCONCNFCARD,
                IDTPPRATSEGP, IDTPPRATTERP, IDTPPRATQUAP, IDTPPRATQUIP,
                IDTPPRATSEXP, IDTPPRATSABP, IDTPPRATDOMP, IDTPPRATFERP
                FROM CONFCARD
              WHERE CDFILIAL = CUR_ORIDEST.CDFILIDEST AND
                    CDSERVFILI = CUR_ORIDEST.CDSERVDEST);
            END IF;

            /*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 nÃºmero 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 perÃ­odo
            Permite inclusÃ£o quando: 
            V_IDCARDIAUTIL <> 'S' (NÃƒO BLOQUEIA INCLUSÃƒO EM DIA NÃƒO ÃšTIL) OR
            V_IDCARDIAUTIL = 'S' AND V_CONTFERIFILI = 0 (SE BLOQUEIA, e O DIA Ã‰ 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 distribuição 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'||CUR_ORIDEST.CDFILIDEST, V_NRLOGINTCARD, 1);
                        V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD,11,10);
                     END IF;
                     GRAVA_LOGINTCARD(V_NRLOGINTCARD, CUR_ORIDEST.CDFILIORIG, CUR_ORIDEST.CDFILIDEST, CUR_ORIDEST.CDSERVDEST, CUR_PERIODO.DTMESDEST, ' ', ' ', ' ', '9');
                     CONTINUE;
                  END IF;
                  
                  /*Se cardÃ¡pio DESCENTRALIZADO*/
                  IF (V_TIPOCARDAPIO = 'D') AND (V_IDCARDDESCENTR = 'S') THEN
                     /*Insere na COMPCARDDEL todos os sequenciais de pratos que serÃ£o sobrepostos*/
                     IF P_MODOPROC = 'M' THEN
                        /*Verifica se o cardÃ¡pio esta liberado pela Matriz*/
                        IF V_AUXESP = 'S' THEN
                           IF V_IDMANCORCARDALT = 'S' THEN
                              /*Com o parÃ¢metro marcado insere todos os pratos que estÃ£o no cardÃ¡pio 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                         
                         NOT EXISTS (SELECT CDFILIAL, NRCARDAPIO
                                       FROM RETIRADA
                                      WHERE CDFILIAL = CARDADIA.CDFILIAL AND
                                            NRCARDAPIO = CARDADIA.NRCARDAPIO);
               END IF;

               IF V_IDTIPOSERV = 'N' THEN
                  SELECT COUNT(*)
                  INTO   V_CONTCARDDEST
                  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 COUNT(*)
                  INTO   V_CONTCARDDEST
                  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 estÃ¡ cadastrada no serviÃ§o 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 - InclusÃ£o cardadia e cÃ³pia de comensais */
                  /* Gera novo nÃºmero de cardÃ¡pio se nÃ£o existir */
                  IF V_CONTCARDDEST = 0 THEN
                     V_NRCARDAPIO := NULL;
                     NOVO_CODIGO('CARDAPIO'||V_CDFILIDEST, V_NRCARDAPIO, 1);
                     V_NRCARDAPIO := SUBSTR(V_NRCARDAPIO,11,10);

                     IF P_IDCOPCOMENS = 'S' THEN
                     /* Insere o novo cardÃ¡pio 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 cardÃ¡pio com os comensais do serviÃ§o 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
                        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);
                        /*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 serviÃ§o de origem, entao insere cardacli do origem*/
                              IF P_IDCOPCOMENS = 'S' THEN
                                 /*verifica se todos os pontos de entrega do serviÃ§o origem Ã© 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 serviÃ§o 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'||V_CDFILIDEST, V_NRLOGINTCARD, 1);
                                       V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD,11,10);
                                    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'||V_CDFILIDEST, V_NRLOGINTCARD, 1);
                           V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD,11,10);
                        END IF;
                        /* 1o Caso: CardÃ¡pio seria gravado com comensais = 0 */
                        GRAVA_LOGINTCARD(V_NRLOGINTCARD, CUR_ORIDEST.CDFILIORIG, V_CDFILIDEST, V_CDSERVDEST, CUR_PERIODO.DTDEST, ' ', ' ', ' ',  '0');
                     END IF;
                  ELSE
                     /* Se jÃ¡ existir cardÃ¡pio, insere com o nÃºmero antigo */
                     IF V_IDTIPOSERV = 'N' THEN
                        SELECT MAX(NRCARDAPIO), SUM(QTCOMESTICAR)
                        INTO   V_NRCARDAPIO, C_COMENSSERV_QTCOM
                        FROM   CARDADIA
                        WHERE  CDFILIAL   = V_CDFILIDEST AND
                              CDSERVFILI = V_CDSERVDEST AND
                              DTCARDAPIO = CUR_PERIODO.DTDEST;
                     END IF;
                     IF V_IDTIPOSERV = 'D' THEN
                       SELECT MAX(NRCARDAPIO), SUM(QTCOMESTICAR)
                       INTO   V_NRCARDAPIO, C_COMENSSERV_QTCOM
                       FROM   CARDADIA
                       WHERE  CDFILIAL   = V_CDFILIDEST AND
                              CDSERVFILI = V_CDSERVDEST AND
                              DTCARDAPIO = CUR_PERIODO.DTDEST AND
                              CDDIETPAD  = V_CDDIETDEST;
                     END IF;
                  END IF; /* V_CONTCARDEST = 0 */
                  /*FIM - ETAPA 5 ===== */

                  /* ===== ETAPA 6 - Processamento dos pratos do cardÃ¡pio 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;
                        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' --> NÃƒO PERMITE RECEITA PADRÃƒO NO CARDÃÂPIO */
                        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 HÃÂ 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 nÃ­vel completo*/
                        SELECT MAX(CDNVPRODUTO)
                        INTO V_CDNVPRODUTO
                        FROM PRODUTO;

                        V_PRODINATIVO   := FALSE;
                        V_CDARVPRODINAT :='';

                        IF (V_TIPORECE = 'S') THEN  /*RECEITA POR SERVIÃ‡O 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) GUARDARÃÂ 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) GUARDARÃÂ 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) GUARDARÃÂ 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 SERVIÃ‡O COM PRODUTO INATIVO geovane*/

                        /***********************/

                        IF (V_CONTPRAT <> 0) AND (V_CONTPRODRECE <> 0) AND (V_PRODINATIVO <> TRUE)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 NÃ£o manter configuraÃ§Ã£o (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 NÃ£o manter configuraÃ§Ã£o (adicionando colunas a mais) altera apenas as colunas adicionais */
                                 AND NRSEQCNFMES > V_MAXNRSEQDEST;
                           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; 


                              /*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,
                                       C_PRATOSORIG.VRPECONSCARD + V_VALOR_A_MAIS, C_PRATOSORIG.CDTIPOPRAT, V_NRORDCNFMES, V_PRATOESPE,
                                       V_CDPRATODEL, V_CDFILAUXPRATDEL);  
                           END IF;

                        END IF;

                     END LOOP;
                  ELSE
                     IF V_NRLOGINTCARD IS NULL THEN
                        NOVO_CODIGO('LOGINTCARD'||V_CDFILIDEST, V_NRLOGINTCARD, 1);
                        V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD,11,10);
                     END IF;
                     /* 1o Caso: CardÃ¡pio 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 ===== */

      IF (V_TIPOCARDAPIO = 'D') AND (V_IDCARDDESCENTR = 'S') THEN
         /*Atualiza CARDADIA para especÃ­fico se nela existir algum prato especÃ­fico ou excluÃ­do*/
         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 nÃ£o fazer parte da configuraÃ§Ã£o do cardÃ¡pio e que nÃ£o guardam informaÃ§Ã£o 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 incluÃ­dos 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 configuraÃ§Ã£o de cardÃ¡pio do serviÃ§o 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 perÃ­odo*/
   DELETE FROM LISTA
       WHERE NRLISTA = P_NRLISTAPERIODO;

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

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