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

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

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

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

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

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

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

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

if not exists(select * from information_schema.columns where TABLE_NAME='sl_cashier_mgm' and COLUMN_NAME='amt_banpum')
BEGIN
ALTER TABLE sl_cashier_mgm
ADD amt_banpum int
END
GO

if not exists(select * from information_schema.columns where TABLE_NAME='sl_member_join' and COLUMN_NAME='posrcp_link')
BEGIN
ALTER TABLE sl_member_join
ADD posrcp_link varchar(14)
END
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

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_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),
	@NO_POS			VARCHAR(2),
	@POSRCP_LINK		VARCHAR(14),
	@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,
							NO_POS,
							POSRCP_LINK,
							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,
							@NO_POS,
							@POSRCP_LINK,
							@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

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),
	@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,
					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
		SET @PNT_USE = @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

		--포인트를 사용한 영수증은 포인트를 적립하지 않는다
		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 = 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
		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


	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'
	

	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

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

CREATE TRIGGER [TRIGGER_MS_LOCK_DELETE] ON [dbo].[MS_LOCK]
FOR DELETE
AS
  declare
	@cd_store		varchar(4),
	@no_lock		varchar(4),
	@ds_lock		varchar(3), 
	@no_rfid			varchar(20),
	@ds_status		varchar(1),
	@dt_change		datetime
begin
  select 	@cd_store		=d.cd_store,
	@no_lock		=d.no_lock,
	@ds_lock		=d.ds_lock, 
	@no_rfid			=d.no_rfid,
	@ds_status		=d.ds_status,
	@dt_change		=d.dt_change

    from	 deleted d

	  declare  @cnt  int
	  select @cnt = count(*) from sl_order_h where cd_store =@cd_store and no_lock = @no_lock and ds_lock = @ds_lock

  	set @cnt = isnull(@cnt,0)

	if @cnt > 0
	begin
	     insert into ms_lock(cd_store, no_lock, ds_lock, no_rfid, ds_status, dt_change)
	           values(@cd_store, @no_lock, @ds_lock, @no_rfid, @ds_status, @dt_change)
	end

end
GO

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_CASHIER_MGM]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[POS_SELECT_CASHIER_MGM]
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,
	@AMT_OKC			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,
	@AMT_BANPUM		INT,
	@CNT_CUSTOMER		INT,
	@AMT_AVERAGE		INT,
	@AMT_LACK			INT,
	@AMT_CASHRCP		INT,
	@RCP_BEGIN			VARCHAR(4),
	@RCP_END			VARCHAR(4),
	@_CHECK			INT,
	@_50000			INT,
	@_10000			INT,
	@_5000			INT,
	@_1000			INT,
	@_500				INT,
	@_100				INT,
	@_50				INT,
	@_10				INT,
	@SEQ				INT OUTPUT
AS
BEGIN
	
	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		,
							AMT_OKC		,
							DC_MENU		,
							DC_CODE		,
							DC_RECEIPT		,
							DC_MEMBER		,
							DC_ENURI		,
							DC_SPC		,
							DC_COUPON		,
							DC_CUT		,
							DC_VAT		,
							AMT_VOID		,
							AMT_BANPUM		,
							CNT_CUSTOMER	,
							AMT_AVERAGE	,
							AMT_LACK		,
							AMT_CASHRCP	,
							RCP_BEGIN		,
							RCP_END		,
							_CHECK,
							_50000,
							_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		,
							 @AMT_OKC		,
							 @DC_MENU		,
							 @DC_CODE		,
							 @DC_RECEIPT	,
							 @DC_MEMBER	,
							 @DC_ENURI		,
							 @DC_SPC		,
							 @DC_COUPON	,
							 @DC_CUT		,
							 @DC_VAT		,
							 @AMT_VOID		,
							 @AMT_BANPUM	,
							 @CNT_CUSTOMER	,
							 @AMT_AVERAGE	,
							 @AMT_LACK		,
							 @AMT_CASHRCP	,
							 @RCP_BEGIN		,
							 @RCP_END		,
							 @_CHECK,
							 @_50000,
							 @_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		,
						AMT_OKC		=@AMT_OKC		,
						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		,
						AMT_BANPUM		=@AMT_BANPUM	,
						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		,
						_50000			=@_50000		,
						_10000			=@_10000		,
						_5000			=@_5000		,
						_1000			=@_1000		,
						_500			=@_500		,
						_100			=@_100		,
						_50			=@_50			,
						_10			=@_10			,
						DT_CHANGE		=GETDATE()		,
						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_CASHIER_MGM

	@CD_STORE 	    		VARCHAR(4),
	@YMD_CLOSE			VARCHAR(8),
	@NO_POS			VARCHAR(2),
	@CD_SAWON			VARCHAR(6),
	@AMT_READY			INT OUTPUT,
	@AMT_MID			INT OUTPUT,
	@AMT_ACCT_IN		INT OUTPUT,
	@AMT_ACCT_OUT		INT OUTPUT,
	@AMT_SALE			INT OUTPUT,
	@AMT_TAX			INT OUTPUT,
	@AMT_CASH			INT OUTPUT,
	@AMT_CARD			INT OUTPUT,
	@AMT_CHECK			INT OUTPUT,
	@AMT_TRUST			INT OUTPUT,
	@AMT_GIFT			INT OUTPUT,
	@AMT_POINT			INT OUTPUT,
	@AMT_LOSS			INT OUTPUT,
	@AMT_OKC			INT OUTPUT,
	@DC_MENU			INT OUTPUT,
	@DC_CODE			INT OUTPUT,
	@DC_RECEIPT			INT OUTPUT,
	@DC_MEMBER			INT OUTPUT,
	@DC_ENURI			INT OUTPUT,
	@DC_SPC			INT OUTPUT,
	@DC_VAT			INT OUTPUT,
	@DC_CUT			INT OUTPUT,
	@DC_COUPON			INT OUTPUT,
	@DC_TOTAL			INT OUTPUT,
	@AMT_VOID			INT OUTPUT,
	@AMT_BANPUM		INT OUTPUT,
	@AMT_CASHRCP		INT OUTPUT,
	@CNT_CUSTOMER		INT OUTPUT,
	@AMT_AVERAGE		INT OUTPUT,
	@AMT_PRESENT		INT OUTPUT,
	@RCP_BEGIN			VARCHAR(4) OUTPUT,
	@RCP_END			VARCHAR(4) OUTPUT
AS
BEGIN
	
	DECLARE @SEQ	INT

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

	--준비금, 중간출금
	SELECT @AMT_READY	=ISNULL(AMT_READY,0),
		 @AMT_MID		=ISNULL(AMT_MID_CASH,0) + ISNULL(AMT_MID_CHECK,0)
	   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

	--준비금, 중간출금
	SELECT @AMT_ACCT_IN	=SUM(AMT_PAYIN),
		 @AMT_ACCT_OUT	=SUM(AMT_OUT)
	   FROM SL_ACCT
	WHERE CD_STORE		=@CD_STORE	
	      AND YMD_OCCUR		=@YMD_CLOSE
	      AND CD_SAWON_CHG	=@CD_SAWON
	      AND NO_POS		=@NO_POS
	      AND NO_CLOSE		=0

	SELECT @AMT_READY	=ISNULL(@AMT_READY	,0)
	SELECT @AMT_MID		=ISNULL(@AMT_MID		,0)
	SELECT @AMT_ACCT_IN	=ISNULL(@AMT_ACCT_IN	,0)
	SELECT @AMT_ACCT_OUT	=ISNULL(@AMT_ACCT_OUT	,0)


	SELECT @AMT_TAX		=SUM(AMT_TAX),
		 @AMT_CASH		=SUM(AMT_CASH),
		 @AMT_CARD		=SUM(AMT_CARD),
		 @AMT_CHECK		=SUM(AMT_CHECK),
		 @AMT_TRUST	=SUM(AMT_TRUST),
		 @AMT_GIFT		=SUM(AMT_GIFT),
		 @AMT_POINT		=SUM(AMT_POINT),
		 @AMT_LOSS		=SUM(AMT_LOSS),
		 @AMT_OKC		=SUM(AMT_OKC),
		 @DC_MENU		=SUM(DC_MENU),
		 @DC_CODE		=SUM(DC_CODE),
		 @DC_RECEIPT	=SUM(DC_RECEIPT),
		 @DC_ENURI		=SUM(DC_ENURI),
		 @DC_MEMBER	=SUM(DC_MEMBER),
		 @DC_SPC		=SUM(DC_SPC),
		 @DC_CUT		=SUM(DC_CUT),
		 @DC_VAT		=SUM(DC_VAT),
		 @DC_COUPON	=SUM(DC_COUPON),
		 @DC_TOTAL		=SUM(DC_TOT),
		 @CNT_CUSTOMER	=COUNT(*),
		 @AMT_AVERAGE	=AVG(AMT_SALE),
		 @AMT_CASHRCP	=SUM(AMT_CASHRCP),
		 @RCP_BEGIN		=MIN(NO_RCP),
		 @RCP_END		=MAX(NO_RCP)	 
	   FROM SL_SALE_H
	WHERE CD_STORE	=@CD_STORE	
	      AND YMD_SALE	=@YMD_CLOSE
	      AND NO_POS	=@NO_POS
	      AND CD_SAWON	=@CD_SAWON
	      AND DS_SALE	<>'V'
	      AND NO_CLOSE	=0

	SELECT @AMT_VOID		=SUM(AMT_SALE)	 
	   FROM SL_SALE_H
	WHERE CD_STORE	=@CD_STORE	
	      AND YMD_SALE	=@YMD_CLOSE
	      AND NO_POS	=@NO_POS
	      AND CD_SAWON	=@CD_SAWON
	      AND DS_SALE	='V'
	      AND NO_CLOSE	=0

	SELECT @AMT_BANPUM		=SUM(AMT_SALE)	 
	   FROM SL_SALE_H
	WHERE CD_STORE	=@CD_STORE	
	      AND YMD_SALE	=@YMD_CLOSE
	      AND NO_POS	=@NO_POS
	      AND CD_SAWON	=@CD_SAWON
	      AND DS_SALE	='B'
	      AND NO_CLOSE	=0
	
	SELECT	@AMT_SALE		=ISNULL(@AMT_SALE		,0)
	SELECT	@AMT_TAX		=ISNULL(@AMT_TAX		,0)
	SELECT	@AMT_CASH		=ISNULL(@AMT_CASH		,0)
	SELECT	@AMT_CARD		=ISNULL(@AMT_CARD		,0)
	SELECT	@AMT_CHECK		=ISNULL(@AMT_CHECK	,0)
	SELECT	@AMT_TRUST		=ISNULL(@AMT_TRUST	,0)
	SELECT	@AMT_GIFT		=ISNULL(@AMT_GIFT		,0)
	SELECT	@AMT_POINT		=ISNULL(@AMT_POINT	,0)
	SELECT	@AMT_LOSS		=ISNULL(@AMT_LOSS		,0)
	SELECT	@AMT_OKC		=ISNULL(@AMT_OKC		,0)
	SELECT	@DC_MENU		=ISNULL(@DC_MENU		,0)
	SELECT	@DC_CODE		=ISNULL(@DC_CODE		,0)
	SELECT	@DC_RECEIPT		=ISNULL(@DC_RECEIPT	,0)
	SELECT	@DC_ENURI		=ISNULL(@DC_ENURI		,0)
	SELECT	@DC_MEMBER		=ISNULL(@DC_MEMBER	,0)
	SELECT	@DC_SPC		=ISNULL(@DC_SPC		,0)
	SELECT	@DC_CUT		=ISNULL(@DC_CUT		,0)
	SELECT	@DC_VAT		=ISNULL(@DC_VAT		,0)
	SELECT	@DC_COUPON		=ISNULL(@DC_COUPON	,0)
	SELECT	@DC_TOTAL		=ISNULL(@DC_TOTAL		,0)
	SELECT	@CNT_CUSTOMER	=ISNULL(@CNT_CUSTOMER	,0)
	SELECT	@AMT_AVERAGE	=ISNULL(@AMT_AVERAGE	,0)
	SELECT	@AMT_CASHRCP	=ISNULL(@AMT_CASHRCP	,0)
	SELECT	@AMT_VOID		=ISNULL(@AMT_VOID		,0)
	SELECT	@AMT_BANPUM	=ISNULL(@AMT_BANPUM	,0)
	SELECT	@RCP_BEGIN		=ISNULL(@RCP_BEGIN		,'')
	SELECT	@RCP_END		=ISNULL(@RCP_END		,'')


	
	SELECT @AMT_VOID		=ISNULL(@AMT_VOID,0)
	SELECT @AMT_PRESENT	= @AMT_READY - @AMT_MID + @AMT_ACCT_IN - @AMT_ACCT_OUT + @AMT_CASH + @AMT_GIFT + @AMT_CHECK 
	SELECT @AMT_SALE		= @AMT_CASH +  @AMT_CARD + @AMT_CHECK +  @AMT_TRUST + @AMT_LOSS + @AMT_OKC

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,	--연기기간
	@NEXT_JOIN		VARCHAR(30) OUTPUT		--다음가입기간
AS
BEGIN
	DECLARE	@NO_RCP	VARCHAR(4)
	DECLARE	@TODATE	DATETIME
	DECLARE	@YMD_SALE	VARCHAR(8)
	DECLARE	@TEMP_LOCKER	VARCHAR(25)
	DECLARE	@TEMP_LOCKER1	VARCHAR(25)

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

	IF LEN(@CD_MEMBER) <> 10
	BEGIN
		SELECT TOP 1 @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  YMD_SALE  = 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_FINISH, 
		  @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  YMD_SALE  BETWEEN A.YMD_BEGIN AND A.YMD_FINISH ) ,
		  @DELAY_RANGE	 = DBO.STOD(A.DELAY_BEGIN) +' ~ ' + DBO.STOD(A.DELAY_TO),
		  @DS_STATUS	=A.DS_STATUS,
		  @NO_RCP		=A.NO_RCP,
		  @NEXT_JOIN		=(SELECT TOP 1 DBO.STOD(A.YMD_BEGIN) +' ~ ' + DBO.STOD(A.YMD_FINISH) FROM SL_MEMBER_JOIN WHERE CD_MEMBER = A.CD_MEMBER AND YMD_BEGIN > A.YMD_FINISH)
	   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 TOP 1 @NO_RCP = NO_RCP FROM SL_MEMBER_JOIN WHERE CD_MEMBER	= @CD_MEMBER ORDER BY YMD_SALE DESC, NO_RCP DESC

		IF @NO_RCP = NULL
		BEGIN	
			--가입상품없음
			SET @DS_STATUS	= '5'
		END
		ELSE
		BEGIN
			--기간만료
			SET	@DS_STATUS = '4'
			
			SELECT  TOP 1
				  @YMD_BEGIN	=A.YMD_BEGIN, 
				  @YMD_END		=A.YMD_FINISH, 
				  @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  YMD_SALE  BETWEEN A.YMD_BEGIN AND A.YMD_FINISH ) 
			   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
			ORDER BY A.YMD_SALE DESC, NO_RCP DESC
		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 TOP 1 @TEMP_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_MEMBER

	SELECT TOP 2 @TEMP_LOCKER1 =  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_MEMBER

	SET @LOCKER = ISNULL(@TEMP_LOCKER,'') +' '+  ISNULL(@TEMP_LOCKER1,'')


	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].[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

		--포인트를 사용한 영수증은 포인트를 적립하지 않는다
		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 = 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
		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


	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
	

	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

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

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

		--포인트를 사용한 영수증은 포인트를 적립하지 않는다
		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 = 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
		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


	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

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO




CREATE    PROCEDURE SAVE_SL_POINT                                                                                   
                                                   
	 @YMD_SALE	 	VARCHAR(8),    	-- 매출일자          
	 @SEQ			INT,                                                   
	 @CD_MEMBER	VARCHAR(10),	-- 회원코드       
	 @CD_CODE		VARCHAR(3),	-- 코드        
	 @POINT  	 	INT,  		-- 포인트      
	 @LINK			VARCHAR(14),
	 @REMARK		VARCHAR(100), 
	 @CD_SAWON_CHG	VARCHAR(6),
	 @GUBUN		VARCHAR(1),	--0:사용, 1:지급
	 @WORK_KIND	VARCHAR(1)	--작업구분(I:추가, U:수정, D:삭제)                                                    
AS       
begin                                     
	DECLARE	@CNT		INT
	DECLARE	@BEF_POINT	INT

	IF @GUBUN = '0'
	BEGIN
		--수정전 포인트를 구한다
		SELECT @BEF_POINT = PNT_USE
		   FROM SL_PNT_PAY
		 WHERE YMD_PAY	=@YMD_SALE
		       AND SEQ		=@SEQ  

		IF @WORK_KIND = 'I'
		BEGIN
			SELECT @CNT = ISNULL(MAX(SEQ),0) + 1 FROM SL_PNT_PAY	WHERE YMD_PAY = @YMD_SALE

			INSERT INTO SL_PNT_PAY (YMD_PAY, SEQ, CD_MEMBER, CD_CODE, PNT_USE, REMARK, CD_SAWON_CHG)
					VALUES       (@YMD_SALE, @CNT, @CD_MEMBER, @CD_CODE, @POINT,  @REMARK, @CD_SAWON_CHG)

	
	
		END
		ELSE IF @WORK_KIND = 'U'
		BEGIN
			UPDATE SL_PNT_PAY	SET PNT_USE = @POINT,
 						        REMARK  = @REMARK,
						        CD_SAWON_CHG = @CD_SAWON_CHG,
						        DT_CHANGE 	=GETDATE()
			 WHERE  YMD_PAY	=@YMD_SALE
			       AND SEQ		=@SEQ

		END
		ELSE IF @WORK_KIND = 'D'
		BEGIN

			DELETE SL_PNT_PAY
			 WHERE YMD_PAY	=@YMD_SALE
			       AND SEQ		=@SEQ
			
		END
	END   
	ELSE IF @GUBUN = '1'  	
	BEGIN
		--수정전 포인트를 구한다
		SELECT @BEF_POINT = PNT_CASH
		   FROM SL_PNT_OCCUR
		 WHERE YMD_OCCUR	=@YMD_SALE
		       AND SEQ		=@SEQ

		IF @WORK_KIND = 'I'
		BEGIN
			SELECT @CNT = ISNULL(MAX(SEQ),0) + 1 FROM SL_PNT_OCCUR	WHERE YMD_OCCUR = @YMD_SALE

			INSERT INTO SL_PNT_OCCUR ( YMD_OCCUR, SEQ, CD_MEMBER, CD_CODE, PNT_CASH, AMT_CASH, PNT_CARD, AMT_CARD, PNT_CASHRCP, AMT_CASHRCP, LINK, REMARK, CD_SAWON_CHG)
					VALUES       (@YMD_SALE, @CNT, @CD_MEMBER, @CD_CODE, @POINT, 0, 0, 0, 0, 0, @LINK, @REMARK, @CD_SAWON_CHG)

	
		END
		ELSE IF @WORK_KIND = 'U'
		BEGIN
			UPDATE SL_PNT_OCCUR	SET PNT_CASH = @POINT,
	 						        REMARK  = @REMARK,
							        CD_SAWON_CHG = @CD_SAWON_CHG,
							        DT_CHANGE 	=GETDATE()
			 WHERE YMD_OCCUR	=@YMD_SALE
			       AND SEQ		=@SEQ

		END
		ELSE IF @WORK_KIND = 'D'
		BEGIN

			DELETE SL_PNT_OCCUR
			 WHERE YMD_OCCUR	=@YMD_SALE
			       AND SEQ		=@SEQ
			
		END
	END 

 END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

