IF OBJECT_ID('P_PDV_VD_IT_FIDELIDADE_EXECUTE_ALL') IS NOT NULL
	BEGIN
		DROP PROCEDURE [dbo].[P_PDV_VD_IT_FIDELIDADE_EXECUTE_ALL]
	END
 GO 
GO
--EXEC P_PDV_VD_IT_FIDELIDADE_EXECUTE_ALL '2016-06-01','2016-07-04',1
CREATE PROCEDURE [dbo].[P_PDV_VD_IT_FIDELIDADE_EXECUTE_ALL] 
						@DT_INI DATETIME = NULL, 
						@DT_FIM DATETIME = NULL,
						@FLAG_REPROCESSAR INT=0 AS
PRINT 'Inicio de pontuacao'  
SET NOCOUNT ON
DECLARE @CD_FILIAL INT

IF @DT_INI IS NULL
begin
	SELECT @DT_INI=dbo.F_ONTEM()
	SET @DT_FIM = @DT_INI
	SET @DT_INI= @DT_INI -7
	
end

DECLARE filiais CURSOR FAST_FORWARD FOR 
		SELECT CD_FILIAL 
		FROM PRC_FILIAL 
		ORDER BY CD_FILIAL
		
		
OPEN filiais
FETCH NEXT FROM filiais INTO @CD_FILIAL
	WHILE @@FETCH_STATUS=0
	  begin
		PRINT 'Filial ' + CONVERT(VARCHAR, @CD_FILIAL) + ' ' + CONVERT(VARCHAR, @DT_INI,112) + ' a ' + CONVERT(VARCHAR, @DT_FIM,112)
		
		EXEC P_PDV_VD_IT_FIDELIDADE_PONTUAR_FILIAL 1,@CD_FILIAL,@DT_INI,@DT_FIM,@FLAG_REPROCESSAR

		FETCH NEXT FROM filiais INTO @CD_FILIAL
	  end

PRINT 'Concluido.'
CLOSE filiais;
DEALLOCATE filiais;

