CREATE OR REPLACE PROCEDURE EXPORTA_CARDAPIOPME

   (P_CDFILIORIG   IN VARCHAR2,
    P_CDSERVORIG   IN VARCHAR2,
    P_CDTIPOCARD   IN VARCHAR2,
    P_NRCARDORIG   IN VARCHAR2,
    P_NRCARDDEST   IN VARCHAR2,
    P_DTINIORIG    IN DATE,
    P_DTFINORIG    IN DATE,
    P_CDFILIDEST   IN VARCHAR2,
    P_CDSERVDEST   IN VARCHAR2,
    P_DTINIDEST    IN DATE,
    P_DTFINDEST    IN DATE,
    P_CONFIGORIG   IN VARCHAR2) IS

    V_DTMESORIG        DATE;
    V_DTMESDEST        DATE;
    V_DTCARDDEST       DATE;
    V_CONTCARDDEST     NUMBER;
    V_NRCARDIAPME      VARCHAR2(20);
    V_NRCARDIAPMECENT  VARCHAR2(20);
    V_MAXNRSEQDEST     NUMBER;
    V_MAXNRORDDEST     NUMBER;
    V_CONTPRAT         NUMBER;
    V_CDFILAUXDEST     VARCHAR2(4);
    V_IDCARDIAUTIL     VARCHAR2(1);
    V_DIASEMANA        NUMBER;
    V_CONTFERIFILI     NUMBER;
    V_CONTFERICARD     NUMBER;    
    V_CONTPRODRECE     NUMBER;
    V_IDUTRPADCA       VARCHAR2(1);
    V_NOVOSEQ          NUMBER;
    V_NOVOORD          NUMBER;
    V_COUNTCARDIAPME   NUMBER;
    V_EXISTE_CARDIAPME NUMBER;

    /* Variaveis para cursores */

    C_DATAS_DTORIGEM        DATE;
    C_COMENSSERV_QTCOM_CENT FLOAT(126);
    C_COMENSSERV_QTCOM      FLOAT(126);
    C_COMENSSERV_QTCLI      FLOAT(126);
    C_COMENSSERV_QTFUN      FLOAT(126);
    C_COMENSSERV_QTDOA      FLOAT(126);


    CURSOR C_DATAS IS
       SELECT DTDATA
       FROM DATA
       WHERE (DTDATA BETWEEN P_DTINIORIG AND P_DTFINORIG);

BEGIN
   V_DTMESORIG := P_DTINIORIG;
   V_DTMESDEST := P_DTINIDEST;

   V_MAXNRSEQDEST := 0;
   V_MAXNRORDDEST := 0;

   /*Busca a ultima configuracao do cardapio de destino */
   SELECT NVL(MAX(NRSEQCNFCPME),0), NVL(MAX(NRORDCNFCPME),0)
   INTO V_MAXNRSEQDEST, V_MAXNRORDDEST
   FROM CONFCARDPME
   WHERE
      CDFILCENTPME = P_CDFILIDEST   AND
      CDSERVFILI   = P_CDSERVDEST   AND
      NRCARDPME    = P_NRCARDDEST;

   /*Insere a configuracao origem no cardapio destino */
   INSERT INTO CONFCARDPME
      (CDFILCENTPME, NRCARDPME, CDSERVFILI, NRSEQCNFCPME,
      CDTIPOPRAT, IDOPCCNFCPME, IDIMPCNFCPME, NRORDCNFCPME)
      SELECT
         CDFILIDEST, NRCARDDEST, CDSERVDEST, V_MAXNRSEQDEST+ROWNUM,
         CDTIPOPRAT, IDOPCCNFCPME, IDIMPCNFCPME, V_MAXNRORDDEST+ROWNUM
      FROM
      (SELECT
           P_CDFILIDEST CDFILIDEST, P_NRCARDDEST NRCARDDEST, P_CDSERVDEST CDSERVDEST,
           NRSEQCNFCPME, CDTIPOPRAT, IDOPCCNFCPME, IDIMPCNFCPME, NRORDCNFCPME
       FROM
          CONFCARDPME
       WHERE
          (CDFILCENTPME = P_CDFILIORIG) AND
          (CDSERVFILI   = P_CDSERVORIG) AND
          (NRCARDPME    = P_NRCARDORIG) AND
          (INSTR(P_CONFIGORIG,LPAD(NRSEQCNFCPME,3,'0')) <> 0)
       ORDER BY
          NRORDCNFCPME);

   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
                CDFILCENTPME, CDSERVFILI, DTCARDIAPME, NRCARDIAPME,
                NRCARDPME, QTCOMESTCPME, QTCLIESTCPME, QTFUNESTCPME,
                QTDOAESTCPME
            FROM
                CARDIAPME
            WHERE
                CDFILCENTPME = P_CDFILIORIG     AND
                NRCARDPME    = P_NRCARDORIG     AND
                CDSERVFILI   = P_CDSERVORIG     AND
                DTCARDIAPME  = C_DATAS_DTORIGEM AND
                CDFILCENTPME = CDFILIAL
            ORDER BY
                CDFILCENTPME,
                CDSERVFILI,
                NRCARDPME)
         LOOP
            SELECT COUNT(*)
            INTO V_CONTCARDDEST
            FROM CARDIAPME
            WHERE
                CDFILCENTPME = P_CDFILIDEST AND
                NRCARDPME    = P_NRCARDDEST AND
                CDSERVFILI   = P_CDSERVDEST AND
                DTCARDIAPME  = V_DTCARDDEST AND
                CDFILCENTPME = CDFILIAL;

            /* Gera novo numero de cardapio se nao existir */
            IF V_CONTCARDDEST = 0 THEN
               /* Insere o cardapio para a Central de Merenda */
               V_NRCARDIAPMECENT := NULL;
               NOVO_CODIGO('CARDIAPME'||P_CDFILIDEST||P_NRCARDDEST, V_NRCARDIAPMECENT, 1);
               V_NRCARDIAPMECENT := SUBSTR(V_NRCARDIAPMECENT,11,10);

               SELECT
                  NVL(DECODE(V_DIASEMANA,2,A.QTCOMPMESEG,3,A.QTCOMPMETER,4,A.QTCOMPMEQUA,5,A.QTCOMPMEQUI,6,A.QTCOMPMESEX,7,A.QTCOMPMESAB,1,
                     A.QTCOMPMEDOM,8,A.QTCOMPMEFER),0),
                  NVL(DECODE(V_DIASEMANA,2,A.QTCLIPMESEG,3,A.QTCLIPMETER,4,A.QTCLIPMEQUA,5,A.QTCLIPMEQUI,6,A.QTCLIPMESEX,7,A.QTCLIPMESAB,1,
                     A.QTCLIPMEDOM,8,A.QTCLIPMEFER),0),
                  NVL(DECODE(V_DIASEMANA,2,A.QTFUNPMESEG,3,A.QTFUNPMETER,4,A.QTFUNPMEQUA,5,A.QTFUNPMEQUI,6,A.QTFUNPMESEX,7,A.QTFUNPMESAB,1,
                     A.QTFUNPMEDOM,8,A.QTFUNPMEFER),0),
                  NVL(DECODE(V_DIASEMANA,2,A.QTDOAPMESEG,3,A.QTDOAPMETER,4,A.QTDOAPMEQUA,5,A.QTDOAPMEQUI,6,A.QTDOAPMESEX,7,A.QTDOAPMESAB,1,
                     A.QTDOAPMEDOM,8,A.QTDOAPMEFER),0)
               INTO
                  C_COMENSSERV_QTCOM_CENT,
                  C_COMENSSERV_QTCLI,
                  C_COMENSSERV_QTFUN,
                  C_COMENSSERV_QTDOA
               FROM (
                  SELECT SUM(QTCOMPMESEG) AS QTCOMPMESEG, SUM(QTCOMPMETER) AS QTCOMPMETER, SUM(QTCOMPMEQUA) AS QTCOMPMEQUA, SUM(QTCOMPMEQUI) AS QTCOMPMEQUI, SUM(QTCOMPMESEX) AS QTCOMPMESEX, SUM(QTCOMPMESAB) AS QTCOMPMESAB, SUM(QTCOMPMEDOM) AS QTCOMPMEDOM, SUM(QTCOMPMEFER) AS QTCOMPMEFER,
                         SUM(QTCLIPMESEG) AS QTCLIPMESEG, SUM(QTCLIPMETER) AS QTCLIPMETER, SUM(QTCLIPMEQUA) AS QTCLIPMEQUA, SUM(QTCLIPMEQUI) AS QTCLIPMEQUI, SUM(QTCLIPMESEX) AS QTCLIPMESEX, SUM(QTCLIPMESAB) AS QTCLIPMESAB, SUM(QTCLIPMEDOM) AS QTCLIPMEDOM, SUM(QTCLIPMEFER) AS QTCLIPMEFER,
                         SUM(QTFUNPMESEG) AS QTFUNPMESEG, SUM(QTFUNPMETER) AS QTFUNPMETER, SUM(QTFUNPMEQUA) AS QTFUNPMEQUA, SUM(QTFUNPMEQUI) AS QTFUNPMEQUI, SUM(QTFUNPMESEX) AS QTFUNPMESEX, SUM(QTFUNPMESAB) AS QTFUNPMESAB, SUM(QTFUNPMEDOM) AS QTFUNPMEDOM, SUM(QTFUNPMEFER) AS QTFUNPMEFER,
                         SUM(QTDOAPMESEG) AS QTDOAPMESEG, SUM(QTDOAPMETER) AS QTDOAPMETER, SUM(QTDOAPMEQUA) AS QTDOAPMEQUA, SUM(QTDOAPMEQUI) AS QTDOAPMEQUI, SUM(QTDOAPMESEX) AS QTDOAPMESEX, SUM(QTDOAPMESAB) AS QTDOAPMESAB, SUM(QTDOAPMEDOM) AS QTDOAPMEDOM, SUM(QTDOAPMEFER) AS QTDOAPMEFER
                  FROM COMESERVTCAR C, FILITIPOCARD F
                  WHERE (C.CDFILCENTPME = P_CDFILIDEST)   AND
                        (C.CDTIPOCARD   = P_CDTIPOCARD)   AND
                        (C.CDSERVFILI   = P_CDSERVDEST)   AND
                        (F.CDFILCENTPME = C.CDFILCENTPME) AND
                        (F.CDTIPOCARD   = C.CDTIPOCARD)   AND
                        (F.CDFILIAL     = C.CDFILIAL)     AND
                        (F.IDTCARATFILI = 'S') AND
                        EXISTS (SELECT 1 FROM FILIAL FI WHERE FI.CDFILIAL = C.CDFILIAL AND FI.CDFILCENTPME = C.CDFILCENTPME)) A;

               IF (C_COMENSSERV_QTCOM_CENT <> 0) AND (NOT C_COMENSSERV_QTCOM_CENT IS NULL) THEN
                  SELECT COUNT(*)
                  INTO V_EXISTE_CARDIAPME
                  FROM CARDIAPME
                  WHERE
                      CDFILCENTPME = P_CDFILIDEST AND
                      NRCARDPME    = P_NRCARDDEST AND
                      CDSERVFILI   = P_CDSERVDEST AND
                      DTCARDIAPME  = V_DTCARDDEST AND
                      CDFILIAL     = P_CDFILIDEST;

                  IF V_EXISTE_CARDIAPME = 0 THEN
                     INSERT INTO CARDIAPME
                       (CDFILCENTPME, NRCARDPME, NRCARDIAPME, CDSERVFILI, CDFILIAL,
                        DTCARDIAPME, QTCOMESTCPME, QTCLIESTCPME, QTFUNESTCPME, QTDOAESTCPME,
                        QTCOMEFECPME, QTCLIEFECPME, QTFUNEFECPME, QTDOAEFECPME, IDCARDESPEC)
                     VALUES
                        (P_CDFILIDEST, P_NRCARDDEST, V_NRCARDIAPMECENT, P_CDSERVDEST, P_CDFILIDEST,
                        V_DTCARDDEST, C_COMENSSERV_QTCOM_CENT, C_COMENSSERV_QTCLI, C_COMENSSERV_QTFUN, C_COMENSSERV_QTDOA,
                        0, 0, 0, 0, 'N');
                  END IF;      
               END IF;

                  /* Loop para inserir os cardapios para as Agregadas */
               FOR C_CARDFILIAGREG IN
                  (SELECT T.CDFILIAL
                   FROM FILITIPOCARD T, SERVTIPOCARD S
                   WHERE (T.CDFILCENTPME = P_CDFILIDEST)   AND
                         (T.CDTIPOCARD   = P_CDTIPOCARD)   AND
                         (S.CDFILCENTPME = T.CDFILCENTPME) AND
                         (S.CDTIPOCARD   = T.CDTIPOCARD)   AND
                         (S.CDSERVFILI   = P_CDSERVDEST)   AND
                         (T.IDTCARATFILI = 'S') AND
                        EXISTS (SELECT 1 FROM FILIAL FI WHERE FI.CDFILIAL = T.CDFILIAL AND FI.CDFILCENTPME = T.CDFILCENTPME) )
               LOOP
                  SELECT COUNT(*)
                  INTO V_COUNTCARDIAPME
                  FROM CARDIAPME
                  WHERE (CDFILCENTPME = P_CDFILIDEST)             AND
                        (NRCARDPME    = P_NRCARDDEST)             AND
                        (CDSERVFILI   = P_CDSERVDEST)             AND
                        (CDFILIAL     = C_CARDFILIAGREG.CDFILIAL) AND
                        (DTCARDIAPME  = V_DTCARDDEST);

                  IF V_COUNTCARDIAPME = 0 THEN
                     /* Inclui o cardapio somente se a agregada utilizar o tipo de cardapio */
                     /* Gera o novo codigo */
                     V_NRCARDIAPME := NULL;
                     NOVO_CODIGO('CARDIAPME'||P_CDFILIDEST||P_NRCARDDEST, V_NRCARDIAPME, 1);
                     V_NRCARDIAPME := SUBSTR(V_NRCARDIAPME,11,10);
                     /* Total de comensais por servico da agregada */
                     SELECT
                        NVL(DECODE(V_DIASEMANA,2,A.QTCOMPMESEG,3,A.QTCOMPMETER,4,A.QTCOMPMEQUA,5,A.QTCOMPMEQUI,6,
                           A.QTCOMPMESEX,7,A.QTCOMPMESAB,1, A.QTCOMPMEDOM,8,A.QTCOMPMEFER),0),
                        NVL(DECODE(V_DIASEMANA,2,A.QTCLIPMESEG,3,A.QTCLIPMETER,4,A.QTCLIPMEQUA,5,A.QTCLIPMEQUI,6,
                           A.QTCLIPMESEX,7,A.QTCLIPMESAB,1, A.QTCLIPMEDOM,8,A.QTCLIPMEFER),0),
                        NVL(DECODE(V_DIASEMANA,2,A.QTFUNPMESEG,3,A.QTFUNPMETER,4,A.QTFUNPMEQUA,5,A.QTFUNPMEQUI,6,
                           A.QTFUNPMESEX,7,A.QTFUNPMESAB,1, A.QTFUNPMEDOM,8,A.QTFUNPMEFER),0),
                        NVL(DECODE(V_DIASEMANA,2,A.QTDOAPMESEG,3,A.QTDOAPMETER,4,A.QTDOAPMEQUA,5,A.QTDOAPMEQUI,6,
                           A.QTDOAPMESEX,7,A.QTDOAPMESAB,1, A.QTDOAPMEDOM,8,A.QTDOAPMEFER),0)
                     INTO
                        C_COMENSSERV_QTCOM,
                        C_COMENSSERV_QTCLI,
                        C_COMENSSERV_QTFUN,
                        C_COMENSSERV_QTDOA
                     FROM (
                     SELECT SUM(QTCOMPMESEG) AS QTCOMPMESEG, SUM(QTCOMPMETER) AS QTCOMPMETER, SUM(QTCOMPMEQUA) AS QTCOMPMEQUA, SUM(QTCOMPMEQUI) AS QTCOMPMEQUI, SUM(QTCOMPMESEX) AS QTCOMPMESEX, SUM(QTCOMPMESAB) AS QTCOMPMESAB, SUM(QTCOMPMEDOM) AS QTCOMPMEDOM, SUM(QTCOMPMEFER) AS QTCOMPMEFER,
                            SUM(QTCLIPMESEG) AS QTCLIPMESEG, SUM(QTCLIPMETER) AS QTCLIPMETER, SUM(QTCLIPMEQUA) AS QTCLIPMEQUA, SUM(QTCLIPMEQUI) AS QTCLIPMEQUI, SUM(QTCLIPMESEX) AS QTCLIPMESEX, SUM(QTCLIPMESAB) AS QTCLIPMESAB, SUM(QTCLIPMEDOM) AS QTCLIPMEDOM, SUM(QTCLIPMEFER) AS QTCLIPMEFER,
                            SUM(QTFUNPMESEG) AS QTFUNPMESEG, SUM(QTFUNPMETER) AS QTFUNPMETER, SUM(QTFUNPMEQUA) AS QTFUNPMEQUA, SUM(QTFUNPMEQUI) AS QTFUNPMEQUI, SUM(QTFUNPMESEX) AS QTFUNPMESEX, SUM(QTFUNPMESAB) AS QTFUNPMESAB, SUM(QTFUNPMEDOM) AS QTFUNPMEDOM, SUM(QTFUNPMEFER) AS QTFUNPMEFER,
                            SUM(QTDOAPMESEG) AS QTDOAPMESEG, SUM(QTDOAPMETER) AS QTDOAPMETER, SUM(QTDOAPMEQUA) AS QTDOAPMEQUA, SUM(QTDOAPMEQUI) AS QTDOAPMEQUI, SUM(QTDOAPMESEX) AS QTDOAPMESEX, SUM(QTDOAPMESAB) AS QTDOAPMESAB, SUM(QTDOAPMEDOM) AS QTDOAPMEDOM, SUM(QTDOAPMEFER) AS QTDOAPMEFER
                     FROM COMESERVTCAR C, FILITIPOCARD F
                     WHERE (C.CDFILCENTPME = P_CDFILIDEST)             AND
                           (C.CDTIPOCARD   = P_CDTIPOCARD)             AND
                           (C.CDSERVFILI   = P_CDSERVDEST)             AND
                           (C.CDFILIAL     = C_CARDFILIAGREG.CDFILIAL) AND
                           (F.CDFILCENTPME = C.CDFILCENTPME)           AND
                           (F.CDTIPOCARD   = C.CDTIPOCARD)             AND
                           (F.CDFILIAL     = C.CDFILIAL)               AND
                           (F.IDTCARATFILI = 'S')) A;

                     /*Insere na Cardiapme para cada Filial Agregada*/
                     SELECT COUNT(*)
                     INTO V_EXISTE_CARDIAPME
                     FROM CARDIAPME
                     WHERE
                         CDFILCENTPME = P_CDFILIDEST AND
                         NRCARDPME    = P_NRCARDDEST AND
                         CDSERVFILI   = P_CDSERVDEST AND
                         DTCARDIAPME  = V_DTCARDDEST AND
                         CDFILIAL     = C_CARDFILIAGREG.CDFILIAL;

                     IF V_EXISTE_CARDIAPME = 0 THEN

                        INSERT INTO CARDIAPME
                          (CDFILCENTPME, NRCARDPME, NRCARDIAPME, CDSERVFILI, CDFILIAL,
                           DTCARDIAPME, QTCOMESTCPME, QTCLIESTCPME, QTFUNESTCPME, QTDOAESTCPME,
                           QTCOMEFECPME, QTCLIEFECPME, QTFUNEFECPME, QTDOAEFECPME, IDCARDESPEC)
                        VALUES
                           (P_CDFILIDEST, P_NRCARDDEST, V_NRCARDIAPME, P_CDSERVDEST, C_CARDFILIAGREG.CDFILIAL,
                           V_DTCARDDEST, C_COMENSSERV_QTCOM, C_COMENSSERV_QTCLI, C_COMENSSERV_QTFUN, C_COMENSSERV_QTDOA,
                           0, 0, 0, 0, 'N');
                     END IF;
                  END IF;
               END LOOP;
            ELSE
               /* Se ja existir cardapio, insere com o numero antigo */
               SELECT MAX(NRCARDIAPME),  SUM(QTCOMESTCPME)
               INTO V_NRCARDIAPMECENT, C_COMENSSERV_QTCOM_CENT
               FROM CARDIAPME
               WHERE CDFILCENTPME = P_CDFILIDEST AND
                     NRCARDPME    = P_NRCARDDEST AND
                     CDSERVFILI   = P_CDSERVDEST AND
                     DTCARDIAPME  = V_DTCARDDEST AND
                     CDFILIAL     = CDFILCENTPME;
            END IF; /* V_CONTCARDEST = 0 */

            IF (C_COMENSSERV_QTCOM_CENT <> 0) AND (NOT C_COMENSSERV_QTCOM_CENT IS NULL) THEN
            /* INSERT NA COMPCARD.... */
               V_NOVOSEQ := V_MAXNRSEQDEST;
               V_NOVOORD := V_MAXNRORDDEST;
               FOR C_PRATOSORIG IN
                 (SELECT
                     CDFILCENTPME,
                     NRCARDPME,
                     NRSEQPRATCAR,
                     CDFILAUXPRAT,
                     CDPRATO,
                     VRPECONSCPME,
                     CDTIPOPRAT,
                     NRORDPRATCAR,
                     NMPRATO
                  FROM
                  (SELECT
                      F.CDFILCENTPME,
                      C.NRCARDPME,
                      F.NRSEQCNFCPME NRSEQPRATCAR,
                      C.CDFILAUXPRAT,
                      NVL(C.CDPRATO,'') CDPRATO,
                      C.VRPECONSCPME,
                      F.CDTIPOPRAT,
                      F.NRORDCNFCPME NRORDPRATCAR,
                      P.NMPRATO
                   FROM
                      PRATO P,
                      CARDIAPME CD,
                      COMPCARDPME C,
                      CONFCARDPME F
                   WHERE
                      CD.CDFILCENTPME    = P_CDFILIORIG         AND
                      CD.NRCARDPME       = P_NRCARDORIG         AND
                      CD.CDSERVFILI      = P_CDSERVORIG         AND
                      CD.CDFILCENTPME    = CD.CDFILIAL          AND
                      CD.DTCARDIAPME     = C_DATAS_DTORIGEM     AND
                      CD.CDFILCENTPME    = C.CDFILCENTPME(+)    AND
                      CD.NRCARDPME       = C.NRCARDPME(+)       AND
                      CD.NRCARDIAPME     = C.NRCARDIAPME(+)     AND
                      (INSTR(P_CONFIGORIG,LPAD(C.NRSEQPRACPME,3,'0')) <> 0) AND
                      F.CDFILCENTPME     = CD.CDFILCENTPME      AND
                      F.NRCARDPME        = CD.NRCARDPME         AND
                      F.CDSERVFILI       = CD.CDSERVFILI        AND
                      C.NRSEQPRACPME     = F.NRSEQCNFCPME       AND
                      P.CDPRATO      (+) = C.CDPRATO            AND
                      P.CDFILAUXPRAT (+) = C.CDFILAUXPRAT        
                               
                   UNION 
                   SELECT 
                      F.CDFILCENTPME,
                      P_NRCARDORIG, 
                      F.NRSEQCNFCPME NRSEQPRATCAR,
                      'P' CDFILAUXPRAT, 
                      '' CDPRATO,
                      0 VRPECONSCPME, 
                      F.CDTIPOPRAT,
                      F.NRORDCNFCPME NRORDPRATCAR,
                      '' AS NMPRATO
                   FROM
                      CARDIAPME CD,
                      CONFCARDPME F
                   WHERE
                      CD.CDFILCENTPME    = P_CDFILIORIG         AND
                      CD.NRCARDPME       = P_NRCARDORIG         AND
                      CD.CDSERVFILI      = P_CDSERVORIG         AND
                      CD.CDFILCENTPME    = CD.CDFILIAL          AND
                      CD.DTCARDIAPME     = C_DATAS_DTORIGEM     AND
                      (INSTR(P_CONFIGORIG,LPAD(F.NRSEQCNFCPME,3,'0')) <> 0) AND
                      F.CDFILCENTPME     = CD.CDFILCENTPME      AND
                      F.NRCARDPME        = CD.NRCARDPME         AND
                      F.CDSERVFILI       = CD.CDSERVFILI        AND
                      NOT EXISTS (SELECT
                                     F2.NRORDCNFCPME NRORDPRATCAR
                                  FROM
                                     PRATO P2,
                                     CARDIAPME CD2,
                                     COMPCARDPME C2,
                                     CONFCARDPME F2
                                  WHERE
                                     CD2.CDFILCENTPME    = P_CDFILIORIG         AND
                                     CD2.NRCARDPME       = P_NRCARDORIG         AND
                                     CD2.CDSERVFILI      = P_CDSERVORIG         AND
                                     CD2.CDFILCENTPME    = CD2.CDFILIAL          AND
                                     CD2.DTCARDIAPME     = C_DATAS_DTORIGEM     AND
                                     CD2.CDFILCENTPME    = C2.CDFILCENTPME(+)    AND
                                     CD2.NRCARDPME       = C2.NRCARDPME(+)       AND
                                     CD2.NRCARDIAPME     = C2.NRCARDIAPME(+)     AND
                                     (INSTR(P_CONFIGORIG,LPAD(C2.NRSEQPRACPME,3,'0')) <> 0) AND
                                     F2.CDFILCENTPME     = CD2.CDFILCENTPME      AND
                                     F2.NRCARDPME        = CD2.NRCARDPME         AND
                                     F2.CDSERVFILI       = CD2.CDSERVFILI        AND
                                     C2.NRSEQPRACPME     = F2.NRSEQCNFCPME       AND
                                     P2.CDPRATO      (+) = C2.CDPRATO            AND
                                     P2.CDFILAUXPRAT (+) = C2.CDFILAUXPRAT       AND
                                     F.NRORDCNFCPME = F2.NRORDCNFCPME)
                 )
                 ORDER BY
                    NRORDPRATCAR)

               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;
                     IF P_CDFILIDEST <> P_CDFILIORIG THEN
                        V_CONTPRAT := 0;
                        SELECT COUNT(*)
                        INTO V_CONTPRAT
                        FROM PRATO
                        WHERE
                           CDFILAUXPRAT = V_CDFILAUXDEST  AND
                           CDPRATO = C_PRATOSORIG.CDPRATO AND
                           STRCOMP(NMPRATO, C_PRATOSORIG.NMPRATO)=0;
                     END IF;

                     SELECT MAX(IDUTRPADCA) INTO V_IDUTRPADCA
                     FROM PARAMGERAL
                     WHERE CDPARGERAL = '0001';

                     /* 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 RECETPCARD
                         WHERE
                            CDFILAUXPRAT = V_CDFILAUXDEST  AND
                            CDPRATO = C_PRATOSORIG.CDPRATO AND
                            CDFILCENTPME = C_PRATOSORIG.CDFILCENTPME AND
                            CDTIPOCARD = P_CDTIPOCARD
                         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 = P_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 RECETPCARD
                         WHERE
                            CDFILAUXPRAT = V_CDFILAUXDEST  AND
                            CDPRATO = C_PRATOSORIG.CDPRATO AND
                            CDFILCENTPME = C_PRATOSORIG.CDFILCENTPME AND
                            CDTIPOCARD = P_CDTIPOCARD
                         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 = P_CDSERVDEST);
                     END IF;

                     IF (V_CONTPRAT <> 0) AND (V_CONTPRODRECE <> 0) THEN
                        /* ACRESCENTAR */
                        /* OBS.: Os tipos de prato serao incluidos ao final da tabela de configuracao */

                        INSERT INTO COMPCARDPME
                           (CDFILCENTPME,
                            NRCARDPME,
                            NRCARDIAPME,
                            NRSEQPRACPME,
                            CDFILAUXPRAT,
                            CDPRATO,
                            VRPECONSCPME,
                            CDTIPOPRAT,
                            NRORDPRACPME)
                        VALUES
                           (P_CDFILIDEST,
                            P_NRCARDDEST,
                            V_NRCARDIAPMECENT,
                            V_NOVOSEQ,
                            V_CDFILAUXDEST,
                            C_PRATOSORIG.CDPRATO,
                            C_PRATOSORIG.VRPECONSCPME,
                            C_PRATOSORIG.CDTIPOPRAT,
                            V_NOVOORD);
                     END IF;
                  END IF;
               END LOOP; /* C_PRATOSORIG */
            END IF;
         END LOOP; /* C_CARDORIG */
      END IF; /* V_DTCARDDEST */
      V_DTCARDDEST := V_DTCARDDEST + 1;
   END LOOP; /* C_DATAS */
   CLOSE C_DATAS;
   COMMIT;
END;
/