IF EXISTS (SELECT * FROM SYSOBJECTS O, SYSUSERS U
           WHERE O.NAME = 'NOVA_PARTIDA' AND
                 TYPE = 'P' 	         AND
	              U.UID = O.UID      AND
	              U.NAME = USER)
DROP PROCEDURE NOVA_PARTIDA
GO

CREATE PROCEDURE NOVA_PARTIDA(
    @P_CDEMPRESA    VARCHAR(2),
    @P_CDLOTE       VARCHAR(20),
    @P_DTMOVI       DATETIME,
    @P_NRLANCLOTE   VARCHAR(6),
    @P_CDCONTCTBL   VARCHAR(20),
    @P_CDHISTPADR   VARCHAR(4),		
    @P_DSHISTORICO  VARCHAR(60),
    @P_VRLANCCTB    DOUBLE PRECISION,
    @P_IDLANCCOME   VARCHAR(1),
    @P_CDCONTCOME   VARCHAR(21),
    @P_IDMOVI       VARCHAR,
    @P_DOCUMENTO    VARCHAR(20),
    @P_IDACUMULA    VARCHAR(1))
AS 
BEGIN
    DECLARE @V_DTINIINTCONTDFE DATETIME; 
    DECLARE @V_DTFININTCONTDFE DATETIME; 
    DECLARE @V_DTINIINTCONTDFS DATETIME; 
    DECLARE @V_DTFININTCONTDFS DATETIME;
    DECLARE @V_DTINICCTBL      DATETIME;
    DECLARE @V_DTFINACCTBL     DATETIME;
    DECLARE @V_DTINICCONT      DATETIME;
    DECLARE @V_DTFINACONT      DATETIME;
    DECLARE @V_DATAOK          CHAR(1);
    DECLARE @V_CDCONTREDU      VARCHAR(20);
    DECLARE @V_CODCOMERC       VARCHAR(20);
    DECLARE @V_IDTIPOCOME      VARCHAR(01);
    DECLARE @V_SEQUENCIAL      VARCHAR(6);

    SELECT
       @V_IDTIPOCOME = IDTIPOCOME,
       @V_CDCONTREDU = CDCONTREDU
    FROM CONTCTBL
    WHERE  CDCONTCTBL = @P_CDCONTCTBL;

    IF @V_IDTIPOCOME = @P_IDLANCCOME BEGIN
       SET @V_CODCOMERC = @P_CDCONTCOME;
    END
    ELSE BEGIN
       SET @V_CODCOMERC  = NULL;
       SET @V_IDTIPOCOME = NULL;
    END;

    IF @P_IDACUMULA = 'S' BEGIN
       SELECT @V_SEQUENCIAL = ISNULL(MAX(NRSEQLANLOTE),0)
         FROM LANCLOTE
        WHERE CDEMPRESA  = @P_CDEMPRESA
          AND CDLOTE     = @P_CDLOTE
          AND NRLANCLOTE = @P_NRLANCLOTE
          AND CDCONTCTBL = @P_CDCONTCTBL;

       IF @V_SEQUENCIAL <> 0 BEGIN
          UPDATE LANCLOTE
             SET VRLANCLOTE = VRLANCLOTE + @P_VRLANCCTB
           WHERE @P_CDEMPRESA  = CDEMPRESA
             AND @P_CDLOTE     = CDLOTE
             AND @P_NRLANCLOTE = NRLANCLOTE
             AND @V_SEQUENCIAL = NRSEQLANLOTE
             AND @P_CDCONTCTBL = CDCONTCTBL
             AND @V_IDTIPOCOME = IDLLOTCOME
             AND @V_CODCOMERC  = CDLLOTCOME;
       END
       ELSE BEGIN

         SELECT @V_SEQUENCIAL = ISNULL(MAX(NRSEQLANLOTE), '0')
           FROM LANCLOTE
          WHERE CDEMPRESA  = @P_CDEMPRESA
            AND CDLOTE     = @P_CDLOTE
            AND NRLANCLOTE = @P_NRLANCLOTE;

         SET @V_SEQUENCIAL = RIGHT(REPLICATE('0', 5) + CONVERT(VARCHAR(2),(CONVERT(INT, @V_SEQUENCIAL) + 1)), 5);

         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;
    END
    ELSE BEGIN
       SELECT @V_SEQUENCIAL = ISNULL(MAX(NRSEQLANLOTE), '0')
         FROM LANCLOTE
        WHERE CDEMPRESA  = @P_CDEMPRESA
          AND CDLOTE     = @P_CDLOTE
          AND NRLANCLOTE = @P_NRLANCLOTE;

       SET @V_SEQUENCIAL = RIGHT(REPLICATE('0', 5) + CONVERT(VARCHAR(2),(CONVERT(INT, @V_SEQUENCIAL) + 1)), 5);

       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;
END
GO

