if exists(select 1 from sysobjects where name='tr_im_sheet_master')
  drop Trigger tr_im_sheet_master
go

create trigger tr_im_sheet_master on t_im_sheet_master  
for update 
as
begin
  if not update(approve_flag) return

  declare
    @sheet_no  char(18) ,--工作单号
    @trans_no      char(2)         ,--库存操作方式
    @db_no        char(1)      ,--出入库方式
    @branch_no       char(6)        ,--本店代码
    @d_branch_no    char(6)        ,--目的地代码
    @voucher_no      char(18)         ,--原始凭证号
    @supcust_no     char(8)        ,--供应商客户代码
    @coin_no         char(3)         ,--币种
    @pay_way        char(2)         ,--付款方式
    @pay_date        datetime   ,--付款日期
    @approve_flag   char(1)      ,--审批标志  --0-未审核，1-审核
    @oper_date       datetime   ,--操作日期
    @work_date       datetime   ,--审核日期
    @order_man       char(4),
    @oper_id         char(4),
    @confirm_man     char(4),
    @trans_flag      char(1),
    @acct_flag       char(1),
    @item_no         char(20)        ,--货品代码
    @order_qty          numeric(16,4)   ,--数量
    @real_qty          numeric(16,4)   ,--数量
    @orgi_price      numeric(16,4)   ,--原价
    @valid_price     numeric(16,4)   ,--实际单价
    @sale_price     numeric(16,4)   ,--零售单价
    @sub_amt         numeric(16,4),
    @tax             numeric(5,4),
    @valid_date      datetime,
    @old_trans       char(2),
    @t_branch_no     char(6),
    @total_amt       numeric(16,4),   --金额
    @t_temp          integer,
    @pay_type        integer ,
    @account         char(1),
    @already_update  char(1),
    @y_item_no       char(20)        ,--货品代码(调价)
    @other1			varchar(20),
    @yh_no      char(18),        --原始要货单号
    @pi_sale_amt     numeric(16,4),--
    @stock_qty	     numeric(16,4),--库存
    @cost_price	     numeric(16,4),--成本价

    @sheet_no_var  char(18) ,--工作单号
    @trans_no_var      char(2)         ,--库存操作方式
    @branch_no_var       char(6)        ,--本店代码
    @supcust_no_var     char(8)        ,--供应商客户代码
    @sale_way_var     char(1),
    @oper_date_var       datetime   ,--操作日期
    @valid_date_var      datetime,
    @save_accflag	char(1),
    @branch_type	char(1),
    @branch_no_var_b	char(1),
    @branch_type_to	char(1),
    @d_branch_no_var    char(6),        --目的地代码
    @lb_change_price	char(1),
    @stock_money	numeric(16,4)--库存金额

  declare ic_cur_1 cursor for
    select a.sheet_no,a.trans_no,a.db_no,a.branch_no,isnull(a.d_branch_no,''),
        isnull(a.voucher_no,''),isnull(a.supcust_no,''),isnull(a.coin_no,'RMB'),
        isnull(a.pay_way,'A'), a.pay_date,a.approve_flag,
        a.oper_date,a.work_date,a.order_man,a.oper_id,a.confirm_man,
        a.trans_flag,a.acct_flag,
        b.item_no,b.order_qty,b.real_qty,b.orgi_price,b.valid_price,
        b.sub_amt,b.tax,b.sale_price
    from inserted a, t_im_sheet_detail b
    where ( a.sheet_no = b.sheet_no )
      and (a.approve_flag = '1') and  b.real_qty <> 0

  select @total_amt=0
  select @already_update='0'
  select @save_accflag = '0'

  open ic_cur_1
  fetch ic_cur_1 into
        @sheet_no,@trans_no,@db_no,@branch_no,@d_branch_no,
        @voucher_no,@supcust_no,@coin_no,
        @pay_way, @pay_date,@approve_flag,
        @oper_date,@work_date,@order_man, @oper_id,@confirm_man,
        @trans_flag,@acct_flag,
        @item_no,@order_qty,@real_qty,@orgi_price,@valid_price,
        @sub_amt,@tax,@sale_price


  while @@fetch_status=0
  begin

     select @sheet_no_var = @sheet_no
     select @trans_no_var = @trans_no
     select @oper_date_var = @oper_date
     select @branch_no_var = @branch_no
     select @d_branch_no_var = @d_branch_no
     SELECT @lb_change_price = change_cost FROM t_sys_sheet_no WHERE sheet_id = @trans_no

    if @trans_no='MI' or @trans_no='DI'  --外部调入、门店外部调入
    begin
      select @supcust_no=@branch_no  --退货客户/供应商为源机构
      
      select @branch_type = trade_type from t_bd_branch_info where branch_no = @d_branch_no

      if @branch_type = '4' or @branch_type = '8'
	       select @save_accflag = '2'

  
--只写目标仓库入库流水（源仓库为外部机构）

     
     
	
          select @t_branch_no=@d_branch_no
          select @d_branch_no=@branch_no
          select @branch_no=@t_branch_no
	
      select @db_no='+'
      
      --更新原始调出单的收货状态
      if rtrim(@voucher_no) <> ''
      begin
         --修改原始调出单order_qty,状态
         UPDATE t_im_sheet_detail  SET order_qty = isnull(order_qty,0) + @real_qty
           where sheet_no = @voucher_no and item_no = @item_no
         if @@error != 0 goto myerror
    
         --更新定单状态
         IF not exists(select sheet_no   from t_im_sheet_detail
           where sheet_no = @voucher_no and real_qty > order_qty group by sheet_no)
           UPDATE t_im_sheet_master  SET in_flag = '2' where sheet_no = @voucher_no
         else
           UPDATE t_im_sheet_master  SET in_flag = '1' where sheet_no = @voucher_no
         if @@error != 0 goto myerror
      end

      select @yh_no=isnull(voucher_no,'') from t_im_sheet_master where sheet_no=@voucher_no
	
  	--对于门店收货，写门店要货的最终到货量
      if len(@yh_no)>0 
  	begin
         --修改要货单到货数量num1,订单状态
         UPDATE t_pm_sheet_detail SET come_num = isnull(come_num,0) + @real_qty
           where sheet_no = @yh_no and item_no = @item_no
         if @@error != 0 goto myerror
    
         --更新要货单状态
         IF not exists(select sheet_no from t_pm_sheet_detail where sheet_no = @yh_no and real_qty > isnull(come_num,0) group by sheet_no)
           UPDATE t_pm_sheet_master  SET in_flag = '2' where sheet_no = @yh_no
         else
           UPDATE t_pm_sheet_master  SET in_flag = '1' where sheet_no = @yh_no
           if @@error != 0 goto myerror
  	end

      --update t_im_sheet_master set in_flag='1'  where sheet_no=@voucher_no

      
    end
    else if @trans_no = 'IO'   --转仓单  
    begin

			--获得转入仓库库存数量和成本价
		 	select @stock_qty = null
    	select @stock_money = null
    	select @cost_price = null
    	select @stock_qty = stock_qty,@cost_price = avg_cost from t_im_branch_stock 
    	 where branch_no =@d_branch_no and item_no = @item_no
    	if @@error != 0 goto myerror

    	if @stock_qty is null 
        	select @stock_qty = 0

    	if @cost_price is null 
	       select @cost_price = price from t_bd_item_info where item_no = @item_no

      --select @branch_type = trade_type from t_bd_branch_info where branch_no = @branch_no_var
        
			--select @real_qty = - @real_qty
			select @stock_qty = @stock_qty + @real_qty
			
			select @cost_price = @valid_price
			  /*取成本价已改为在t_im_flow表的插入时取，此处成本价取当前价
			if @lb_change_price = '1'
	                   -- Add By Huangsc 2006.04.14
	                   if @stock_qty = 0 
		   	      select @cost_price = (abs(@stock_money) + abs(@real_qty*@valid_price))/(abs(@stock_qty)+ abs(@real_qty))
	                   else
	                      select @cost_price = (@stock_money + @real_qty*@valid_price)/@stock_qty
	      */
	      
			--写流水(目的)
        Insert into t_im_flow(voucher_no,oper_date, item_no,real_qty,price,cost_price,sale_price,
			    sheet_amt,branch_no,d_branch_no,trans_no,db_no,supcust_no,oper_id,
			    pre_qnty,valid_date,memo,acc_date)
			  values(@sheet_no,@work_date,@item_no,@real_qty,@valid_price,@cost_price,@sale_price,@real_qty*@valid_price,
			        @d_branch_no,@branch_no,@trans_no,'+',@branch_no,@oper_id,
				@stock_qty, @valid_date,@voucher_no,@pay_date)
		
      	if @@error != 0 goto myerror
        
      --获得转出仓库库存数量和成本价
		 	select @stock_qty = null
    	select @stock_money = null
    	select @cost_price = null
    	select @stock_qty = stock_qty,@cost_price = avg_cost from t_im_branch_stock 
    	 where branch_no =@branch_no and item_no = @item_no
    	if @@error != 0 goto myerror

    	if @stock_qty is null 
        	select @stock_qty = 0

    	if @cost_price is null 
	       select @cost_price = price from t_bd_item_info where item_no = @item_no

      --select @branch_type = trade_type from t_bd_branch_info where branch_no = @branch_no_var
        
			--select @real_qty = - @real_qty
			select @stock_qty = @stock_qty - @real_qty
			
			select @cost_price = @valid_price
			  /*取成本价已改为在t_im_flow表的插入时取，此处成本价取当前价
			if @lb_change_price = '1'
	                   -- Add By Huangsc 2006.04.14
	                   if @stock_qty = 0 
		   	      select @cost_price = (abs(@stock_money) + abs(@real_qty*@valid_price))/(abs(@stock_qty)+ abs(@real_qty))
	                   else
	                      select @cost_price = (@stock_money + @real_qty*@valid_price)/@stock_qty
	      */
	      
			--写流水(目的)
        Insert into t_im_flow(voucher_no,oper_date, item_no,real_qty,price,cost_price,sale_price,
			    sheet_amt,branch_no,d_branch_no,trans_no,db_no,supcust_no,oper_id,
			    pre_qnty,valid_date,memo,acc_date)
			  values(@sheet_no,@work_date,@item_no,@real_qty,@valid_price,@cost_price,@sale_price,@real_qty*@valid_price,
			        @branch_no,@d_branch_no,@trans_no,'-',@branch_no,@oper_id,
				@stock_qty, @valid_date,@voucher_no,@pay_date)
		
      	if @@error != 0 goto myerror

        
        
				--update t_im_sheet_master set in_flag = '1' where sheet_no = @sheet_no

				--if @@error != 0 goto myerror
	
    end
    
    else if @trans_no='MO' or @trans_no='DO' --外部调出
    begin
      --只写源仓库出库流水
      select @db_no='-'
      select @supcust_no=@d_branch_no  --客户/供应商为目标机构

      --更新原始要货单
      --update t_pm_sheet_master set in_flag='1' where sheet_no=@voucher_no

      if rtrim(@voucher_no) <> ''
      begin
      	 --修改直调申请单数量order_qty,订单状态
         UPDATE t_im_sheet_detail  SET order_qty = isnull(order_qty,0) + @real_qty
           where sheet_no = @voucher_no and item_no = @item_no
         if @@error != 0 goto myerror
         --更新直调申请单状态
         IF not exists(select sheet_no   from t_im_sheet_detail
           where sheet_no = @voucher_no and real_qty > order_qty group by sheet_no)
           UPDATE t_im_sheet_master  SET other_appr_flag = '3' where sheet_no = @voucher_no
         else
           UPDATE t_im_sheet_master  SET other_appr_flag = '2' where sheet_no = @voucher_no
         if @@error != 0 goto myerror
         
         --修改要货单采购数量order_qty,订单状态
         UPDATE t_pm_sheet_detail  SET order_qty = isnull(order_qty,0) + @real_qty
           where sheet_no = @voucher_no and item_no = @item_no
         if @@error != 0 goto myerror
    
         --更新定单状态
         IF not exists(select sheet_no   from t_pm_sheet_detail
           where sheet_no = @voucher_no and real_qty > order_qty group by sheet_no)
           UPDATE t_pm_sheet_master  SET order_status = '2' where sheet_no = @voucher_no
         else
           UPDATE t_pm_sheet_master  SET order_status = '1' where sheet_no = @voucher_no
         if @@error != 0 goto myerror
      end
      
      if @branch_type = '4' or @branch_type = '8'
	 select @save_accflag = '1'

    end

    --获得库存数量和成本价
    select @stock_qty = null
    select @stock_money = null
    select @cost_price = null
    select @stock_qty = stock_qty,@cost_price = avg_cost, @stock_money = cost_amt from t_im_branch_stock 
    where branch_no =@branch_no and item_no = @item_no
    if @@error != 0 goto myerror

    if @stock_qty is null 
        select @stock_qty = 0
    
    if @stock_money is null 
        select @stock_money = 0

    if @cost_price is null 
	select @cost_price = price from t_bd_item_info where item_no = @item_no

    if @trans_no = 'MI' or @trans_no = 'DI'
       begin
	       select @stock_qty = @stock_qty + @real_qty
	       
	       select @cost_price = @valid_price
	/*取成本价已改为在t_im_flow表的插入时取，此处成本价取当前价
	if @lb_change_price = '1'
           -- Add by Huangsc 2006.04.14
           if @stock_qty = 0
	      select @cost_price = (abs(@stock_money) + abs(@real_qty*@valid_price))/(abs(@stock_qty)+ abs(@real_qty))
           else
              select @cost_price = (@stock_money + @real_qty*@valid_price)/@stock_qty
   */
       end

    if @trans_no = 'MO' or @trans_no = 'DO'
       begin
	     select @stock_qty = @stock_qty - @real_qty
	     
	     select @cost_price = @valid_price
	/*取成本价已改为在t_im_flow表的插入时取，此处成本价取当前价
	if @lb_change_price = '1'
           -- Add By Huangsc 2006.04.14
           if @stock_qty = 0
             select @cost_price = (abs(@stock_money) - abs(@real_qty*@valid_price))/(abs(@stock_qty)+ abs(@real_qty))
           else
	     select @cost_price = (@stock_money - @real_qty*@valid_price)/@stock_qty
   */
       end

   if @trans_no = 'OO' or @trans_no = 'MD'
       begin

	if @db_no = '+'
	  begin	
	    select @stock_qty = @stock_qty + @real_qty
		  
		  select @cost_price = @valid_price
		  
		  /*取成本价已改为在t_im_flow表的插入时取，此处成本价取当前价
	    if @lb_change_price = '1'
            -- Add By Huangsc 2006.04.14
            if @stock_qty = 0
                select @cost_price = (abs(@stock_money) + abs(@real_qty*@valid_price))/(abs(@stock_qty)+ abs(@real_qty))
            else
                select @cost_price = (@stock_money + @real_qty*@valid_price)/@stock_qty
      */
	  end
        else
 	  begin	
	    select @stock_qty = @stock_qty - @real_qty
		  select @cost_price = @valid_price
		  /*取成本价已改为在t_im_flow表的插入时取，此处成本价取当前价
	    if @lb_change_price = '1'
                -- Add By Huangsc 2006.04.14
                if @stock_qty = 0
                   select @cost_price = (abs(@stock_money) - abs(@real_qty*@valid_price))/(abs(@stock_qty)+ abs(@real_qty))
                else 
		   select @cost_price = (@stock_money - @real_qty*@valid_price)/@stock_qty
		   */
	  end
       end

   --写流水
    if @trans_no <> 'JJ' and @trans_no <> 'ZH' and @trans_no <> 'IO' and @trans_no <> 'DI' --残次品调价、转货
    begin
         if @trans_no = 'MI' or @trans_no = 'DI'
	    select @pi_sale_amt = round(@real_qty * @sale_price, 2)
	 else
	    select @pi_sale_amt = 0
	 
	 Insert into t_im_flow(voucher_no,oper_date, item_no,real_qty,price,cost_price,sale_price,
	    sheet_amt,branch_no,d_branch_no,trans_no,db_no,supcust_no,oper_id,
	    pre_qnty,valid_date,memo,acc_date)
	  values(@sheet_no,@work_date,@item_no,@real_qty,@valid_price,@cost_price,@sale_price,@real_qty*@valid_price,
	        @branch_no,@d_branch_no,@trans_no,@db_no,@supcust_no,@oper_id,
		@stock_qty, @valid_date,@voucher_no,@pay_date)

         if @@error != 0 goto myerror
    end  

    if @trans_no = 'DI'
     begin

	--获得库存数量和成本价
 	select @stock_qty = null
    	select @stock_money = null
    	select @cost_price = null
    	select @stock_qty = stock_qty,@cost_price = avg_cost from t_im_branch_stock 
    	 where branch_no =@d_branch_no and item_no = @item_no
    	if @@error != 0 goto myerror

    	if @stock_qty is null 
        	select @stock_qty = 0

    	if @cost_price is null 
	  select @cost_price = price from t_bd_item_info where item_no = @item_no

        select @branch_type = trade_type from t_bd_branch_info where branch_no = @branch_no_var
     
        if ( @branch_type = '5' or @branch_type = '6' ) and len(@sheet_no) = 15
	 begin
		
		--select @branch_no_var_b = trade_type from t_bd_branch_info where branch_no = SUBSTRING(@branch_no_var,1,3)  
		--if @branch_no_var_b = '7'
			--begin
				--select @real_qty = -1 * @real_qty
			--end
			
		select @stock_qty = @stock_qty + @real_qty
		
		select @cost_price = @valid_price
		  /*取成本价已改为在t_im_flow表的插入时取，此处成本价取当前价
		if @lb_change_price = '1'
         -- Add By Huangsc 2006.04.14
                   if @stock_qty = 0 
	   	      select @cost_price = (abs(@stock_money) + abs(@real_qty*@valid_price))/(abs(@stock_qty)+ abs(@real_qty))
                   else
                      select @cost_price = (@stock_money + @real_qty*@valid_price)/@stock_qty
      */
      
		--写流水(目的)
	        Insert into t_im_flow(voucher_no,oper_date, item_no,real_qty,price,cost_price,sale_price,
		    sheet_amt,branch_no,d_branch_no,trans_no,db_no,supcust_no,oper_id,
		    pre_qnty,valid_date,memo,acc_date)
		  values(@sheet_no,@work_date,@item_no,@real_qty,@valid_price,@cost_price,@sale_price,@real_qty*@valid_price,
		        @d_branch_no,@branch_no,@trans_no,'+',@branch_no,@oper_id,
			@stock_qty, @valid_date,@voucher_no,@pay_date)
	
	      	if @@error != 0 goto myerror

		update t_im_sheet_master set in_flag = '1' where sheet_no = @sheet_no
	
		if @@error != 0 goto myerror
	end
	
     end
    
    if  @trans_no = 'DO'
     begin

	--获得库存数量和成本价
	select @stock_qty = null
    	select @stock_money = null
    	select @cost_price = null
    	select @stock_qty = stock_qty,@cost_price = avg_cost from t_im_branch_stock 
    	where branch_no =@d_branch_no and item_no = @item_no
    	if @@error != 0 goto myerror

    	if @stock_qty is null 
        	select @stock_qty = 0

    	if @cost_price is null 
		select @cost_price = price from t_bd_item_info where item_no = @item_no

	select @branch_type = trade_type from t_bd_branch_info where branch_no = left(@d_branch_no,3)


	if ( @branch_type = '5' or @branch_type = '6') and len(@sheet_no) = 15
	 begin
		select @real_qty = - @real_qty
		select @stock_qty = @stock_qty - @real_qty
		
		select @cost_price = @valid_price
		  /*取成本价已改为在t_im_flow表的插入时取，此处成本价取当前价
		if @lb_change_price = '1'
	   	   select @cost_price = (@stock_money - @real_qty*@valid_price)/@stock_qty
      */
		--写流水(目的)
	        Insert into t_im_flow(voucher_no,oper_date, item_no,real_qty,price,cost_price,sale_price,
		    sheet_amt,branch_no,d_branch_no,trans_no,db_no,supcust_no,oper_id,
		    pre_qnty,valid_date,memo,acc_date)
		  values(@sheet_no,@work_date,@item_no,@real_qty,@valid_price,@cost_price,@sale_price,@real_qty*@valid_price,
		        @d_branch_no,@branch_no,@trans_no,'-',@branch_no,@oper_id,
			@stock_qty, @valid_date,@voucher_no,@pay_date)
	        if @@error != 0 goto myerror
	--add by wangy DO单,如果是同一仓库,直接修改状态为已调入 2007-03-15
	if exists (select 1 from  t_bd_branch_info where trade_type='5' and branch_no=@branch_no) and   exists (select 1 from  t_bd_branch_info where trade_type='5' and branch_no=@d_branch_no)
	and  left(@branch_no,3) = left(@d_branch_no,3)
		update t_im_sheet_master set in_flag = '2' where sheet_no = @sheet_no
	else
		update t_im_sheet_master set in_flag = '1' where sheet_no = @sheet_no
	
		if @@error != 0 goto myerror
	 end
     end

 
    if @trans_no='DI' or @trans_no='DO' or @trans_no='MI' or @trans_no='MO'
      select @total_amt=@total_amt+@real_qty*@valid_price

    fetch ic_cur_1 into
        @sheet_no,@trans_no,@db_no,@branch_no,@d_branch_no,
        @voucher_no,@supcust_no,@coin_no,
        @pay_way, @pay_date,@approve_flag,
        @oper_date,@work_date,@order_man, @oper_id,@confirm_man,
        @trans_flag,@acct_flag,
        @item_no,@order_qty,@real_qty,@orgi_price,@valid_price,
        @sub_amt,@tax,@sale_price
  end


 --产生帐款
    if @trans_no_var = 'DI' 
     begin

	   select @branch_type = trade_type from t_bd_branch_info where branch_no = @d_branch_no_var
	   select @branch_type_to = trade_type from t_bd_branch_info where branch_no = @branch_no_var

	   if @branch_type = '5' 
	    	select @d_branch_no_var = left(@d_branch_no_var,3)
		select @branch_type = trade_type from t_bd_branch_info where branch_no = @d_branch_no_var
	   if @branch_type_to = '5' 
	    	select @branch_no_var = left(@branch_no_var,3)
		select @branch_type_to = trade_type from t_bd_branch_info where branch_no = @branch_no_var
		
 	   if @branch_type = '4' or @branch_type_to = '4' or @branch_type = '8' or @branch_type_to = '8'
	     begin
		insert into t_fm_recpay_flow(pay_type,voucher_no,trans_no,should_amt,tax_amt,sale_way,oper_date,settle_flag,supcust_no,pay_way,acc_date,branch_no,acc_type)
	  	values(1, @sheet_no_var, @trans_no_var,-1*@total_amt,0,'A',@oper_date_var,'0',@branch_no_var,'A',@pay_date,@d_branch_no_var,'B')
		if @@error != 0 
goto myerror
	    end
	
     end

    if @trans_no_var='DO' 
     begin

	   select @branch_type = trade_type from t_bd_branch_info where branch_no = @branch_no_var
	   select @branch_type_to = trade_type from t_bd_branch_info where branch_no = @d_branch_no_var

	   if @branch_type = '5' 
	    	select @branch_no_var = left(@branch_no_var,3)
		select @branch_type = trade_type from t_bd_branch_info where branch_no = @branch_no_var
	   if @branch_type_to = '5' 
	    	select @d_branch_no_var = left(@d_branch_no_var,3)
		select @branch_type_to = trade_type from t_bd_branch_info where branch_no = @d_branch_no_var

	   if @branch_type = '4' or  @branch_type_to = '4' or @branch_type = '8' or @branch_type_to = '8'
	     begin
		insert into t_fm_recpay_flow(pay_type,voucher_no,trans_no,should_amt,tax_amt,sale_way,oper_date,settle_flag,supcust_no,pay_way,acc_date,branch_no,acc_type)
	  	values(1, @sheet_no_var, @trans_no_var,@total_amt,0,'A',@oper_date_var,'0',@branch_no_var,'A',@oper_date_var,@d_branch_no_var,'B')
		if @@error != 0 goto myerror
	     end

  	if @@error != 0 goto myerror
	
     end
 --add by wangy 2007-03-19:分公司:调入调出产生帐款 
    if @trans_no_var = 'MI' 
     begin
	 if exists (select  var_value from t_sys_branch_set where var_id='u_acc_flag' and (branch_no=left(@d_branch_no_var,3) or branch_no=left(@branch_no_var,3) )and var_value='1')
	
		insert into t_fm_recpay_flow(pay_type,voucher_no,trans_no,should_amt,tax_amt,sale_way,oper_date,settle_flag,supcust_no,pay_way,acc_date,branch_no,acc_type)
	  	values(1, @sheet_no_var, @trans_no_var,-1*@total_amt,0,'A',@oper_date_var,'0',@d_branch_no_var,'A',@oper_date_var,left(@branch_no_var,3),'B')
		if @@error != 0 goto myerror
	  
	
     end

    if @trans_no_var='MO' 
      begin
	 if exists (select  var_value from t_sys_branch_set where var_id='u_acc_flag' and (branch_no=left(@d_branch_no_var,3) or branch_no=left(@branch_no_var,3) )and var_value='1')
	
		insert into t_fm_recpay_flow(pay_type,voucher_no,trans_no,should_amt,tax_amt,sale_way,oper_date,settle_flag,supcust_no,pay_way,acc_date,branch_no,acc_type)
	  	values(1, @sheet_no_var, @trans_no_var,@total_amt,0,'A',@oper_date_var,'0',@branch_no_var,'A',@pay_date,left(@d_branch_no_var,3),'B')
		if @@error != 0 goto myerror
	  
	
     end

myerror:
  close ic_cur_1
  deallocate ic_cur_1
  return
end

go

