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

   DROP PROCEDURE INICIALIZA_SALDO_CONSUMIDOR
   GO

CREATE PROCEDURE INICIALIZA_SALDO_CONSUMIDOR
   @P_CDFILIAL  VARCHAR(4),
   @P_CDCLIENTE VARCHAR(4000),
   @P_DATA      DATETIME,
   @P_CDOPERADOR  VARCHAR(12)

AS

   DECLARE @V_CDCLIENTE    VARCHAR(21);
   DECLARE @V_CDCONSUMIDOR VARCHAR(21);
   DECLARE @V_VRMESUBCONS  NUMERIC(15,2);
   DECLARE @V_VRRESTOSALDO NUMERIC(15,2);
   DECLARE @V_VRSALDOPESS  NUMERIC(15,2);
   DECLARE @V_SEQUENCIAL   VARCHAR(20);
   DECLARE @V_PARAMETROS   VARCHAR(50);

BEGIN
   IF (@P_CDCLIENTE = 'T') BEGIN
      DECLARE CUR_CONS CURSOR FOR
         SELECT C.CDCLIENTE, C.CDCONSUMIDOR, ISNULL(C.VRMESUBCONS,0)
         FROM CONSUMIDOR C, CLIENFILIAL CF
         WHERE CF.CDFILIAL = @P_CDFILIAL
           AND C.CDCLIENTE = CF.CDCLIENTE
           AND C.IDSITCONSUMI = '1'
   END
   ELSE BEGIN
      DECLARE CUR_CONS CURSOR FOR
         SELECT C.CDCLIENTE, C.CDCONSUMIDOR, ISNULL(C.VRMESUBCONS,0)
         FROM CONSUMIDOR C, CLIENFILIAL CF
         WHERE CF.CDFILIAL  = @P_CDFILIAL
           AND (CHARINDEX('_'+CF.CDCLIENTE+'_', @P_CDCLIENTE) <> 0)
           AND C.CDCLIENTE  = CF.CDCLIENTE
           AND C.IDSITCONSUMI = '1'
   END;

   OPEN CUR_CONS;
   FETCH NEXT FROM CUR_CONS INTO @V_CDCLIENTE, @V_CDCONSUMIDOR, @V_VRMESUBCONS;
   WHILE (@@FETCH_STATUS = 0) BEGIN

      SELECT @V_VRRESTOSALDO = ISNULL(MAX(VRSALDOCONS),0),
             @V_VRSALDOPESS = ISNULL(MAX(VRSALDPECONS),0)
      FROM MOVICLIE
      WHERE CDCLIENTE    = @V_CDCLIENTE
        AND CDCONSUMIDOR = @V_CDCONSUMIDOR
        AND DTMOVCLI     = (SELECT MAX(DTMOVCLI)
                            FROM MOVICLIE
                            WHERE CDCLIENTE = @V_CDCLIENTE
                              AND CDCONSUMIDOR = @V_CDCONSUMIDOR);

      IF (@V_VRRESTOSALDO IS NOT NULL) BEGIN
         SET @V_PARAMETROS = 'MOVCLIE' + @V_CDCLIENTE;
         EXECUTE NOVO_CODIGO @V_PARAMETROS, @P_SEQUENCIAL = @V_SEQUENCIAL OUTPUT, @P_QTDE = 1;
         SET @V_SEQUENCIAL = SUBSTRING(@V_SEQUENCIAL,11,10);

         INSERT INTO MOVICLIE
         (CDCLIENTE, NRSEQMOVCLI, CDCONSUMIDOR, VRMOVCLI,
          DTMOVCLI, DSHISTMOV, IDTIPMOCVLI, CDFILIAL, CDCAIXA, NRSEQVENDA, DTABERCAIX,
          NRSEQUMOVI, CDTIPORECE, VRSALDOCONS, VRMOVPESCONS, VRSALDPECONS,
          DTATUMOVCLIE, CDOPERADOR, DSATUMOVCLIE)
         VALUES
         (@V_CDCLIENTE, @V_SEQUENCIAL, @V_CDCONSUMIDOR, @V_VRRESTOSALDO,
          @P_DATA, 'Ajuste de saldo', 'A', @P_CDFILIAL, NULL, NULL, NULL,
          NULL, NULL, 0, 0, @V_VRSALDOPESS,
          @P_DATA, @P_CDOPERADOR,'Ajuste de Saldo Mensal do Consumidor');
      END;

      SET @V_PARAMETROS = 'MOVCLIE' + @V_CDCLIENTE;
      EXECUTE NOVO_CODIGO @V_PARAMETROS, @P_SEQUENCIAL = @V_SEQUENCIAL OUTPUT, @P_QTDE = 1;

      SET @V_SEQUENCIAL = SUBSTRING(@V_SEQUENCIAL,11,10);

      INSERT INTO MOVICLIE
      (CDCLIENTE, NRSEQMOVCLI, CDCONSUMIDOR, VRMOVCLI,
       DTMOVCLI, DSHISTMOV, IDTIPMOCVLI, CDFILIAL, CDCAIXA, NRSEQVENDA, DTABERCAIX,
       NRSEQUMOVI, CDTIPORECE, VRSALDOCONS, VRMOVPESCONS, VRSALDPECONS,
       DTATUMOVCLIE, CDOPERADOR, DSATUMOVCLIE)
      VALUES
      (@V_CDCLIENTE, @V_SEQUENCIAL, @V_CDCONSUMIDOR, @V_VRMESUBCONS,
       @P_DATA+0.00002, 'Credito de subsidio', 'C', @P_CDFILIAL, NULL, NULL, NULL,
       NULL, NULL, @V_VRMESUBCONS, 0, @V_VRSALDOPESS,
       @P_DATA+0.00002, @P_CDOPERADOR,'Ajuste de Saldo Mensal do Consumidor');

      FETCH NEXT FROM CUR_CONS INTO @V_CDCLIENTE, @V_CDCONSUMIDOR, @V_VRMESUBCONS;

   END;

   CLOSE CUR_CONS;
   DEALLOCATE CUR_CONS;
   
END;
GO

-- VERSAO --
EXEC ATUVEROBJ 'INICIALIZA_SALDO_CONSUMIDOR','5.07.008';
GO

