create or replace PROCEDURE EXPORTA_CARDAPIO_CONFIG
   (P_CDFILIORIG   IN FILIAL.CDFILIAL%TYPE,
    P_CDSERVORIG   IN VARCHAR2,
    P_DTINIORIG    IN CARDADIA.DTCARDAPIO%TYPE,
    P_DTFINORIG    IN CARDADIA.DTCARDAPIO%TYPE,
    P_CDFILIDEST   IN FILIAL.CDFILIAL%TYPE,
    P_CDSERVDEST   IN VARCHAR2,
    P_DTINIDEST    IN CARDADIA.DTCARDAPIO%TYPE,
    P_DTFINDEST    IN CARDADIA.DTCARDAPIO%TYPE,
    P_CONFIGORIG   IN VARCHAR2,
    P_IDCOPCOMENS  IN VARCHAR2,
    P_IDSOBREPOR   IN VARCHAR2,
    P_CDOPERADOR   IN OPERADOR.CDOPERADOR%TYPE,
    P_MODOPROC     IN VARCHAR2,
    P_NRLOGINTCARD OUT LOGINTCARD.NRLOGINTCARD%TYPE,
    P_IDGERASUBST  IN VARCHAR DEFAULT 'S') IS

    V_COUNT          NUMBER;
    V_LCONFIGORIG    VARCHAR(2000);
    V_CONFIGORIG     VARCHAR(10);
    V_LCDSERVDEST    VARCHAR(2000);
    V_CDSERVDEST     VARCHAR(5);
    V_LCDSERVORIG    VARCHAR(2000);
    V_CDSERVORIG     VARCHAR(5);
    V_DTMESORIG      DATE;
    V_DTMESDEST      DATE;
    V_DTCARDDEST     DATE;
    V_CONTCARDDEST   NUMBER;
    V_NRCARDAPIO     VARCHAR2(20);
    V_CONTPRAT       NUMBER;
    V_CDFILAUXDEST   VARCHAR2(4);
    V_IDCARDIAUTIL   VARCHAR2(1);
    V_MAXNRSEQDEST   NUMBER;
    V_MAXNRORDDEST   NUMBER;
    V_DIASEMANA      NUMBER;
    V_CONTFERIFILI   NUMBER;
    V_CONTFERICARD   NUMBER;    
    V_CONTPRODRECE   NUMBER;
    V_IDUTRPADCA     VARCHAR2(1);
    V_IDTIPOSERV     VARCHAR2(1);
    V_IDCARDDESCENTR VARCHAR2(1);
    V_CDDIETDEST     VARCHAR2(4);
    V_NRLOGINTCARD   VARCHAR2(20);
    V_EXISTPRAT      NUMBER;
    V_NOVOSEQ        NUMBER;
    V_NOVOORD        NUMBER;
    V_ORD            NUMBER;
    V_VALOR_A_MAIS   NUMBER;
    V_PRATOESPE      VARCHAR2(1);
    V_TIPOCARDAPIO   VARCHAR2(1);
    V_NRLISTA        VARCHAR2(20);
    V_CDLISTA        VARCHAR2(50);
    V_DTLIBECARDMATR DATE;
    V_DTAPROCARDFILI DATE;
    V_DTAPROCARDMATR DATE;
    V_DTLIMAPROVFILI DATE;
    V_TIPORECE       VARCHAR2(1);
    V_CDNVPRODUTO    VARCHAR2(1);
    V_PRODINATIVO    BOOLEAN := FALSE;
    V_CDARVPRODINAT  VARCHAR2(20);
    V_AUXESP         VARCHAR2(1);
    V_CONTCOMPCARDDEL NUMBER; /*CONTADOR AUXILIAR PARA SABER SE JA EXISTE REGISTRO NA COMPCARDDEL*/
    V_CDPRATODEL     VARCHAR2(10);
    V_CDFILAUXPRATDEL VARCHAR2(4);
    V_IDMANCORCARDALT VARCHAR2(1);
    V_VRCUSTPADR      FLOAT(126);
    V_IDCUSPADCAR    VARCHAR2(1);
    V_EXICNFCAMES    VARCHAR2(2);
    V_NRORDCNFMES    NUMBER;
    V_NRSEQCNFMES    NUMBER;
    V_IDSERVTRAN     VARCHAR2(1);
    V_EXISCARDACLI   NUMBER;
    V_PTOENTREGA     NUMBER;
    V_IDATIVO VARCHAR2(1);
    V_EXISTDIST     NUMBER;
    /* Variaveis para cursores */

    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);

    /* Cursores */
    CURSOR C_DATAS IS
    SELECT DTDATA
    FROM   DATA
    WHERE  DTDATA BETWEEN P_DTINIORIG AND P_DTFINORIG
    ORDER BY DTDATA;

    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 = P_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 = P_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 = P_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 = P_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 = P_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     = P_CDFILIDEST AND
            CDSERVFILI   = V_CDSERVDEST AND
            S.CDDIETPAD  = V_CDDIETDEST AND
            P.CDDIETPAD  = S.CDDIETPAD  AND
            P.IDTIPODIET = 'S';

BEGIN
   V_LCDSERVDEST := P_CDSERVDEST;
   V_LCDSERVORIG := P_CDSERVORIG;
   V_LCONFIGORIG := P_CONFIGORIG;
   V_NRLOGINTCARD := NULL;

   IF SUBSTR(V_LCDSERVORIG, 1, 1) = '_' THEN
      V_LCDSERVORIG := SUBSTR(V_LCDSERVORIG, 2, LENGTH(V_LCDSERVORIG));
   END IF;

   IF SUBSTR(V_LCDSERVDEST, 1, 1) = '_' THEN
      V_LCDSERVDEST := SUBSTR(V_LCDSERVDEST, 2, LENGTH(V_LCDSERVDEST));
   END IF;

   IF SUBSTR(V_LCONFIGORIG, 1, 1) = '_' THEN
      V_LCONFIGORIG := SUBSTR(V_LCONFIGORIG, 2, LENGTH(V_LCONFIGORIG));
   END IF;

   WHILE ( ( NOT V_LCDSERVDEST IS NULL ) AND ( NOT V_LCDSERVORIG IS NULL ) AND ( NOT V_LCONFIGORIG IS NULL ) )
   LOOP /* Loop para Exportar mais de um Servico */
   /* Atribui valores de lista nas devidas variaveis */
      V_COUNT := 1;
      IF LENGTH(V_LCDSERVORIG) > 5 THEN
         LOOP
            EXIT WHEN SUBSTR(V_LCDSERVORIG, V_COUNT, 1)= '_';
            V_COUNT := V_COUNT + 1 ;
         END LOOP;
         V_CDSERVORIG := SUBSTR(V_LCDSERVORIG, 1, V_COUNT-1);   
      ELSE
         V_CDSERVORIG := V_LCDSERVORIG;
      END IF;


      V_COUNT := 1;
      IF LENGTH (V_LCDSERVDEST) > 5 THEN
         LOOP
            EXIT WHEN SUBSTR(V_LCDSERVDEST, V_COUNT, 1)= '_';
            V_COUNT := V_COUNT + 1 ;
         END LOOP;
         V_CDSERVDEST := SUBSTR(V_LCDSERVDEST, 1, V_COUNT-1);
      ELSE
         V_CDSERVDEST := V_LCDSERVDEST;
      END IF;
      
      
      IF V_LCONFIGORIG = 'T' THEN  
         NOVO_CODIGO('LISTA', V_LCONFIGORIG, 1);
         V_CONFIGORIG := SUBSTR(V_LCONFIGORIG, 11,10);
         FOR C_CARDIA IN (
            SELECT LPAD(NRSEQCNFMES, 3, '0') AS  NRSEQCNFMES
              FROM CNFCAMES
             WHERE CDFILIAL     = P_CDFILIORIG
               AND CDSERVFILI   = V_CDSERVORIG
               AND DTREFECNFMES = LAST_DAY(ADD_MONTHS(P_DTINIORIG,-1))+1) LOOP
               
               INSERT INTO LISTA (NRLISTA, CDLISTA)
               VALUES (V_CONFIGORIG, C_CARDIA.NRSEQCNFMES);
            END LOOP;
      ELSE
         V_COUNT := 1;
         LOOP
            EXIT WHEN SUBSTR(V_LCONFIGORIG, V_COUNT, 1)= '_';
            V_COUNT := V_COUNT + 1 ;
         END LOOP;

         V_CONFIGORIG := SUBSTR(V_LCONFIGORIG, 1, V_COUNT-1);
      END IF;
    
     /* Fim de atribuicao de valores de lista */

      /* Grava o primeiro dia do mes de origem e destino */
      V_DTMESORIG := LAST_DAY(ADD_MONTHS(P_DTINIORIG,-1))+1;
      V_DTMESDEST := LAST_DAY(ADD_MONTHS(P_DTINIDEST,-1))+1;

      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;

      SELECT IDTIPOSERV
      INTO   V_IDTIPOSERV
      FROM   SERVICO
      WHERE  CDFILIAL   = P_CDFILIORIG AND
             CDSERVFILI = V_CDSERVORIG;

      SELECT NVL(IDCARDDESCENTR, 'N'), NVL(IDSERVTRAN,'N')
      INTO   V_IDCARDDESCENTR, V_IDSERVTRAN
      FROM   SERVICO
      WHERE  CDFILIAL   = P_CDFILIDEST AND
             CDSERVFILI = V_CDSERVDEST;
      
      /* Verifica se o parametro "Verifica custo padrao na Montagem do Cardapio" esta marcado */
      SELECT MAX(IDCUSPADCAR)
        INTO V_IDCUSPADCAR
        FROM PARAMPLANEJA
       WHERE CDFILIAL = P_CDFILIDEST;

      IF V_IDCUSPADCAR = 'S' THEN
         /* Verifica se existe custo padrao para o servico de destino */
         SELECT MAX(VRCUSTPADR) AS VRCUSTPADR
           INTO V_VRCUSTPADR
           FROM CUSTPADR
          WHERE (CDFILIAL   = P_CDFILIDEST) 
            AND (CDSERVFILI = V_CDSERVDEST)
            AND (DTREFCUSPADR = V_DTMESDEST);    
      
         IF V_VRCUSTPADR IS NULL THEN
            IF V_NRLOGINTCARD IS NULL THEN
               NOVO_CODIGO('LOGINTCARD'||P_CDFILIDEST, V_NRLOGINTCARD, 1);
               V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD,11,10);
            END IF;
            GRAVA_LOGINTCARD(V_NRLOGINTCARD, P_CDFILIORIG, P_CDFILIDEST, V_CDSERVDEST, V_DTMESDEST, ' ', ' ', ' ', '4');
         END IF;       
      END IF;
       
      /*Gera novo codigo para a tabela LISTA*/
      /*(P_IDSOBREPOR = 'S') Executado pelo Assistente PLA05900*/
     /* IF (V_TIPOCARDAPIO = 'D') AND (P_MODOPROC = 'M') AND (P_IDSOBREPOR = 'S') AND (V_IDCARDDESCENTR = 'S') THEN
         V_NRLISTA := NULL;
         NOVO_CODIGO('LISTA', V_NRLISTA, 1);
         V_NRLISTA := SUBSTR(V_NRLISTA, 11, 10);
      END IF; */

      IF (V_TIPOCARDAPIO = 'D') AND (P_MODOPROC = 'M') AND (V_IDCARDDESCENTR = 'S') THEN
         BEGIN
            SELECT 'S'
              INTO V_AUXESP
              FROM CTRLAPROVCARD
             WHERE CDFILIAL = P_CDFILIDEST
               AND CDSERVFILI = V_CDSERVDEST
               AND TO_CHAR(DTMESCARDAPROV, 'MM/YYYY') = TO_CHAR(P_DTINIDEST, 'MM/YYYY')
               AND DTLIBECARDMATR IS NOT NULL;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
            V_PRATOESPE := 'N';
         END;
      END IF;

      IF (V_TIPOCARDAPIO = 'D') AND (P_MODOPROC = 'F') AND (V_IDCARDDESCENTR = 'S') 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       = P_CDFILIDEST AND
                   CTRL.CDSERVFILI     = V_CDSERVDEST AND
                   CTRL.DTMESCARDAPROV = V_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;
      
      /*VERIFICA SE EXISTE CONFIGURACAO DE CARDAPIO PARA O MES*/
      SELECT COUNT(*) 
        INTO V_EXICNFCAMES
        FROM CNFCAMES
       WHERE CDFILIAL     = P_CDFILIDEST AND
             CDSERVFILI   = V_CDSERVDEST AND
             DTREFECNFMES = V_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, V_DTMESDEST,
          CDTIPOPRAT, IDOPCCNFCARD, IDIMPCNFCARD, NRPAGCNFCARD,
          VRPERCOMCARD, NRORDCNFCARD, DSCOMPLTP, IDCONCNFCARD,
          IDTPPRATSEGP, IDTPPRATTERP, IDTPPRATQUAP, IDTPPRATQUIP,
          IDTPPRATSEXP, IDTPPRATSABP, IDTPPRATDOMP, IDTPPRATFERP, IDRESERVAMYMENU
          FROM CONFCARD
        WHERE CDFILIAL     = P_CDFILIDEST AND
              CDSERVFILI   = V_CDSERVDEST);
      END IF;  
           
      /* Opcao sobrepor */
      IF P_IDSOBREPOR = 'S' THEN 
      
         --IF (V_TIPOCARDAPIO = 'D') AND (V_IDCARDDESCENTR = 'S') THEN
            /*Inserer na COMPCARDDEL todos os pratos que eram excluidos da filial*/
            --IF P_MODOPROC = 'M' THEN
               /*Verifica se o cardapio esta liberado pela Matriz*/
               --IF V_AUXESP = 'S' THEN
                  /* se o cardapio estiver liberado pela matriz nao faz o filtro de local de exclusao*/
               /*   INSERT INTO LISTA
                     (NRLISTA, CDLISTA)
                  (SELECT V_NRLISTA, TO_CHAR(DTPRATEXCL, 'dd')||LPAD(NRSEQPRATEXCL, 3, '0')
                   FROM   COMPCARDDEL
                   WHERE  CDFILIAL   = P_CDFILIDEST AND
                          CDSERVFILI = V_CDSERVDEST AND
                          DTPRATEXCL BETWEEN V_DTMESDEST AND LAST_DAY(V_DTMESDEST));
               ELSE
                  INSERT INTO LISTA
                     (NRLISTA, CDLISTA)
                  (SELECT V_NRLISTA, TO_CHAR(DTPRATEXCL, 'dd')||LPAD(NRSEQPRATEXCL, 3, '0')
                   FROM   COMPCARDDEL
                   WHERE  CDFILIAL   = P_CDFILIDEST AND
                          CDSERVFILI = V_CDSERVDEST AND
                          DTPRATEXCL BETWEEN V_DTMESDEST AND LAST_DAY(V_DTMESDEST) AND
                          IDLOCALEXCL = 'F');
               END IF;
            END IF;
         END IF; */

         /* Pratos e Cardapios */
         FOR C_CARDDEL IN
            (SELECT CDFILIAL, CDSERVFILI, DTCARDAPIO, NRCARDAPIO
             FROM   CARDADIA
             WHERE  CDFILIAL   = P_CDFILIDEST AND
                    CDSERVFILI = V_CDSERVDEST AND 
                    DTCARDAPIO BETWEEN P_DTINIDEST AND P_DTFINDEST)
         LOOP

            /* Verifica se existe distribuicao de cardapio transportado no dia*/
            BEGIN
               SELECT COUNT(*)
                 INTO V_EXISTDIST
                 FROM DISTCARD D
                WHERE D.CDFILIAL = P_CDFILIDEST AND
                      D.NRCARDAPIO = C_CARDDEL.NRCARDAPIO;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_EXISTDIST := 0;
            END;

            IF (V_EXISTDIST > 0) THEN
               IF V_NRLOGINTCARD IS NULL THEN
                  NOVO_CODIGO('LOGINTCARD'||P_CDFILIDEST, V_NRLOGINTCARD, 1);
                  V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD,11,10);
               END IF;
               GRAVA_LOGINTCARD(V_NRLOGINTCARD, P_CDFILIORIG, P_CDFILIDEST, V_CDSERVDEST, C_CARDDEL.DTCARDAPIO, ' ', ' ', ' ', '9');
               CONTINUE;
            END IF;

            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 C_CARDDEL.CDFILIAL AS CDFILIAL, C_CARDDEL.CDSERVFILI AS CDSERVFILI, C_CARDDEL.DTCARDAPIO AS DTCARDAPIO,
                                   C.NRSEQPRATCAR, P_MODOPROC AS IDLOCALEXCL, C.CDPRATO, C.CDFILAUXPRAT
                              FROM COMPCARD C
                             WHERE C.CDFILIAL = C_CARDDEL.CDFILIAL
                               AND C.NRCARDAPIO = C_CARDDEL.NRCARDAPIO
                               AND (C.CDPRATOMAT IS NULL)
                               AND (C.CDFILAUXPRATMAT IS NULL)
                               AND (C.IDPRATOESPE = 'N' )
                             UNION
                            SELECT C_CARDDEL.CDFILIAL, C_CARDDEL.CDSERVFILI, C_CARDDEL.DTCARDAPIO,
                                   C.NRSEQPRATCAR, P_MODOPROC, C.CDPRATOMAT, C.CDFILAUXPRATMAT
                              FROM COMPCARD C
                             WHERE C.CDFILIAL = C_CARDDEL.CDFILIAL
                               AND C.NRCARDAPIO = C_CARDDEL.NRCARDAPIO
                               AND (C.IDPRATOESPE <> 'N' )) 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 C_CARDDEL.CDFILIAL AS CDFILIAL, C_CARDDEL.CDSERVFILI AS CDSERVFILI, C_CARDDEL.DTCARDAPIO AS DTCARDAPIO,
                                   C.NRSEQPRATCAR, P_MODOPROC AS IDLOCALEXCL, C.CDPRATO, C.CDFILAUXPRAT
                              FROM COMPCARD C
                             WHERE C.CDFILIAL = C_CARDDEL.CDFILIAL
                               AND C.NRCARDAPIO = C_CARDDEL.NRCARDAPIO
                               AND (C.CDPRATOMAT IS NULL)
                               AND (C.CDFILAUXPRATMAT IS NULL)
                               AND (C.IDPRATOESPE = 'N' )
                             UNION
                            SELECT C_CARDDEL.CDFILIAL, C_CARDDEL.CDSERVFILI, C_CARDDEL.DTCARDAPIO,
                                   C.NRSEQPRATCAR, P_MODOPROC, C.CDPRATOMAT, C.CDFILAUXPRATMAT
                              FROM COMPCARD C
                             WHERE C.CDFILIAL = C_CARDDEL.CDFILIAL
                               AND C.NRCARDAPIO = C_CARDDEL.NRCARDAPIO
                               AND NOT(C.CDPRATOMAT IS NULL)
                               AND NOT(C.CDFILAUXPRATMAT IS NULL)) 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;
               ELSE
                  INSERT INTO COMPCARDDEL
                     (CDFILIAL, CDSERVFILI, DTPRATEXCL, NRSEQPRATEXCL, IDLOCALEXCL, CDPRATO, CDFILAUXPRAT)
                  (SELECT C_CARDDEL.CDFILIAL, C_CARDDEL.CDSERVFILI, C_CARDDEL.DTCARDAPIO,
                          C.NRSEQPRATCAR, P_MODOPROC, C.CDPRATO, C.CDFILAUXPRAT
                     FROM COMPCARD C
                    WHERE C.CDFILIAL = C_CARDDEL.CDFILIAL
                      AND C.NRCARDAPIO = C_CARDDEL.NRCARDAPIO
                      AND (C.CDPRATOMAT IS NULL)
                      AND (C.CDFILAUXPRATMAT IS NULL)
                      AND (C.IDPRATOESPE = 'N' )
                      AND NOT EXISTS (SELECT CD.NRSEQPRATEXCL
                                        FROM COMPCARDDEL CD
                                       WHERE CD.CDFILIAL = C_CARDDEL.CDFILIAL
                                         AND CD.CDSERVFILI = C_CARDDEL.CDSERVFILI
                                         AND CD.DTPRATEXCL = C_CARDDEL.DTCARDAPIO
                                         AND CD.CDFILIAL = C.CDFILIAL
                                         AND CD.NRSEQPRATEXCL = C.NRSEQPRATCAR)
                   UNION
                   SELECT C_CARDDEL.CDFILIAL, C_CARDDEL.CDSERVFILI, C_CARDDEL.DTCARDAPIO,
                          C.NRSEQPRATCAR, P_MODOPROC, C.CDPRATOMAT, C.CDFILAUXPRATMAT
                     FROM COMPCARD C
                    WHERE C.CDFILIAL = C_CARDDEL.CDFILIAL
                      AND C.NRCARDAPIO = C_CARDDEL.NRCARDAPIO
                      AND NOT(C.CDPRATOMAT IS NULL)
                      AND NOT(C.CDFILAUXPRATMAT IS NULL)
                      AND NOT EXISTS (SELECT CD.NRSEQPRATEXCL
                                        FROM COMPCARDDEL CD
                                       WHERE CD.CDFILIAL = C_CARDDEL.CDFILIAL
                                         AND CD.CDSERVFILI = C_CARDDEL.CDSERVFILI
                                         AND CD.DTPRATEXCL = C_CARDDEL.DTCARDAPIO
                                         AND CD.CDFILIAL = C.CDFILIAL
                                         AND CD.NRSEQPRATEXCL = C.NRSEQPRATCAR));
               END IF;

               /*Insere na tabela LISTA todos os pratos ESPECIFICOS e REPROVADOS*/
               --IF P_MODOPROC = 'M' THEN
                --  IF V_AUXESP = 'S' THEN
                     /*Insere LISTA para o dia seguinte*/
                  /*   INSERT INTO LISTA
                        (NRLISTA, CDLISTA)
                     (SELECT V_NRLISTA,  TO_CHAR(C_CARDDEL.DTCARDAPIO, 'dd')||LPAD(NRSEQPRATCAR, 3, '0')
                      FROM   COMPCARD
                      WHERE  CDFILIAL   = C_CARDDEL.CDFILIAL   AND
                             NRCARDAPIO = C_CARDDEL.NRCARDAPIO);
                  ELSE
                     INSERT INTO LISTA
                        (NRLISTA, CDLISTA)
                     (SELECT V_NRLISTA,  TO_CHAR(C_CARDDEL.DTCARDAPIO, 'dd')||LPAD(NRSEQPRATCAR, 3, '0')
                      FROM   COMPCARD
                      WHERE  CDFILIAL   = C_CARDDEL.CDFILIAL   AND
                             NRCARDAPIO = C_CARDDEL.NRCARDAPIO AND
                             IDPRATOESPE IN ('S', 'R'));
                  END IF;
               END IF; */
            END IF;

            DELETE FROM COMPCARD
            WHERE CDFILIAL = C_CARDDEL.CDFILIAL AND
                  NRCARDAPIO = C_CARDDEL.NRCARDAPIO;

            DELETE FROM CARDADIA
            WHERE CDFILIAL = C_CARDDEL.CDFILIAL AND
                  NRCARDAPIO = C_CARDDEL.NRCARDAPIO AND
                  NOT EXISTS (SELECT CDFILIAL, NRCARDAPIO
                              FROM   RETIRADA
                              WHERE  CDFILIAL = CARDADIA.CDFILIAL AND
                                     NRCARDAPIO = CARDADIA.NRCARDAPIO);
         END LOOP;
      END IF;
      
      /*OPCAO ACRESCENTAR MARCADO
      IF P_IDSOBREPOR = 'N' THEN
      
      END IF;*/


      V_DTCARDDEST := P_DTINIDEST;

      /* Verifica o parametro que permite cardapio em dia nao-util */
      SELECT NVL(MAX(IDCARDIAUTIL), 'N')
      INTO   V_IDCARDIAUTIL
      FROM   PARAMPLANEJA
      WHERE  CDFILIAL = P_CDFILIDEST;

      
      OPEN C_DATAS;
      LOOP
         FETCH C_DATAS INTO C_DATAS_DTORIGEM;
         EXIT WHEN C_DATAS%NOTFOUND;
      
         SELECT COUNT(*)
         INTO   V_CONTFERIFILI
         FROM   FERIFILI
         WHERE  CDFILIAL = P_CDFILIDEST AND
                DTFERIFILI = V_DTCARDDEST;

         SELECT COUNT(*)
         INTO   V_CONTFERICARD
         FROM   FERICARD
         WHERE  CDFILIAL = P_CDFILIDEST AND
                DTFERICARD = V_DTCARDDEST;

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

         /* Permite inclusao quando: */
         /* V_IDCARDIAUTIL <> 'S' (NAO BLOQUEIA INCLUSAO EM DIA NAO UTIL) OR
            V_IDCARDIAUTIL = 'S' AND V_CONTFERIFILI = 0 (SE BLOQUEIA, O DIA E NORMAL) */
         IF (V_DTCARDDEST <= P_DTFINDEST) AND
            ((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   = P_CDFILIORIG AND
                      CDSERVFILI = V_CDSERVORIG AND
                      DTCARDAPIO = C_DATAS_DTORIGEM
               ORDER BY CDFILIAL, CDSERVFILI, DTCARDAPIO, CDDIETPAD)
            LOOP
               IF V_IDTIPOSERV = 'N' THEN
                  SELECT COUNT(*)
                  INTO   V_CONTCARDDEST
                  FROM   CARDADIA
                  WHERE  CDFILIAL   = P_CDFILIDEST AND
                         CDSERVFILI = V_CDSERVDEST AND
                         DTCARDAPIO = V_DTCARDDEST;
               END IF;
               IF V_IDTIPOSERV = 'D' THEN
                  SELECT COUNT(*)
                  INTO   V_CONTCARDDEST
                  FROM   CARDADIA
                  WHERE  CDFILIAL   = P_CDFILIDEST AND
                         CDSERVFILI = V_CDSERVDEST AND
                         DTCARDAPIO = V_DTCARDDEST AND
                         CDDIETPAD  = C_CARDORIG.CDDIETPAD;
               END IF;
               V_CDDIETDEST := 'XXXX';

               /* 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 = P_CDFILIDEST AND
                      CDSERVFILI = V_CDSERVDEST AND
                      S.CDDIETPAD = C_CARDORIG.CDDIETPAD AND
                      P.CDDIETPAD = S.CDDIETPAD AND
                      P.IDTIPODIET = 'S';

               IF (V_IDTIPOSERV = 'N') OR (V_IDTIPOSERV = 'D' AND (V_CDDIETDEST <> 'XXXX')) THEN
               /* Gera novo numero de cardapio se nao existir */
                  IF V_CONTCARDDEST = 0 THEN
                     V_NRCARDAPIO := NULL;
                     NOVO_CODIGO('CARDAPIO'||P_CDFILIDEST, V_NRCARDAPIO, 1);
                     V_NRCARDAPIO := SUBSTR(V_NRCARDAPIO,11,10);

                     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
                        INSERT INTO CARDADIA
                          (CDFILIAL, CDSERVFILI, DTCARDAPIO, NRCARDAPIO,
                           CDDIETPAD, DTMESCARD, CDOPERADOR, DSCARDAPIO, DTULTATUACAR,
                           QTCOMESTICAR, QTCLIESTICAR, QTFUNESTICAR, QTDOAESTICAR,
                           QTESTITURNO1, QTESTITURNO2, QTESTITURNO3, QTESTITURNO4)
                        VALUES
                           (P_CDFILIDEST, V_CDSERVDEST, V_DTCARDDEST, V_NRCARDAPIO,
                            C_CARDORIG.CDDIETPAD, V_DTMESDEST, 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   = P_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   = P_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   = P_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 P_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'||P_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, P_CDFILIORIG, P_CDFILIDEST, V_CDSERVDEST, V_DTCARDDEST, ' ', ' ', ' ',  '6');                                 
                                 END IF;
                                 
                              ELSE /*insere na cardacli os pontos de entrega do servico de destino*/
                                 INSERT INTO CARDACLI (CDFILIAL, NRCARDAPIO, CDCLIENTE, CDDESTFILI, QTCOMESTICLI)
                                 (SELECT P_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   = P_CDFILIDEST)   AND
                                         (CDSERVFILI = V_CDSERVDEST));  
                              END IF;
                           END IF;   
                        END IF;
                     ELSE
                        IF V_NRLOGINTCARD IS NULL THEN
                           NOVO_CODIGO('LOGINTCARD'||P_CDFILIDEST, V_NRLOGINTCARD, 1);
                           V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD,11,10);
                        END IF;
                        /* 1o Caso: Cardapio seria gravado com comensais = 0 */
                        GRAVA_LOGINTCARD(V_NRLOGINTCARD, P_CDFILIORIG, P_CDFILIDEST, V_CDSERVDEST, V_DTCARDDEST, ' ', ' ', ' ',  '0');
                     END IF;
                  ELSE
                  /* Se ja existir cardapio, insere com o numero antigo */
                     IF V_IDTIPOSERV = 'N' THEN
                        SELECT MAX(NRCARDAPIO), SUM(QTCOMESTICAR)
                        INTO   V_NRCARDAPIO, C_COMENSSERV_QTCOM
                        FROM   CARDADIA
                        WHERE  CDFILIAL   = P_CDFILIDEST AND
                               CDSERVFILI = V_CDSERVDEST AND
                               DTCARDAPIO = V_DTCARDDEST;
                     END IF;
                     IF V_IDTIPOSERV = 'D' THEN
                        SELECT MAX(NRCARDAPIO), SUM(QTCOMESTICAR)
                        INTO   V_NRCARDAPIO, C_COMENSSERV_QTCOM
                        FROM   CARDADIA
                        WHERE  CDFILIAL   = P_CDFILIDEST AND
                               CDSERVFILI = V_CDSERVDEST AND
                               DTCARDAPIO = V_DTCARDDEST AND
                               CDDIETPAD  = V_CDDIETDEST;
                     END IF;
                  END IF; /* V_CONTCARDEST = 0 */
              
                  IF (C_COMENSSERV_QTCOM <> 0) AND (NOT C_COMENSSERV_QTCOM IS NULL) THEN
                  /* INSERT NA COMPCARD.... */
                     V_NOVOSEQ := 0;
                     V_NOVOORD := 0;
                     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   = P_CDFILIDEST
                                                                               AND L.CDSERVFILI = V_CDSERVDEST
                                                                               AND P_IDGERASUBST = 'S'
                                                                             GROUP BY L.CDFILIAL, L.CDSERVFILI, L.CDFILAUXPRATORIG, L.CDPRATOORIG
                                                                             ) LIS
                         WHERE  F.CDFILIAL     = P_CDFILIORIG AND
                                F.CDSERVFILI   = V_CDSERVORIG AND
                                F.DTREFECNFMES = V_DTMESORIG  AND
                                L.NRLISTA      = V_CONFIGORIG 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
                        V_NOVOSEQ := V_NOVOSEQ + 1;
                        V_NOVOORD := V_NOVOORD + 1;

                        IF C_PRATOSORIG.CDPRATO <> '' THEN
                           V_CDFILAUXDEST := C_PRATOSORIG.CDFILAUXPRAT;
                           IF C_PRATOSORIG.CDFILAUXPRAT <> 'P' THEN
                              V_CDFILAUXDEST := P_CDFILIDEST;
                           END IF;

                           /* Verifica se o prato existe na filial destino */
                           V_CONTPRAT  := 1;
                           V_EXISTPRAT := 1;
                           IF P_CDFILIDEST <> P_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), 'AEIOUAEIOUAEIOUAEEIOEUEAONC', 'AEIOUAEIOUAEIOUAEIOUAONC') =
                                        TRANSLATE(UPPER(C_PRATOSORIG.NMPRATO), 'AEIOUAEIOUAEIOUAEEIOEUEAONC', '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 = P_CDFILIDEST
                                     UNION ALL
                                     SELECT COUNT(*) NUM
                                     FROM   RECESERV
                                     WHERE  CDFILAUXPRAT = V_CDFILAUXDEST  AND
                                            CDPRATO = C_PRATOSORIG.CDPRATO AND
                                            CDFILIAL = P_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 = P_CDFILIDEST
                                    UNION ALL
                                    SELECT COUNT(*) NUM
                                    FROM   RECESERV
                                    WHERE  CDFILAUXPRAT = V_CDFILAUXDEST AND
                                           CDPRATO = C_PRATOSORIG.CDPRATO AND
                                           CDFILIAL = P_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     = P_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     = P_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     = P_CDFILIDEST)    AND
                                                            (CDPRATO      = P.CDPRATO)       AND
                                                            (CDFILAUXPRAT = P.CDFILAUXPRAT)) AND
                                         NOT EXISTS (SELECT CDPRATO
                                                     FROM   RECESERV
                                                     WHERE  (CDFILIAL     = P_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 (+) = P_CDFILIDEST           AND
                                        P.CDNVPRODUTO   = V_CDNVPRODUTO          AND
                                        RS.CDFILIAL     = P_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 (+) = P_CDFILIDEST         AND
                                           P.CDNVPRODUTO   = V_CDNVPRODUTO        AND
                                           RF.CDFILIAL     = P_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 PADRAO 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
                                              P_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 PADRAO COM PRODUTO INATIVO*/
                             END IF;  /*RECEITA LOCAL COM PRODUTO INATIVO geovane*/
                           END IF; /*RECEITA POR SERVICO COM PRODUTO INATIVO geovane*/

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

                           /* FIM - VERIFICAR SE HA PRODUTO INATIVO NO PRATO */

                           V_IDATIVO := PRATO_RECEITA_ATIVA(C_PRATOSORIG.CDPRATO, V_CDFILAUXDEST, P_CDFILIDEST, V_CDSERVDEST, V_TIPORECE); /* VALIDANDO PRATO RECEITA UTILZIADA ATIVA */

                           IF (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   = P_CDFILIORIG AND
                                             C.NRCARDAPIO = C_CARDORIG.NRCARDAPIO AND
                                             NOT EXISTS (SELECT L.CDLISTA
                                                         FROM   LISTA L
                                                         WHERE  L.NRLISTA = V_CONFIGORIG 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     = P_CDFILIORIG AND
                                             C.CDSERVFILI   = V_CDSERVORIG AND
                                             C.DTREFECNFMES = V_DTMESORIG  AND
                                             L.NRLISTA      = V_CONFIGORIG 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;

                              /*Verifica se existia um prato REPROVADO ou ESPECIFICO neste sequencial
                                a ser incluido*/
                              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 = P_CDFILIDEST
                                             AND CDSERVFILI = V_CDSERVDEST
                                             AND DTPRATEXCL = V_DTCARDDEST
                                             AND NRSEQPRATEXCL = V_NOVOSEQ;
                                       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 = P_CDFILIDEST
                                          AND CDSERVFILI = V_CDSERVDEST
                                          AND DTPRATEXCL = V_DTCARDDEST
                                          AND NRSEQPRATEXCL = V_NOVOSEQ;
                                    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;        

                              /*INSERT COMPCARD*/
                              
                              SELECT MIN(NRORDCNFMES)
                                INTO V_NRORDCNFMES
                                FROM CNFCAMES CF
                               WHERE CF.CDFILIAL     = P_CDFILIDEST
                                 AND CF.CDSERVFILI   = V_CDSERVDEST
                                 AND CF.DTREFECNFMES = V_DTMESDEST
                                 AND CF.CDTIPOPRAT   = C_PRATOSORIG.CDTIPOPRAT
                                 AND NOT EXISTS (SELECT NRORDPRATCAR FROM COMPCARD
                                                  WHERE CDFILIAL     = P_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     = P_CDFILIDEST
                                    AND CDSERVFILI   = V_CDSERVDEST
                                    AND DTREFECNFMES = V_DTMESDEST
                                    AND CDTIPOPRAT   = C_PRATOSORIG.CDTIPOPRAT
                                    AND NRORDCNFMES  = V_NRORDCNFMES;
                        

                                 IF NOT V_NRSEQCNFMES IS NULL THEN    
                                     /*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 (P_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;

                           ELSE
                           /* LOG DE RESTRICOES DA EXPORTACAO */
                              IF V_NRLOGINTCARD IS NULL THEN
                                 NOVO_CODIGO('LOGINTCARD'||P_CDFILIDEST, V_NRLOGINTCARD, 1);
                                 V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD,11,10);
                              END IF;
                              /* 1o Caso: Prato nao existe no destino */
                              IF V_EXISTPRAT = 0 THEN
                                 GRAVA_LOGINTCARD(V_NRLOGINTCARD, P_CDFILIORIG, P_CDFILIDEST, V_CDSERVDEST, V_DTCARDDEST, 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, P_CDFILIORIG, P_CDFILIDEST, V_CDSERVDEST, V_DTCARDDEST, C_PRATOSORIG.CDPRATO, C_PRATOSORIG.CDFILAUXPRAT, V_CDFILAUXDEST, '2');
                              ELSIF
                              /* 3o Caso: Prato no destino mudaria para status padrao ou sem receita */
                              V_CONTPRODRECE = 0 THEN
                                 GRAVA_LOGINTCARD(V_NRLOGINTCARD, P_CDFILIORIG, P_CDFILIDEST, V_CDSERVDEST, V_DTCARDDEST, 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, P_CDFILIORIG, P_CDFILIDEST, V_CDSERVDEST, V_DTCARDDEST, C_PRATOSORIG.CDPRATO, C_PRATOSORIG.CDFILAUXPRAT, NULL, NULL);
                              ELSIF
                                /* 5 Caso: Prato ou receita utilizada inativa */
                              V_IDATIVO = 'N' THEN
                                 GRAVA_LOGINTCARD(V_NRLOGINTCARD, P_CDFILIORIG, P_CDFILIDEST, V_CDSERVDEST, V_DTCARDDEST, C_PRATOSORIG.CDPRATO, C_PRATOSORIG.CDFILAUXPRAT, NULL, '7');
                              END IF;
                           END IF;
                        END IF;
                     END LOOP; /* C_PRATOSORIG */
                  ELSE
                     IF V_NRLOGINTCARD IS NULL THEN
                        NOVO_CODIGO('LOGINTCARD'||P_CDFILIDEST, V_NRLOGINTCARD, 1);
                        V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD,11,10);
                     END IF;
                     /* 1o Caso: Cardapio seria gravado com comensais = 0 */
                     GRAVA_LOGINTCARD(V_NRLOGINTCARD, P_CDFILIORIG, P_CDFILIDEST, V_CDSERVDEST, V_DTCARDDEST, ' ', ' ', ' ', '0');
                  END IF;
               END IF;               
            END LOOP; /* C_CARDORIG */
         END IF; /* V_DTCARDDEST */
         V_DTCARDDEST := V_DTCARDDEST + 1;
      END LOOP; /* C_DATAS */
      CLOSE C_DATAS;
      
      
      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   = P_CDFILIDEST AND
                C.CDSERVFILI = V_CDSERVDEST AND
                C.DTMESCARD  = V_DTMESDEST  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 = P_CDFILIDEST
            AND CDSERVFILI = V_CDSERVDEST
            AND DTCARDAPIO BETWEEN P_DTINIDEST AND P_DTFINDEST
            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 = P_CDFILIDEST
               AND CDSERVFILI = V_CDSERVDEST
               AND DTPRATEXCL BETWEEN P_DTINIDEST AND P_DTFINDEST;
         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 = P_CDFILIDEST
               AND CDSERVFILI = V_CDSERVDEST
               AND DTPRATEXCL BETWEEN P_DTINIDEST AND P_DTFINDEST
               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 = P_CDFILIDEST
                              AND CARDADIA.CDSERVFILI = V_CDSERVDEST
                              AND CARDADIA.DTCARDAPIO BETWEEN P_DTINIDEST AND P_DTFINDEST
                              AND COMPCARDDEL.CDFILIAL = CARDADIA.CDFILIAL
                              AND COMPCARDDEL.CDSERVFILI = CARDADIA.CDSERVFILI
                              AND COMPCARDDEL.DTPRATEXCL = CARDADIA.DTCARDAPIO
                              AND COMPCARDDEL.NRSEQPRATEXCL = COMPCARD.NRSEQPRATCAR) ;
         END IF;
                               
         /*Deleta a tabela LISTA*/
        -- DELETE FROM LISTA
        -- WHERE NRLISTA = V_NRLISTA;
      END IF;
      
      DELETE FROM LISTA
      WHERE NRLISTA = V_CONFIGORIG;
      
      COMMIT;
      P_NRLOGINTCARD := NVL(V_NRLOGINTCARD, '');

      V_LCDSERVORIG := SUBSTR(V_LCDSERVORIG, LENGTH(V_CDSERVORIG)+1, LENGTH(V_LCDSERVORIG));
      V_LCDSERVDEST := SUBSTR(V_LCDSERVDEST, LENGTH(V_CDSERVDEST)+1, LENGTH(V_LCDSERVDEST));
      V_LCONFIGORIG := SUBSTR(V_LCONFIGORIG, LENGTH(V_CONFIGORIG)+1, LENGTH(V_LCONFIGORIG));

      IF SUBSTR(V_LCDSERVORIG, 1, 1) = '_' THEN
         V_LCDSERVORIG := SUBSTR(V_LCDSERVORIG, 2, LENGTH(V_LCDSERVORIG));
      END IF;

      IF SUBSTR(V_LCDSERVDEST, 1, 1) = '_' THEN
         V_LCDSERVDEST := SUBSTR(V_LCDSERVDEST, 2, LENGTH(V_LCDSERVDEST));
      END IF;

      IF SUBSTR(V_LCONFIGORIG, 1, 1) = '_' THEN
         V_LCONFIGORIG := SUBSTR(V_LCONFIGORIG, 2, LENGTH(V_LCONFIGORIG));
      END IF;
   END LOOP; /* Loop para Exportar mais de um Servico */
END;
/