create or replace PROCEDURE SUBSTITUI_PRATO_PAE (
    P_CDCENTRAL     IN   VARCHAR2,
    P_LISTATPCARD   IN   VARCHAR2,
    P_LISTASERV     IN   VARCHAR2,
    P_DTINICIAL     IN   DATE,
    P_DTFINAL       IN   DATE,
    P_CDPRATORIG    IN   VARCHAR2,
    P_CDFAPRATORIG  IN   VARCHAR2,
    P_CDPRATDEST    IN   VARCHAR2,
    P_CDFAPRATDEST  IN   VARCHAR2,
    P_CDFILIESP     IN   VARCHAR2,
    P_NRLOGINTCARD  OUT  VARCHAR2
) IS

    V_IDUTRPADCA    VARCHAR2(1);
    V_QTDIABLOQCAR  NUMBER(2);
    V_IDUTILDTPROC  VARCHAR2(1);
    V_DTPROC        DATE;
    V_DTINICIAL     DATE;
    V_TIPORECE      VARCHAR2(1);
    V_NRLOGINTCARD  VARCHAR2(20);
    V_PRODINATIVO   VARCHAR2(1) := '0';
    V_QTCOMENSAIS   NUMBER;
    V_PRATO_ATIVO   VARCHAR2(1);
	V_PRATOORI      BOOLEAN;

    PROCEDURE INSERE_LOG (
        P_NRLOGINTCARD  IN  NUMBER,
        P_CDFILCENTPME  VARCHAR2,
        P_CDTIPOCARD    VARCHAR2,
        P_DTCARDIAPME   IN  DATE,
        P_CDSERVFILI    IN  VARCHAR2,
        P_CDFILIAL      IN  VARCHAR2,
        P_CDPRATO       IN  VARCHAR2,
        P_CDFILAUXPRAT  IN  VARCHAR2,
        P_CDPRODUTO     IN  VARCHAR2,
        P_ERRO          IN  NUMBER
    ) AS
    BEGIN
		/*
			0 - Produto <CDARVPROD - NMPRODUTO> inativo na receita <padrão ou local ou tipo de cardápio ou serviço> do prato de destino.
			1 - Comensais zerados no dia <DTCARDIAPME> do cardápio do tipo de prato <CDTIPOCARD - NMTIPOCARD> serviço <CDSERVFILI - NMSERVICO> da <central ou unidade>: <CDFILIAL - NMFILIAL>.
			2 - Prato de destino sem receita cadastrada.
			3 - Prato de destino sem receita revisada.
			4 - Intervalo de data fora do período permitido. Verifique a data de processamento.
			5 - Prato de destino inativo.
			6 - Prato de origem não encontrada nos cardápios.
		*/
        INSERT INTO LOGINTCARDPAE (
            NRLOGINTCARDPAE,
            CDFILCENTPME,
            CDTIPOCARD,
            DTCARDIAPME,
            CDSERVFILI,
            CDFILIAL,
            CDPRATO,
            CDFILAUXPRAT,
            CDPRODUTO,
            NRERRO
        ) VALUES (
            P_NRLOGINTCARD,
            P_CDFILCENTPME,
            P_CDTIPOCARD,
            P_DTCARDIAPME,
            P_CDSERVFILI,
            P_CDFILIAL,
            P_CDPRATO,
            P_CDFILAUXPRAT,
            P_CDPRODUTO,
            P_ERRO
        );

    END;

BEGIN
	/* VERIFICA PARAMETRO UTILIZA RECEITA PADRAO NO CARDAPIO */
    SELECT
        NVL(IDUTRPADCA, 'N'),
        NVL(QTDIABLOQCAR, 0),
        NVL(IDUTILDTPROC, 'N')
    INTO
        V_IDUTRPADCA,
        V_QTDIABLOQCAR,
        V_IDUTILDTPROC
    FROM
        PARAMGERAL
    WHERE
        CDPARGERAL = '0001';    

	/*TRATA DATA DE PROCESSAMENTO*/
    SELECT
        DECODE(V_IDUTILDTPROC, 'S', PF.DTPROCESSA, TRUNC(SYSDATE))
    INTO V_DTPROC
    FROM
        PARAMFILIAL PF
    WHERE
        PF.CDFILIAL = P_CDCENTRAL;

    IF ( V_DTPROC IS NULL ) THEN
        V_DTPROC := TRUNC(SYSDATE);
    ELSE
        V_DTPROC := V_DTPROC + V_QTDIABLOQCAR;
    END IF;

    IF ( P_DTINICIAL <= V_DTPROC ) THEN
        V_DTINICIAL := V_DTPROC;
        IF ( P_DTFINAL <= V_DTPROC ) THEN
            IF V_NRLOGINTCARD IS NULL THEN
                NOVO_CODIGO('LOGINTCARDPAE' || P_CDCENTRAL, V_NRLOGINTCARD, 1);
                V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD, 11, 10);
            END IF;

            INSERE_LOG(V_NRLOGINTCARD, P_CDCENTRAL, '', '', '',
                      '', '', '', '', 4);

            P_NRLOGINTCARD := NVL(V_NRLOGINTCARD, '');
            RETURN;
        END IF;

    ELSE
        V_DTINICIAL := P_DTINICIAL;

    END IF;
    
    SELECT
        NVL(IDATIVO, 'S')
        INTO V_PRATO_ATIVO
        FROM
            PRATO
        WHERE
                CDPRATO = P_CDPRATDEST
            AND CDFILAUXPRAT = P_CDFAPRATDEST;

        IF ( V_PRATO_ATIVO = 'N' ) THEN
            IF V_NRLOGINTCARD IS NULL THEN
                NOVO_CODIGO('LOGINTCARDPAE' || P_CDCENTRAL, V_NRLOGINTCARD, 1);
                V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD, 11, 10);
            END IF;

            INSERE_LOG(V_NRLOGINTCARD, P_CDCENTRAL, '', '', '',
                      '', P_CDPRATDEST, P_CDFAPRATDEST, '',
                      5);
            P_NRLOGINTCARD := NVL(V_NRLOGINTCARD, '');
            RETURN;
        END IF;
		
	V_PRATOORI := FALSE;

	/* PERCORRE CARDAPIO NO PERIODO SELECIONADO */
    FOR C_CARDAPIOS IN (
        SELECT
            C.CDTIPOCARD,
            C.CDFILCENTPME,
            C.NRCARDPME,
            D.NRCARDIAPME,
            D.DTCARDIAPME,
            D.CDSERVFILI,
            D.CDFILIAL,
            D.IDCARDESPEC,
            P.CDPRATO,
            P.CDFILAUXPRAT,
            NVL(D.QTCOMESTCPME, 0) AS QTCOMESTCPME
        FROM
                 CARDAPIOPME C
            INNER JOIN LISTA_TIPOCARD  T ON T.NRLISTA = P_LISTATPCARD
                                           AND T.CDFILCENTPME = C.CDFILCENTPME
                                           AND T.CDTIPOCARD = C.CDTIPOCARD
            INNER JOIN CARDIAPME       D ON D.CDFILCENTPME = C.CDFILCENTPME
                                      AND D.NRCARDPME = C.NRCARDPME
                                      AND D.DTCARDIAPME BETWEEN V_DTINICIAL AND P_DTFINAL
                                      AND ( P_CDFILIESP IS NULL
                                            OR ( D.CDFILIAL = P_CDFILIESP
                                                 AND D.IDCARDESPEC = 'S' ) )
                               -- AND D.QTCOMESTCPME > 0
            INNER JOIN LISTA_SERVICO   S ON S.NRLISTA = P_LISTASERV
                                          AND S.CDFILIAL = D.CDFILCENTPME
                                          AND S.CDSERVFILI = D.CDSERVFILI
            INNER JOIN COMPCARDPME     P ON P.CDFILCENTPME = D.CDFILCENTPME
                                        AND P.NRCARDPME = D.NRCARDPME
                                        AND P.NRCARDIAPME = D.NRCARDIAPME
                                        AND P.CDPRATO = P_CDPRATORIG
                                        AND P.CDFILAUXPRAT = P_CDFAPRATORIG
        WHERE
            C.CDFILCENTPME = P_CDCENTRAL
        ORDER BY
            C.CDTIPOCARD,
            C.CDFILCENTPME,
            C.NRCARDPME,
            D.NRCARDIAPME,
            D.DTCARDIAPME,
            D.CDSERVFILI,
            D.CDFILIAL
    ) LOOP
		V_PRATOORI := TRUE;
		
        IF (
            C_CARDAPIOS.QTCOMESTCPME = 0
            AND ( (
                P_CDFILIESP IS NULL
                AND C_CARDAPIOS.CDFILIAL = P_CDCENTRAL
            ) OR  -- CENTRAL
             (
                C_CARDAPIOS.CDFILIAL = P_CDFILIESP
                AND C_CARDAPIOS.IDCARDESPEC = 'S'
            ) -- ESPECIFICA
             )
        ) THEN
			/* 1- Comensais do dia zerados */
            IF V_NRLOGINTCARD IS NULL THEN
                NOVO_CODIGO('LOGINTCARDPAE' || C_CARDAPIOS.CDFILCENTPME, V_NRLOGINTCARD, 1);
                V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD, 11, 10);
            END IF;

            INSERE_LOG(V_NRLOGINTCARD, C_CARDAPIOS.CDFILCENTPME, C_CARDAPIOS.CDTIPOCARD, C_CARDAPIOS.DTCARDIAPME,
                      C_CARDAPIOS.CDSERVFILI,
                      C_CARDAPIOS.CDFILIAL,
                      '',
                      '',
                      '',
                      1);

        ELSE				
			/* VERIFICAR RECEITA UTILIZADA  */
            V_TIPORECE := 'N';
            SELECT
                NVL(MAX(TIPO), 'N')
            INTO V_TIPORECE
            FROM
                (
                    SELECT
                        'S' AS TIPO
                    FROM
                        RECESERV
                    WHERE
                            CDFILIAL = C_CARDAPIOS.CDFILIAL
                        AND CDSERVFILI = C_CARDAPIOS.CDSERVFILI
                        AND CDPRATO = P_CDPRATDEST
                        AND CDFILAUXPRAT = P_CDFAPRATDEST
                    UNION ALL
                    SELECT
                        'F' AS TIPO
                    FROM
                        RECEFILI F
                    WHERE
                            CDFILIAL = C_CARDAPIOS.CDFILIAL
                        AND CDPRATO = P_CDPRATDEST
                        AND CDFILAUXPRAT = P_CDFAPRATDEST
                        AND NOT EXISTS (
                            SELECT
                                CDPRATO
                            FROM
                                RECESERV
                            WHERE
                                    CDFILIAL = C_CARDAPIOS.CDFILIAL
                                AND CDSERVFILI = C_CARDAPIOS.CDSERVFILI
                                AND CDPRATO = P_CDPRATDEST
                                AND CDFILAUXPRAT = P_CDFAPRATDEST
                        )
                    UNION ALL
                    SELECT
                        'T' AS TIPO
                    FROM
                        RECETPCARD
                    WHERE
                            CDFILCENTPME = C_CARDAPIOS.CDFILCENTPME
                        AND CDTIPOCARD = C_CARDAPIOS.CDTIPOCARD
                        AND CDPRATO = P_CDPRATDEST
                        AND CDFILAUXPRAT = P_CDFAPRATDEST
                        AND NOT EXISTS (
                            SELECT
                                CDPRATO
                            FROM
                                RECEFILI
                            WHERE
                                    CDFILIAL = C_CARDAPIOS.CDFILIAL
                                AND CDPRATO = P_CDPRATDEST
                                AND CDFILAUXPRAT = P_CDFAPRATDEST
                        )
                        AND NOT EXISTS (
                            SELECT
                                CDPRATO
                            FROM
                                RECESERV
                            WHERE
                                    CDFILIAL = C_CARDAPIOS.CDFILIAL
                                AND CDSERVFILI = C_CARDAPIOS.CDSERVFILI
                                AND CDPRATO = P_CDPRATDEST
                                AND CDFILAUXPRAT = P_CDFAPRATDEST
                        )
                    UNION ALL
                    SELECT
                        'P' AS TIPO
                    FROM
                        RECEPADR P
                    WHERE
                            CDPRATO = P_CDPRATDEST
                        AND CDFILAUXPRAT = P_CDFAPRATDEST
                        AND NOT EXISTS (
                            SELECT
                                CDPRATO
                            FROM
                                RECETPCARD
                            WHERE
                                    CDFILCENTPME = C_CARDAPIOS.CDFILCENTPME
                                AND CDTIPOCARD = C_CARDAPIOS.CDTIPOCARD
                                AND CDPRATO = P_CDPRATDEST
                                AND CDFILAUXPRAT = P_CDFAPRATDEST
                        )
                        AND NOT EXISTS (
                            SELECT
                                CDPRATO
                            FROM
                                RECEFILI
                            WHERE
                                    CDFILIAL = C_CARDAPIOS.CDFILIAL
                                AND CDPRATO = P_CDPRATDEST
                                AND CDFILAUXPRAT = P_CDFAPRATDEST
                        )
                        AND NOT EXISTS (
                            SELECT
                                CDPRATO
                            FROM
                                RECESERV
                            WHERE
                                    CDFILIAL = C_CARDAPIOS.CDFILIAL
                                AND CDSERVFILI = C_CARDAPIOS.CDSERVFILI
                                AND CDPRATO = P_CDPRATDEST
                                AND CDFILAUXPRAT = P_CDFAPRATDEST
                        )
                ) A;

            IF ( V_TIPORECE = 'N' ) THEN
				/* 2- Prato sem receita */
                IF V_NRLOGINTCARD IS NULL THEN
                    NOVO_CODIGO('LOGINTCARDPAE' || C_CARDAPIOS.CDFILCENTPME, V_NRLOGINTCARD, 1);
                    V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD, 11, 10);
                END IF;

                INSERE_LOG(V_NRLOGINTCARD, C_CARDAPIOS.CDFILCENTPME, C_CARDAPIOS.CDTIPOCARD, C_CARDAPIOS.DTCARDIAPME,
                          C_CARDAPIOS.CDSERVFILI,
                          C_CARDAPIOS.CDFILIAL,
                          P_CDPRATDEST,
                          P_CDFAPRATDEST,
                          '',
                          2);
				P_NRLOGINTCARD := NVL(V_NRLOGINTCARD, '');
				RETURN;

            ELSE
                IF (
                    V_TIPORECE = 'P'
                    AND V_IDUTRPADCA = 'N'
                ) THEN
				  /* 3- Prato de destino sem receita revisada */
                    IF V_NRLOGINTCARD IS NULL THEN
                        NOVO_CODIGO('LOGINTCARDPAE' || C_CARDAPIOS.CDFILCENTPME, V_NRLOGINTCARD, 1);
                        V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD, 11, 10);
                    END IF;

                    INSERE_LOG(V_NRLOGINTCARD, C_CARDAPIOS.CDFILCENTPME, C_CARDAPIOS.CDTIPOCARD, C_CARDAPIOS.DTCARDIAPME,
                              C_CARDAPIOS.CDSERVFILI,
                              C_CARDAPIOS.CDFILIAL,
                              P_CDPRATDEST,
                              P_CDFAPRATDEST,
                              '',
                              3);
							  
					P_NRLOGINTCARD := NVL(V_NRLOGINTCARD, '');
					RETURN;

                ELSE
                    V_PRODINATIVO := '0';
                    IF ( V_TIPORECE = 'S' ) THEN  /*RECEITA POR SERVIÇO COM PRODUTO INATIVO*/
                        FOR C_RECESERV IN (
                            SELECT
                                P.CDPRODUTO,
                                P.IDPRODATIVPR,
                                PF.IDPRODATIV
                            FROM
                                     RECESERV RS
                                INNER JOIN PRODUTO   P ON P.CDPRODUTO = RS.CDPRODUTO
                                                        AND P.IDPRODATIVPR = 'N'
                                LEFT JOIN PRODFILI  PF ON PF.CDPRODUTO = P.CDPRODUTO
                                                         AND PF.CDFILIAL = C_CARDAPIOS.CDFILIAL
                                                         AND PF.IDPRODATIV = 'I'
                            WHERE
                                    RS.CDFILIAL = C_CARDAPIOS.CDFILIAL
                                AND RS.CDSERVFILI = C_CARDAPIOS.CDSERVFILI
                                AND RS.CDPRATO = P_CDPRATDEST
                                AND RS.CDFILAUXPRAT = P_CDFAPRATDEST
                        ) LOOP
                            IF V_NRLOGINTCARD IS NULL THEN
                                NOVO_CODIGO('LOGINTCARDPAE' || C_CARDAPIOS.CDFILCENTPME, V_NRLOGINTCARD, 1);
                                V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD, 11, 10);
                            END IF;

                            V_PRODINATIVO := '1';
                            INSERE_LOG(V_NRLOGINTCARD, C_CARDAPIOS.CDFILCENTPME, '', C_CARDAPIOS.DTCARDIAPME,
                                      C_CARDAPIOS.CDSERVFILI,
                                      C_CARDAPIOS.CDFILIAL,
                                      P_CDPRATDEST,
                                      P_CDFAPRATDEST,
                                      C_RECESERV.CDPRODUTO,
                                      0);

                        END LOOP;

                    END IF;

                    IF ( V_TIPORECE = 'F' ) THEN  /*RECEITA LOCAL COM PRODUTO INATIVO*/
                        FOR C_RECEFILI IN (
                            SELECT
                                P.CDPRODUTO,
                                P.IDPRODATIVPR,
                                PF.IDPRODATIV
                            FROM
                                     RECEFILI RF
                                INNER JOIN PRODUTO   P ON P.CDPRODUTO = RF.CDPRODUTO
                                                        AND P.IDPRODATIVPR = 'N'
                                LEFT JOIN PRODFILI  PF ON PF.CDPRODUTO = P.CDPRODUTO
                                                         AND PF.CDFILIAL = RF.CDFILIAL
                                                         AND PF.IDPRODATIV = 'I'
                            WHERE
                                    RF.CDFILIAL = C_CARDAPIOS.CDFILIAL
                                AND RF.CDPRATO = P_CDPRATDEST
                                AND RF.CDFILAUXPRAT = P_CDFAPRATDEST
                        ) LOOP
                            IF V_NRLOGINTCARD IS NULL THEN
                                NOVO_CODIGO('LOGINTCARDPAE' || C_CARDAPIOS.CDFILIAL, V_NRLOGINTCARD, 1);
                                V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD, 11, 10);
                            END IF;

                            V_PRODINATIVO := '1';
                            INSERE_LOG(V_NRLOGINTCARD, C_CARDAPIOS.CDFILCENTPME, '', C_CARDAPIOS.DTCARDIAPME, '',
                                      C_CARDAPIOS.CDFILIAL,
                                      P_CDPRATDEST,
                                      P_CDFAPRATDEST,
                                      C_RECEFILI.CDPRODUTO,
                                      0);

                        END LOOP;
                    END IF;

                    IF ( V_TIPORECE = 'T' ) THEN   /*RECEITA TIPO DE CARDAPIO COM PRODUTO INATIVO*/
                        FOR C_RECETPCARD IN (
                            SELECT
                                P.CDPRODUTO,
                                P.IDPRODATIVPR,
                                PF.IDPRODATIV
                            FROM
                                     RECETPCARD RT
                                INNER JOIN PRODUTO   P ON P.CDPRODUTO = RT.CDPRODUTO
                                                        AND P.IDPRODATIVPR = 'N'
                                LEFT JOIN PRODFILI  PF ON PF.CDPRODUTO = P.CDPRODUTO
                                                         AND PF.CDFILIAL = RT.CDFILCENTPME
                                                         AND PF.IDPRODATIV = 'I'
                            WHERE
                                    RT.CDFILCENTPME = C_CARDAPIOS.CDFILCENTPME
                                AND RT.CDTIPOCARD = C_CARDAPIOS.CDTIPOCARD
                                AND RT.CDPRATO = P_CDPRATDEST
                                AND RT.CDFILAUXPRAT = P_CDFAPRATDEST
                                AND RT.CDPRODUTO = P.CDPRODUTO
                        ) LOOP
                            IF V_NRLOGINTCARD IS NULL THEN
                                NOVO_CODIGO('LOGINTCARDPAE' || C_CARDAPIOS.CDFILCENTPME, V_NRLOGINTCARD, 1);
                                V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD, 11, 10);
                            END IF;

                            V_PRODINATIVO := '1';
                            INSERE_LOG(V_NRLOGINTCARD, C_CARDAPIOS.CDFILCENTPME, C_CARDAPIOS.CDTIPOCARD, C_CARDAPIOS.DTCARDIAPME,
                                      '',
                                      '',
                                      P_CDPRATDEST,
                                      P_CDFAPRATDEST,
                                      C_RECETPCARD.CDPRODUTO,
                                      0);

                        END LOOP;
                    END IF;

                    IF ( V_TIPORECE = 'P' ) THEN   /*RECEITA PADRÃO COM PRODUTO INATIVO*/
                        FOR C_RECEPADR IN (
                            SELECT
                                P.CDPRODUTO,
                                P.IDPRODATIVPR,
                                PF.IDPRODATIV
                            FROM
                                     RECEPADR RP
                                INNER JOIN PRODUTO   P ON P.CDPRODUTO = RP.CDPRODUTO
                                                        AND P.IDPRODATIVPR = 'N'
                                LEFT JOIN PRODFILI  PF ON PF.CDPRODUTO = P.CDPRODUTO
                                                         AND PF.IDPRODATIV = 'I'
                            WHERE
                                    RP.CDPRATO = P_CDPRATDEST
                                AND RP.CDFILAUXPRAT = P_CDFAPRATDEST
                                AND RP.CDPRODUTO = P.CDPRODUTO
                        ) LOOP
                            IF V_NRLOGINTCARD IS NULL THEN
                                NOVO_CODIGO('LOGINTCARDPAE' || C_CARDAPIOS.CDFILCENTPME, V_NRLOGINTCARD, 1);
                                V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD, 11, 10);
                            END IF;

                            V_PRODINATIVO := '1';
                            INSERE_LOG(V_NRLOGINTCARD, C_CARDAPIOS.CDFILCENTPME, '', C_CARDAPIOS.DTCARDIAPME, '',
                                      '',
                                      P_CDPRATDEST,
                                      P_CDFAPRATDEST,
                                      C_RECEPADR.CDPRODUTO,
                                      0);

                        END LOOP;
                    END IF;

                    IF ( V_PRODINATIVO = '0' ) THEN
						/* OK - Substitui prato */
                        UPDATE COMPCARDPME
                        SET
                            CDPRATO = P_CDPRATDEST,
                            CDFILAUXPRAT = P_CDFAPRATDEST
                        WHERE
                                CDFILCENTPME = C_CARDAPIOS.CDFILCENTPME
                            AND NRCARDPME = C_CARDAPIOS.NRCARDPME
                            AND NRCARDIAPME = C_CARDAPIOS.NRCARDIAPME
                            AND CDPRATO = P_CDPRATORIG
                            AND CDFILAUXPRAT = P_CDFAPRATORIG;

						/* ATUALIZA PERCENTUAL DE ADESAO QUANDO INFORMADA UMA UNIDADE ESPECIFICA */
                        IF (
                            C_CARDAPIOS.CDFILIAL = P_CDFILIESP
                            AND C_CARDAPIOS.IDCARDESPEC = 'S'
                        ) THEN
                            BEGIN
                                SELECT
                                    C_CARDAPIOS.QTCOMESTCPME + ( ( PA.VRPERACEIT / 100 ) * C_CARDAPIOS.QTCOMESTCPME ) AS COMENSAIS
                                INTO V_QTCOMENSAIS
                                FROM
                                    PERACEITPRATO PA
                                WHERE
                                        PA.CDFILCENTPME = P_CDCENTRAL
                                    AND PA.CDFILIAL = P_CDFILIESP
                                    AND PA.CDTIPOCARD = C_CARDAPIOS.CDTIPOCARD
                                    AND PA.CDPRATO = P_CDPRATDEST
                                    AND PA.CDFILAUXPRAT = P_CDFAPRATDEST;

                            EXCEPTION
                                WHEN NO_DATA_FOUND THEN
                                    V_QTCOMENSAIS := C_CARDAPIOS.QTCOMESTCPME;
                            END;

                            UPDATE CARDIAPME
                            SET
                                QTCOMESTCPME = V_QTCOMENSAIS,
                                QTCLIESTCPME = V_QTCOMENSAIS
                            WHERE
                                    CDFILCENTPME = P_CDCENTRAL
                                AND NRCARDPME = C_CARDAPIOS.NRCARDPME
                                AND NRCARDIAPME = C_CARDAPIOS.NRCARDIAPME
                                AND CDSERVFILI = C_CARDAPIOS.CDSERVFILI
                                AND CDFILIAL = C_CARDAPIOS.CDFILIAL
                                AND DTCARDIAPME = C_CARDAPIOS.DTCARDIAPME
                                AND IDCARDESPEC = 'S';

                        END IF;

                    ELSE 						
						IF (NVL(V_NRLOGINTCARD, '') <> '') THEN
							P_NRLOGINTCARD := NVL(V_NRLOGINTCARD, '');
							RETURN;
						END IF;
					END IF;

                END IF;
            END IF;

        END IF;
    END LOOP;
	
	IF V_PRATOORI = FALSE THEN
		IF V_NRLOGINTCARD IS NULL THEN
			NOVO_CODIGO('LOGINTCARDPAE' || P_CDCENTRAL, V_NRLOGINTCARD, 1);
			V_NRLOGINTCARD := SUBSTR(V_NRLOGINTCARD, 11, 10);
		END IF;
		
		INSERE_LOG(V_NRLOGINTCARD, P_CDCENTRAL, '', '', '', '', '', '','', 6);
	END IF;

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


-- VERSAO --
BEGIN ATUVEROBJ('SUBSTITUI_PRATO_PAE','X.XX.XXX'); END;
/
