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

CREATE PROCEDURE CALCULA_LUCRO_PRESUMIDO
   @P_CDIMPOSTO      VARCHAR(02),
   @P_NRSEQUMAPAPU   VARCHAR(10)
   
AS
   DECLARE @V_NRSEQITMAPR    VARCHAR(04);
   DECLARE @V_VRIMPOSTONF    NUMERIC(16,2);
   DECLARE @V_SEQUENCIAL     NUMERIC(16,2);
   DECLARE @V_VRSALDODEBI    NUMERIC(16,2);
   DECLARE @V_VROUTRCRED     NUMERIC(16,2);
   DECLARE @V_VRTOTACRED     NUMERIC(16,2);

BEGIN
  /* totaliza o valor do imposto Lucro presumido, que foi apurado com o ICMS  */
  SELECT @V_VRIMPOSTONF = SUM(VRIMPOSTONF)
    FROM IMPOSTONF I, (SELECT DISTINCT CDFILIAL, NRLANCTONF
                         FROM IMPOSTONF
                        WHERE NRSEQUMAPAPU = @P_NRSEQUMAPAPU
                          AND CDIMPOSTO = @P_CDIMPOSTO) IM
   WHERE I.CDFILIAL = IM.CDFILIAL
     AND I.NRLANCTONF = IM.NRLANCTONF
     AND CDIMPOSTO = (SELECT MAX(CDLUCROPRESUMI)
                        FROM IMPOSTO
                       WHERE CDIMPOSTO = @P_CDIMPOSTO);

   IF @V_VRIMPOSTONF > 0 BEGIN
      SELECT @V_NRSEQITMAPR = MAX(NRSEQITMAPR)
        FROM ITMAPARECUP
       WHERE NRSEQUMAPAPU = @P_NRSEQUMAPAPU
         AND IDTIPOITMAPR = 'OC'
         AND DSITEMMAPR LIKE 'CREDITO PRESUMIDO OPERACOES INTERNAS';

      IF @V_NRSEQITMAPR IS NULL BEGIN
         SELECT @V_SEQUENCIAL = MAX(NRSEQITMAPR)
           FROM ITMAPARECUP
          WHERE NRSEQUMAPAPU = @P_NRSEQUMAPAPU
            AND IDTIPOITMAPR = 'OC';

         INSERT INTO ITMAPARECUP
           (NRSEQUMAPAPU, IDTIPOITMAPR, NRSEQITMAPR, DSITEMMAPR, VRITEMMAPR)
         VALUES
           (@P_NRSEQUMAPAPU, 'OC', RIGHT(REPLICATE('0', 3) + CONVERT(VARCHAR, ISNULL(@V_SEQUENCIAL,0)),3), 'CREDITO PRESUMIDO OPERACOES INTERNAS',ROUND(@V_VRIMPOSTONF,2));

           UPDATE MAPARECUP
              SET VROUTRCRED = ISNULL(VROUTRCRED,0) + ROUND(@V_VRIMPOSTONF,2),
                  VRTOTACRED = ISNULL(VRTOTACRED,0) + ROUND(@V_VRIMPOSTONF,2),
                  VRSALDODEBI = ABS(VRTOTADEBI - VRTOTACRED)
            WHERE NRSEQUMAPAPU = @P_NRSEQUMAPAPU;

           SELECT @V_VROUTRCRED = SUM(VROUTRCRED),
                  @V_VRTOTACRED = SUM(VRTOTACRED)
             FROM MAPARECUP
            WHERE NRSEQUMAPAPU = @P_NRSEQUMAPAPU;

           SELECT @V_VRSALDODEBI = SUM(VRSALDODEBI)
             FROM MAPARECUP
            WHERE NRSEQUMAPAPU = @P_NRSEQUMAPAPU;

           /* atualiza o valor do imposto a recolher */
           UPDATE MAPAAPURAC
              SET VRTOTAIMPOS = ROUND(@V_VRSALDODEBI,2) -ROUND(@V_VRTOTACRED,2)
            WHERE NRSEQUMAPAPU = @P_NRSEQUMAPAPU;

           UPDATE MAPARECUP
              SET VRSALDODEBI = ROUND(@V_VRSALDODEBI,2) -ROUND(@V_VRTOTACRED,2)
            WHERE NRSEQUMAPAPU = @P_NRSEQUMAPAPU;
      END
      ELSE BEGIN
         UPDATE ITMAPARECUP
            SET VRITEMMAPR = ROUND(@V_VRIMPOSTONF,2)
          WHERE NRSEQUMAPAPU = @P_NRSEQUMAPAPU
            AND IDTIPOITMAPR = 'OC'
            AND DSITEMMAPR LIKE 'CREDITO PRESUMIDO OPERACOES INTERNAS'
            AND NRSEQITMAPR = @V_NRSEQITMAPR;
          COMMIT;
      END;
   END;
END;

GO


