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)
WITH ENCRYPTION
AS
  BEGIN
      DECLARE @ls_branchno            CHAR(6),
              @ls_itemno              CHAR(20),
              @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),  --本次记积分
              --只需要两位小数即可，以解决积分清零后结余积分会出现-0.00的积分的情况。add by wangyy  at 2014-03-19
              @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_spec_flag           CHAR(10),--特价类型
              @pos_vip_weekn_double   VARCHAR(10),---每星期几双倍积分 
              @ls_double              VARCHAR(1),
              @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_lessrate_flag       VARCHAR(10),
              @ln_percent             NUMERIC(6, 2),
              @ln_profitrate          NUMERIC(16, 2),
              @ln_multi_sd            NUMERIC(16, 4),-- 时段N倍积分
              @ls_cs                  VARCHAR(1),
              @ld_acc_rate            DECIMAL(10, 2),--会员积分比例
              @ls_birthflag           CHAR(1),---农历生日处理 tansp 20110517
              @ls_bth_flag            VARCHAR(10),---农历生日处理 tansp 20110517
              @pos_vip_auto_int       CHAR(1),--记会员积分自动抹零
              @pos_vip_auto_int_dec   INT,--积分四舍五入几位小数之后抹零
              @ln_vip_acc_amount_time NUMERIC(16, 4),--时段记积分
              @ls_clsno_time          CHAR(20),--时段积分类别
              @ls_temp                VARCHAR(20),
              @zs_amount              NUMERIC(16, 4),--赠送金额 20160825
              @ls_mobile              VARCHAR(20) -- 手机号
      DECLARE @ld_birthmonth      DECIMAL(10, 2),--会员生日月积分倍数
              @ld_daterange       DECIMAL(10, 2),--期间积分倍数
              @ls_daterange_s     VARCHAR(10),--期间开始日期
              @ls_daterange_e     VARCHAR(10),--期间结束日期
              @ls_birthmonth_flag CHAR(1),--会员生日月标志
              @ln_max_mult        DECIMAL(10, 2),--会员生日月标志
              @ls_max_mult        CHAR(1) --取最大的会员积分倍数 wangyy 2011-11-10
      DECLARE @li_flow      INT,
              @ls_allret    VARCHAR(2),
              @ls_retflowno VARCHAR(20)
      /*会员日相关变量*/
      DECLARE @vipday_mode  VARCHAR(10),
              @vipday_value VARCHAR(80),
              @vipday_acc   DECIMAL(10, 2),
              @oper_date    DATETIME
      --子卡积分母按比例增加积分 by WangC 2015.7.28  150722-103803
      DECLARE @ldec_child_jf  DECIMAL(6, 2),--子卡积分比例
              @ldec_father_jf DECIMAL(6, 2),--母卡积分比例
              @ls_father      VARCHAR(20),--母卡会员号
              @ldec_father    DECIMAL(10, 2) --母卡积分
      DECLARE @ln_max_mult_last DECIMAL(10, 2) --yejh 20151107 add 
      DECLARE @item_sup_flag CHAR(1),--商品经营方式  add by huangkl 2016-07-21
              @sup_ly_rate   NUMERIC(16, 4) --联营扣率
      DECLARE @posid    CHAR(4),--add by liuy 2017-11-16 判断是否是断网数据
              @li_row   INT,
              @ls_value CHAR(1)
	  --按付款方式积分 ligq 2017-12-05
	  DECLARE @lnway_addtotal_amount     NUMERIC(16, 4),--本单付款方式积分的金额  
	          @lnway_vip_bl              NUMERIC(16, 4),--本单付款方式积分比例
			  @count_pay                 INT

      SELECT @ls_double = '0'

      SELECT @ln_vip_amount = 0

      SELECT @ln_vip_acc_amount = 0

      SELECT @ln_multi_birthday = 0

      SELECT @ln_max_mult = 0

      SELECT @ln_vip_acc_amount_time = 0

      SELECT @li_flow = Charindex('|', @as_flowno, 1)

      SELECT @ls_allret = '0'
	  
	  SELECT @lnway_vip_bl = 1

      SELECT @count_pay = 0

      IF @li_flow > 0
        BEGIN
            SELECT @ls_allret = Substring(@as_flowno, @li_flow + 1, 1)

            SELECT @as_flowno = LEFT(@as_flowno, @li_flow - 1)
        END

	 --存在返利储值才执行 20201228
	if exists(select * from sysobjects where name='pr_rm_vip_fl')
		exec pr_rm_vip_fl @as_flowno ,@as_branch_no

      SELECT TOP 1 @ls_vipno = b.vip_no,
                   @ls_branchno = b.branch_no,
                   @oper_date = a.oper_date,
                   @posid = b.posid
      FROM   t_rm_saleflow a with(nolock),
             t_rm_payflow b  with(nolock)
      WHERE  a.flow_no = b.flow_no
             AND a.flow_no = @as_flowno
             AND b.branch_no = @as_branch_no
             AND Charindex('一卡通', Isnull(b.memo, '')) <= 0
      
     IF ( ( @ls_vipno IS NULL ) OR ( @ls_vipno = '' ) )    RETURN    --add by yanghc 2019-05-23  

      -- add by liuy 2017-11-16
      IF ( @posid != ''
            OR @posid IS NOT NULL )
        BEGIN
            SELECT @ls_value = sys_var_value
            FROM   t_sys_system
            WHERE  sys_var_id = 'online_vip' --是否启用 在线会员

            IF @ls_value = '1'
              BEGIN
                  SELECT @li_row = Count(*)
                  FROM   t_rm_vip_info
                  WHERE  card_id = @ls_vipno;

                  IF @li_row = 0
                    BEGIN
                        SET @ls_mobile = @ls_vipno

                        SELECT TOP 1 @ls_vipno = card_id
                        FROM   t_rm_vip_info with(nolock)
                        WHERE  mobile = @ls_vipno
                               AND card_status = '0'

                        SET @li_row = 0

                        SELECT @li_row = Count(*)
                        FROM   t_rm_vip_info with(nolock)
                        WHERE  mobile = @ls_mobile

                        IF @li_row <> 0
                          BEGIN
                              UPDATE t_rm_card_paylist
                              SET    card_id = @ls_vipno
                              WHERE  flow_no = @as_flowno
                                     AND branch_no = LEFT(@as_branch_no, 2)
                                     AND card_id = @ls_mobile
                          END
                    END
              END
        END

      SELECT TOP 1 @ls_card_no = card_no
      FROM   t_rm_payflow  with(nolock)
      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

      IF @ls_allret = '1'--整单退货
        BEGIN
            SELECT TOP 1 @ls_retflowno = voucher_no
            FROM   t_rm_payflow  with(nolock)
            WHERE  flow_no = @as_flowno
                   AND branch_no = @as_branch_no
                   AND Isnull(voucher_no, '') <> ''

            IF Isnull(@ls_retflowno, '') = ''
              RETURN
                SELECT TOP 1 @ln_vip_acc_amount = ( -1 * vip_acc ),
                             @ln_vip_amount = ( -1 * amount )
                FROM   t_rm_card_paylist  with(nolock)
                WHERE  branch_no = Substring(@ls_branchno, 1, 2)
                       AND card_type = '1'
                       AND card_id = @ls_vipno
                       AND flow_no = @ls_retflowno

            IF EXISTS(SELECT *
                      FROM   t_rm_vip_consume  with(nolock)
                      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_date = Getdate(),
                         modify_date = Getdate(),
                         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,
                           cons_date,
                           modify_date)
              VALUES     (@ls_vipno,
                          LEFT(@ls_branchno, 2),
                          @ln_vip_amount,
                          @ln_vip_amount,
                          @ln_vip_acc_amount,
                          @ln_vip_acc_amount,
                          1,
                          Getdate(),
                          Getdate())

            --更新积分
            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

            --增加记录对应母卡的积分 by WangC 2015.7.28
            SELECT @ls_father = Isnull(father_card, '')
            FROM   t_rm_vip_info
            WHERE  card_id = @ls_vipno

            IF Len(@ls_father) > 0
               AND @ln_vip_acc_amount <> 0
              BEGIN
                  SELECT @ldec_child_jf = CONVERT(DECIMAL(6, 2), Isnull(sys_var_value, '0'))
                  FROM   t_sys_system  with(nolock)
                  WHERE  sys_var_id = 'pos_child_jf'

                  SELECT @ldec_father_jf = CONVERT(DECIMAL(6, 2), Isnull(sys_var_value, '0'))
                  FROM   t_sys_system  with(nolock)
                  WHERE  sys_var_id = 'pos_father_jf'

                  IF @ldec_child_jf > 0
                     AND @ldec_father_jf > 0
                    BEGIN
                        SET @ldec_father = Round(@ln_vip_acc_amount * @ldec_father_jf / @ldec_child_jf, 2)

                        IF @ls_pos_acc_int = '1'
                          BEGIN
                              SELECT @ls_temp = sys_var_value
                              FROM   t_sys_system  with(nolock)
                              WHERE  Lower(sys_var_id) = 'acc_round'

                              IF @ls_temp IS NULL
                                SELECT @ls_temp = '1' --默认为向下取整

                              IF @ls_temp = '1'
                                BEGIN
                                    --向下取整
                                    IF @ldec_father > 0
                                      SELECT @ldec_father = Floor(@ldec_father)
                                    ELSE
                                      SELECT @ldec_father = -Floor(Abs(@ldec_father))
                                END
                              ELSE IF @ls_temp = '2'
                                BEGIN
                                    --向上取整
                                    IF @ldec_father > 0
                                      SELECT @ldec_father = Ceiling(@ldec_father)
                                    ELSE
                                      SELECT @ldec_father = -Ceiling(Abs(@ldec_father))
                                END
                              ELSE
                                BEGIN
                                    --四舍五入
                                    IF @ldec_father > 0
                                      SELECT @ldec_father = Round(@ldec_father, 0)
                                    ELSE
                                      SELECT @ldec_father = -Round(Abs(@ldec_father), 0)
                                END
                          END

                        IF @ldec_father <> 0
                          BEGIN
                              IF EXISTS(SELECT *
                                        FROM   t_rm_vip_consume  with(nolock)
                                        WHERE  card_id = @ls_father
                                               AND branch_no = LEFT(@ls_branchno, 2))
                                BEGIN
                                    UPDATE t_rm_vip_consume
                                    SET    vip_acc_amount = Isnull(vip_acc_amount, 0) + @ldec_father,
                                           vip_cur_acc_amount = Isnull(vip_cur_acc_amount, 0) + @ldec_father,
                                           com_flag = '0',
                                           cons_date = Getdate(),
                                           modify_date = Getdate(),
                                           cons_count = Isnull(cons_count, 0) + 1
                                    WHERE  card_id = @ls_father
                                           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_father,
                                            LEFT(@ls_branchno, 2),
                                            0,
                                            0,
                                            @ldec_father,
                                            @ldec_father,
                                            1,
                                            Getdate(),
                                            Getdate())

                              INSERT t_rm_card_paylist
                       
              						 (flow_no,
                                      card_type,
                                      card_id,
                                      amount,
                                      pay_time,
                                      other1,
                                      other2,
                                      branch_no,
                                      residual_amt,
                                      com_flag,
                                      vip_acc)
                              VALUES (@as_flowno,
                                      '1',
                                      @ls_father,
                                      0,
                                      Getdate(),
                                      '子卡消费积分',
                                      @ls_vipno,
                                      Substring(@ls_branchno, 1, 2),
                                      0,
                                      '0',
                                      @ldec_father)

                              UPDATE t_rm_vip_info
                              SET    acc_num = Isnull(acc_num, 0) + @ldec_father
                              WHERE  card_id = @ls_father
                          END
                    END
              END

            ---yejh 20170320 update  t_rm_card_paylist  时已经触发不再更新  
	        /*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'
	        */
            --@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

            --记录充值卡消费金额
            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  with(nolock)
                    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  with(nolock)
                                  WHERE  card_id = @ls_card_no
                                         AND branch_no = LEFT(@ls_branchno, 2))
                          BEGIN
                              UPDATE t_rm_vip_consume
                              SET    com_flag = '0',
                                     cons_date = Getdate(),
                                     modify_date = Getdate(),
                                     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,
                                       cons_date,
                                       modify_date)
                          VALUES     (@ls_card_no,
                                      LEFT(@ls_branchno, 2),
                                      0,
                                      0,
                                      0,
                                      0,
                                      1,
                                      @ln_total_amount,
                                      Getdate(),
                                      Getdate())

                        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
        END

      --zhangyf付款方式为GIF的小票不积分，返回（只有是赠送商品产生的付款单不参与积分，如果是促销导致的GIF还是需要积分的）
      --if exists(select 1 from t_rm_saleflow a,t_rm_payflow b where (b.pay_way='GIF' and b.sell_way = 'C') and a.flow_no=b.flow_no and a.flow_no=@as_flowno and b.branch_no = @as_branch_no) return
      --wangjj 20160819 (整笔单为赠送不积分,一单中有销售,有赠送,销售部分积分(包含促销导致的GIF,C 销售部分积分)) 160727-494295
      IF NOT EXISTS(SELECT 1
                    FROM   t_rm_saleflow a  with(nolock),
                           t_rm_payflow b  with(nolock)
                    WHERE  ( b.pay_way <> 'GIF'
                             AND b.sell_way <> 'C' )
                           AND a.flow_no = b.flow_no
                           AND a.flow_no = @as_flowno
                           AND b.branch_no = @as_branch_no)
        RETURN

      --宽广特殊需求，付款方式为LGK或SAV的小票不积分，返回
      IF (SELECT sys_var_value
          FROM   t_sys_system  with(nolock)
          WHERE  sys_var_id = 'pos_use_bank_card') = '1'
        BEGIN
            IF EXISTS(SELECT 1
                      FROM   t_rm_saleflow a  with(nolock), 
                             t_rm_payflow b  with(nolock)
                      WHERE  ( b.pay_way = 'LGK'
                                OR b.pay_way = 'SAV' )
                             AND a.flow_no = b.flow_no
                             AND a.flow_no = @as_flowno
                             AND b.branch_no = @as_branch_no)
              RETURN
        END

      --有储值卡付款的小票不参与积分
      IF (SELECT sys_var_value
          FROM   t_sys_system with(nolock)
          WHERE  sys_var_id = 'pos_use_sav_noacc') = '1'
        BEGIN
            IF EXISTS(SELECT 1
                      FROM   t_rm_saleflow a with(nolock),
                             t_rm_payflow b with(nolock)
                      WHERE  b.pay_way = 'SAV'
                             AND a.flow_no = b.flow_no
                             AND a.flow_no = @as_flowno
                             AND b.branch_no = @as_branch_no)
              RETURN
        END

      --使用代金券付款的小票不参与积分
      IF (SELECT sys_var_value
          FROM   t_sys_system with(nolock)
          WHERE  sys_var_id = 'pos_use_shp_noacc') = '1'
        BEGIN
            IF EXISTS(SELECT 1
                      FROM   t_rm_saleflow a with(nolock),
                             t_rm_payflow b with(nolock)
                      WHERE  b.pay_way = 'SHP'
                             AND a.flow_no = b.flow_no
                          
   							 AND a.flow_no = @as_flowno
                             AND b.branch_no = @as_branch_no)
              RETURN
        END

	  --记会员积分自动抹零
      SELECT @pos_vip_auto_int = sys_var_value
      FROM   t_sys_system with(nolock)
      WHERE  sys_var_id = 'pos_vip_auto_int'
	  --四舍五入N位小数后抹零
      SELECT @pos_vip_auto_int_dec = CONVERT(INT, sys_var_value)
      FROM   t_sys_system with(nolock)
      WHERE  sys_var_id = 'pos_vip_auto_int_dec'

      -- modified by wujs 2005-01-14 会员卡所属类型是否积分 acc_flag 是否积分标志
      IF EXISTS (SELECT card_id
                 FROM   t_rm_vip_info  with(nolock)
                 WHERE  card_id = @ls_vipno
                        AND  exists (SELECT *
                                          FROM   t_rm_vip_type
                                          WHERE  acc_flag = '0'
												and type_id=t_rm_vip_info.card_type))
        SELECT @ls_record_acc = '1' --当前会员记录积分 
      ELSE
        SELECT @ls_record_acc = '0' --当前会员不记录积分 
		
	   --按消费次数积分，不考虑付款方式是否积分 ligq 2017-12-05
      SELECT @ls_consume_type = sys_var_value
      FROM   t_sys_system   with(nolock)
      WHERE  Lower(sys_var_id) = 'pos_consume_type'
	  
      SELECT @ls_pos_record_vip = sys_var_value
      FROM   t_sys_system  with(nolock)
      WHERE  Lower(sys_var_id) = 'pos_record_vip'

      --mahw 不积分也要减使用次数
      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   with(nolock)
                      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   with(nolock)
            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_consume_type = sys_var_value
          FROM   t_sys_system   with(nolock)
          WHERE  Lower(sys_var_id) = 'pos_consume_type'

      SELECT @ls_acc_flag = sys_var_value
      FROM   t_sys_system   with(nolock)
      WHERE  Lower(sys_var_id) = 'pos_vipitem_point'

      SELECT @li_clslen = sys_var_value
      FROM   t_sys_system   with(nolock)
      WHERE  sys_var_id = 'gl_itm_cls_nolen'

      SELECT @ls_max_mult = sys_var_value
      FROM   t_sys_system   with(nolock)
      WHERE  sys_var_id = 'pos_vip_max_mult'

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

      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   with(nolock)
      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

      -- 会员记录消费金额
      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   with(nolock)
                      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   with(nolock)
            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

      -- 记录会员积分
      --会员生日N倍积分 liug 2009-09-19
      --农历生日处理 tansp 2011-05-17
      SELECT @ls_birthflag = '0'

      SELECT @ls_birthmonth_flag = '0'

      SELECT @ls_bth_flag = bth_flag,
             @ls_vip_birthday = RIGHT(CONVERT(CHAR(10), birthday, 121), 5)
      FROM   t_rm_vip_info with(nolock)
      WHERE  card_id = @ls_vipno

      IF @ls_bth_flag = '农历'
        BEGIN
            IF RIGHT(@as_lunar_date, 5) = @ls_vip_birthday
              SELECT @ls_birthflag = '1'

            --判断是不是农历生日的那一个月  add by wangyy at 2011-07-27
            IF LEFT(RIGHT(@as_lunar_date, 5), 2) = LEFT(@ls_vip_birthday, 2)
               AND EXISTS (SELECT 1
                           FROM   t_sys_system   with(nolock)
                           WHERE  sys_var_id = 'pos_vip_birthmonth'
                                  AND sys_var_value = '1')
              SET @ls_birthmonth_flag = '1'
        END
      ELSE
        BEGIN
            IF RIGHT(CONVERT(CHAR(10), Getdate(), 121), 5) = @ls_vip_birthday
              SELECT @ls_birthflag = '1'

            --判断是不是公历生日的那一个月  add by wangyy at 2011-07-27
            IF LEFT(RIGHT(CONVERT(CHAR(10), Getdate(), 121), 5), 2) = LEFT(@ls_vip_birthday, 2)
               AND EXISTS (SELECT 1
                           FROM   t_sys_system   with(nolock)
  
                         WHERE  sys_var_id = 'pos_vip_birthmonth'
                                  AND sys_var_value = '1')
              SET @ls_birthmonth_flag = '1'
        END

      IF @ls_birthflag = '1'
        --select @ls_vip_birthday = right(convert(char(10), birthday, 121), 5) from t_rm_vip_info where card_id = @ls_vipno
        --if right(convert(char(10), getdate(), 121), 5) = @ls_vip_birthday
        BEGIN
            SELECT @ls_multi_birthday = sys_var_value
            FROM   t_sys_system   with(nolock)
            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倍积分
                  --SELECT @ln_vip_acc_amount = @ln_vip_acc_amount * @ln_multi_birthday 
                  SELECT @ln_max_mult = @ln_multi_birthday

                  SELECT @ls_double = '1'
              END
        END

      --如果没有生日N倍积分，则考虑生日当月N倍积分
      IF @ls_double <> '1'
          OR @ls_max_mult = '1'
        BEGIN
            IF @ls_birthmonth_flag = '1'
              BEGIN
                  SELECT @ld_birthmonth = Cast(Isnull(sys_var_value, '2') AS INT)
                  FROM   t_sys_system   with(nolock)
                  WHERE  Lower(sys_var_id) = 'pos_vip_birthmonth_v'

                  IF @ld_birthmonth IS NULL
                    SET @ld_birthmonth = 2

                  --SELECT @ln_vip_acc_amount = @ln_vip_acc_amount * @ld_birthmonth --N倍积分
                  IF @ln_max_mult < @ld_birthmonth
                    SET @ln_max_mult = @ld_birthmonth

                  SELECT @ls_double = '1'
              END
        END

      --是否N倍积分日 fufl 09-09-22
      --当天是生日N倍积分时，就不再做每月第几日N倍积分操作
      IF @ls_double <> '1'
          OR @ls_max_mult = '1'
        BEGIN
            SELECT @pos_vip_multiple = sys_var_value
            FROM   t_sys_system   with(nolock)
            WHERE  Lower(sys_var_id) = 'pos_vip_multiple'

            SELECT @pos_vip_multiple = Isnull(@pos_vip_multiple, '0/1')

            --N倍积分 liug20090919,N[2,20]
            SELECT @ln_multi_month = Cast(Isnull(sys_var_value, '2') AS INT)
            FROM   t_sys_system  with(nolock)
            WHERE  Lower(sys_var_id) = 'pos_vip_multi_month'

            IF @ln_multi_month IS NULL
              SELECT @ln_multi_month = 2

            IF ( Substring(@pos_vip_multiple, 1, 1) = '1'
                 AND Len(@pos_vip_multiple) > 2 )
              BEGIN
                  IF ( Datepart(dd, Getdate()) = CONVERT(INT, Substring(@pos_vip_multiple, 3, Len(@pos_vip_multiple) - 2)) )
                    BEGIN
                        --SELECT @ln_vip_acc_amount = @ln_vip_acc_amount * @ln_multi_month --N倍积分
                        IF @ln_max_mult < @ln_multi_month
                          SET @ln_max_mult = @ln_multi_month

                        SELECT @ls_double = '1'
                    END
              END
        END

      --每星期几N倍积分日 fufl 09-09-22
      --当天是生日N倍积分或每月第几日N倍积分日时，就不再做每星期几双倍积分操作
      IF @ls_double <> '1'
          OR @ls_max_mult = '1'
        BEGIN
            SELECT @pos_vip_weekn_double = sys_var_value
            FROM   t_sys_system  with(nolock)
            WHERE  Lower(sys_var_id) = 'pos_vip_weekn_double'

            SELECT @pos_vip_weekn_double = Isnull(@pos_vip_weekn_double, '0/')

            --N倍积分 liug20090919,N[2,20]
            SELECT @ln_multi_week = Cast(Isnull(sys_var_value, '2') AS INT)
            FROM   t_sys_system  with(nolock)
            WHERE  Lower(sys_var_id) = 'pos_vip_multi_week'

            IF @ln_multi_week IS NULL
              SELECT @ln_multi_week = 2

            IF ( Substring(@pos_vip_weekn_double, 1, 1) = '1'
          
       AND Len(@pos_vip_weekn_double) > 2 )
              BEGIN
                  IF ( Datename(dw, Getdate()) = Substring(@pos_vip_weekn_double, 3, Len(@pos_vip_weekn_double) - 2) )
                    BEGIN
                        --SELECT @ln_vip_acc_amount = @ln_vip_acc_amount * @ln_multi_week --N倍积分
                        IF @ln_max_mult < @ln_multi_week
                          SET @ln_max_mult = @ln_multi_week

                        SELECT @ls_double = '1'
                    END
              END
        END

      --期间积分 如前面N倍积分都无效，则考虑期间积分 add by wangyy at 2011-07-27
      IF @ls_double <> '1'
          OR @ls_max_mult = '1'
        BEGIN
            IF EXISTS (SELECT 1
                       FROM   t_sys_system  with(nolock)
                       WHERE  sys_var_id = 'pos_vip_date_range'
                              AND sys_var_value = '1')
              BEGIN
                  SELECT @ls_daterange_s = sys_var_value
                  FROM   t_sys_system  with(nolock)
                  WHERE  sys_var_id = 'pos_vip_date_range_s'

                  SELECT @ls_daterange_e = sys_var_value
                  FROM   t_sys_system  with(nolock)
                  WHERE  sys_var_id = 'pos_vip_date_range_e'

                  IF CONVERT(CHAR(10), Getdate(), 120) BETWEEN @ls_daterange_s AND @ls_daterange_e
                    BEGIN
                        SELECT @ld_daterange = Cast(sys_var_value AS INT)
                        FROM   t_sys_system  with(nolock)
                        WHERE  sys_var_id = 'pos_vip_date_range_v'

                        IF @ld_daterange IS NULL
                            OR @ld_daterange <= 0
                          SET @ld_daterange = 2

                        --set @ln_vip_acc_amount = @ln_vip_acc_amount * @ld_daterange
                        IF @ln_max_mult < @ld_daterange
                          SET @ln_max_mult = @ld_daterange

                        SET @ls_double = '1'
                    END
              END
        END

      IF @ln_max_mult IS NULL
          OR @ln_max_mult = 0
        SET @ln_max_mult = 1

      --Add by Dengzw 2014.09.05会员日积分处理开始
      SELECT @vipday_mode = sys_var_value
      FROM   t_sys_system  with(nolock)
      WHERE  sys_var_id = 'vipday_mode'

      SELECT @vipday_value = sys_var_value
      FROM   t_sys_system  with(nolock)
      WHERE  sys_var_id = 'vipday_value'

      DECLARE @vipday_acc2 VARCHAR(80)

      SELECT @vipday_acc2 = sys_var_value
      FROM   t_sys_system  with(nolock)
      WHERE  sys_var_id = 'vipday_acc'

      IF ( Isnumeric(@vipday_acc2) = 0 )
        SET @vipday_acc2 = '1'

      SELECT @vipday_acc = Cast(Isnull(@vipday_acc2, 1) AS DECIMAL(10, 2))

      /*设置会员日默认值*/
      IF ( Isnull(@vipday_acc, 1) <= 1 )
        SET @vipday_acc = 1

      DECLARE @i        INT,
              @isvipday CHAR(1)

      /*按日期设置的会员日*/
      SET @i = 1
      SET @isvipday = '0'

      WHILE ( @i < Len(@vipday_value) )
        BEGIN
            IF CONVERT(INT, Substring(@vipday_value, @i, 2)) = Datepart(DD, @oper_date)
               AND @vipday_mode = '1'
              SET @isvipday = '1'

            IF ( CONVERT(INT, Substring(@vipday_value, @i, 2)) = Datepart(w, @oper_date) - 1 )
               AND @vipday_mode = '2'
              SET @isvipday = '1'

            SET @i = @i + 2
        END

      /*未开启会员日或当天不是会员日，则积分倍数为1倍*/
      IF ( @isvipday = '0' )
        SET @vipday_acc = 1

      IF @ln_max_mult < @vipday_acc
        SET @ln_max_mult = @vipday_acc
	  
	    --开启会员按付款方式积分
	  DECLARE @payway_integral CHAR(1)
	  
	  SELECT @payway_integral = isnull(sys_var_value,'0')
      FROM   t_sys_system  with(nolock)
      WHERE  sys_var_id = 'payway_integral'
	  
      --会员日积分处理完成
      SET @ln_max_mult_last = Isnull(@ln_max_mult, 1) --yejh 20151107 add 
      SET @ln_max_mult=1 --yejh 20151107 add 
      IF ( @ls_pos_record_vip = '1' )
         AND ( @ls_record_acc = '1' )
         AND NOT ( ( @ls_vipno IS NULL )
                    OR ( @ls_vipno = '' ) )
        BEGIN
			--ligq 2017-12-05 按付款方式积分
			IF @ls_consume_type <> '2' and @payway_integral= '1'
              BEGIN
                  --消费金额
                  SELECT TOP 1 @ls_saleway = sell_way,
                               @ln_total_amount = sale_amount,
                               @ls_branchno = branch_no
                  FROM   t_rm_payflow with(nolock)
                  WHERE  flow_no = @as_flowno
                         AND pay_way <> 'CHG'
                         AND branch_no = @as_branch_no

                  --积分的消费金额
                  --销售方式为退货and外送零钱的时候付款金额为负数 add by tianmin 20160523
                  SELECT @lnway_addtotal_amount = Isnull(Sum(a.coin_rate * ( CASE
                                                                               WHEN ( a.sell_way = 'D'
                                                                                       OR ( a.sell_way = 'B'
                                                                                            AND a.memo = '外送零钱' ) ) THEN -a.pay_amount
                                                                               ELSE a.pay_amount
                                                                             END )), 0)
                  FROM   t_rm_payflow a with(nolock)
                         LEFT JOIN t_bd_payment_info with(nolock)
                                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'

                  --tianmin 20170504 抹零不计算在内
                  SELECT @count_pay = Count(*)
                  FROM   t_rm_payflow with(nolock)
                  WHERE  flow_no = @as_flowno
                         AND pay_way <> 'CHG'

                  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 with(nolock)
                  WHERE  Lower(sys_var_id) = 'pos_bytiem_limit'

                  SELECT @ln_bytiem_score = CONVERT(NUMERIC(16, 4), sys_var_value)
                  FROM   t_sys_system with(nolock)
                  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 with(nolock)
                  WHERE  flow_no = @as_flowno
                         AND pay_way <> 'CHG'
                         AND branch_no = @as_branch_no

                  --begin add by wangjj 20160825 160727-494295 赠送部分金额 不算积分
                  SET @zs_amount = 0.00

                  SELECT TOP 1 @zs_amount = pay_amount
                  FROM   t_rm_payflow with(nolock)
                  WHERE  flow_no = @as_flowno
                         AND pay_way = 'GIF'
                         AND sell_way = 'C'
                         AND branch_no = @as_branch_no

                  SET @ln_total_amount = @ln_total_amount - Isnull(@zs_amount, 0.00)

                  --end add by wangjj 20160825 160727-494295 赠送部分金额 不算积分
                  IF @ln_total_amount > @ln_bytiem_limit
                    BEGIN
                        --必须要初始化@ln_multi_sd，否则，当下的面SQL语句查不到值的时候，@ln_multi_sd还是取上一商品的@ln_multi_sd。edit by wangyy at 2014-02-18
                        SELECT @ln_multi_sd = 1

                        -- end edit wangyy
                        --记录时段积分
                        SELECT TOP 1 @ln_multi_sd = d.num1
                        FROM   t_pc_price_flow_master m with(nolock),
                               t_pc_price_flow_detail d with(nolock)
                        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
                               AND ( d.item_no = 'JF000'
                                      OR d.item_no = 'ALL' )
                        ORDER  BY work_date DESC

                        IF @ln_multi_sd IS NULL
                          SET @ln_multi_sd = 1

                        --时段积分倍数不参与最大倍数优先。故屏蔽之。edit by wangyy at 2014-02-12 
                        --if @ln_multi_sd > @ln_max_mult
                        -- begin
                        --select @ln_vip_acc_amount_time = @ln_vip_acc_amount * @ln_multi_sd
                        --  set @ln_max_mult = @ln_multi_sd
                        --end
                        IF @ls_saleWay = 'B'
                          BEGIN
                              SELECT @ln_vip_amount = -@ln_total_amount

                              SELECT @ln_vip_acc_amount = -@ln_bytiem_score * @ln_max_mult
                          END
                        ELSE
                          BEGIN
                              SELECT @ln_vip_amount = @ln_total_amount

                              SELECT @ln_vip_acc_amount = @ln_bytiem_score * @ln_max_mult
                          END
                    END
              ---- 按次数记积分 end --------------------------------
              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 with(nolock)
                    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
                        IF @ls_acc_flag = '1' -- 增加单品是否积分 add by jincm 2005-01-24   
                          BEGIN
                              SELECT @ls_itemvip_flag = vip_acc_flag
                              FROM   t_bd_item_info with(nolock)
                              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_saleWay = 'C' --wangjj 20160819 160727-494295
                          BEGIN
                              FETCH cur_vip INTO @ls_branchno, @ls_itemno, @ln_saleQty, @ln_sourcePrc, @ln_salePrc, @ls_saleWay, @ls_spec_flag

                              CONTINUE
                          END

                        IF @ls_lessrate_flag = '1' --当商品档案毛利率<X %时不积分add by fufl20100115 
                          BEGIN
                              SELECT @item_sup_flag = item_sup_flag,
                                     @sup_ly_rate = sup_ly_rate,
                                     @ln_profitrate = Round(( CASE sale_price
                                                                WHEN 0 THEN 0
                                                                ELSE ( sale_price - price ) / sale_price
                                                              END ), 4) * 100
                              FROM   t_bd_item_info with(nolock)
                              WHERE  item_no = @ls_itemno

                              IF @item_sup_flag = 'C' --联营商品的毛利率以联营扣率替代判断 add by huangkl 2016-07-21
                                SELECT @ln_profitrate = @sup_ly_rate * 100

                              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 with(nolock)
                        WHERE  item_no = @ls_itemno

                        SELECT @ln_scale = NULL

                        WHILE ( Len(@ls_clsno) >= @li_clslen )
                          BEGIN
                              IF EXISTS (SELECT vip_amt
                                         FROM   t_rm_vip_set with(nolock)
                                         WHERE  vip_item_cls = @ls_clsno)
                                BEGIN
                                    SELECT @ln_scale = vip_amt
                                    FROM   t_rm_vip_set with(nolock)
                                    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 with(nolock)
                        WHERE  sys_var_id = 'pos_vip_policy'

                        SELECT @ls_cs = sys_var_value
                        FROM   t_sys_system with(nolock)
                        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 = 'pe'
                            OR @ls_spec_flag = 'pc'
                            OR @ls_spec_flag = 'pcb'
                     
       						OR @ls_spec_flag = 'pd'
                            OR @ls_spec_flag = 'pdb'
                            OR @ls_spec_flag = 'dm'
                            OR @ls_spec_flag = 'py'
                            OR @ls_spec_flag = 'pg'
                            OR @ls_spec_flag = 'pm'
                            OR @ls_spec_flag = 'lf'
                            OR @ls_spec_flag = 'ph'
                            OR @ls_spec_flag = 'ep'
							OR @ls_spec_flag = 'pr'
							OR @ls_spec_flag = 'gp'   --20190722 zengfy 条件组合促销也算
							OR @ls_spec_flag = 'pz'   --20200106 liushuai1 买满直减（单据）促销也算
							OR CHARINDEX('^',@ls_spec_flag )>0 --方案促销中的促销单据都是^开头
                          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 with(nolock)
                              WHERE  sys_var_id = 'pos_vipprice_point'

                              --如果数据库中不存在此记录，则默认为不记积分 add by wangyy at 2013-01-09
                              IF @ls_vip_price IS NULL
                                  OR Rtrim(@ls_vip_price) = ''
                                SELECT @ls_vip_price = '0'

                              -- 默认为积分
                              -- 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 with(nolock)
                              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 with(nolock)
                                                     WHERE  branch_no = (SELECT sys_var_value
                                                                         FROM   t_sys_system with(nolock)
     
                                                                    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 with(nolock)
                              WHERE  sys_var_id = 'pos_vip_spec_price'

                              --如果数据库中不存在此记录，则默认为不记积分 add by wangyy at 2013-01-09
                              IF @ls_vip_spce_price IS NULL
                                  OR Rtrim(@ls_vip_spce_price) = ''
                                SELECT @ls_vip_spce_price = '0'

                              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时表示该类别不积分
                          SELECT @ln_scale = CONVERT(NUMERIC(16, 4), sys_var_value)
                          FROM   t_sys_system with(nolock)
                          WHERE  Lower(sys_var_id) = 'pos_consume_score'

                        SELECT @ls_clsno = item_clsno
                        FROM   t_bd_item_info with(nolock)
                        WHERE  item_no = @ls_itemno

                        --必须要初始化@ln_multi_sd，否则，当下的面SQL语句查不到值的时候，@ln_multi_sd还是取上一商品的@ln_multi_sd。edit by wangyy at 2014-02-18
                        SELECT @ln_multi_sd = 1

                        -- end edit wangyy
                        SELECT TOP 1 @ln_multi_sd = d.num1
                        FROM   t_pc_price_flow_master m with(nolock),
                               t_pc_price_flow_detail d with(nolock)
                        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
                               AND ( ( @ls_clsno LIKE Rtrim(item_no) + '%' )
                                      OR item_no = 'ALL' )
                        ORDER  BY work_date DESC

                        IF @ln_multi_sd IS NULL
                          SET @ln_multi_sd = 1

                        IF @ln_scale > 0
                          BEGIN
                              IF @ls_saleWay = 'B'
                                BEGIN
                                    --select @ln_vip_acc_amount = @ln_vip_acc_amount - @ln_nativeamt/@ln_scale
                                    --select @ln_vip_amount = @ln_vip_amount - @ln_nativeamt 
                                    --记录时段积分  add by zhoucc 2012-10-10
                                    IF @ls_max_mult = '1'
                                      BEGIN
                                          IF @ln_multi_sd > @ln_max_mult
                                            BEGIN
                                                IF @lnway_vip_bl = 1 --ligq 2017-10-26													
													SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount - ( @ln_nativeamt / @ln_scale * @ln_multi_sd ) * @lnway_vip_bl
												ELSE
													SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount - ( @ln_nativeamt / @ln_scale * @ln_multi_sd ) * ( @lnway_addtotal_amount / @ln_total_amount )

                                                SELECT @ln_vip_amount = @ln_vip_amount - @ln_nativeamt
                                            END
                                          ELSE
                                            BEGIN
                                                	
												IF @lnway_vip_bl = 1 --ligq 2017-10-26
													SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount - ( @ln_nativeamt / @ln_scale * @ln_max_mult ) * @lnway_vip_bl
												ELSE
													SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount - ( @ln_nativeamt / @ln_scale * @ln_max_mult ) * ( @lnway_addtotal_amount / @ln_total_amount )

                                                SELECT @ln_vip_amount = @ln_vip_amount - @ln_nativeamt
                                            END
                                      END
                                    ELSE
                                      BEGIN
                                          --如果没有设置时段积分倍数，则取原来的倍数
                                          --if @ln_multi_sd = 0 select @ln_multi_sd = @ln_max_mult
                                          --前面已经给@ln_multi_sd赋了初始值1了，所以此处判断有没有时段积分倍数时应该判断@ln_multi_sd的值是否为1才对。edit by wangyy at 2014-07-08
                                          IF @ln_multi_sd = 1
                                            SELECT @ln_multi_sd = @ln_max_mult

                                           IF @lnway_vip_bl = 1 --ligq 2017-10-26
											SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount - ( @ln_nativeamt / @ln_scale * @ln_multi_sd ) * @lnway_vip_bl 
										  ELSE
											SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount - ( @ln_nativeamt / @ln_scale * @ln_multi_sd ) * ( @lnway_addtotal_amount / @ln_total_amount )

                                          SELECT @ln_vip_amount = @ln_vip_amount - @ln_nativeamt
                                      END
                                END
                              ELSE
                                BEGIN
                                    IF @ls_max_mult = '1'
                                      BEGIN
                                          IF @ln_multi_sd > @ln_max_mult
                                            BEGIN
                                                IF @lnway_vip_bl = 1 --ligq 2017-10-26
													SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount + ( @ln_nativeamt / @ln_scale * @ln_multi_sd ) * @lnway_vip_bl 
												ELSE
													SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount + ( @ln_nativeamt / @ln_scale * @ln_multi_sd ) * ( @lnway_addtotal_amount / @ln_total_amount )

                                                SELECT @ln_vip_amount = @ln_vip_amount + @ln_nativeamt
                                            END
                                          ELSE
                                            BEGIN
                                                IF @lnway_vip_bl = 1 --ligq 2017-10-26
													SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount + ( @ln_nativeamt / @ln_scale * @ln_max_mult ) * @lnway_vip_bl 
												ELSE
													SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount + ( @ln_nativeamt / @ln_scale * @ln_max_mult ) * ( @lnway_addtotal_amount / @ln_total_amount )

                                                SELECT @ln_vip_amount = @ln_vip_amount + @ln_nativeamt
                                            END
                                      END
                                    ELSE
                                      BEGIN
                                          --如果没有设置时段积分倍数，则取原来的倍数
                                          --if @ln_multi_sd = 0 select @ln_multi_sd = @ln_max_mult
                                          --前面已经给@ln_multi_sd赋了初始值1了，所以此处判断有没有时段积分倍数时应该判断@ln_multi_sd的值是否为1才对。edit by wangyy at 2014-07-08
                                          IF @ln_multi_sd = 1
                                            SELECT @ln_multi_sd = @ln_max_mult

                                          IF @lnway_vip_bl = 1 --ligq 2017-10-26
											SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount + ( @ln_nativeamt / @ln_scale * @ln_multi_sd ) * @lnway_vip_bl
										  ELSE
											SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount + ( @ln_nativeamt / @ln_scale * @ln_multi_sd ) * ( @lnway_addtotal_amount / @ln_total_amount )

                                          SELECT @ln_vip_amount = @ln_vip_amount + @ln_nativeamt
                                      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 ------------------------------
              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 with(nolock)
                    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
                        FROM   t_bd_item_info with(nolock)
                        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 @item_sup_flag = item_sup_flag,
                                     @sup_ly_rate = sup_ly_rate,
                                     @ln_profitrate = Round(( CASE sale_price
                                                                WHEN 0 THEN 0
                                                                ELSE ( sale_price - price ) / sale_price
                                                              END ), 4) * 100
                              FROM   t_bd_item_info with(nolock)
                              WHERE  item_no = @ls_itemno

                              IF @item_sup_flag = 'C' --联营商品的毛利率以联营扣率替代判断 add by huangkl 2016-07-21
                                SELECT @ln_profitrate = @sup_ly_rate * 100

                              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

                        IF @ls_saleWay = 'C' --wangjj 20160819 160727-494295
                          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 jincm 小类可以积分，有大类和小类时以小类为准
                        SELECT @ls_clsno = item_clsno
                        FROM   t_bd_item_info with(nolock)
                        WHERE  item_no = @ls_itemno

                        WHILE ( Len(@ls_clsno) >= @li_clslen )
                          BEGIN
                              IF EXISTS (SELECT vip_amt
                                         FROM   t_rm_vip_set with(nolock)
                                         WHERE  vip_item_cls = @ls_clsno)
                                BEGIN
                                    SELECT @ln_scale = vip_amt
                                    FROM   t_rm_vip_set with(nolock)
                                    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'

                        -- 根据优惠方式是否为空来判断当前销售是否为特价 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'
                            OR @ls_spec_flag = 'dm'
                            OR @ls_spec_flag = 'py'
                            OR @ls_spec_flag = 'pg'
                            OR @ls_spec_flag = 'pm'
                            OR @ls_spec_flag = 'lf'
                            OR @ls_spec_flag = 'ph'
                            OR @ls_spec_flag = 'ep'
                            OR @ls_spec_flag = 'cs'
							OR @ls_spec_flag = 'pr'
							OR @ls_spec_flag = 'gp'
							OR @ls_spec_flag = 'pz'   --20200106 liushuai1 买满直减（单据）促销也算
							OR CHARINDEX('^',@ls_spec_flag )>0 --方案促销中的促销单据都是^开头
                          SELECT @ls_isspec = '1'
                        ELSE
                          SELECT @ls_isspec = '0'

                        IF @ls_tj = '0'
                           AND @ls_isspec = '1'
                          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 with(nolock)
                              WHERE  sys_var_id = 'pos_vipprice_point'

                              --如果数据库中不存在此记录，则默认为不记积分 add by wangyy at 2013-01-09
                              IF @ls_vip_price IS NULL
                                  OR Rtrim(@ls_vip_price) = ''
                                SELECT @ls_vip_price = '0'

                              -- 如果后台设为为不积分时还要比较价格是否相等，如果相等则认为没有优惠，还是积分。 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 with(nolock)
                              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 with(nolock)
                                                     WHERE  branch_no = (SELECT sys_var_value
                                                                         FROM   t_sys_system with(nolock)
                                                                         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 with(nolock)
                              WHERE  sys_var_id = 'pos_vip_spec_price'

                              --如果数据库中不存在此记录，则默认为不记积分 add by wangyy at 2013-01-09
                              IF @ls_vip_spce_price IS NULL
                                  OR Rtrim(@ls_vip_spce_price) = ''
                                SELECT @ls_vip_spce_price = '0'

                              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 add by lihg 2006-06-14
                          END

                        SELECT @ls_clsno = item_clsno
                        FROM   t_bd_item_info with(nolock)
                        WHERE  item_no = @ls_itemno

                        --必须要初始化@ln_multi_sd，否则，当下的面SQL语句查不到值的时候，@ln_multi_sd还是取上一商品的@ln_multi_sd。edit by wangyy at 2014-02-18
                        SELECT @ln_multi_sd = 1

                        -- end edit wangyy
                        SELECT TOP 1 @ln_multi_sd = d.num1
                        FROM   t_pc_price_flow_master m with(nolock),
                               t_pc_price_flow_detail d with(nolock)
                        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
                               AND ( ( @ls_clsno LIKE Rtrim(item_no) + '%' )
                                      OR ( item_no = 'ALL' ) )
                        ORDER  BY work_date DESC

                        IF @ln_multi_sd IS NULL
                          SET @ln_multi_sd = 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 
                                    --记录时段积分    add by zhoucc 2012-10-10
                                    IF @ls_max_mult = '1'
                                      BEGIN
                                          IF @ln_multi_sd > @ln_max_mult
                                            BEGIN
                                               IF @lnway_vip_bl = 1 --ligq 2017-10-26
													SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount - ( @ln_saleQty * @ld_itemvip_acc * @ln_multi_sd ) * @lnway_vip_bl
												ELSE
													SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount - ( @ln_saleQty * @ld_itemvip_acc * @ln_multi_sd ) * ( @lnway_addtotal_amount / @ln_total_amount )

                                                SELECT @ln_vip_amount = @ln_vip_amount - @ln_nativeamt
                                            END
                                          ELSE
                                            BEGIN
                                                IF @lnway_vip_bl = 1 --ligq 2017-10-26
													SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount - ( @ln_saleQty * @ld_itemvip_acc * @ln_max_mult ) * @lnway_vip_bl 
												ELSE
													SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount - ( @ln_saleQty * @ld_itemvip_acc * @ln_max_mult ) * ( @lnway_addtotal_amount / @ln_total_amount )

                                                SELECT @ln_vip_amount = @ln_vip_amount - @ln_nativeamt
                                            END
                                      END
                                    ELSE
                                      BEGIN
                                          --如果没有设置时段积分倍数，则取原来的倍数
                                          --if @ln_multi_sd = 0 select @ln_multi_sd = @ln_max_mult
                                          --前面已经给@ln_multi_sd赋了初始值1了，所以此处判断有没有时段积分倍数时应该判断@ln_multi_sd的值是否为1才对。edit by wangyy at 2014-07-08
                                          IF @ln_multi_sd = 1
                                            SELECT @ln_multi_sd = @ln_max_mult

                                           IF @lnway_vip_bl = 1 --ligq 2017-10-26
											SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount - ( @ln_saleQty * @ld_itemvip_acc * @ln_multi_sd ) * @lnway_vip_bl 
										  ELSE
											SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount - ( @ln_saleQty * @ld_itemvip_acc * @ln_max_mult ) * ( @lnway_addtotal_amount / @ln_total_amount )

                                          SELECT @ln_vip_amount = @ln_vip_amount - @ln_nativeamt
                                      END
                                END
                              ELSE
                                BEGIN
                                    IF @ls_max_mult = '1'
                                      BEGIN
                                          IF @ln_multi_sd > @ln_max_mult
                                            BEGIN
                                                IF @lnway_vip_bl = 1 --ligq 2017-10-26
													SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount + ( @ln_saleQty * @ld_itemvip_acc * @ln_multi_sd ) * @lnway_vip_bl 
												ELSE
													SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount + ( @ln_saleQty * @ld_itemvip_acc * @ln_multi_sd ) * ( @lnway_addtotal_amount / @ln_total_amount )

                                                SELECT @ln_vip_amount = @ln_vip_amount + @ln_nativeamt
                                            END
                                          ELSE
                                            BEGIN
                                                IF @lnway_vip_bl = 1 --ligq 2017-10-26
													SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount + ( @ln_saleQty * @ld_itemvip_acc * @ln_max_mult ) * @lnway_vip_bl 
												ELSE
													SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount + ( @ln_saleQty * @ld_itemvip_acc * @ln_max_mult ) * ( @lnway_addtotal_amount / @ln_total_amount )

                                                SELECT @ln_vip_amount = @ln_vip_amount + @ln_nativeamt
                                            END
                                      END
                                    ELSE
                                      BEGIN
                                          --如果没有设置时段积分倍数，则取原来的倍数
                                          --if @ln_multi_sd = 0 select @ln_multi_sd = @ln_max_mult
                                          --前面已经给@ln_multi_sd赋了初始值1了，所以此处判断有没有时段积分倍数时应该判断@ln_multi_sd的值是否为1才对。edit by wangyy at 2014-07-08
                                          IF @ln_multi_sd = 1
                                            SELECT @ln_multi_sd = @ln_max_mult

                                          IF @lnway_vip_bl = 1 --ligq 2017-10-26 
											SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount + ( @ln_saleQty * @ld_itemvip_acc * @ln_multi_sd ) * @lnway_vip_bl 
										  ELSE
											SELECT @ln_vip_acc_amount =  @ln_vip_acc_amount + ( @ln_saleQty * @ld_itemvip_acc * @ln_multi_sd ) * ( @lnway_addtotal_amount / @ln_total_amount )

                                          SELECT @ln_vip_amount = @ln_vip_amount + @ln_nativeamt
                                      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 -------------------
              END

            -- 积分更新
            -- 积分取整
            SELECT @ls_pos_acc_int = '1'

            SELECT @ls_pos_acc_int = sys_var_value
            FROM   t_sys_system with(nolock)
            WHERE  Lower(sys_var_id) = 'pos_vip_acc_int'

            IF @ls_pos_acc_int = '1'
              BEGIN
                  SELECT @ls_temp = sys_var_value
                  FROM   t_sys_system with(nolock)
                  WHERE  Lower(sys_var_id) = 'acc_round'

                  IF @ls_temp IS NULL
                    SELECT @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))
                    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))
                    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)
                    END
              END

            SET @ln_vip_acc_amount = @ln_vip_acc_amount * @ln_max_mult_last --yejh 20151107 add 
            --会员类别积分比例 add by wangyy at 2011-07-20 -----------------------------------------------------------------------------------
            SELECT @ld_acc_rate = acc_rate
            FROM   t_rm_vip_type with(nolock)
            WHERE  type_id = (SELECT card_type
                              FROM   t_rm_vip_info with(nolock)
                              WHERE  card_id = @ls_vipno)

            IF @ld_acc_rate IS NULL
                OR @ld_acc_rate <= 0
              SET @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

            IF @pos_vip_auto_int = '1'
              BEGIN
                  SET @ln_vip_amount=Round(@ln_vip_amount, Isnull(@pos_vip_auto_int_dec, 0))
                  SET @ln_vip_acc_amount=Round(@ln_vip_acc_amount, Isnull(@pos_vip_auto_int_dec, 0))
              END

            IF EXISTS(SELECT *
                      FROM   t_rm_vip_consume with(nolock)
                      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_date = Getdate(),
                         modify_date = Getdate(),
                         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,
                           cons_date,
                           modify_date)
              VALUES     (@ls_vipno,
           
               LEFT(@ls_branchno, 2),
                          @ln_vip_amount,
                          @ln_vip_amount,
                          @ln_vip_acc_amount,
                          @ln_vip_acc_amount,
                          1,
                          Getdate(),
                          Getdate())

            --更新积分
            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

            --增加记录对应母卡的积分 by WangC 2015.7.28
            SELECT @ls_father = Isnull(father_card, '')
            FROM   t_rm_vip_info with(nolock)
            WHERE  card_id = @ls_vipno

            IF Len(@ls_father) > 0
               AND @ln_vip_acc_amount <> 0
              BEGIN
                  SELECT @ldec_child_jf = CONVERT(DECIMAL(6, 2), Isnull(sys_var_value, '0'))
                  FROM   t_sys_system
                  WHERE  sys_var_id = 'pos_child_jf'

                  SELECT @ldec_father_jf = CONVERT(DECIMAL(6, 2), Isnull(sys_var_value, '0'))
                  FROM   t_sys_system
                  WHERE  sys_var_id = 'pos_father_jf'

                  IF @ldec_child_jf > 0
                     AND @ldec_father_jf > 0
                    BEGIN
                        SET @ldec_father = Round(@ln_vip_acc_amount * @ldec_father_jf / @ldec_child_jf, 2)

                        IF @ls_pos_acc_int = '1'
                          BEGIN
                              SELECT @ls_temp = sys_var_value
                              FROM   t_sys_system
                              WHERE  Lower(sys_var_id) = 'acc_round'

                              IF @ls_temp IS NULL
                                SELECT @ls_temp = '1' --默认为向下取整

                              IF @ls_temp = '1'
                                BEGIN
                                    --向下取整
                                    IF @ldec_father > 0
                                      SELECT @ldec_father = Floor(@ldec_father)
                                    ELSE
                                      SELECT @ldec_father = -Floor(Abs(@ldec_father))
                                END
                              ELSE IF @ls_temp = '2'
                                BEGIN
                                    --向上取整
                                    IF @ldec_father > 0
                                      SELECT @ldec_father = Ceiling(@ldec_father)
                                    ELSE
                                      SELECT @ldec_father = -Ceiling(Abs(@ldec_father))
                                END
                              ELSE
                                BEGIN
                                    --四舍五入
                                    IF @ldec_father > 0
                                      SELECT @ldec_father = Round(@ldec_father, 0)
                                    ELSE
                                      SELECT @ldec_father = -Round(Abs(@ldec_father), 0)
                                END
                          END

                        IF @ldec_father <> 0
                          BEGIN
                              IF EXISTS(SELECT *
                                        FROM   t_rm_vip_consume with(nolock)
                                        WHERE  card_id = @ls_father
                                               AND branch_no = LEFT(@ls_branchno, 2))
                                BEGIN
                                    UPDATE t_rm_vip_consume
                                    SET    vip_acc_amount = Isnull(vip_acc_amount, 0) + @ldec_father,
                    
                       					   vip_cur_acc_amount = Isnull(vip_cur_acc_amount, 0) + @ldec_father,
                                           com_flag = '0',
                                           cons_date = Getdate(),
                                           modify_date = Getdate(),
                                           cons_count = Isnull(cons_count, 0) + 1
                                    WHERE  card_id = @ls_father
                                           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_father,
                                            LEFT(@ls_branchno, 2),
                                            0,
                                            0,
                                            @ldec_father,
                                            @ldec_father,
                                            1,
                                            Getdate(),
                                            Getdate())

                              INSERT t_rm_card_paylist
                                     (flow_no,
                                      card_type,
                                      card_id,
                                      amount,
                                      pay_time,
                                      other1,
                                      other2,
                                      branch_no,
                                      residual_amt,
                                      com_flag,
                                      vip_acc)
                              VALUES (@as_flowno,
                                      '1',
                                      @ls_father,
                                      0,
                                      Getdate(),
                                      '子卡消费积分',
                                      @ls_vipno,
                                      Substring(@ls_branchno, 1, 2),
                                      0,
                                      '0',
                                      @ldec_father)

                              UPDATE t_rm_vip_info
                              SET    acc_num = Isnull(acc_num, 0) + @ldec_father
                              WHERE  card_id = @ls_father
                          END
                    END
              END

            ---yejh 20170320 update  t_rm_card_paylist  时已经触发不再更新
        /*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'
               */
            --@ln_vip_acc_amount
            SELECT @li_limit_num = use_num
            FROM   t_rm_vip_info with(nolock)
            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 with(nolock)
              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 with(nolock)
                            WHERE  card_id = @ls_card_no
                                   AND branch_no = LEFT(@ls_branchno, 2))
                    BEGIN
                        UPDATE t_rm_vip_consume
                        SET    com_flag = '0',
                               cons_date = Getdate(),
                               modify_date = Getdate(),
                               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,
                                 cons_date,
                                 modify_date)
                    VALUES     (@ls_card_no,
                                LEFT(@ls_branchno, 2),
                                0,
                                0,
                                0,
                                0,
                                1,
                                @ln_total_amount,
                                Getdate(),
                                Getdate())

                  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
go