if exists(select 1 from sysobjects where name='pr_pub_prom_branch')
  drop procedure pr_pub_prom_branch
go


/*---------------------------------------------------------------------------------------
  @loginBranch：登录机构
  @refBranch：其他门店相关约束：传入值：
   1.为ALL时，返回所有门店列表
   2.为某机构时（如001)：返回其下属所有分店列表
   3.为具体门店时（如0000002）返回此门店
   4.为区域编码时（部分产品支持按区域建促销方案）：返回此区域下的分店列表
  @operId:操作员
------------------------------------------------------------------------------------------*/
create proc pr_pub_prom_branch(@loginBranch varchar(10),@refBranch varchar(10),@operId varchar(10))
as
begin
	declare @area_manager char(1)
	declare @group_id varchar(10)
	declare @branch_tb table
	(
		branch_no varchar(10) not null,
		branch_name varchar(30) null,
		cls_flag char(1) null --是否为区域 1：是：其他：否
	)
	if upper(@refBranch) ='ALL'
	begin
		if @loginBranch = '00'  --总部
		begin
			insert into @branch_tb select 'ALL','所有分店','0'
			insert into @branch_tb
			SELECT t_bd_branch_info.branch_no,t_bd_branch_info.branch_name,'0'
			FROM t_bd_branch_info  
			WHERE (t_bd_branch_info.property = '0' OR t_bd_branch_info.property = '1' ) and 
			(not (t_bd_branch_info.property = '0' and trade_type = '3' )) and
			t_bd_branch_info.dj_yw <> '1'
		end
		else  --分店
		begin
			insert into @branch_tb
			select branch_no,branch_name,'0' from t_bd_branch_info where branch_no = @loginBranch
		end
	end
	else
	begin
	if @loginBranch = '00'  --总部
		begin
			insert into @branch_tb select 'ALL','所有分店','0'
			insert into @branch_tb
			SELECT t_bd_branch_info.branch_no,t_bd_branch_info.branch_name,'0'
			FROM t_bd_branch_info  
			WHERE (t_bd_branch_info.property = '0' OR t_bd_branch_info.property = '1' ) and 
			(not (t_bd_branch_info.property = '0' and trade_type = '3' )) and
			t_bd_branch_info.dj_yw <> '1'
		end
		else  --分店
		begin
			insert into @branch_tb
			select branch_no,branch_name,'0' from t_bd_branch_info where branch_no = @loginBranch
		end
	end

	select branch_no,branch_name,cls_flag from @branch_tb
end
go
