IF EXISTS (SELECT * FROM dbo.sysobjects  WHERE id =OBJECT_ID(N'[dbo].[pr_pc_price_flow_effect]') and  OBJECTPROPERTY(id,N'IsProcedure')=1)
DROP procedure [dbo].[pr_pc_price_flow_effect]
GO

create procedure pr_pc_price_flow_effect 
as
begin

  declare 
  @trans_no            varchar(2)        ,-- 交易编号
  @branch_no           varchar(6)        ,-- 仓库编号
  @sheet_no            varchar(14)       ,-- 单据编号
  @item_no             varchar(20)       ,-- 货品代码
  @valid_flag          char(1)           ,-- 生效状态
  @new_price           numeric(16,4)     ,-- 新单价
  @g_branch_no         varchar(6)        ,-- 系统仓库编号
  @is_distributed      char(1),           -- 分布式版
  @update_date         char(10)          ,-- 龙飞收款机接口 改t_bd_item_info.other3
  @old_price            numeric(16,4)     ,-- 原进价
  @old_price1           numeric(16,4)     ,-- 原售价
  @new_price1           numeric(16,4)     ,-- 新售价
  @old_price2           numeric(16,4)     ,-- 原会员价
  @new_price2           numeric(16,4)     ,-- 新会员价
  @old_price3           numeric(16,4)     ,-- 原最低价
  @new_price3           numeric(16,4)     ,-- 新最低价
  @old_price4           numeric(16,4)     ,-- 原配送价
  @new_price4           numeric(16,4)     ,-- 新配送价
  @branch_flag         varchar(10)        ,-- 调价制单机构
  @ls_com_flag         char(1)            ,-- 传输标志
  @ls_other2           varchar(20)        ,
  @ls_other3           varchar(20)        ,
  @ls_branch_group     varchar(255)       ,-- 店组
  @branchlist          varchar(255)       ,-- 店组
  @ChgInPrice_HQ       char(1)            ,
  @trade_type          char(1)            ,
  @chgType             varchar(10)        ,--调价类型(6位字串:'111111') 位:1 进货价,2 批发价, 3 零售价, 4会员价, 5 配送价
  @have_hq_branch      char(1),
  @work_date     datetime,
  @valid_date     datetime
  
  --　设置过程运行标志
  if not exists(select sys_var_value from t_sys_system where lower(sys_var_id)='change_price')
      insert into t_sys_system(sys_var_id,sys_var_name) values('change_price','change_price')
  else
      return
 
  select @update_date=convert(char(10), getdate(), 121)
      
  select @g_branch_no = sys_var_value from t_sys_system where lower(sys_var_id)='g_branch_no'
  if @g_branch_no is null     select @g_branch_no = '00'
  
  select @trade_type = trade_type from t_bd_branch_info where branch_no = @g_branch_no
  select @is_distributed = sys_var_value from t_sys_system where lower(sys_var_id)='is_distributed'
  if @is_distributed is null  select @is_distributed = '0'

  --总部集中调进价 modified by wujs 2005-11-08
  select @ChgInPrice_HQ = sys_var_value from t_sys_system where sys_var_id = 'ChgInPrice_HQ'
  select @ChgInPrice_HQ = isnull(@ChgInPrice_HQ, '0')
  

  -- add by wujs 2005-08-17 a.other2,   a.other3, 店组信息
  -- valid_flag ( '0':未生效, '1':已生效 , '2':已更新仓库价格表但未生效)
  if @is_distributed = '1'  --总店
     declare priceflow_cursor cursor for
       select distinct a.trans_no, a.branch_no, a.sheet_no, a.other2,   a.other3,  a.chgtype,a.branchlist,
                       a.valid_flag, b.item_no, b.new_price,b.old_price,b.new_price1,b.old_price1,
           b.new_price2, b.old_price2,b.new_price3,b.old_price3, a.branch_flag,b.old_price4,b.new_price4,a.work_date,a.valid_date
         from t_pc_price_flow_master a, 
              t_pc_price_flow_detail b   
        where ( a.valid_flag = '0' or 
                ( a.valid_flag = '1' and a.branch_no in (
                   Select branch_no  From t_bd_branch_info where (property = '0' and (trade_type = '1' or trade_type = '3' or trade_type = '2' or trade_type = '0'))
                   )
                )
              ) and 
              ( a.approve_flag = '1') and ( a.trans_no = 'PX' or a.trans_no = 'LK' ) and
              ( a.valid_date <= getdate()) and 
              ( a.sheet_no = b.sheet_no )
        order by a.work_date
  else                       --分店
     declare priceflow_cursor cursor for
       select distinct a.trans_no, a.branch_no, a.sheet_no, a.other2,   a.other3, a.chgtype,a.branchlist,
                       a.valid_flag, b.item_no, b.new_price,b.old_price,b.new_price1,b.old_price1,
           b.new_price2,b.old_price2,b.new_price3,b.old_price3, a.branch_flag,b.old_price4,b.new_price4,a.work_date,a.valid_date
         from t_pc_price_flow_master a, 
              t_pc_price_flow_detail b   
        where ( a.valid_flag = '0' or a.valid_flag = '2')
          and ( a.approve_flag = '1') and ( a.trans_no = 'PX'or a.trans_no = 'LK') 
          and ( a.sheet_no = b.sheet_no ) 
          and ( a.valid_date <= getdate())
        order by a.work_date
  
  
  open priceflow_cursor
  fetch priceflow_cursor into @trans_no, @branch_no, @sheet_no, @ls_other2, @ls_other3, @chgType,@branchlist,
                           @valid_flag, @item_no, @new_price,@old_price,@new_price1,@old_price1,
         @new_price2,@old_price2,@new_price3,@old_price3,@branch_flag,@old_price4,@new_price4,@work_date,@valid_date
  while @@fetch_status=0
  begin

	
    if @trans_no='PX'  -- 进价\零售价\会员价\批发价
    begin
       select @ls_branch_group = isnull(@branchlist, '')
             
	  /******* 处理调价单中不同门店调价不同的问题 ******/
	  IF Exists( select 1 from t_pc_price_flow_branch where sheet_no = @sheet_no and branch_no = @g_branch_no and item_no = @item_no)
	  begin
		select	@new_price  = max(new_price), 
				@new_price1 = max(new_price1), 
				@new_price2 = max(new_price2),
				@new_price3 = max(new_price3),
				@new_price4 = max(new_price4)
		from t_pc_price_flow_branch 
		where sheet_no = @sheet_no and branch_no = @g_branch_no and item_no = @item_no
	  end      
	  /*************************************************/

       -- modified by wujs 2005-08-17 增加店组处理
       --1.先更新商品档案中价格(调价门店含本店时)
       if @branch_no = @g_branch_no or @branch_no = 'ALL' or 
          (len(@branch_no)=3 and @branch_no <> 'ALL' and charindex(@g_branch_no,@ls_branch_group) > 0)
       begin
          -- Modified by huangc  2005-01-04 最低售价-->批发价
          if @trade_type = '2'  --加盟店
          begin
            --增加调价价格选择 wujs 2006-09-12
            --update t_bd_item_info set sale_price=@new_price1,vip_price=@new_price2, base_price=@new_price3,other3=@update_date where (item_no = @item_no)
            update t_bd_item_info set
               sup_ly_rate = (case when substring(@chgType, 2, 1) = '1' then @new_price3 else sup_ly_rate end),
               sale_price = (case when substring(@chgType, 3, 1) = '1'  then @new_price1 else sale_price end),
               vip_price  = (case when substring(@chgType, 4, 1) = '1'  then @new_price2 else vip_price end), 
               other3 = @update_date where (item_no = @item_no)
          end
          else
          begin
            --增加调价价格选择 wujs 2006-09-12
            --update t_bd_item_info set price = @new_price,sale_price=@new_price1,vip_price=@new_price2,base_price=@new_price3,other3=@update_date where (item_no = @item_no)
            update t_bd_item_info set 
               price     = (case when substring(@chgType, 1, 1) = '1'  then @new_price else price end),
              sup_ly_rate = (case when substring(@chgType, 2, 1) = '1' then @new_price3 else sup_ly_rate end),
              sale_price = (case when substring(@chgType, 3, 1) = '1'  then @new_price1 else sale_price end),
               vip_price = (case when substring(@chgType, 4, 1) = '1'  then @new_price2 else vip_price end),
               other3 = @update_date where (item_no = @item_no)
          end
       end
       
       if @is_distributed = '1' or @is_distributed = '0' --总店、托管分店
       begin
            if @branch_no = 'All'
            begin
            --------------------------------------------------------------------------------------------
               ----不存在时说明所有门店以同一价格调价
                if not exists (select 1 from t_pc_price_flow_branch where sheet_no = @sheet_no and item_no = @item_no)
                  begin
                     update t_pc_branch_price set 
                          price       = (case when substring(@chgType, 1, 1) = '1'  then @new_price  else price end),
                          sup_ly_rate = (case when substring(@chgType, 2, 1) = '1' then @new_price3 else sup_ly_rate end),
                          sale_price  = (case when substring(@chgType, 3, 1) = '1'  then @new_price1 else sale_price end),
                          vip_price   = (case when substring(@chgType, 4, 1) = '1'  then @new_price2 else vip_price end)
                      where (item_no = @item_no)
                   end
                else  ---存在则按门店分别更新各自的价格
                  begin
            	      update a set
		          price       = (case when substring(@chgType, 1, 1) = '1'  then b.new_price  else a.price end),
				  sup_ly_rate = (case when substring(@chgType, 2, 1) = '1'  then b.new_price3 else a.sup_ly_rate end),
				  sale_price  = (case when substring(@chgType, 3, 1) = '1'  then b.new_price1 else a.sale_price end),
				  vip_price   = (case when substring(@chgType, 4, 1) = '1'  then b.new_price2 else a.vip_price end)
            	        from t_pc_branch_price a, t_pc_price_flow_branch b
				where	a.item_no	= b.item_no 
					and a.branch_no = b.branch_no 
					and b.sheet_no	= @sheet_no 
					and b.item_no	= @item_no
                  end 
		
                -- Modified by wujs 2006-09-13
                if (substring(@chgType, 5, 1) = '1') --是否调配送价
                begin
                    if @g_branch_no = '00' --and @new_price4 <> @old_price4--改总店的配送价
                        update t_bd_item_info set trans_price = @new_price4 where item_no = @item_no
                     --改加盟店的配送价 进货价 = 配送价 注意!!!!!!
		    --if @new_price4 <> @old_price4
                   	update t_pc_branch_price 
                   	set price = @new_price4,com_flag='0'
                    where (item_no = @item_no and branch_no in (select branch_no from t_bd_branch_info where trade_type = '2'))                    
                end
            end
            else if (len(@branch_no)=3 and @branch_no <> 'ALL') -- 店组调价,只允许总店 wujs 2005-08-17
            begin
                if (CHARINDEX('00', @ls_branch_group) > 0 ) --店组中包括总店
                    select @have_hq_branch = '1'
                else
                    select @have_hq_branch = '0'
                
                while len(@ls_branch_group) > 0
                begin
                    select @ls_other2=substring(@ls_branch_group,1,2) -- 逐个分店更新价格
                    select @ls_branch_group = (case len(@ls_branch_group) when 2 then '' 
                           else substring(@ls_branch_group,4,len(@ls_branch_group)-3) end )
                   
                  	  /******* 处理调价单中不同门店调价不同的问题,列表中不存在总部会导致单价更新出错  ******/
					  IF Exists( select 1 from t_pc_price_flow_branch where sheet_no = @sheet_no and branch_no = @ls_other2 and item_no = @item_no)
					  begin
						select	@new_price  = max(new_price), 
								@new_price1 = max(new_price1), 
								@new_price2 = max(new_price2),
								@new_price3 = max(new_price3),
								@new_price4 = max(new_price4)
						from t_pc_price_flow_branch 
						where sheet_no = @sheet_no and branch_no = @ls_other2 and item_no = @item_no
					  end      
					/*************************************************/
                  
                   
                    update t_pc_branch_price set 
                        price      = (case when substring(@chgType, 1, 1) = '1'  then @new_price  else price end),
                        sup_ly_rate = (case when substring(@chgType, 2, 1) = '1' then @new_price3 else sup_ly_rate end),
                        sale_price = (case when substring(@chgType, 3, 1) = '1'  then @new_price1 else sale_price end),
                        vip_price  = (case when substring(@chgType, 4, 1) = '1'  then @new_price2 else vip_price end)
                        --,com_flag=@ls_com_flag 不改下传标识
                    where (item_no = @item_no) and (branch_no = @ls_other2)
                   
                    --无总店调价, 且调配送价 wujs 2006-09-14
                    if (@have_hq_branch = '0') and (substring(@chgType, 5, 1) = '1')
                    begin  --且 是加盟店 wujs 2006-09-14
                        if exists (select 1 from t_bd_branch_info where trade_type = '2' and branch_no = @ls_other2)
			   -- if @new_price4 <> @old_price4
                            update t_pc_branch_price 
                            set price = @new_price4,com_flag='0'  
                            where (item_no = @item_no and branch_no=@ls_other2)
                    end
                end --end with while
                
                -- 店组调价中含 00 机构调价 是否调配送价
                if (@have_hq_branch = '1' and substring(@chgType, 5, 1) = '1')
                begin
                   --总店调配送价
		  --if @new_price4 <> @old_price4
		  --begin
                   update t_bd_item_info set trans_price = @new_price4 where item_no = @item_no
                   --加盟店配送价  进货价 = 配送价 注意!!!!!!
                   update t_pc_branch_price set price = @new_price4,com_flag='0'
                        where (item_no = @item_no and branch_no in (select branch_no from t_bd_branch_info where trade_type = '2'))
		  --end
                end
            end
            else
            begin
                -- modified by wujs 2005-06-01 分店上传到总店的调价单  传输标志置 1
                -- commented by wujs 2006-01-12
                --if exists (Select branch_no From t_bd_branch_info 
                --       where property = '0' and branch_no = @branch_no and (trade_type = '1' or trade_type = '3'))
                --    select @ls_com_flag = '1'
                --else
                --    select @ls_com_flag = '0'
                
                --Modified by wujs 2006-09-13
                -------如果总分部对某商品同时调价，但总部调价在后，分部在前，且调价都生效了，这种情况下分部先下载，再上传，那就不要去更新了。
                if not exists(select * from t_pc_price_flow_detail where item_no=@item_no and sheet_no in 
                       (select sheet_no from t_pc_price_flow_master where (branch_no='ALL' or branch_no=@branch_no or charindex(@branch_no,branchlist)>0) and
                       branch_flag='00' and trans_no='PX' and valid_flag='2' and valid_date>=@valid_date and work_date>@work_date))
	               begin
	                update t_pc_branch_price set
	                     price      = (case when substring(@chgType, 1, 1) = '1'  then @new_price  else price end),
	                     sup_ly_rate = (case when substring(@chgType, 2, 1) = '1' then @new_price3 else sup_ly_rate end),
	                     sale_price = (case when substring(@chgType, 3, 1) = '1'  then @new_price1 else sale_price end),
	                     vip_price  = (case when substring(@chgType, 4, 1) = '1'  then @new_price2 else vip_price end)
	                     --,com_flag=@ls_com_flag 此处不改传输标识
	                  where (item_no = @item_no) and (branch_no = @branch_no)
	                end
                --Modified by wujs 2006-09-12
                if (substring(@chgType, 5, 1) = '1') --是否调配送价
                begin
		  --If @new_price4 <> @old_price4
		 -- begin

                    --调总部的配送价
                    if @branch_no = '00' 
                        update t_bd_item_info set trans_price = @new_price4 where item_no = @item_no
                    
                    --调总店的配送价的同时需要调所有加盟店的进货价(配送价)
                    if @branch_no = '00' 
                        update t_pc_branch_price set price = @new_price4,com_flag='0'
                            where (item_no = @item_no and branch_no in (select branch_no from t_bd_branch_info where trade_type = '2'))
                    else
                    begin
                        if (@branch_flag = @g_branch_no) and --是本店制的调价单 and 被调价门店是加盟店 才更新进货为配送价
                           exists (select 1 from t_bd_branch_info where trade_type = '2' and branch_no = @branch_no)
                        begin
                            update t_pc_branch_price 
                            set price = @new_price4,com_flag='0'
 
                            where (item_no = @item_no and branch_no = @branch_no)
                        end
                    end
		 -- end
                end
            end 
       /*------------------------*/
       end -- End With @is_distributed = '1' (在总店数据库执行的处理完毕)
       else if (@branch_no = @g_branch_no or @branch_no = 'ALL' or len(@branch_no)=3 ) and @is_distributed = '2' --分部
       begin
			
            --if (@branch_flag <> @g_branch_no) -- 非分部本地的调价单(即总部的调价单)
            --    select @ls_com_flag = '1'
            --else
            --    select @ls_com_flag = '0'
             -------如果总分部对某商品同时调价，但总部调价在前，分部在后，且调价都生效了，这种情况下分部先上传，再下载，那就不要去更新了。
             if not exists(select * from t_pc_price_flow_detail where item_no=@item_no and sheet_no in 
                       (select sheet_no from t_pc_price_flow_master where branch_flag=@g_branch_no and trans_no='PX' and valid_flag='1' and  valid_date>=@valid_date and work_date>@work_date))
	           begin			

	            update t_pc_branch_price  set
	               price      = (case when substring(@chgType, 1, 1) = '1'  then (case when @trade_type='2' then @new_price4 else @new_price end)  else price end),
	               sup_ly_rate = (case when substring(@chgType, 2, 1) = '1' then @new_price3 else sup_ly_rate end),
	               sale_price = (case when substring(@chgType, 3, 1) = '1'  then @new_price1 else sale_price end),
	               vip_price  = (case when substring(@chgType, 4, 1) = '1'  then @new_price2 else vip_price end)
	               --,com_flag=@ls_com_flag
	               where (item_no = @item_no) and branch_no=@g_branch_no
	           end 
        end
    end
    
    if @trans_no='LK'  -- 批发价调价单
    begin
       select @ls_branch_group = isnull(@branchlist,'')
      
       -- 增加店组处理
       --1.先更新商品档案中价格(调价门店含本店时)
       if @branch_no = @g_branch_no or @branch_no = 'ALL' or 
          (len(@branch_no)=3 and @branch_no <> 'ALL' and charindex(@g_branch_no,@ls_branch_group) > 0)
       begin
            --增加调价价格选择
            update t_bd_item_info set
               base_price  = (case when substring(@chgType, 1, 1) = '1'  then @new_price else base_price end),
               base_price1 = (case when substring(@chgType, 2, 1) = '1'  then @new_price1 else base_price1 end),
               base_price2 = (case when substring(@chgType, 3, 1) = '1'  then @new_price2 else base_price2 end), 
               base_price3 = (case when substring(@chgType, 4, 1) = '1'  then @new_price3 else base_price3 end),
               base_price4 = (case when substring(@chgType, 5, 1) = '1'  then @new_price4 else base_price4 end), 
               other3 = @update_date where (item_no = @item_no)
        end
       
       if @is_distributed = '1' or @is_distributed = '0' --总店、托管分店
       begin
            if @branch_no = 'All'
            begin
                update t_pc_branch_price set 
	               base_price  = (case when substring(@chgType, 1, 1) = '1'  then @new_price else base_price end),
	               base_price1 = (case when substring(@chgType, 2, 1) = '1'  then @new_price1 else base_price1 end),
	               base_price2 = (case when substring(@chgType, 3, 1) = '1'  then @new_price2 else base_price2 end), 
	               base_price3 = (case when substring(@chgType, 4, 1) = '1'  then @new_price3 else base_price3 end),
	               base_price4 = (case when substring(@chgType, 5, 1) = '1'  then @new_price4 else base_price4 end)
	        where (item_no = @item_no)               
                
            end
            else if (len(@branch_no)=3 and @branch_no <> 'ALL') -- 店组调价,只允许总店 wujs 2005-08-17
            begin
                if (CHARINDEX('00', @ls_branch_group) > 0 ) --店组中包括总店
                    select @have_hq_branch = '1'
                else
                    select @have_hq_branch = '0'
                
                while len(@ls_branch_group) > 0
                begin
                    select @ls_other2=substring(@ls_branch_group,1,2) -- 逐个分店更新价格
                    select @ls_branch_group = (case len(@ls_branch_group) when 2 then '' 
                           else substring(@ls_branch_group,4,len(@ls_branch_group)-3) end )
                   
                    /******* 处理调价单中不同门店调价不同的问题,列表中不存在总部会导致单价更新出错  ******/
					  IF Exists( select 1 from t_pc_price_flow_branch where sheet_no = @sheet_no and branch_no = @ls_other2 and item_no = @item_no)
					  begin
						select	@new_price  = max(new_price), 
								@new_price1 = max(new_price1), 
								@new_price2 = max(new_price2),
								@new_price3 = max(new_price3),
								@new_price4 = max(new_price4)
						from t_pc_price_flow_branch 
						where sheet_no = @sheet_no and branch_no = @ls_other2 and item_no = @item_no
					  end      
					/*************************************************/ 
                    update t_pc_branch_price set 
	               base_price  = (case when substring(@chgType, 1, 1) = '1'  then @new_price else base_price end),
	               base_price1 = (case when substring(@chgType, 2, 1) = '1'  then @new_price1 else base_price1 end),
	               base_price2 = (case when substring(@chgType, 3, 1) = '1'  then @new_price2 else base_price2 end), 
	               base_price3 = (case when substring(@chgType, 4, 1) = '1'  then @new_price3 else base_price3 end),
	               base_price4 = (case when substring(@chgType, 5, 1) = '1'  then @new_price4 else base_price4 end) 
                     where (item_no = @item_no) and (branch_no = @ls_other2)                   
                   
                end --end with while               
               
            end
            else
            begin

                update t_pc_branch_price set
	               base_price  = (case when substring(@chgType, 1, 1) = '1'  then @new_price else base_price end),
	               base_price1 = (case when substring(@chgType, 2, 1) = '1'  then @new_price1 else base_price1 end),
	               base_price2 = (case when substring(@chgType, 3, 1) = '1'  then @new_price2 else base_price2 end), 
	               base_price3 = (case when substring(@chgType, 4, 1) = '1'  then @new_price3 else base_price3 end),
	               base_price4 = (case when substring(@chgType, 5, 1) = '1'  then @new_price4 else base_price4 end)
                  where (item_no = @item_no) and (branch_no = @branch_no)               
            end 
       /*------------------------*/
       end -- End With @is_distributed = '1' (在总店数据库执行的处理完毕)
       else if (@branch_no = @g_branch_no or @branch_no = 'ALL' or len(@branch_no)=3 ) and @is_distributed = '2' --分部
       begin
            
            update t_pc_branch_price  set
               base_price  = (case when substring(@chgType, 1, 1) = '1'  then @new_price else base_price end),
               base_price1 = (case when substring(@chgType, 2, 1) = '1'  then @new_price1 else base_price1 end),
               base_price2 = (case when substring(@chgType, 3, 1) = '1'  then @new_price2 else base_price2 end), 
               base_price3 = (case when substring(@chgType, 4, 1) = '1'  then @new_price3 else base_price3 end),
               base_price4 = (case when substring(@chgType, 5, 1) = '1'  then @new_price4 else base_price4 end)
             where (item_no = @item_no) and branch_no=@g_branch_no
        end
    end


    -- 更新生效标志(总部:(编码为总部-->设为'1',编码为分部-->设为'2',编码为'ALL'-->设为'2')
    --             (分部:(全部设为'1'))
    if @is_distributed = '1' 
    begin
       if @branch_no = 'ALL' or ( len(@branch_no)=3 and @branch_no <> 'ALL' )  -- 店组全部置为 2
          update t_pc_price_flow_master set valid_flag = '2' where sheet_no = @sheet_no
       else
       begin  
          if @branch_no = @g_branch_no 
             update t_pc_price_flow_master set valid_flag = '1' where sheet_no = @sheet_no
          else
             update t_pc_price_flow_master set valid_flag = '2' where sheet_no = @sheet_no
          end
       end 
    else
       update t_pc_price_flow_master set valid_flag = '1' where sheet_no = @sheet_no
    
    
    if @@error != 0 
    begin
       close priceflow_cursor
       deallocate  priceflow_cursor
       delete t_sys_system where lower(sys_var_id)='change_price'
       return
    end
    
    fetch priceflow_cursor into @trans_no, @branch_no, @sheet_no, @ls_other2, @ls_other3,@chgType,@branchlist,
                   @valid_flag, @item_no,  @new_price, @old_price,@new_price1,@old_price1,
                   @new_price2, @old_price2,@new_price3,@old_price3,@branch_flag,@old_price4,@new_price4,@work_date,@valid_date
  end 
  close priceflow_cursor
  deallocate  priceflow_cursor 
  
  --　对于分布式版的分部，以机构当前的价格修正商品信息表
  if @is_distributed = '2' --独立管理分店
  begin
      /* 加盟店的机构价格进货价项 都已经赋给了总店的配送价, 所以在更新档案价格时无须区分 连锁类型 wujs 2006-09-14
      if @trade_type = '2'
        update t_bd_item_info set 
        base_price=t_pc_branch_price.base_price,
        sale_price=t_pc_branch_price.sale_price,
        vip_price=t_pc_branch_price.vip_price
        from t_bd_item_info,t_pc_branch_price
        where t_bd_item_info.item_no=t_pc_branch_price.item_no
          and t_pc_branch_price.branch_no=@g_branch_no
          and (t_bd_item_info.price <> t_pc_branch_price.price 
            or t_bd_item_info.base_price <> t_pc_branch_price.base_price 
            or t_bd_item_info.sale_price <> t_pc_branch_price.sale_price 
            or t_bd_item_info.vip_price <> t_pc_branch_price.vip_price 
               )
      else
      */  
        update t_bd_item_info set 
        price=t_pc_branch_price.price,
        sup_ly_rate=t_pc_branch_price.sup_ly_rate,
        sale_price=t_pc_branch_price.sale_price,
        vip_price=t_pc_branch_price.vip_price,
		base_price=t_pc_branch_price.base_price,
		base_price1=t_pc_branch_price.base_price1,
		base_price2=t_pc_branch_price.base_price2,
		base_price3=t_pc_branch_price.base_price3,
		base_price4=t_pc_branch_price.base_price4,
		main_supcust=t_pc_branch_price.supcust_no --同步供应商20211030
        from t_bd_item_info,t_pc_branch_price
        where t_bd_item_info.item_no=t_pc_branch_price.item_no
          and t_pc_branch_price.branch_no=@g_branch_no
          and (t_bd_item_info.price <> t_pc_branch_price.price 
            or t_bd_item_info.base_price <> t_pc_branch_price.base_price 
            or t_bd_item_info.sale_price <> t_pc_branch_price.sale_price 
            or t_bd_item_info.vip_price <> t_pc_branch_price.vip_price 
            or t_bd_item_info.base_price1 <> t_pc_branch_price.base_price1 
            or t_bd_item_info.base_price2 <> t_pc_branch_price.base_price2 
            or t_bd_item_info.base_price3 <> t_pc_branch_price.base_price3 
            or t_bd_item_info.base_price4 <> t_pc_branch_price.base_price4 
			or t_bd_item_info.main_supcust <> t_pc_branch_price.supcust_no --同步供应商20211030
               )
      if @@error != 0 
      begin
         delete t_sys_system where lower(sys_var_id)='change_price'
         return
      end
  end
    
  delete t_sys_system where lower(sys_var_id)='change_price'
   
  /***********删除失效的100+1商品***2004-07-16*********/
  delete from t_rm_spec_price 
         where special_type='9' and (branch_no='00' Or branch_no = 'ALL') and 
                 (convert(char(10),getdate(),121) > convert(char(10),end_date,121))
  
end 
go