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

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

/*微信点单的信息转入到部门营业系统中*/
CREATE PROCEDURE WXG_WeiXinBillToTCP
	@strWXBillNo	VARCHAR(18),
	@strUserName	VARCHAR(30),
	@intNotReckon TINYINT,
	@strOldBillNo	VARCHAR(18)=''
AS
BEGIN
	IF @strWXBillNo IS NULL
		SELECT @strWXBillNo=''
	IF LEN(@strWXBillNo)<10
		RETURN
	IF @strUserName IS NULL
		SELECT @strUserName=''
	IF @intNotReckon IS NULL
		SELECT @intNotReckon=0
	IF @strOldBillNo IS NULL
		SELECT @strOldBillNo=''

	--1.得到微信订单信息
	DECLARE @lngDListNo		AS DECIMAL(18,0)
	DECLARE @strTableNo		AS VARCHAR(10)
	DECLARE @intManCount	AS INT
	DECLARE @strManName	AS VARCHAR(60)
	DECLARE @strSubNo		AS VARCHAR(14)
	DECLARE @fltPayVal		AS DECIMAL(18,1)
	DECLARE @strTelNo		AS VARCHAR(20)
	DECLARE @strPayBillNo	AS VARCHAR(32)

	SELECT @strSubNo=Sub_No,@lngDListNo=DListNo,@intManCount=ManCount,@strTableNo=TableNo,@strManName=ReceiveMan,@fltPayVal=ItemValue,@strTelNo=ReceiveTel,@strPayBillNo=wxpayorderno FROM WX_Bill_Inform Where Bill_No=@strWXBillNo
	IF @strSubNo IS NULL
		SELECT @strSubNo=''
	IF @lngDListNo IS NULL
		SELECT @lngDListNo=0
	IF @intManCount IS NULL
		SELECT @intManCount=0
	IF @strTableNo IS NULL
		SELECT @strTableNo=''
	IF @strManName IS NULL
		SELECT @strManName=''
	IF @fltPayVal IS NULL
		SELECT @fltPayVal=0
	IF @strTelNo IS NULL
		SELECT @strTelNo=''
	IF @strPayBillNo IS NULL
		SELECT @strPayBillNo=''

	IF (@lngDListNo =0)
		RETURN

	DECLARE @datServerTime	AS DATETIME
	SELECT @datServerTime=GetDate()

	DECLARE @strRemarks		AS VARCHAR(80)
	SELECT @strRemarks='微信:' + @strWXBillNo

	IF LEN(@strPayBillNo)>10
		SELECT @strRemarks=@strRemarks + ',商户单号:' + @strPayBillNo

	DECLARE @strNewBillNo	AS VARCHAR(18)

	IF LEN(@strOldBillNo)>10
		BEGIN
			SELECT @strNewBillNo=Bill_No FROM TCP_Bill_Inform WHERE  Bill_No=@strOldBillNo And BillState=1
			IF @strNewBillNo IS NULL
				SELECT @strNewBillNo=''
		END

	ELSE
		BEGIN
			--2.创建部门主单信息
			EXECUTE TCP_SaveBillMainWX @lngDListNo,@strTableNo,@strUserName,@intManCount,@strManName,@strSubNo,@datServerTime,@strRemarks
		
			--3.得到单号			
			SELECT @strNewBillNo=MAX(Bill_No) FROM TCP_Bill_Inform WHERE BillState=1 And DListNo=@lngDListNo And TableNo=@strTableNo And TradeTime>DateAdd( second,-30,@datServerTime) And Remarks LIKE '%' + @strWXBillNo + '%'
			IF @strNewBillNo IS NULL
				SELECT @strNewBillNo=''
		END

	IF @strNewBillNo=''
		RETURN

	--4.添加消费单明细
	DECLARE @strItemNo		VARCHAR(10)
	DECLARE @fltItemPrice		DECIMAL(18,2)
	DECLARE @fltItemCount	DECIMAL(18,2)
	DECLARE @strItemRemarks	VARCHAR(60)

	DECLARE CurW1 CURSOR FOR
	SELECT Item_No,Item_Price,Item_Count,Remarks FROM WX_Bill_Item Where Bill_No=@strWXBillNo And Item_Count>0
	OPEN CurW1
	FETCH CurW1 INTO @strItemNo,@fltItemPrice,@fltItemCount,@strItemRemarks
	WHILE @@FETCH_STATUS=0     
		BEGIN
			IF @strItemNo IS NULL
				SELECT @strItemNo=''
			IF @fltItemPrice IS NULL
				SELECT @fltItemPrice=0
			IF @fltItemCount IS NULL
				SELECT @fltItemCount=0
			IF @strItemRemarks IS NULL
				SELECT @strItemRemarks=''

			--保存点单信息
			IF (@fltItemCount>0 And @fltItemPrice>0)
				BEGIN
					--EXECUTE TCP_SaveBillItemWX @strNewBillNo,@strItemNo,@fltItemPrice,@fltItemCount,100,0,@strUserName,'微信点单'
					EXECUTE TCP_SaveBillItemWX @strNewBillNo,@strItemNo,@fltItemPrice,@fltItemCount,100,0,@strUserName,@strItemRemarks
				END

			FETCH CurW1 INTO @strItemNo,@fltItemPrice,@fltItemCount,@strItemRemarks
		End
	CLOSE CurW1
	DEALLOCATE CurW1

	--添加打印备注中含 
	DECLARE @strNewRemarks 	AS VARCHAR(60)
	SELECT @strNewRemarks=''
	IF LEN(@strSubNo)>0
		SELECT @strNewRemarks=@strNewRemarks + '会员:' + @strSubNo

	IF LEN(@strTelNo)>0
		SELECT @strNewRemarks=@strNewRemarks + '手机:' + @strTelNo

	UPDATE TCP_Bill_Inform Set PrintTemp=PrintTemp + @strNewRemarks Where Bill_No=@strNewBillNo

	--5.开始分单打印
	EXECUTE TCP_BillSendToPrint @strNewBillNo

	IF @intNotReckon<1
		BEGIN
			--6.更改单据状态为完成
			EXECUTE TCP_SaveReckonAll @strNewBillNo,@strUserName,0,'',0,'',0,'微信点单',@fltPayVal,'',0,'',0,'',0,'',0,'微信点单已支付',''

			--7.更改为已接单状态
			SELECT @strRemarks = '分单:' + @strNewBillNo
			Update WX_Bill_Inform Set BillState=2,SendOutTime=@datServerTime,SendOutMan=@strUserName,Remarks=@strRemarks Where Bill_No=@strWXBillNo And BillState=1

			--8.设置自动打印结账单
			UPDATE TCP_Bill_Inform SET WXReckon=1 Where Bill_No=@strNewBillNo 
		END
	ELSE
		BEGIN
			--7.更改为已接单状态
			SELECT @strRemarks = '分单:' + @strNewBillNo
			Update WX_Bill_Inform Set BillState=2,SendOutTime=@datServerTime,SendOutMan=@strUserName,Remarks=@strRemarks Where Bill_No=@strWXBillNo And BillState<2
		END

END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

