CREATE OR REPLACE PROCEDURE EXPORTA_CARDAPIO_PAE (
   P_CDCENTRAL        IN  VARCHAR2,
   P_NRCARDPME        IN  VARCHAR2,
   P_CDTIPOCARD       IN  VARCHAR2,
   P_NMTIPOCARD       IN  VARCHAR2,
   P_CDSERVFILI       IN  VARCHAR2,
   P_NMSERVICO        IN  VARCHAR2,
   P_DTINICARDPME     IN  DATE,
   P_DTFINCARDPME     IN  DATE,
   P_DSCARDPME        IN  VARCHAR2,

   P_CDTIPOCARDDEST   IN  VARCHAR2,
   P_NMTIPOCARDDEST   IN  VARCHAR2,
   P_CDSERVFILIDEST   IN  VARCHAR2,
   P_NMSERVICODEST    IN  VARCHAR2,
   P_DTINICARDPMEDEST IN  DATE,
   P_DTFINCARDPMEDEST IN  DATE,
   P_DSCARDPMEDEST    IN  VARCHAR2,

   P_NRLISTACARDIAPME IN  VARCHAR2,

   P_NRLOGINTCARD     OUT VARCHAR2
) IS
   V_NRCARDPMENOVO    VARCHAR2(10);
   V_NRCARDIAPMENOVO  VARCHAR2(20);
   V_NRCARDIACOMP     VARCHAR2(20);
   V_EXISTNRCARDAPIO  VARCHAR2(10);
   V_IDCUSPADCAR      VARCHAR2(1);
   V_N_CONFCARDPME    VARCHAR2(20);
   V_PERI_COMPREEND   VARCHAR2(20);
   V_SRV_N_CUSTPADR   VARCHAR2(20);
   V_EXISTNRCARDPME   VARCHAR2(10);
   V_IDCARDIAUTIL     VARCHAR2(1);
   V_CDFILIAL         VARCHAR2(4);
   V_NO_DATA_FOUND    VARCHAR2(1);

   V_NRSEQCNFCPME     NUMBER;
   V_NRSEQCNFTCAR     NUMBER;
   V_PERIOCOMPR       NUMBER;
   V_COUNT            NUMBER;

   V_DTFERICARD       DATE;
   V_DTFERIFILI       DATE;
   V_DTCARDIADESTPME  DATE;

   V_VRCUSPADTCAR     FLOAT;
   V_QTCOMPMESEG      FLOAT;
   V_QTCOMPMETER      FLOAT;
   V_QTCOMPMEQUA      FLOAT;
   V_QTCOMPMEQUI      FLOAT;
   V_QTCOMPMESEX      FLOAT;
   V_QTCOMPMESAB      FLOAT;
   V_QTCOMPMEDOM      FLOAT;
   V_QTCOMPMEFER      FLOAT;
   V_QTCLIPMESEG      FLOAT;
   V_QTCLIPMETER      FLOAT;
   V_QTCLIPMEQUA      FLOAT;
   V_QTCLIPMEQUI      FLOAT;
   V_QTCLIPMESEX      FLOAT;
   V_QTCLIPMESAB      FLOAT;
   V_QTCLIPMEDOM      FLOAT;
   V_QTCLIPMEFER      FLOAT;
   V_QTFUNPMESEG      FLOAT;
   V_QTFUNPMETER      FLOAT;
   V_QTFUNPMEQUA      FLOAT;
   V_QTFUNPMEQUI      FLOAT;
   V_QTFUNPMESEX      FLOAT;
   V_QTFUNPMESAB      FLOAT;
   V_QTFUNPMEDOM      FLOAT;
   V_QTFUNPMEFER      FLOAT;
   V_QTDOAPMESEG      FLOAT;
   V_QTDOAPMETER      FLOAT;
   V_QTDOAPMEQUA      FLOAT;
   V_QTDOAPMEQUI      FLOAT;
   V_QTDOAPMESEX      FLOAT;
   V_QTDOAPMESAB      FLOAT;
   V_QTDOAPMEDOM      FLOAT;
   V_QTDOAPMEFER      FLOAT;

   V_QTCOMESTC        FLOAT;
   V_QTCLIESTC        FLOAT;
   V_QTFUNESTC        FLOAT;
   V_QTDOAESTC        FLOAT;

BEGIN
   Begin
      SELECT NRCARDPME
        INTO V_EXISTNRCARDPME
        FROM CARDAPIOPME
       WHERE CDFILCENTPME = P_CDCENTRAL
         AND CDTIPOCARD   = P_CDTIPOCARDDEST
         AND DTINICARDPME = P_DTINICARDPMEDEST
         AND DTFINCARDPME = P_DTFINCARDPMEDEST;
   Exception
      When No_Data_Found Then
         V_EXISTNRCARDPME := Null;
   End;

   /* Caso nao exista cardapio para o periodo cria um novo */
   IF V_EXISTNRCARDPME IS NULL THEN
      V_NRCARDPMENOVO := RETORNA_NOVO_CODIGO('CARDAPIOPME'|| P_CDCENTRAL, 1, 10);

      INSERT INTO CARDAPIOPME (CDFILCENTPME, NRCARDPME, DSCARDPME, DTINICARDPME, DTFINCARDPME, CDTIPOCARD)
                       VALUES (P_CDCENTRAL, V_NRCARDPMENOVO, P_DSCARDPMEDEST, P_DTINICARDPMEDEST, P_DTFINCARDPMEDEST, P_CDTIPOCARDDEST);
   ELSE
      V_NRCARDPMENOVO := V_EXISTNRCARDPME;

      /* Verifica se existe composicao cadastrado no periodo do novo cardapio */
      SELECT COUNT(*)
        INTO V_COUNT
        FROM COMPCARDPME
       WHERE CDFILCENTPME = P_CDCENTRAL
         AND NRCARDPME    = V_NRCARDPMENOVO;

      IF V_COUNT > 0 THEN
         FOR C_DELCOMPOSICAO IN (
            SELECT CC.NRCARDIAPME,
                   CC.NRSEQPRACPME
              FROM CONFCARDPME CM,
                   (SELECT CD.NRCARDIAPME,
                           C.NRSEQPRACPME
                      FROM CARDIAPME CD,
                           COMPCARDPME C
                     WHERE CD.NRCARDPME    = V_NRCARDPMENOVO
                       AND CD.CDFILCENTPME = P_CDCENTRAL
                       AND CD.CDSERVFILI   = P_CDSERVFILIDEST
                       AND CD.NRCARDPME    = C.NRCARDPME
                       AND CD.CDFILCENTPME = C.CDFILCENTPME
                       AND CD.NRCARDIAPME  = C.NRCARDIAPME) CC
             WHERE CM.NRCARDPME    = V_NRCARDPMENOVO
               AND CM.CDFILCENTPME = P_CDCENTRAL
               AND CM.CDSERVFILI   = P_CDSERVFILIDEST
               AND CM.NRSEQCNFCPME = CC.NRSEQPRACPME(+)
             ORDER BY CC.NRCARDIAPME
         ) LOOP
            /* Deleta os dados da Composicao do servico */
            DELETE COMPCARDPME
             WHERE CDFILCENTPME = P_CDCENTRAL
               AND NRCARDPME    = V_NRCARDPMENOVO
               AND NRCARDIAPME  = C_DELCOMPOSICAO.NRCARDIAPME;
         END LOOP;
      END IF;

      /* Verifica se existe dados na CARDIAPME para o servico no periodo*/
      SELECT COUNT(*)
        INTO V_COUNT
        FROM CARDIAPME
       WHERE CDFILCENTPME = P_CDCENTRAL
         AND NRCARDPME    = V_NRCARDPMENOVO
         AND CDSERVFILI   = P_CDSERVFILIDEST;

      /* Se existir deleta */
      IF V_COUNT > 0 THEN
         DELETE CARDIAPME
          WHERE CDFILCENTPME = P_CDCENTRAL
            AND NRCARDPME    = V_NRCARDPMENOVO
            AND CDSERVFILI   = P_CDSERVFILIDEST;
      END IF;

      /* Verifica se existe dados na CONFCARDPME para o servico no periodo */
      SELECT COUNT(*)
        INTO V_COUNT
        FROM CONFCARDPME
       WHERE CDFILCENTPME = P_CDCENTRAL
         AND NRCARDPME    = V_NRCARDPMENOVO
         AND CDSERVFILI   = P_CDSERVFILIDEST;

      /* Se existir deleta */
      IF V_COUNT > 0 THEN
         DELETE CONFCARDPME
          WHERE CDFILCENTPME = P_CDCENTRAL
            AND NRCARDPME    = V_NRCARDPMENOVO
            AND CDSERVFILI   = P_CDSERVFILIDEST;
      END IF;
   END IF;

   /* Grava configuracao Padrao (CONFCARDPME) */
   INSERT INTO CONFCARDPME (CDFILCENTPME, NRCARDPME, CDSERVFILI, NRSEQCNFCPME, CDTIPOPRAT, IDOPCCNFCPME, IDIMPCNFCPME, NRORDCNFCPME)
       (SELECT C.CDFILCENTPME, V_NRCARDPMENOVO, P_CDSERVFILIDEST, C.NRSEQCNFCPME, C.CDTIPOPRAT, C.IDOPCCNFCPME, C.IDIMPCNFCPME, C.NRORDCNFCPME
          FROM CONFCARDPME C, CARDAPIOPME CA
         WHERE CA.CDFILCENTPME = C.CDFILCENTPME
           AND CA.NRCARDPME    = C.NRCARDPME
           AND C.CDFILCENTPME  = P_CDCENTRAL
           AND CA.NRCARDPME    = P_NRCARDPME
           AND CA.CDTIPOCARD   = P_CDTIPOCARD
           AND C.CDSERVFILI    = P_CDSERVFILI);

   FOR C_DATAS IN (
  /*    SELECT DTCARDIADESTPME
        FROM LISTA_DTCARDIAPME
       WHERE NRLISTA = P_NRLISTACARDIAPME
       ORDER BY DTCARDIADESTPME
 */
      SELECT DISTINCT LD.DTCARDIADESTPME 
        FROM CARDIAPME CD, LISTA_DTCARDIAPME LD
       WHERE CD.DTCARDIAPME = LD.DTCARDIAPME
         AND CDFILCENTPME = P_CDCENTRAL
         AND NRCARDPME    = P_NRCARDPME
         AND NRLISTA      = P_NRLISTACARDIAPME
       ORDER BY 1
   ) LOOP
      /* Verifica feriado para a filial */
      Begin
         SELECT DTFERIFILI
           INTO V_DTFERIFILI
           FROM FERIFILI
          WHERE CDFILIAL   = P_CDCENTRAL
            AND DTFERIFILI = C_DATAS.DTCARDIADESTPME;
      Exception
         When No_Data_Found Then
            V_DTFERIFILI := Null;
      End;

      Begin
         SELECT DTFERICARD
           INTO V_DTFERICARD
           FROM FERICARD
          WHERE CDFILIAL   = P_CDCENTRAL
            AND DTFERICARD = C_DATAS.DTCARDIADESTPME;
      Exception
         When No_Data_Found Then
            V_DTFERICARD := Null;
      End;

      SELECT IDCARDIAUTIL
        INTO V_IDCARDIAUTIL
        FROM PARAMPLANEJA
       WHERE CDFILIAL     = P_CDCENTRAL
         AND CDPARPLANEJA = '1';

      /* VERIFICA FERIADOS PARA A CENTRALIZADORA */
      IF ((V_DTFERIFILI IS NULL) OR (V_IDCARDIAUTIL = 'N')) THEN
         IF V_DTFERICARD IS NULL THEN
            /* Verifica os comensais para a Centralizadora */
            SELECT A.QTCOMPMESEG, A.QTCOMPMETER, A.QTCOMPMEQUA, A.QTCOMPMEQUI, A.QTCOMPMESEX, A.QTCOMPMESAB,
                   A.QTCOMPMEDOM, A.QTCOMPMEFER, A.QTCLIPMESEG, A.QTCLIPMETER, A.QTCLIPMEQUA, A.QTCLIPMEQUI,
                   A.QTCLIPMESEX, A.QTCLIPMESAB, A.QTCLIPMEDOM, A.QTCLIPMEFER, A.QTFUNPMESEG, A.QTFUNPMETER,
                   A.QTFUNPMEQUA, A.QTFUNPMEQUI, A.QTFUNPMESEX, A.QTFUNPMESAB, A.QTFUNPMEDOM, A.QTFUNPMEFER,
                   A.QTDOAPMESEG, A.QTDOAPMETER, A.QTDOAPMEQUA, A.QTDOAPMEQUI, A.QTDOAPMESEX, A.QTDOAPMESAB,
                   A.QTDOAPMEDOM, A.QTDOAPMEFER
              INTO V_QTCOMPMESEG, V_QTCOMPMETER, V_QTCOMPMEQUA, V_QTCOMPMEQUI, V_QTCOMPMESEX, V_QTCOMPMESAB,
                   V_QTCOMPMEDOM, V_QTCOMPMEFER, V_QTCLIPMESEG, V_QTCLIPMETER, V_QTCLIPMEQUA, V_QTCLIPMEQUI,
                   V_QTCLIPMESEX, V_QTCLIPMESAB, V_QTCLIPMEDOM, V_QTCLIPMEFER, V_QTFUNPMESEG, V_QTFUNPMETER,
                   V_QTFUNPMEQUA, V_QTFUNPMEQUI, V_QTFUNPMESEX, V_QTFUNPMESAB, V_QTFUNPMEDOM, V_QTFUNPMEFER,
                   V_QTDOAPMESEG, V_QTDOAPMETER, V_QTDOAPMEQUA, V_QTDOAPMEQUI, V_QTDOAPMESEX, V_QTDOAPMESAB,
                   V_QTDOAPMEDOM, V_QTDOAPMEFER
              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_CDCENTRAL     )
                       AND (C.CDTIPOCARD   = P_CDTIPOCARDDEST)
                       AND (C.CDSERVFILI   = P_CDSERVFILIDEST)
                       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;

            V_QTCOMESTC := 0;
            V_QTCLIESTC := 0;
            V_QTFUNESTC := 0;
            V_QTDOAESTC := 0;

            IF TO_CHAR(C_DATAS.DTCARDIADESTPME, 'D') = '1' THEN
               V_QTCOMESTC := V_QTCOMPMEDOM;
               V_QTCLIESTC := V_QTCLIPMEDOM;
               V_QTFUNESTC := V_QTFUNPMEDOM;
               V_QTDOAESTC := V_QTDOAPMEDOM;
            END IF;

            IF TO_CHAR(C_DATAS.DTCARDIADESTPME, 'D') = '2' THEN
               V_QTCOMESTC := V_QTCOMPMESEG;
               V_QTCLIESTC := V_QTCLIPMESEG;
               V_QTFUNESTC := V_QTFUNPMESEG;
               V_QTDOAESTC := V_QTDOAPMESEG;
            END IF;

            IF TO_CHAR(C_DATAS.DTCARDIADESTPME, 'D') = '3' THEN
               V_QTCOMESTC := V_QTCOMPMETER;
               V_QTCLIESTC := V_QTCLIPMETER;
               V_QTFUNESTC := V_QTFUNPMETER;
               V_QTDOAESTC := V_QTDOAPMETER;
            END IF;

            IF TO_CHAR(C_DATAS.DTCARDIADESTPME, 'D') = '4' THEN
               V_QTCOMESTC := V_QTCOMPMEQUA;
               V_QTCLIESTC := V_QTCLIPMEQUA;
               V_QTFUNESTC := V_QTFUNPMEQUA;
               V_QTDOAESTC := V_QTDOAPMEQUA;
            END IF;

            IF TO_CHAR(C_DATAS.DTCARDIADESTPME, 'D') = '5' THEN
               V_QTCOMESTC := V_QTCOMPMEQUI;
               V_QTCLIESTC := V_QTCLIPMEQUI;
               V_QTFUNESTC := V_QTFUNPMEQUI;
               V_QTDOAESTC := V_QTDOAPMEQUI;
            END IF;

            IF TO_CHAR(C_DATAS.DTCARDIADESTPME, 'D') = '6' THEN
               V_QTCOMESTC := V_QTCOMPMESEX;
               V_QTCLIESTC := V_QTCLIPMESEX;
               V_QTFUNESTC := V_QTFUNPMESEX;
               V_QTDOAESTC := V_QTDOAPMESEX;
            END IF;

            IF TO_CHAR(C_DATAS.DTCARDIADESTPME, 'D') = '7' THEN
               V_QTCOMESTC := V_QTCOMPMESAB;
               V_QTCLIESTC := V_QTCLIPMESAB;
               V_QTFUNESTC := V_QTFUNPMESAB;
               V_QTDOAESTC := V_QTDOAPMESAB;
            END IF;

            IF (V_QTCOMESTC IS NULL) THEN
               V_QTCOMESTC := 0;
            END IF;

            IF (V_QTCLIESTC IS NULL) THEN
               V_QTCLIESTC := 0;
            END IF;

            IF (V_QTFUNESTC IS NULL) THEN
               V_QTFUNESTC := 0;
            END IF;

            IF (V_QTDOAESTC IS NULL) THEN
               V_QTDOAESTC := 0;
            END IF;

            /* Somente inserir se existir comensais para a filial */
            IF (NOT V_QTCOMESTC IS NULL) AND (V_QTCOMESTC > 0) THEN
               /* Cria o novo codigo para os novos lancamentos da cardia para o periodo */
               NOVO_CODIGO('CARDIAPME' || P_CDCENTRAL || V_NRCARDPMENOVO, V_NRCARDIAPMENOVO, 1);
               V_NRCARDIAPMENOVO := SUBSTR(V_NRCARDIAPMENOVO, 11, 10);

               /* Insere dados para a Centralizadora */
               INSERT INTO CARDIAPME
                      (CDFILCENTPME , NRCARDPME      , NRCARDIAPME , CDSERVFILI  ,
                       CDFILIAL     , DTCARDIAPME    , QTCOMESTCPME, QTCLIESTCPME,
                       QTFUNESTCPME , QTDOAESTCPME   , QTCOMEFECPME, QTCLIEFECPME,
                       QTFUNEFECPME , QTDOAEFECPME   , IDCARDESPEC)
               VALUES (P_CDCENTRAL  , V_NRCARDPMENOVO, V_NRCARDIAPMENOVO, P_CDSERVFILIDEST,
                       P_CDCENTRAL  , C_DATAS.DTCARDIADESTPME, V_QTCOMESTC  , V_QTCLIESTC,
                       V_QTFUNESTC  , V_QTDOAESTC    , 0            , 0,
                       0, 0, 'N');

               /* Verifica as filias agregadas da Centralizadora */
               FOR C_AGREGADA IN (
                 SELECT FILITIPOCARD.CDFILIAL,
                        (SELECT MAX(DTFERIFILI)
                           FROM FERIFILI
                          WHERE CDFILIAL   = FILITIPOCARD.CDFILIAL
                            AND DTFERIFILI = C_DATAS.DTCARDIADESTPME) AS FERIADO,
                        (SELECT MAX(DTFERICARD)
                           FROM FERICARD
                          WHERE CDFILIAL   = FILITIPOCARD.CDFILIAL
                            AND DTFERICARD = C_DATAS.DTCARDIADESTPME) AS FERICARD,
                        (SELECT MAX(IDCARDIAUTIL)
                           FROM PARAMPLANEJA
                          WHERE CDFILIAL     = FILITIPOCARD.CDFILIAL
                            AND CDPARPLANEJA = '1') AS IDCARDIAUTIL
                   FROM FILITIPOCARD, SERVTIPOCARD, PARAMPLANEJA
                  WHERE (FILITIPOCARD.CDFILCENTPME = P_CDCENTRAL)
                    AND (FILITIPOCARD.CDTIPOCARD   = P_CDTIPOCARDDEST)
                    AND (SERVTIPOCARD.CDFILCENTPME = FILITIPOCARD.CDFILCENTPME)
                    AND (SERVTIPOCARD.CDTIPOCARD   = FILITIPOCARD.CDTIPOCARD)
                    AND (SERVTIPOCARD.CDSERVFILI   = P_CDSERVFILIDEST)
                    AND (FILITIPOCARD.CDFILIAL     = PARAMPLANEJA.CDFILIAL)
                    AND (FILITIPOCARD.IDTCARATFILI = 'S')
                    AND NOT EXISTS (SELECT 1
                                      FROM CARDIAPME
                                     WHERE CDFILCENTPME = FILITIPOCARD.CDFILCENTPME
                                       AND NRCARDPME    = V_NRCARDPMENOVO
                                       AND CDSERVFILI   = SERVTIPOCARD.CDSERVFILI
                                       AND CDFILIAL     = FILITIPOCARD.CDFILIAL
                                       AND DTCARDIAPME  = C_DATAS.DTCARDIADESTPME)
                    AND EXISTS (SELECT 1 FROM FILIAL FI WHERE FI.CDFILIAL = FILITIPOCARD.CDFILIAL AND FI.CDFILCENTPME = FILITIPOCARD.CDFILCENTPME)
               ) LOOP
                  IF ((C_AGREGADA.FERIADO IS NULL) OR (C_AGREGADA.IDCARDIAUTIL = 'N')) THEN
                     IF C_AGREGADA.FERICARD IS NULL THEN
                        V_NO_DATA_FOUND := 'S';

                        V_QTCOMESTC := 0;
                        V_QTCLIESTC := 0;
                        V_QTFUNESTC := 0;
                        V_QTDOAESTC := 0;

                        /* Comensais das agregadas */
                        BEGIN
                           SELECT CDFILIAL   , QTCOMPMESEG, QTCOMPMETER, QTCOMPMEQUA,
                                  QTCOMPMEQUI, QTCOMPMESEX, QTCOMPMESAB, QTCOMPMEDOM,
                                  QTCOMPMEFER, QTCLIPMESEG, QTCLIPMETER, QTCLIPMEQUA,
                                  QTCLIPMEQUI, QTCLIPMESEX, QTCLIPMESAB, QTCLIPMEDOM,
                                  QTCLIPMEFER, QTFUNPMESEG, QTFUNPMETER, QTFUNPMEQUA,
                                  QTFUNPMEQUI, QTFUNPMESEX, QTFUNPMESAB, QTFUNPMEDOM,
                                  QTFUNPMEFER, QTDOAPMESEG, QTDOAPMETER, QTDOAPMEQUA,
                                  QTDOAPMEQUI, QTDOAPMESEX, QTDOAPMESAB, QTDOAPMEDOM,
                                  QTDOAPMEFER
                             INTO V_CDFILIAL   , V_QTCOMPMESEG, V_QTCOMPMETER, V_QTCOMPMEQUA,
                                  V_QTCOMPMEQUI, V_QTCOMPMESEX, V_QTCOMPMESAB, V_QTCOMPMEDOM,
                                  V_QTCOMPMEFER, V_QTCLIPMESEG, V_QTCLIPMETER, V_QTCLIPMEQUA,
                                  V_QTCLIPMEQUI, V_QTCLIPMESEX, V_QTCLIPMESAB, V_QTCLIPMEDOM,
                                  V_QTCLIPMEFER, V_QTFUNPMESEG, V_QTFUNPMETER, V_QTFUNPMEQUA,
                                  V_QTFUNPMEQUI, V_QTFUNPMESEX, V_QTFUNPMESAB, V_QTFUNPMEDOM,
                                  V_QTFUNPMEFER, V_QTDOAPMESEG, V_QTDOAPMETER, V_QTDOAPMEQUA,
                                  V_QTDOAPMEQUI, V_QTDOAPMESEX, V_QTDOAPMESAB, V_QTDOAPMEDOM,
                                  V_QTDOAPMEFER
                             FROM COMESERVTCAR C
                            WHERE (C.CDFILCENTPME = P_CDCENTRAL        )
                              AND (C.CDTIPOCARD   = P_CDTIPOCARDDEST   )
                              AND (C.CDSERVFILI   = P_CDSERVFILIDEST   )
                              AND (C.CDFILIAL     = C_AGREGADA.CDFILIAL)
                              AND EXISTS (SELECT 1 FROM FILIAL FI WHERE FI.CDFILIAL = C.CDFILIAL AND FI.CDFILCENTPME = C.CDFILCENTPME);
                        EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                              V_NO_DATA_FOUND := 'N';
                        END;

                        IF V_NO_DATA_FOUND = 'S' THEN
                           IF TO_CHAR(C_DATAS.DTCARDIADESTPME, 'D') = '1' THEN
                              V_QTCOMESTC := V_QTCOMPMEDOM;
                              V_QTCLIESTC := V_QTCLIPMEDOM;
                              V_QTFUNESTC := V_QTFUNPMEDOM;
                              V_QTDOAESTC := V_QTDOAPMEDOM;
                           END IF;

                           IF TO_CHAR(C_DATAS.DTCARDIADESTPME, 'D') = '2' THEN
                              V_QTCOMESTC := V_QTCOMPMESEG;
                              V_QTCLIESTC := V_QTCLIPMESEG;
                              V_QTFUNESTC := V_QTFUNPMESEG;
                              V_QTDOAESTC := V_QTDOAPMESEG;
                           END IF;

                           IF TO_CHAR(C_DATAS.DTCARDIADESTPME, 'D') = '3' THEN
                              V_QTCOMESTC := V_QTCOMPMETER;
                              V_QTCLIESTC := V_QTCLIPMETER;
                              V_QTFUNESTC := V_QTFUNPMETER;
                              V_QTDOAESTC := V_QTDOAPMETER;
                           END IF;

                           IF TO_CHAR(C_DATAS.DTCARDIADESTPME, 'D') = '4' THEN
                              V_QTCOMESTC := V_QTCOMPMEQUA;
                              V_QTCLIESTC := V_QTCLIPMEQUA;
                              V_QTFUNESTC := V_QTFUNPMEQUA;
                              V_QTDOAESTC := V_QTDOAPMEQUA;
                           END IF;

                           IF TO_CHAR(C_DATAS.DTCARDIADESTPME, 'D') = '5' THEN
                              V_QTCOMESTC := V_QTCOMPMEQUI;
                              V_QTCLIESTC := V_QTCLIPMEQUI;
                              V_QTFUNESTC := V_QTFUNPMEQUI;
                              V_QTDOAESTC := V_QTDOAPMEQUI;
                           END IF;

                           IF TO_CHAR(C_DATAS.DTCARDIADESTPME, 'D') = '6' THEN
                              V_QTCOMESTC := V_QTCOMPMESEX;
                              V_QTCLIESTC := V_QTCLIPMESEX;
                              V_QTFUNESTC := V_QTFUNPMESEX;
                              V_QTDOAESTC := V_QTDOAPMESEX;
                           END IF;

                           IF TO_CHAR(C_DATAS.DTCARDIADESTPME, 'D') = '7' THEN
                              V_QTCOMESTC := V_QTCOMPMESAB;
                              V_QTCLIESTC := V_QTCLIPMESAB;
                              V_QTFUNESTC := V_QTFUNPMESAB;
                              V_QTDOAESTC := V_QTDOAPMESAB;
                           END IF;
                        END IF; /* Se existe comensais para a filial agregada */

                        IF (V_QTCOMESTC IS NULL) THEN
                           V_QTCOMESTC := 0;
                        END IF;

                        IF (V_QTCLIESTC IS NULL) THEN
                           V_QTCLIESTC := 0;
                        END IF;

                        IF (V_QTFUNESTC IS NULL) THEN
                           V_QTFUNESTC := 0;
                        END IF;

                        IF (V_QTDOAESTC IS NULL) THEN
                           V_QTDOAESTC := 0;
                        END IF;

                        /* Cria o novo codigo para os novos lancamentos da cardia para o periodo */
                        NOVO_CODIGO('CARDIAPME' || P_CDCENTRAL || V_NRCARDPMENOVO, V_NRCARDIAPMENOVO, 1);
                        V_NRCARDIAPMENOVO := SUBSTR(V_NRCARDIAPMENOVO, 11, 10);

                        INSERT INTO CARDIAPME
                                    (CDFILCENTPME, NRCARDPME      , NRCARDIAPME , CDSERVFILI  ,
                                     CDFILIAL    , DTCARDIAPME    , QTCOMESTCPME, QTCLIESTCPME,
                                     QTFUNESTCPME, QTDOAESTCPME   , QTCOMEFECPME, QTCLIEFECPME,
                                     QTFUNEFECPME, QTDOAEFECPME   , IDCARDESPEC)
                             VALUES (P_CDCENTRAL , V_NRCARDPMENOVO, V_NRCARDIAPMENOVO, P_CDSERVFILIDEST    ,
                                     C_AGREGADA.CDFILIAL, C_DATAS.DTCARDIADESTPME, V_QTCOMESTC, V_QTCLIESTC,
                                     V_QTFUNESTC        , V_QTDOAESTC, 0                , 0                ,
                                     0                  , 0          , 'N');


                     END IF;    /* Feriado (DTFERICARD) das Agregadas */
                  END IF;       /* Feriado Filial ou IDCARDIAUTIL = 'N' das Agregadas*/
               END LOOP;        /* Agregadas */
            END IF;             /* Se o comensais da Central for zero nao inclui para as agregadas*/
         END IF;                /* Feriado (DTFERICARD) da Central */
      END IF;                   /* Feriado Filial ou IDCARDIAUTIL = 'N' da Central*/
   END LOOP;                    /* Datas destino */

   /* Inclui a composicao dos pratos */
   FOR C_COMPOSICAO IN (
      SELECT CC.NRCARDPME,
             CC.NRCARDIAPME,
             CC.NRSEQPRACPME,
             CC.CDFILAUXPRAT,
             CC.CDPRATO,
             CM.CDTIPOPRAT,
             CC.VRPECONSCPME,
             CC.NRORDPRACPME,
             CC.DTCARDIAPME
        FROM CONFCARDPME CM,
             (SELECT C.NRCARDPME,
                     CD.NRCARDIAPME,
                     C.NRSEQPRACPME,
                     C.CDFILAUXPRAT,
                     C.CDPRATO,
                     C.CDTIPOPRAT,
                     C.VRPECONSCPME,
                     C.NRORDPRACPME,
                     CD.DTCARDIAPME
                FROM CARDIAPME CD,
                     COMPCARDPME C
               WHERE CD.NRCARDPME    = P_NRCARDPME
                 AND CD.CDFILCENTPME = P_CDCENTRAL
                 AND CD.CDFILIAL     = P_CDCENTRAL
                 AND CD.CDSERVFILI   = P_CDSERVFILI
                 AND CD.NRCARDPME    = C.NRCARDPME
                 AND CD.CDFILCENTPME = C.CDFILCENTPME
                 AND CD.NRCARDIAPME  = C.NRCARDIAPME) CC
       WHERE CM.NRCARDPME    = V_NRCARDPMENOVO
         AND CM.CDFILCENTPME = P_CDCENTRAL
         AND CM.CDSERVFILI   = P_CDSERVFILIDEST
         AND CM.NRSEQCNFCPME = CC.NRSEQPRACPME(+)
       ORDER BY CC.DTCARDIAPME, CM.NRORDCNFCPME
   ) LOOP

      Begin
         /* Busca a data de destino da cardiapme de acordo com a data de origem na lista*/
         SELECT TO_DATE(DTCARDIADESTPME, 'DD/MM/YYYY') AS DTCARDIADESTPME
           INTO V_DTCARDIADESTPME
           FROM LISTA_DTCARDIAPME
          WHERE NRLISTA     = P_NRLISTACARDIAPME
            AND DTCARDIAPME = C_COMPOSICAO.DTCARDIAPME;
      Exception
         When No_Data_Found Then
            V_DTCARDIADESTPME := Null;
      End;

      Begin
         /* Pega o NRCARDIAPME dos novos registros da CARDIAPME */
         SELECT NRCARDIAPME
           INTO V_NRCARDIACOMP
           FROM CONFCARDPME CF, CARDIAPME CD
          WHERE CF.CDFILCENTPME  = CD.CDFILCENTPME
            AND CF.NRCARDPME     = CD.NRCARDPME
            AND CF.CDSERVFILI    = CD.CDSERVFILI
            AND CF.NRSEQCNFCPME  = C_COMPOSICAO.NRSEQPRACPME
            AND CF.CDTIPOPRAT    = C_COMPOSICAO.CDTIPOPRAT
            AND CD.CDFILCENTPME  = P_CDCENTRAL
            AND CD.CDFILIAL      = P_CDCENTRAL
            AND CD.NRCARDPME     = V_NRCARDPMENOVO
            AND CD.CDSERVFILI    = P_CDSERVFILIDEST
            AND CD.DTCARDIAPME   = V_DTCARDIADESTPME;
      Exception
         When No_Data_Found Then
            V_NRCARDIACOMP := Null;
      End;

      IF NOT V_NRCARDIACOMP IS NULL THEN
         INSERT INTO COMPCARDPME (CDFILCENTPME, NRCARDPME, NRCARDIAPME, NRSEQPRACPME, CDFILAUXPRAT,
                                  CDPRATO, CDTIPOPRAT, VRPECONSCPME, NRORDPRACPME)
                          VALUES (P_CDCENTRAL, V_NRCARDPMENOVO, V_NRCARDIACOMP, C_COMPOSICAO.NRSEQPRACPME, C_COMPOSICAO.CDFILAUXPRAT,
                                  C_COMPOSICAO.CDPRATO, C_COMPOSICAO.CDTIPOPRAT, C_COMPOSICAO.VRPECONSCPME, C_COMPOSICAO.NRORDPRACPME);
      END IF;
   END LOOP;

    DELETE FROM LISTA_DTCARDIAPME
    WHERE NRLISTA = P_NRLISTACARDIAPME;

   COMMIT;
END;
/