20130515
20100325

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

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

-- 주문취소내역 저장
CREATE  PROCEDURE POS_SAVE_ORDER_C

	@CD_STORE 	    		VARCHAR(4),
	@YMD_SALE			VARCHAR(8),
	@NO_LOCK			VARCHAR(4),
	@DS_LOCK			VARCHAR(3),
	@YN_ORDER			VARCHAR(1),
	@CD_MENU			VARCHAR(14),
	@QTY_CANCEL		INT,
	@PR_SALE			INT,
	@AMT_SALE			INT,
	@NO_POS			VARCHAR(2),
	@CD_SAWON			VARCHAR(6),
	@CD_DAMDANG		VARCHAR(6)

AS
BEGIN

	DECLARE @SEQ INT
	DECLARE @CNT INT
	DECLARE @QTY INT

	SELECT @CNT = COUNT(*) FROM SL_ORDER_H WHERE CD_STORE =@CD_STORE AND DS_LOCK =@DS_LOCK AND NO_LOCK = @NO_LOCK


	IF @CNT = 1
	BEGIN

		IF @YN_ORDER = 'Y'
		BEGIN
			SELECT @SEQ = ISNULL( MAX(SEQ), 0 ) + 1
			   FROM SL_ORDER_D
			WHERE CD_STORE	=@CD_STORE
			      AND DS_LOCK	=@DS_LOCK
			      AND NO_LOCK	=@NO_LOCK
		
			IF @SEQ IS NULL SET @SEQ = 1
	
			DECLARE	@DT_ORDER			DATETIME
	
			SELECT TOP 1 @DT_ORDER = DT_CREATE 
			   FROM SL_ORDER_D 
			WHERE CD_STORE	= @CD_STORE 
			      AND DS_LOCK 	=@DS_LOCK 
			      AND NO_LOCK 	= @NO_LOCK 
			      AND CD_MENU 	= @CD_MENU 
			      AND CD_DAMDANG= @CD_DAMDANG
			      AND QTY_SALE	> 0
			ORDER BY ORDERSEQ

			SELECT @QTY = SUM(QTY_SALE) 
			   FROM SL_ORDER_D 
			WHERE CD_STORE	= @CD_STORE 
			      AND DS_LOCK 	=@DS_LOCK 
			      AND NO_LOCK 	= @NO_LOCK 
			      AND CD_MENU 	= @CD_MENU 
			      AND CD_DAMDANG= @CD_DAMDANG
			      AND PR_SALE	= @PR_SALE

			IF @QTY = 0 RETURN	
		
			INSERT INTO SL_ORDER_D(
							CD_STORE,
							DS_LOCK,
							NO_LOCK,
							CD_MENU,
							SEQ,	
							QTY_SALE,
							PR_SALE,
							AMT_SALE,
							NO_POS,
							CD_DAMDANG
						         )
					VALUES
						        (
							@CD_STORE,
							@DS_LOCK,
							@NO_LOCK,
							@CD_MENU,
							@SEQ,
							@QTY_CANCEL*-1,
							@PR_SALE,
							(@QTY_CANCEL * @PR_SALE)*-1,
							@NO_POS,
							@CD_DAMDANG
						        )
	
			UPDATE SL_ORDER_H SET AMT_SALE = AMT_SALE - (@QTY_CANCEL * @PR_SALE)
			WHERE CD_STORE	=@CD_STORE
			      AND DS_LOCK	=@DS_LOCK
			      AND NO_LOCK	=@NO_LOCK
		END

		SELECT @SEQ = ISNULL( MAX(SEQ), 0 ) + 1
		   FROM SL_SALE_C
		WHERE CD_STORE	=@CD_STORE
		      AND YMD_SALE	=@YMD_SALE
	
		IF @SEQ IS NULL SET @SEQ = 1

		IF @YN_ORDER = 'N'
			SET @DT_ORDER = NULL
	
		INSERT INTO SL_SALE_C(
						CD_STORE,
						YMD_SALE,
						SEQ,
						DS_LOCK,
						NO_LOCK,
						CD_MENU,
						QTY_CANCEL,
						NO_POS,
						CD_SAWON,
						DT_ORDER
					         )
				VALUES
					        (
						@CD_STORE,
						@YMD_SALE,
						@SEQ,
						@DS_LOCK,
						@NO_LOCK,
						@CD_MENU,
						@QTY_CANCEL,
						@NO_POS,
						@CD_SAWON,
						@DT_ORDER
					        )

	END
	ELSE IF LEN(@YMD_SALE) = 8
	BEGIN
		SELECT @SEQ = ISNULL( MAX(SEQ), 0 ) + 1
		   FROM SL_SALE_C
		WHERE CD_STORE	=@CD_STORE
		      AND YMD_SALE	=@YMD_SALE
	
		IF @SEQ IS NULL SET @SEQ = 1
	
		INSERT INTO SL_SALE_C(
						CD_STORE,
						YMD_SALE,
						SEQ,
						DS_LOCK,
						NO_LOCK,
						CD_MENU,
						QTY_CANCEL,
						NO_POS,
						CD_SAWON
					         )
				VALUES
					        (
						@CD_STORE,
						@YMD_SALE,
						@SEQ,
						@DS_LOCK,
						@NO_LOCK,
						@CD_MENU,
						@QTY_CANCEL,
						@NO_POS,
						@CD_SAWON
					        )
	END

END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

