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

create trigger tr_bd_i_item_info on t_bd_item_info
for insert
as
begin
	
declare @branch_sale char(1)
select @branch_sale = '1'
	--检查是否是分部式版，如果不是则返回
        if not (select sys_var_value from t_sys_system where lower(sys_var_id)='is_distributed') = '1'
        begin
             if (select sys_var_value from t_sys_system where lower(sys_var_id)='is_distributed') = '0'
	     begin
	     	if (select sys_var_value from t_sys_system where lower(sys_var_id)='sys_branch_price') = '3'--总分部商品属性完全不同
		begin
		  insert 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,base_price1,base_price2,base_price3,base_price4,direct,status,item_stock)
		  select b.branch_no ,a.item_no,a.main_supcust,a.item_sup_flag,a.price,a.base_price,a.sale_price,
		       a.vip_price,a.sup_ly_rate,a.branch_price,'1',a.base_price1,a.base_price2,a.base_price3,a.base_price4,
		       isnull(a.direct,'2'),isnull(a.status,'0'),a.item_stock
	            from inserted a,  t_bd_branch_info b 
		    where b.property ='1'
		    and   not exists (select 1 from t_pc_branch_price c where c.branch_no = b.branch_no and c.item_no = a.item_no)

	       	  insert 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,base_price1,base_price2,base_price3,base_price4,direct,status,item_stock)
		  select b.branch_no ,a.item_no,a.main_supcust,a.item_sup_flag,a.price,a.base_price,a.sale_price,
		       a.vip_price,a.sup_ly_rate,a.branch_price,@branch_sale,a.base_price1,a.base_price2,a.base_price3,a.base_price4,
		       isnull(a.direct,'2'),isnull(a.status,'0'),a.item_stock
	            from inserted a,  t_bd_branch_info b 
		    where b.property ='0'
		    and   not exists (select 1 from t_pc_branch_price c where c.branch_no = b.branch_no and c.item_no = a.item_no)
		end
	     	else  --只售价体系不同
		begin
		  insert 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,direct,status,item_stock)
		  select b.branch_no ,a.item_no,a.main_supcust,a.item_sup_flag,a.price,a.base_price,a.sale_price,
		       a.vip_price,a.sup_ly_rate,a.branch_price,'1',isnull(a.direct,'2'),isnull(a.status,'0'),a.item_stock
	            from inserted a,  t_bd_branch_info b 
		    where b.property ='1'
		    and   not exists (select 1 from t_pc_branch_price c where c.branch_no = b.branch_no and c.item_no = a.item_no)

		  insert 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,direct,status,item_stock)
		  select b.branch_no ,a.item_no,a.main_supcust,a.item_sup_flag,a.price,a.base_price,a.sale_price,
		       a.vip_price,a.sup_ly_rate,a.branch_price,@branch_sale,isnull(a.direct,'2'),isnull(a.status,'0'),a.item_stock
	            from inserted a,  t_bd_branch_info b 
		    where b.property ='0'
		    and   not exists (select 1 from t_pc_branch_price c where c.branch_no = b.branch_no and c.item_no = a.item_no)
		end
	     
end
	     --Modified by Dengzw 2014.10.10 还有后续代码需要执行,此处不需要返回。
	     --return
        end

	--声明变量
--	declare @key1 varchar(30)
	declare @item_no varchar(20),@main_supcust varchar(20),@sales_way varchar(10),@branch_no varchar(10)
	
	--系统默认的仓库
	select @branch_no=sys_var_value from t_sys_system where sys_var_id='g_branch_no'
	select @branch_no = @branch_no + '01'
	
	--插入
	declare insert_item_cur cursor for
		select  item_no,main_supcust,item_sup_flag from inserted
		for read only
	open insert_item_cur
	fetch insert_item_cur into @item_no,@main_supcust,@sales_way
	while @@fetch_status=0
	begin
		if not exists(select item_no from t_im_branch_stock where branch_no = @branch_no and item_no = @item_no)
			insert t_im_branch_stock (item_no,branch_no,oper_date) values (@item_no, @branch_no,getdate())

		insert into t_sys_cm_data (com_table, change_type, key1, update_date, branch_no)
			values ('t_bd_item_info', 'I', @item_no, getdate(), null)
		
		--modified by wujs 2005-12-23
                --if not exists (select item_no from t_bd_supcust_item where item_no = @item_no and supcust_no = @main_supcust)
		--     insert t_bd_supcust_item (item_no,supcust_no,sale_way,contract_date)
		--	    values (@item_no,@main_supcust,@sales_way,getdate())

		fetch insert_item_cur into @item_no,@main_supcust,@sales_way
	end
	close insert_item_cur
	deallocate insert_item_cur


     --写商品供应商关系表 modified by wujs 2005-12-23
	 insert t_bd_supcust_item (branch_no,item_no,supcust_no,sale_way,contract_date)
         select b.branch_no ,a.item_no, a.main_supcust,a.item_sup_flag,getdate()
           from inserted a, t_bd_branch_info b
         where (b.property = '1' or (b.property = '0' and b.trade_type in ('1','3')) )
           and not exists (select 1 from t_bd_supcust_item c 
                              where c.branch_no = b.branch_no 
                                and c.item_no = a.item_no 
                                and c.supcust_no = a.main_supcust )
	--生鲜写称表20200627
	insert into t_pc_item_balance(item_no,sale_price,flag,oper_date)
	select item_no,sale_price,'0',getdate()
	from inserted
	where len(item_no)=5 and (num3=1 or num3=2)

--190725-841637 连锁分店不写t_pc_branch_price数据 yanghc 2019-08-12                                
if (select sys_var_value from t_sys_system where lower(sys_var_id)='is_distributed') <> '2' 
begin
        --写机构商品信息表 modified by wujs 2005-12-23
	if (select sys_var_value from t_sys_system where lower(sys_var_id)='sys_branch_price') = '3'--总分部商品属性完全不同
	     begin
		  insert 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,base_price1,base_price2,base_price3,base_price4,direct,status,item_stock)
		  select b.branch_no ,a.item_no,a.main_supcust,a.item_sup_flag,(case trade_type when '2' then a.trans_price else a.price end),a.base_price,a.sale_price,
		       a.vip_price,a.sup_ly_rate,a.branch_price,@branch_sale,a.base_price1,a.base_price2,a.base_price3,a.base_price4,
		       isnull(a.direct,'2'),isnull(a.status,'0'),a.item_stock
	            from inserted a,  t_bd_branch_info b 
		    where b.property ='1'
		    and   not exists (select 1 from t_pc_branch_price c where c.branch_no = b.branch_no and c.item_no = a.item_no)

	       	  insert 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,base_price1,base_price2,base_price3,base_price4,direct,status,item_stock)
		  select b.branch_no ,a.item_no,a.main_supcust,a.item_sup_flag,(case trade_type when '2' then a.trans_price else a.price end),a.base_price,a.sale_price,
		       a.vip_price,a.sup_ly_rate,a.branch_price,@branch_sale,a.base_price1,a.base_price2,a.base_price3,a.base_price4,
		       isnull(a.direct,'2'),isnull(a.status,'0'),a.item_stock
	            from inserted a,  t_bd_branch_info b 
		    where b.property ='0'
		    and   not exists (select 1 from t_pc_branch_price c where c.branch_no = b.branch_no and c.item_no = a.item_no)
	     end
	else  --只售价体系不同
	    begin
		  insert 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,direct,status,item_stock)
		  select b.branch_no ,a.item_no,a.main_supcust,a.item_sup_flag,(case trade_type when '2' then a.trans_price else a.price end),a.base_price,a.sale_price,
		       a.vip_price,a.sup_ly_rate,a.branch_price,@branch_sale,isnull(a.direct,'2'),isnull(a.status,'0'),a.item_stock
	            from inserted a,  t_bd_branch_info b 
		    where b.property ='1'
		    and   not exists (select 1 from t_pc_branch_price c where c.branch_no = b.branch_no and c.item_no = a.item_no)

		  insert 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,direct,status,item_stock)
		  select b.branch_no ,a.item_no,a.main_supcust,a.item_sup_flag,(case trade_type when '2' then a.trans_price else a.price end),a.base_price,a.sale_price,
		       a.vip_price,a.sup_ly_rate,a.branch_price,@branch_sale,isnull(a.direct,'2'),isnull(a.status,'0'),a.item_stock
	            from inserted a,  t_bd_branch_info b 
		    where b.property ='0'
		    and   not exists (select 1 from t_pc_branch_price c where c.branch_no = b.branch_no and c.item_no = a.item_no)
	end
end

	--begin yejh 20150210 add 增加科组
	IF (select sys_var_value from t_sys_system where lower(sys_var_id)='is_distributed') = '1'
		OR (select sys_var_value from t_sys_system where lower(sys_var_id)='is_distributed') = '0'
	BEGIN	
		update t_pc_branch_price
		set t_pc_branch_price.dept_clsno=a.dept_clsno,t_pc_branch_price.com_flag='0'
		from inserted a,  t_pc_branch_price
					where t_pc_branch_price.item_no = a.item_no
    END
    --end yejh 20150210 add 增加科组
	--总部不应受SKU数的限制 --yejh 20140529
	--if (select sys_var_value from t_sys_system where lower(sys_var_id) = 'itemcls_sku_set') = '1'
	if (select sys_var_value from t_sys_system where lower(sys_var_id) = 'itemcls_sku_set') = '1' --and left(@branch_no,2) <> '00' --yejh 20140529
	begin
		--add by wangjian at 20110418 配置了SKU数之后新增的商品默认branch_sale为0
		
		update t_pc_branch_price set branch_sale = '0' from 
		inserted a,t_bd_branchcls_sku b where a.item_no = t_pc_branch_price.item_no and 
		t_pc_branch_price.branch_no = b.branch_no and left(a.item_clsno,(select cast((select sys_var_value from t_sys_system where sys_var_id = 'gl_itm_cls_nolen') as int))) = b.item_clsno
		and b.sku > 0  and b.branch_no <> '00'  --yejh 20140529
		
		update t_bd_item_info set branch_sale = '0'
		from inserted a,t_bd_branchcls_sku b
		where t_bd_item_info.item_no = a.item_no and
		a.item_clsno = b.item_clsno and
		b.branch_no = left(@branch_no,2) and
		b.sku > 0 and b.branch_no <> '00'  --yejh 20140529

	end


end
go