20080306
20080304

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

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE  PROCEDURE [dbo].[POS_SAVE_SALE_H]

	@WORK_KIND			VARCHAR(1),
	@CD_STORE 	    		VARCHAR(4),
	@YMD_SALE			VARCHAR(8),
	@NO_POS			VARCHAR(2),
	@NO_RCP			VARCHAR(4),
	@DS_SALE			VARCHAR(1),
	@AMT_SALE			INT,
	@AMT_DUTY			INT,
	@AMT_TAX			INT,
	@AMT_DUTYFREE		INT,
	@AMT_CASH			INT,
	@AMT_CARD			INT,
	@AMT_CHECK			INT,
	@AMT_TRUST			INT,
	@AMT_GIFT			INT,
	@AMT_POINT			INT,
	@AMT_LOSS			INT,
	@DC_MENU			INT,
	@DC_RECEIPT			INT,
	@DC_ENURI			INT,
	@DC_CODE			INT,
	@DC_COUPON			INT,
	@CD_CODE			VARCHAR(3),
	@DC_MEMBER			INT,	
	@DC_CUT			INT,
	@DC_VAT			INT,
	@AMT_CASHRCP		INT,
	@DS_LOCK			VARCHAR(3),
	@NO_LOCK			VARCHAR(4),
	@CD_SAWON			VARCHAR(6),	
	@CD_MEMBER			VARCHAR(10),
	@YMD_SALE_ORG		VARCHAR(8),
	@NO_RCP_ORG		VARCHAR(4),
	@CD_MENU			VARCHAR(14),
	@AMT_EXISTPOINT		INT,
	@PNT_OCCUR			INT OUT,
	@PNT_USE			INT,
	@PNT_TOTAL			INT OUT
AS
BEGIN

	--VOID 또는 입금변경시 사용하기위함
	DECLARE @PNT_OCCUR_ORG 	INT
	DECLARE @PNT_USE_ORG 		INT
	DECLARE @AMT_TRUST_ORG 		INT
	DECLARE @CD_MEMBER_ORG	VARCHAR(10)
	DECLARE @NO_TICKET_REF		VARCHAR(14)

	SELECT @PNT_OCCUR_ORG = PNT_OCCUR,
		  @PNT_USE_ORG	=PNT_USE,
		  @AMT_TRUST_ORG	=AMT_TRUST,
		  @CD_MEMBER_ORG	=CD_MEMBER
	   FROM SL_SALE_H
	WHERE CD_STORE	=@CD_STORE
	      AND YMD_SALE	=@YMD_SALE
	      AND NO_POS	=@NO_POS
	      AND NO_RCP	=@NO_RCP

	/**************************************************************************************************************************************************************************************/
	/*                                               					포인트 적용   시작 								   */
	/**************************************************************************************************************************************************************************************/

	--결제변경일 경우에는 무조건 이전의 적립 및 사용내역을 삭제한다
	IF @WORK_KIND IN('C','V')
	BEGIN

		DELETE 
		   FROM SL_PNT_OCCUR
		WHERE LINK		=@YMD_SALE+@NO_POS+@NO_RCP

		DELETE 
		   FROM SL_PNT_PAY
		WHERE  LINK		=@YMD_SALE+@NO_POS+@NO_RCP	

		UPDATE SL_POINT SET PNT_OCCUR = PNT_OCCUR - @PNT_OCCUR_ORG,
					  PNT_USE	=PNT_USE	 - @PNT_USE_ORG
		 WHERE  CD_MEMBER	=@CD_MEMBER_ORG

		UPDATE MS_MEMBER SET 	AMT_SALE = AMT_SALE - @AMT_SALE
		 WHERE CD_MEMBER	=@CD_MEMBER

		SELECT @PNT_TOTAL = PNT_TOTAL
		   FROM SL_POINT
		 WHERE CD_MEMBER	=@CD_MEMBER
		
	END
	ELSE SET @PNT_USE = @AMT_POINT

	IF (@CD_MEMBER <> '' AND @WORK_KIND <> 'V')
	BEGIN
		DECLARE @CASHAMT_STD	INT
		DECLARE @CARDAMT_STD	INT
		DECLARE @CASHPNT_STD	INT
		DECLARE @CARDPNT_STD	INT

		DECLARE @CASHAMT		INT
		DECLARE @CARDAMT		INT
		DECLARE @PNT_CASH	INT
		DECLARE @PNT_CARD	INT


		--회원이면 회원별로 포인트가 설정되어져있는지 체크한다
		IF @CD_MEMBER <> ''
		BEGIN
			
			SELECT @CASHAMT_STD = CONVERT(INT, ISNULL(A.NM_CODE5,'0')),
				  @CASHPNT_STD = CONVERT(INT, ISNULL(A.NM_CODE6,'0')),
				  @CARDAMT_STD = CONVERT(INT, ISNULL(A.NM_CODE7,'0')),
				  @CARDPNT_STD = CONVERT(INT, ISNULL(A.NM_CODE8,'0'))
			    FROM MS_CODE A, MS_MEMBER  B
			 WHERE A.CD_KIND    		= '05'
	                                  AND A.CD_CODE   		=B.DS_MEMBER
			       AND A.CD_STORE 		=@CD_STORE
			       AND B.CD_MEMBER	=@CD_MEMBER

			SET @CASHAMT_STD = ISNULL(@CASHAMT_STD,0)
			SET @CARDAMT_STD = ISNULL(@CARDAMT_STD,0)
			SET @CASHPNT_STD  = ISNULL(@CASHPNT_STD,0)
			SET @CARDPNT_STD = ISNULL(@CARDPNT_STD,0)

		END

		
		IF (@CASHAMT_STD = 0) OR (@CARDAMT_STD = 0)
		BEGIN
			SELECT @CASHAMT_STD = CASHAMT_STD,
				  @CASHPNT_STD = CASH_POINT,
				  @CARDAMT_STD = CARDAMT_STD,
				  @CARDPNT_STD = CARD_POINT
			    FROM MS_STORE
			 WHERE CD_STORE =@CD_STORE
		END

	
		SET @CASHAMT =ISNULL(@AMT_CASH,0)+ISNULL(@AMT_CHECK,0)+ISNULL(@AMT_GIFT,0)
		SET @CARDAMT = @AMT_CARD

		--카드금액이 없으면 포인트제외 금액을 현금에서 뺀다
		IF @CARDAMT = 0 
			SET @CASHAMT = @CASHAMT - @AMT_EXISTPOINT

		--카드금액이 있으면 포인트제외 금액을 카드금에서 뺀다(카드금액이 포인트 제외금액보다 작을때는 귀찮아서 처리안함)
		IF @CARDAMT > 0
			SET @CARDAMT = @CARDAMT - @AMT_EXISTPOINT

		IF @CASHPNT_STD > 0
			SET	@PNT_CASH = @CASHAMT / @CASHAMT_STD * @CASHPNT_STD
		ELSE
			SET	@PNT_CASH = 0

		IF @CARDPNT_STD > 0
			SET	@PNT_CARD = @CARDAMT / @CASHAMT_STD * @CARDPNT_STD
		ELSE
			SET	@PNT_CARD = 0

		SET 	@PNT_OCCUR = @PNT_CASH + @PNT_CARD

		--포인트를 사용한 영수증은 포인트를 적립하지 않는다
		IF (@PNT_USE <> 0)
			SET @PNT_OCCUR = 0

		--VOID일때는 발생포인트에 *-1을 곱한다
		IF @WORK_KIND = 'V'
		BEGIN
			SET @PNT_OCCUR = @PNT_OCCUR * -1
		END

		SELECT @PNT_TOTAL = PNT_TOTAL FROM SL_POINT WHERE CD_MEMBER = @CD_MEMBER
	
		--총포인트
		SELECT @PNT_TOTAL = @PNT_OCCUR + @PNT_TOTAL - @PNT_USE
	
		DECLARE @PNT_SEQ INT

		--적립에 대해 적용
		IF (@PNT_OCCUR <> 0)
		BEGIN
			SELECT @PNT_SEQ = ISNULL(MAX(SEQ),0) +1
			   FROM SL_PNT_OCCUR
			WHERE YMD_OCCUR = @YMD_SALE
		
			INSERT INTO SL_PNT_OCCUR( 
							CD_MEMBER,
							YMD_OCCUR,
							SEQ,
							CD_CODE,
							AMT_CASH,
							AMT_CARD,
							PNT_CASH,
							PNT_CARD,
							LINK
							)
					VALUES(
							@CD_MEMBER,
							@YMD_SALE,
							@PNT_SEQ,
							'000',
							@AMT_CASH,
							@AMT_CARD,
							@PNT_CASH,
							@PNT_CARD,
							@YMD_SALE+@NO_POS+@NO_RCP	
						)
		END
		ELSE IF @PNT_USE <> 0
		BEGIN
			SELECT @PNT_SEQ = ISNULL(MAX(SEQ),0) +1
			   FROM SL_PNT_PAY
			WHERE  YMD_PAY = @YMD_SALE
		
			INSERT INTO SL_PNT_PAY( 
							CD_MEMBER,
							YMD_PAY,
							SEQ,
							CD_CODE,
							PNT_USE,
							LINK
							)
					VALUES(
							@CD_MEMBER,
							@YMD_SALE,
							@PNT_SEQ,
							'000',
							@PNT_USE,
							@YMD_SALE+@NO_POS+@NO_RCP	
						)
		END
			
		SELECT CD_MEMBER FROM SL_POINT WHERE  CD_MEMBER = @CD_MEMBER
	
		IF (@@ROWCOUNT > 0)
		BEGIN
			UPDATE SL_POINT SET PNT_OCCUR = PNT_OCCUR + @PNT_OCCUR,
						  PNT_USE	= PNT_USE + @PNT_USE
			WHERE CD_MEMBER 	= @CD_MEMBER
		END
		ELSE
		BEGIN
			INSERT INTO 	SL_POINT(	
							CD_MEMBER,
							PNT_OCCUR,
							PNT_USE
						)
					VALUES(
							@CD_MEMBER,
							@PNT_OCCUR,
							@PNT_USE
						)
		END	

	END

	/**************************************************************************************************************************************************************************************/
	/*                                               					포인트 적용    END 								   */
	/**************************************************************************************************************************************************************************************/

	/**************************************************************************************************************************************************************************************/
	/*                                               					회원 외상 적용  시작 								   */
	/**************************************************************************************************************************************************************************************/



	IF @WORK_KIND IN ('S','B')
	BEGIN
	
		DECLARE @COME_TIME	DATETIME

		IF @WORK_KIND = 'S'
		BEGIN
			SELECT @COME_TIME 	=COME_TIME
			   FROM SL_ORDER_H
			WHERE CD_STORE	=@CD_STORE
			      AND DS_LOCK	=@DS_LOCK
			      AND NO_LOCK	=@NO_LOCK

			SET @COME_TIME = ISNULL(@COME_TIME, GETDATE())
			EXEC SAVE_LOCKER_CANCEL @CD_STORE, @YMD_SALE, @DS_LOCK, @NO_LOCK

			IF (@CD_MEMBER <> '') AND (@CD_MENU <> '')
			BEGIN
				INSERT INTO SL_MEMBER_USE (YMD_SALE, NO_RCP, CD_MEMBER, CD_MENU)
							VALUES   (CONVERT(VARCHAR(8), GETDATE(), 112), @CD_STORE+@YMD_SALE+@NO_POS+@NO_RCP, @CD_MEMBER, @CD_MENU)
			END

			--입장권번호
			SELECT @NO_TICKET_REF = YMD_SALE+NO_POS+NO_RCP 
			   FROM SL_LOCK_ALLOT
			WHERE CD_STORE	=@CD_STORE
			      AND DS_LOCK	=@DS_LOCK
			      AND NO_LOCK	=@NO_LOCK
			      AND OUT_TIME	IS NULL
		END
		--반품이면 회원 이용내역을 삭제한다
		ELSE IF @WORK_KIND = 'B'		
		BEGIN
			SET @COME_TIME = GETDATE()
			IF @CD_MEMBER <> ''
			BEGIN
				DELETE FROM SL_MEMBER_USE WHERE NO_RCP = @CD_STORE+@YMD_SALE+@NO_POS+@NO_RCP
			END
		END

		INSERT INTO SL_SALE_H(
						CD_STORE,
						YMD_SALE,
						NO_POS,
						NO_RCP,
						DS_SALE,
						AMT_SALE,
						AMT_DUTY,
						AMT_TAX,
						AMT_DUTYFREE,
						AMT_CASH,
						AMT_CARD,
						AMT_CHECK,
						AMT_TRUST,
						AMT_GIFT,
						AMT_POINT,
						AMT_LOSS,
						DC_MENU,
						DC_RECEIPT,
						DC_ENURI,
						DC_CODE,
						CD_CODE,
						DC_MEMBER,
						DC_COUPON,
						DC_CUT,
						DC_VAT,
						AMT_CASHRCP,
						DS_LOCK,
						NO_LOCK,
						COME_TIME,
						DT_SALE,
						CD_SAWON,
						CD_MEMBER,
						NO_RCP_ORG,
						PNT_OCCUR,
						PNT_USE,
						NO_ACCT_REF,
						NO_TICKET_REF
						)
					VALUES
						(
						@CD_STORE,
						@YMD_SALE,
						@NO_POS,
						@NO_RCP,
						@DS_SALE,
						@AMT_SALE,
						@AMT_DUTY,
						@AMT_TAX,
						@AMT_DUTYFREE,
						@AMT_CASH,
						@AMT_CARD,
						@AMT_CHECK,
						@AMT_TRUST,
						@AMT_GIFT,
						@AMT_POINT,
						@AMT_LOSS,
						@DC_MENU,
						@DC_RECEIPT,
						@DC_ENURI,
						@DC_CODE,
						@CD_CODE,
						@DC_MEMBER,
						@DC_COUPON,
						@DC_CUT,
						@DC_VAT,
						@AMT_CASHRCP,
						@DS_LOCK,
						@NO_LOCK,
						@COME_TIME,
						GETDATE(),
						@CD_SAWON,
						@CD_MEMBER,
						@YMD_SALE_ORG +  @NO_POS + @NO_RCP_ORG,
						@PNT_OCCUR,
						@PNT_USE,
						'',
						@NO_TICKET_REF	
						)

						

		IF @CD_MEMBER <> ''
		BEGIN
			UPDATE MS_MEMBER SET 	YMD_VISIT = CONVERT(VARCHAR(8), GETDATE(), 112),
							AMT_SALE = AMT_SALE + @AMT_SALE,
							CNT_VISIT = CNT_VISIT + 1
			 WHERE CD_MEMBER	=@CD_MEMBER
		END


		--반품일때
		IF @WORK_KIND = 'B'
		BEGIN
			INSERT INTO SL_SALE_D(
							CD_STORE,
							YMD_SALE,
							NO_POS,
							NO_RCP,
							DS_SALE,
							CD_MENU,
							SEQ,
							QTY_SALE,	
							PR_SALE,
							AMT_SALE,
							DC_MENU,
							DC_RECEIPT,
							DC_MEMBER,
							AMT_VAT,
							DS_TAX
							)
						
					SELECT	CD_STORE,
							@YMD_SALE,
							NO_POS,
							@NO_RCP,
							DS_SALE,
							CD_MENU,
							SEQ,
							QTY_SALE *-1,
							PR_SALE,
							AMT_SALE *-1,
							DC_MENU * -1,
							DC_RECEIPT * -1,
							DC_MEMBER*-1,
							AMT_VAT * -1,
							DS_TAX
			
					FROM		SL_SALE_D
					WHERE	CD_STORE	=@CD_STORE
					      AND 	YMD_SALE	=@YMD_SALE_ORG
					      AND 	NO_POS	=@NO_POS
					      AND 	NO_RCP	=@NO_RCP_ORG

						
		END

					
	END
	--현금영수증만 발행
	ELSE IF @WORK_KIND = 'H'
	BEGIN
		UPDATE SL_SALE_H SET
						AMT_CASHRCP=@AMT_CASHRCP,
						DT_CHANGE  	=GETDATE()

		WHERE CD_STORE	=@CD_STORE
		      AND YMD_SALE	=@YMD_SALE
		      AND NO_POS	=@NO_POS
		      AND NO_RCP	=@NO_RCP

	END
	--결제변경
	ELSE IF @WORK_KIND = 'C' 
	BEGIN
		UPDATE SL_SALE_H SET	AMT_SALE		=@AMT_SALE,
						AMT_CASH		=@AMT_CASH,
						AMT_CARD		=@AMT_CARD,
						AMT_CHECK		=@AMT_CHECK,
						AMT_TRUST		=@AMT_TRUST,
						AMT_GIFT		=@AMT_GIFT,
						AMT_POINT		=@AMT_POINT,
						AMT_LOSS		=@AMT_LOSS,
						AMT_CASHRCP	=@AMT_CASHRCP,
						DC_MENU		=@DC_MENU,
						CD_MEMBER		=@CD_MEMBER,
						DC_MEMBER		=@DC_MEMBER,
						DC_RECEIPT		=@DC_RECEIPT,
						DC_ENURI		=@DC_ENURI,
						DC_CODE		=@DC_CODE,
						DC_COUPON		=@DC_COUPON,
						DC_CUT		=@DC_CUT,
						DC_VAT		=@DC_VAT,
						CD_SAWON		=@CD_SAWON,
						AMT_DUTY		=@AMT_DUTY,
						AMT_TAX		=@AMT_TAX,
						PNT_OCCUR		=@PNT_OCCUR,
						PNT_USE		=@PNT_USE,
						DT_CHANGE  		=GETDATE()

		WHERE CD_STORE	=@CD_STORE
		      AND YMD_SALE	=@YMD_SALE
		      AND NO_POS	=@NO_POS
		      AND NO_RCP	=@NO_RCP

		DELETE 
		   FROM SL_SALE_D
		WHERE CD_STORE	=@CD_STORE
		      AND YMD_SALE	=@YMD_SALE
		      AND NO_POS	=@NO_POS
		      AND NO_RCP	=@NO_RCP

	END
	-- VOID 처리
	ELSE IF @WORK_KIND = 'V'
	BEGIN

		UPDATE SL_SALE_H SET
						DS_SALE	='V',
						DT_CHANGE  	=GETDATE()

		WHERE CD_STORE	=@CD_STORE
		      AND YMD_SALE	=@YMD_SALE
		      AND NO_POS	=@NO_POS
		      AND NO_RCP	=@NO_RCP


		--회원이용내역을 삭제한다
		IF @CD_MEMBER <> ''
		BEGIN
			DELETE FROM SL_MEMBER_USE WHERE NO_RCP = @CD_STORE+@YMD_SALE+@NO_POS+@NO_RCP
		END

	END
	ELSE IF @WORK_KIND = 'P'
	BEGIN
		UPDATE SL_SALE_H SET
						CD_MEMBER=@CD_MEMBER,
						PNT_OCCUR = @PNT_OCCUR,
						DT_CHANGE  	=GETDATE()

		WHERE CD_STORE	=@CD_STORE
		      AND YMD_SALE	=@YMD_SALE
		      AND NO_POS	=@NO_POS
		      AND NO_RCP	=@NO_RCP

		IF @CD_MEMBER <> ''
		BEGIN
			UPDATE MS_MEMBER SET 	YMD_VISIT = CONVERT(VARCHAR(8), GETDATE(), 112),
							AMT_SALE = AMT_SALE + @AMT_SALE,
							CNT_VISIT = CNT_VISIT + 1
			 WHERE CD_MEMBER	=@CD_MEMBER
		END

	END


	SELECT @PNT_OCCUR = ISNULL(@PNT_OCCUR, 0)
	SELECT @PNT_TOTAL = ISNULL(@PNT_TOTAL, 0)


END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO





UPDATE MS_MEMBER SET MS_MEMBER.AMT_SALE = (SELECT SUM(T1.AMT_SALE) FROM SL_SALE_H T1 WHERE  MS_MEMBER.CD_MEMBER = T1.CD_MEMBER AND DS_SALE <> 'V'),
                     MS_MEMBER.CNT_VISIT = (SELECT COUNT(*) FROM SL_SALE_H T1 WHERE MS_MEMBER.CD_MEMBER = T1.CD_MEMBER AND DS_SALE <> 'V')
  FROM MS_MEMBER
GO

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 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, SL_MEMBER_DELAY B
			WHERE SL_MEMBER_JOIN.CD_MEMBER = B.CD_MEMBER
			     AND SL_MEMBER_JOIN.NO_RCP        = B.NO_RCP
			     AND SL_MEMBER_JOIN.DS_STATUS    = '0'
			     AND B.YMD_FROM >= CONVERT(VARCHAR(8), GETDATE(), 112)
							
			SET @RESULT = ''
		END
	END


END
GO


SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

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

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO





CREATE  PROCEDURE SAVE_MEMBER_DELAY

	@YMD_SALE			VARCHAR(8),
	@NO_RCP			VARCHAR(4),
	@YMD_DELAY			VARCHAR(8),
	@CD_DELAY			VARCHAR(3),
	@YMD_FROM			VARCHAR(8),
	@YMD_TO			VARCHAR(8),
	@CD_SAWON_CHG		VARCHAR(6),
	@WORK_KIND			INT		--0:신규, 1:수정
AS
BEGIN

	DECLARE	@CD_MEMBER		VARCHAR(10)
	DECLARE	@DS_STATUS		VARCHAR(1)

	IF (CONVERT(VARCHAR(8), GETDATE(), 112) >= @YMD_FROM) AND (CONVERT(VARCHAR(8), GETDATE(), 112) <= @YMD_TO)
		SET @DS_STATUS = '1'
	ELSE
		SET @DS_STATUS = '0'

	SELECT @CD_MEMBER = CD_MEMBER FROM SL_MEMBER_JOIN WHERE YMD_SALE =@YMD_SALE AND NO_RCP = @NO_RCP


	IF @WORK_KIND = 0
	BEGIN
		INSERT INTO  SL_MEMBER_DELAY (	
							YMD_SALE,
							NO_RCP,
							YMD_DELAY,
							CD_DELAY,
							CD_MEMBER,
							YMD_FROM,
							YMD_TO,
							CD_SAWON_CHG
						)
	
	               		 	VALUES(	
							@YMD_SALE,
							@NO_RCP,
							@YMD_DELAY,
							@CD_DELAY,
							@CD_MEMBER,
							@YMD_FROM,
							@YMD_TO,
							@CD_SAWON_CHG
						)		
	END
	ELSE
	BEGIN
		UPDATE SL_MEMBER_DELAY SET CD_DELAY	=@CD_DELAY,
						       YMD_TO	=@YMD_TO
		 WHERE YMD_SALE	=@YMD_SALE
		       AND NO_RCP	=@NO_RCP
		       AND YMD_DELAY	=@YMD_DELAY
	END



	UPDATE  SL_MEMBER_JOIN     SET 
					         DELAY_BEGIN	= @YMD_FROM,
					        DELAY_TO	= @YMD_TO,
					        DAY_DELAY = DATEDIFF( DAY, CONVERT(DATETIME, @YMD_FROM), CONVERT(DATETIME, @YMD_TO)),
					         YMD_FINISH	=  CONVERT(VARCHAR(8), DATEADD(DAY, DATEDIFF( DAY, CONVERT(DATETIME, @YMD_FROM), CONVERT(DATETIME, @YMD_TO)), CONVERT(DATETIME, YMD_FINISH)    ) , 112),
					        DS_STATUS          = @DS_STATUS
			WHERE    YMD_SALE 		= @YMD_SALE
			      AND    NO_RCP		= @NO_RCP
	
	
	

	
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

UPDATE SL_MEMBER_JOIN SET DS_STATUS = '1'
  FROM SL_MEMBER_JOIN, SL_MEMBER_DELAY B
 WHERE SL_MEMBER_JOIN.CD_MEMBER = B.CD_MEMBER
   AND SL_MEMBER_JOIN.NO_RCP        = B.NO_RCP
   AND SL_MEMBER_JOIN.DS_STATUS    = '0'
   AND B.YMD_FROM >= CONVERT(VARCHAR(8), GETDATE(), 112)
GO
