/****** Object:  StoredProcedure [dbo].[P_EST_NF_SAI_IT_RECALCULO_CUSTO_ALL]    Script Date: 11/28/2013 09:36:38 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[P_EST_NF_SAI_IT_RECALCULO_CUSTO_ALL]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[P_EST_NF_SAI_IT_RECALCULO_CUSTO_ALL]
GO
GO
--EXECUTE P_EST_NF_SAI_IT_RECALCULO_CUSTO_ALL 11, 2013
CREATE PROCEDURE [dbo].[P_EST_NF_SAI_IT_RECALCULO_CUSTO_ALL]( @MES INT=NULL, @ANO INT=NULL,@CD_FILIAL_PAR INT=0 )  AS
 	DECLARE @CD_FILIAL AS INT
	DECLARE @AGORA DATETIME
	
	SET NOCOUNT ON
	IF @ANO IS NULL OR @MES IS NULL
		begin
			SELECT @AGORA=DATEADD(month, -1, GETDATE()), @ANO=YEAR(@AGORA), @MES=MONTH(@AGORA)
		end
	 
	DECLARE cSaiFilial CURSOR FAST_FORWARD READ_ONLY FOR 
	SELECT  
	 	CD_FILIAL
	FROM 
		PRC_FILIAL
	WHERE 
		CD_EMP = 1  
		AND CD_FILIAL = CASE WHEN @CD_FILIAL_PAR = 0 THEN CD_FILIAL ELSE @CD_FILIAL_PAR END
		
	OPEN cSaiFilial
	FETCH NEXT FROM cSaiFilial INTO @CD_FILIAL

	WHILE @@FETCH_STATUS=0
	BEGIN
		  
		PRINT 'Filial ' + CONVERT(VARCHAR, @CD_FILIAL) 
		 
		EXECUTE P_EST_NF_SAI_IT_RECALCULO_CUSTO 1, @CD_FILIAL,@MES,@ANO,1;
		 
		FETCH NEXT FROM  cSaiFilial INTO @CD_FILIAL 
	END
	
CLOSE cSaiFilial
DEALLOCATE cSaiFilial







GO


