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

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[POS_SELECT_MEMBER]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[POS_SELECT_MEMBER]
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 POS_SAVE_CASHIER_MGM

	@CD_STORE 	    		VARCHAR(4),
	@YMD_CLOSE			VARCHAR(8),
	@NO_POS			VARCHAR(2),
	@CD_SAWON			VARCHAR(6),
	@AMT_ACCT_IN		INT,
	@AMT_ACCT_OUT		INT,
	@AMT_SALE			INT,
	@AMT_TAX			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_CODE			INT,
	@DC_RECEIPT			INT,
	@DC_MEMBER			INT,
	@DC_ENURI			INT,
	@DC_SPC			INT,
	@DC_COUPON			INT,
	@DC_CUT			INT,
	@DC_VAT			INT,
	@AMT_VOID			INT,
	@CNT_CUSTOMER		INT,
	@AMT_AVERAGE		INT,
	@AMT_LACK			INT,
	@AMT_CASHRCP		INT,
	@RCP_BEGIN			VARCHAR(4),
	@RCP_END			VARCHAR(4),
	@_CHECK			INT,
	@_10000			INT,
	@_5000			INT,
	@_1000			INT,
	@_500				INT,
	@_100				INT,
	@_50				INT,
	@_10				INT
AS
BEGIN
	
	DECLARE @SEQ INT
	DECLARE @CNT INT

	SELECT @SEQ =ISNULL(MAX(SEQ),	(SELECT ISNULL(MAX(SEQ),0)+1
						   FROM SL_CASHIER_MGM
						WHERE CD_STORE 	=@CD_STORE
						      AND YMD_CLOSE	=@YMD_CLOSE
						      AND NO_POS	=@NO_POS
						      AND CD_SAWON	=@CD_SAWON
						      AND YN_CLOSE	='Y'
				)
			)
	  FROM SL_CASHIER_MGM 
	 WHERE CD_STORE 	=@CD_STORE
	   AND YMD_CLOSE	=@YMD_CLOSE
	   AND NO_POS		=@NO_POS
	   AND CD_SAWON	=@CD_SAWON
	   AND YN_CLOSE = 'N' 

	SELECT @CNT= COUNT(*)
	   FROM SL_CASHIER_MGM
	WHERE CD_STORE 	=@CD_STORE
	      AND YMD_CLOSE	=@YMD_CLOSE
	      AND NO_POS	=@NO_POS
	      AND CD_SAWON	=@CD_SAWON
	      AND SEQ		=@SEQ

	IF @CNT = 0
	BEGIN

	
		INSERT INTO SL_CASHIER_MGM(
							CD_STORE		,
							YMD_CLOSE		,
							NO_POS		,
							CD_SAWON		,
							SEQ			,
							AMT_ACCT_IN		,
							AMT_ACCT_OUT	,
							AMT_SALE		,
							AMT_TAX		,
							AMT_CASH		,
							AMT_CARD		,
							AMT_CHECK		,
							AMT_TRUST		,
							AMT_GIFT		,
							AMT_POINT		,
							AMT_LOSS		,
							DC_MENU		,
							DC_CODE		,
							DC_RECEIPT		,
							DC_MEMBER		,
							DC_ENURI		,
							DC_SPC		,
							DC_COUPON		,
							DC_CUT		,
							DC_VAT		,
							AMT_VOID		,
							CNT_CUSTOMER	,
							AMT_AVERAGE	,
							AMT_LACK		,
							AMT_CASHRCP	,
							RCP_BEGIN		,
							RCP_END		,
							_CHECK,
							_10000,
							_5000,
							_1000,
							_500,
							_100,
							_50,
							_10,
							YN_CLOSE		
						)
					VALUES
						(
							 @CD_STORE		,
							 @YMD_CLOSE		,
							 @NO_POS		,
							 @CD_SAWON		,
							 @SEQ			,
							 @AMT_ACCT_IN	,
							 @AMT_ACCT_OUT	,
							 @AMT_SALE		,
							 @AMT_TAX		,
							 @AMT_CASH		,
							 @AMT_CARD		,
							 @AMT_CHECK		,
							 @AMT_TRUST	,
							 @AMT_GIFT		,
							 @AMT_POINT		,
							 @AMT_LOSS		,
							 @DC_MENU		,
							 @DC_CODE		,
							 @DC_RECEIPT	,
							 @DC_MEMBER	,
							 @DC_ENURI		,
							 @DC_SPC		,
							 @DC_COUPON	,
							 @DC_CUT		,
							 @DC_VAT		,
							 @AMT_VOID		,
							 @CNT_CUSTOMER	,
							 @AMT_AVERAGE	,
							 @AMT_LACK		,
							 @AMT_CASHRCP	,
							 @RCP_BEGIN		,
							 @RCP_END		,
							 @_CHECK,
							 @_10000,
							 @_5000,
							 @_1000,
							 @_500,
							 @_100,
							 @_50,
							 @_10,
							 'Y'
						)
	END
	ELSE
	BEGIN
		UPDATE SL_CASHIER_MGM SET 
							
						AMT_SALE	 	=@AMT_SALE		,
						AMT_TAX		=@AMT_TAX		,
						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		,
						DC_MENU	 	=@DC_MENU		,
						DC_CODE	 	=@DC_CODE		,
						DC_RECEIPT	 	=@DC_RECEIPT	,
						DC_MEMBER	 	=@DC_MEMBER	,
						DC_ENURI	 	=@DC_ENURI		,
						DC_SPC	 	=@DC_SPC		,
						DC_COUPON		=@DC_COUPON		,
						DC_CUT		=@DC_CUT		,
						DC_VAT		=@DC_VAT		,
						AMT_VOID	 	=@AMT_VOID		,
						CNT_CUSTOMER	=@CNT_CUSTOMER	,
						AMT_AVERAGE	=@AMT_AVERAGE	,
						AMT_LACK		=@AMT_LACK		,
						AMT_CASHRCP	=@AMT_CASHRCP	,
						RCP_BEGIN	 	=@RCP_BEGIN		,
						RCP_END	 	=@RCP_END		,
						_CHECK		=@_CHECK		,
						_10000			=@_10000		,
						_5000			=@_5000		,
						_1000			=@_1000		,
						_500			=@_500		,
						_100			=@_100		,
						_50			=@_50			,
						_10			=@_10			,
						YN_CLOSE		= 'Y'
			WHERE CD_STORE 	=@CD_STORE
			      AND YMD_CLOSE	=@YMD_CLOSE
			      AND NO_POS	=@NO_POS
			      AND CD_SAWON	=@CD_SAWON
			      AND SEQ		=@SEQ
	END

	--매출헤더에 계산원마감 번호 셋팅
	UPDATE SL_SALE_H SET NO_CLOSE = @SEQ
	WHERE CD_STORE	=@CD_STORE	
	      AND YMD_SALE	=@YMD_CLOSE
	      AND NO_POS	=@NO_POS
	      AND CD_SAWON	=@CD_SAWON
	      AND NO_CLOSE	=0

	--현금출납에 계산원마감 번호 셋팅
	UPDATE SL_ACCT SET NO_CLOSE = @SEQ
	WHERE CD_STORE		=@CD_STORE	
	      AND YMD_OCCUR		=@YMD_CLOSE
	      AND NO_POS		=@NO_POS
	      AND CD_SAWON_CHG	=@CD_SAWON
	      AND NO_CLOSE		=0
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


--회원조회
CREATE PROCEDURE POS_SELECT_MEMBER
	@CD_STORE		VARCHAR(4),
	@CD_MEMBER		VARCHAR(10),
	@DS_STATUS		VARCHAR(1) OUTPUT,		
	@YMD_BEGIN		VARCHAR(10) OUTPUT,
	@YMD_END		VARCHAR(10) OUTPUT,
	@NM_GOODS		VARCHAR(50) OUTPUT,
	@CD_MENU		VARCHAR(14) OUTPUT,
	@CD_MENU2		VARCHAR(14) OUTPUT,
	@NM_MENU		VARCHAR(50) OUTPUT,
	@LOCKER		VARCHAR(50) OUTPUT,
	@ALLOW_TOTCNT	INT	OUTPUT,
	@ALLOW_DAYCNT	INT	OUTPUT,
	@USE_TOT		INT	OUTPUT,
	@DELAY_RANGE	VARCHAR(30) OUTPUT	--연기기간
AS
BEGIN
	DECLARE	@NO_RCP	VARCHAR(4)
	DECLARE	@TODATE	DATETIME
	DECLARE	@YMD_SALE	VARCHAR(8)

	SET @TODATE = convert(VARCHAR(8), GETDATE(), 112) 

	IF LEN(@CD_MEMBER) <> 10
	BEGIN
		SELECT @CD_MEMBER  = CD_MEMBER FROM MS_MEMBER_CARD WHERE DS_STATUS = '0' AND NO_CARD = @CD_MEMBER
	END


	SELECT A.CD_MEMBER,
		 A.NM_MEMBER,
                            A.DS_MEMBER AS CD_CLASS,
		 B.NM_CODE1	AS DS_MEMBER,
		 B.NM_CODE2	AS DC_RATE,
		 A.TEL_MOBIL,
		 ISNULL( (SELECT TOP 1 NO_CARD FROM MS_MEMBER_CARD WHERE CD_MEMBER = A.CD_MEMBER AND DS_STATUS = '0' ), '') AS NO_CARD,
		 A.YMD_VISIT,
		 A.CNT_VISIT,
		 A.YN_TRUST,
		 (SELECT ISNULL(PNT_TOTAL,0) FROM SL_POINT WHERE CD_MEMBER = A.CD_MEMBER ) AS PNT_TOTAL,
		 (SELECT COUNT(*) FROM SL_MEMBER_USE WHERE CD_MEMBER = A.CD_MEMBER AND  CONVERT(VARCHAR(8), DT_SALE, 112)  = CONVERT(VARCHAR(8), GETDATE(), 112)  )  AS USE_DAY,
		CASE A.DS_SEX WHEN '0' THEN '남자' WHEN '1' THEN '여자' END AS GENDER,
	             (SELECT TOP 1 NEWS_TXT FROM SL_MEMBER_NEWS WHERE CD_MEMBER =@CD_MEMBER AND DS_STATUS = '0' ORDER BY SEQ DESC) AS NEWS_TXT
	FROM MS_MEMBER A, MS_CODE B
          WHERE  A.DS_MEMBER	*= B.CD_CODE
	   AND  B.CD_KIND	= '05'
	   AND  B.CD_STORE	=@CD_STORE
	   AND  A.CD_MEMBER	=@CD_MEMBER

	SELECT  @YMD_SALE		=A.YMD_SALE,
		  @YMD_BEGIN	=A.YMD_BEGIN, 
		  @YMD_END		=A.YMD_END, 
		  @NM_GOODS		=B.NM_GOODS, 
		  @CD_MENU		=C.CD_MENU, 
		  @CD_MENU2		=B.CD_MENU2, 
		  @NM_MENU		=C.NM_MENU,
		  @ALLOW_TOTCNT	=B.ALLOW_TOTCNT,
		  @ALLOW_DAYCNT	=B.ALLOW_DAYCNT,
		  @USE_TOT		=(SELECT COUNT(*) FROM SL_MEMBER_USE WHERE CD_MEMBER = A.CD_MEMBER AND  CONVERT(VARCHAR(8), DT_SALE, 112)  BETWEEN A.YMD_BEGIN AND A.YMD_END ) ,
		  @DELAY_RANGE	 = DBO.STOD(A.DELAY_BEGIN) +' ~ ' + DBO.STOD(A.DELAY_TO),
		  @DS_STATUS	=A.DS_STATUS,
		  @NO_RCP		=A.NO_RCP
	   FROM SL_MEMBER_JOIN A, MS_GOODS B, MS_MENU C
	WHERE A.CD_GOODS		= B.CD_GOODS
	      AND B.CD_MENU		= C.CD_MENU
--	      AND A.DS_STATUS		= '0'
	      AND A.CD_MEMBER	=@CD_MEMBER
	      AND A.YMD_BEGIN <= @TODATE 
                   AND A.YMD_FINISH >= @TODATE
--	      AND NOT ( (ISNULL(A.DELAY_BEGIN,@TODATE) <= @TODATE) AND (ISNULL(A.DELAY_TO,@TODATE) <= @TODATE)) 

	IF @YMD_BEGIN = NULL
	BEGIN
		
		SELECT @NO_RCP = MAX(NO_RCP) FROM SL_MEMBER_JOIN WHERE CD_MEMBER	= @CD_MEMBER	

		IF @NO_RCP = NULL
		BEGIN
			SET @DS_STATUS	= '5'
		END
		ELSE
		BEGIN
			SET	@DS_STATUS = '4'
			
			SELECT  @YMD_BEGIN	=A.YMD_BEGIN, 
				  @YMD_END		=A.YMD_END, 
				  @NM_GOODS		=B.NM_GOODS, 
				  @CD_MENU		=C.CD_MENU, 
				  @CD_MENU2		=B.CD_MENU2, 
				  @NM_MENU		=C.NM_MENU,
				  @ALLOW_TOTCNT	=B.ALLOW_TOTCNT,
				  @ALLOW_DAYCNT	=B.ALLOW_DAYCNT,
				  @USE_TOT		=(SELECT COUNT(*) FROM SL_MEMBER_USE WHERE CD_MEMBER = A.CD_MEMBER AND  CONVERT(VARCHAR(8), DT_SALE, 112)  BETWEEN A.YMD_BEGIN AND A.YMD_END ) 
			   FROM SL_MEMBER_JOIN A, MS_GOODS B, MS_MENU C
			WHERE A.CD_GOODS		= B.CD_GOODS
			      AND B.CD_MENU		= C.CD_MENU
			      AND A.DS_STATUS		= '0'
			      AND A.CD_MEMBER	=@CD_MEMBER
			      AND A.NO_RCP		=@NO_RCP
		END
	
	END
	ELSE
	BEGIN
		--환불상태이면 환불일자를 가져온다
		IF @DS_STATUS = '2'
		BEGIN
			SELECT @DELAY_RANGE	= DBO.STOD(YMD_REFUND) FROM SL_MEMBER_REFUND WHERE YMD_SALE =@YMD_SALE AND NO_RCP = @NO_RCP
		END
	END


	SELECT @LOCKER =  DBO.GETCOMMONNAME(@CD_STORE, '03', A.CD_PLACE) +'-'+ A.NO_LOCKER + ' ( '+DBO.GETCOMMONNAME(@CD_STORE, '04', B.CD_LOCKER) +' )' 
	   FROM SL_LOCKER A, MS_LOCKER B
	WHERE A.CD_PLACE	= B.CD_PLACE
	      AND A.NO_LOCKER	= B.NO_LOCKER
	      AND A.CD_MEMBER = @CD_STORE


	SET @DS_STATUS 	= ISNULL(@DS_STATUS,'')
	SET @YMD_BEGIN 	= ISNULL(@YMD_BEGIN,'')
	SET @YMD_END 	= ISNULL(@YMD_END,'')
	SET @NM_GOODS 	= ISNULL(@NM_GOODS,'')
	SET @CD_MENU 	= ISNULL(@CD_MENU,'')
	SET @CD_MENU2 	= ISNULL(@CD_MENU2,'')
	SET @NM_MENU 	= ISNULL(@NM_MENU,'')
	SET @LOCKER 	= ISNULL(@LOCKER,'')
	SET @USE_TOT	= ISNULL(@USE_TOT,0)
	SET @ALLOW_TOTCNT	= ISNULL(@ALLOW_TOTCNT,0)
	SET @ALLOW_DAYCNT	= ISNULL(@ALLOW_DAYCNT,0)
	SET @DELAY_RANGE 	= ISNULL(@DELAY_RANGE,'')

END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
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)
AS
BEGIN
		DECLARE	@CD_MEMBER		VARCHAR(10)
		SELECT @CD_MEMBER = CD_MEMBER FROM SL_MEMBER_JOIN WHERE YMD_SALE =@YMD_SALE AND NO_RCP = @NO_RCP

	
		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
						)		



	
		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          = '1'
				WHERE    YMD_SALE 		= @YMD_SALE
				      AND    NO_RCP		= @NO_RCP
	
	
	

	
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO


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   (@YMD_SALE, @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

		IF @CD_MEMBER <> ''
		BEGIN
			UPDATE MS_MEMBER SET 	YMD_VISIT = CONVERT(VARCHAR(8), GETDATE(), 112),
							AMT_SALE = AMT_SALE + @AMT_SALE
			 WHERE CD_MEMBER	=@CD_MEMBER
		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

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

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE SAVE_SL_SILSA
	
 	@CD_STORE		VARCHAR(4),		--점포코드
	@YM_SILSA	 	VARCHAR(8),		--실사월
	@CD_MENU		VARCHAR(14),
	@QTY_STOCK		INT,
	@QTY_SILSA		INT,
	@QTY_SETTLE	INT,
	@REMARK		VARCHAR(100),
	@CD_SAWON_CHG	VARCHAR(6),
	@WORK_KIND		VARCHAR(1)		--(I:INSERT, U:UPDATE, D:DELETE)

AS
BEGIN
	DECLARE	@CNT		INT

	SELECT @CNT = COUNT(*) 
	  FROM SL_SILSA
	WHERE CD_STORE	=@CD_STORE
	      AND YM_SILSA	=@YM_SILSA
	      AND CD_MENU	=@CD_MENU

	SET @CNT = ISNULL(@CNT, 0)
	IF @CNT = 0  SET @WORK_KIND = 'I'

	IF @WORK_KIND = 'I'
	BEGIN
		INSERT INTO SL_SILSA(CD_STORE, YM_SILSA, CD_MENU, QTY_STOCK, QTY_SILSA, QTY_SETTLE, REMARK, CD_SAWON_CHG)
				VALUES(@CD_STORE, @YM_SILSA, @CD_MENU, @QTY_STOCK, @QTY_SILSA, @QTY_SETTLE, @REMARK, @CD_SAWON_CHG)
	END
	ELSE IF @WORK_KIND = 'U'
	BEGIN
		UPDATE SL_SILSA SET	QTY_STOCK	=@QTY_STOCK,
						QTY_SILSA	=@QTY_SILSA,
						QTY_SETTLE	=@QTY_SETTLE,
						REMARK	=@REMARK
		WHERE CD_STORE	=@CD_STORE
		      AND YM_SILSA	=@YM_SILSA
		      AND CD_MENU	=@CD_MENU
	END
	ELSE IF @WORK_KIND = 'D'
	BEGIN
		DELETE SL_SILSA
		WHERE CD_STORE	=@CD_STORE
		      AND YM_SILSA	=@YM_SILSA
		      AND CD_MENU	=@CD_MENU
	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].[POS_SELECT_MEMBER]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[POS_SELECT_MEMBER]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


--회원조회
CREATE PROCEDURE POS_SELECT_MEMBER
	@CD_STORE		VARCHAR(4),
	@CD_MEMBER		VARCHAR(10),
	@DS_STATUS		VARCHAR(1) OUTPUT,		
	@YMD_BEGIN		VARCHAR(10) OUTPUT,
	@YMD_END		VARCHAR(10) OUTPUT,
	@NM_GOODS		VARCHAR(50) OUTPUT,
	@CD_MENU		VARCHAR(14) OUTPUT,
	@CD_MENU2		VARCHAR(14) OUTPUT,
	@NM_MENU		VARCHAR(50) OUTPUT,
	@LOCKER		VARCHAR(50) OUTPUT,
	@ALLOW_TOTCNT	INT	OUTPUT,
	@ALLOW_DAYCNT	INT	OUTPUT,
	@USE_TOT		INT	OUTPUT,
	@DELAY_RANGE	VARCHAR(30) OUTPUT	--연기기간
AS
BEGIN
	DECLARE	@NO_RCP	VARCHAR(4)
	DECLARE	@TODATE	DATETIME
	DECLARE	@YMD_SALE	VARCHAR(8)

	SET @TODATE = convert(VARCHAR(8), GETDATE(), 112) 

	IF LEN(@CD_MEMBER) <> 10
	BEGIN
		SELECT @CD_MEMBER  = CD_MEMBER FROM MS_MEMBER_CARD WHERE DS_STATUS = '0' AND NO_CARD = @CD_MEMBER
	END


	SELECT A.CD_MEMBER,
		 A.NM_MEMBER,
                            A.DS_MEMBER AS CD_CLASS,
		 B.NM_CODE1	AS DS_MEMBER,
		 B.NM_CODE2	AS DC_RATE,
		 A.TEL_MOBIL,
		 ISNULL( (SELECT TOP 1 NO_CARD FROM MS_MEMBER_CARD WHERE CD_MEMBER = A.CD_MEMBER AND DS_STATUS = '0' ), '') AS NO_CARD,
		 A.YMD_VISIT,
		 A.CNT_VISIT,
		 A.YN_TRUST,
		 (SELECT ISNULL(PNT_TOTAL,0) FROM SL_POINT WHERE CD_MEMBER = A.CD_MEMBER ) AS PNT_TOTAL,
		 (SELECT COUNT(*) FROM SL_MEMBER_USE WHERE CD_MEMBER = A.CD_MEMBER AND  CONVERT(VARCHAR(8), DT_SALE, 112)  = CONVERT(VARCHAR(8), GETDATE(), 112)  )  AS USE_DAY,
		CASE A.DS_SEX WHEN '0' THEN '남자' WHEN '1' THEN '여자' END AS GENDER,
	             (SELECT TOP 1 NEWS_TXT FROM SL_MEMBER_NEWS WHERE CD_MEMBER =@CD_MEMBER AND DS_STATUS = '0' ORDER BY SEQ DESC) AS NEWS_TXT
	FROM MS_MEMBER A, MS_CODE B
          WHERE  A.DS_MEMBER	*= B.CD_CODE
	   AND  B.CD_KIND	= '05'
	   AND  B.CD_STORE	=@CD_STORE
	   AND  A.CD_MEMBER	=@CD_MEMBER

	SELECT  @YMD_SALE		=A.YMD_SALE,
		  @YMD_BEGIN	=A.YMD_BEGIN, 
		  @YMD_END		=A.YMD_END, 
		  @NM_GOODS		=B.NM_GOODS, 
		  @CD_MENU		=C.CD_MENU, 
		  @CD_MENU2		=B.CD_MENU2, 
		  @NM_MENU		=C.NM_MENU,
		  @ALLOW_TOTCNT	=B.ALLOW_TOTCNT,
		  @ALLOW_DAYCNT	=B.ALLOW_DAYCNT,
		  @USE_TOT		=(SELECT COUNT(*) FROM SL_MEMBER_USE WHERE CD_MEMBER = A.CD_MEMBER AND  CONVERT(VARCHAR(8), DT_SALE, 112)  BETWEEN A.YMD_BEGIN AND A.YMD_END ) ,
		  @DELAY_RANGE	 = DBO.STOD(A.DELAY_BEGIN) +' ~ ' + DBO.STOD(A.DELAY_TO),
		  @DS_STATUS	=A.DS_STATUS,
		  @NO_RCP		=A.NO_RCP
	   FROM SL_MEMBER_JOIN A, MS_GOODS B, MS_MENU C
	WHERE A.CD_GOODS		= B.CD_GOODS
	      AND B.CD_MENU		= C.CD_MENU
--	      AND A.DS_STATUS		= '0'
	      AND A.CD_MEMBER	=@CD_MEMBER
	      AND A.YMD_BEGIN <= @TODATE 
                   AND A.YMD_FINISH >= @TODATE
--	      AND NOT ( (ISNULL(A.DELAY_BEGIN,@TODATE) <= @TODATE) AND (ISNULL(A.DELAY_TO,@TODATE) <= @TODATE)) 

	IF LEN(@YMD_BEGIN) <> 8
	BEGIN
		
		SELECT @NO_RCP = MAX(NO_RCP) FROM SL_MEMBER_JOIN WHERE CD_MEMBER	= @CD_MEMBER	

		IF @NO_RCP = NULL
		BEGIN
			SET @DS_STATUS	= '5'
		END
		ELSE
		BEGIN
			SET	@DS_STATUS = '4'
			
			SELECT  @YMD_BEGIN	=A.YMD_BEGIN, 
				  @YMD_END		=A.YMD_END, 
				  @NM_GOODS		=B.NM_GOODS, 
				  @CD_MENU		=C.CD_MENU, 
				  @CD_MENU2		=B.CD_MENU2, 
				  @NM_MENU		=C.NM_MENU,
				  @ALLOW_TOTCNT	=B.ALLOW_TOTCNT,
				  @ALLOW_DAYCNT	=B.ALLOW_DAYCNT,
				  @USE_TOT		=(SELECT COUNT(*) FROM SL_MEMBER_USE WHERE CD_MEMBER = A.CD_MEMBER AND  CONVERT(VARCHAR(8), DT_SALE, 112)  BETWEEN A.YMD_BEGIN AND A.YMD_END ) 
			   FROM SL_MEMBER_JOIN A, MS_GOODS B, MS_MENU C
			WHERE A.CD_GOODS		= B.CD_GOODS
			      AND B.CD_MENU		= C.CD_MENU
			      AND A.CD_MEMBER	=@CD_MEMBER
			      AND A.NO_RCP		=@NO_RCP
		END
	
	END
	ELSE
	BEGIN
		--환불상태이면 환불일자를 가져온다
		IF @DS_STATUS = '2'
		BEGIN
			SELECT @DELAY_RANGE	= DBO.STOD(YMD_REFUND) FROM SL_MEMBER_REFUND WHERE YMD_SALE =@YMD_SALE AND NO_RCP = @NO_RCP
		END
	END


	SELECT @LOCKER =  DBO.GETCOMMONNAME(@CD_STORE, '03', A.CD_PLACE) +'-'+ A.NO_LOCKER + ' ( '+DBO.GETCOMMONNAME(@CD_STORE, '04', B.CD_LOCKER) +' )' 
	   FROM SL_LOCKER A, MS_LOCKER B
	WHERE A.CD_PLACE	= B.CD_PLACE
	      AND A.NO_LOCKER	= B.NO_LOCKER
	      AND A.CD_MEMBER = @CD_STORE


	SET @DS_STATUS 	= ISNULL(@DS_STATUS,'')
	SET @YMD_BEGIN 	= ISNULL(@YMD_BEGIN,'')
	SET @YMD_END 	= ISNULL(@YMD_END,'')
	SET @NM_GOODS 	= ISNULL(@NM_GOODS,'')
	SET @CD_MENU 	= ISNULL(@CD_MENU,'')
	SET @CD_MENU2 	= ISNULL(@CD_MENU2,'')
	SET @NM_MENU 	= ISNULL(@NM_MENU,'')
	SET @LOCKER 	= ISNULL(@LOCKER,'')
	SET @USE_TOT	= ISNULL(@USE_TOT,0)
	SET @ALLOW_TOTCNT	= ISNULL(@ALLOW_TOTCNT,0)
	SET @ALLOW_DAYCNT	= ISNULL(@ALLOW_DAYCNT,0)
	SET @DELAY_RANGE 	= ISNULL(@DELAY_RANGE,'')

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_STOCK_CLOSE]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[SAVE_STOCK_CLOSE]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE SAVE_STOCK_CLOSE
	@CD_STORE 		VARCHAR(4),		--점포코드
	@NEXT_CLOSE	VARCHAR(6), 		--마감다음월
	@CURR_CLOSE	VARCHAR(6)		--마감월

AS
BEGIN
	--임시테이블에 월 기초재고를 옮겨놓는다
	delete sl_stock_tmp 

	insert into sl_stock_tmp (cd_store, ym_stock, cd_menu, qty_base)
	  select cd_store, ym_stock, cd_menu, qty_base from sl_stock_month where cd_store =@cd_store and ym_stock =@curr_close


	delete sl_stock_month where cd_store =@cd_store and ym_stock = @curr_close

	insert into sl_stock_month ( cd_store, ym_stock, yn_close, cd_menu, qty_base, qty_in, qty_out, qty_silsa, qty_adj)
	select  a.cd_store, @curr_close, 'Y', a.cd_menu, isnull(e.qty_base,0), isnull(b.qty_buy,0), isnull(c.qty_sale,0), d.qty_silsa, isnull(d.qty_settle,0)
	   from ms_menu a,
	          (select cd_store, cd_goods as cd_menu, sum(isnull(qty_buy,0)) as qty_buy
	              from sl_buy_d
	           where substring(ymd_buy,1,6) = @curr_close
	            group by cd_store, cd_goods
	          ) b,
	          (select cd_store, cd_menu, sum(isnull(qty_sale,0)) as qty_sale
	              from sl_sale_s
	           where substring(ymd_sale,1,6) = @curr_close
	            group by cd_store, cd_menu
	          ) c,
	          (select cd_store, cd_menu, qty_silsa, isnull(qty_settle,0) qty_settle
	              from sl_silsa
	           where ym_silsa = @curr_close
	          ) d,
	          sl_stock_tmp e
	where a.cd_store *= b.cd_store
	    and a.cd_menu *= b.cd_menu
	    and a.cd_store *= c.cd_store
	    and a.cd_menu *= c.cd_menu
	    and a.cd_store *= c.cd_store
	    and a.cd_menu *= d.cd_menu
                 and a.cd_menu *= e.cd_menu
	    and a.ds_stock  = '1'
                       and a.yn_use    = 'Y'
	    and a.cd_store  =@cd_store


	--다음월의 기초재고를 입력한다
	delete sl_stock_month where cd_store =@cd_store and ym_stock = @next_close

	insert into sl_stock_month ( cd_store, ym_stock, cd_menu, qty_base)
	select  a.cd_store, @next_close,  a.cd_menu, b.qty_stock
	   from ms_menu a,
	          sl_stock_month b
	where a.cd_store *= b.cd_store
	    and a.cd_menu *= b.cd_menu
	    and b.ym_stock = @curr_close
	    and a.ds_stock  = '1'
	    and a.cd_store  =@cd_store



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_JOIN]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[SAVE_MEMBER_JOIN]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO





CREATE  PROCEDURE SAVE_MEMBER_JOIN

	@YMD_SALE			VARCHAR(8),
	@NO_RCP			VARCHAR(4),
	@CD_MEMBER			VARCHAR(10),
	@CD_GOODS      		VARCHAR(4),
	@YMD_BEGIN			VARCHAR(8),
	@YMD_END			VARCHAR(8),
	@AMT_GOODS			INT,
	@AMT_DC			INT,
	@AMT_SALE			INT,
	@DS_SALE			VARCHAR(1),
	@CD_SAWON_CHG		VARCHAR(6),
	@REMARK			VARCHAR(100),
	@I_SW           			CHAR(1)
AS
BEGIN
	DECLARE	@SEQ	INT


	IF @I_SW = 0
	BEGIN
		SELECT @NO_RCP = dbo.LPAD(ISNULL(CONVERT(INT, MAX(NO_RCP)),0)+1,4,'0')
		   FROM SL_MEMBER_JOIN
		WHERE    YMD_SALE 		= @YMD_SALE
	
		INSERT INTO  SL_MEMBER_JOIN (	
							YMD_SALE,
							NO_RCP,
							CD_MEMBER,
							CD_GOODS,
							YMD_BEGIN,
							YMD_END,
							YMD_FINISH,
							AMT_GOODS,
							AMT_DC,
							AMT_SALE,
							DS_SALE,
							CD_SAWON_CHG
						)
	
	               		 	VALUES(	
							@YMD_SALE,
							@NO_RCP,
							@CD_MEMBER,
							@CD_GOODS,
							@YMD_BEGIN,
							@YMD_END,
							@YMD_END,
							@AMT_GOODS,
							@AMT_DC,
							@AMT_SALE,
							@DS_SALE,
							@CD_SAWON_CHG
						)		

		SELECT @SEQ = ISNULL(MAX(SEQ),0) + 1 FROM SL_MEMBER_JOIN_LOG WHERE YMD_SALE = @YMD_SALE AND NO_RCP = @NO_RCP
	
		INSERT INTO  SL_MEMBER_JOIN_LOG (	
							YMD_SALE,
							NO_RCP,
							SEQ,
							CD_MEMBER,
							CD_GOODS,
							YMD_BEGIN,
							YMD_END,
							AMT_GOODS,
							AMT_DC,
							AMT_SALE,
							DS_SALE,
							CD_SAWON_CHG,
							REMARK
						)
	
	               		 	VALUES(	
							@YMD_SALE,
							@NO_RCP,
							@SEQ,
							@CD_MEMBER,
							@CD_GOODS,
							@YMD_BEGIN,
							@YMD_END,
							@AMT_GOODS,
							@AMT_DC,
							@AMT_SALE,
							@DS_SALE,
							@CD_SAWON_CHG,
							@REMARK
						)	


	END
	ELSE 
	BEGIN
		DECLARE	@DAYCNT	INT
		DECLARE	@DATE_ORG	DATETIME
		DECLARE	@FINISH_ORG	DATETIME
		DECLARE	@YMD_FINISH	VARCHAR(8)

		SELECT @DATE_ORG = CONVERT(DATETIME, DBO.STOD(YMD_END)+' 00:00:00') ,
			 @FINISH_ORG = CONVERT(DATETIME, DBO.STOD(YMD_FINISH)+' 00:00:00') 
		   FROM SL_MEMBER_JOIN
		WHERE YMD_SALE 		= @YMD_SALE
		      AND  NO_RCP		= @NO_RCP

		SET	@DAYCNT = DATEDIFF(DAY, @DATE_ORG, @YMD_END)

		SET @YMD_FINISH = CONVERT(VARCHAR(8), DATEADD(D, @DAYCNT, @FINISH_ORG), 112)

		UPDATE  SL_MEMBER_JOIN     SET 
						CD_GOODS	=@CD_GOODS,
						AMT_GOODS	=@AMT_GOODS,
						AMT_DC	=@AMT_DC,
						AMT_SALE	=@AMT_SALE,
						DS_SALE	=@DS_SALE,
						YMD_BEGIN	=@YMD_BEGIN,
						YMD_END	=@YMD_END,
						YMD_FINISH	=@YMD_FINISH,
						CD_SAWON_CHG	=@CD_SAWON_CHG,
						DT_CHANGE          	=GETDATE()
				WHERE    YMD_SALE 		= @YMD_SALE
				      AND    NO_RCP		= @NO_RCP
	
	
		SELECT @SEQ = ISNULL(MAX(SEQ),0) + 1 FROM SL_MEMBER_JOIN_LOG WHERE YMD_SALE = @YMD_SALE AND NO_RCP = @NO_RCP
	
		INSERT INTO  SL_MEMBER_JOIN_LOG (	
							YMD_SALE,
							NO_RCP,
							SEQ,
							CD_MEMBER,
							CD_GOODS,
							YMD_BEGIN,
							YMD_END,
							AMT_GOODS,
							AMT_DC,
							AMT_SALE,
							DS_SALE,
							CD_SAWON_CHG,
							REMARK
						)
	
	               		 	VALUES(	
							@YMD_SALE,
							@NO_RCP,
							@SEQ,
							@CD_MEMBER,
							@CD_GOODS,
							@YMD_BEGIN,
							@YMD_END,
							@AMT_GOODS,
							@AMT_DC,
							@AMT_SALE,
							@DS_SALE,
							@CD_SAWON_CHG,
							@REMARK
						)	
	END
    

	
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

