20100312
20100325
if not exists(select * from information_schema.columns where TABLE_NAME='ms_store' and COLUMN_NAME='ticket_end6')
BEGIN
ALTER TABLE ms_store
ADD ticket_end6 varchar(42)
END
GO

if not exists(select * from information_schema.columns where TABLE_NAME='ms_store' and COLUMN_NAME='ticket_end7')
BEGIN
ALTER TABLE ms_store
ADD ticket_end7 varchar(42)
END
GO

if not exists(select * from information_schema.columns where TABLE_NAME='ms_store' and COLUMN_NAME='ticket_end8')
BEGIN
ALTER TABLE ms_store
ADD ticket_end8 varchar(42)
END
GO

if not exists(select * from information_schema.columns where TABLE_NAME='ms_store' and COLUMN_NAME='ticket_end9')
BEGIN
ALTER TABLE ms_store
ADD ticket_end9 varchar(42)
END
GO

if not exists(select * from information_schema.columns where TABLE_NAME='ms_store' and COLUMN_NAME='ticket_end10')
BEGIN
ALTER TABLE ms_store
ADD ticket_end10 varchar(42)
END
GO

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

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO



CREATE   PROCEDURE SAVE_MS_STORE
	@CD_STORE 		VARCHAR(4),
	@CD_SAWON_CHG 	VARCHAR(6),
	@I_SW    	  	CHAR(1),
	@NM_STORE  	 	VARCHAR(30),
	@NM_BOSS   	 	VARCHAR(10),
	@NO_BIZER  	 	VARCHAR(12),
	@NM_UPTAE  	 	VARCHAR(30),
	@NM_UPJONG 	VARCHAR(30),
	@TEL_OFFICE 	 	VARCHAR(13),
	@TEL_MOBIL1		VARCHAR(13),
	@TEL_MOBIL2		VARCHAR(13),
	@TEL_FAX   		VARCHAR(13),
	@NO_POST  	 	VARCHAR(6),
	@ADDR1     	 	VARCHAR(40),
	@ADDR2    	 	VARCHAR(40),
	@PNT_MIN_USE 	INT,
	@RCP_TITLE1	 	VARCHAR(42),
	@RCP_TITLE2	 	VARCHAR(42),
	@RCP_TITLE3 	 	VARCHAR(42),
	@RCP_TITLE4	 	VARCHAR(42),
	@CARD_TITLE1	VARCHAR(42),
	@CARD_TITLE2	VARCHAR(42),
	@CARD_TITLE3 	VARCHAR(42),
	@CARD_TITLE4	VARCHAR(42),
	@RCP_END1 		VARCHAR(42),
	@RCP_END2 		VARCHAR(42),
	@RCP_END3 		VARCHAR(42),
	@TICKET_END1	VARCHAR(42),
	@TICKET_END2	VARCHAR(42),
	@TICKET_END3	VARCHAR(42),
	@TICKET_END4	VARCHAR(42),
	@TICKET_END5	VARCHAR(42),
	@TICKET_END6	VARCHAR(42),
	@TICKET_END7	VARCHAR(42),
	@TICKET_END8	VARCHAR(42),
	@TICKET_END9	VARCHAR(42),
	@TICKET_END10	VARCHAR(42),
	@CDP_ACCOUNT 	VARCHAR(40),
	@CDP_READY  	VARCHAR(40),
	@CONF        	 	VARCHAR(200),
	@USE_MENU		VARCHAR(50),
	@REMARK    		VARCHAR(100),
	@DC_UNIT		INT,
	@LEN_CARD		INT,
	@PREFIX		VARCHAR(20),
	@NO_POS		VARCHAR(2),
	@LEN_MENU		INT,
	@CASHAMT_STD	INT,
	@CASH_POINT		INT,
	@CARDAMT_STD	INT,
	@CARD_POINT	INT,
	@CASHRCPAMT_STD	INT,
	@CASHRCP_POINT	INT,
	@POINT_MIN		INT,
	@RT_ONEVAN		INT,
	@AMT_LIMIT		INT,
	@CUST_TITLE		VARCHAR(30),
	@RPT_PWD		VARCHAR(20),
	@SIMPLE_RCP		VARCHAR(50),
	@MIN_AFTER		INT,
	@MIN_EACH		INT,
	@AMT_ADD		INT,
	@ADD_MENU		VARCHAR(14),
	@CLSTEP1		INT,
	@CLSTEP2		INT,
	@CLSTEP3		INT,
	@CLCOLOR1		VARCHAR(30),
	@CLCOLOR2		VARCHAR(30),
	@CLCOLOR3		VARCHAR(30),
	@LOCK_X		INT,
	@LOCK_Y		INT,
	@LOCKPOS_X		INT,
	@LOCKPOS_Y		INT,
	@LOCKPOS_F		INT,
	@CLIPBOARD		VARCHAR(30),
	@TICKET_TITLE	VARCHAR(20),
	@LEN_COUPON	INT,
	@DAYTIME_BEGIN	VARCHAR(5),
	@DAYTIME_END	VARCHAR(5),
	@NIGHTTIME_BEGIN	VARCHAR(5),
	@NIGHTTIME_END	VARCHAR(5),
	@BACKUP_DIR		VARCHAR(100),
	@AMT_DEFREADY	INT,
	@CARD_FEE		NUMERIC(3,1),
	@POSJOINMENU	VARCHAR(13),
	@POSFILLMENU	VARCHAR(13),
	@POSFILLCODE	VARCHAR(3),
	@POINTMEMBER	VARCHAR(3),
	@POSUSEMENU1	VARCHAR(4),
	@POSUSEMENU2	VARCHAR(4)


AS
BEGIN
	UPDATE MS_STORE SET 	NM_STORE		=@NM_STORE,
					NM_BOSS		=@NM_BOSS,
					NO_BIZER		=@NO_BIZER,
					NM_UPTAE		=@NM_UPTAE,
					NM_UPJONG 		=@NM_UPJONG,
			                  	TEL_OFFICE		=@TEL_OFFICE,
					TEL_MOBIL1		=@TEL_MOBIL1,
					TEL_MOBIL2		=@TEL_MOBIL2,
					TEL_FAX		=@TEL_FAX,
					NO_POST		=@NO_POST,
					ADDR1			=@ADDR1,
					ADDR2			=@ADDR2,
					PNT_MIN_USE		=@PNT_MIN_USE,
					RCP_TITLE1		=@RCP_TITLE1,
					RCP_TITLE2		=@RCP_TITLE2,
					RCP_TITLE3		=@RCP_TITLE3,
					RCP_TITLE4		=@RCP_TITLE4,
					CARD_TITLE1		=@CARD_TITLE1,
					CARD_TITLE2		=@CARD_TITLE2,
					CARD_TITLE3		=@CARD_TITLE3,
					CARD_TITLE4		=@CARD_TITLE4,
					RCP_END1		=@RCP_END1,
					RCP_END2		=@RCP_END2,
			                  	RCP_END3		=@RCP_END3,
					TICKET_END1		=@TICKET_END1,
					TICKET_END2		=@TICKET_END2,
			                  	TICKET_END3		=@TICKET_END3,
			                  	TICKET_END4		=@TICKET_END4,
			                  	TICKET_END5		=@TICKET_END5,
			                  	TICKET_END6		=@TICKET_END6,
			                  	TICKET_END7		=@TICKET_END7,
			                  	TICKET_END8		=@TICKET_END8,
			                  	TICKET_END9		=@TICKET_END9,
			                  	TICKET_END10		=@TICKET_END10,
					CDP_ACCOUNT	=@CDP_ACCOUNT,
					CDP_READY		=@CDP_READY,
					CONF 			=@CONF,
					USE_MENU		=@USE_MENU,
					REMARK 		=@REMARK,
					DC_UNIT		=@DC_UNIT,
					LEN_CARD		=@LEN_CARD,
					PREFIX			=@PREFIX,
					NO_POS		=@NO_POS,
					LEN_MENU		=@LEN_MENU,
					CASHAMT_STD	=@CASHAMT_STD,
					CASH_POINT		=@CASH_POINT,
					CARDAMT_STD	=@CARDAMT_STD,
					CARD_POINT		=@CARD_POINT,
					CASHRCPAMT_STD	=@CASHRCPAMT_STD,
					CASHRCP_POINT	=@CASHRCP_POINT,
					POINT_MIN		=@POINT_MIN,
					RT_ONEVAN		=@RT_ONEVAN,
					CUST_TITLE		=@CUST_TITLE,
					POSRPT_PWD		=@RPT_PWD,
					SIMPLE_RCP		=@SIMPLE_RCP,
					AMT_LIMIT		=@AMT_LIMIT,
					MIN_AFTER		=@MIN_AFTER,
					MIN_EACH		=@MIN_EACH,
					AMT_ADD		=@AMT_ADD,
					ADD_MENU		=@ADD_MENU,
					CLSTEP1		=@CLSTEP1,
					CLSTEP2		=@CLSTEP2,
					CLSTEP3		=@CLSTEP3,
					CLCOLOR1		=@CLCOLOR1,
					CLCOLOR2		=@CLCOLOR2,
					CLCOLOR3		=@CLCOLOR3,
					LOCK_X		=@LOCK_X,
					LOCK_Y		=@LOCK_Y,
					LOCKPOS_X		=@LOCKPOS_X,
					LOCKPOS_Y		=@LOCKPOS_Y,
					LOCKPOS_F		=@LOCKPOS_F,
					CLIPBOARD		=@CLIPBOARD,
					TICKET_TITLE		=@TICKET_TITLE,
					LEN_COUPON		=@LEN_COUPON,
					DAYTIME_BEGIN	=@DAYTIME_BEGIN,
					DAYTIME_END		=@DAYTIME_END,
					NIGHTTIME_BEGIN	=@NIGHTTIME_BEGIN,
					NIGHTTIME_END	=@NIGHTTIME_END,
					BACKUP_DIR		=@BACKUP_DIR,
					AMT_DEFREADY	=@AMT_DEFREADY,
					CARD_FEE		=@CARD_FEE,
					POSJOINMENU		=@POSJOINMENU,
					POSFILLMENU		=@POSFILLMENU,
					POSFILLCODE		=@POSFILLCODE,
					POINTMEMBER	=@POINTMEMBER,
					POSUSEMENU1	=@POSUSEMENU1,
					POSUSEMENU2	=@POSUSEMENU2,
					DT_CHANGE		=GETDATE()
	                      WHERE 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].[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,
	@AMT_OKC			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_OKC_OCCUR		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 MS_MEMBER SET 	AMT_SALE = AMT_SALE - ABS(@AMT_SALE)
		 WHERE CD_MEMBER	=@CD_MEMBER

		SELECT @PNT_TOTAL = PNT_TOTAL
		   FROM SL_POINT
		 WHERE CD_MEMBER	=@CD_MEMBER
		
	END
	ELSE
	BEGIN
		IF (@PNT_USE = 0) 
			SET @PNT_USE = @AMT_POINT
	END

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

		DECLARE @CASHAMT		INT
		DECLARE @CARDAMT		INT
		DECLARE @CASHRCPAMT	INT
		DECLARE @PNT_CASH	INT
		DECLARE @PNT_CARD	INT
		DECLARE @PNT_CASHRCP	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')),
				  @CASHRCPAMT_STD = CONVERT(INT, ISNULL(A.NM_CODE9,'0')),
				  @CASHRCPPNT_STD = CONVERT(INT, ISNULL(A.NM_CODE10,'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 @CASHRCPAMT_STD 	= ISNULL(@CASHRCPAMT_STD,0)

			SET @CASHPNT_STD  	= ISNULL(@CASHPNT_STD,0)
			SET @CARDPNT_STD 		= ISNULL(@CARDPNT_STD,0)
			SET @CASHRCPPNT_STD 	= ISNULL(@CASHRCPPNT_STD,0)
		END

		
		IF (@CASHAMT_STD = 0) AND (@CARDAMT_STD = 0) AND (@CASHRCPAMT_STD = 0)
		BEGIN
			SELECT @CASHAMT_STD = CASHAMT_STD,
				  @CASHPNT_STD = CASH_POINT,
				  @CARDAMT_STD = CARDAMT_STD,
				  @CARDPNT_STD = CARD_POINT,
				  @CASHRCPAMT_STD = CASHRCPAMT_STD,
				  @CASHRCPPNT_STD = CASHRCP_POINT
			    FROM MS_STORE
			 WHERE CD_STORE =@CD_STORE

		END

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

		SET @CASHAMT 	=ABS(@CASHAMT)
		SET @CARDAMT 	=ABS(@CARDAMT)
		SET @CASHRCPAMT 	=ABS(@CASHRCPAMT)
		SET @AMT_EXISTPOINT = ABS(@AMT_EXISTPOINT)


		DECLARE	@AMT_APPLY	INT
		DECLARE	@KIND		INT	--0:현금, 1:현금영수증, 2:신용카드
		SET 		@AMT_APPLY = 0
		SET 		@KIND = 0

		IF ((@CASHAMT = 0) AND (@CARDAMT = 0) AND (@CASHRCPAMT = 0) ) OR (@CASHAMT+@CARDAMT+@CASHRCPAMT <= @AMT_EXISTPOINT)
		BEGIN
			SELECT @PNT_TOTAL = ISNULL(PNT_TOTAL,0) FROM SL_POINT WHERE CD_MEMBER = @CD_MEMBER		
			IF @PNT_USE = 0 	
				GOTO PASS_1
			ELSE
				GOTO PASS
		END
	
		WHILE @AMT_APPLY< @AMT_EXISTPOINT
		BEGIN
			IF @KIND = 0 
			BEGIN
				IF @CASHAMT >= (@AMT_EXISTPOINT-@AMT_APPLY)
				BEGIN
					SET @CASHAMT 	= @CASHAMT - @AMT_EXISTPOINT
					SET @AMT_APPLY 	= @AMT_EXISTPOINT
				END
				ELSE IF @CASHAMT > 0
				BEGIN
					SET @AMT_APPLY 	=@CASHAMT
					SET @CASHAMT 	=@CASHAMT - (@AMT_EXISTPOINT -  (@AMT_EXISTPOINT- @CASHAMT))
				END
				SET 		@KIND = 1		
			END
			ELSE IF @KIND = 1
			BEGIN
				IF @CASHRCPAMT >= (@AMT_EXISTPOINT-@AMT_APPLY)
				BEGIN
					SET @CASHRCPAMT 	= @CASHRCPAMT - (@AMT_EXISTPOINT-@AMT_APPLY)
					SET @AMT_APPLY 	= @AMT_APPLY + (@AMT_EXISTPOINT-@AMT_APPLY)
				END
				ELSE IF @CASHRCPAMT > 0
				BEGIN
					SET @AMT_APPLY 	=@AMT_APPLY + @CASHRCPAMT
					IF (@AMT_EXISTPOINT>= @AMT_APPLY) 
						SET @CASHRCPAMT 	= 0
					ELSE
						SET @CASHRCPAMT =@AMT_EXISTPOINT-@AMT_APPLY
				END
				SET 		@KIND = 2		
			END
			ELSE IF @KIND = 2
			BEGIN
				IF @CARDAMT >= (@AMT_EXISTPOINT-@AMT_APPLY)
				BEGIN
					SET @CARDAMT 	= @CARDAMT - (@AMT_EXISTPOINT-@AMT_APPLY)
					SET @AMT_APPLY 	= @AMT_APPLY + @AMT_EXISTPOINT-@AMT_APPLY
				END
				ELSE IF @CARDAMT > 0
				BEGIN
					SET @AMT_APPLY 	=@AMT_APPLY + @CARDAMT
					IF (@AMT_EXISTPOINT>= @AMT_APPLY) 
						SET @CARDAMT 	= 0
					ELSE
						SET @CARDAMT 	=@AMT_EXISTPOINT-@AMT_APPLY
				END

			END

		END

PASS:

		--반품일때는 포인트 제외상품금액에 더한다
		IF @WORK_KIND = 'B'
		BEGIN
			SET @CASHAMT 	=@CASHAMT		*-1
			SET @CARDAMT 	=@CARDAMT		*-1
			SET @CASHRCPAMT 	=@CASHRCPAMT	*-1
			SET @AMT_EXISTPOINT =@AMT_EXISTPOINT *-1
		END


		--현금 포인트
		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 / @CARDAMT_STD * @CARDPNT_STD
		ELSE
			SET	@PNT_CARD = 0

		--현금영수증 포인트
		IF @CASHRCPPNT_STD > 0
			SET	@PNT_CASHRCP = @CASHRCPAMT / @CASHRCPAMT_STD * @CASHRCPPNT_STD
		ELSE
			SET	@PNT_CASHRCP = 0

		SET 	@PNT_OCCUR = @PNT_CASH + @PNT_CARD + @PNT_CASHRCP

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

		SELECT @PNT_TOTAL = ISNULL(PNT_TOTAL,0) FROM SL_POINT WHERE CD_MEMBER = @CD_MEMBER
	
		SET @PNT_TOTAL	= ISNULL(@PNT_TOTAL, 0)
		SET @PNT_OCCUR 	= ISNULL(@PNT_OCCUR, 0)
		SET @PNT_USE 	= ISNULL(@PNT_USE, 0)

		--총포인트
		SELECT @PNT_TOTAL = @PNT_OCCUR + @PNT_TOTAL - @PNT_USE
	
		SET @CASHAMT =ISNULL(@AMT_CASH,0)+ISNULL(@AMT_CHECK,0)+ISNULL(@AMT_GIFT,0) - ISNULL(@AMT_CASHRCP,0)
		SET @CARDAMT = @AMT_CARD
		SET @CASHRCPAMT = @AMT_CASHRCP

		SET @CASHAMT 	=ABS(@CASHAMT)
		SET @CARDAMT 	=ABS(@CARDAMT)
		SET @CASHRCPAMT 	=ABS(@CASHRCPAMT)
		SET @AMT_EXISTPOINT = ABS(@AMT_EXISTPOINT)	

		IF @WORK_KIND = 'B'
		BEGIN
			SET @CASHAMT 	=@CASHAMT		*-1
			SET @CARDAMT 	=@CARDAMT		*-1
			SET @CASHRCPAMT 	=@CASHRCPAMT	*-1
			SET @AMT_EXISTPOINT =@AMT_EXISTPOINT *-1
		END
	
		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,
							AMT_CASHRCP,
							PNT_CASH,
							PNT_CARD,

							PNT_CASHRCP,
							AMT_EXIST,
							LINK
							)
					VALUES(
							@CD_MEMBER,
							@YMD_SALE,
							@PNT_SEQ,
							'000',
							@AMT_CASH,
							@AMT_CARD,
							@AMT_CASHRCP,
							@PNT_CASH,
							@PNT_CARD,
							@PNT_CASHRCP,
							@AMT_EXISTPOINT,
							@YMD_SALE+@NO_POS+@NO_RCP	
						)
		END
		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


	END

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

PASS_1:

	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,
						AMT_OKC,
						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_OKC_OCCUR,
						PNT_OCCUR,
						PNT_USE,
						AMT_EXISTPOINT,
						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,
						@AMT_OKC,
						@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_OKC_OCCUR,
						@PNT_OCCUR,
						@PNT_USE,
						@AMT_EXISTPOINT,
						'',
						@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


		--쿠폰사용내역 환원
		UPDATE MS_COUPON SET CNT_USE = CNT_USE - T1.QTY_SALE
		  FROM SL_SALE_D T1
		 WHERE MS_COUPON.CD_STORE	= T1.CD_STORE
		   AND MS_COUPON.NO_COUPON+MS_COUPON.NO_SEQ	= T1.NO_COUPON
		   AND T1.CD_STORE			= @CD_STORE
		   AND T1.YMD_SALE			= @YMD_SALE
		   AND T1.NO_POS			= @NO_POS
		   AND T1.NO_RCP			= @NO_RCP
		   AND T1.DS_SALE			= 'C'

		--반품일때
		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
						PNT_OKC_OCCUR	=@PNT_OKC_OCCUR,
						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_OKC		=@AMT_OKC,
						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_OKC_OCCUR	=@PNT_OKC_OCCUR,
						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

		--쿠폰사용내역 환원
		UPDATE MS_COUPON SET CNT_USE = CNT_USE - T1.QTY_SALE
		  FROM SL_SALE_D T1
		 WHERE MS_COUPON.CD_STORE	= T1.CD_STORE
		   AND MS_COUPON.NO_COUPON+MS_COUPON.NO_SEQ	= T1.NO_COUPON
		   AND T1.CD_STORE			= @CD_STORE
		   AND T1.YMD_SALE			= @YMD_SALE
		   AND T1.NO_POS			= @NO_POS
		   AND T1.NO_RCP			= @NO_RCP
		   AND T1.DS_SALE			= 'C'

		DELETE SL_MEMBER_JOIN WHERE POSRCP_LINK = @YMD_SALE+@NO_POS+@NO_RCP
		DELETE SL_PNT_OCCUR WHERE LINK = @YMD_SALE+@NO_POS+@NO_RCP
	

	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
	ELSE IF @WORK_KIND = 'O'
	BEGIN
		UPDATE SL_SALE_H SET
						PNT_OKC_OCCUR = @PNT_OKC_OCCUR,
						DT_CHANGE  	=GETDATE()

		WHERE CD_STORE	=@CD_STORE
		      AND YMD_SALE	=@YMD_SALE
		      AND NO_POS	=@NO_POS
		      AND NO_RCP	=@NO_RCP		
	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

