create or replace PROCEDURE GERA_PEDIDO_FECHAMENTO(P_CDFILIAL IN PEDIDO.CDFILIAL%TYPE,
                                                                 P_NRPEDIDO   IN PEDIDO.NRPEDIDO%TYPE,
                                                                 P_CDOPERADOR IN OPERADOR.CDOPERADOR%TYPE)
AS
   V_IDEXIBIMPITPED   PARAMPEDIDO.IDEXIBIMPITPED%TYPE;
   V_IDUTRESELOTE     PARAMPEDIDO.IDUTRESELOTE%TYPE;
   V_IDCTRLVENEXP     PAREXPEDICAO.IDCTRLVENEXP%TYPE;
   V_CDCLIENTE        CLIENTE.CDCLIENTE%TYPE;
   V_CDFILIALPED      FILIAL.CDFILIAL%TYPE;
   V_CDTIPOOPER       TIPOOPERAC.CDTIPOOPER%TYPE;
   V_CDVENDEDOR       VENDEDOR.CDVENDEDOR%TYPE;
   V_IDSITPEDIDO      PEDIDO.IDSITPEDIDO%TYPE;
   V_CDMODPGTO        PEDIDO.CDMODPGTO%TYPE;
   V_IDTIPOPEDIDO     PEDIDO.IDTIPOPEDIDO%TYPE;
   V_IDATUESTTPOP     TIPOOPERAC.IDATUESTTPOP%TYPE;
   V_IDSITPEDIDOPAR   PEDIDO.IDSITPEDIDO%TYPE;
   V_CDEMPRESA        EMPRESA.CDINSCESTA%TYPE;
   V_CDINSCESTA       FILIAL.CDINSCESTA%TYPE;
   V_IDORIGDESTOP     TIPOOPERAC.IDORIGDESTOP%TYPE;
   V_IDSITITEMPEDPAR  ITEMPEDIDO.IDSITITEMPED%TYPE;
   V_VRPEDIDO         ITEMPEDIDO.VRPREUNITPED%TYPE;
   V_VRPARCELATOT     PRAZOPGTOPED.VRPARCELA%TYPE;
   V_NRDIASPGTOPED    PRAZOPGTOPED.NRDIASPGTOPED%TYPE;
   V_EXISTERESEVA     INTEGER;
   V_COUNTPARCELASMOD INTEGER;
   V_COUNTPARCELASPED INTEGER;
   V_EXIST_PRAZ       INTEGER;
   V_VRRECEBIMENTO    PRAZOPGTOPED.VRPARCELA%TYPE;
   V_QTRESPRODPED     Float;
   V_DTENTPED         PEDIDO.DTENTPED%TYPE;
   V_HRENTRPROD       PARAMPRODMRP.HRENTRPROD%TYPE;
   V_DTENTITPED       ITEMPEDIDO.DTENTITPED%TYPE;
   V_CDROTATRANSPED   ROTATRANS.CDROTATRANS%TYPE;
   V_CDROTATRANS      ROTATRANS.CDROTATRANS%TYPE;
   V_IDGRAVOBSCLIPED  CLIENTE.IDGRAVOBSCLIPED%TYPE;
   V_DSOBSERVCLIE     OBSERVCLIE.DSOBSERVCLIE%TYPE;
   V_COUNT            INTEGER;
   V_IDTPIJURTRAN     TRANSPORT.IDTPIJURTRAN%TYPE;
   V_NRINSJURTRAN     TRANSPORT.NRINSJURTRAN%TYPE;
   V_CDTIPTRANS       TIPOTRANSP.CDTIPTRANS%TYPE;
   V_IDPGFRETTRAN     PEDIDO.IDPGFRETTRAN%TYPE;
   V_NRINSJURTRANCLIE TRANSPORT.IDTPIJURTRAN%TYPE;
   V_IDTPIJURTRANCLIE TRANSPORT.NRINSJURTRAN%TYPE;
   V_CDTIPTRANSCLIE   TIPOTRANSP.CDTIPTRANS%TYPE;
   V_IDPGFRETTRANCLIE PEDIDO.IDPGFRETTRAN%TYPE;
   V_IDUTENDEENTRPED  PARAMPEDIDO.IDUTENDEENTRPED%TYPE;
   V_NRLOCALENTRCLIENTE PEDIDO.NRLOCALENTRCLIENTE%TYPE;
   V_IDTPENDE           ENDECLIE.IDTPENDECLIE%TYPE;
   V_DSENDEPED          ENDECLIE.DSENDECLIE%TYPE;
   V_NMBAIRROPED        ENDECLIE.NMBAIRCLIE%TYPE;
   V_CDMUNICIPIOPED     ENDECLIE.CDMUNICIPIO%TYPE;
   V_NMMUNIPED          MUNICIPIO.NMMUNICIPIO%TYPE;
   V_SGESTADOPED        ENDECLIE.SGESTADO%TYPE;
   V_NMESTADOPED        ESTADO.NMESTADO%TYPE;
   V_CDPAISPED          ENDECLIE.CDPAIS%TYPE;
   V_NMPAISPED          PAIS.NMPAIS%TYPE;
   V_NRCEPPED           ENDECLIE.NRCEPCLIE%TYPE;
   V_NRSEQEND           ENDECLIE.NRSEQEND%TYPE;
   V_BONIFIC            FLOAT;
BEGIN
   BEGIN
      SELECT NVL(IDEXIBIMPITPED, 'N'), NVL(IDUTRESELOTE, 'N'), NVL(IDUTENDEENTRPED, 'S')
        INTO V_IDEXIBIMPITPED, V_IDUTRESELOTE, V_IDUTENDEENTRPED
        FROM PARAMPEDIDO
       WHERE CDFILIAL = P_CDFILIAL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_IDEXIBIMPITPED := 'N'; V_IDUTRESELOTE := 'N'; V_IDUTENDEENTRPED := 'S';
      END;
   END;

   BEGIN
      SELECT NVL(IDCTRLVENEXP, 'N')
        INTO V_IDCTRLVENEXP
        FROM PAREXPEDICAO
       WHERE CDFILIAL = P_CDFILIAL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_IDCTRLVENEXP := 'N';
      END;
   END;

   BEGIN
      SELECT P.CDCLIENTE,      P.CDFILIALPED,  P.CDTIPOOPER,   P.CDVENDEDOR,
             P.IDSITPEDIDO,    P.CDMODPGTO,    P.IDTIPOPEDIDO, P.DTENTPED,
             P.CDROTATRANSPED, P.IDTPIJURTRAN, P.NRINSJURTRAN, P.CDTIPTRANS,
             P.IDPGFRETTRAN,   P.NRLOCALENTRCLIENTE
        INTO V_CDCLIENTE,      V_CDFILIALPED,  V_CDTIPOOPER,   V_CDVENDEDOR,
             V_IDSITPEDIDO,    V_CDMODPGTO,    V_IDTIPOPEDIDO, V_DTENTPED,
             V_CDROTATRANSPED, V_IDTPIJURTRAN, V_NRINSJURTRAN, V_CDTIPTRANS,
             V_IDPGFRETTRAN,   V_NRLOCALENTRCLIENTE
        FROM PEDIDO P
       WHERE P.CDFILIAL = P_CDFILIAL
         AND P.NRPEDIDO = P_NRPEDIDO;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_CDCLIENTE := NULL;   V_CDFILIALPED := NULL; V_CDTIPOOPER := NULL;   V_CDVENDEDOR := NULL;
         V_IDSITPEDIDO := NULL; V_CDMODPGTO := NULL;   V_IDTIPOPEDIDO := NULL; V_DTENTPED := NULL;
         V_CDROTATRANSPED := NULL; V_IDTPIJURTRAN := NULL; V_NRINSJURTRAN := NULL; V_CDTIPTRANS := NULL;
         V_IDPGFRETTRAN := NULL; V_NRLOCALENTRCLIENTE := NULL;
      END;
   END;

   BEGIN
      SELECT NVL(IDATUESTTPOP, 'S'), NVL(IDORIGDESTOP, 'C')
        INTO V_IDATUESTTPOP, V_IDORIGDESTOP
        FROM TIPOOPERAC
       WHERE IDENTRSAIDOP = 'S'
         AND CDTIPOOPER = V_CDTIPOOPER;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_IDATUESTTPOP := 'S'; V_IDORIGDESTOP := 'C';
      END;
   END;

   IF (V_IDATUESTTPOP = 'N') THEN
      DELETE FROM RESERPRODPED
            WHERE CDFILIAL  = P_CDFILIAL
              AND NRPEDIDO  = P_NRPEDIDO;
   ELSE
      IF ((V_IDUTRESELOTE = 'S') AND (V_IDTIPOPEDIDO = '1')) THEN
         BEGIN
            SELECT COUNT(1)
              INTO V_EXISTERESEVA
              FROM RESERPRODPED
             WHERE CDFILIAL = P_CDFILIAL
               AND NRPEDIDO = P_NRPEDIDO;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_EXISTERESEVA := 0;
            END;
         END;
         IF (V_EXISTERESEVA = 0) THEN
            INSERE_RESERPRODPED(P_CDFILIAL, P_NRPEDIDO, NULL,'S');
         END IF;
      END IF;
   END IF;

   IF (V_IDTIPOPEDIDO = '2') THEN
      DELETE FROM RESERPRODPED
            WHERE CDFILIAL  = P_CDFILIAL
              AND NRPEDIDO  = P_NRPEDIDO;
   END IF;

   IF (V_IDEXIBIMPITPED = 'S') THEN
      BEGIN
         SELECT CDEMPRESA, CDINSCESTA
           INTO V_CDEMPRESA, V_CDINSCESTA
           FROM FILIAL
          WHERE CDFILIAL = P_CDFILIAL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_CDEMPRESA := NULL; V_CDINSCESTA := NULL;
         END;
      END;
   END IF;
   V_COUNT := 0;
   BEGIN
      SELECT COUNT(1)
        INTO V_COUNT
        FROM ITEMPEDIDO
       WHERE CDFILIAL = P_CDFILIAL
         AND NRPEDIDO = P_NRPEDIDO;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_COUNT := 0;
      END;
   END;
   IF (V_COUNT = 0) THEN
      DELETE FROM PEDIDO
      WHERE CDFILIAL = P_CDFILIAL
         AND NRPEDIDO = P_NRPEDIDO;
   ELSE
      FOR C_ITENS IN (SELECT I.NRITEMPED, I.DTENTITPED, I.IDSITITEMPED, I.CDPRODUTO,I.VRPREUNITPED, 
                            (I.QTITEMPED*P.VRFATOCONV) AS QTRESERVA
                        FROM ITEMPEDIDO I JOIN PRODUTO P
                                            ON I.CDPRODUTO = P.CDPRODUTO
                       WHERE I.CDFILIAL = P_CDFILIAL
                         AND I.NRPEDIDO = P_NRPEDIDO)
      LOOP

         UPDATE ITEMPEDIDO
            SET CDEMISSAOITPED = BUSCA_CDEMISSAO_CLIENTE(P_CDFILIAL, V_CDCLIENTE, C_ITENS.CDPRODUTO),
                DSEMISSAOITPED = BUSCA_DSEMISSAO_CLIENTE(V_CDCLIENTE, C_ITENS.CDPRODUTO),
                IDITPEDRESER = '1'
          WHERE CDFILIAL = P_CDFILIAL
            AND NRPEDIDO = P_NRPEDIDO
            AND NRITEMPED = C_ITENS.NRITEMPED;

         IF (V_IDEXIBIMPITPED = 'S') THEN
            IMPOSTO_ITEMPEDIDO(V_CDEMPRESA,V_CDINSCESTA,V_CDCLIENTE,V_CDFILIALPED,P_CDFILIAL,P_NRPEDIDO,
                               C_ITENS.NRITEMPED, C_ITENS.DTENTITPED,V_CDTIPOOPER,V_IDORIGDESTOP,'N');
         END IF;

         IF ((V_IDCTRLVENEXP = 'S') OR
             (V_IDCTRLVENEXP = 'M') OR
             (V_IDCTRLVENEXP = 'R')) THEN

            DELETE FROM VENDITEMPED
             WHERE CDFILIAL  = P_CDFILIAL 
               AND NRPEDIDO  = P_NRPEDIDO 
               AND NRITEMPED = C_ITENS.NRITEMPED;


            GERA_COMISSAOVEND(P_CDFILIAL,P_NRPEDIDO,V_CDVENDEDOR,P_CDOPERADOR,
                              C_ITENS.NRITEMPED, V_IDSITITEMPEDPAR, C_ITENS.CDPRODUTO, C_ITENS.VRPREUNITPED,
                                                NULL, 'N');

            FOR C_VENDADICIONAL IN (SELECT P.CDVENDEDOR
                                      FROM VENDPEDAUX P
                                     WHERE (P.CDFILIAL = P_CDFILIAL)
                                       AND (P.NRPEDIDO = P_NRPEDIDO))
            LOOP
               GERA_COMISSAOVEND(P_CDFILIAL,P_NRPEDIDO,C_VENDADICIONAL.CDVENDEDOR,P_CDOPERADOR,
                                 C_ITENS.NRITEMPED, V_IDSITITEMPEDPAR, C_ITENS.CDPRODUTO, C_ITENS.VRPREUNITPED,
                                 NULL, 'N');
            END LOOP;
         END IF;

         IF ((V_IDATUESTTPOP = 'S') AND (V_IDUTRESELOTE = 'S') AND (V_IDTIPOPEDIDO = '1')) THEN
         
            V_QTRESPRODPED := 0;
            
            
            
            BEGIN  
               SELECT SUM(NVL(QTRESPRODPED,0))
                 INTO V_QTRESPRODPED
                 FROM RESERPRODPED
                WHERE CDFILIAL = P_CDFILIAL
                  AND NRPEDIDO = P_NRPEDIDO
                  AND NRITEMPED = C_ITENS.NRITEMPED
                  AND CDPRODUTO = C_ITENS.CDPRODUTO; 
            EXCEPTION
               WHEN NO_DATA_FOUND THEN BEGIN
                  V_QTRESPRODPED := 0;
               END;
            END;
            
            IF V_QTRESPRODPED IS NULL THEN
               V_QTRESPRODPED := 0;
            END IF;

            IF (C_ITENS.QTRESERVA <> V_QTRESPRODPED) THEN
            
               DELETE FROM RESERPRODPED
                WHERE CDFILIAL  = P_CDFILIAL
                  AND NRPEDIDO  = P_NRPEDIDO
                  AND NRITEMPED = C_ITENS.NRITEMPED;
            
               INSERE_RESERPRODPED(P_CDFILIAL, P_NRPEDIDO, C_ITENS.NRITEMPED,'N');
            END IF;

         END IF;
      END LOOP;
      IF (V_IDEXIBIMPITPED = 'S') THEN
         IMPOSTO_PEDIDO(P_CDFILIAL, P_NRPEDIDO);
      END IF;

      BEGIN
         SELECT IDGRAVOBSCLIPED,   BUSCA_OBSERVACAO_CLIENTE(CDCLIENTE)
           INTO V_IDGRAVOBSCLIPED, V_DSOBSERVCLIE
           FROM CLIENTE
          WHERE CDCLIENTE = V_CDCLIENTE;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_IDGRAVOBSCLIPED := 'S';   V_DSOBSERVCLIE := NULL;
         END;
      END;

      V_CDROTATRANS := NULL;

      IF (V_CDROTATRANSPED IS NULL) THEN
         IF (V_CDFILIALPED IS NOT NULL) THEN
            BEGIN
               SELECT FL.CDROTATRANS
                 INTO V_CDROTATRANS
                 FROM FILIALROTA FL
                WHERE FL.CDFILIAL = P_CDFILIAL
                  AND FL.CDFILIROTA = V_CDFILIALPED
                  AND FL.IDROTAPRINCFILI= 'S';
            EXCEPTION
               WHEN NO_DATA_FOUND THEN BEGIN
                  V_CDROTATRANS := NULL;
               END;
            END;
         ELSE
            IF (V_CDCLIENTE IS NOT NULL) THEN
               BEGIN
                  SELECT CL.CDROTATRANS
                    INTO V_CDROTATRANS
                    FROM CLIENROTA CL
                   WHERE CL.CDFILIAL = P_CDFILIAL
                     AND CL.CDCLIENTE = V_CDCLIENTE
                     AND CL.IDROTAPRINCLI = 'S';
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN BEGIN
                     V_CDROTATRANS := NULL;
                  END;
               END;
            END IF;
         END IF;
      END IF;

      UPDATE PEDIDO
         SET VRPBRUVOLPED = PESOPRODUTO_PEDIDO(P_CDFILIAL, P_NRPEDIDO),
             VRPLIQVOLPED = PESOLIQUIDO_PEDIDO(P_CDFILIAL, P_NRPEDIDO),
             CDROTATRANSPED = NVL(V_CDROTATRANS, CDROTATRANSPED)
       WHERE CDFILIAL = P_CDFILIAL
         AND NRPEDIDO = P_NRPEDIDO;

      IF (V_IDGRAVOBSCLIPED = 'S') AND (V_DSOBSERVCLIE IS NOT NULL) THEN
         V_COUNT := 0;
         BEGIN
            SELECT COUNT(1)
              INTO V_COUNT
              FROM OBSPEDIDO
             WHERE CDFILIAL = P_CDFILIAL
               AND NRPEDIDO = P_NRPEDIDO;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_COUNT := 0;
            END;
         END;
         IF (V_COUNT = 0) THEN
            INSERT INTO OBSPEDIDO
            (CDFILIAL, NRPEDIDO, TXOBSPED)
            VALUES
            (P_CDFILIAL, P_NRPEDIDO, V_DSOBSERVCLIE);
         END IF;
      END IF;

      IF (V_IDUTENDEENTRPED = 'S') THEN

         V_COUNT := 0;

         BEGIN
            SELECT COUNT(1)
              INTO V_COUNT
              FROM DADOSENDPED
             WHERE CDFILIAL = P_CDFILIAL
               AND NRPEDIDO = P_NRPEDIDO;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN BEGIN
               V_COUNT := 0;
            END;
         END;

         IF (V_COUNT = 0) THEN

            IF (V_CDFILIALPED IS NOT NULL) THEN

               BEGIN
                  SELECT EN.IDTPENDEFILI, EN.DSENDEFILI, EN.NMBAIRFILI,
                         EN.CDMUNICIPIO, MU.NMMUNICIPIO, EN.SGESTADO, ES.NMESTADO, EN.CDPAIS, PA.NMPAIS,
                         EN.NRCEPFILI
                    INTO V_IDTPENDE, V_DSENDEPED, V_NMBAIRROPED,
                         V_CDMUNICIPIOPED, V_NMMUNIPED, V_SGESTADOPED, V_NMESTADOPED, V_CDPAISPED, V_NMPAISPED,
                         V_NRCEPPED
                    FROM ENDEFILI EN JOIN PAIS PA
                                       ON (EN.CDPAIS = PA.CDPAIS)
                                     JOIN ESTADO ES
                                       ON (EN.CDPAIS = ES.CDPAIS)
                                      AND (EN.SGESTADO = ES.SGESTADO)
                                     JOIN MUNICIPIO MU
                                       ON (EN.CDPAIS = MU.CDPAIS)
                                      AND (EN.SGESTADO = MU.SGESTADO)
                                      AND (EN.CDMUNICIPIO = MU.CDMUNICIPIO )
                   WHERE (EN.CDFILIAL = V_CDFILIALPED)
                     AND (EN.IDTPENDEFILI = 'T');
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN BEGIN
                     V_IDTPENDE := NULL; V_DSENDEPED := NULL; V_NMBAIRROPED := NULL;
                     V_CDMUNICIPIOPED := NULL; V_NMMUNIPED := NULL; V_SGESTADOPED := NULL; V_NMESTADOPED := NULL; V_CDPAISPED := NULL; V_NMPAISPED := NULL;
                     V_NRCEPPED := NULL;
                  END;
               END;

               IF (V_DSENDEPED IS NOT NULL) THEN
                  INSERT INTO DADOSENDPED
                  (CDFILIAL, NRPEDIDO, IDTPENDECLIE, DSENDEPED, NMBAIRROPED,
                   CDMUNICIPIOPED, NMMUNIPED, SGESTADOPED, NMESTADOPED, CDPAISPED, NMPAISPED,
                   NRCEPPED, CDCLIENTE, NRSEQEND)
                  VALUES
                  (P_CDFILIAL, P_NRPEDIDO, V_IDTPENDE, V_DSENDEPED, V_NMBAIRROPED,
                   V_CDMUNICIPIOPED, V_NMMUNIPED, V_SGESTADOPED, V_NMESTADOPED, V_CDPAISPED, V_NMPAISPED,
                   V_NRCEPPED, NULL,  NULL);
               ELSE
                  BEGIN
                     SELECT EN.IDTPENDEFILI, EN.DSENDEFILI, EN.NMBAIRFILI,
                            EN.CDMUNICIPIO, MU.NMMUNICIPIO, EN.SGESTADO, ES.NMESTADO, EN.CDPAIS, PA.NMPAIS,
                            EN.NRCEPFILI
                       INTO V_IDTPENDE, V_DSENDEPED, V_NMBAIRROPED,
                            V_CDMUNICIPIOPED, V_NMMUNIPED, V_SGESTADOPED, V_NMESTADOPED, V_CDPAISPED, V_NMPAISPED,
                            V_NRCEPPED
                       FROM ENDEFILI EN JOIN PAIS PA
                                          ON (EN.CDPAIS = PA.CDPAIS)
                                        JOIN ESTADO ES
                                          ON (EN.CDPAIS = ES.CDPAIS)
                                         AND (EN.SGESTADO = ES.SGESTADO)
                                        JOIN MUNICIPIO MU
                                          ON (EN.CDPAIS = MU.CDPAIS)
                                         AND (EN.SGESTADO = MU.SGESTADO)
                                         AND (EN.CDMUNICIPIO = MU.CDMUNICIPIO )
                      WHERE (EN.CDFILIAL = V_CDFILIALPED)
                        AND (EN.IDTPENDEFILI = 'P');
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_IDTPENDE := NULL; V_DSENDEPED := NULL; V_NMBAIRROPED := NULL;
                        V_CDMUNICIPIOPED := NULL; V_NMMUNIPED := NULL; V_SGESTADOPED := NULL; V_NMESTADOPED := NULL; V_CDPAISPED := NULL; V_NMPAISPED := NULL;
                        V_NRCEPPED := NULL;
                     END;
                  END;

                  IF (V_DSENDEPED IS NOT NULL) THEN
                     INSERT INTO DADOSENDPED
                     (CDFILIAL, NRPEDIDO, IDTPENDECLIE, DSENDEPED, NMBAIRROPED,
                      CDMUNICIPIOPED, NMMUNIPED, SGESTADOPED, NMESTADOPED, CDPAISPED, NMPAISPED,
                      NRCEPPED, CDCLIENTE, NRSEQEND)
                     VALUES
                     (P_CDFILIAL, P_NRPEDIDO, V_IDTPENDE, V_DSENDEPED, V_NMBAIRROPED,
                      V_CDMUNICIPIOPED, V_NMMUNIPED, V_SGESTADOPED, V_NMESTADOPED, V_CDPAISPED, V_NMPAISPED,
                      V_NRCEPPED, NULL,  NULL);
                  END IF;

               END IF;
            ELSE
               BEGIN
                  SELECT EN.NRSEQEND, EN.IDTPENDECLIE, EN.DSENDECLIE, EN.NMBAIRCLIE,
                         EN.CDMUNICIPIO, MU.NMMUNICIPIO, EN.SGESTADO, ES.NMESTADO, EN.CDPAIS, PA.NMPAIS,
                         EN.NRCEPCLIE
                    INTO V_NRSEQEND, V_IDTPENDE, V_DSENDEPED, V_NMBAIRROPED,
                         V_CDMUNICIPIOPED, V_NMMUNIPED, V_SGESTADOPED, V_NMESTADOPED, V_CDPAISPED, V_NMPAISPED,
                         V_NRCEPPED
                    FROM ENDECLIE EN JOIN PAIS PA
                                       ON (EN.CDPAIS = PA.CDPAIS)
                                     JOIN ESTADO ES
                                       ON (EN.CDPAIS = ES.CDPAIS)
                                      AND (EN.SGESTADO = ES.SGESTADO)
                                     JOIN MUNICIPIO MU
                                       ON (EN.CDPAIS = MU.CDPAIS)
                                      AND (EN.SGESTADO = MU.SGESTADO)
                                      AND (EN.CDMUNICIPIO = MU.CDMUNICIPIO)
                   WHERE (EN.CDCLIENTE = V_CDCLIENTE)
                     AND (EN.IDTPENDECLIE = 'T');
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN BEGIN
                     V_NRSEQEND := NULL; V_IDTPENDE := NULL; V_DSENDEPED := NULL; V_NMBAIRROPED := NULL;
                     V_CDMUNICIPIOPED := NULL; V_NMMUNIPED := NULL; V_SGESTADOPED := NULL; V_NMESTADOPED := NULL; V_CDPAISPED := NULL; V_NMPAISPED := NULL;
                     V_NRCEPPED := NULL;
                  END;
               END;

               IF (V_DSENDEPED IS NOT NULL) THEN
                  INSERT INTO DADOSENDPED
                  (CDFILIAL, NRPEDIDO, IDTPENDECLIE, DSENDEPED, NMBAIRROPED,
                   CDMUNICIPIOPED, NMMUNIPED, SGESTADOPED, NMESTADOPED, CDPAISPED, NMPAISPED,
                   NRCEPPED, CDCLIENTE, NRSEQEND)
                  VALUES
                  (P_CDFILIAL, P_NRPEDIDO, V_IDTPENDE, V_DSENDEPED, V_NMBAIRROPED,
                   V_CDMUNICIPIOPED, V_NMMUNIPED, V_SGESTADOPED, V_NMESTADOPED, V_CDPAISPED, V_NMPAISPED,
                   V_NRCEPPED, V_CDCLIENTE,  V_NRSEQEND);
               ELSE
                  BEGIN
                     SELECT EN.NRSEQEND, EN.IDTPENDECLIE, EN.DSENDECLIE, EN.NMBAIRCLIE,
                            EN.CDMUNICIPIO, MU.NMMUNICIPIO, EN.SGESTADO, ES.NMESTADO, EN.CDPAIS, PA.NMPAIS,
                            EN.NRCEPCLIE
                       INTO V_NRSEQEND, V_IDTPENDE, V_DSENDEPED, V_NMBAIRROPED,
                            V_CDMUNICIPIOPED, V_NMMUNIPED, V_SGESTADOPED, V_NMESTADOPED, V_CDPAISPED, V_NMPAISPED,
                            V_NRCEPPED
                       FROM ENDECLIE EN JOIN PAIS PA
                                          ON (EN.CDPAIS = PA.CDPAIS)
                                        JOIN ESTADO ES
                                          ON (EN.CDPAIS = ES.CDPAIS)
                                         AND (EN.SGESTADO = ES.SGESTADO)
                                        JOIN MUNICIPIO MU
                                          ON (EN.CDPAIS = MU.CDPAIS)
                                         AND (EN.SGESTADO = MU.SGESTADO)
                                         AND (EN.CDMUNICIPIO = MU.CDMUNICIPIO)
                      WHERE (EN.CDCLIENTE = V_CDCLIENTE)
                        AND (EN.IDTPENDECLIE = 'P');
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_NRSEQEND := NULL; V_IDTPENDE := NULL; V_DSENDEPED := NULL; V_NMBAIRROPED := NULL;
                        V_CDMUNICIPIOPED := NULL; V_NMMUNIPED := NULL; V_SGESTADOPED := NULL; V_NMESTADOPED := NULL; V_CDPAISPED := NULL; V_NMPAISPED := NULL;
                        V_NRCEPPED := NULL;
                     END;
                  END;

                  IF (V_DSENDEPED IS NOT NULL) THEN
                     INSERT INTO DADOSENDPED
                     (CDFILIAL, NRPEDIDO, IDTPENDECLIE, DSENDEPED, NMBAIRROPED,
                      CDMUNICIPIOPED, NMMUNIPED, SGESTADOPED, NMESTADOPED, CDPAISPED, NMPAISPED,
                      NRCEPPED, CDCLIENTE, NRSEQEND)
                     VALUES
                     (P_CDFILIAL, P_NRPEDIDO, V_IDTPENDE, V_DSENDEPED, V_NMBAIRROPED,
                      V_CDMUNICIPIOPED, V_NMMUNIPED, V_SGESTADOPED, V_NMESTADOPED, V_CDPAISPED, V_NMPAISPED,
                      V_NRCEPPED, V_CDCLIENTE,  V_NRSEQEND);
                  END IF;
               END IF;
            END IF;
         END IF;
      END IF;

      V_VRPEDIDO := VALOR_PEDIDO(P_CDFILIAL, P_NRPEDIDO);

      V_BONIFIC := VALOR_BONIFICACAO_PEDIDO(P_CDFILIAL, P_NRPEDIDO);

      IF V_BONIFIC > 0 THEN
         V_VRRECEBIMENTO := V_VRPEDIDO - V_BONIFIC;
      ELSE
         V_VRRECEBIMENTO := V_VRPEDIDO;
      END IF;

      IF (V_VRRECEBIMENTO > 0) THEN 

         SELECT COUNT(1)
           INTO V_EXIST_PRAZ
           FROM PRAZOMODPGTO
          WHERE CDMODPGTO = V_CDMODPGTO;

         IF V_EXIST_PRAZ > 0 THEN

            DELETE FROM PRAZOPGTOPED
                  WHERE CDFILIAL = P_CDFILIAL
                    AND NRPEDIDO = P_NRPEDIDO;       

            FOR C_PRAZO IN (SELECT NRDIASMODPGTO, VRPERCMODPGTO, CDMODPGTO
                              FROM PRAZOMODPGTO
                             WHERE CDMODPGTO = V_CDMODPGTO)
            LOOP  

               INSERT INTO PRAZOPGTOPED
               (CDFILIAL, NRPEDIDO, NRDIASPGTOPED, VRPERCPGTOPED, VRPARCELA)
               VALUES
               (P_CDFILIAL, P_NRPEDIDO, C_PRAZO.NRDIASMODPGTO, C_PRAZO.VRPERCMODPGTO, ROUND((V_VRRECEBIMENTO*C_PRAZO.VRPERCMODPGTO)/100,2));
            END LOOP;

         END IF;   

      END IF;

      UPDATE PRAZOPGTOPED
         SET VRPARCELA = ROUND(((V_VRPEDIDO * VRPERCPGTOPED)/100), 2)
       WHERE CDFILIAL = P_CDFILIAL
         AND NRPEDIDO = P_NRPEDIDO;

      SELECT SUM(P.VRPARCELA)
        INTO V_VRPARCELATOT
        FROM PRAZOPGTOPED P
       WHERE P.CDFILIAL = P_CDFILIAL
         AND P.NRPEDIDO = P_NRPEDIDO;

      IF (ABS(V_VRRECEBIMENTO - V_VRPARCELATOT) > 0) THEN
         SELECT MAX(P.NRDIASPGTOPED)
           INTO V_NRDIASPGTOPED
           FROM PRAZOPGTOPED P
          WHERE (P.CDFILIAL = P_CDFILIAL)
            AND (P.NRPEDIDO = P_NRPEDIDO)
            AND (P.VRPARCELA = (SELECT VRPARCELA
                                  FROM (SELECT A.VRPARCELA
                                          FROM PRAZOPGTOPED A
                                         WHERE (A.CDFILIAL = P.CDFILIAL)
                                           AND (A.NRPEDIDO = P.NRPEDIDO)
                                        ORDER BY 1 DESC)
                                WHERE ROWNUM = 1));

         UPDATE PRAZOPGTOPED
            SET VRPARCELA = (VRPARCELA + (V_VRPEDIDO - V_VRPARCELATOT))
          WHERE CDFILIAL = P_CDFILIAL
            AND NRPEDIDO = P_NRPEDIDO
            AND NRDIASPGTOPED = V_NRDIASPGTOPED;

      END IF;
   END IF;
END;
/