
declare @item_id int,@branch_id int,@branch_no varchar(50),@sup_id int,@item_subno varchar(50),@in_price numeric(14,4)
declare @sale_way varchar(1),@unit_no varchar(4),@ly_deduct_flag bit,@ly_deduct numeric(5,4),@ctrlstock_flag bit,@ctrlprice_flag bit,@def_sup_flag tinyint


select @branch_no=sys_var_value from sys_t_system where sys_var_id='g_branch_no'

select @branch_id=branch_id from bi_t_branch_info where branch_no=@branch_no

--备份门店供货资格和供货价
select * into bi_t_item_sup_bak20240101 from bi_t_item_sup where branch_id=@branch_id
select * into bi_t_item_sup_price_bak20240101 from bi_t_item_sup_price where branch_id=@branch_id or branch_id=0

declare cur_item_sup cursor for
select item_id, sup_id, branch_id, sale_way, in_price, ly_deduct_flag, ly_deduct, ctrlstock_flag, ctrlprice_flag, def_sup_flag, '', ''
from bi_t_item_sup_bak20240101
where branch_id=@branch_id
open cur_item_sup
fetch cur_item_sup into @item_id, @sup_id, @branch_id, @sale_way, @in_price, @ly_deduct_flag, @ly_deduct, @ctrlstock_flag, @ctrlprice_flag, @def_sup_flag, @item_subno, @unit_no
while @@FETCH_STATUS = 0
begin
	if @@FETCH_STATUS <> 0 break
		insert into cm_t_cmdata (com_data, update_date, up_flag, branch_no, down_flag, d_branch_no) 
		select 'if not exists(select 1 from bi_t_item_sup where '+ ' item_id= ' +'0' +rtrim(convert(char,isnull(@item_id,0)))+' and sup_id= ' +'0' +rtrim(convert(char,isnull(@sup_id,0)))+' and branch_id= ' +'0' +rtrim(convert(char,isnull(@branch_id,0))) + ') '
				+ ' begin '
				+ ' insert into bi_t_item_sup(def_sup_flag,item_id,sup_id,branch_id,item_subno,sale_way,unit_no,in_price,ly_deduct_flag,ly_deduct,ctrlstock_flag,ctrlprice_flag) values ('
				+ '0'+rtrim(convert(char,isnull(@def_sup_flag,0)))+','+ '0'+rtrim(convert(char,isnull(@item_id,0)))+','+ '0'+rtrim(convert(char,isnull(@sup_id,0)))+','+ '0'+rtrim(convert(char,isnull(@branch_id,0)))
				+ ','+ char(39)+rtrim(isnull(@item_subno,''))+char(39)+','+ char(39)+rtrim(isnull(@sale_way,''))+char(39)+','+ char(39)+rtrim(isnull(@unit_no,''))+char(39)+','+ '0'+rtrim(convert(char,isnull(@in_price,0)))
				+ ','+ '0'+rtrim(convert(char,isnull(@ly_deduct_flag,0)))+','+ '0'+rtrim(convert(char,isnull(@ly_deduct,0)))+','+ '0'+rtrim(convert(char,isnull(@ctrlstock_flag,0)))+','+ '0'+rtrim(convert(char,isnull(@ctrlprice_flag,0)))+') ' 
				+'  end', getdate(), 1, null, 0, null
	fetch cur_item_sup into @item_id, @sup_id, @branch_id, @sale_way, @in_price, @ly_deduct_flag, @ly_deduct, @ctrlstock_flag, @ctrlprice_flag, @def_sup_flag, @item_subno, @unit_no
end
close cur_item_sup
deallocate cur_item_sup

declare cur_item_sup_price cursor for
select item_id,branch_id,sup_id,item_subno,in_price 
from bi_t_item_sup_price_bak20240101
where branch_id=@branch_id --or branch_id=0
open cur_item_sup_price
fetch cur_item_sup_price into @item_id,@branch_id,@sup_id,@item_subno,@in_price 
while @@FETCH_STATUS = 0
begin
	if @@FETCH_STATUS <> 0 break
	delete from bi_t_item_sup_price where item_id=@item_id and branch_id=@branch_id and sup_id=@sup_id and item_subno=@item_subno
	INSERT INTO bi_t_item_sup_price(item_id,branch_id,sup_id,item_subno,in_price)
	values(@item_id,@branch_id,@sup_id,@item_subno,@in_price)
	fetch cur_item_sup_price into @item_id,@branch_id,@sup_id,@item_subno,@in_price 
end
close cur_item_sup_price
deallocate cur_item_sup_price
--select * from cm_t_cmdata where update_date  > '2024-01-26'

