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

create procedure pr_rm_vip_fl @as_flowno varchar(20),@as_branch_no char(6)
WITH ENCRYPTION
as
begin
	--分销返利
	 declare @ls_cardid varchar(20),       -- 会员卡号
			@pay_amount numeric(16,4),	--销售金额
			 @send_amt numeric(16,4),	--赠送金额
			@ls_father_card1 varchar(20),--父卡id
			@ls_father_card2 varchar(20),--父父卡id
			@save_amt_new numeric(16,4),	--充值后余额
			@fl_rate1		numeric(16,4),--父卡返利比率
			@fl_rate2		numeric(16,4)--父父卡返利比率

	--初始化
	select @pay_amount=0
	select @send_amt=0
	select @save_amt_new=0
	select @fl_rate1=0
	select @fl_rate2=0
	
	--不是总部或者单店返回，分店不支持
	if not exists(select * from t_sys_system where sys_var_id = 'Is_Distributed' and sys_var_value in('0','1'))
		return

	--得到付款金额
	SELECT TOP 1 
				@ls_cardid=isnull(vip_no,''), 
				@pay_amount=sum(case when sell_way='B' then 0 - pay_amount else pay_amount end)
	FROM		t_rm_payflow 
	WHERE		flow_no = @as_flowno 
	AND		pay_way not in('CHG','GIF','JFP','SHC','SHP','SHC') 
	AND		branch_no = @as_branch_no
	and sell_way in('A','B')
	group by vip_no
	
	
	--为空返回
	if @ls_cardid='' 
		return
	if @pay_amount=0 or @pay_amount is null
		return

	--判断是否有父卡
	select @ls_father_card1=isnull(father_card,'')from  t_rm_vip_info where card_id=@ls_cardid
	if @ls_father_card1='' or @ls_father_card1 is null
		return
	
	--判断父卡是否为会员
	if not exists(select 1 from t_rm_vip_info where card_id=@ls_father_card1 ) 
		return

	--判断是否启用父卡返利
	if not exists(select * from t_sys_system where sys_var_id='pos_vip_fl_leve1' and sys_var_value='1')
		return

	--取父卡返利比率
	select @fl_rate1=convert(numeric(16,4),sys_var_value) from t_sys_system where sys_var_id='pos_vip_fl_rate1'
	if @fl_rate1<=0 or @fl_rate1>=1
		return
	
	--父卡返利充值
	select @send_amt=@pay_amount * @fl_rate1
	if @send_amt<0.01 
		return

	
	--开始充值
	--增加储值
	select @save_amt_new=0
	select @save_amt_new=dbo.f_get_descry(residual_amt)+@send_amt from t_rm_vip_info where card_id=@ls_father_card1

	update t_rm_vip_info
	set residual_amt=dbo.f_get_encrypt((dbo.f_get_descry(residual_amt) + @send_amt)*10000)
	where card_id=@ls_father_card1

	INSERT INTO t_rm_saving_plus_record ( card_id  , branch_no  , oper_id , oper_date  , plus_count ,old_count, other1,com_flag,pay_way) 
	select  card_id , left(@as_branch_no,2) , '1001' , getdate() ,  @send_amt ,@save_amt_new -@send_amt , '分销子卡返利:['+@ls_cardid +']单号:'+@as_flowno, '0','RMB' 
	from  t_rm_vip_info
	where card_id=@ls_father_card1
	
	--判断是否有父父卡
	select @ls_father_card2=isnull(father_card,'')from  t_rm_vip_info where card_id=@ls_father_card1
	if @ls_father_card2='' or @ls_father_card2 is null
		return

	--判断父父卡是否为会员
	if not exists(select 1 from t_rm_vip_info where card_id=@ls_father_card2 ) 
		return

	--父父卡返利
	if not exists(select * from t_sys_system where sys_var_id='pos_vip_fl_leve2' and sys_var_value='1')
		return

	--取父父卡返利比率
	select @fl_rate2=convert(numeric(16,4),sys_var_value) from t_sys_system where sys_var_id='pos_vip_fl_rate2'
	if @fl_rate2<=0 or @fl_rate2>=1
		return
	
	--父卡返利充值
	select @send_amt=@pay_amount * @fl_rate2
	if @send_amt<0.01 
		return

	--开始充值
	--增加储值
	select @save_amt_new=0
	select @save_amt_new=dbo.f_get_descry(residual_amt)+@send_amt from t_rm_vip_info where card_id=@ls_father_card2

	update t_rm_vip_info
	set residual_amt=dbo.f_get_encrypt((dbo.f_get_descry(residual_amt) + @send_amt)*10000)
	where card_id=@ls_father_card2

	INSERT INTO t_rm_saving_plus_record ( card_id  , branch_no  , oper_id , oper_date  , plus_count ,old_count, other1,com_flag,pay_way) 
	select  card_id , left(@as_branch_no,2) , '1001' , getdate() ,  @send_amt ,@save_amt_new -@send_amt , '分销子卡返利:['+@ls_father_card1 +']单号:'+@as_flowno, '0','RMB' 
	from  t_rm_vip_info
	where card_id=@ls_father_card2

end
go