select branch_no,item_no,price,base_price,sale_price,vip_price,com_flag from #SDB..t_pc_branch_price where 1=2
##
if (#TV = 'hbyjtv6' or #TV = 'hbyjtv7' or #TV = 'hbyjtv8' or #TV = 'hbposv5')
	exec('insert into t_pc_branch_price (branch_no,item_no,price,base_price,sale_price,vip_price,com_flag) 
		  Select b.branch_no,a.item_no,a.price,a.base_price,a.sale_price,a.vip_price,
		  ''0'' as com_flag  From #TDB..t_bd_branch_info b,#TDB..t_bd_item_info a 
		  Where  (select sys_var_value from t_sys_system where sys_var_id = ''g_branch_no'') = ''00'' and
				 b.property in (''0'',''1'') and 
				not exists (select top 1 1 from t_pc_branch_price c where c.branch_no = b.branch_no and c.item_no = a.item_no) ')

else if (#TV = 'hbposv6' or #TV = 'hbposv7' or #TV = 'hbposv8')
		exec('insert into t_pc_branch_price (branch_no,item_no,supcust_no,sale_way,price, base_price,sale_price,vip_price, sup_ly_rate,branch_price,branch_sale,com_flag,item_stock) 
		  Select b.branch_no,item_no,main_supcust,item_sup_flag,price,base_price,sale_price,vip_price,sup_ly_rate,a.branch_price,''1'',''0'',a.item_stock
		  From #TDB..t_bd_branch_info b,#TDB..t_bd_item_info a 
		  Where  (select sys_var_value from t_sys_system where sys_var_id = ''g_branch_no'') = ''00'' and
				b.property in (''0'',''1'') and 
				not exists (select top 1 1 from t_pc_branch_price c where c.branch_no = b.branch_no and c.item_no = a.item_no) ')
else 
	exec('insert into t_pc_branch_price (branch_no,item_no,supcust_no,sale_way,price, base_price,sale_price,vip_price, sup_ly_rate,branch_price,branch_sale,com_flag,item_stock,direct,status)
		  Select b.branch_no,item_no,main_supcust,item_sup_flag,price,base_price,sale_price,vip_price,sup_ly_rate,a.branch_price,''1'',''0'',a.item_stock,a.direct,a.status
		  From #TDB..t_bd_branch_info b,#TDB..t_bd_item_info a 
		  Where  (select sys_var_value from t_sys_system where sys_var_id = ''g_branch_no'') = ''00'' and
				 b.property in (''0'',''1'') and 
				not exists (select top 1 1 from t_pc_branch_price c where c.branch_no = b.branch_no and c.item_no = a.item_no) ')
GO



if exists(select sys_var_value from t_sys_system where sys_var_id = 'g_branch_no' and sys_var_value <> '00')
begin
	declare @sheet_no int,@s_date varchar(8),@d_date datetime,@branch_no varchar(20),@sheet_id varchar(2),@v_no varchar(20)
	
	select @sheet_id = 'PX'
	select @branch_no = sys_var_value from t_sys_system where sys_var_id = 'g_branch_no'
	select @sheet_no=(isnull(sheet_value,0) + 1) 	
		from t_sys_sheet_no	where Lower(sheet_id) = Lower(@sheet_id)
	if @sheet_no>9999  
	   set   @sheet_no =0	 
	update t_sys_sheet_no set sheet_value= @sheet_no where Lower(sheet_id) = Lower(@sheet_id)
	select @v_no = '0000'+convert(char,@sheet_no)
	select @v_no = right(rtrim(@v_no),4)
	select @s_date=convert(char(8),getdate(),112)
	select @v_no = upper(@sheet_id) + @branch_no + right(@s_date,6) + @v_no

	Insert Into t_pc_price_flow_master(sheet_no,branch_no,trans_no,oper_date,oper_id,confirm_man,work_date,valid_date,other1,week)
		Values(@v_no,@branch_no,'PX',getdate(),'1001','1001',getdate(),getdate(),'数据转入','1111111');

	INSERT INTO t_pc_price_flow_detail(sheet_no,item_no,price_type,start_date,end_date,week,new_price,new_price1,new_price2,new_price3,new_price4)
	Select @v_no,b.item_no,'1',getdate(),DATEADD(day,30,getdate()),'1111111',b.price,b.sale_price,b.vip_price,a.sup_ly_rate,b.price From 
	#SDB..t_pc_branch_price b,#SDB..t_bd_item_info a Where a.item_no = b.item_no and b.branch_no = @branch_no

	if exists(select sheet_no from t_pc_price_flow_master where sheet_no = @v_no )
	update t_pc_price_flow_master set approve_flag = '1' where sheet_no = @v_no
	
	select @sheet_id = 'LK'
	select @branch_no = sys_var_value from t_sys_system where sys_var_id = 'g_branch_no'
	select @sheet_no=(isnull(sheet_value,0) + 1) 	
		from t_sys_sheet_no	where Lower(sheet_id) = Lower(@sheet_id)
	if @sheet_no>9999  
	   set   @sheet_no =0	 
	update t_sys_sheet_no set sheet_value= @sheet_no where Lower(sheet_id) = Lower(@sheet_id)
	select @v_no = '0000'+convert(char,@sheet_no)
	select @v_no = right(rtrim(@v_no),4)
	select @s_date=convert(char(8),getdate(),112)
	select @v_no = upper(@sheet_id) + @branch_no + right(@s_date,6) + @v_no

		if (#TV = 'hbyjtv6' or #TV = 'hbyjtv7' or #TV = 'hbyjtv8')
		exec('Insert Into t_pc_price_flow_master(sheet_no,branch_no,trans_no,oper_date,oper_id,confirm_man,work_date,valid_date,other1,week)
			  Values(''' + @v_no +''',''' +@branch_no +''',''LK'',getdate(),''1001'',''1001'',getdate(),getdate(),''数据转入'',''1111111'')')
	else
		exec('Insert Into t_pc_price_flow_master(sheet_no,branch_no,trans_no,oper_date,oper_id,confirm_man,work_date,valid_date,other1,week,chgtype)
	         Values(''' + @v_no +''',''' +@branch_no +''',''LK'',getdate(),''1001'',''1001'',getdate(),getdate(),''数据转入'',''1111111'',''100000'')')

	INSERT INTO t_pc_price_flow_detail(sheet_no,item_no,price_type,start_date,end_date,week,new_price)
	Select @v_no,b.item_no,'1',getdate(),DATEADD(day,30,getdate()),'1111111',b.base_price From #SDB..t_pc_branch_price b,#SDB..t_bd_item_info a 
	Where a.item_no = b.item_no and b.branch_no = @branch_no

	if exists(select sheet_no from t_pc_price_flow_master where sheet_no = @v_no )
	update t_pc_price_flow_master set approve_flag = '1' where sheet_no = @v_no
end 
GO