/****** Object:  StoredProcedure [dbo].[P_ATUALIZA_PRECO_TABLOIDE_REVISAO]    Script Date: 04/02/2014 12:39:26 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[P_ATUALIZA_PRECO_TABLOIDE_REVISAO]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[P_ATUALIZA_PRECO_TABLOIDE_REVISAO]
GO
CREATE PROCEDURE P_ATUALIZA_PRECO_TABLOIDE_REVISAO @CD_EMP INT=0, @CD_USU INT=1 AS
SET NOCOUNT ON
DECLARE @CD_FILIAL INT
DECLARE FILIALZERARVALOROFERTA CURSOR FAST_FORWARD FOR 
	SELECT 
		CD_FILIAL
	FROM
		PRC_FILIAL
	ORDER BY
		CD_FILIAL
						
OPEN FILIALZERARVALOROFERTA
FETCH NEXT FROM FILIALZERARVALOROFERTA INTO @CD_FILIAL
	WHILE @@FETCH_STATUS=0
	  BEGIN
		PRINT 'ANALISANDO FILIAL.: ' + CONVERT(VARCHAR, @CD_FILIAL) + ' ' + CONVERT(VARCHAR, GETDATE (),108)

		UPDATE
			EST_PROD_PRECO
		SET 
			VLR_OFERTA=0
		WHERE
			VLR_OFERTA > 0
			AND CD_FILIAL = @CD_FILIAL
			AND CD_EMP =@CD_EMP

		FETCH NEXT FROM FILIALZERARVALOROFERTA INTO @CD_FILIAL
	  END

PRINT 'CONCLUIDO.'
CLOSE FILIALZERARVALOROFERTA;
DEALLOCATE FILIALZERARVALOROFERTA;

PRINT 'Trantando tlaboides'
UPDATE
 EST_PROD_TABLOIDE
SET 
 FLAG_VIGENCIA=0
UPDATE 
 EST_PROD_TABLOIDE
SET
 FLAG_VIGENCIA=2
WHERE      
 EST_PROD_TABLOIDE.DT_FIM < CONVERT(DATE,GETDATE())
 AND EST_PROD_TABLOIDE.FLAG_VIGENCIA IN(1,0)
 AND EST_PROD_TABLOIDE.CD_EMP=@CD_EMP

PRINT 'Aplicando tlaboides'
EXEC P_ATUALIZA_PRECO_TABLOIDE @CD_EMP,0,@CD_USU
PRINT 'Aplicando oferta permanente'
EXEC P_EST_PROD_PRECO_OFERTA_PERMANENTE