CREATE OR REPLACE PROCEDURE NOVA_PARTIDA(
    P_CDEMPRESA   IN EMPRESA.CDEMPRESA%TYPE,
    P_CDLOTE      IN CAPALOTE.CDLOTE%TYPE,
    P_DTMOVI      IN DATE,
    P_NRLANCLOTE  IN LANCLOTE.NRLANCLOTE%TYPE,
    P_CDCONTCTBL  IN CONTCTBL.CDCONTCTBL%TYPE,
    P_CDHISTPADR  IN HISTPADR.CDHISTPADR%TYPE,
    P_DSHISTORICO IN HISTPADR.DSHISTPADR%TYPE,
    P_VRLANCCTB   IN CAPALOTE.VRTOTAINFO%TYPE,
    P_IDLANCCOME  IN CONTCTBL.IDTIPOCOME%TYPE,
    P_CDCONTCOME  IN SALDCOME.CDSALDCOME%TYPE,
    P_IDMOVI      IN VARCHAR2,
    P_DOCUMENTO   IN VARCHAR2,
    P_IDACUMULA   IN LANCCONT.IDLANCCOME%TYPE)
IS
    V_DTINIINTCONTDFE DATE;
    V_DTFININTCONTDFE DATE;
    V_DTINIINTCONTDFS DATE;
    V_DTFININTCONTDFS DATE;
    V_DTINICCTBL      DATE;
    V_DTFINACCTBL     DATE;
    V_DTINICCONT      DATE;
    V_DTFINACONT      DATE;
    V_CDCONTREDU      VARCHAR2(20);
    V_CODCOMERC       VARCHAR2(21);
    V_IDTIPOCOME      VARCHAR2(01);
    V_SEQUENCIAL      VARCHAR2(06);
BEGIN
   BEGIN
      SELECT IDTIPOCOME, CDCONTREDU
        INTO V_IDTIPOCOME, V_CDCONTREDU
        FROM CONTCTBL
       WHERE  CDCONTCTBL = P_CDCONTCTBL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_IDTIPOCOME := '';
   END;

   IF V_IDTIPOCOME = P_IDLANCCOME THEN
      V_CODCOMERC := P_CDCONTCOME;
   ELSE
      V_CODCOMERC  := NULL;
      V_IDTIPOCOME := NULL;
   END IF;

      IF P_IDACUMULA = 'S' THEN

         SELECT NVL(MAX(NRSEQLANLOTE),0)
           INTO V_SEQUENCIAL
           FROM LANCLOTE
          WHERE CDEMPRESA = P_CDEMPRESA
            AND CDLOTE = P_CDLOTE
            AND NRLANCLOTE = P_NRLANCLOTE
            AND CDCONTCTBL = P_CDCONTCTBL;

            IF V_SEQUENCIAL <> 0 THEN
                UPDATE LANCLOTE
                   SET VRLANCLOTE = VRLANCLOTE + P_VRLANCCTB
                 WHERE CDEMPRESA = P_CDEMPRESA
                   AND CDLOTE = P_CDLOTE
                   AND NRLANCLOTE = P_NRLANCLOTE
                   AND NRSEQLANLOTE = V_SEQUENCIAL
                   AND CDCONTCTBL = P_CDCONTCTBL;
            ELSE
                SELECT NVL(MAX(NRSEQLANLOTE),0)
                  INTO V_SEQUENCIAL
                  FROM LANCLOTE
                 WHERE CDEMPRESA = P_CDEMPRESA
                   AND CDLOTE = P_CDLOTE
                   AND NRLANCLOTE = P_NRLANCLOTE;

                V_SEQUENCIAL := LPAD(TO_CHAR(TO_NUMBER(V_SEQUENCIAL) + 1), 5, '0');

                INSERT INTO LANCLOTE
                  (CDEMPRESA, CDLOTE, NRLANCLOTE, NRSEQLANLOTE,CDCONTCTBL, CDHISTPADR, DSHISTLOTE,
                   DTLANCLOTE, NRDOCULOTE, VRLANCLOTE, IDLLOTCOME, CDLLOTCOME, CDREDUZIDO)
                VALUES
                  (P_CDEMPRESA,P_CDLOTE,P_NRLANCLOTE,V_SEQUENCIAL,P_CDCONTCTBL,P_CDHISTPADR, P_DSHISTORICO,
                   P_DTMOVI, P_DOCUMENTO, P_VRLANCCTB, V_IDTIPOCOME,V_CODCOMERC,V_CDCONTREDU);
            END IF;
      ELSE
         BEGIN
           SELECT NVL(MAX(NRSEQLANLOTE),0)
             INTO V_SEQUENCIAL
             FROM LANCLOTE
           WHERE CDEMPRESA = P_CDEMPRESA
             AND CDLOTE = P_CDLOTE
             AND NRLANCLOTE = P_NRLANCLOTE;
         END;

         V_SEQUENCIAL := LPAD(TO_CHAR(TO_NUMBER(V_SEQUENCIAL) + 1), 5, '0');

         INSERT INTO LANCLOTE
           (CDEMPRESA, CDLOTE, NRLANCLOTE, NRSEQLANLOTE,CDCONTCTBL, CDHISTPADR, DSHISTLOTE,
            DTLANCLOTE, NRDOCULOTE, VRLANCLOTE, IDLLOTCOME, CDLLOTCOME, CDREDUZIDO)
         VALUES
           (P_CDEMPRESA,P_CDLOTE,P_NRLANCLOTE,V_SEQUENCIAL,P_CDCONTCTBL,P_CDHISTPADR, P_DSHISTORICO,
            P_DTMOVI, P_DOCUMENTO, P_VRLANCCTB, V_IDTIPOCOME,V_CODCOMERC,V_CDCONTREDU);

      END IF;
END;
/
