/****** Object:  StoredProcedure [dbo].[P_RC_CLI_CONV_PBM_CONTROLE_EXECUTE_ALL]    Script Date: 11/07/2013 18:13:23 ******/

IF EXISTS
(
    SELECT *
    FROM sys.objects
    WHERE object_id = OBJECT_ID(N'[dbo].[P_RC_CLI_CONV_PBM_CONTROLE_EXECUTE_ALL]')
          AND type IN(N'P', N'PC')
)
    BEGIN
        DROP PROCEDURE [dbo].[P_RC_CLI_CONV_PBM_CONTROLE_EXECUTE_ALL]
    END;
GO
GO
GO
--EXEC P_RC_CLI_CONV_PBM_CONTROLE_EXECUTE_ALL 
CREATE PROCEDURE [dbo].[P_RC_CLI_CONV_PBM_CONTROLE_EXECUTE_ALL]  @CD_FILIAL_PAR INT=0
AS
    BEGIN
        PRINT 'Inicio do recalculo';
        SET NOCOUNT ON;
        DECLARE @CD_FILIAL INT;
        DECLARE @CD_EMP INT 

	PRINT 'Analise processo de vinculo PBM ' + CONVERT(VARCHAR(8), GETDATE(), 114) + ')...';
	EXEC P_EST_NF_ENT_PBM_VINCULACAO 1 ;
	PRINT 'Concluido.' ;
	
	PRINT 'Analise reposicao PBM centralizada'
	EXEC P_RC_CLI_CONV_PBM_CONTROLE_CENTRALIZADO
	PRINT 'Concluido'
	
    DECLARE ProcPainelPBMALL CURSOR FAST_FORWARD FOR 
	SELECT 
		CD_EMP, 
		CD_FILIAL
        FROM 
			PRC_FILIAL
		WHERE 
			CD_FILIAL = CASE WHEN  @CD_FILIAL_PAR > 0 THEN  @CD_FILIAL_PAR ELSE CD_FILIAL END ;

	
		OPEN ProcPainelPBMALL;
        FETCH NEXT FROM ProcPainelPBMALL INTO @CD_EMP,@CD_FILIAL;
        WHILE @@FETCH_STATUS = 0
            BEGIN
             PRINT 'Filial '+ CONVERT( VARCHAR(10), @CD_FILIAL) + ' ' + convert(varchar,getdate(),108);
             
				EXEC P_RC_CLI_CONV_PBM_CONTROLE @CD_EMP,@CD_FILIAL;

               FETCH NEXT FROM ProcPainelPBMALL INTO @CD_EMP,@CD_FILIAL;
            END;
        PRINT 'Concluido.';
        CLOSE ProcPainelPBMALL;
        DEALLOCATE ProcPainelPBMALL;
    END;