if exists(select 1 from sysobjects where name='tr_rm_saleflow')
  drop TRIGGER tr_rm_saleflow
go

create trigger tr_rm_saleflow on t_rm_saleflow 
for insert
as
begin

declare @ls_branchNO 			char(6),-- 仓库编码
	      @ls_ItemNO 			char(20),-- 商品编码
	      @ls_ClassNO 			char(6),-- 商品类别编码
	      @ln_saleQty			numeric(16,4),-- 商品销售数量
	      @ln_sourcePrc  		numeric(16,4), -- 商品原来预定的单价
	      @ln_salePrc 			numeric(16,4),-- 商品销售单价
	      @ln_sourceAmt 		numeric(16,4),  -- 商品原来预定的销售金额
	      @ln_saleAmt 			numeric(16,4),    -- 商品销售金额
	      @ln_dctAmt 			numeric(16,4),   -- 商品折扣金额.
	      @ls_saleWay 			char(1),        -- 销售方式
	      @ldt_saleDateTime 	datetime,   -- 销售时间
	      @ls_operDate 			char(10),       -- 字符型日期（当天），格式为：yyyymmdd
	      @ls_ItemExistFlag 	char(1),   -- 明细商品在商品销售日结表(t_rm_daySum)中是否已存在标志，'0'=不存在；'1'=已存在
	      @ls_casherExistFlag 	char(1), -- 收银员在收银员日结表(t_rm_daySum_cls)中是否已存在标志，'0'=不存在；'1'=已存在
	      @ls_casher  			char(4),
	      @ln_discountAmt 		numeric(16,4),
	      @ls_spec_flag 		varchar(10),
	      @ls_spec_flag_new         varchar(10), -----huangdw 更新已经销售用，原来@ls_spec_flag的这个不能用，这个是计算让利用的
	      --@ls_local_flag char(1),
	      @flow_no 				varchar(20),
	      @flow_id 				int,
	      @dec_cost_price 		numeric(16,4),
	      @ln_sale_amt 			numeric(16,4),
	      @ln_ret_amt 			numeric(16,4),
	      @ls_counter_no 		varchar(16),
	      @ln_ly_rate 			numeric(16,4),  --联营扣率
	      @ln_tmp_rate 			numeric(16,4),  --联营扣率
	      @ln_flow_id 			numeric(16),
	      @ls_sup_sale_way 		char(1),
	      @supcust_no  			varchar(10),
		  @remote_flag		varchar(1),
          @ln_flowno                numeric(16,0),
	      @ls_flag_o    char(1),
	      @ln_tmp_ly_rate		numeric(16,4),
		  @ln_sale_qty 			numeric(16,4),  --每单限量
		  @ln_limited_qty		numeric(16,4),   --全场限量
		  @ls_spec_sheet_no 	char(14),
		  @ls_cic_sheet_no    varchar(14),  ---旅游超市发牌记录
		  @ls_item_clsno       varchar(12),
		  @ll_exist            int,
		  @ldec_sale_money     numeric(16,4),
		  @ldec_ciceroni_rate  numeric(16,4),
		  @ldec_travel_rate    numeric(16,4),
		  @ls_cic_sheet_no_tmp varchar(14),
		  @ls_more_cic_flag	   varchar(1),
		  @ll_max_tab_id	   numeric(16,0),
		  @ll_min_tab_id	   numeric(16,0),
		  @ls_cnte_type		   varchar(1),
		  @ls_cnte_id		   varchar(10),
		  @ls_payway		   varchar(10)


---youdp--2014-02-25--创建临时表用来存储多导游明细---------
create table #tab_cn_more_info(tab_id numeric(16,0) identity(1,1),cnte_type varchar(1),cnte_id varchar(10))
set @ls_cic_sheet_no_tmp = ''
set @ls_more_cic_flag = '0'
-----------------------------------------------------------

select @ls_flag_o = sys_var_value from t_sys_system where sys_var_id = 'LYKL_effect_zero' 
if @ls_flag_o is null or @ls_flag_o = '' 
	select @ls_flag_o = '1'

DECLARE inserted_cursor CURSOR FOR 
select branch_no,item_no,sale_qnty,source_price,sale_price,sell_way,oper_date,
		  oper_id,	  spec_flag,
		  pref_amt,	  flow_no,
		  flow_id,	  counter_no,
		  remote_flag,spec_sheet_no,cic_sheet_no,sale_money
      from inserted 
	where remote_flag <> '1'

 -- 是否传输明细数据到日结表
--  if ( @remote_flag = '1' ) RETURN --独立分店上传时直接返回不处理,且配送中心不传

OPEN inserted_cursor

FETCH NEXT FROM inserted_cursor 
INTO @ls_branchNO,@ls_ItemNO,@ln_saleQty,
		  @ln_sourcePrc,	  @ln_salePrc,
		  @ls_saleWay,	  @ldt_saleDateTime,
		  @ls_casher,	  @ls_spec_flag,
		  @ln_dctAmt,	  @flow_no,
		  @flow_id,	  @ls_counter_no,
		  @remote_flag,@ls_spec_sheet_no,@ls_cic_sheet_no,@ldec_sale_money
/* -- 当前销售流水数据
      select @ls_branchNO=branch_no,
		  @ls_ItemNO=item_no,	          @ln_saleQty=sale_qnty,
		  @ln_sourcePrc=source_price,	  @ln_salePrc=sale_price,
		  @ls_saleWay=sell_way,	  @ldt_saleDateTime=oper_date,
		  @ls_casher=oper_id,	  @ls_spec_flag=spec_flag,
		  @ln_dctAmt=pref_amt,	  @flow_no = flow_no,
		  @flow_id = flow_id,	  @ls_counter_no=counter_no
      from inserted
*/
WHILE @@FETCH_STATUS = 0
BEGIN

 --初始化变量
        select @dec_cost_price=0, @ls_sup_sale_way='', @supcust_no=''
	select @ln_ly_rate = null,@ln_tmp_rate = null,@ln_tmp_ly_rate = 0, @ln_sale_qty = 0, @ln_limited_qty = 0	
 -- 是否传输明细数据到日结表
--  if ( @remote_flag = '1' ) RETURN --独立分店上传时直接返回不处理,且配送中心不传
        
	-- 计算原来预定的销售金额和实际的销售金额  modified by wujs 2004-12-17 金额只取两位小数
	select @ln_sourceAmt = round(@ln_saleQty * @ln_sourcePrc,2)
	--select @ln_saleAmt = round(@ln_saleQty * @ln_salePrc,2)
	--优化销售金额为sale_money，避免单价*数量 不等于金额时出现误差 主要I15 liangbc 20151210
	select @ln_saleAmt = @ldec_sale_money
	-- 直接取让利金额
	
	if not (@ls_saleWay = 'C' and @ldec_sale_money = 0)
		begin
			if (@ls_spec_flag is null or ltrim(@ls_spec_flag)='') 
				select @ln_dctAmt = 0
			set @ls_payway = 'RMB'
		end
	else
		set @ls_payway = 'GIF'

	-- select @ln_dctAmt = @ln_sourceAmt - @ln_saleAmt
	
	-- 转换字符型日期（当天），格式为：'112'=yyyymmdd
	select @ls_operDate=convert(char(10),@ldt_saleDateTime,121)
        
        -- 取成本价 modified by wujs 2005-12-20
--raiserror
        select @dec_cost_price = isnull(avg_cost,0) from t_im_branch_stock 
             where item_no = @ls_ItemNO and branch_no = @ls_branchNO
        if @dec_cost_price = 0 or @dec_cost_price is null -- 成本价为零取机构进价
        begin
           if exists(select 1 from t_pc_branch_price where item_no = @ls_ItemNO and branch_no = substring(@ls_branchNO,1,2))
	        select @dec_cost_price = isnull(price,0) from t_pc_branch_price
	              where item_no = @ls_ItemNO and branch_no = substring(@ls_branchNO,1,2)
	   else
	   	select @dec_cost_price = isnull(price,0) from t_bd_item_info where item_no = @ls_ItemNO
        end

        if @dec_cost_price = 0 or @dec_cost_price is null -- 为零时使用售价
            select @dec_cost_price = @ln_salePrc
        
        --销售和退货 取扣率 
        if @ls_saleWay = 'A' or @ls_saleWay = 'B'
        begin
           if exists (select 1 from t_pc_branch_price where item_no = @ls_ItemNO and branch_no = substring(@ls_branchNO,1,2)) and (select top 1 sup_ly_rate from t_pc_branch_price where item_no = @ls_ItemNO and branch_no = substring(@ls_branchNO,1,2)) <> 0
	           select @ls_sup_sale_way = sale_way, @ln_ly_rate = sup_ly_rate
	               from t_pc_branch_price
               	       where item_no = @ls_ItemNO and branch_no = substring(@ls_branchNO,1,2)
           else
	           select @ls_sup_sale_way = item_sup_flag, @ln_ly_rate = sup_ly_rate
	               from t_bd_item_info
	               where item_no = @ls_ItemNO
	     
		select @ln_tmp_ly_rate = @ln_ly_rate 	
          
           if @ls_sup_sale_way = 'C' or @ls_sup_sale_way = 'F'  --联营和扣率代销
           begin
              --原价销售取档案扣率 
              --if @ln_sourcePrc <> @ln_salePrc --特价销售取特价单扣率 mahw modify
              --begin
                 -- 5,6是类别和品牌促销,此时discount为折扣
		---先处理会员类优惠huq   
		if  @ls_spec_flag = 'PV'
			begin
				select @ln_flowno =max(flow_no) from t_rm_spec_price 
	                       where ( branch_no = substring(@ls_branchNO,1,2))
	                         and ( item_no = @ls_ItemNO )
	                       --  and ( spe_price =  @ln_salePrc )
	                         and ( special_type = '2' )---会员限量特价单
	                         and ( getdate() between start_date and  convert(datetime,(convert(char(10),end_date,121) + ' 23:59:59.99'),121) )
	                         and (
	                                 (begin_time is null or ltrim(begin_time)='') or 
	                                 ( 
	                                     (begin_time is not null and ltrim(begin_time)>'') and 
	             	                     (right('0'+convert(varchar(2),datepart(hh,getdate())),2)+':'+
	                                      right('0'+convert(varchar(2),datepart(mi,getdate())),2)+':'+
	                                      right('0'+convert(varchar(2),datepart(ss,getdate())),2) 
	                                      between begin_time and end_time )
	                                  )
	                              )
	                    
     and substring(week, DATEPART(weekday,getdate()),1)= '1'
				and (limited_qty > sold_qty or limited_qty = 0)
				if @ln_flowno > 0
					select  @ln_tmp_rate = discount from t_rm_spec_price where flow_no=@ln_flowno and item_no=@ls_ItemNO
				else
					select @ln_tmp_rate = @ln_ly_rate
		                if @ln_tmp_rate is not null and @ln_tmp_rate <> 0  --如果 isnull(@ln_tmp_rate) 则不存在促销扣率--可排除单笔限量huq
		                    	select @ln_ly_rate = @ln_tmp_rate
				else if @ls_flag_o = '0' and @ln_tmp_rate = 0
					select @ln_ly_rate = @ln_tmp_rate
			end
		else --除会员类优惠另外理 huq   
			begin 
	                 select @ln_flowno =max(flow_no) from t_rm_spec_price 
	                       where ( branch_no = substring(@ls_branchNO,1,2))
	                         and ( item_no = @ls_ItemNO )
	                       --  and ( spe_price =  @ln_salePrc )
	                         and ( special_type not in ('C','6', '5','2') )
	                         and ( getdate() between start_date and  convert(datetime,(convert(char(10),end_date,121) + ' 23:59:59.99'),121) )
	                         and (
	                                 (begin_time is null or ltrim(begin_time)='') or 
	                                 ( 
	                                     (begin_time is not null and ltrim(begin_time)>'') and 
	             	                     (right('0'+convert(varchar(2),datepart(hh,getdate())),2)+':'+
	                                      right('0'+convert(varchar(2),datepart(mi,getdate())),2)+':'+
	                                      right('0'+convert(varchar(2),datepart(ss,getdate())),2) 
	                                      between begin_time and end_time )
	                                  )
	                              )
	                         and substring(week, DATEPART(weekday,getdate()),1)= '1'

	                 -- if (@ls_spec_flag is null or ltrim(@ls_spec_flag)='')  and exists(select 1 from t_rm_spec_price where flow_no=@ln_flowno and item_no=@ls_ItemNO and special_type in ('D','8'))
				--select @ln_tmp_rate = @ln_ly_rate
			 -- else
	                 	select  @ln_tmp_rate = discount,@ln_sale_qty = sale_qty,@ln_limited_qty = limited_qty from t_rm_spec_price where flow_no=@ln_flowno and item_no=@ls_ItemNO
                
	                   if @ln_tmp_rate is not null and @ln_tmp_rate <> 0  --如果 isnull(@ln_tmp_rate) 则不存在促销扣率-
	                    	select @ln_ly_rate = @ln_tmp_rate
			   else if @ls_flag_o = '0' and @ln_tmp_rate = 0
				select @ln_ly_rate = @ln_tmp_rate

				select @ln_sale_qty = isnull(@ln_sale_qty,0)
				select @ln_limited_qty = isnull(@ln_limited_qty,0)

			  if (@ln_flowno > 0 and @ln_tmp_rate is not null and @ln_tmp_rate <> 0 ) and (@ls_spec_flag is null or rtrim(ltrim(@ls_spec_flag))='')
					--考虑到整单限量,限量外销售取商品档案扣率.huq
					if @ln_sale_qty + @ln_limited_qty > 0  --存在全场限量或每单限量时，为超量部分
						select @ln_ly_rate  = 	@ln_tmp_ly_rate		
					--else  --不是超量部分，该情况属于原价跟特价相等，没特价标识的情况，仍取特价扣率
						--select @ln_ly_rate = @ln_tmp_rate
			  end
             -- end
           end
           else
              select @ln_ly_rate = 0         
        end --取联营扣率结束
        
        --取商品主供应商
        --add by dutz 2006-02-22
        --select @supcust_no = main_supcust from t_bd_item_info where item_no = @ls_itemNO
	--liangbc 20141212
         if exists(select 1 from t_pc_branch_price where branch_no = substring(@ls_branchNO,1,2) and item_no = @ls_itemNO)
	   select @supcust_no = supcust_no from t_pc_branch_price where branch_no = substring(@ls_branchNO,1,2) and item_no = @ls_itemNO
	 else
	   select @supcust_no = main_supcust from t_bd_item_info where item_no = @ls_itemNO
        
	-- Modifeid by wujs 2005-12-20
	-- 在　t_rm_daysum　中区分一天内不同售价，不同扣率，不同退货价的商品销售记录。
	-- 并且记录当时的商品成本金额  

	-- 查找在商品销售日结表中是否已存在
	select @ls_ItemExistFlag='0',@ln_flow_id = 0
	if @ls_saleWay = 'A' --销售
	begin
	    select top 1 @ln_flow_id = flow_id from t_rm_daysum 
	    
   where branch_no = @ls_branchNO and item_no    = @ls_ItemNO
	         and oper_date = @ls_operDate and sale_price = @ln_salePrc and sup_ly_rate = @ln_ly_rate and supcust_no = @supcust_no
	    if @ln_flow_id > 0 --存在
	      select @ls_ItemExistFlag='1'
	end
	else if @ls_saleWay = 'B' --退货
	begin --modified by wujs 2005-12-19 区分售价、扣率、退货价
	    select  @ln_flow_id = max(flow_id) from t_rm_daysum 
	        where branch_no = @ls_branchNO and item_no   = @ls_ItemNO 
	          and oper_date = @ls_operDate and ret_price = @ln_salePrc and sale_price = @ln_salePrc and sup_ly_rate = @ln_ly_rate and supcust_no = @supcust_no
            if @ln_flow_id > 0 --存在
	       select @ls_ItemExistFlag='1'
	end
	else --if  @ls_saleWay = 'C' --赠送
	begin
	   select top 1 @ln_flow_id = flow_id from t_rm_daysum 
		where branch_no = @ls_branchNO and item_no  = @ls_ItemNO 
		  and oper_date = @ls_operDate and supcust_no = @supcust_no
		  and sale_price = 0 
           if @ln_flow_id > 0  --存在
	      select @ls_ItemExistFlag='1'
	end
        
	-- 写商品销售日结表(t_rm_daySum).............................................start
	if @ls_ItemExistFlag='0' -- 明细不存在，增加
        begin
	   if @ln_ly_rate is null
	      select @ln_ly_rate = 0

           if @ls_saleWay='A' -- 销售
	      --modified by wujs 2005-12-19
              insert t_rm_daysum(branch_no,item_no,oper_date,sale_price,ret_price,sup_ly_rate,
	                         sale_qty,ret_qty,giv_qty,sale_amt,ret_amt,giv_amt,cost_price,
			         pre_qty,other1,other2,other3,dct_amount,supcust_no)
	         values(@ls_branchNO,@ls_ItemNO,@ls_operDate,@ln_salePrc,0,@ln_ly_rate,
		        @ln_saleQty, 0,  0,  @ln_saleAmt,0,0,@dec_cost_price * @ln_saleQty,
			0,'0',null,null,@ln_dctAmt,@supcust_no)
           else if @ls_saleWay='B' -- 退货
	      --modified by wujs 2005-12-19
              insert t_rm_daysum(branch_no,item_no,oper_date,sale_price,ret_price,sup_ly_rate,
	                         sale_qty,ret_qty,giv_qty,sale_amt,ret_amt,giv_amt,cost_price,
			         pre_qty,other1,other2,other3,dct_amount,supcust_no)
	         values(@ls_branchNO, @ls_ItemNO, @ls_operDate,@ln_salePrc, @ln_salePrc, @ln_ly_rate,
		        0, @ln_saleQty, 0, 0,@ln_saleAmt,0,@dec_cost_price * @ln_saleQty,
			0,'0',null,null,@ln_dctAmt,@supcust_no)
           else if @ls_saleWay='C' -- 赠送
	      --modified by wujs 2005-12-19
              insert t_rm_daysum(branch_no,item_no,oper_date,sale_price,ret_price,sup_ly_rate,
	                         sale_qty,ret_qty,giv_qty,sale_amt,ret_amt,giv_amt,cost_price,
			         pre_qty,other1,other2,other3,dct_amount,supcust_no)
	         values(@ls_branchNO,@ls_ItemNO,@ls_operDate,0,0,@ln_ly_rate,
		        0, 0, @ln_saleQty, 0, 0, (case when @ln_saleAmt = 0 then @ln_dctAmt else @ln_saleAmt end), @dec_cost_price * @ln_saleQty,
                        0,'0',null,null,@ln_dctAmt,@supcust_no)
        end
	else  -- 明细已存在，更新
        begin
            -- other1 = '0'  other1 标志该行记录是否处理 modified by wujs 2005-05-24
            if @ls_saleWay='A' -- 销售
               update t_rm_daysum set
                  sale_qty   = sale_qty + @ln_saleQty,  sale_amt = sale_amt + @ln_saleAmt,
                  dct_amount = dct_amount + @ln_dctAmt,  --sale_price = @ln_salePrc,
                  cost_price = cost_price + @dec_cost_price * @ln_saleQty,
                  other1     = '0',         com_flag   = '0'
                where flow_id = @ln_flow_id 
                --where branch_no=@ls_branchNO and item_no=@ls_ItemNO and oper_date=@ls_operDate
	    else if @ls_saleWay='B' -- 退货
	      update t_rm_daysum set
	          ret_qty    = ret_qty + @ln_saleQty,  ret_amt    = ret_amt + @ln_saleAmt,
	          dct_amount = dct_amount - @ln_dctAmt, --sale_price = @ln_salePrc,
                  cost_price = cost_price + @dec_cost_price * @ln_saleQty,
		  other1     = '0',        com_flag   ='0'
                where flow_id = @ln_flow_id 
	        --where branch_no=@ls_branchNO and item_no=@ls_ItemNO and oper_date=@ls_operDate
	    else if @ls_saleWay='C' -- 赠送
	      update t_rm_daysum set
	          giv_qty  = giv_qty + @ln_saleQty,  giv_amt = giv_amt + (case when @ln_saleAmt = 0 then @ln_dctAmt else @ln_saleAmt end),
	          --sale_price=@ln_salePrc,
                  cost_price = cost_price + @dec_cost_price * @ln_saleQty,
                  other1   = '0',      com_flag = '0'
                where flow_id = @ln_flow_id 
               --where branch_no=@ls_branchNO and item_no=@ls_ItemNO and oper_date=@ls_operDate
        end
      -- 写商品销售日结表(t_rm_daySum).............................................end
      
     
      --流水中记录成本价
      update t_rm_saleflow set in_price= @dec_cost_price * @ln_saleQty
              where flow_no = @flow_no and flow_id = @flow_id
      
       

      -- 写收银员日结表(t_rm_casher_daysum),记录让利情况
      if @ln_dctAmt <> 0
      begin	
  
           -- 本机构或总部托管的分部上传的POS流水才可写收银员日结表
	   -- 查找在收银员日结表(t_rm_casher_daySum)中是否已存在
	   if not exists(select * from t_rm_casher_daysum
	        where (oper_date=@ls_operDate and casher_no=@ls_casher and branch_no=substring(@ls_branchNO,1,2) and pay_way=@ls_payway and sale_way=@ls_saleWay and coin_no='RMB')) -- 不存在
	      select @ls_casherExistFlag='0'
	   else  -- 已存在
	      select @ls_casherExistFlag='1'
	
	 
   -- 写收银员日结表(t_rm_casher_daySum).............................................start
	    if @ls_casherExistFlag='0'

		 insert into t_rm_casher_daysum
		       (oper_date,   casher_no, branch_no,                  sell_amt,real_amt,sale_way,   pay_way,coin_no,oper_type,other1,other2,other3,num1,num2,num3)
		 values(@ls_operDate,@ls_casher,substring(@ls_branchNO,1,2),0,       0,       @ls_saleWay,@ls_payway,  'RMB',  '0',      '',    '',    '',    0,   0,   0)

			if @ls_saleWay ='C' and @ls_payway = 'GIF'  
				begin
					select @ln_discountAmt = isnull(real_amt,0) from t_rm_casher_daysum   
					where (oper_date=@ls_operDate and casher_no=@ls_casher and 
				   branch_no=substring(@ls_branchNO,1,2) and pay_way=@ls_payway and
				   sale_way=@ls_saleWay and coin_no='RMB')

					select @ln_discountAmt = @ln_discountAmt + @ln_dctAmt

					update t_rm_casher_daysum set real_amt=@ln_discountAmt,com_flag='0'
					where (oper_date=@ls_operDate and casher_no=@ls_casher and
					branch_no=substring(@ls_branchNO,1,2) and pay_way=@ls_payway and 
					sale_way=@ls_saleWay and coin_no='RMB')
				end
			else     
				begin  
					select @ln_discountAmt = isnull(num1,0) from t_rm_casher_daysum   
						where (oper_date=@ls_operDate and casher_no=@ls_casher and 
					   branch_no=substring(@ls_branchNO,1,2) and pay_way=@ls_payway and
					   sale_way=@ls_saleWay and coin_no='RMB')

					select @ln_discountAmt = @ln_discountAmt + @ln_dctAmt

					update t_rm_casher_daysum set num1=@ln_discountAmt,com_flag='0'
										where (oper_date=@ls_operDate and casher_no=@ls_casher and
										branch_no=substring(@ls_branchNO,1,2) and pay_way=@ls_payway and 
										sale_way=@ls_saleWay and coin_no='RMB')
				end

	    
      end

      -- 写柜台日结表(t_rm_counter_daysum).............................................start
      if @ls_saleWay = 'A' or @ls_saleWay = 'C'   -- 销售或赠送
      begin
			-- 2012-05-05-youdp---不需要赠送
			if @ls_saleWay = 'C' 
				select @ln_sale_amt = 0, @ln_ret_amt = 0
			else
				select @ln_sale_amt = @ln_saleAmt, @ln_ret_amt = 0
            -- commented by wujs 2005-09-04 一票多柜台，不能区分银行卡付款中多少钱付给哪一个柜台
            -- if @ls_payWay = 'CRD' select @ln_bankcard_amt = @ln_sale_amt 
            -- else  select @ln_bankcard_amt = 0 --在专柜月结中有统计银行卡付款,与一票多柜台功能冲突！
      end
      else  -- 退货
      begin
          select @ln_ret_amt = @ln_saleAmt, @ln_sale_amt = 0 -- ,@ln_bankcard_amt = 0
      end

      if not exists(select* from t_rm_counter_daysum
             where (oper_date=@ls_operDate and branch_no=substring(@ls_branchNO,1,2) and counter_no=@ls_counter_no))
          -- 柜台不存在，增加
          insert into t_rm_counter_daysum
                   (oper_date,branch_no,counter_no,sale_amt,ret_amt,bankcard_amt,other1,number1) 
             values(@ls_operDate,substring(@ls_branchNO,1,2),@ls_counter_no,@ln_sale_amt,@ln_ret_amt,0,null,null) -- @ln_bankcard_amt
      else -- 柜台已存在，更新         bankcard_amt=bankcard_amt+@ln_bankcard_amt
          update t_rm_counter_daysum 
                 set sale_amt=sale_amt+@ln_sale_amt, ret_amt=ret_amt+@ln_ret_amt, com_flag='0'
                where (oper_date=@ls_operDate and branch_no=substring(@ls_branchNO,1,2) and 
		       counter_no = @ls_counter_no)
     -- 写柜台日结表(t_rm_counter_daySum).............................................end

--		if @ls_spec_flag = 'ps' or @ls_spec_flag = 'pe' or @ls_spec_flag = 'dm' or @ls_spec_flag = 'py' or @ls_spec_flag = 'pv' 
--		or @ls_spec_flag = 'pn'or @ls_spec_flag = 'pm'or @ls_spec_flag = 'lf'or @ls_spec_flag = 'cs'or @ls_spec_flag = 'ph'
--		or @ls_spec_flag = 'pu'
		select @ls_spec_flag_new = lower(left(@ls_spec_sheet_no,2)) -----------huangdw 20131225 用这个变量
		if isnull(@ls_spec_flag_new,'') <> ''   -----------huangdw 20131225 用这个变量
			if @ls_spec_flag_new in ( 'pu', 'pz' ) --类别品牌不带货号 -----------huangdw 20131225 用这个变量
				begin
				  if @ls_saleWay = 'A'
					begin
					 UPDATE t_rm_spec_price Set sold_qty = @ln_saleQty + sold_qty,com_flag = '0'
					Where other3 = @ls_spec_sheet_no and branch_no = left(@ls_branchNO,2) 
					end
				  if @ls_saleWay = 'B'
					begin
					 UPDATE t_rm_spec_price Set sold_qty = sold_qty - @ln_saleQty ,com_flag = '0'
					Where other3 = @ls_spec_sheet_no and branch_no = left(@ls_branchNO,2) 
				   end
				 end
			 ELSE
				 begin
				  if @ls_saleWay = 'A'
					begin
					 UPDATE t_rm_spec_price Set sold_qty = @ln_saleQty + sold_qty,com_flag = '0'
					Where other3 = @ls_spec_sheet_no and branch_no = left(@ls_branchNO,2) and item_no = @ls_ItemNO
					end
				  if @ls_saleWay = 'B'
					begin
					 UPDATE t_rm_spec_price Set sold_qty = sold_qty - @ln_saleQty ,com_flag = '0'
					Where other3 = @ls_spec_sheet_no and branch_no = left(@ls_branchNO,2) and item_no = @ls_ItemNO
				   end
				 end
		
	--------youdp-2013-04-24-写导游消费-类别提成表---------------------------------------------------------------
	set @ls_cic_sheet_no = isnull(@ls_cic_sheet_no,'')
	if ltrim(rtrim(@ls_cic_sheet_no)) <> ''
		begin
			set @ll_exist = 0
			set @ldec_ciceroni_rate = 0
			set @ls_item_clsno = ''
			----youdp--2014-02-25------------------------------------------------------------------------------------------
			if @ls_cic_sheet_no <> @ls_cic_sheet_no_tmp
				begin
					set @ls_cic_sheet_no_tmp = @ls_cic_sheet_no			
					set @ls_more_cic_flag = '0'
					select top 1 @ls_more_cic_flag = more_cic_flag from t_cn_history with(nolock) where t_cn_history.sheet_no = @ls_cic_sheet_no
					set @ls_more_cic_flag = isnull(@ls_more_cic_flag,'0')
					
					delete #tab_cn_more_info
					
					if @ls_more_cic_flag = '1' ---多导游-------------
						begin
							insert into #tab_cn_more_info(cnte_type,cnte_id)
							select distinct '1' as cnte_type,tab_b.ciceroni_id as cnte_id
							from t_cn_more_history as tab_b with(nolock) 
							where tab_b.cic_sheet_no = @ls_cic_sheet_no
							union
							select distinct '2' as cnte_type,tab_b.travel_id as cnte_id
							from t_cn_more_history as tab_b with(nolock)
							where tab_b.cic_sheet_no = @ls_cic_sheet_no
						end
					else
						begin
							insert into #tab_cn_more_info(cnte_type,cnte_id)
							select distinct '1' as cnte_type,tab_b.ciceroni_id as cnte_id
							from t_cn_history as tab_b with(nolock) 
							where tab_b.sheet_no = @ls_cic_sheet_no
							union
							select distinct '2' as cnte_type,tab_b.travel_id as cnte_id
							from t_cn_history as tab_b with(nolock)
							where tab_b.sheet_no = @ls_cic_sheet_no
						end
				end
				
				
				
			
	set @ll_max_tab_id = 0
				set @ll_min_tab_id = 0
				select @ll_max_tab_id = max(tab_id),@ll_min_tab_id = min(tab_id) from #tab_cn_more_info
				set @ll_max_tab_id = isnull(@ll_max_tab_id,0)
				set @ll_min_tab_id = isnull(@ll_min_tab_id,0)
				
				while @ll_max_tab_id - @ll_min_tab_id >= 0 and @ll_max_tab_id > 0 and @ll_min_tab_id > 0
					begin
						set @ll_exist = 0
						set @ldec_ciceroni_rate = 0
						set @ls_item_clsno = ''
						set @ls_cnte_type  = ''
						set @ls_cnte_id    = ''
						set @ldec_travel_rate = 0
				
					
						select @ls_cnte_type = cnte_type,@ls_cnte_id = cnte_id from #tab_cn_more_info with(nolock) where tab_id = @ll_min_tab_id
						if @ls_cnte_type = '1' 
							----------------youdp-2014-02-25--------导游提成-------------------------------------------------------------------------------
							begin
								select top 1 @ldec_ciceroni_rate = t_cn_item_cls_rate.rate,@ll_exist = 1,@ls_item_clsno = t_cn_item_cls_rate.item_clsno
								from t_cn_item_cls_rate with(nolock),t_bd_item_info with(nolock)
								where t_cn_item_cls_rate.type = '1' and
									  t_cn_item_cls_rate.item_clsno = left(t_bd_item_info.item_clsno,len(t_cn_item_cls_rate.item_clsno)) and 
									  len(t_cn_item_cls_rate.item_clsno) <= len(t_bd_item_info.item_clsno) and
									  t_bd_item_info.item_no = @ls_ItemNO and 
									  t_cn_item_cls_rate.cic_sheet_no = @ls_cic_sheet_no and
									  ((@ls_more_cic_flag = '1' and t_cn_item_cls_rate.cnte_id = @ls_cnte_id) or (@ls_more_cic_flag <> '1'))
								order by t_cn_item_cls_rate.item_clsno desc

								if @ll_exist <> 1
									begin
										set @ll_exist = 0
										select top 1 @ldec_ciceroni_rate = t_bd_item_cls.ciceroni_rate,@ll_exist = 1,@ls_item_clsno = t_bd_item_cls.item_clsno
										from t_bd_item_cls with(nolock),t_bd_item_info with(nolock)
										where t_bd_item_cls.item_clsno = left(t_bd_item_info.item_clsno,len(t_bd_item_cls.item_clsno)) and 
											  len(t_bd_item_cls.item_clsno) <= len(t_bd_item_info.item_clsno) and
											  t_bd_item_info.item_no = @ls_ItemNO
										order by (case when t_bd_item_cls.ciceroni_rate <= 0 then '1' else '2' end) desc,t_bd_item_cls.item_clsno desc
										if @ll_exist <> 1
											set @ldec_ciceroni_rate = 0
									end
								
								if exists(select top 1 1 from t_rm_cn_daysum with(nolock) 
	    												 where cic_sheet_no = @ls_cic_sheet_no and 
							   								   branch_no = left(@ls_branchNO,2) and
															   item_clsno = @ls_item_clsno and
															   fm_type = '1' and
															   oper_date = convert(varchar(10),@ldt_saleDateTime,121) and 
															   ((@ls_more_cic_flag = '1' and t_rm_cn_daysum.cnte_id = @ls_cnte_id) or (@ls_more_cic_flag <> '1'))
															   )
									update t_rm_cn_daysum set real_amt = real_amt + (case @ls_saleWay when 'B' then - @ldec_sale_money when 'c' then 0 else  @ldec_sale_money end ) * rate,
															  sale_amt = sale_amt + (case @ls_saleWay when 'B' then - @ldec_sale_money when 'c' then 0 else  @ldec_sale_money end ),
															  sale_qnty = sale_qnty + (case @ls_saleWay when 'B' then - @ln_saleQty when 'c' then 0 else  @ln_saleQty end )
									where cic_sheet_no = @ls_cic_sheet_no and 
										  branch_no = left(@ls_branchNO,2) and
										  item_clsno = @ls_item_clsno and
										  fm_type = '1' and
										  oper_date = convert(varchar(10),@ldt_saleDateTime,121) and
										  ((@ls_more_cic_flag = '1' and t_rm_cn_daysum.cnte_id = @ls_cnte_id) or (@ls_more_cic_flag <> '1'))
								else
									insert into t_rm_cn_daysum (cic_sheet_no,branch_no,item_clsno,real_amt,sale_amt,sale_qnty,fm_type,rate,oper_date,cnte_id)
									values (@ls_cic_sheet_no,left(@ls_branchNO,2),@ls_item_clsno,(case @ls_saleWay when 'B' then - @ldec_sale_money when 'c' then 0 else  @ldec_sale_money end ) * @ldec_ciceroni_rate,
											(case @ls_saleWay when 'B' then - @ldec_sale_money when 'c' then 0 else  @ldec_sale_money end ),(case @ls_saleWay when 'B' then - @ln_saleQty when 'c' then 0 else  @ln_saleQty end ),'1',@ldec_ciceroni_rate,convert(varchar(10),@ldt_saleDateTime,121),
											 (case when @ls_more_cic_flag = '1' then @ls_cnte_id else '' end) )
										
							end
							-------------------------------------------------------------------------------------------------------------------------------------
						else
							-------------youdp-2014-02-25--------------旅行社提成--------------------------------------------------------------------------------
							begin 
								select top 1 @ldec_travel_rate = t_cn_item_cls_rate.rate,@ll_exist = 1,@ls_item_clsno = t_cn_item_cls_rate.item_clsno
								from t_cn_item_cls_rate with(nolock),t_bd_item_info with(nolock)
								where t_cn_item_cls_rate.type = '2' and
									  t_cn_item_cls_rate.item_clsno = left(t_bd_item_info.item_clsno,len(t_cn_item_cls_rate.item_clsno)) and 
									  len(t_cn_item_cls_rate.item_clsno) <= len(t_bd_item_info.item_clsno) and
									  t_bd_item_info.item_no = @ls_ItemNO and
									  t_cn_item_cls_rate.cic_sheet_no = @ls_cic_sheet_no and
									  ((@ls_more_cic_flag = '1' and t_cn_item_cls_rate.cnte_id = @ls_cnte_id) or (@ls_more_cic_flag <> '1'))
								order by t_cn_item_cls_rate.item_clsno desc

								if @ll_exist <> 1
									begin
										set @ll_exist = 0
										select top 1 @ldec_travel_rate = t_bd_item_cls.travel_rate,@ll_exist = 1,@ls_item_clsno = t_bd_item_cls.item_clsno
										from t_bd_item_cls with(nolock),t_bd_item_info with(nolock)
										where t_bd_item_cls.item_clsno = left(t_bd_item_info.item_clsno,len(t_bd_item_cls.item_clsno)) and 
											  len(t_bd_item_cls.item_clsno) <= len(t_bd_item_info.item_clsno) and
											  t_bd_item_info.item_no = @ls_ItemNO
										order by (case when t_bd_item_cls.travel_rate <= 0 then '1' else '2' end) desc,t_bd_item_cls.item_clsno desc
										if @ll_exist <> 1
											set @ldec_travel_rate = 0
									end
								
								if exists(select top 1 1 from t_rm_cn_daysum with(nolock) 
    													 where cic_sheet_no = @ls_cic_sheet_no and 
						   									   branch_no = left(@ls_branchNO,2) and
															   item_clsno = @ls_item_clsno and
															   fm_type = '2' and
															   oper_date = convert(varchar(10),@ldt_saleDateTime,121) and
															   ((@ls_more_cic_flag = '1' and t_rm_cn_daysum.cnte_id = @ls_cnte_id) or (@ls_more_cic_flag <> '1'))
															   )
									update t_rm_cn_daysum set real_amt = real_amt + (case @ls_saleWay when 'B' then - @ldec_sale_money when 'c' then 0 else  @ldec_sale_money end ) * rate,
															  sale_amt = sale_amt + (case @ls_saleWay when 'B' then - @ldec_sale_money when 'c' then 0 else  @ldec_sale_money end ),
															  sale_qnty = sale_qnty + (case @ls_saleWay when 'B' then - @ln_saleQty when 'c' then 0 else  @ln_saleQty end )
									where cic_sheet_no = @ls_cic_sheet_no and 
										  branch_no = left(@ls_branchNO,2) and
										  item_clsno = @ls_item_clsno and
										  fm_type = '2' and
										  oper_date = convert(varchar(10),@ldt_saleDateTime,121) and
										  ((@ls_more_cic_flag = '1' and t_rm_cn_daysum.cnte_id = @ls_cnte_id) or (@ls_more_cic_flag <> '1'))
								else
									insert into t_rm_cn_daysum (cic_sheet_no,branch_no,item_clsno,real_amt,sale_amt,sale_qnty,fm_type,rate,oper_date,cnte_id)
									values (@ls_cic_sheet_no,left(@ls_branchNO,2),@ls_item_clsno,(case @ls_saleWay when 'B' then - @ldec_sale_money when 'c' then 0 else  @ldec_sale_money end ) * @ldec_travel_rate,
											(case @ls_saleWay when 'B' then - @ldec_sale_money when 'c' then 0 else  @ldec_sale_money end ),(case @ls_saleWay when 'B' then - @ln_saleQty when 'c' then 0 else  @ln_saleQty end ),'2',@ldec_travel_rate,convert(varchar(10),@ldt_saleDateTime,121),
											(case when @ls_more_cic_flag = '1' then @ls_cnte_id else '' end) )					
							end
							--------------------------------------------------------------------------------------------------------------------------------------------
						set @ll_min_tab_id = @ll_min_tab_id + 1
					end
			  ------------------------------------------------------------------------------------------------------------------
		end
	-------------------------------------------------------------------------------------------------------------

     FETCH NEXT FROM inserted_cursor 
	INTO @ls_branchNO,@ls_ItemNO,@ln_saleQty,
		  @ln_sourcePrc,	  @ln_salePrc,
		  @ls_saleWay,	  @ldt_saleDateTime,
		  @ls_casher,	  @ls_spec_flag,
		  @ln_dctAmt,	  @flow_no,
		  @flow_id,	  @ls_counter_no,
		  @remote_flag,@ls_spec_sheet_no,@ls_cic_sheet_no,@ldec_sale_money
	
  end
rtn:
drop table #tab_cn_more_info
CLOSE inserted_cursor
DEALLOCATE inserted_cursor
END
go