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

create Trigger tr_im_flow on t_im_flow
for Insert
as
begin
  declare
    @flow_id        char(18) ,--流水号
    @voucher_no      char(18) ,--原始凭证号
    @oper_date       datetime   ,--操作日期
    @item_no         char(20)        ,--货品代码
    @real_qty        numeric(16,4)   ,--数量
    @price      numeric(16,4)   ,
    @cost_price      numeric(16,4)   ,--成本单价
    @sheet_amt       numeric(16,4)   ,--金额
    @branch_no       char(6)        ,--本店代码
    @d_branch_no     char(6)        ,--目的地代码
    @trans_no        char(2)         ,--库存操作方式
    @db_no           char(1)      ,--出入库方式
    @supcust_no      char(8)        ,--供应商客户代码
    @sale_way        char(1)      ,--销售方式
    @t_in_price       numeric(16,4) ,  --基准进价
    @branch_type  char(1), --机构类型
    --@branch_type_to  char(1),
    @stock_money  numeric(16,4),--库存金额
    @ls_operdate  char(10),
    @stock_qty    numeric(16,4),--库存数量
    @stock_price  numeric(16,4),--成本价
    @sale_price      numeric(16,4),--售价
    @sup_no       char(8),        --供应商客户代码
    @so_price     numeric(16,4),--批发价
    @lb_change_price char(1),
    @stock_type char(1) ,  --门店类型
    @local_branch char(6),
    @adjust_num1  integer,
    @adjust_num2  integer,
    @item_stock  char(1), ---商品是否维护库存
	@sup_item_stock char(1), -- 启用联营商品不管理库存
    @item_sup_flag char(1), ---经营方式 A 购销，C 联营
    @item_sup_rate decimal(16,4), ----商品档案联营扣率
    @spec_sup_rate decimal(16,4), ----特价联营扣率
    @cost_qty     numeric(16,4),----影响成本价入库商品数量
    @cost_money     numeric(16,4),---影响成本入库成本金额
    @init_qty     numeric(16,4),----需计算成本价期初数量
    @sup_rate     numeric(16,4),--联营商品的联营扣率
    @init_amt     numeric(16,4),--期初成本金额
    @cost_change_amt numeric(16,4),--调价单生成成本差额
    @sheet_zb_manage char(1), --总部代管实时型门店的单据
    @shop_zb_manage char(1), --托管型门店
    @local_branch_no char(3),--机构代码
    @cost_price_pc      numeric(16,4),@as_version_flag char(1),
 	@daysalenum 	numeric(16,4) --日销售数量合计

   select @sup_item_stock = isnull(sys_var_value,'0') from t_sys_system where sys_var_id='sup_item_stock'
   select @flow_id = flow_id, @voucher_no = voucher_no, @oper_date = oper_date, @item_no = item_no, @real_qty = real_qty,
       @price = price, @sheet_amt = sheet_amt, @branch_no = branch_no, @d_branch_no = d_branch_no,
       @trans_no = trans_no, @db_no = db_no, @supcust_no = supcust_no, @sale_way = sale_way,
       @sale_price = sale_price,@cost_price_pc = price
   from inserted
   SELECT @stock_type = trade_type FROM t_bd_branch_info WHERE branch_no = @branch_no
   SELECT @local_branch = isnull(sys_var_value, '') FROM t_sys_system WHERE sys_var_id = 'g_stock_no'
   
 --modify by fangtao 2014-03-06 
   select @sup_rate = isnull(sup_rate,0), @item_sup_flag = item_sup_flag from t_bd_item_info where item_no=@item_no

    --读取机构类型
  SELECT @branch_type = trade_type FROM t_bd_branch_info WHERE branch_no = left(@branch_no,3)
	
	--added by lil on 20130527 变量赋初值
	select @shop_zb_manage = '0'
  If @stock_type = '7'
   begin
      SELECT @shop_zb_manage = isnull(zbtg_flag, '0') FROM t_bd_branch_info WHERE branch_no = @branch_no
      IF (@shop_zb_manage is null or @shop_zb_manage = '') select @shop_zb_manage = '0'
   end

 -----如果数据为门店上传，则不再往下执行。（分公司目前有传t_im_flow）
  if ( @local_branch <> @branch_no ) and ( @stock_type = '7' or @stock_type = '8' or ((@branch_type ='1' or @branch_type='2') and (left(@local_branch,3) <> left(@branch_no,3)) ))
	begin
		if @shop_zb_manage ='0'
		begin
			select @as_version_flag=version_flag from t_bd_branch_info where branch_no = @branch_no
			--modified by lil on 20130604 如果对于加盟店直接上传至本店的直接上级机构时都应通过触发器进行数据传输处理
			if @as_version_flag = '1' 
			begin
				if not (@stock_type = '8' and left(@local_branch, 3) = left(@branch_no, 3))
					return
			end
		end
	end
	
   SELECT @sheet_zb_manage = isnull(sys_var_value, '0') FROM t_sys_system WHERE sys_var_id = 'sheet_zb_manager'
   IF @sheet_zb_manage is null or @sheet_zb_manage = '' 
      select @sheet_zb_manage = '0'   
 
   SELECT @ls_operdate = convert(char(10),@oper_date,121)
   SELECT @item_stock = item_stock FROM t_bd_item_info WHERE item_no = @item_no 
   
   SELECT @so_price = base_price, @sup_no = main_supcust, @sale_price = sale_price,
          @t_in_price = price ,@item_sup_flag = item_sup_flag ,@item_sup_rate = isnull(sup_rate,0)
     FROM t_bd_item_info 
	WHERE item_no = @item_no
   
   SELECT @lb_change_price = isnull(change_cost,'0') FROM t_sys_sheet_no WHERE sheet_id = @trans_no
   
    select @adjust_num1 = 0
   select @adjust_num1 = convert(numeric,sys_var_value) from t_sys_system where sys_var_id = 'adjust_cost_settle1'
   select @adjust_num1 = isnull(@adjust_num1, 0)
   
   select @adjust_num2 = 0
   select @adjust_num2 = convert(numeric,sys_var_value) from t_sys_system where sys_var_id = 'adjust_cost_settle2'
   select @adjust_num2 = isnull(@adjust_num2, 0)
   
   select @stock_qty = 0
   select @stock_money = 0
   select  @stock_price = 0
   
   SELECT  @stock_qty = isnull(stock_qty,0), @stock_price = isnull(avg_cost,0), @stock_money = isnull(cost_amt ,0)
    FROM t_im_branch_stock 
	WHERE branch_no=@branch_no and item_no=@item_no
     
   select @stock_money = isnull(@stock_money,0)
   select @stock_qty   = isnull(@stock_qty,  0)
   select @cost_qty=0
   select @cost_money=0
   select @init_qty=0
   select @init_amt=0
   select @cost_change_amt=0
	
   
   if @trans_no='PC' 
   begin
       exec pr_pc_change_costprice @item_no, @branch_no, @cost_price_pc
   end

   if ( (@branch_type='0' or @branch_type='2' )  and (@stock_type='5' or @stock_type='6' or (@stock_type='3' and @sheet_zb_manage = '1') or (@stock_type = '7' and @shop_zb_manage = '1'))  and charindex(@trans_no,'PI/RO/GI/CS')>=1) 
   
       SELECT @cost_qty = isnull(pi_qty,0), @cost_money = isnull(pi_amt ,0),@init_qty=isnull(init_qty,0),@init_amt=isnull(init_amt,0),@cost_change_amt=isnull(cost_change_amt,0)
       FROM t_da_jxc_daysum 
	   WHERE branch_no=@branch_no and item_no=@item_no and oper_date = @ls_operdate
   
   else if ( not ( (@branch_type='0' or @branch_type='2' )  and (@stock_type='5' or @stock_type='6' or (@stock_type='3' and @sheet_zb_manage = '1') or (@stock_type = '7' and @shop_zb_manage = '1')) ) and charindex(@trans_no,'PI/RO/GI/CS/MI/DI')>=1)
   
       SELECT @cost_qty = isnull(pi_qty+mi_qty+di_qty,0), @cost_money = isnull(pi_amt+mi_amt+di_amt ,0),@init_qty=isnull(init_qty,0),@init_amt=isnull(init_amt,0),@cost_change_amt=isnull(cost_change_amt,0)
       FROM t_da_jxc_daysum 
	   WHERE branch_no=@branch_no and item_no=@item_no and oper_date = @ls_operdate
   
   select @cost_money = isnull(@cost_money, 0.0000)
   select @cost_qty   = isnull(@cost_qty  , 0)
   select @init_amt   = isnull(@init_amt  , 0)
   select @init_qty   = isnull(@init_qty  , 0)
   
   select @cost_qty=@cost_qty+@init_qty
   select @cost_money=@cost_money+@init_amt+@cost_change_amt
   
   if (@cost_qty is null or @cost_qty=0) 
      select @cost_qty=@stock_qty ,@cost_money=@stock_money
   
   IF (@cost_qty is null or @cost_qty = 0) and (@stock_qty is null or @stock_qty = 0) 
      Select @cost_qty = 0,@cost_money = 0,@stock_price = @t_in_price
      
   --added by lil on 20130530 对于转仓单必须考虑有一个仓库没有业务的情况(调出仓有成本价而调入仓是新仓库且档案进价为0时的异常情况)
   if @trans_no = 'IO' and @db_no = '+'
   begin
	   	if not exists(select 1 from t_im_branch_stock where branch_no=@branch_no and item_no=@item_no)
	   	begin
	   		if exists(select 1 from t_bd_item_info where item_no = @item_no and price > 0)
	   			select  @stock_price = price from t_bd_item_info where item_no = @item_no
	   		else
	   			select  @stock_price = @price
	   	end
   end
   --added by lil on 20130530 结束
   
   if ( ( @branch_type='0' or @branch_type='2' )  and
        (  (@stock_type = '5') or  (@stock_type='6') or
           (@stock_type = '3'  and @sheet_zb_manage = '1') or 
           (@stock_type = '7'  and @shop_zb_manage = '1')
         )
         and charindex(@trans_no,'PI/RO/GI/CS/MI/IO')>=1
      )
      or 
      ( not ( (@branch_type='0' or @branch_type='2' )  and @stock_type='5' ) and charindex(@trans_no,'PI/RO/GI/CS/MI/DI/IO')>=1 )
   begin
      if @db_no = '+'
      begin
        if @stock_qty < 0 and @stock_qty + @real_qty > 0 
           select @cost_price = @price
        else if @stock_qty + @real_qty <= 0 or @stock_money + @real_qty * @price < 0 or @cost_qty + @real_qty <=0
           select @cost_price = @price
	  	else if @stock_money = 0 and @stock_qty = 0 
	    	select @cost_price = @price
        else
           select @cost_price = abs(@cost_money + @real_qty * @price)/abs(@cost_qty + @real_qty)
      end 
      else
      begin
        begin
			if @stock_qty < 0 and @stock_qty - abs(@real_qty) > 0	--采购退货单据@real_qty值<0，判断时因取绝对值后再判断	zhouzx	2015-3-11
			   select @cost_price = @price
			else if @stock_qty - abs(@real_qty) <= 0 or @stock_money - abs(@real_qty) * @price < 0 or @cost_qty - abs(@real_qty) <=0	--zhouzx	2015-3-11
			   select @cost_price = @price
			else
			   select @cost_price = abs(@cost_money - abs(@real_qty) * @price)/abs(@cost_qty - abs(@real_qty))	--zhouzx	2015-3-11
		  end
      end
   end 
   else
   begin
     --如果当天已经产生进销存记录，直接取进销存表里当天的成本价 modify by lifb 20140408 begin
	 --如果是分店，库存<=0取配送价20210608
	 if exists(select * from t_bd_branch_info where branch_no=@branch_no and property='2' and trade_type in('7','8'))
		 begin--分店
			if @stock_qty<=0 --分店配送价
				select @cost_price=scheme_price from t_pc_branch_price where item_no=@item_no and  branch_no=@branch_no
			else if @stock_qty>0 --成本价
				select @cost_price = @stock_price
			
			--如果还为0，取档案配送价
			 if @cost_price is null or @cost_price=0
			 begin
				select  @cost_price = scheme_price from t_bd_item_info where item_no=@item_no 
			end
		 end
	 else
		begin--总部或者单店
	      select @cost_price = cost_price from t_da_jxc_daysum 
	      WHERE (oper_date=@ls_operdate and item_no=@item_no and  branch_no=@branch_no  )
	      if @cost_price <=0 or @cost_price is null
			 begin
	         	select @cost_price = @stock_price
			 end
		 --完善当进销存记录与库存表无成本价或成本价为零时，成本价取机构档案进价，再为空则去基本档案进价，则软件中进价不能为空 add by lifb 20140429 begin
	      if @cost_price is null or @cost_price=0
			begin
	         select @cost_price = price from t_pc_branch_price where item_no=@item_no and  branch_no=@branch_no
			end
		  if @cost_price is null or @cost_price=0
			 begin
				select  @cost_price = price from t_bd_item_info where item_no=@item_no 
			end
		 -- add by lifb 20140429 end
		end
   end
	 -- modify by lifb 20140408 end

   if @trans_no<>'GI'and (@trans_no <> 'SI') and (@trans_no <> 'MI')   --赠品入库不更新成本金额，通过当前库存金额/库存+赠品数推算
	     update t_im_flow set cost_price = @cost_price where flow_id = @flow_id
   if @@error != 0 goto myerror
   --add  by dutz 2007-02-03.end
   
   if @trans_no='GI'  --进货入库
   BEGIN
      
      IF exists(select item_no from t_im_branch_stock where item_no=@item_no and branch_no = @branch_no)
         update t_im_branch_stock 
	    set last_inprice=@price,oper_date=getdate(),
		avg_cost = case (@real_qty + @stock_qty) when 0 then avg_cost else (case when cost_amt/(@real_qty + @stock_qty)<0 then avg_cost else cost_amt/(@real_qty + @stock_qty) end) end,
		cost_amt = case (@real_qty + @stock_qty) when 0 then 0 else (case when cost_amt/(@real_qty + @stock_qty)<0 then (@real_qty + @stock_qty)*avg_cost else cost_amt end) end,
                stock_qty=stock_qty + @real_qty ,com_flag='0'
          where branch_no=@branch_no and item_no=@item_no
      ELSE
         insert into t_im_branch_stock (branch_no, item_no, stock_qty, avg_cost,last_inprice,oper_date,cost_amt)
                values(@branch_no, @item_no, @real_qty, @cost_price,@price,getdate(),@real_qty * @cost_price)
      
      IF @@error != 0 goto myerror
	      IF exists(select * from t_da_jxc_daysum where(item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no))
	           UPDATE t_da_jxc_daysum
	            SET   item_sup_flag = @item_sup_flag,sup_ly_rate = @sup_rate,
					  avg_cost_amt = (case (@stock_qty + @real_qty) when 0 then 0 else (case when settle_amt/(@stock_qty + @real_qty)<0 then cost_price else settle_amt/(@stock_qty + @real_qty) end) end) * (so_qty + pos_qty + pos_ret_qty + pos_send_qty), 
			          cost_price = (case (@stock_qty + @real_qty) when 0 then cost_price else (case when settle_amt/(@stock_qty + @real_qty)<0 then cost_price else (settle_amt/(@stock_qty + @real_qty)) end) end),
	                  avg_profit_amt = (so_amt + pos_amt + pos_ret_amt) - @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty),
	                  pi_qty = pi_qty + @real_qty, 
	                  settle_qty = @stock_qty + @real_qty,
				      settle_amt = case (@stock_qty + @real_qty) when 0 then 0 else (case when settle_amt/(@stock_qty + @real_qty)<0 then (@stock_qty + @real_qty)*cost_price else settle_amt end) end,
	                  settle_amt2 = (@stock_qty + @real_qty)* @sale_price, 
					  com_flag = '0',
					  update_date=getdate()
	           WHERE (item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no)
	      
	      ELSE
	           INSERT INTO t_da_jxc_daysum (
	               item_no, oper_date, branch_no, cost_price, init_qty, init_amt, init_amt2,
	               avg_cost_amt, avg_profit_amt,pi_qty, pi_amt, so_qty, so_amt, mi_qty,mi_amt,
	               mo_qty, mo_amt, di_qty, di_amt,do_qty, do_amt, pos_qty,pos_amt, pos_ret_qty,pos_ret_amt,
	               pos_send_qty,pos_send_amt, oo_qty, oo_amt, settle_qty,settle_amt,settle_amt2,
	               cost_change_amt, pos_dct_amt, main_sup, dec_rate,so_price, sale_price, com_flag,item_sup_flag,sup_ly_rate,update_date )
	           VALUES ( @item_no, @ls_operdate ,@branch_no, @cost_price,@stock_qty,@stock_money,@stock_qty*@sale_price,
	                  0,0,@real_qty,@real_qty * @price,0,0,0,0,
	               0,0,0,0,0,0,0,0,0,0,0,0,0,0,@stock_qty + @real_qty,round((@stock_qty + @real_qty) * @cost_price,4),(@stock_qty + @real_qty)* @sale_price,
	               0,0,@sup_no,0,@so_price, @sale_price, '0',@item_sup_flag,@sup_rate,getdate())

      IF @@error != 0 goto myerror
   
   END  --- if @trans_no='GI'  --进货入库
   
   
   if @trans_no='PI' or @trans_no='RO'  --进货入库
   BEGIN
 
      
      IF exists(select item_no from t_im_branch_stock where item_no=@item_no and branch_no = @branch_no)
         update t_im_branch_stock set last_inprice=@price,oper_date=getdate(),avg_cost = @cost_price ,
                cost_amt = (stock_qty + @real_qty)*@cost_price, stock_qty=stock_qty + @real_qty ,com_flag='0'
          where branch_no=@branch_no and item_no=@item_no
      ELSE
         insert into t_im_branch_stock (branch_no, item_no, stock_qty, avg_cost,last_inprice,oper_date,cost_amt)
             values(@branch_no, @item_no, @real_qty, @cost_price,@price,getdate(),@real_qty * @cost_price)
      
      if @@error != 0 goto myerror
	     IF exists(select * from t_da_jxc_daysum where(item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no))
	      
	          UPDATE  t_da_jxc_daysum
	            SET   item_sup_flag = @item_sup_flag,sup_ly_rate = @sup_rate,
				avg_cost_amt = @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty), cost_price = @cost_price,
	            avg_profit_amt = (so_amt + pos_amt + pos_ret_amt) - @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty),
	                  pi_qty = pi_qty + @real_qty, pi_amt = pi_amt + @real_qty * @price,
	                  settle_qty = @stock_qty + @real_qty,settle_amt = (@stock_qty + @real_qty) * @cost_price,
	                  settle_amt2 = (@stock_qty + @real_qty)* @sale_price, com_flag = '0',update_date=getdate()
	          WHERE (item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no)
	        
	      ELSE
	         INSERT INTO t_da_jxc_daysum (
	            item_no, oper_date, branch_no, cost_price, init_qty, init_amt, init_amt2,
	            avg_cost_amt, avg_profit_amt,pi_qty, pi_amt, so_qty, so_amt, mi_qty,mi_amt,
	            mo_qty, mo_amt, di_qty, di_amt,do_qty, do_amt, pos_qty,pos_amt, pos_ret_qty,pos_ret_amt,
	            pos_send_qty,pos_send_amt, oo_qty, oo_amt, settle_qty,settle_amt,settle_amt2,
	            cost_change_amt, pos_dct_amt, main_sup, dec_rate,so_price, sale_price, com_flag,item_sup_flag,sup_ly_rate,update_date )
	          VALUES ( @item_no, @ls_operdate ,@branch_no, @cost_price,@stock_qty,@stock_money,@stock_qty*@sale_price,
	                     0,0,@real_qty,@real_qty * @price,0,0,0,0,
	            0,0,0,0,0,0,0,0,0,0,0,0,0,0,@stock_qty + @real_qty,(@stock_qty + @real_qty) * @cost_price,(@stock_qty + @real_qty)* @sale_price,
	            0,0,@sup_no,0,@so_price, @sale_price, '0',@item_sup_flag,@sup_rate,getdate())

      If @@error != 0 goto myerror
   END  --if @trans_no='PI' or @trans_no='RO'  --进货入库
   
   if @trans_no='SO' or @trans_no='RI' or @trans_no='SI' --销售
   BEGIN
      if @sup_item_stock = '1'  -- 启用联营商品不管理库存
	  begin
		if @trans_no='RI' and @price = 0 -- 批发退货单，如果价格为0，则成本价取机构价格表进价。
		begin
			select @cost_price = isnull(price,0)  from t_pc_branch_price where item_no=@item_no and branch_no=@branch_no
		end
	  end
      IF exists(select item_no from t_im_branch_stock where item_no=@item_no and branch_no = @branch_no)
        
          update t_im_branch_stock set last_baseprice=@price,oper_date=getdate(),avg_cost = @cost_price ,
                cost_amt = (stock_qty - @real_qty) * @cost_price , stock_qty=stock_qty - @real_qty ,com_flag='0'
               where branch_no=@branch_no and item_no=@item_no
      ELSE
          insert into t_im_branch_stock (branch_no, item_no, stock_qty, avg_cost,last_baseprice,oper_date,cost_amt)
          values(@branch_no, @item_no, -@real_qty, @cost_price,@price,getdate(),-@real_qty * @cost_price)
     
      if @@error != 0 goto myerror

		if @sup_item_stock = '1' -- 启用联营商品不管理库存
		begin
			if @item_sup_flag = 'C' 
			begin
				if not (@price = 0 )  -- 联营商品，批发销售，批发退货价格不为0才更新平均成本价，如果为0，则不更新成本价。
				begin
					set @cost_price = @price * ( 1 - @item_sup_rate )	       
				end
			end
		end			    
		if @sale_way = 'C' set @price = 0  --批发销售的赠送部分，金额为0。	
	     IF exists(select * from t_da_jxc_daysum where(item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no))
	       
	          UPDATE t_da_jxc_daysum
	               SET   item_sup_flag = @item_sup_flag,sup_ly_rate = @sup_rate,
				   avg_cost_amt = @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty + @real_qty), cost_price = @cost_price,
	               avg_profit_amt = (so_amt + pos_amt + pos_ret_amt + @real_qty * @price) - @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty + @real_qty),
	                     so_qty = so_qty + @real_qty, so_amt = so_amt + @real_qty * @price,
	                     settle_qty = @stock_qty - @real_qty,settle_amt = (@stock_qty - @real_qty) * @cost_price,
	                     settle_amt2 = (@stock_qty - @real_qty)* @sale_price, com_flag = '0',update_date=getdate()
	             WHERE (item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no)
	      ELSE
	          INSERT INTO t_da_jxc_daysum (
	            item_no, oper_date, branch_no, cost_price, init_qty, init_amt, init_amt2,
	            avg_cost_amt, avg_profit_amt,pi_qty, pi_amt, so_qty, so_amt, mi_qty,mi_amt,
	            mo_qty, mo_amt, di_qty, di_amt,do_qty, do_amt, pos_qty,pos_amt, pos_ret_qty,pos_ret_amt,
	            pos_send_qty,pos_send_amt, oo_qty, oo_amt, settle_qty,settle_amt,settle_amt2,
	            cost_change_amt, pos_dct_amt, main_sup, dec_rate,so_price, sale_price, com_flag,item_sup_flag,sup_ly_rate ,update_date)
	          VALUES ( @item_no, @ls_operdate ,@branch_no, @cost_price,@stock_qty,@stock_money,@stock_qty*@sale_price,
	                     @cost_price * @real_qty, @real_qty * (@price - @cost_price),0,0,@real_qty,@real_qty * @price,0,0,
	            0,0,0,0,0,0,0,0,0,0,0,0,0,0,@stock_qty - @real_qty,(@stock_qty - @real_qty) * @cost_price ,(@stock_qty - @real_qty)* @sale_price,
	            0,0,@sup_no,0,@so_price, @sale_price, '0',@item_sup_flag,@sup_rate,getdate())

   END  --if @trans_no='SO' or @trans_no='RI' --销售
   
   if @trans_no='PT'  OR @trans_no='LP' --pos销售
   BEGIN
      IF exists(select item_no from t_im_branch_stock where item_no=@item_no and branch_no = @branch_no)
       
         update t_im_branch_stock set last_baseprice=@price,oper_date=getdate(),avg_cost = @cost_price ,
          cost_amt = (stock_qty - @real_qty) * @cost_price , stock_qty=stock_qty - @real_qty ,com_flag='0'
         where branch_no=@branch_no and item_no=@item_no
       
      ELSE
         insert into t_im_branch_stock (branch_no, item_no, stock_qty, avg_cost,last_baseprice,oper_date,cost_amt)
          values(@branch_no, @item_no, -@real_qty, @cost_price,@price,getdate(),-@real_qty * @cost_price)
      
      if @@error != 0 goto myerror
	
	  if @sup_item_stock = '1' -- 启用联营商品不管理库存     
	  begin
		if @item_sup_flag = 'C'  set @cost_price = @price * ( 1 - @item_sup_rate )
	  end

      IF exists(select * from t_da_jxc_daysum where(item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no))
      begin
		if @sup_item_stock = '1' -- 启用联营商品不管理库存
		begin
			if @sale_way = 'C' -- 赠送商品，成本价取不是赠送的那个成本价。		
				select @cost_price = isnull(cost_price,0) from t_da_jxc_daysum WHERE (item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no)
		end 	   
    
         if @sale_way = 'A' 
                UPDATE  t_da_jxc_daysum
                  SET   item_sup_flag = @item_sup_flag,sup_ly_rate = @sup_rate,
						avg_cost_amt = @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty + @real_qty), cost_price = @cost_price,
                        avg_profit_amt = (so_amt + pos_amt + pos_ret_amt + @real_qty * @price) - @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty + @real_qty),
                        pos_qty = pos_qty + @real_qty, pos_amt = pos_amt + @real_qty * @price,
                        settle_qty = @stock_qty - @real_qty,settle_amt = (@stock_qty - @real_qty) * @cost_price,
                        settle_amt2 = (@stock_qty - @real_qty)* @sale_price, com_flag = '0',update_date=getdate()
                WHERE (item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no)
           
         else if @sale_way = 'C'
                UPDATE  t_da_jxc_daysum
                  SET   item_sup_flag = @item_sup_flag,sup_ly_rate = @sup_rate,
						avg_cost_amt = @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty + @real_qty), cost_price = @cost_price,
                        avg_profit_amt = (so_amt + pos_amt + pos_ret_amt + @real_qty * @price) - @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty + @real_qty),
                        pos_send_qty = pos_send_qty + @real_qty, pos_send_amt = pos_send_amt + @real_qty * @price,
                        settle_qty = @stock_qty - @real_qty,settle_amt = (@stock_qty - @real_qty) * @cost_price,
                        settle_amt2 = (@stock_qty - @real_qty)* @sale_price, com_flag = '0',update_date=getdate()
                WHERE (item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no)
          
         else
                UPDATE  t_da_jxc_daysum
                  SET   item_sup_flag = @item_sup_flag,sup_ly_rate = @sup_rate,
						avg_cost_amt = @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty + @real_qty), cost_price = @cost_price,
                        avg_profit_amt = (so_amt + pos_amt + pos_ret_amt + @real_qty * @price) - @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty + @real_qty),
                        pos_ret_qty = pos_ret_qty + @real_qty, pos_ret_amt = pos_ret_amt + @real_qty * @price,
                        settle_qty = @stock_qty - @real_qty,settle_amt = (@stock_qty - @real_qty) * @cost_price,
                        settle_amt2 = (@stock_qty - @real_qty)* @sale_price, com_flag = '0',update_date=getdate()
    
            WHERE (item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no)
      end
      ELSE
      begin
         if @sale_way = 'A'
                INSERT INTO t_da_jxc_daysum (
                  item_no, oper_date, branch_no, cost_price, init_qty, init_amt, init_amt2,
                  avg_cost_amt, avg_profit_amt,pi_qty, pi_amt, so_qty, so_amt, mi_qty,mi_amt,
                  mo_qty, mo_amt, di_qty, di_amt,do_qty, do_amt, pos_qty,pos_amt, pos_ret_qty,pos_ret_amt,
                  pos_send_qty,pos_send_amt, oo_qty, oo_amt, settle_qty,settle_amt,settle_amt2,
                  cost_change_amt, pos_dct_amt, main_sup, dec_rate,so_price, sale_price, com_flag ,item_sup_flag,sup_ly_rate,update_date)
                VALUES ( @item_no, @ls_operdate ,@branch_no, @cost_price,@stock_qty,@stock_money,@stock_qty*@sale_price,
                           @cost_price * @real_qty, @real_qty * (@price - @cost_price),0,0,0,0,0,0,0,0,0,0,0,0,@real_qty,@real_qty * @price,0,0,
                  0,0,0,0,@stock_qty - @real_qty,(@stock_qty - @real_qty) * @cost_price ,(@stock_qty - @real_qty)* @sale_price,
                  0,0,@sup_no,0,@so_price, @sale_price, '0',@item_sup_flag,@sup_rate,getdate())
          
         else if @sale_way = 'B'
                INSERT INTO t_da_jxc_daysum (
                  item_no, oper_date, branch_no, cost_price, init_qty, init_amt, init_amt2,
                  avg_cost_amt, avg_profit_amt,pi_qty, pi_amt, so_qty, so_amt, mi_qty,mi_amt,
                  mo_qty, mo_amt, di_qty, di_amt,do_qty, do_amt, pos_qty,pos_amt, pos_ret_qty,pos_ret_amt,
                  pos_send_qty,pos_send_amt, oo_qty, oo_amt, settle_qty,settle_amt,settle_amt2,
                  cost_change_amt, pos_dct_amt, main_sup, dec_rate,so_price, sale_price, com_flag,item_sup_flag ,sup_ly_rate,update_date)
                VALUES ( @item_no, @ls_operdate ,@branch_no, @cost_price,@stock_qty,@stock_money,@stock_qty*@sale_price,
                           @cost_price * @real_qty, @real_qty * (@price - @cost_price),0,0,0,0,0,0,0,0,0,0,0,0,0,0,@real_qty,@real_qty * @price,0,0,
                  0,0,@stock_qty - @real_qty,(@stock_qty - @real_qty) * @cost_price ,(@stock_qty - @real_qty)* @sale_price,
                  0,0,@sup_no,0,@so_price, @sale_price, '0',@item_sup_flag,@sup_rate,getdate())
         
         else
                INSERT INTO t_da_jxc_daysum (
                  item_no, oper_date, branch_no, cost_price, init_qty, init_amt, init_amt2,
                  avg_cost_amt, avg_profit_amt,pi_qty, pi_amt, so_qty, so_amt, mi_qty,mi_amt,
                  mo_qty, mo_amt, di_qty, di_amt,do_qty, do_amt, pos_qty,pos_amt, pos_ret_qty,pos_ret_amt,
                  pos_send_qty,pos_send_amt, oo_qty, oo_amt, settle_qty,settle_amt,settle_amt2,
                  cost_change_amt, pos_dct_amt, main_sup, dec_rate,so_price, sale_price, com_flag,item_sup_flag ,sup_ly_rate,update_date)
                VALUES ( @item_no, @ls_operdate ,@branch_no, @cost_price,@stock_qty,@stock_money,@stock_qty*@sale_price,
                           @cost_price * @real_qty, @real_qty * (@price - @cost_price),0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,@real_qty,@real_qty * @price,
                  0,0,@stock_qty - @real_qty,(@stock_qty - @real_qty) * @cost_price ,(@stock_qty - @real_qty)* @sale_price,
                  0,0,@sup_no,0,@so_price, @sale_price, '0',@item_sup_flag,@sup_rate,getdate())
      end
   END  --if @trans_no='PT'  OR @trans_no='LP' --pos销售
   
   
   if @trans_no='IO' --转仓单 dutz 2007-05-22
   BEGIN
       if @db_no = '+'
       begin
          IF exists(select item_no from t_im_branch_stock where item_no=@item_no and branch_no = @branch_no)
              update t_im_branch_stock set last_baseprice=@price,oper_date=getdate(),avg_cost = @cost_price ,
                     cost_amt = (stock_qty + @real_qty) * @cost_price , stock_qty=stock_qty + @real_qty ,com_flag='0'
               where branch_no=@branch_no and item_no=@item_no
          ELSE
              insert into t_im_branch_stock (branch_no, item_no, stock_qty, avg_cost,last_baseprice,oper_date,cost_amt)
                values(@branch_no, @item_no, @real_qty, @cost_price,@price,getdate(),@real_qty * @cost_price)
      
          if @@error != 0 goto myerror

          IF exists(select * from t_da_jxc_daysum where(item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no))
              UPDATE    t_da_jxc_daysum
                     SET  item_sup_flag = @item_sup_flag,sup_ly_rate = @sup_rate,						
						  avg_cost_amt = @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty), cost_price = @cost_price,
						  avg_profit_amt = (so_amt + pos_amt + pos_ret_amt) - @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty),
                        di_qty = di_qty + @real_qty, di_amt = di_amt + @real_qty * @price,
                          settle_qty = @stock_qty + @real_qty,settle_amt = (@stock_qty + @real_qty) * @cost_price,
                          settle_amt2 = (@stock_qty + @real_qty)* @sale_price, com_flag = '0',update_date=getdate()
                   WHERE (item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no)
          ELSE
              INSERT INTO t_da_jxc_daysum (
                     item_no, oper_date, branch_no, cost_price, init_qty, init_amt, init_amt2,
                     avg_cost_amt, avg_profit_amt,pi_qty, pi_amt, so_qty, so_amt, mi_qty,mi_amt,
                     mo_qty, mo_amt, di_qty, di_amt,do_qty, do_amt, pos_qty,pos_amt, pos_ret_qty,pos_ret_amt,
                     pos_send_qty,pos_send_amt, oo_qty, oo_amt, settle_qty,settle_amt,settle_amt2,
                     cost_change_amt, pos_dct_amt, main_sup, dec_rate,so_price, sale_price, com_flag,item_sup_flag ,sup_ly_rate,update_date)
                  VALUES ( @item_no, @ls_operdate ,@branch_no, @cost_price,@stock_qty,@stock_money,@stock_qty*@sale_price,
                              0, 0,0,0,0,0,0,0,0,0,@real_qty,@real_qty * @price,
                     0,0,0,0,0,0,0,0,0,0,@stock_qty + @real_qty,(@stock_qty + @real_qty) * @cost_price ,(@stock_qty + @real_qty)* @sale_price,
                     0,0,@sup_no,0,@so_price, @sale_price, '0',@item_sup_flag,@sup_rate,getdate())
        end

       else
       begin
          IF exists(select item_no from t_im_branch_stock where item_no=@item_no and branch_no = @branch_no)
              update t_im_branch_stock set last_baseprice=@price,oper_date=getdate(),avg_cost = @cost_price ,
                     cost_amt = (stock_qty - @real_qty) * @cost_price , stock_qty=stock_qty - @real_qty ,com_flag='0'
               where branch_no=@branch_no and item_no=@item_no
          ELSE
              insert into t_im_branch_stock (branch_no, item_no, stock_qty, avg_cost,last_baseprice,oper_date,cost_amt)
                values(@branch_no, @item_no, -@real_qty, @cost_price,@price,getdate(),-@real_qty * @cost_price)
          
          If @@error != 0 goto myerror

	          IF exists(select * from t_da_jxc_daysum where(item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no))
	              UPDATE    t_da_jxc_daysum
	                     SET   item_sup_flag = @item_sup_flag,sup_ly_rate = @sup_rate,
							   avg_cost_amt = @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty), cost_price = @cost_price,
							   avg_profit_amt = (so_amt + pos_amt + pos_ret_amt) - @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty),
	                           do_qty = do_qty + @real_qty, do_amt = do_amt + @real_qty * @price,
	                           settle_qty = @stock_qty - @real_qty,settle_amt = (@stock_qty - @real_qty) * @cost_price,
	                           settle_amt2 = (@stock_qty - @real_qty)* @sale_price, com_flag = '0',update_date=getdate()
	                   WHERE (item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no)
	          ELSE
	              INSERT INTO t_da_jxc_daysum (
	                     item_no, oper_date, branch_no, cost_price, init_qty, init_amt, init_amt2,
	 
                    avg_cost_amt, avg_profit_amt,pi_qty, pi_amt, so_qty, so_amt, mi_qty,mi_amt,
	                     mo_qty, mo_amt, di_qty, di_amt,do_qty, do_amt, pos_qty,pos_amt, pos_ret_qty,pos_ret_amt,
	                     pos_send_qty,pos_send_amt, oo_qty, oo_amt, settle_qty,settle_amt,settle_amt2,
	                     cost_change_amt, pos_dct_amt, main_sup, dec_rate,so_price, sale_price, com_flag,item_sup_flag,sup_ly_rate ,update_date)
	                  VALUES ( @item_no, @ls_operdate ,@branch_no, @cost_price,@stock_qty,@stock_money,@stock_qty*@sale_price,
	                              0, 0,0,0,0,0,0,0,0,0,0,0,@real_qty,@real_qty * @price,
	                     0,0,0,0,0,0,0,0,@stock_qty - @real_qty,(@stock_qty - @real_qty) * @cost_price ,(@stock_qty - @real_qty)* @sale_price,
	                     0,0,@sup_no,0,@so_price, @sale_price, '0',@item_sup_flag,@sup_rate,getdate())

       end
   END  --if @trans_no='IO' --转仓单 dutz 2007-05-22
   
   if @trans_no='MI'
   BEGIN
      IF exists(select item_no from t_im_branch_stock where item_no=@item_no and branch_no = @branch_no)
         update t_im_branch_stock set last_inprice=@price,oper_date=getdate(),avg_cost = @cost_price ,  
              cost_amt = (stock_qty + @real_qty) * @cost_price, stock_qty=stock_qty + @real_qty ,com_flag='0'
          where branch_no=@branch_no and item_no=@item_no
      ELSE
         insert into t_im_branch_stock (branch_no, item_no, stock_qty, avg_cost,last_baseprice,oper_date,cost_amt)
          values(@branch_no, @item_no, @real_qty, @cost_price,@price,getdate(),@real_qty * @cost_price)
   
      if @@error != 0 goto myerror

	      IF exists(select * from t_da_jxc_daysum where(item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no))
	         UPDATE   t_da_jxc_daysum
	               SET   item_sup_flag = @item_sup_flag,sup_ly_rate = @sup_rate,
						 avg_cost_amt = @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty), cost_price = @cost_price,
	                     avg_profit_amt = (so_amt + pos_amt + pos_ret_amt) - @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty),
	                     mi_qty = mi_qty + @real_qty, mi_amt = mi_amt + @real_qty * @price,
	                     settle_qty = @stock_qty + @real_qty,settle_amt = (@stock_qty + @real_qty) * @cost_price,
	                     settle_amt2 = (@stock_qty + @real_qty)* @sale_price, com_flag = '0',update_date=getdate()
	             WHERE (item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no)
	      ELSE
	         INSERT INTO t_da_jxc_daysum (
	               item_no, oper_date, branch_no, cost_price, init_qty, init_amt, init_amt2,
	               avg_cost_amt, avg_profit_amt,pi_qty, pi_amt, so_qty, so_amt, mi_qty,mi_amt,
	               mo_qty, mo_amt, di_qty, di_amt,do_qty, do_amt, pos_qty,pos_amt, pos_ret_qty,pos_ret_amt,
	               pos_send_qty,pos_send_amt, oo_qty, oo_amt, settle_qty,settle_amt,settle_amt2,
	               cost_change_amt, pos_dct_amt, main_sup, dec_rate,so_price, sale_price, com_flag,item_sup_flag,sup_ly_rate,update_date )
	            VALUES ( @item_no, @ls_operdate ,@branch_no, @cost_price,@stock_qty,@stock_money,@stock_qty*@sale_price,
	                        0, 0,0,0,0,0,@real_qty,@real_qty * @price,
	               0,0,0,0,0,0,0,0,0,0,0,0,0,0,@stock_qty + @real_qty,(@stock_qty + @real_qty) * @cost_price ,(@stock_qty + @real_qty)* @sale_price,
	               0,0,@sup_no,0,@so_price, @sale_price, '0',@item_sup_flag,@sup_rate,getdate())

   END  --if @trans_no='MI'
   
   if @trans_no='DI'
   BEGIN
       IF exists(select item_no from t_im_branch_stock where item_no=@item_no and branch_no = @branch_no)
          update t_im_branch_stock set last_inprice=@price,oper_date=getdate(),avg_cost = @cost_price ,
                 cost_amt = (stock_qty + @real_qty) * @cost_price, stock_qty=stock_qty + @real_qty ,com_flag='0'
          where branch_no=@branch_no and item_no=@item_no
       ELSE
         insert into t_im_branch_stock (branch_no, item_no, stock_qty, avg_cost,last_baseprice,oper_date,cost_amt)
          values(@branch_no, @item_no, @real_qty, @cost_price,@price,getdate(),@real_qty * @cost_price)
       
       if @@error != 0 goto myerror

	 IF exists(select * from t_da_jxc_daysum where(item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no))
	           UPDATE    t_da_jxc_daysum
	               SET   item_sup_flag = @item_sup_flag,sup_ly_rate = @sup_rate,
						 avg_cost_amt = @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty), cost_price = @cost_price,
						 avg_profit_amt = (so_amt + pos_amt + pos_ret_amt) - @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty),
	                     di_qty = di_qty + @real_qty, di_amt = di_amt + @real_qty * @price,
	                     settle_qty = @stock_qty + @real_qty,settle_amt = (@stock_qty + @real_qty) * @cost_price,
	                     settle_amt2 = (@stock_qty + @real_qty)* @sale_price, com_flag = '0',update_date=getdate()
	             WHERE (item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no)
	       ELSE
	          INSERT INTO t_da_jxc_daysum (
	               item_no, oper_date, branch_no, cost_price, init_qty, init_amt, init_amt2,
	               avg_cost_amt, avg_profit_amt,pi_qty, pi_amt, so_qty, so_amt, mi_qty,mi_amt,
	               mo_qty, mo_amt, di_qty, di_amt,do_qty, do_amt, pos_qty,pos_amt, pos_ret_qty,pos_ret_amt,
	               pos_send_qty,pos_send_amt, oo_qty, oo_amt, settle_qty,settle_amt,settle_amt2,
	               cost_change_amt, pos_dct_amt, main_sup, dec_rate,so_price, sale_price, com_flag,item_sup_flag,sup_ly_rate,update_date )
	            VALUES ( @item_no, @ls_operdate ,@branch_no, @cost_price,@stock_qty,@stock_money,@stock_qty*@sale_price,
	                        0, 0,0,0,0,0,0,0,0,0,@real_qty,@real_qty * @price,
	               0,0,0,0,0,0,0,0,0,0,@stock_qty + @real_qty,(@stock_qty + @real_qty) * @cost_price ,(@stock_qty + @real_qty)* @sale_price,
	               0,0,@sup_no,0,@so_price, @sale_price, '0',@item_sup_flag,@sup_rate,getdate())

   END
   
   if @trans_no='MO'
   BEGIN
       IF exists(select item_no from t_im_branch_stock where item_no=@item_no and branch_no = @branch_no)
          update t_im_branch_stock set last_inprice=@price,oper_date=getdate(),avg_cost = @cost_price ,
             cost_amt = (stock_qty - @real_qty) * @cost_price , stock_qty=stock_qty - @real_qty ,com_flag='0'
           where branch_no=@branch_no and item_no=@item_no  
       ELSE
          insert into t_im_branch_stock (branch_no, item_no, stock_qty, avg_cost,last_baseprice,oper_date,cost_amt)
          values(@branch_no, @item_no, -@real_qty, @cost_price,@price,getdate(),-@real_qty * @cost_price)
       
       if @@error != 0 goto myerror

	       IF exists(select * from t_da_jxc_daysum where(item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no))
	            UPDATE    t_da_jxc_daysum
	               SET   item_sup_flag = @item_sup_flag,sup_ly_rate = @sup_rate,
						 avg_cost_amt = @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty), cost_price = @cost_price,
						 avg_profit_amt = (so_amt + pos_amt + pos_ret_amt) - @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty),
	                     mo_qty = mo_qty + @real_qty, mo_amt = mo_amt + @real_qty * @price,
	                     settle_qty = @stock_qty - @real_qty,settle_amt = (@stock_qty - @real_qty) * @cost_price,
	                     settle_amt2 = (@stock_qty - @real_qty)* @sale_price, com_flag = '0',update_date=getdate()
	             WHERE (item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no)
	       ELSE
	           INSERT INTO t_da_jxc_daysum (
	               item_no, oper_date, branch_no, cost_price, init_qty, init_amt, init_amt2,
	               avg_cost_amt, avg_profit_amt,pi_qty, pi_amt, so_qty, so_amt, mi_qty,mi_amt,
	               mo_qty, mo_amt, di_qty, di_amt,do_qty, do_amt, pos_qty,pos_amt, pos_ret_qty,pos_ret_amt,
	               pos_send_qty,pos_send_amt, oo_qty, oo_amt, settle_qty,settle_amt,settle_amt2,
	               cost_change_amt, pos_dct_amt, main_sup, dec_rate,so_price, sale_price, com_flag,item_sup_flag ,sup_ly_rate,update_date)
	     VALUES ( @item_no, @ls_operdate ,@branch_no, @cost_price,@stock_qty,@stock_money,@stock_qty*@sale_price,
	                        0, 0,0,0,0,0,0,0,@real_qty,@real_qty * @price,
	               0,0,0,0,0,0,0,0,0,0,0,0,@stock_qty - @real_qty,(@stock_qty - @real_qty) * @cost_price ,(@stock_qty - @real_qty)* @sale_price,
	               0,0,@sup_no,0,@so_price, @sale_price, '0',@item_sup_flag,@sup_rate,getdate())

   END
   
   if @trans_no='DO'
   BEGIN
       IF exists(select item_no from t_im_branch_stock where item_no=@item_no and branch_no = @branch_no)
          update t_im_branch_stock set last_inprice=@price,oper_date=getdate(),avg_cost = @cost_price ,
                 cost_amt = (stock_qty - @real_qty) * @cost_price , stock_qty=stock_qty - @real_qty ,com_flag='0'
           where branch_no=@branch_no and item_no=@item_no
       ELSE
          insert into t_im_branch_stock (branch_no, item_no, stock_qty, avg_cost,last_baseprice,oper_date,cost_amt)
          values(@branch_no, @item_no, -@real_qty, @cost_price,@price,getdate(),-@real_qty * @cost_price)
     
      if @@error != 0 goto myerror

	      IF exists(select * from t_da_jxc_daysum where(item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no))
	          UPDATE    t_da_jxc_daysum
	             SET     item_sup_flag = @item_sup_flag,sup_ly_rate = @sup_rate,
						 avg_cost_amt = @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty), cost_price = @cost_price,
	                     avg_profit_amt = (so_amt + pos_amt + pos_ret_amt) - @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty),
	                     do_qty = do_qty + @real_qty, do_amt = do_amt + @real_qty * @price,
	                     settle_qty = @stock_qty - @real_qty,settle_amt = (@stock_qty - @real_qty) * @cost_price,
	                     settle_amt2 = (@stock_qty - @real_qty)* @sale_price, com_flag = '0',update_date=getdate()
	          WHERE (item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no)
	      ELSE
	          INSERT INTO t_da_jxc_daysum ( 
	               item_no, oper_date, branch_no, cost_price, init_qty, init_amt, init_amt2,   
	               avg_cost_amt, avg_profit_amt,pi_qty, pi_amt, so_qty, so_amt, mi_qty,mi_amt,   
	               mo_qty, mo_amt, di_qty, di_amt,do_qty, do_amt, pos_qty,pos_amt, pos_ret_qty,pos_ret_amt,
	               pos_send_qty,pos_send_amt, oo_qty, oo_amt, settle_qty,settle_amt,settle_amt2,
	               cost_change_amt, pos_dct_amt, main_sup, dec_rate,so_price, sale_price, com_flag,item_sup_flag,sup_ly_rate ,update_date)
	            VALUES ( @item_no, @ls_operdate ,@branch_no, @cost_price,@stock_qty,@stock_money,@stock_qty*@sale_price,
	                        0, 0,0,0,0,0,0,0,0,0,0,0,@real_qty,@real_qty * @price,
	               0,0,0,0,0,0,0,0,@stock_qty - @real_qty,(@stock_qty - @real_qty) * @cost_price ,(@stock_qty - @real_qty)* @sale_price,
	               0,0,@sup_no,0,@so_price, @sale_price, '0',@item_sup_flag,@sup_rate,getdate())

   END
   
   if @trans_no='OO' or @trans_no='PD' or @trans_no='CS' --Add By Huangsc 2006.06.28 添加组合拆分单
   BEGIN
       IF @db_no = '-'
         select @real_qty = - @real_qty
       
       IF exists(select item_no from t_im_branch_stock where item_no=@item_no and branch_no = @branch_no)
          update t_im_branch_stock set last_inprice=@price,oper_date=getdate(),avg_cost = @cost_price ,
             cost_amt = (stock_qty + @real_qty) * @cost_price, stock_qty=stock_qty + @real_qty ,com_flag='0'
           where branch_no=@branch_no and item_no=@item_no
       ELSE
          insert into t_im_branch_stock (branch_no, item_no, stock_qty, avg_cost,last_inprice,oper_date,cost_amt)
          values(@branch_no, @item_no, @real_qty, @cost_price,@price,getdate(),@real_qty * @cost_price)
       
       if @@error != 0 goto myerror

	       IF exists(select * from t_da_jxc_daysum where(item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no))
	     
	          UPDATE  t_da_jxc_daysum
	            SET   item_sup_flag = @item_sup_flag,sup_ly_rate = @sup_rate,
					  avg_cost_amt = @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty), cost_price = @cost_price,
					  avg_profit_amt = (so_amt + pos_amt + pos_ret_amt) - @cost_price * (so_qty + pos_qty + pos_ret_qty + pos_send_qty),
	                  oo_qty = oo_qty + @real_qty, oo_amt = oo_amt + @real_qty * @price,
	                  settle_qty = @stock_qty + @real_qty,settle_amt = (@stock_qty +@real_qty) * @cost_price,
	                  settle_amt2 = (@stock_qty + @real_qty)* @sale_price, com_flag = '0',update_date=getdate()
	          WHERE (item_no=@item_no and oper_date=@ls_operdate and branch_no=@branch_no)
	       ELSE
	          INSERT INTO t_da_jxc_daysum (
	            item_no, oper_date, branch_no, cost_price, init_qty, init_amt, init_amt2,
	            avg_cost_amt, avg_profit_amt,pi_qty, pi_amt, so_qty, so_amt, mi_qty,mi_amt,
	            mo_qty, mo_amt, di_qty, di_amt,do_qty, do_amt, pos_qty,pos_amt, pos_ret_qty,pos_ret_amt,
	            pos_send_qty,pos_send_amt, oo_qty, oo_amt, settle_qty,settle_amt,settle_amt2,
	            cost_change_amt, pos_dct_amt, main_sup, dec_rate,so_price, sale_price, com_flag,item_sup_flag ,sup_ly_rate,update_date)
	         VALUES ( @item_no, @ls_operdate ,@branch_no, @cost_price,@stock_qty,@stock_money,@stock_qty*@sale_price,
	                     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,@real_qty,@real_qty * @price,
	            @stock_qty + @real_qty,(@stock_qty + @real_qty) * @cost_price,(@stock_qty + @real_qty)* @sale_price,
	            0,0,@sup_no,0,@so_price, @sale_price, '0',@item_sup_flag,@sup_rate,getdate())

      
      if @@error != 0 goto myerror
      
   END 
  
   
   if @item_stock = '1'  
      delete t_im_branch_stock where item_no = @item_no
   
   if @@error != 0 goto myerror

myerror:
  return
end
go
