create proc REP_TAGBSNR1 @evt varchar(30) , @bsnr varchar(20)  , @type varchar(20)
as
begin
  if @evt='ver' begin select 'v1.1' status ; return end;
  declare @cnr varchar(30), @tag varchar(100), @xnote varchar(500), @bs char(1) , @mark varchar(20)
  select @bsnr = upper(rtrim(ltrim(@bsnr))),@type = upper(rtrim(ltrim(@type)))
  select @cnr = mem from auctmark where evt = @evt  and mark = @bsnr
  select @bs = case when exists(select marksell from auct where eventnr = @evt and marksell = @bsnr) then 'S' else 'B' end
  create table #T(ID int identity(1,1) primary key, SRC varchar(20), BS varchar(20),NR varchar(20), SNR varchar(20), BNR varchar(20), TAG varchar(100), XNOTE varchar(500), TID int )  
  ------
  
  if @bs = 'B'  ------------------- BUYER DEPARTURES
  begin
    insert into #T(SRC,BS,NR,SNR,BNR,TAG,XNOTE,TID)
      select 'DEPARTURE','BUYER',@bsnr,'', @bsnr,TAG,'T='+isnull(TIMESTAMP,'')+';D='+convert(varchar(100),isnull(XDEVICE,0))+';B='+convert(varchar(100),isnull(XBATCH,0)),ID
      from ATAGSCAN where EVT = @evt and BSNR = @bsnr  order by ID
  end


  if @bs = 'S' --------------------- SELLER ARRIVALS
  begin
    insert into #T(SRC,BS,NR,SNR,BNR,TAG,XNOTE,TID)
      select 'ARRIVAL','SELLER',@bsnr, @bsnr , '' ,TAG,'T='+isnull(TIMESTAMP,'')+';D='+convert(varchar(100),isnull(XDEVICE,0))+';B='+convert(varchar(100),isnull(XBATCH,0)),ID
      from ATAGSCAN where EVT = @evt and BSNR = @bsnr order by ID
  end
  
  
  if @bs = 'S'  -------------------- SELLER BUYER-DEPARTURES
  begin
    DECLARE c1 CURSOR FOR
    SELECT distinct MARKBUY from AUCT where EventNR = @evt and marksell = @bsnr and isnull(markbuy,'') <> ''  and membuy <> @cnr 
    OPEN c1 
    FETCH NEXT FROM c1 INTO @mark
    WHILE @@FETCH_STATUS = 0 
    BEGIN
       insert into #T(SRC,BS,NR,SNR,BNR,TAG,XNOTE,TID)
         select 'DEPARTURE','BUYER',@mark,@bsnr, @mark,TAG,'T='+isnull(TIMESTAMP,'')+';D='+convert(varchar(100),isnull(XDEVICE,0))+';B='+convert(varchar(100),isnull(XBATCH,0)),ID
         from ATAGSCAN where EVT = @evt and BSNR = @mark and TAG in (select TAG from ATAGSCAN where EVT=@evt and BSNR = @bsnr) order by ID
       FETCH NEXT FROM c1  INTO @mark
    END
    CLOSE c1 
    DEALLOCATE c1 
  end
  
  if @bs = 'S'   ------------------   SELLER DEPARTURES UNSOLD/BSNR
  begin
    DECLARE c2 CURSOR FOR SELECT distinct MARKBUY from AUCT where EventNR = @evt and marksell = @bsnr and isnull(markbuy,'') <> '' and membuy = @cnr 
    OPEN c2
    FETCH NEXT FROM c2 INTO @mark
    WHILE @@FETCH_STATUS = 0
    BEGIN
      insert into #T(SRC,BS,NR,SNR,BNR,TAG,XNOTE,TID)
         select 'DEPARTURE','SELLER UNSOLD/BSNR',@mark, @bsnr, @mark ,TAG,'T='+isnull(TIMESTAMP,'')+';D='+convert(varchar(100),isnull(XDEVICE,0))+';B='+convert(varchar(100),isnull(XBATCH,0)),ID
         from ATAGSCAN where EVT = @evt and BSNR = @mark order by ID
      FETCH NEXT FROM c2 INTO @mark
    END
    CLOSE c2
    DEALLOCATE c2
  end
  
  if @bs = 'S'  ------------------   SELLER DEPARTURES UNSOLD/NOTSOLD
    insert into #T(SRC,BS,NR,SNR,BNR,TAG,XNOTE,TID)
       select 'DEPARTURE','SELLER UNSOLD/NS',@bsnr, @bsnr,'' ,TAG,'T='+isnull(TIMESTAMP,'')+';D='+convert(varchar(100),isnull(XDEVICE,0))+';B='+convert(varchar(100),isnull(XBATCH,0)),ID
       from ATAGSCAN where EVT = @evt and BSNR = @bsnr
       and TAG not in (select TAG from #T where ISNULL(BNR,'') <> '' )  order by ID
       

   --------
  if @type = 'OWNLIST' --own listing
  begin
    if @BS = 'S'
	  select * from #T where left(BS,1) = 'S'  order by SRC,BS desc, ID
	else
      select * from #T   order by SRC,BS desc, ID
  end
  else
  if @type = 'OWNGROUP' -- own summary
  begin
    if @BS = 'S'
      select SRC,BS,NR, TA=sum(case when left(src,1) ='A' then 1 else 0 end), TD = sum(case when left(src,1) ='D' then 1 else 0 end)  from #T  where left(BS,1) = 'S'  group by SRC,BS,NR   order by SRC,BS desc
	else
      select SRC,BS,NR, TA=sum(case when left(src,1) ='A' then 1 else 0 end), TD = sum(case when left(src,1) ='D' then 1 else 0 end)
	    from #T group by SRC,BS,NR   order by SRC,BS desc
  end
  else
  if @type = 'ALLLIST' -- all listing
  begin
      select * from #T   order by SRC,BS desc, ID
  end
  else
  if @type = 'ALLGROUP'  -- all summary
  begin
      select SRC,BS,NR, TA=sum(case when left(src,1) ='A' then 1 else 0 end), TD = sum(case when left(src,1) ='D' then 1 else 0 end)
	    from #T group by SRC,BS,NR   order by SRC,BS desc
  end
  else 
    select 'ERROR' + @type status
end
