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

create procedure pr_rm_vip
  @as_flowno varchar(20),@as_branch_no char(6),@as_lunar_date varchar(10)
as
begin
declare  @ls_vip_branchNO char(6),
	@ls_vip_branchNO_pass char(6),
	@ls_pos_vip_branch char(6),
	@ls_branchno char(6),
    @ls_itemno char(20),     
	@ls_itemType char(12),
    @ls_vipno varchar(20),  
    @ls_card_no varchar(20),          --储值卡号
    @ls_tj varchar(1),                 
    @ln_scale numeric(16,4),           
    @ln_nativeamt numeric(16,4),
    @ln_saleQty numeric(16,4),
    @ln_sourcePrc numeric(16,4),
    @ln_salePrc numeric(16,4),
    @ls_saleWay  varchar(1),
    @li_limit_num int,
    @ls_other1 varchar(20),
    @ls_pos_record_vip varchar(1),  --是否记积分
    @ls_pos_acc_int varchar(1),     --是取整积分
    @ls_consume_type varchar(1),    --记积分方式
    @pos_vip_multiple varchar(10),  --双倍积分设置  wujs 06-03-30
    @ln_bytiem_limit numeric(16,4),
    @ln_bytiem_score numeric(16,4),
    @ln_total_amount numeric(16,4), --本单合计金额
    @ln_vip_acc_amount numeric(16,4),  --本次记积分
    @ln_vip_amount numeric(16,4),   --本次记积分金额
    @ls_vip_price  varchar(1),      --是否积分
    @ls_isspec		varchar(1),			--当前是否特价
    @ls_acc_flag	varchar(1),	      --是否起用“商品是否积分”
    @ls_itemvip_flag    varchar(1),	--当前商品是否积分
    @ld_itemvip_acc	numeric(16,4),	--商品积分值
    @ls_record_acc char(1),         --会员类别是否记录会员积分
    @ls_clsno  char(20),            --商品类别
    @li_clslen int,                 --类别长度
    @ll_cnt    int,
    @ls_vip_spce_price varchar(1),  --会员特价是否积分
	@ls_cs_spce_price varchar(1),  --组合商品特价是否积分
    @ls_spec_flag char(10),          --特价类型
    @pos_vip_weekn_double varchar(10), ---每星期几双倍积分 
    @ls_double varchar(1),
    @ln_vipnum int ,
    @ln_multi_month int, --第几天N倍分
    @ln_multi_week int,   --每星期几N倍积分 
    @ls_multi_birthday varchar(10), --会员生日N倍积分
    @ln_multi_birthday int,
    @ls_vip_birthday varchar(5),
    @ls_bth_flag varchar(10),
    @ls_birthflag char(1),
    @ls_birthflag2 char(1),
    @ls_lessrate_flag varchar(10),
    @ln_percent numeric(6,2),
    @ln_profitrate numeric(16,2),
    @ls_vip_month_birthday varchar(5),
    @li_bei int,
    @ls_cs varchar(1),
		@ln_multi_sd 				NUMERIC(16,4), 	--时段N倍积分
		@ln_max_mult 				NUMERIC(10,2),    --最大的会员积分倍数
		@ls_max_mult 				CHAR(1)  ,			--会员最大的会员积分倍数标志
	@ls_time_jf			char(1)				,		--时段积分
	@ls_payway_vip 				CHAR(1)    	,	  --付款方式是否积分
    @ln_vip_acc_amount_jf numeric(16,4),  --本次时段积分
	@ln_vip_acc_amount_jf_part numeric(16,4),  
	@ln_multi_as 				NUMERIC(16,4),  	--时段N倍积分
    @ld_acc_rate decimal(10,2)	,  --会员积分比例 
				@lnway_addtotal_amount         NUMERIC(16,4), --本单付款方式积分的金额  
				@lnway_vip_bl              NUMERIC(16,4)	,  --本单付款方式积分比例
	@count_pay int,
	@ls_ret_acc varchar(1),
	@ls_saleWay_tmp varchar(1),
	@ls_temp varchar(10)

select @ls_double='0'
select @ln_vip_amount = 0
select @ln_vip_acc_amount = 0
select @ln_vip_acc_amount_jf = 0
select @ln_vip_acc_amount_jf_part = 0
select @ln_multi_birthday = 0
SELECT @ln_max_mult = 0
select @lnway_vip_bl = 1
select @count_pay = 0

	SELECT TOP 1
				@ls_vipNO = b.vip_no,	
				@ls_branchno = b.branch_no 
	FROM		t_rm_saleflow a,
				t_rm_payflow b 
	WHERE		a.flow_no = b.flow_no 
	AND		a.flow_no = @as_flowno 
	AND		b.branch_no = @as_branch_no
	AND		b.pay_way <> 'MOY'

	SELECT TOP 1 
				@ls_card_no=card_no 
	FROM		t_rm_payflow 
	WHERE		flow_no = @as_flowno 
	AND		pay_way ='SAV' 
	AND		branch_no = @as_branch_no

if ((@ls_vipNO is Null) or (@ls_vipNO = '')) and ((@ls_card_no is Null) or (@ls_card_no = '')) return
--bengin yejh 20121008 只有发卡店才算积分,参数设置	
--只有发卡店才算积分 隐藏功能是否打开
select @ls_vip_branchNO_pass = sys_var_value
from t_sys_system where lower(sys_var_id)='pos_vip_branch_pass'
if @ls_vip_branchNO_pass is null or @ls_vip_branchNO_pass = '' 
select @ls_vip_branchNO_pass = '0'
--只有发卡店才算积分 参数是否设置
select @ls_pos_vip_branch = sys_var_value 
from t_sys_system where lower(sys_var_id)='pos_vip_branch'
if @ls_pos_vip_branch is null or @ls_pos_vip_branch = '' 
select @ls_pos_vip_branch = '0'
--end	

if @ls_pos_vip_branch = '1'  and @ls_vip_branchNO_pass = '1'
	begin
		select TOP 1 @ls_vip_branchNO = isnull(branch_no,'') from t_rm_vip_info where card_id  = @ls_vipNO
		if @ls_vip_branchNO = ''
			select @ls_vip_branchNO = '00'
		if left(@as_branch_no,2) <> @ls_vip_branchNO return
	end	
--end 
-- modified by wujs 2005-01-14 会员卡所属类型是否积分 acc_flag 是否积分标志
if exists (select card_id from t_rm_vip_info where card_id = @ls_vipNO and card_type in (select type_id from t_rm_vip_type where acc_flag='0')) 
   select @ls_record_acc = '1' --当前会员记录积分 
else
   select @ls_record_acc = '0' --当前会员不记录积分 

	--按消费次数积分，不考虑付款方式 是否积分
	SELECT @ls_consume_type = sys_var_value FROM t_sys_system WHERE LOWER(sys_var_id) = 'pos_consume_type'
	if @ls_consume_type <> '2' 
		begin
			--付款方式是否积分	
			IF exists (SELECT b.pay_way FROM t_rm_payflow b , t_bd_payment_info a  WHERE a.pay_way = b.pay_way AND a.Is_integral='1' ) 
			   select @ls_payway_vip = '1' --付款方式积分 
			else
			   begin
				select @ls_payway_vip = '0' --付款方式不记录积分 
				select @ls_record_acc = '0' --当前会员不记录积分 
			   end
		end


--mahw 不积分也要减使用次数
select @ls_pos_record_vip = sys_var_value from t_sys_system where lower(sys_var_id)='pos_record_vip'
if (@ls_pos_record_vip='1') and (@ls_record_acc <> '1') and not ((@ls_vipNO is Null) or (@ls_vipNO = ''))
begin
	if exists(select * from t_rm_vip_consume where card_id=@ls_vipno and branch_no=left(@ls_branchno,2))
   begin
			      UPDATE	t_rm_vip_consume 
					SET		cons_count = isnull(cons_count, 0) + 1, 
								com_flag	= '0',
								cons_date = getdate(),
								modify_date = getdate()
          where card_id = @ls_vipNO and branch_no=left(@ls_branchno,2)
   end
   else
				INSERT INTO		
							t_rm_vip_consume
							(card_id,branch_no,vip_cons_amount,vip_cur_cons_amount,vip_acc_amount,vip_cur_acc_amount,cons_count,cons_date,modify_date)
          values(@ls_vipNO, left(@ls_branchno,2),0,0,0,0,1,getdate(),getdate())

	select @li_limit_num = use_num from t_rm_vip_info where card_id=@ls_vipNO
   if @li_limit_num is null return
   if @li_limit_num >0 
   begin
      --更新使用次数
      select @li_limit_num =@li_limit_num - 1
      update t_rm_vip_info set use_num=convert(varchar(10),@li_limit_num) where card_id=@ls_vipNO
   end
end

--不记录积分且不是储值卡,返回
if @ls_record_acc = '0' and ((@ls_card_no is Null) or (@ls_card_no = '')) return


--select @ls_pos_record_vip = sys_var_value from t_sys_system where lower(sys_var_id)='pos_record_vip'
select @ls_consume_type = sys_var_value from t_sys_system where lower(sys_var_id)='pos_consume_type'
select @ls_acc_flag =  sys_var_value from t_sys_system where lower(sys_var_id)='pos_vipitem_point'

select @li_clslen = sys_var_value from t_sys_system where sys_var_id='gl_itm_cls_nolen'
SELECT @ls_max_mult = sys_var_value FROM t_sys_system WHERE sys_var_id='pos_vip_max_mult'

IF @ls_max_mult IS null SET @ls_max_mult = '0'

-----------------取得多倍积分
--当天是N倍积分日时不再处理
	select @ls_birthflag = '0'
	select @ls_bth_flag = bth_flag, @ls_vip_birthday = right(convert(char(10), birthday, 121), 5) from t_rm_vip_info where card_id = @ls_vipno
	if @ls_bth_flag = '农历'
	begin
	   if right(@as_lunar_date, 5) = @ls_vip_birthday
	      select @ls_birthflag = '1'
	end
	else
	begin
	   if (select right(convert(char(10), getdate(), 121), 5)) = @ls_vip_birthday
	      select @ls_birthflag = '1'
	end
		
	if @ls_birthflag = '1'
	begin
	   select @ls_multi_birthday = sys_var_value FROM t_sys_system WHERE lower(sys_var_id) = 'pos_vip_birth_double'
	   select @ls_multi_birthday = isnull(@ls_multi_birthday, '0/2')
	   if substring(@ls_multi_birthday, 1, 1) = '1' 
	   begin
	      select @ln_multi_birthday = convert(int, substring(@ls_multi_birthday,3,len(@ls_multi_birthday) - 2))
	    
  if @ln_multi_birthday is null
	         select @ln_multi_birthday = 2
								
		    
  --N倍积分 
                if @ln_max_mult < @ln_multi_birthday set @ln_max_mult = @ln_multi_birthday				
				select @ls_double = '1'
	   end
	end
	--多倍积分日 START
	IF @ls_double <> '1' OR @ls_max_mult = '1'
	begin
	   --是否为又倍积分日 wujs 06-03-30
	   SELECT @pos_vip_multiple = sys_var_value FROM t_sys_system WHERE lower(sys_var_id) = 'pos_vip_multiple'
	   SELECT @pos_vip_multiple = ISNULL(@pos_vip_multiple, '0')
	   --N倍积分fufl20090724,N>=2
	   SELECT @ln_multi_month = cast( substring(sys_var_value,day(getdate())* 2 - 1,2) as int) FROM t_sys_system WHERE lower(sys_var_id) = 'pos_vip_multi_month'
	   if @ln_multi_month is null 
	      select @ln_multi_month=1
	   IF (@pos_vip_multiple='1' and @ln_multi_month>1)
	   begin 
	      IF @ln_max_mult < @ln_multi_month SET @ln_max_mult = @ln_multi_month --倍积分
	      select @ls_double='1'
	   END
	end
	---每星期几双倍积分日 fufl 07-08-03
	---当天又是每月第几日双倍积分日时，就不再做每星期几双倍积分日操作
	IF @ls_double <> '1' OR @ls_max_mult = '1' 
	begin
		select @pos_vip_weekn_double = sys_var_value FROM t_sys_system WHERE lower(sys_var_id) = 'pos_vip_weekn_double'
		select @pos_vip_weekn_double = isnull(@pos_vip_weekn_double,'0')
		--N倍积分fufl20090724,N>=2
		select @ln_multi_week = cast( substring(sys_var_value,DatePart(w,getdate()) * 2 - 1,2) as int) 
		FROM t_sys_system WHERE lower(sys_var_id) = 'pos_vip_multi_week'
		if @ln_multi_week is null 
		   select @ln_multi_week=1
		if (@pos_vip_weekn_double='1' and @ln_multi_week>1 )
		begin 
        IF @ln_max_mult < @ln_multi_week SET @ln_max_mult = @ln_multi_week --倍积分
          select @ls_double='1'
		end
	end
	---会员生日当月N倍积分 START
	IF @ls_double <> '1' OR @ls_max_mult = '1'
	begin
		if @ls_bth_flag = '农历'
		begin
		   if left(right(@as_lunar_date, 5),2) = left(@ls_vip_birthday,2)
		      select @ls_birthflag2 = '1'
		end
		else
		begin
		   if (select left(right(convert(char(10), getdate(), 121), 5),2)) = left(@ls_vip_birthday,2)
		      select @ls_birthflag2 = '1'
		end
		if @ls_birthflag2 = '1'	
		begin
	            select @ls_vip_month_birthday = sys_var_value FROM t_sys_system WHERE lower(sys_var_id) = 'pos_vip_month_double'
		    select @ls_vip_month_birthday = isnull(@ls_vip_month_birthday,'0/2')
		    if left(@ls_vip_month_birthday,1) = '1'
		    begin
			    select @li_bei = convert(int,substring(@ls_vip_month_birthday,3,len(@ls_vip_month_birthday)-2))
			    if @li_bei is null
			  	select @li_bei = 2
				
			    --N倍积分 
					IF @ln_max_mult < @li_bei SET @ln_max_mult = @li_bei
					SELECT @ls_double = '1'	
		    end
		end
	end
-----------------取得多倍积分


if @ls_acc_flag is null 
   select @ls_acc_flag = '0'

--增加当商品档案毛利率<X%时不积分  按消费金额积分和按单品的积分值积分时有效  add by fufl20100115 
select @ls_lessrate_flag = rtrim(sys_var_value) from t_sys_system where sys_var_id='noacc_less_itemrate'
if @ls_lessrate_flag is null or @ls_lessrate_flag=''
	select @ls_lessrate_flag='0'  
else
	begin	  
           select @ln_percent=cast(substring(@ls_lessrate_flag,3,len(@ls_lessrate_flag) - 2 ) as  numeric(6,2))
           select @ls_lessrate_flag=left(@ls_lessrate_flag,1)
	end

--存在时段积分单
SELECT	@ls_time_jf = '0' 

SELECT	@ls_time_jf = '1' 
FROM		t_pc_price_flow_master m, t_pc_price_flow_detail d 
WHERE		m.trans_no = 'JF' AND d.sheet_no = m.sheet_no AND m.valid_flag = '0' 
	AND		m.approve_flag = '1' AND SUBSTRING(m.WEEK, DATEPART(dw, GETDATE()), 1) = '1'  
	AND		CONVERT(CHAR(10), GETDATE(), 121) BETWEEN m.valid_date AND m.end_date
	AND		RIGHT(CONVERT(CHAR(19), GETDATE(), 121), 8) BETWEEN d.begin_time AND d.end_time 
	AND		( CHARINDEX(LEFT(@ls_branchno, 2), m.branchlist) > 0  or m.branch_no = 'ALL' )

-- 会员记录消费金额
if @ls_pos_record_vip <> '1' and ((@ls_card_no is Null) or (@ls_card_no = ''))
begin
   if exists(select * from t_rm_vip_consume where card_id=@ls_vipno and branch_no=left(@ls_branchno,2))
   begin
      update t_rm_vip_consume set cons_count = isnull(cons_count, 0) + 1, com_flag='0'
          where card_id = @ls_vipNO and branch_no=left(@ls_branchno,2)
   end
   else
      insert into t_rm_vip_consume(card_id,branch_no,vip_cons_amount,vip_cur_cons_amount,vip_acc_amount,vip_cur_acc_amount,cons_count)
          values(@ls_vipNO, left(@ls_branchno,2),0,0,0,0,1)
   
   select @li_limit_num = use_num from t_rm_vip_info where card_id=@ls_vipNO
   if @li_limit_num is null return
   if @li_limit_num >0 
   begin
      -- 更新使用次数
      select @li_limit_num =@li_limit_num - 1
      update t_rm_vip_info set use_num=convert(varchar(10),@li_limit_num) where card_id=@ls_vipNO
   end
   -- 记录了消费次数后， 直接返回.  wujs 2005-05-16
   return
end
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 记录会员积分
if (@ls_pos_record_vip='1') and (@ls_record_acc='1') and not ((@ls_vipNO is Null) or (@ls_vipNO = ''))
begin
		IF @ls_consume_type <> '2'
				begin
					--消费金额
					SELECT TOP 1 
							 @ls_saleway = sell_way, 
							 @ln_total_amount = sale_amount, 
							 @ls_branchno = branch_no  
					FROM	 t_rm_payflow 
					WHERE	 flow_no = @as_flowno AND pay_way<>'CHG' AND branch_no = @as_branch_no 
					--积分的消费金额
					select @lnway_addtotal_amount = isnull( sum( a.coin_rate *  (case when a.sell_way = 'D' then -a.pay_amount else a.pay_amount end)), 0 )
					from t_rm_payflow a left join t_bd_payment_info  on t_bd_payment_info.pay_way = a.pay_way
					WHERE a.flow_no = @as_flowno AND  a.pay_way<>'CHG' AND a.pay_way<>'GIF' AND  a.branch_no = @as_branch_no 
						AND  t_bd_payment_info.Is_integral = '1' and t_bd_payment_info.pay_flag = '0'
 
					
					select @count_pay = count(*) from t_rm_payflow where flow_no = @as_flowno
					
					if ( @ln_total_amount is null ) or (@ln_total_amount = 0)
						select @lnway_vip_bl = 1
					else
					begin
					   if  @count_pay = 1 and  @lnway_addtotal_amount > 0 
						select @lnway_vip_bl = 1  --只有一条付款方式，且该付款方式积分的
    					   else
						select @lnway_vip_bl = ( @lnway_addtotal_amount / @ln_total_amount ) --积分比例
					end 
				end

   -- 按次数记积分
   if @ls_consume_type = '2'
   begin

     select @ln_bytiem_limit = convert(numeric(16,4), sys_var_value) from t_sys_system where lower(sys_var_id)='pos_bytiem_limit'
     select @ln_bytiem_score = convert(numeric(16,4), sys_var_value) from t_sys_system where lower(sys_var_id)='pos_bytiem_score'
   
     select top 1 @ls_saleway = sell_way, @ln_total_amount = sale_amount, @ls_branchno = branch_no  from t_rm_payflow where flow_no=@as_flowno and pay_way<>'CHG' and branch_no = @as_branch_no
   
     if @ln_total_amount >= @ln_bytiem_limit
     begin
		if @ls_saleway='B'
			begin
			   select @ln_vip_amount = -@ln_total_amount
			   select @ln_vip_acc_amount = -@ln_bytiem_score
			end
		else
			begin
       			   select @ln_vip_amount = @ln_total_amount
				   select @ln_vip_acc_amount = @ln_bytiem_score
			end
     end
--	print @ln_vip_amount
--	print @ln_vip_acc_amount

   end
   -- 按金额记积分
   else if @ls_consume_type = '1'
   begin 
     declare cur_vip cursor for 
         select branch_no,item_no,sale_qnty,source_price,sale_price,sell_way,spec_flag from t_rm_saleflow
         where flow_no=@as_flowno and branch_no = @as_branch_no
     
     open cur_vip 
     fetch cur_vip into @ls_branchNO,@ls_ItemNO,@ln_saleQty,@ln_sourcePrc,@ln_salePrc,@ls_saleWay,@ls_spec_flag
     
     while (@@fetch_Status = 0) and @ls_pos_record_vip = '1'
     begin
	   --
	   --select @ln_scale = 0
       select @ls_itemvip_flag = vip_acc_flag , @ls_itemType = item_clsno from t_bd_item_info where item_no = @ls_ItemNO---------add by huangdw 20131216运行到这个地方时@ls_itemtype 没有值，所以取倍数有问题
       if @ls_acc_flag = '1'  -- 增加单品是否积分 add by jincm 2005-01-24   
       begin

		  select @ls_itemvip_flag = vip_acc_flag , @ls_itemType = item_clsno from t_bd_item_info where item_no = @ls_ItemNO
		  if @ls_itemvip_flag='0' or @ls_itemvip_flag is null
		  begin
		     fetch cur_vip into @ls_branchNO,@ls_ItemNO,@ln_saleQty,@ln_sourcePrc,@ln_salePrc,@ls_saleWay,@ls_spec_flag
		     continue
		  end
      
		end

       if @ls_lessrate_flag = '1' --当商品档案毛利率<X %时不积分add by fufl20100115 
       begin
       		select @ln_profitrate = round((case sale_price when 0 then 0 else (sale_price - price)/sale_price end),4) * 100 
                from t_bd_item_info where item_no = @ls_ItemNO  
                if @ln_profitrate < @ln_percent 
                 begin
		     fetch cur_vip into @ls_branchNO,@ls_ItemNO,@ln_saleQty,@ln_sourcePrc,@ln_salePrc,@ls_saleWay,@ls_spec_flag
		     continue
		  end
       end

      -- add by jincm 小类可以积分，有大类和小类时以小类为准
       select @ls_clsno = item_clsno from t_bd_item_info where item_no=@ls_ItemNO
       select @ln_scale = null
       select @ln_vipnum = null

       while (len(@ls_clsno) >=  @li_clslen)
		begin
		   if exists (select vip_amt from t_rm_vip_set where vip_item_cls = @ls_clsno)
			begin
				select @ln_scale = vip_amt,@ln_vipnum=vip_num from t_rm_vip_set where vip_item_cls = @ls_clsno
				select @ls_clsno = ''
			end
		   else
			begin
			select @ls_clsno = left(@ls_clsno,len(@ls_clsno) - @li_clslen)
			end	   
		end       
       -- select @ln_scale = vip_amt from t_rm_vip_set where vip_item_cls=( select left(item_clsno,((select sys_var_value from t_sys_system where sys_var_id='gl_itm_cls_nolen'))) from t_bd_item_info where item_no=@ls_ItemNO)

       select @ln_nativeamt = round(@ln_saleQty*@ln_saleprc,2)

      -- 特价是否积分 
       select @ls_tj = sys_var_value from t_sys_system where sys_var_id='pos_vip_policy'
	   select @ls_cs = sys_var_value from t_sys_system where sys_var_id='pos_vip_cs_set' 	
	   if @ls_cs is null 
			select @ls_cs = '0' 
       -- 当前商品是否特价 

       -- 根据优惠方式是否为空来判断当前销售是否为特价 modified by wujs 2005-09-19
       -- 即省了查询特价时间，又能防止超量特价不能判断是否需要积分
       if @ls_spec_flag='ps' or @ls_spec_flag='pn' or @ls_spec_flag='pu' or @ls_spec_flag='pc' or @ls_spec_flag='pcb' or 
		  @ls_spec_flag='pd' or @ls_spec_flag='pdb' or @ls_spec_flag='ep'  or @ls_spec_flag='pe'
          select @ls_isspec = '1'
       else
          select @ls_isspec = '0'
       
       if ( @ls_tj='0' and @ls_isspec ='1' ) or ( @ls_cs ='0' and  @ls_spec_flag='cs')
       begin
        fetch cur_vip into @ls_branchNO,@ls_ItemNO,@ln_saleQty,@ln_sourcePrc,@ln_salePrc,@ls_saleWay,@ls_spec_flag
          continue
       end
           
       -- start 会员价商品消费记入积分(只对按金额积分有效) modified by zenglw 2004-05-10
       if @ls_isspec ='0' and ( @ls_spec_flag='pv' or @ls_spec_flag='cs')  -- 本开关只在无特价时生效 zhangw
       begin
           select @ls_vip_price=sys_var_value from t_sys_system where sys_var_id='pos_vipprice_point'
	   -- 默认为积分
	   -- if (@ls_vip_price is null or rtrim(ltrim(@ls_vip_price)) = '' ) select @ls_vip_price = '1' 
	   -- if @ls_vip_price<>'1' 
	   -- 如果后台设为为不积分时还要比较价格是否相等，如果相等则认为没有优惠，还是积分。 zhangw
           if @ls_vip_price='0' and @ln_sourcePrc > @ln_salePrc 
           begin       
	        fetch cur_vip into @ls_branchNO,@ls_ItemNO,@ln_saleQty,@ln_sourcePrc,@ln_salePrc,@ls_saleWay,@ls_spec_flag
	        continue
           end
			
		-- 会员特价商品是否记积分(一定要是会员价记积分才能是特价是否记积分) add by lihg 2006-06-14
			-- 取会员特价
			select @ll_cnt = count(item_no) from t_rm_spec_price
			where branch_no=(select sys_var_value from t_sys_system where sys_var_id = 'g_branch_no')
			and item_no = @ls_ItemNO and special_type = '2' 
			and ( convert(char(10),getdate(),121) between convert(char(10),start_date,121) and convert(char(10),end_date,121))
			and ( substring(week, DATEPART(dw, getdate()), 1) = '1')
			and ( left(other3,2)='PV') 
			and ( other3=( select max(other3) 
							 from t_rm_spec_price 
							where branch_no=(select sys_var_value from t_sys_system where sys_var_id = 'g_branch_no')
							  and item_no= @ls_ItemNO
							  and (convert(char(10),getdate(),121) between convert(char(10),start_date,121) and convert(char(10),end_date,121))));
			select @ls_vip_spce_price=sys_var_value from t_sys_system where sys_var_id='pos_vip_spec_price'
			if @ls_vip_price = '1' and @ls_vip_spce_price = '0' and @ll_cnt > 0
			begin
				fetch cur_vip into @ls_branchNO,@ls_ItemNO,@ln_saleQty,@ln_sourcePrc,@ln_salePrc,@ls_saleWay,@ls_spec_flag
	        	continue
			end
		-- end add by lihg 2006-06-14

       end
       -- ended moddified 
      
       if @ln_scale is null --or @ln_scale = 0 为0时表示该类别不积分
           begin
             select @ln_scale = convert(numeric(16,4), sys_var_value) from t_sys_system where lower(sys_var_id)='pos_consume_score'
             select @ln_vipnum=1
           end 

       if @ln_vipnum is null 
          select @ln_vipnum=1
    
       if @ln_scale>0 and @ln_vipnum>0
       begin
          if @ls_saleWay='B'
          begin
             if @lnway_vip_bl = 1 -- qinjl 20131111
              begin
                 select @ln_vip_acc_amount = @ln_vip_acc_amount - (@ln_nativeamt/@ln_scale)*@ln_vipnum * @lnway_vip_bl
                 select @ln_vip_amount = @ln_vip_amount - @ln_nativeamt * @lnway_vip_bl
              end 
             else
              begin
                 select @ln_vip_acc_amount = @ln_vip_acc_amount - (@ln_nativeamt/@ln_scale)*@ln_vipnum * @lnway_addtotal_amount / @ln_total_amount
                 select @ln_vip_amount = @ln_vip_amount - @ln_nativeamt * @lnway_addtotal_amount / @ln_total_amount
              end 

			 select @ln_multi_as = 0
 			 if @ls_time_jf = '1' 
			 begin
				select @ls_itemvip_flag = vip_acc_flag , @ls_itemType = item_clsno from t_bd_item_info where item_no = @ls_ItemNO---------add by huangdw 20131216运行到这个地方时@ls_itemtype 没有值，所以取倍数有问题
				SELECT	TOP 1  @ln_multi_as = isnull( d.num1 , 0)
				FROM		t_pc_price_flow_master m, t_pc_price_flow_detail d 
				WHERE		m.trans_no = 'JF' AND d.sheet_no = m.sheet_no AND  m.valid_flag='0' 
				AND		m.approve_flag = '1' AND SUBSTRING(m.WEEK, DATEPART(dw, GETDATE()), 1) = '1'  
				AND		CONVERT(CHAR(10), GETDATE(), 121) BETWEEN m.valid_date AND m.end_date
				AND		RIGHT(CONVERT(CHAR(19), GETDATE(), 121),8) BETWEEN d.begin_time AND d.end_time 
				AND		( CHARINDEX(LEFT(@ls_branchNO, 2), m.branchlist) > 0  or m.branch_no = 'ALL') 
				AND		(item_no = 'ALL' or  d.item_no = substring(@ls_itemType, 1,len(item_no)) )
				ORDER BY oper_date DESC
 
			 end
			if  @ls_max_mult = '1' 
				begin
				 if @ln_multi_as < @ln_max_mult  select  @ln_multi_as = @ln_max_mult
				end 

			If @ln_multi_as = 0 	
				begin
                    if @lnway_vip_bl = 1 
					   select	@ln_vip_acc_amount_jf_part =  @ln_vip_acc_amount_jf_part -  (@ln_nativeamt/@ln_scale)*@ln_vipnum * @lnway_vip_bl
                    else
                       select	@ln_vip_acc_amount_jf_part =  @ln_vip_acc_amount_jf_part -  (@ln_nativeamt/@ln_scale)*@ln_vipnum * @lnway_addtotal_amount / @ln_total_amount

				--	select	@ln_vip_acc_amount_jf =  @ln_vip_acc_amount_jf - (@ln_nativeamt/@ln_scale)*@ln_vipnum  * @lnway_vip_bl
				end
			else 
				begin
					if @ln_multi_as > 0 	
                       begin
                        if @lnway_vip_bl = 1 
						   select	@ln_vip_acc_amount_jf =  @ln_vip_acc_amount_jf - ( (@ln_nativeamt/@ln_scale)*@ln_vipnum * @ln_multi_as)* @lnway_vip_bl
                        else
                           select	@ln_vip_acc_amount_jf =  @ln_vip_acc_amount_jf - ( (@ln_nativeamt/@ln_scale)*@ln_vipnum * @ln_multi_as)* @lnway_addtotal_amount / @ln_total_amount
                       end
				end
			
          end
		  else
			begin
                 if @lnway_vip_bl = 1	
                    begin
					   select @ln_vip_acc_amount = @ln_vip_acc_amount + (@ln_nativeamt/@ln_scale)*@ln_vipnum* @lnway_vip_bl
					   select @ln_vip_amount = @ln_vip_amount + @ln_nativeamt * @lnway_vip_bl
                    end
                 else  ---- qinjl 20131111
                   begin
                    select @ln_vip_acc_amount = @ln_vip_acc_amount + (@ln_nativeamt/@ln_scale)*@ln_vipnum* @lnway_addtotal_amount / @ln_total_amount
					select @ln_vip_amount = @ln_vip_amount + @ln_nativeamt * @lnway_addtotal_amount / @ln_total_amount
                   end

				 select @ln_multi_as = 0
 				 if @ls_time_jf = '1' 
				 begin
					select @ls_itemvip_flag = vip_acc_flag , @ls_itemType = item_clsno from t_bd_item_info where item_no = @ls_ItemNO---------add by huangdw 20131216运行到这个地方时@ls_itemtype 没有值，所以取倍数有问题
					SELECT	TOP 1 @ln_multi_as = d.num1 
					FROM		t_pc_price_flow_master m, t_pc_price_flow_detail d 
					WHERE		m.trans_no = 'JF' AND d.sheet_no = m.sheet_no AND  m.valid_flag='0' 
					AND		m.approve_flag = '1' AND SUBSTRING(m.WEEK, DATEPART(dw, GETDATE()), 1) = '1'  
					AND		CONVERT(CHAR(10), GETDATE(), 121) BETWEEN m.valid_date AND m.end_date
					AND		RIGHT(CONVERT(CHAR(19), GETDATE(), 121),8) BETWEEN d.begin_time AND d.end_time 
					AND		( CHARINDEX(LEFT(@ls_branchNO, 2), m.branchlist) > 0  or m.branch_no = 'ALL') 
					AND		(item_no = 'ALL' or  d.item_no = substring(@ls_itemType, 1,len(item_no)) )
					ORDER BY oper_date DESC
				 end
 

				if  @ls_max_mult = '1' 
					begin
					 if @ln_multi_as < @ln_max_mult  select  @ln_multi_as = @ln_max_mult
					end 

				If @ln_multi_as = 0 	
					begin
                       if @lnway_vip_bl = 1 
						  select	@ln_vip_acc_amount_jf_part =  @ln_vip_acc_amount_jf_part + (@ln_nativeamt/@ln_scale)*@ln_vipnum * @lnway_vip_bl
                       else  ---- qinjl 20131111
                          select	@ln_vip_acc_amount_jf_part =  @ln_vip_acc_amount_jf_part + (@ln_nativeamt/@ln_scale)*@ln_vipnum * @lnway_addtotal_amount / @ln_total_amount

					--	select	@ln_vip_acc_amount_jf =  @ln_vip_acc_amount_jf +  (@ln_nativeamt/@ln_scale)*@ln_vipnum * @lnway_vip_bl
					end
				else 
					begin
					if @ln_multi_as >= 0 	
                       begin
                        if @lnway_vip_bl = 1
						   select	@ln_vip_acc_amount_jf =  @ln_vip_acc_amount_jf + ( (@ln_nativeamt/@ln_scale)*@ln_vipnum * @ln_multi_as)* @lnway_vip_bl
                        else  -- qinjl 20131111
                           select	@ln_vip_acc_amount_jf =  @ln_vip_acc_amount_jf + ( (@ln_nativeamt/@ln_scale)*@ln_vipnum * @ln_multi_as)* @lnway_addtotal_amount / @ln_total_amount
                       end 
					end 
  		  end 
       end

       fetch cur_vip into @ls_branchNO,@ls_ItemNO,@ln_saleQty,@ln_sourcePrc,@ln_salePrc,@ls_saleWay,@ls_spec_flag
     end
     close cur_vip
     deallocate cur_vip  
   end



   else -- 增加单品积分 add by jincm 2005-01-24   
   begin 
       declare cur_vip_item cursor for 
         select branch_no,item_no,sale_qnty,source_price,sale_price,sell_way,spec_flag from t_rm_saleflow
         where flow_no=@as_flowno and branch_no = @as_branch_no
     
       open cur_vip_item 
       fetch cur_vip_item into @ls_branchNO,@ls_ItemNO,@ln_saleQty,@ln_sourcePrc,@ln_salePrc,@ls_saleWay,@ls_spec_flag
     
       while (@@fetch_Status = 0) and @ls_pos_record_vip = '1'
   
		begin
          select @ld_itemvip_acc = vip_acc_num, @ls_itemvip_flag = vip_acc_flag , @ls_itemType = item_clsno  from t_bd_item_info where item_no = @ls_ItemNO
          if @ls_acc_flag = '1' -- 单品是否积分
          begin
              if @ls_itemvip_flag='0'  or @ls_itemvip_flag is null
              begin
                fetch cur_vip_item into @ls_branchNO,@ls_ItemNO,@ln_saleQty,@ln_sourcePrc,@ln_salePrc,@ls_saleWay,@ls_spec_flag
                continue
              end
              else if @ld_itemvip_acc = 0 or @ld_itemvip_acc is null
              begin
                  fetch cur_vip_item into @ls_branchNO,@ls_ItemNO,@ln_saleQty,@ln_sourcePrc,@ln_salePrc,@ls_saleWay,@ls_spec_flag
                  continue
          
    end
          end

          if @ls_lessrate_flag = '1' --当商品档案毛利率<X %时不积分add by fufl20100115 
          begin
       		select @ln_profitrate = round((case sale_price when 0 then 0 else (sale_price - price)/sale_price end),4) * 100 
                from t_bd_item_info where item_no = @ls_ItemNO  
                if @ln_profitrate < @ln_percent 
                 begin
				 fetch cur_vip_item into @ls_branchNO,@ls_ItemNO,@ln_saleQty,@ln_sourcePrc,@ln_salePrc,@ls_saleWay,@ls_spec_flag
                     continue
				 end
          end
            
         select @ln_nativeamt =round( @ln_saleQty*@ln_saleprc ,2)

	  -- 特价是否积分 
          select @ls_tj = sys_var_value from t_sys_system where sys_var_id='pos_vip_policy'
		  select @ls_cs = sys_var_value from t_sys_system where sys_var_id='pos_vip_cs_set' 	
		   if @ls_cs is null 
				select @ls_cs = '0'

--	 	if @ls_spec_flag = 'pu' 
--			select @ls_isspec ='1'
--	  	else
--      	    select @ls_isspec ='0'
                 -- 当前商品是否特价   
       


          -- 根据优惠方式是否为空来判断当前销售是否为特价 modified by wujs 2005-09-19
          -- 即省了查询特价时间，又能防止超量特价不能判断是否需要积分
	  if @ls_spec_flag='ps' or @ls_spec_flag='pn' or @ls_spec_flag='pu' or @ls_spec_flag='pe' or
	     @ls_spec_flag='pc' or @ls_spec_flag='pcb' or @ls_spec_flag='pd' or @ls_spec_flag='pdb'
             select @ls_isspec = '1'
          else
             select @ls_isspec = '0'
          
          
          if ( @ls_tj='0' and @ls_isspec ='1' ) or ( @ls_cs ='0' and  @ls_spec_flag='cs')
          begin
             fetch cur_vip_item into @ls_branchNO,@ls_ItemNO,@ln_saleQty,@ln_sourcePrc,@ln_salePrc,@ls_saleWay,@ls_spec_flag
             continue
          end
          
          -- start 会员价商品消费记入积分(只对按金额积分有效) modified by zenglw 2004-05-10
          if @ls_isspec ='0' and @ls_spec_flag='pv' -- 本开关只在无特价时生效 zhangw
          begin 
			  select @ls_vip_price=sys_var_value from t_sys_system where sys_var_id='pos_vipprice_point'

		      -- 如果后台设为为不积分时还要比较价格是否相等，如果相等则认为没有优惠，还是积分。 zhangw
		      if @ls_vip_price='0' and @ln_sourcePrc > @ln_salePrc 
		      begin       
		        fetch cur_vip_item into @ls_branchNO,@ls_ItemNO,@ln_saleQty,@ln_sourcePrc,@ln_salePrc,@ls_saleWay,@ls_spec_flag
		        continue
		      end

				-- 会员特价商品是否记积分(一定要是会员价记积分才能是特价是否记积分) add by lihg 2006-06-14
				-- 取会员特价
				select @ll_cnt = count(item_no) from t_rm_spec_price
				where branch_no=(select sys_var_value from t_sys_system where sys_var_id = 'g_branch_no')
				and item_no = @ls_ItemNO and special_type = '2' 
				and ( convert(char(10),getdate(),121) between convert(char(10),start_date,121) and convert(char(10),end_date,121))
				and ( substring(week, DATEPART(dw, getdate()), 1) = '1')
				and ( left(other3,2)='PV') 
				and ( other3=( select max(other3) 
				from t_rm_spec_price 
								where branch_no=(select sys_var_value from t_sys_system where sys_var_id = 'g_branch_no')
								  and item_no= @ls_ItemNO
								  and (convert(char(10),getdate(),121) between convert(char(10),start_date,121) and convert(char(10),end_date,121))));

				select @ls_vip_spce_price=sys_var_value from t_sys_system where sys_var_id='pos_vip_spec_price'
				if @ls_vip_price = '1' and @ls_vip_spce_price = '0' and @ll_cnt > 0
				begin
					fetch cur_vip_item into @ls_branchNO,@ls_ItemNO,@ln_saleQty,@ln_sourcePrc,@ln_salePrc,@ls_saleWay,@ls_spec_flag
	        		continue
				end  
			end

			--mahw 促销组合特价的商品控制不积分，界面参数未放出
			if @ls_isspec ='0' and @ls_spec_flag='cs'   --mahw
			begin
				select @ll_cnt=0
				select @ll_cnt = count(item_no) from t_rm_spec_price
				where branch_no=(select sys_var_value from t_sys_system where sys_var_id = 'g_branch_no')
				and item_no = @ls_ItemNO 
				and ( convert(char(10),getdate(),121) between convert(char(10),start_date,121) and convert(char(10),end_date,121))
				and ( substring(week, DATEPART(dw, getdate()), 1) = '1')
				and item_no in (select item_no from t_bd_item_info where combine_sta = '1')
				and ( other3=( select max(other3) 
								 from t_rm_spec_price 
								where branch_no=(select sys_var_value from t_sys_system where sys_var_id = 'g_branch_no')
								  and item_no= @ls_ItemNO
								  and (convert(char(10),getdate(),121) between convert(char(10),start_date,121) and convert(char(10),end_date,121))));

				select @ls_cs_spce_price=sys_var_value from t_sys_system where sys_var_id='pos_cs_spec_price'
				if @ls_cs_spce_price is null  
					select @ls_cs_spce_price = '1'

				if @ls_cs = '1' and @ls_cs_spce_price = '0' and @ll_cnt > 0
				begin
					fetch cur_vip into @ls_branchNO,@ls_ItemNO,@ln_saleQty,@ln_sourcePrc,@ln_salePrc,@ls_saleWay,@ls_spec_flag
	        		continue
				end
			end

          if @ln_vipnum is null 
						select @ln_vipnum=1

          if @ld_itemvip_acc > 0
          begin
			 if @ls_saleWay='B'
			 begin
             		select @ln_vip_acc_amount = @ln_vip_acc_amount - @ln_saleQty*@ld_itemvip_acc
             		select @ln_vip_amount = @ln_vip_amount - @ln_nativeamt 
					
					select @ln_multi_as = 0
 					 if @ls_time_jf = '1' 
					 begin
						select @ls_itemvip_flag = vip_acc_flag , @ls_itemType = item_clsno from t_bd_item_info where item_no = @ls_ItemNO---------add by huangdw 20131216运行到这个地方时@ls_itemtype 没有值，所以取倍数有问题
						SELECT	TOP 1 @ln_multi_as = d.num1 
						FROM		t_pc_price_flow_master m, t_pc_price_flow_detail d 
						WHERE		m.trans_no = 'JF' AND d.sheet_no = m.sheet_no AND  m.valid_flag='0' 
						AND		m.approve_flag = '1' AND SUBSTRING(m.WEEK, DATEPART(dw, GETDATE()), 1) = '1'  
						AND		CONVERT(CHAR(10), GETDATE(), 121) BETWEEN m.valid_date AND m.end_date
						AND		RIGHT(CONVERT(CHAR(19), GETDATE(), 121),8) BETWEEN d.begin_time AND d.end_time 
						AND		( CHARINDEX(LEFT(@ls_branchNO, 2), m.branchlist) > 0  or m.branch_no = 'ALL') 
						AND		(item_no = 'ALL' or  d.item_no = substring(@ls_itemType, 1,len(item_no)) )
						ORDER BY oper_date DESC
					 end 
					if  @ls_max_mult = '1' 
						begin
						 if @ln_multi_as < @ln_max_mult  select  @ln_multi_as = @ln_max_mult
						end 

					If @ln_multi_as = 0 	
						begin
                            if @lnway_vip_bl = 1
							   select	@ln_vip_acc_amount_jf_part =  @ln_vip_acc_amount_jf_part -  (@ln_saleQty*@ld_itemvip_acc)*@ln_vipnum * @lnway_vip_bl
                            else  --- qinjl 20131111
                               select	@ln_vip_acc_amount_jf_part =  @ln_vip_acc_amount_jf_part -  (@ln_saleQty*@ld_itemvip_acc)*@ln_vipnum * @lnway_addtotal_amount / @ln_total_amount

					--		select	@ln_vip_acc_amount_jf =  @ln_vip_acc_amount_jf -  (@ln_saleQty*@ld_itemvip_acc)*@ln_vipnum * @lnway_vip_bl
						end
					else 
						begin
						if @ln_multi_as >= 0 
                           begin
                            if @lnway_vip_bl = 1	
							   select	@ln_vip_acc_amount_jf =  @ln_vip_acc_amount_jf - ( (@ln_saleQty*@ld_itemvip_acc)*@ln_vipnum * @ln_multi_as)* @lnway_vip_bl
                            else  ----qinjl 20131111
                               select	@ln_vip_acc_amount_jf =  @ln_vip_acc_amount_jf - ( (@ln_saleQty*@ld_itemvip_acc)*@ln_vipnum * @ln_multi_as)* @lnway_addtotal_amount / @ln_total_amount
                           end
						end

			 end
           else
			 begin 

				select @ln_vip_acc_amount = @ln_vip_acc_amount + @ln_saleQty*@ld_itemvip_acc
				select @ln_vip_amount = @ln_vip_amount + @ln_nativeamt 	
				select @ln_multi_as = 0
 				if @ls_time_jf = '1' 
				begin
					select @ls_itemvip_flag = vip_acc_flag , @ls_itemType = item_clsno from t_bd_item_info where item_no = @ls_ItemNO---------add by huangdw 20131216运行到这个地方时@ls_itemtype 没有值，所以取倍数有问题
					SELECT	TOP 1 @ln_multi_as = d.num1 
					FROM		t_pc_price_flow_master m, t_pc_price_flow_detail d 
					WHERE		m.trans_no = 'JF' AND d.sheet_no = m.sheet_no AND  m.valid_flag='0' 
					AND		m.approve_flag = '1' AND SUBSTRING(m.WEEK, DATEPART(dw, GETDATE()), 1) = '1'  
					AND		CONVERT(CHAR(10), GETDATE(), 121) BETWEEN m.valid_date AND m.end_date
					AND		RIGHT(CONVERT(CHAR(19), GETDATE(), 121),8) BETWEEN d.begin_time AND d.end_time 
					AND		( CHARINDEX(LEFT(@ls_branchNO, 2), m.branchlist) > 0  or m.branch_no = 'ALL') 
					AND		(item_no = 'ALL' or  d.item_no = substring(@ls_itemType, 1,len(item_no)))
					ORDER BY oper_date DESC
				end 
 
				if  @ls_max_mult = '1' 
					begin
					 if @ln_multi_as < @ln_max_mult  select  @ln_multi_as = @ln_max_mult
					end 
				If @ln_multi_as = 0 	
					begin     
                        if @lnway_vip_bl = 1
						   select	@ln_vip_acc_amount_jf_part =  @ln_vip_acc_amount_jf_part +  (@ln_saleQty*@ld_itemvip_acc)*@ln_vipnum * @lnway_vip_bl 
                        else  -- qinjl 20131111
                           select	@ln_vip_acc_amount_jf_part =  @ln_vip_acc_amount_jf_part +  (@ln_saleQty*@ld_itemvip_acc)*@ln_vipnum * @lnway_addtotal_amount / @ln_total_amount
					end
				else 
					begin 
						if @ln_multi_as >= 0 
                            if 	@lnway_vip_bl = 1
							    select @ln_vip_acc_amount_jf =  @ln_vip_acc_amount_jf + ( (@ln_saleQty*@ld_itemvip_acc)*@ln_vipnum * @ln_multi_as)* @lnway_vip_bl
                            else  -- qinjl 20131111
                                select @ln_vip_acc_amount_jf =  @ln_vip_acc_amount_jf + ( (@ln_saleQty*@ld_itemvip_acc)*@ln_vipnum * @ln_multi_as)* @lnway_addtotal_amount / @ln_total_amount
					end

			 end 
          end
         
          fetch cur_vip_item into @ls_branchNO,@ls_ItemNO,@ln_saleQty,@ln_sourcePrc,@ln_salePrc,@ls_saleWay,@ls_spec_flag
       end
       close cur_vip_item
       deallocate cur_vip_item  
   end

--	print @ln_vip_acc_amount 
--	print @ln_vip_acc_amount_jf_part 
--	print @ln_vip_acc_amount_jf 

   -- 积分更新

 

--print @ln_vip_acc_amount
--print @ln_vip_acc_amount_jf_part
--print @ln_max_mult
--print @ln_vip_acc_amount_jf
	  
	--时段积分  END
	if @ln_vip_acc_amount_jf_part is null set @ln_vip_acc_amount_jf_part = 0
	if @ln_vip_acc_amount_jf is null set @ln_vip_acc_amount_jf = 0
 

	--时段积分优先
	IF @ln_max_mult IS null OR @ln_max_mult = 0 SET @ln_max_mult = 1
	if @ls_time_jf = '1'   
		begin   
			if @ls_consume_type = '2'
				SELECT @ln_vip_acc_amount = @ln_vip_acc_amount * @ln_max_mult 
			else 
				begin  

					SELECT @ln_vip_acc_amount_jf_part = @ln_vip_acc_amount_jf_part * @ln_max_mult 

					SELECT @ln_vip_acc_amount =	@ln_vip_acc_amount_jf_part + @ln_vip_acc_amount_jf 	
				end 
		end
	else 
		begin 
			if @ls_consume_type = '2'
				SELECT @ln_vip_acc_amount = @ln_vip_acc_amount * @ln_max_mult 
			else 
				SELECT @ln_vip_acc_amount = @ln_vip_acc_amount_jf_part * @ln_max_mult + @ln_vip_acc_amount_jf   
		end 
  
   -- 积分取整
   select @ls_pos_acc_int = '1'
   select @ls_pos_acc_int = sys_var_value from t_sys_system where lower(sys_var_id)='pos_vip_acc_int'
   
   if @ls_pos_acc_int = '1'
   begin
		--------youdp--2012-12-19--------------------
		 set @ls_temp = '1'
		 select top 1 @ls_temp = sys_var_value from t_sys_system with(nolock) where lower(sys_var_id) = 'acc_round'
		 select @ls_temp = isnull(@ls_temp,'1') --默认为向下取整
		 if @ls_temp = '1' 
		 begin	
			--向下取整
			 if @ln_vip_acc_amount > 0 
				select @ln_vip_acc_amount = floor(@ln_vip_acc_amount)  
			 else 
	      		select @ln_vip_acc_amount = -floor(abs(@ln_vip_acc_amount))   

             if @ln_vip_acc_amount_jf_part > 0 
					select @ln_vip_acc_amount_jf_part  = floor(@ln_vip_acc_amount_jf_part ) 
			 else
	      			select @ln_vip_acc_amount_jf_part  = -floor(abs(@ln_vip_acc_amount_jf_part )) 

			 if @ln_vip_acc_amount_jf > 0 
					select @ln_vip_acc_amount_jf  = floor(@ln_vip_acc_amount_jf ) 
			 else
					select @ln_vip_acc_amount_jf  = -floor(abs(@ln_vip_acc_amount_jf))
			 
		 end 
		 else if @ls_temp = '2'
		 begin
			--向上取整
			
	if @ln_vip_acc_amount > 0
				select @ln_vip_acc_amount = ceiling(@ln_vip_acc_amount) 
			else
      			select @ln_vip_acc_amount = -ceiling(abs(@ln_vip_acc_amount)) 
			
			if @ln_vip_acc_amount_jf_part > 0 
					select @ln_vip_acc_amount_jf_part  = ceiling(@ln_vip_acc_amount_jf_part ) 
			else
	      			select @ln_vip_acc_amount_jf_part  = -ceiling(abs(@ln_vip_acc_amount_jf_part )) 

			if @ln_vip_acc_amount_jf > 0 
				select @ln_vip_acc_amount_jf  = ceiling(@ln_vip_acc_amount_jf ) 
			else
				select @ln_vip_acc_amount_jf  = -ceiling(abs(@ln_vip_acc_amount_jf))
		 end
		 else
		 begin
			--四舍五入
			if @ln_vip_acc_amount > 0
				select @ln_vip_acc_amount = round(@ln_vip_acc_amount,0) 
			else
      			select @ln_vip_acc_amount = -round(abs(@ln_vip_acc_amount),0) 
			
			if @ln_vip_acc_amount_jf_part > 0 
					select @ln_vip_acc_amount_jf_part  = round(@ln_vip_acc_amount_jf_part,0) 
			else
	      			select @ln_vip_acc_amount_jf_part  = -round(abs(@ln_vip_acc_amount_jf_part ),0) 

			if @ln_vip_acc_amount_jf > 0 
				select @ln_vip_acc_amount_jf  = round(@ln_vip_acc_amount_jf ,0) 
			else
				select @ln_vip_acc_amount_jf  = -round(abs(@ln_vip_acc_amount_jf),0)

		 end
   end 

 
--会员类别积分比例 add by hehf at 2012-06-05 -----------------------------------------------------------------------------------
  --select @ld_acc_rate = acc_rate from t_rm_vip_type where type_id = (select card_type from t_rm_vip_info where card_id = @ls_vipNO)
 --if @ld_acc_rate is null or @ld_acc_rate <= 0 
 --set @ld_acc_rate = 1
  
 select @ld_acc_rate = 1
  
  select @ln_vip_acc_amount = @ln_vip_acc_amount * @ld_acc_rate
  
  ---------------------- end add ---------------------------------------------------------------------------------------------------

	if @ls_saleWay = 'D'
	begin
	   select @ln_vip_amount = -@ln_vip_amount
	   select @ln_vip_acc_amount = -@ln_vip_acc_amount
	end
   
	--------youdp--2012-09-14--------按单退货--原单没有积分---不减积分--------------------------
	set @ls_saleWay_tmp = ''
	select top 1 @ls_saleWay_tmp = sell_way 
	from t_rm_payflow with(nolock) 
	where t_rm_payflow.flow_no = @as_flowno and
		  isnull(t_rm_payflow.voucher_no,'') <> '' and
		  isnull(t_rm_payflow.vip_no,'') <> ''
	select @ls_saleWay_tmp = isnull(@ls_saleWay_tmp,'')
    set @ls_ret_acc = '1'
	if @ls_saleWay_tmp = 'B'
		begin
			if not exists(select top 1 1 from t_rm_payflow with(nolock),t_rm_card_paylist with(nolock)
										where t_rm_payflow.flow_no = @as_flowno and
											  t_rm_payflow.sell_way = 'B' and
											  t_rm_payflow.voucher_no = t_rm_card_paylist.flow_no and
											  t_rm_payflow.vip_no = t_rm_card_paylist.card_id and
											  t_rm_payflow.vip_no = @ls_vipno and
											  isnull(t_rm_payflow.voucher_no,'') <> '' and
											  t_rm_card_paylist.vip_acc > 0)
								
			set @ls_ret_acc = '0'
		end
		
---------------------------------------------------------------------------------------------


    --原单为赠送时的退货单，退货不减积分
   if not (exists (select 1 from t_rm_payflow where flow_no=( select top 1 voucher_no from t_rm_payflow where flow_no = @as_flowno ) and pay_way='GIF' and memo not like '买满直减%' ) or @ls_ret_acc = '0')
            
     begin

	  if exists(select * from t_rm_vip_consume where card_id=@ls_vipno and branch_no=left(@ls_branchno,2))
	   begin
	 

	
	       update t_rm_vip_consume set
	          vip_cons_amount = isnull(vip_cons_amount,0) + @ln_vip_amount,
	          vip_cur_cons_amount = isnull(vip_cur_cons_amount,0) + @ln_vip_amount,
	          vip_acc_amount = isnull(vip_acc_amount,0) + @ln_vip_acc_amount,
	          vip_cur_acc_amount = isnull(vip_cur_acc_amount,0) + @ln_vip_acc_amount,
	          com_flag='0',
	          cons_count = isnull(cons_count, 0) + 1
	          where card_id = @ls_vipNO and branch_no=left(@ls_branchno,2)
	   
	   end
	   else 
	       insert into t_rm_vip_consume(card_id,branch_no,vip_cons_amount,vip_cur_cons_amount,vip_acc_amount,vip_cur_acc_amount,cons_count)
	             values(@ls_vipNO, left(@ls_branchno,2),@ln_vip_amount,@ln_vip_amount,@ln_vip_acc_amount,@ln_vip_acc_amount,1)
	   
	   --  end
   
           --更新积分
           update t_rm_vip_info set acc_num = isnull(acc_num,0) + @ln_vip_acc_amount where card_id=@ls_vipNO
    
   
	   --记录第笔小票积分 wujs 2006-01-03
	   update t_rm_card_paylist set vip_acc = @ln_vip_acc_amount 
		where branch_no = substring(@ls_branchno,1,2) and card_type='1'
		  and card_id = @ls_vipNO and flow_no = @as_flowno
	
	   update mess_t_vip_message set acc_num = @ln_vip_acc_amount
		where branch_no = substring(@ls_branchno,1,2) and card_type='1'
		  and card_id = @ls_vipNO and flow_no = @as_flowno and send_flag = '0'
     end

   --@ln_vip_acc_amount
  
   select @li_limit_num = use_num from t_rm_vip_info where card_id=@ls_vipNO
   if @li_limit_num is null return
   
   
   if @li_limit_num >0 
   begin
      --更新使用次数
      select @li_limit_num =@li_limit_num - 1
      update t_rm_vip_info set use_num=convert(varchar(10),@li_limit_num) where card_id=@ls_vipNO
   end
end

--记录充值卡消费金额
if  not ((@ls_card_no is Null) or (@ls_card_no = ''))
begin
   --select top 1 @ln_total_amount = pay_amount, @ls_branchno = branch_no from t_rm_payflow where flow_no=@as_flowno
   --modified by wujs 2005-02-03  如果有多笔付款(多张充值卡,现金混合)
   declare ldc_card_pay cursor for 
       select card_no, (case sell_way when 'B' then - pay_amount else pay_amount end), branch_no from t_rm_payflow
            where flow_no=@as_flowno and pay_way = 'SAV'

   open ldc_card_pay

   fetch ldc_card_pay into @ls_card_no, @ln_total_amount, @ls_branchno
   while @@fetch_status=0
   begin
        if exists(select * from t_rm_vip_consume where card_id=@ls_card_no and branch_no=left(@ls_branchno,2))
        begin
             update t_rm_vip_consume set com_flag='0',
                                     cons_count = isnull(cons_count, 0) + 1,
                                     cons_amount = isnull(cons_amount,0) + @ln_total_amount
                               where card_id = @ls_card_no and branch_no=left(@ls_branchno,2)
   
        end
        else
             insert into t_rm_vip_consume(card_id,branch_no,vip_cons_amount,vip_cur_cons_amount,vip_acc_amount,vip_cur_acc_amount,cons_count,cons_amount)
             values(@ls_card_no, left(@ls_branchno,2),0,0,0,0,1,@ln_total_amount)
       
        if @@error <> 0
        begin
           close ldc_card_pay
           deallocate ldc_card_pay
           goto myerror
       end
        
       fetch ldc_card_pay into @ls_card_no, @ln_total_amount, @ls_branchno
   end
   close ldc_card_pay
   deallocate ldc_card_pay
end 
return

myerror:
/*
  close cur_vip
  deallocate cur_vip
  close cur_vip_item
  deallocate cur_vip_item
*/
  return
end
