if exists(select 1 from sysobjects where name='pr_wxsf_distribution_cash')
  drop procedure pr_wxsf_distribution_cash
go



CREATE procedure pr_wxsf_distribution_cash  
	@cardid nvarchar(50), --分销员卡号
	@amount  decimal(18,4),--提成金额
	@retval nvarchar(10) out, --放回值，1成功，0失败
	@retmessing nvarchar(100) OUT
WITH ENCRYPTION
as
begin
	declare @tempamount decimal(18,2)
	declare @day_str VARCHAR(3) 
	declare @day int --可提现的间隔天数
	declare @card_no nvarchar(20) --会员号
	declare @card_type nvarchar(10) --会员等级
	declare @branch_no nvarchar(20)--门店编号
	declare @saveamount decimal(18,2) --会员当前可用余额
	declare @flow_no nvarchar(50) --单据编号
	declare @updatetime datetime --提取时间
	DECLARE @max_flow_no VARCHAR(50),@max_flow_no_num NUMERIC,@right_no VARCHAR(50)
	set @retval='1'
	if(@cardid='') 
	begin
		set @retval='0'
		set @retmessing='请传入分销员'
		return
	end
	select @day_str=sys_var_value from t_sys_system where sys_var_id='FXConsumeAfterDay'
	if(@day_str is null or @day_str='')
	BEGIN
		set @day=1
	END
	ELSE
	BEGIN
		IF ISNUMERIC(@day_str) = 1
			SET @day = CAST(@day_str AS INT)
		ELSE
			SET @day = 1
	END

	--select @card_no=card_flowno,@card_type=card_type,@branch_no=branch_no,@saveamount=dbo.f_get_descry_star(residual_amt) 
	select @card_no=card_flowno,@card_type=card_type,@branch_no=branch_no,@saveamount=dbo.f_get_descry(residual_amt) 
	from t_rm_vip_info where card_id=@cardid
 
	SELECT @tempamount=isnull(SUM(res.deduct_amt),0) 
	FROM t_vip_fathercard_deduct_flow res where res.father_card=@cardid and isnull(res.Fa_status,'0')='0'
	and DATEDIFF (day,res.oper_date,GETDATE())>=CONVERT(int,@day) 
	--print @tempamount
	if(@tempamount=0)
	begin
	set @retval='0'
		set @retmessing='无提现金额,或还未达到可提现天数条件。'
		return
	end
	if(@tempamount<>@amount)
	begin
     set @retval='0'
     set @retmessing='传入金额与真实可提现金额不符，无法提现'
     return
	end

	begin tran	
	--获取单据编号
	set @updatetime=GETDATE()
	
	/*
	--Exec [dbo].[pr_get_sheetno] 'PT' ,@branch_no, @flow_no out; 
	IF EXISTS (SELECT 1 from t_rm_vip_savelist WHERE DATEDIFF(dd,GETDATE(),ope_date)=0 AND branch_no = @branch_no AND oper_type = '4' AND ISNULL(specsav_flag,'0') = '0' AND LEFT(flow_no,2) = 'FA')
	begin
		SELECT @max_flow_no = MAX(flow_no) FROM t_rm_vip_savelist WHERE DATEDIFF(dd,GETDATE(),ope_date)=0 AND branch_no = @branch_no AND oper_type = '4' AND ISNULL(specsav_flag,'0') = '0' AND LEFT(flow_no,2) = 'FA'
		IF LEN(@max_flow_no) > 0
		BEGIN
			SET @right_no = RIGHT(@max_flow_no,LEN(@max_flow_no) - 2)
			IF ISNUMERIC(@right_no) = 1
			BEGIN
				SET @max_flow_no_num = CAST(@right_no AS NUMERIC) + 1
				SET @max_flow_no = CAST(@max_flow_no_num AS VARCHAR(50))
			END	
			ELSE
				GOTO new_no			
		END
	end		
	ELSE
	BEGIN
		new_no:
		SELECT @max_flow_no = CONVERT(VARCHAR(8),GETDATE(),112)+'0001'
	END
	SET @flow_no = 'FA'+@max_flow_no
	IF LEN(@flow_no) <=0
	BEGIN
		SET	@retmessing = '生成流水单号失败！'
		GOTO my_err
	END
	*/
	
	--将提现金额转储值--实付金额应该为0，提现没付金额
	--insert into t_rm_vip_savelist(card_id,card_no,card_type,branch_no,flow_no,consum_amt,oper_des,oper_type,ope_date,oper_id,  memo,real_amt,com_flag,specsav_flag,inverse_flag)
	--values(@cardid,@card_no,@card_type,@branch_no,@flow_no,@amount,'储值卡充值','4',GETDATE(),'1001','线上分销员提现',0,'1','0','0')
	INSERT INTO t_rm_saving_plus_record ( branch_no, card_id, plus_count, oper_id,
	                                       oper_date, old_count, num1, other1,
	                                       residual_amt, com_flag, plus_real_count,
	                                       pay_way )
	VALUES  ( @branch_no,@cardid,@amount,'1001',
			GETDATE(),@saveamount,0.00,'线上分销提现',
			@saveamount,'0',0,'RMB')	--实付金额为0，分销不用付额
	IF @@error <> 0
	BEGIN
		SET	@retmessing = '生成储值记录事变！'
		GOTO my_err
	END

	update t_rm_vip_info set --save_amt=ISNULL(save_amt,0)+@amount,
	residual_amt=dbo.f_wxsf_set_descry(@saveamount+@amount)
	where card_id=@cardid
    IF @@error <> 0 OR @@ROWCOUNT = 0
	BEGIN
		SET	@retmessing = '更新会员余额失败！'
		GOTO my_err
	END
	
	--更新状态
	UPDATE  t_vip_fathercard_deduct_flow  SET Fa_status='1' ,fa_date=@updatetime,fa_flow_no=@flow_no 
	where father_card=@cardid and ISNULL(Fa_status,'0')='0'
	and DATEDIFF (day,oper_date,GETDATE())>=CONVERT(int,@day) 
	IF @@error <> 0
	BEGIN
		SET	@retmessing = '更新分销记录状态失败！'
		GOTO my_err
	END
	
	commit tran
	RETURN

	my_err:
		rollback tran;
		set @retval='0'
		--set @retmessing=ERROR_MESSAGE();
		RETURN
end
go