select '' as voucher_no from sysobjects with(nolock) where 1=2
##
	
if (#SV = 'kmjxc_pro' or #SV = 'kmjxc_pro')
	exec('insert into t_im_flow(voucher_no,oper_date, item_no,real_qty,price,sheet_amt,branch_no,d_branch_no,trans_no,db_no,supcust_no,sale_way,oper_id)
		   select ''PD'',getdate(),a.item_no,isnull(abs(a.stock_qty),0),isnull(a.cost_price,0), isnull(abs(a.stock_qty),0)*isnull(a.cost_price,0),''0001'',
				  '''',''PD'',case when a.stock_qty<0 then ''-'' else ''+'' end, b.sup_no,''A'',''1001'' 
		   from #SDB..ic_t_branch_stock a, kmjxc_pro..bi_t_item_info b where a.item_no=b.item_no and a.branch_no = ''00'' ')
else if #SV = 'bw9kbzpro'
	exec('insert into t_im_flow(voucher_no,oper_date, item_no,real_qty,price,sheet_amt,
			branch_no,trans_no,db_no,supcust_no,sale_way,oper_id) 
			select ''PD'',getdate(),b.barcode,a.stock_qty,isnull(a.cost_price,b.price), a.stock_qty*isnull(a.cost_price,b.price), 
			a.branch_no,''PD'',''+'', b.sup_no,''A'',''1001''  from  #SDB..ic_t_branch_stock a,#SDB..bi_t_item_info b
			where a.item_no=b.item_no and a.stock_qty>0  and b.barcode not in (select barcode from #SDB..bi_t_item_info  group by barcode having count(*)>1)')
else if #SV = 'tiger'
	exec('insert into t_im_branch_stock(item_no, branch_no, stock_qty,fifo_cost,avg_cost,last_inprice)
            SELECT item_no,(case len(branch_no) when 2 then rtrim(branch_no)+''01'' else branch_no end),stock_qty,cost_price,cost_price,cost_price
            FROM #SDB..wh_stock')
else
	begin
		exec('insert into t_im_flow (voucher_no,oper_date,item_no,real_qty,price,sheet_amt,branch_no,d_branch_no,trans_no,db_no,supcust_no,sale_way,oper_id)
			  select ''PD''  as voucher_no ,getdate() as oper_date,a.item_no,isnull(abs(a.stock_qty),0) as real_qty,isnull(a.avg_cost,0) as price, isnull(abs(a.stock_qty),0)*isnull(a.avg_cost,0) as sheet_amt,a.branch_no as branch_no,'' as d_branch_no,''PD'' as trans_no,
				  case when  a.stock_qty<0 then ''-'' else ''+'' end as db_no, b.main_supcust as supcust_no,''A'' as sale_way,''1001'' as oper_id
			from #sdb..t_im_branch_stock a, #sdb..t_bd_item_info b where a.item_no=b.item_no and a.stock_qty <> 0 and a.branch_no = (select sys_var_value from t_sys_system where sys_var_id = ''g_stock_no'')')		  
	end

GO
if #TV = 'hbposev9'
	exec('update t_im_flow set com_flag = ''1'',sup_ly_rate=b.sup_ly_rate,sup_ly_paid = ''1'' from 
          t_im_flow a,t_bd_item_info b where a.item_no = b.item_no')
go


