create or replace PROCEDURE  CALCULA_CURVAABC_PEDIDO(P_CDFILIAL       IN FILIAL.CDFILIAL%TYPE,
                                                    P_TIPOCURVA      IN VARCHAR2,
                                                    P_CDCLIENTE      IN CLIENTE.CDCLIENTE%TYPE,
                                                    P_IDTPENDECLIE   IN ENDECLIE.IDTPENDECLIE%TYPE,
                                                    P_CDPAIS         IN PAIS.CDPAIS%TYPE,
                                                    P_SGESTADO       IN ESTADO.SGESTADO%TYPE,
                                                    P_CDMUNICIPIO    IN MUNICIPIO.CDMUNICIPIO%TYPE,
                                                    P_CDGRUPCLIE     IN GRUPCLIE.CDGRUPCLIE%TYPE,
                                                    P_DTPEDIDOINI    IN PEDIDO.DTPEDIDO%TYPE,
                                                    P_DTPEDIDOFIM    IN PEDIDO.DTPEDIDO%TYPE,
                                                    P_DTENTPEDINI    IN PEDIDO.DTENTPED%TYPE,
                                                    P_DTENTPEDFIM    IN PEDIDO.DTENTPED%TYPE,
                                                    P_DTENTPEDTD     IN VARCHAR2,
                                                    P_IDSITPEDIDO    IN VARCHAR2,
                                                    P_CDVENDEDOR     IN VARCHAR2,
                                                    P_CDTIPOOPER     IN VARCHAR2,
                                                    P_DTEMISSAOINI   IN NOTAFISCAL.DTEMISSAO%TYPE,
                                                    P_DTEMISSAOFIM   IN NOTAFISCAL.DTEMISSAO%TYPE,
                                                    P_TDTEMISSAO     IN VARCHAR2,
                                                    P_DTENTRSAIDINI  IN NOTAFISCAL.DTENTRSAID%TYPE,
                                                    P_DTENTRSAIDFIM  IN NOTAFISCAL.DTENTRSAID%TYPE,
                                                    P_TDTENTRSAID    IN VARCHAR2,
                                                    P_IDSTATUS       IN NOTAFISCAL.IDSTATUS%TYPE,
                                                    P_IDDEVENTR      IN VARCHAR2,
                                                    P_CDTIPOOPERDEV  IN TIPOOPERAC.CDTIPOOPER%TYPE,
                                                    P_CDPRODUTOINI   IN PRODUTO.CDARVPROD%TYPE,
                                                    P_CDPRODUTOFIM   IN PRODUTO.CDARVPROD%TYPE,
                                                    P_NRCURVAABCPED   OUT TRABCURVAABCPED.NRCURVAABCPED%TYPE)
IS

    V_SEQ            NOVOCODIGO.NRSEQUENCIAL%TYPE;
    V_NRCURVAABCPED  TRABCURVAABCPED.NRCURVAABCPED%TYPE;
    V_NRSEQCURVA     INTEGER;
    V_CDFILIEMIT     NOTAFISCAL.CDFILIEMIT%TYPE;
    V_NRLANCTONF     NOTAFISCAL.NRLANCTONF%TYPE;
    V_NRNOTAFISC     NOTAFISCAL.NRNOTAFISC%TYPE;
    V_DTEMISSAO      NOTAFISCAL.DTEMISSAO%TYPE;
    V_DTENTRSAID     NOTAFISCAL.DTENTRSAID%TYPE;
    V_CDSERIE        NOTAFISCAL.CDSERIE%TYPE;
    V_IDSTATUS       NOTAFISCAL.IDSTATUS%TYPE;
    V_TOTAQTITEMPED  TRABCURVAABCPED.QTITEMPED%TYPE;
    V_VRTOTAITEM     TRABCURVAABCPED.VRITEM%TYPE;
    V_VRPECURVAA     PARAMGERAL.VRPECURVAA%TYPE;
    V_VRPECURVAB     PARAMGERAL.VRPECURVAB%TYPE;
    V_VRPECURVAC     PARAMGERAL.VRPECURVAC%TYPE;
    V_CURVAAPED      FLOAT;
    V_CURVABPED      FLOAT;
    V_CURVACPED      FLOAT;
    V_TOTAL          FLOAT;
    V_TOTAITEM       INTEGER;
    V_COUNT          INTEGER;
    V_TOTACLIE       INTEGER;
BEGIN
    DELETE FROM TRABCURVAABCPED;

    NOVO_CODIGO('CURVAABCPED' || P_CDFILIAL,V_SEQ);
    V_NRCURVAABCPED := SUBSTR(V_SEQ,11,10);
    V_NRSEQCURVA := 0;
    FOR C_PEDIDO IN (SELECT P.CDFILIAL, P.NRPEDIDO, P.DTPEDIDO, P.DTENTPED, P.IDSITPEDIDO,
                            P.CDCLIENTE,
                            E.IDTPENDECLIE, E.CDPAIS, E.SGESTADO, E.CDMUNICIPIO,
                            C.CDGRUPCLIE,
                            P.CDVENDEDOR ,
                            P.CDTIPOOPER,
                            I.CDPRODUTO, I.NRITEMPED, DECODE(IDSITITEMPED, '7', (I.QTITEMPED - I.QTEXPITEMPED), I.QTITEMPED) QTITEMPED, I.VRPREUNITPED, I.VRDESITPED, (((DECODE(IDSITITEMPED, '7', (I.QTITEMPED - I.QTEXPITEMPED), I.QTITEMPED)) * I.VRPREUNITPED) - NVL(I.VRDESITPED,0)) AS VRITEM
                       FROM PEDIDO P JOIN CLIENTE C
                                       ON P.CDCLIENTE = C.CDCLIENTE
                                     LEFT JOIN ENDECLIE E
                                            ON P.CDCLIENTE = E.CDCLIENTE
                                     JOIN ITEMPEDIDO I
                                       ON P.CDFILIAL = I.CDFILIAL
                                      AND P.NRPEDIDO = I.NRPEDIDO
                                     JOIN PRODUTO PR
                                       ON I.CDPRODUTO = PR.CDPRODUTO
                      WHERE (P.CDFILIAL = P_CDFILIAL)
                        AND ((P.CDCLIENTE = P_CDCLIENTE) OR (P_CDCLIENTE = 'T'))
                        AND (E.IDTPENDECLIE = P_IDTPENDECLIE)
                        AND ((INSTR(P_CDPAIS, E.CDPAIS||'_') > 0) OR (P_CDPAIS = 'T'))
                        AND ((INSTR(P_SGESTADO, E.SGESTADO||'_') > 0) OR (P_SGESTADO = 'T'))
                        AND ((INSTR(P_CDMUNICIPIO, E.CDMUNICIPIO||'_') > 0) OR (P_CDMUNICIPIO = 'T'))
                        AND ((C.CDGRUPCLIE = P_CDGRUPCLIE) OR (P_CDGRUPCLIE = 'T'))
                        AND (P.DTPEDIDO BETWEEN P_DTPEDIDOINI AND P_DTPEDIDOFIM)
                        AND ((P.DTENTPED BETWEEN P_DTENTPEDINI AND P_DTENTPEDFIM) OR (P_DTENTPEDTD = 'T'))
                        AND (INSTR(P_IDSITPEDIDO, P.IDSITPEDIDO) > 0)
                        AND ((INSTR(P_CDVENDEDOR, P.CDVENDEDOR||'_') > 0) OR (P_CDVENDEDOR = 'T'))
                        AND ((INSTR(P_CDTIPOOPER, P.CDTIPOOPER||'_') > 0) OR (P_CDTIPOOPER = 'T'))
                        AND (PR.CDARVPROD BETWEEN P_CDPRODUTOINI AND P_CDPRODUTOFIM))
    LOOP
       V_NRSEQCURVA := V_NRSEQCURVA +1;
       BEGIN
          SELECT MAX(N.CDFILIEMIT), MAX(N.NRLANCTONF), MAX(N.NRNOTAFISC), MAX(N.DTEMISSAO), MAX(N.DTENTRSAID), MAX(N.CDSERIE), MAX(N.IDSTATUS)
            INTO V_CDFILIEMIT, V_NRLANCTONF, V_NRNOTAFISC, V_DTEMISSAO, V_DTENTRSAID, V_CDSERIE, V_IDSTATUS
            FROM ITEMPRODUTO I JOIN NOTAFISCAL N
                                 ON I.CDFILIAL = N.CDFILIAL
                                AND I.NRLANCTONF = N.NRLANCTONF
           WHERE I.CDFILIALPED = C_PEDIDO.CDFILIAL
             AND I.NRPEDIDO = C_PEDIDO.NRPEDIDO
             AND I.NRITEMPED = C_PEDIDO.NRITEMPED
             AND ((N.DTEMISSAO BETWEEN P_DTEMISSAOINI AND P_DTEMISSAOFIM) OR (P_TDTEMISSAO = 'T'))
             AND ((N.DTENTRSAID BETWEEN P_DTENTRSAIDINI AND P_DTENTRSAIDFIM) OR (P_TDTENTRSAID = 'T'))
             AND ((INSTR(P_IDSTATUS, N.IDSTATUS) > 0) OR (P_IDSTATUS = 'T'));

       EXCEPTION
          WHEN NO_DATA_FOUND THEN
             V_CDFILIEMIT := NULL; V_NRLANCTONF := NULL; V_NRNOTAFISC := NULL; V_DTEMISSAO := NULL; V_DTENTRSAID := NULL; V_CDSERIE := NULL;
             V_IDSTATUS := NULL;
       END;

       INSERT INTO TRABCURVAABCPED
       (CDFILIAL, NRCURVAABCPED, NRSEQCURVA, NRPEDIDO, DTPEDIDO, DTENTPED, IDSITPEDIDO, CDCLIENTE, IDTPENDECLIE, CDGRUPCLIE, CDVENDEDOR, CDTIPOOPER,
        NRITEMPED, CDPRODUTO, QTITEMPED, VRPREUNITPED, VRDESITPED, VRITEM, IDTIPO,
        NRLANCTONF, NRNOTAFISC, DTEMISSAO, DTENTRSAID, CDSERIE)
       VALUES
       (C_PEDIDO.CDFILIAL, V_NRCURVAABCPED, V_NRSEQCURVA, C_PEDIDO.NRPEDIDO, C_PEDIDO.DTPEDIDO, C_PEDIDO.DTENTPED, C_PEDIDO.IDSITPEDIDO, C_PEDIDO.CDCLIENTE, C_PEDIDO.IDTPENDECLIE, C_PEDIDO.CDGRUPCLIE, C_PEDIDO.CDVENDEDOR, C_PEDIDO.CDTIPOOPER,
        C_PEDIDO.NRITEMPED, C_PEDIDO.CDPRODUTO, C_PEDIDO.QTITEMPED, C_PEDIDO.VRPREUNITPED, C_PEDIDO.VRDESITPED, C_PEDIDO.VRITEM, 'N',
        V_NRLANCTONF, V_NRNOTAFISC, V_DTEMISSAO, V_DTENTRSAID, V_CDSERIE);
    END LOOP;

    SELECT COUNT(1)
      INTO V_TOTAITEM
      FROM TRABCURVAABCPED
     WHERE NRCURVAABCPED = V_NRCURVAABCPED;

    BEGIN
       SELECT MAX(VRPECURVAA), MAX(VRPECURVAB), MAX(VRPECURVAC)
         INTO V_VRPECURVAA,    V_VRPECURVAB,    V_VRPECURVAC
         FROM PARAMGERAL
        WHERE CDPARGERAL = '0001';
    EXCEPTION
       WHEN NO_DATA_FOUND THEN
          V_VRPECURVAA := NULL; V_VRPECURVAB := NULL; V_VRPECURVAC := NULL;
    END;

    IF (P_TIPOCURVA = 'PRODUTO-QUANTIDADE') THEN
       V_CURVAAPED := ROUND(((V_VRPECURVAA*V_TOTAITEM)/100), 0);
       V_CURVABPED := ROUND(((V_VRPECURVAB*V_TOTAITEM)/100), 0);
       V_CURVACPED := ROUND(((V_VRPECURVAC*V_TOTAITEM)/100), 0);

       V_COUNT := 1;
       FOR C_CURVAA IN (SELECT CDFILIAL, NRPEDIDO, NRITEMPED, QTITEMPED
                         FROM TRABCURVAABCPED
                        WHERE NRCURVAABCPED = V_NRCURVAABCPED
                          AND IDCURVAPED IS NULL
                       ORDER BY QTITEMPED DESC)
       LOOP
          IF V_COUNT <= V_CURVAAPED THEN
             UPDATE TRABCURVAABCPED
                SET IDCURVAPED = 'A'
              WHERE CDFILIAL = C_CURVAA.CDFILIAL
                AND NRPEDIDO = C_CURVAA.NRPEDIDO
                AND NRITEMPED = C_CURVAA.NRITEMPED;
          END IF;
          V_COUNT := V_COUNT +1;
       END LOOP;

       V_COUNT := 1;
       FOR C_CURVAB IN (SELECT CDFILIAL, NRPEDIDO, NRITEMPED, QTITEMPED
                         FROM TRABCURVAABCPED
                        WHERE NRCURVAABCPED = V_NRCURVAABCPED
                          AND IDCURVAPED IS NULL
                       ORDER BY QTITEMPED DESC)
       LOOP
          IF V_COUNT <= V_CURVABPED THEN
             UPDATE TRABCURVAABCPED
                SET IDCURVAPED = 'B'
              WHERE CDFILIAL = C_CURVAB.CDFILIAL
                AND NRPEDIDO = C_CURVAB.NRPEDIDO
                AND NRITEMPED = C_CURVAB.NRITEMPED;
          END IF;
          V_COUNT := V_COUNT +1;
       END LOOP;


       FOR C_CURVAC IN (SELECT CDFILIAL, NRPEDIDO, NRITEMPED, QTITEMPED
                         FROM TRABCURVAABCPED
                        WHERE NRCURVAABCPED = V_NRCURVAABCPED
                          AND IDCURVAPED IS NULL
                          AND ROWNUM <= V_CURVACPED
                       ORDER BY QTITEMPED DESC)
       LOOP

             UPDATE TRABCURVAABCPED
                SET IDCURVAPED = 'C'
              WHERE CDFILIAL = C_CURVAC.CDFILIAL
                AND NRPEDIDO = C_CURVAC.NRPEDIDO
                AND NRITEMPED = C_CURVAC.NRITEMPED;
       END LOOP;
    ELSE
       IF (P_TIPOCURVA = 'PRODUTO-VALOR') THEN
          V_CURVAAPED := ROUND(((V_VRPECURVAA*V_TOTAITEM)/100), 2);
          V_CURVABPED := ROUND(((V_VRPECURVAB*V_TOTAITEM)/100), 2);
          V_CURVACPED := ROUND(((V_VRPECURVAC*V_TOTAITEM)/100), 2);

          V_COUNT := 1;
          FOR C_CURVAA IN (SELECT CDFILIAL, NRPEDIDO, NRITEMPED, VRITEM
                            FROM TRABCURVAABCPED
                           WHERE NRCURVAABCPED = V_NRCURVAABCPED
                             AND IDCURVAPED IS NULL
                          ORDER BY VRITEM DESC)
          LOOP
             IF V_COUNT <= V_CURVAAPED THEN
                UPDATE TRABCURVAABCPED
                   SET IDCURVAPED = 'A'
                 WHERE CDFILIAL = C_CURVAA.CDFILIAL
                   AND NRPEDIDO = C_CURVAA.NRPEDIDO
                   AND NRITEMPED = C_CURVAA.NRITEMPED;
             END IF;
             V_COUNT := V_COUNT +1;
          END LOOP;

          V_COUNT := 1;
          FOR C_CURVAB IN (SELECT CDFILIAL, NRPEDIDO, NRITEMPED, VRITEM
                            FROM TRABCURVAABCPED
                           WHERE NRCURVAABCPED = V_NRCURVAABCPED
                             AND IDCURVAPED IS NULL
                          ORDER BY VRITEM DESC)
          LOOP
             IF V_COUNT <= V_CURVABPED THEN
                UPDATE TRABCURVAABCPED
                   SET IDCURVAPED = 'B'
                 WHERE CDFILIAL = C_CURVAB.CDFILIAL
                   AND NRPEDIDO = C_CURVAB.NRPEDIDO
                   AND NRITEMPED = C_CURVAB.NRITEMPED;
             END IF;
             V_COUNT := V_COUNT +1;
          END LOOP;


          FOR C_CURVAC IN (SELECT CDFILIAL, NRPEDIDO, NRITEMPED, VRITEM
                            FROM TRABCURVAABCPED
                           WHERE NRCURVAABCPED = V_NRCURVAABCPED
                             AND IDCURVAPED IS NULL
                          ORDER BY VRITEM DESC)
          LOOP
                UPDATE TRABCURVAABCPED
                   SET IDCURVAPED = 'C'
                 WHERE CDFILIAL = C_CURVAC.CDFILIAL
                   AND NRPEDIDO = C_CURVAC.NRPEDIDO
                   AND NRITEMPED = C_CURVAC.NRITEMPED;
          END LOOP;
       ELSE
          SELECT COUNT(DISTINCT CDCLIENTE)
            INTO V_TOTACLIE
            FROM TRABCURVAABCPED
           WHERE NRCURVAABCPED = V_NRCURVAABCPED;

          IF (P_TIPOCURVA = 'CLIENTE-QUANTIDADE') THEN
             V_CURVAAPED := ROUND(((V_VRPECURVAA*V_TOTACLIE)/100), 0);
             V_CURVABPED := ROUND(((V_VRPECURVAB*V_TOTACLIE)/100), 0);
             V_CURVACPED := ROUND(((V_VRPECURVAC*V_TOTACLIE)/100), 0);

             V_COUNT := 1;
             FOR C_CURVAA IN (SELECT CDCLIENTE, SUM(QTITEMPED) QTITEMPED
                               FROM TRABCURVAABCPED
                              WHERE NRCURVAABCPED = V_NRCURVAABCPED
                                AND IDCURVAPED IS NULL
                             GROUP BY CDCLIENTE
                             ORDER BY 2 DESC)
             LOOP
                IF V_COUNT <= V_CURVAAPED THEN
                   FOR C_CURVACLIE IN (SELECT CDFILIAL, NRPEDIDO, NRITEMPED
                                         FROM TRABCURVAABCPED
                                        WHERE NRCURVAABCPED = V_NRCURVAABCPED
                                          AND CDCLIENTE = C_CURVAA.CDCLIENTE)
                   LOOP
                      UPDATE TRABCURVAABCPED
                         SET IDCURVAPED = 'A'
                       WHERE CDFILIAL  = C_CURVACLIE.CDFILIAL
                         AND NRPEDIDO  = C_CURVACLIE.NRPEDIDO
                         AND NRITEMPED = C_CURVACLIE.NRITEMPED;
                   END LOOP;
                   V_COUNT := V_COUNT + 1;
                END IF;
             END LOOP;

             V_COUNT := 1;
             FOR C_CURVAB IN (SELECT CDCLIENTE, SUM(QTITEMPED) QTITEMPED
                               FROM TRABCURVAABCPED
                              WHERE NRCURVAABCPED = V_NRCURVAABCPED
                                AND IDCURVAPED IS NULL
                             GROUP BY CDCLIENTE
                             ORDER BY 2 DESC)
             LOOP
                IF V_COUNT <= V_CURVABPED THEN
                   FOR C_CURVACLIE IN (SELECT CDFILIAL, NRPEDIDO, NRITEMPED
                                         FROM TRABCURVAABCPED
                                        WHERE NRCURVAABCPED = V_NRCURVAABCPED
                                          AND CDCLIENTE = C_CURVAB.CDCLIENTE)
                   LOOP
                      UPDATE TRABCURVAABCPED
                         SET IDCURVAPED = 'B'
                       WHERE CDFILIAL  = C_CURVACLIE.CDFILIAL
                         AND NRPEDIDO  = C_CURVACLIE.NRPEDIDO
                         AND NRITEMPED = C_CURVACLIE.NRITEMPED;
                   END LOOP;
                   V_COUNT := V_COUNT + 1;
                END IF;
             END LOOP;


             FOR C_CURVAC IN (SELECT CDCLIENTE, SUM(QTITEMPED) QTITEMPED
                               FROM TRABCURVAABCPED
                              WHERE NRCURVAABCPED = V_NRCURVAABCPED
                                AND IDCURVAPED IS NULL
                             GROUP BY CDCLIENTE
                             ORDER BY 2 DESC)
             LOOP

                   FOR C_CURVACLIE IN (SELECT CDFILIAL, NRPEDIDO, NRITEMPED
                                         FROM TRABCURVAABCPED
                                        WHERE NRCURVAABCPED = V_NRCURVAABCPED
                                          AND CDCLIENTE = C_CURVAC.CDCLIENTE)
                   LOOP
                      UPDATE TRABCURVAABCPED
                         SET IDCURVAPED = 'C'
                       WHERE CDFILIAL  = C_CURVACLIE.CDFILIAL
                         AND NRPEDIDO  = C_CURVACLIE.NRPEDIDO
                         AND NRITEMPED = C_CURVACLIE.NRITEMPED;
                   END LOOP;
             END LOOP;
          ELSE
             IF (P_TIPOCURVA = 'CLIENTE-VALOR') THEN
                V_CURVAAPED := ROUND(((V_VRPECURVAA*V_TOTACLIE)/100), 0);
                V_CURVABPED := ROUND(((V_VRPECURVAB*V_TOTACLIE)/100), 0);
                V_CURVACPED := ROUND(((V_VRPECURVAC*V_TOTACLIE)/100), 0);

                V_COUNT := 1;
                FOR C_CURVAA IN (SELECT CDCLIENTE, SUM(VRITEM) VRTOTACLIE
                                  FROM TRABCURVAABCPED
                                 WHERE NRCURVAABCPED = V_NRCURVAABCPED
                                   AND IDCURVAPED IS NULL
                                GROUP BY CDCLIENTE
                                ORDER BY 2 DESC)
                LOOP
                   IF V_COUNT <= V_CURVAAPED THEN
                      FOR C_CURVACLIE IN (SELECT CDFILIAL, NRPEDIDO, NRITEMPED
                                            FROM TRABCURVAABCPED
                                           WHERE NRCURVAABCPED = V_NRCURVAABCPED
                                             AND CDCLIENTE = C_CURVAA.CDCLIENTE)
                      LOOP
                         UPDATE TRABCURVAABCPED
                            SET IDCURVAPED = 'A'
                          WHERE CDFILIAL  = C_CURVACLIE.CDFILIAL
                            AND NRPEDIDO  = C_CURVACLIE.NRPEDIDO
                            AND NRITEMPED = C_CURVACLIE.NRITEMPED;
                      END LOOP;
                      V_COUNT := V_COUNT + 1;
                   END IF;
                END LOOP;

                V_COUNT := 1;
                FOR C_CURVAB IN (SELECT CDCLIENTE, SUM(VRITEM) VRTOTACLIE
                                  FROM TRABCURVAABCPED
                                 WHERE NRCURVAABCPED = V_NRCURVAABCPED
                                   AND IDCURVAPED IS NULL
                                GROUP BY CDCLIENTE
                                ORDER BY 2 DESC)
                LOOP
                   IF V_COUNT <= V_CURVABPED THEN
                      FOR C_CURVACLIE IN (SELECT CDFILIAL, NRPEDIDO, NRITEMPED
                                            FROM TRABCURVAABCPED
                                           WHERE NRCURVAABCPED = V_NRCURVAABCPED
                                             AND CDCLIENTE = C_CURVAB.CDCLIENTE)
                      LOOP
                         UPDATE TRABCURVAABCPED
                            SET IDCURVAPED = 'B'
                          WHERE CDFILIAL  = C_CURVACLIE.CDFILIAL
                            AND NRPEDIDO  = C_CURVACLIE.NRPEDIDO
                            AND NRITEMPED = C_CURVACLIE.NRITEMPED;
                      END LOOP;
                      V_COUNT := V_COUNT + 1;
                   END IF;
                END LOOP;


                FOR C_CURVAC IN (SELECT CDCLIENTE, SUM(VRITEM) VRTOTACLIE
                                  FROM TRABCURVAABCPED
                                 WHERE NRCURVAABCPED = V_NRCURVAABCPED
                                   AND IDCURVAPED IS NULL
                                GROUP BY CDCLIENTE
                                ORDER BY 2 DESC)
                LOOP

                      FOR C_CURVACLIE IN (SELECT CDFILIAL, NRPEDIDO, NRITEMPED
                                            FROM TRABCURVAABCPED
                                           WHERE NRCURVAABCPED = V_NRCURVAABCPED
                                             AND CDCLIENTE = C_CURVAC.CDCLIENTE)
                      LOOP
                         UPDATE TRABCURVAABCPED
                            SET IDCURVAPED = 'C'
                          WHERE CDFILIAL  = C_CURVACLIE.CDFILIAL
                            AND NRPEDIDO  = C_CURVACLIE.NRPEDIDO
                            AND NRITEMPED = C_CURVACLIE.NRITEMPED;
                      END LOOP;
                END LOOP;
             END IF;
          END IF;
       END IF;
    END IF;
    P_NRCURVAABCPED := V_NRCURVAABCPED;
END;
/

