if exists(select 1 from sysobjects where name='tr_pc_price_flow_master')
  drop trigger tr_pc_price_flow_master
go

create trigger tr_pc_price_flow_master on t_pc_price_flow_master 
for update 
as
begin
  if not update(approve_flag) return
  declare 
  @sheet_no         char(18)        ,--单据编号
  @branch_no        char(6)        ,--仓库编号
  @trans_no         char(2)        ,--交易编号
  @oper_date       datetime   ,--操作日期
  @oper_id         char(4),
  @work_date       datetime,  --审核日期
  @supcust_no     char(8), --供应商客户代码
  @sale_way       char(1),
  @item_no          char(20)        ,--货品代码
  @price_type     char(1)     ,--类型
  @start_date     datetime  ,--日期1
  @end_date     datetime  ,--日期2
  @week     char(7)  ,--星期
  @begin_time     char(8)  ,--时间1
  @end_time     char(8) ,--时间2
  @old_price      numeric(16,4)     ,--进货单价
  @new_price        numeric(16,4)     ,--零售单价
  @discount         numeric(5,4)    ,--折扣
  @buy_qty      numeric(16,4)   ,--客单限量
  @limited_qty      numeric(16,4)   ,--卖场限量
  @reach_val         numeric(16,2)         ,--其它1
  @add_val         numeric(16,2)         ,--其它2
  @mother1         char(20)         ,--其它1
  @mother2         char(20)         ,--其它2
  @other3         char(20),--其它3
  @i_sort_no         int     , --序号  
  @sort_no           char(5),--序号
  @s_newsheetno	 	char(18),
  @branch_id 	char(2),
  @vip_type	char(2),
  @dec_rate	numeric(16,4),
  @branch_list 	varchar(2000),
  @reach_vip	numeric(16,2), --'PH'促销单的限定积分积分
  @group_no	char(2),
  @trade_type   char(1),
  @cls_flag 	char(1),
  @cls_list 	char(10),
  @branch_list_tmp 	varchar(2000),
  @branch_list_cur 	varchar(2000),
  @start_pos integer 

  select @i_sort_no=0
  declare priceflow_cur cursor for
    select distinct m.trans_no, m.branch_no, m.sheet_no,m.supcust_no,m.oper_id,
      d.item_no,d.price_type, d.start_date,d.end_date,d.week,d.begin_time,d.end_time, d.old_price,
      d.new_price, discount,d.buy_qty,d.limited_qty,isnull(m.vip_type,''),isnull(m.reach_val,0),
      isnull(m.add_val,0),isnull(dec_rate,0),isnull(branch_list,''),isnull(m.reach_vip,0),isnull(d.group_no,''),
      m.cls_flag,m.cls_list,m.work_date,m.other1
      from inserted m, t_pc_price_flow_detail d   
     where ( m.sheet_no = d.sheet_no ) and  
           (m.approve_flag = '1') 

  open priceflow_cur
  fetch priceflow_cur into @trans_no, @branch_no, @sheet_no, @supcust_no,@oper_id,
    @item_no, @price_type, @start_date, @end_date, @week, @begin_time, @end_time, @old_price, 
    @new_price, @discount, @buy_qty,@limited_qty,@vip_type,@reach_val,@add_val,@dec_rate,@branch_list,@reach_vip,@group_no,
    @cls_flag,@cls_list,@work_date,@mother1

  while @@fetch_status=0
  begin
    select @i_sort_no=@i_sort_no+1
    select @sort_no=convert(char(5),@i_sort_no)
    

    if @trans_no='PS' or @trans_no='PN' or @trans_no='PH'or @trans_no='PU' or @trans_no='PE' or @trans_no = 'PA' or @trans_no = 'PG' or @trans_no = 'PL' or @trans_no = 'PY' or 
       @trans_no='PV' or @trans_no='PM' or @trans_no='PK'or @trans_no='PP'
    begin
      if @begin_time is null  select @begin_time=''
      if @end_time is null  select @end_time=''

      if len(@branch_no) = 6 --单个机构调价
	      begin
	          if @trans_no='PN'  or @trans_no='PE' --固定时间特价 超量购买特价
			     insert into  t_rm_spec_price (branch_no, item_no, special_type,vip_type, old_price, spe_price, discount,
		               sale_qty, start_date, end_date, week, begin_time, end_time,sheet_no,limited_qty,sort_no,dec_rate)  
		             values(@branch_no, @item_no, @price_type,@vip_type, @old_price, @new_price, @discount, 
		               @buy_qty, @start_date, @end_date, @week, @begin_time, @end_time,@sheet_no,@limited_qty,@sort_no,@dec_rate) 
		  else if @trans_no='PU'  --类别促销折扣
			       IF exists(select item_no from t_rm_spec_price where (item_no=@item_no and special_type = @price_type and branch_no = @branch_no and vip_type = @vip_type and @begin_time is null))
			                  begin   --begin Exist , Update
			                      
 									if (@discount <= 0)   --不作特价商品,将其价格和折扣都设置为零即可
			                           delete from t_rm_spec_price where (item_no = @item_no and special_type = @price_type and branch_no = @branch_no  and vip_type = @vip_type)
			                       else --更新特价信息（有效数据）
				                   update t_rm_spec_price set
				                          discount = @discount,
				                          start_date = @start_date,  end_date = @end_date, 
				                          begin_time = @begin_time, end_time = @end_time, 
				                          sheet_no = @sheet_no,week =@week
				                          where (item_no=@item_no and special_type = @price_type and 
			                                         branch_no = @branch_no  and vip_type = @vip_type)  
		          		   end   --end Exist , Update
	           		 else  --No Exist, Insert
			 		   begin                      
			 		       insert into  t_rm_spec_price (branch_no, item_no, special_type, vip_type,old_price, spe_price, discount,
			                         		sale_qty, start_date, end_date, begin_time, end_time, week, sheet_no,sort_no,dec_rate)  
			                    		values(@branch_no, @item_no, @price_type,@vip_type, 0, 0, @discount, 
			                           		0, @start_date, @end_date, @begin_time, @end_time, @week,@sheet_no,@sort_no,@dec_rate)  
				           end
	           else if @trans_no='PS' or @trans_no='PH' or @trans_no = 'PA' or @trans_no = 'PG' or @trans_no = 'PL' or @trans_no = 'PY' or @trans_no='PV' or @trans_no='PM' or @trans_no='PK'or @trans_no='PP'
			         begin
			           	if @trans_no='PH' select @price_type='9'
					IF exists(select item_no from t_rm_spec_price where (item_no=@item_no and special_type = @price_type and branch_no = @branch_no and special_type<>'4' and vip_type = @vip_type and @begin_time is null) )
				            begin   --begin Exist , Update
				              if (@new_price < 0 and @discount <= 0)   --不作特价商品,将其价格和折扣都设置为零即可
				                delete from t_rm_spec_price where (item_no = @item_no and special_type = @price_type and branch_no = @branch_no and special_type<>'4' and vip_type = @vip_type)
				              else -- 更新特价信息（有效数据）
				                update t_rm_spec_price set
				                    special_type = @price_type, old_price = @old_price,
				                    spe_price = @new_price, discount = @discount,
				                    sale_qty = @buy_qty, start_date = @start_date,  end_date = @end_date, 
				                    week = @week, 
				                    begin_time = @begin_time, end_time = @end_time,
				                    sheet_no = @sheet_no,limited_qty=@limited_qty,sort_no=@sort_no,
						    cls_flag = @cls_flag,cls_list = @cls_list
				                  where (item_no=@item_no and special_type = @price_type and branch_no = @branch_no and special_type<>'4' and vip_type = @vip_type)  
				            end ---- end Exist , Update
	            		   	else  -- No Exist, Insert
           				 	begin
							If @trans_no='PH' and @add_val = 0 and @new_price <> 0 
							   begin
								select @add_val = @new_price
							   end
							if @trans_no = 'PG' 
							   begin
								set @sort_no = @group_no
							   end
					
					                insert into  t_rm_spec_price (branch_no, item_no, special_type,vip_type, old_price, spe_price, discount,
					                  sale_qty, start_date, end_date, week, begin_time, end_time, sheet_no,limited_qty,sort_no,reach_val,add_val,dec_rate,reach_vip,cls_flag,cls_list,other1)  
					                values(@branch_no, @item_no, @price_type,@vip_type, @old_price, @new_price, @discount, 
					                  @buy_qty, @start_date, @end_date, @week, @begin_time, @end_time, @sheet_no,@limited_qty,@sort_no,@reach_val,@add_val,@dec_rate,@reach_vip,@cls_flag,@cls_list,@mother1)   
					  	end 
	     	 	end 
		end

      if @branch_list='' and len(@branch_no) = 3 --总部/分公司/办事处/配送中心调价
	   
   		begin
	        declare branch_cur cursor for
	        --select branch_no from t_bd_branch_info where property='2' and ( trade_type = '3' or  trade_type = '4'or  trade_type = '5' or  trade_type = '7' or  trade_type = '8') 
	      	select branch_no from t_bd_branch_info where  (trade_type = '3' or  trade_type = '4'or  trade_type = '5' or  trade_type = '7' or  trade_type = '8') and left(branch_no,3) = @branch_no --modified by zhangmz 20090205
	        open branch_cur
	        fetch branch_cur into @branch_no
	        while @@fetch_status=0
	        	begin
	            if @trans_no='PN' or @trans_no='PE' --特价
	               insert into  t_rm_spec_price (branch_no, item_no, special_type, vip_type, old_price, spe_price, discount,
	                  sale_qty, start_date, end_date, week, begin_time, end_time,  sheet_no,limited_qty,sort_no,dec_rate)  
	                values(@branch_no, @item_no, @price_type, @vip_type, @old_price, @new_price, @discount, 
	                  @buy_qty, @start_date, @end_date, @week, @begin_time, @end_time, @sheet_no,@limited_qty,@sort_no,@dec_rate)   
	
    		    else if @trans_no='PU'  --类别促销折扣
	       		IF exists(select item_no from t_rm_spec_price where (item_no=@item_no and special_type = @price_type and branch_no = @branch_no  and vip_type = @vip_type and @begin_time is null))
                  		begin   -- begin Exist , Update
		                       	if (@discount <= 0)   --不作特价商品,将其价格和折扣都设置为零即可
		                           delete from t_rm_spec_price where (item_no = @item_no and special_type = @price_type and branch_no = @branch_no  and vip_type = @vip_type)
		                       	else -- 更新特价信息（有效数据）
			                   update t_rm_spec_price set
			                          discount = @discount,
			                          start_date = @start_date,  end_date = @end_date, 
			                          begin_time = @begin_time, end_time = @end_time, 
						  									sheet_no = @sheet_no,week=@week
			                          where (item_no=@item_no and special_type = @price_type and 
		                                         branch_no = @branch_no  and vip_type = @vip_type)  
          				end   -- end Exist , Update
	          	else  -- No Exist, Insert
		 		  begin                      
		 		       insert into  t_rm_spec_price (branch_no, item_no, special_type,vip_type, old_price, spe_price, discount,
		                         		sale_qty, start_date, end_date, begin_time, end_time, week, sheet_no,sort_no,dec_rate)  
		                    		values(@branch_no, @item_no, @price_type, @vip_type, 0, 0, @discount, 
		                           		0, @start_date, @end_date, @begin_time, @end_time, @week, @sheet_no,@sort_no,@dec_rate)  
			          end
           	     else if @trans_no='PS' or @trans_no='PH' or @trans_no = 'PA' or @trans_no = 'PG' or @trans_no = 'PL' or @trans_no = 'PY' or  @trans_no='PV' or @trans_no='PM' or @trans_no='PK'or @trans_no='PP'
			          begin
			
			               if @trans_no='PH' select @price_type='9'
			
			               IF exists(select item_no from t_rm_spec_price where (item_no=@item_no and special_type = @price_type and branch_no = @branch_no and special_type<>'4' and vip_type = @vip_type and @begin_time is null))
			               begin 
			                    if (@new_price <= 0 and @discount <= 0)   --不作特价商品,将其价格和折扣都设置为零即可
			                        delete from t_rm_spec_price where (item_no = @item_no and special_type = @price_type and branch_no = @branch_no and special_type<>'4' and vip_type = @vip_type)
			                    else --更新特价信息（有效数据）
			                         update t_rm_spec_price set
			                             special_type = @price_type, old_price = @old_price,
			                             spe_price = @new_price, discount = @discount,
			                             sale_qty = @buy_qty, start_date = @start_date, end_date = @end_date,week = @week, 
			                             begin_time = @begin_time, end_time = @end_time, 
			                             sheet_no = @sheet_no,limited_qty=@limited_qty,sort_no=@sort_no,
						     cls_flag = @cls_flag,cls_list = @cls_list
			                           where (item_no=@item_no and special_type = @price_type and branch_no = @branch_no and special_type <>'4' and vip_type = @vip_type)  
			            end 
               	     else
			            begin
					    If @trans_no='PH' and @add_val = 0 and @new_price <> 0 
						begin
							select @add_val = @new_price
						end
					    if @trans_no = 'PG' 
						begin
							set @sort_no = @group_no
						end
			
			                    insert into  t_rm_spec_price (branch_no, item_no, special_type,vip_type, old_price, spe_price, discount,
			                        sale_qty, start_date, end_date, week, begin_time, end_time, sheet_no,limited_qty,sort_no,reach_val,add_val,dec_rate,reach_vip,cls_flag,cls_list,other1)  
			                      values(@branch_no, @item_no, @price_type,@vip_type, @old_price, @new_price, @discount, 
			                        @buy_qty, @start_date, @end_date, @week, @begin_time, @end_time,@sheet_no,@limited_qty,@sort_no,@reach_val,@add_val,@dec_rate,@reach_vip,@cls_flag,@cls_list,@mother1)   
			             end
            end
            fetch branch_cur into @branch_no
         end 
        deallocate  branch_cur 
      end 
      
      ------------ add by zhangmz 20090205
      if @branch_list='' and len(@branch_no) = 3 and @branch_no = 'ALL'--所有门店
	      begin
		select @trade_type = trade_type from t_bd_branch_info where branch_no = (select sys_var_value from t_sys_system where sys_var_id = 'g_branch_no')
		if @trade_type = '1'--办事处所有门店不包含总部的
			begin
				declare branch_cur_all_bsc cursor for
				select branch_no from t_bd_branch_info where branch_no like (select sys_var_value from t_sys_system where sys_var_id = 'g_branch_no') + '%'
			        open branch_cur_all_bsc
			        fetch branch_cur_all_bsc into @branch_no
			        while @@fetch_status=0
			        begin
			            if @trans_no='PN' or @trans_no='PE' --特价
			               insert into  t_rm_spec_price (branch_no, item_no, special_type, vip_type, old_price, spe_price, discount,
			                  sale_qty, start_date, end_date, week, begin_time, end_time,  sheet_no,limited_qty,sort_no,dec_rate)  
			                values(@branch_no, @item_no, @price_type, @vip_type, @old_price, @new_price, @discount, 
			                  @buy_qty, @start_date, @end_date, @week, @begin_time, @end_time, @sheet_no,@limited_qty,@sort_no,@dec_rate)   
			
		    		    else if @trans_no='PU'  --类别促销折扣
			       		IF exists(select item_no from t_rm_spec_price where (item_no=@item_no and special_type = @price_type and branch_no = @branch_no  and vip_type = @vip_type and @begin_time is null))
		                  		begin   -- begin Exist , Update
				                       	if (@discount <= 0)   --不作特价商品,将其价格和折扣都设置为零即可
				                           delete from t_rm_spec_price where (item_no = @item_no and special_type = @price_type and branch_no = @branch_no  and vip_type = @vip_type)
				                       	else -- 更新特价信息（有效数据）
					                   update t_rm_spec_price set
					                          discount = @discount,
					                          start_date = @start_date,  end_date = @end_date, 
					                          begin_time = @begin_time, end_time = @end_time, 
								  									sheet_no = @sheet_no,week=@week
					                          where (item_no=@item_no and special_type = @price_type and 
				                                         branch_no = @branch_no  and vip_type = @vip_type)  
		          			end   -- end Exist , Update
			          	else  -- No Exist, Insert
				 		  begin                      
				 		       insert into  t_rm_spec_price (branch_no, item_no, special_type,vip_type, old_price, spe_price, discount,
				                         		sale_qty, start_date, end_date, begin_time, end_time, week, sheet_no,sort_no,dec_rate)  
				                    		values(@branch_no, @item_no, @price_type, @vip_type, 0, 0, @discount, 
				                           		0, @start_date, @end_date, @begin_time, @end_time, @week, @sheet_no,@sort_no,@dec_rate)  
		    end
		           	     else if @trans_no='PS' or @trans_no='PH' or @trans_no = 'PA' or @trans_no = 'PG' or @trans_no = 'PL' or @trans_no='PY' or  @trans_no='PV' or @trans_no='PM' or @trans_no='PK'or @trans_no='PP'
					          begin
					
					               if @trans_no='PH' select @price_type='9'
					
						               IF exists(select item_no from t_rm_spec_price where (item_no=@item_no and special_type = @price_type and branch_no = @branch_no and special_type<>'4' and vip_type = @vip_type and @begin_time is null))
						               begin 
						                    if (@new_price <= 0 and @discount <= 0)   --不作特价商品,将其价格和折扣都设置为零即可
						                        delete from t_rm_spec_price where (item_no = @item_no and special_type = @price_type and branch_no = @branch_no and special_type<>'4' and vip_type = @vip_type)
						                    else --更新特价信息（有效数据）
						                         update t_rm_spec_price set
						                             special_type = @price_type, old_price = @old_price,
						                             spe_price = @new_price, discount = @discount,
						                             sale_qty = @buy_qty, start_date = @start_date, end_date = @end_date,
						                             week = @week, 
						                             begin_time = @begin_time, end_time = @end_time, 
						                             sheet_no = @sheet_no,limited_qty=@limited_qty,sort_no=@sort_no,cls_flag = @cls_flag,cls_list = @cls_list
						                           where (item_no=@item_no and special_type = @price_type and branch_no = @branch_no and special_type<>'4' and vip_type = @vip_type)  
						            	end 
		               	    			else
							            begin
									    If @trans_no='PH' and @add_val = 0 and @new_price <> 0 
										begin
											select @add_val = @new_price
										end
									    if @trans_no = 'PG' 
										begin
											set @sort_no = @group_no
										end
							
							                    insert into  t_rm_spec_price (branch_no, item_no, special_type,vip_type, old_price, spe_price, discount,
							                        sale_qty, start_date, end_date, week, begin_time, end_time, sheet_no,limited_qty,sort_no,reach_val,add_val,dec_rate,reach_vip,cls_flag,cls_list,other1)  
							                      values(@branch_no, @item_no, @price_type,@vip_type, @old_price, @new_price, @discount, 
							                        @buy_qty, @start_date, @end_date, @week, @begin_time, @end_time,@sheet_no,@limited_qty,@sort_no,@reach_val,@add_val,@dec_rate,@reach_vip,@cls_flag,@cls_list,@mother1)   
							             end
		            			  end
		            			  fetch branch_cur_all_bsc into @branch_no
		         	end 
		        	deallocate  branch_cur_all_bsc 
			end
		else
			begin
				declare branch_cur_all cursor for
				select branch_no from t_bd_branch_info where trade_type = '3' or  trade_type = '4'or  trade_type = '5' or  trade_type = '7' or  trade_type = '8'
			        open branch_cur_all
			        fetch branch_cur_all into @branch_no
			        while @@fetch_status=0
			        begin
			            if @trans_no='PN' or @trans_no='PE' --特价
			               insert into  t_rm_spec_price (branch_no, item_no, special_type, vip_type, old_price, spe_price, discount,
			                  sale_qty, start_date, end_date, week, begin_time, end_time,  sheet_no,limited_qty,sort_no,dec_rate)  
			                values(@branch_no, @item_no, @price_type, @vip_type, @old_price, @new_price, @discount,			     
             @buy_qty, @start_date, @end_date, @week, @begin_time, @end_time, @sheet_no,@limited_qty,@sort_no,@dec_rate)   
			
		    		    else if @trans_no='PU'  --类别促销折扣
			       		IF exists(select item_no from t_rm_spec_price where (item_no=@item_no and special_type = @price_type and branch_no = @branch_no  and vip_type = @vip_type and @begin_time is null))
		                  		begin   -- begin Exist , Update
				                       	if (@discount <= 0)   --不作特价商品,将其价格和折扣都设置为零即可
				                           delete from t_rm_spec_price where (item_no = @item_no and special_type = @price_type and branch_no = @branch_no  and vip_type = @vip_type)
				                       	else -- 更新特价信息（有效数据）
					                   update t_rm_spec_price set
					                          discount = @discount,
					                          start_date = @start_date,  end_date = @end_date, 
					                          begin_time = @begin_time, end_time = @end_time, 
								  									sheet_no = @sheet_no,week=@week
					                          where (item_no=@item_no and special_type = @price_type and 
				                                         branch_no = @branch_no  and vip_type = @vip_type)  
		          			end   -- end Exist , Update
			          	else  -- No Exist, Insert
				 		  begin                      
				 		       insert into  t_rm_spec_price (branch_no, item_no, special_type,vip_type, old_price, spe_price, discount,
				                         		sale_qty, start_date, end_date, begin_time, end_time, week, sheet_no,sort_no,dec_rate)  
				                    		values(@branch_no, @item_no, @price_type, @vip_type, 0, 0, @discount, 
				                           		0, @start_date, @end_date, @begin_time, @end_time, @week, @sheet_no,@sort_no,@dec_rate)  
					          end
		           	     else if @trans_no='PS' or @trans_no='PH' or @trans_no = 'PA' or @trans_no = 'PG' or @trans_no = 'PL' or @trans_no='PY' or @trans_no='PV' or @trans_no='PM' or @trans_no='PK'or @trans_no='PP'
					          begin
					
					               if @trans_no='PH' select @price_type='9'
					
						               IF exists(select item_no from t_rm_spec_price where (item_no=@item_no and special_type = @price_type and branch_no = @branch_no and special_type<>'4' and vip_type = @vip_type and @begin_time is null))
						               begin 
						                    if (@new_price <= 0 and @discount <= 0)   --不作特价商品,将其价格和折扣都设置为零即可
						                        delete from t_rm_spec_price where (item_no = @item_no and special_type = @price_type and branch_no = @branch_no and special_type<>'4' and vip_type = @vip_type)
						                    else --更新特价信息（有效数据）
						                         update t_rm_spec_price set
						                             special_type = @price_type, old_price = @old_price,
						                             spe_price = @new_price, discount = @discount,
						                             sale_qty = @buy_qty, start_date = @start_date, end_date = @end_date,
						                             week = @week, 
						                             begin_time = @begin_time, end_time = @end_time, 
						                             sheet_no = @sheet_no,limited_qty=@limited_qty,sort_no=@sort_no,
									     cls_flag = @cls_flag,cls_list = @cls_list
						                           where (item_no=@item_no and special_type = @price_type and branch_no = @branch_no and special_type<>'4' and vip_type = @vip_type)  
						            	end 
		               	    			else
							            begin
									    If @trans_no='PH' and @add_val = 0 and @new_price <> 0 
										begin
											select @add_val = @new_price
										end
									    if @trans_no = 'PG' 
										begin
											set @sort_no = @group_no
										end
							
							                    insert into  t_rm_spec_price (branch_no, item_no, special_type,vip_type, old_price, spe_price, discount,sale_qty, start_date, end_date, week, begin_time, end_time, sheet_no,limited_qty,sort_no,reach_val,add_val,dec_rate,reach_vip,cls_flag,cls_list,other1)  
							                      values(@branch_no, @item_no, @price_type,@vip_type, @old_price, @new_price, @discount, 
							                        @buy_qty, @start_date, @end_date, @week, @begin_time, @end_time,@sheet_no,@limited_qty,@sort_no,@reach_val,@add_val,@dec_rate,@reach_vip,@cls_flag,@cls_list,@mother1)   
							             end
		            			  end
		            			  fetch branch_cur_all into @branch_no
		         	end 
		        	deallocate  branch_cur_all 
			end
	        
      	end 
	---------

      if len(@branch_list) > 0 and len(@branch_no) = 3 --门店列表调价
	begin		
          select @branch_list_tmp = @branch_list 
          if right(@branch_list_tmp,1)<>'|'
	  	select @branch_list_tmp = @branch_list_tmp + '|'
	  if left(@branch_list_tmp,1)='|'
	  	select @branch_list_tmp = substring(@branch_list_tmp,2,len(@branch_list_tmp) - 1) 
		select @start_pos=CHARINDEX('|',@branch_list_tmp)
    	while  @start_pos>0
   ------- 因为这里的 @branch_list 机构的长度不一样的，直接用截位会有问题 PengLR 2011-12-31

	      ---  while len(substring(@branch_list,2,6) ) > 0
	
      		begin
			select @branch_list_cur=substring(@branch_list_tmp,1,@start_pos-1)
        		if @branch_list_cur is null or len(ltrim(rtrim(@branch_list_cur)))=0
				break
	       select @branch_no = @branch_list_cur
		  ---  select @branch_no = substring(@branch_list,2,6)
		   --- select @branch_list = right(@branch_list, len(@branch_list) - 7 )

	            if @trans_no='PN' or @trans_no='PE' --特价
	               insert into  t_rm_spec_price (branch_no, item_no, special_type, vip_type, old_price, spe_price, discount,
	                  sale_qty, start_date, end_date, week, begin_time, end_time,  sheet_no,limited_qty,sort_no,dec_rate)  
	                values(@branch_no, @item_no, @price_type, @vip_type, @old_price, @new_price, @discount, 
	                  @buy_qty, @start_date, @end_date, @week, @begin_time, @end_time, @sheet_no,@limited_qty,@sort_no,@dec_rate)   
		    else if @trans_no='PU'  --类别促销折扣
		       IF exists(select item_no from t_rm_spec_price where (item_no=@item_no and special_type = @price_type and branch_no = @branch_no  and vip_type = @vip_type and @begin_time is null))
	                  begin   -- begin Exist , Update
	                       if (@discount <= 0)   --不作特价商品,将其价格和折扣都设置为零即可
	                           delete from t_rm_spec_price where (item_no = @item_no and special_type = @price_type and branch_no = @branch_no  and vip_type = @vip_type)
	                       else -- 更新特价信息（有效数据）
		                   update t_rm_spec_price set
		                          discount = @discount,
		                          start_date = @start_date,  end_date = @end_date, 
		                          begin_time = @begin_time, end_time = @end_time, 
					  sheet_no = @sheet_no,week=@week
		                          where (item_no=@item_no and special_type = @price_type and 
	                                         branch_no = @branch_no  and vip_type = @vip_type)  
		          end   -- end Exist , Update
		          else  -- No Exist, Insert
	 		  begin                      
	 		       insert into  t_rm_spec_price (branch_no, item_no, special_type,vip_type, old_price, spe_price, discount,
	                         		sale_qty, start_date, end_date, begin_time, end_time, week, sheet_no,sort_no,dec_rate)  
	                    		values(@branch_no, @item_no, @price_type, @vip_type, 0, 0, @discount, 
	                           		0, @start_date, @end_date, @begin_time, @end_time, @week, @sheet_no,@sort_no,@dec_rate)  
		          end
	            else if @trans_no='PS' or @trans_no='PH' or @trans_no = 'PA' or @trans_no = 'PG' or @trans_no = 'PL' or @trans_no = 'PY' or @trans_no='PV' or @trans_no='PM' or @trans_no='PK'or @trans_no='PP'
	             begin
		
	               if @trans_no='PH' select @price_type='9'
	
	               IF exists(select item_no from t_rm_spec_price where (item_no=@item_no and special_type = @price_type and branch_no = @branch_no and special_type<>'4' and vip_type = @vip_type and @begin_time is null))
	               begin 
	                    if (@new_price <= 0 and @discount <= 0)   --不作特价商品,将其价格和折扣都设置为零即可
	                        delete from t_rm_spec_price where (item_no = @item_no and special_type = @price_type and branch_no = @branch_no and special_type<>'4' and vip_type = @vip_type)
	                    else --更新特价信息（有效数据）
	                         update t_rm_spec_price set
	                             special_type = @price_type, old_price = @old_price,
	                             spe_price = @new_price, discount = @discount,
	                             sale_qty = @buy_qty, start_date = @start_date, end_date = @end_date,
	                             week = @week, 
	                             begin_time = @begin_time, end_time = @end_time, 
	                             sheet_no = @sheet_no,limited_qty=@limited_qty,sort_no=@sort_no,
				     			 cls_flag = @cls_flag,cls_list = @cls_list
	                           where (item_no=@item_no and special_type = @price_type and branch_no = @branch_no and special_type<>'4' and vip_type = @vip_type)  
	                end 
	                else
	                begin

			    If @trans_no='PH' and @add_val = 0 and @new_price <> 0 
				begin
		   			select @add_val = @new_price
				end
			    if @trans_no = 'PG' 
				begin
					set @sort_no = @group_no
				end

	                   insert into  t_rm_spec_price (branch_no, item_no, special_type,vip_type, old_price, spe_price, discount,
	                   sale_qty, start_date, end_date, week, begin_time, end_time, sheet_no,limited_qty,sort_no,reach_val,add_val,dec_rate,reach_vip,cls_flag,cls_list,other1)  
	                    values(@branch_no, @item_no, @price_type,@vip_type, @old_price, @new_price, @discount, 
	                        @buy_qty, @start_date, @end_date, @week, @begin_time, @end_time,@sheet_no,@limited_qty,@sort_no,@reach_val,@add_val,@dec_rate,@reach_vip,@cls_flag,@cls_list,@mother1)   
	               	end
	            end
          
	  select @branch_list_tmp=substring(@branch_list_tmp,@start_pos+1,len(@branch_list_tmp))
      select @start_pos=CHARINDEX('|',@branch_list_tmp)  

	       end 
	end

	

    end 
	
    if @branch_list<>'' and len(@branch_no) = 2 --按分店区域调价. add by zhangmz 2009-10-13
	      begin
	        declare branch_cur_area cursor for
	      	select branch_no from t_bd_branch_info where branch_clsno = @branch_no and (trade_type = '3' or  trade_type = '4'or  trade_type = '5' or  trade_type = '7' or  trade_type = '8')
	        open branch_cur_area
	        fetch branch_cur_area into @branch_no
	        while @@fetch_status=0
	        begin
	            if @trans_no='PN' or @trans_no='PE' --特价
	               insert into  t_rm_spec_price (branch_no, item_no, special_type, vip_type, old_price, spe_price, discount,
	                  sale_qty, start_date, end_date, week, begin_time, end_time,  sheet_no,limited_qty,sort_no,dec_rate)  
	                values(@branch_no, @item_no, @price_type, @vip_type, @old_price, @new_price, @discount, 
	                  @buy_qty, @start_date, @end_date, @week, @begin_time, @end_time, @sheet_no,@limited_qty,@sort_no,@dec_rate)   
	
    		    else if @trans_no='PU'  --类别促销折扣
	       		IF exists(select item_no from t_rm_spec_price where (item_no=@item_no and special_type = @price_type and branch_no = @branch_no  and vip_type = @vip_type and @begin_time is null))
                  		begin   -- begin Exist , Update
		                       	if (@discount <= 0)   --不作特价商品,将其价格和折扣都设置为零即可
		                           delete from t_rm_spec_price where (item_no = @item_no and special_type = @price_type and branch_no = @branch_no  and vip_type = @vip_type)
		                       	else -- 更新特价信息（有效数据）
			                   update t_rm_spec_price set
			                          discount = @discount,
			                          start_date = @start_date,  end_date = @end_date, 
			                          begin_time = @begin_time, end_time = @end_time, 
						  									sheet_no = @sheet_no,week=@week
			                          where (item_no=@item_no and special_type = @price_type and 
		                                         branch_no = @branch_no  and vip_type = @vip_type)  
          			end   -- end Exist , Update
	          	else  -- No Exist, Insert
		 		  begin                      
		 		       insert into  t_rm_spec_price (branch_no, item_no, special_type,vip_type, old_price, spe_price, discount,
		                         		sale_qty, start_date, end_date, begin_time, end_time, week, sheet_no,sort_no,dec_rate)  
		                    		values(@branch_no, @item_no, @price_type, @vip_type, 0, 0, @discount, 
		                           		0, @start_date, @end_date, @begin_time, @end_time, @week, @sheet_no,@sort_no,@dec_rate)  
			          end
           	    else if @trans_no='PS' or @trans_no='PH' or @trans_no = 'PA' or @trans_no = 'PG' or @trans_no = 'PL' or @trans_no = 'PY' or @trans_no='PV' or @trans_no='PM' or @trans_no='PK'or @trans_no='PP'
			          begin
			
			               if @trans_no='PH' select @price_type='9'
			
			               IF exists(select item_no from t_rm_spec_price where (item_no=@item_no and special_type = @price_type and branch_no = @branch_no and special_type<>'4' and vip_type = @vip_type and @begin_time is null))
			              	    begin 
				                    if (@new_price <= 0 and @discount <= 0)   --不作特价商品,将其价格和折扣都设置为零即可
				                        delete from t_rm_spec_price where (item_no = @item_no and special_type = @price_type and branch_no = @branch_no and special_type<>'4' and vip_type = @vip_type)
				                    else --更新特价信息（有效数据）
				                         update t_rm_spec_price set
				                             special_type = @price_type, old_price = @old_price,
				                             spe_price = @new_price, discount = @discount,
				                             sale_qty = @buy_qty, start_date = @start_date, end_date = @end_date,
				                             week = @week, 
				                             begin_time = @begin_time, end_time = @end_time, 
				                             sheet_no = @sheet_no,limited_qty=@limited_qty,sort_no=@sort_no,cls_flag = @cls_flag,cls_list = @cls_list
				                           where (item_no=@item_no and special_type = @price_type and branch_no = @branch_no and special_type<>'4' and vip_type = @vip_type)  
				             end 
               	    			else
				            begin
						    If @trans_no='PH' and @add_val = 0 and @new_price <> 0 
							begin
								select @add_val = @new_price
							end
						    if @trans_no = 'PG' 
							begin
								set @sort_no = @group_no
							end
				
				                    insert into  t_rm_spec_price (branch_no, item_no, special_type,vip_type, old_price, spe_price, discount,
				                        sale_qty, start_date, end_date, week, begin_time, end_time, sheet_no,limited_qty,sort_no,reach_val,add_val,dec_rate,reach_vip,cls_flag,cls_list,other1)  
				                      values(@branch_no, @item_no, @price_type,@vip_type, @old_price, @new_price, @discount, 
				                        @buy_qty, @start_date, @end_date, @week, @begin_time, @end_time,@sheet_no,@limited_qty,@sort_no,@reach_val,@add_val,@dec_rate,@reach_vip,@cls_flag,@cls_list,@mother1)   
				             end
            			end
            		fetch branch_cur_area into @branch_no
         	end 
        	deallocate  branch_cur_area 
      	end 

    if @trans_no='PC' -- 成本调价单
    begin
       --modify by dailj因启用数据传输服务,成本调价改为在t_im_flow表执行pr_pc_change_costprice更新价格
       --exec pr_pc_change_costprice @item_no, @branch_no, @new_price
       Insert into t_im_flow(voucher_no,oper_date, item_no,real_qty,price,cost_price,
	     sheet_amt,branch_no,d_branch_no,trans_no,db_no,supcust_no,oper_id, pre_qnty,acc_date)
	     values(@sheet_no,@work_date,@item_no,0,@new_price,@new_price,
	       0,@branch_no,'','PC','+','',@oper_id,0,@work_date)
	     if @@error != 0 
                begin
                deallocate  priceflow_cur
                return
             end
    end
    
    select @branch_no=ltrim(rtrim(sys_var_value)) from t_sys_system where lower(sys_var_id)='g_branch_no'
    update t_pc_price_flow_master set branch_flag=@branch_no where sheet_no=@sheet_no
    
    if @trans_no='PX' -- or @trans_no='PA' or @trans_no='PB' or @trans_no='PR' or @trans_no='PJ'
         exec pr_pc_price_flow_effect
    
    if @@error != 0 
    begin
       deallocate  priceflow_cur
       return
    end
    fetch priceflow_cur into @trans_no, @branch_no, @sheet_no, @supcust_no,@oper_id,
       @item_no, @price_type, @start_date, @end_date, @week, @begin_time, @end_time, @old_price, 
       @new_price,  @discount, @buy_qty, @limited_qty, @vip_type,@reach_val,@add_val,@dec_rate,@branch_list,@reach_vip,@group_no,
       @cls_flag,@cls_list,@work_date,@mother1
      
  end

  deallocate  priceflow_cur 
end 
go