/****** Object:  StoredProcedure [dbo].[P_PDV_VD_IT_RECALCULAR_COMISSAO_EXECUTE_ALL]    Script Date: 11/07/2013 18:13:23 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[P_PDV_VD_IT_RECALCULAR_COMISSAO_EXECUTE_ALL]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[P_PDV_VD_IT_RECALCULAR_COMISSAO_EXECUTE_ALL]
GO
GO
GO
--exec P_PDV_VD_IT_RECALCULAR_COMISSAO_EXECUTE_ALL '2016-03-21','2016-03-21'
CREATE PROCEDURE [dbo].[P_PDV_VD_IT_RECALCULAR_COMISSAO_EXECUTE_ALL] @DT_INI DATETIME = NULL, @DT_FIM DATETIME = NULL AS
PRINT 'Recalculo de COMISSAO'  
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 -2
	
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_COMIS_RECALC 1,@CD_FILIAL,'',@DT_INI,@DT_FIM

		FETCH NEXT FROM filiais INTO @CD_FILIAL
	  end

PRINT 'Concluido.'
CLOSE filiais;
DEALLOCATE filiais;
