20090721
20090720
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[POS_SAVE_DAYOPEN]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[POS_SAVE_DAYOPEN]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

--         포스 일마감 체크 --
CREATE PROCEDURE POS_SAVE_DAYOPEN
    @CD_STORE	VARCHAR(4),
    @YMD_OPEN	VARCHAR(8),
    @NO_POS		VARCHAR(2),
    @RESULT		VARCHAR(12) OUTPUT
AS
BEGIN

	DECLARE	@CONF	VARCHAR(1)

	SELECT @CONF = SUBSTRING(CONF,10,1) FROM MS_STORE WHERE CD_STORE =@CD_STORE
		
	SET @RESULT = ''
	SELECT @RESULT = DS_STATUS
	   FROM SL_POSCLOSE
	WHERE CD_STORE	=@CD_STORE
	      AND YMD_CLOSE	=@YMD_OPEN
	      AND NO_POS	=@NO_POS

	--마감이 안됐을때
	IF @RESULT <> 'C' 
	BEGIN
		
		--관리프로그램 개점처리
		DECLARE @TEMP1	VARCHAR(1)
		DECLARE @POSNO	VARCHAR(2)

		SELECT @TEMP1 = SUBSTRING(CONF,43,1),
			 @POSNO = NO_POS
		   FROM MS_STORE
		WHERE CD_STORE	=@CD_STORE

		IF (@TEMP1 = '1') AND (@NO_POS = @POSNO)
		BEGIN
			DECLARE @CNT	INT
			
			SELECT @CNT = COUNT(*) 
			   FROM SL_DAY_CLOSE
			WHERE CD_STORE = @CD_STORE
			      AND YMD_CLOSE = @YMD_OPEN

			IF (@CNT = 0)
			BEGIN
				INSERT INTO SL_DAY_CLOSE(CD_STORE, YMD_CLOSE, YN_CLOSE)
						VALUES(@CD_STORE, @YMD_OPEN, 'N')
			END
			

		END

		DECLARE @TEMP VARCHAR(12)

		--전일자에 마감이 안된 일자가 있는지 체크
		SELECT TOP 1 @TEMP =  DBO.STOD(YMD_CLOSE)+NO_POS
		   FROM SL_POSCLOSE
		WHERE CD_STORE	= @CD_STORE
		      AND YMD_CLOSE	< @YMD_OPEN
		      AND NO_POS	=@NO_POS
		      AND DS_STATUS	='O'
		      
		IF @TEMP IS NULL SET @RESULT = ''
		ELSE SET @RESULT = @TEMP
	
		IF (@RESULT = '') OR (@CONF = '1')
		BEGIN
			INSERT INTO SL_POSCLOSE(	CD_STORE,
							YMD_CLOSE,
							NO_POS,
							DS_STATUS
						          )
					VALUES 
						         (   @CD_STORE,
							@YMD_OPEN,
							@NO_POS,
							'O'
						         )

			UPDATE SL_MEMBER_JOIN SET DS_STATUS = '1'
			FROM SL_MEMBER_JOIN
			WHERE  DS_STATUS    = '0'
			     AND delay_begin <= CONVERT(VARCHAR(8), GETDATE(), 112)
			     and delay_to >= CONVERT(VARCHAR(8), GETDATE(), 112)
							
			SET @RESULT = ''
		END
	END


END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

DELETE MS_SAWON_FAV 
GO
