IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_current_ware]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_current_ware]
GO
CREATE procedure [dbo].[mps_g_current_ware](
@cmpid int,
@branchid int,
@userdpid int,
@userotid int ,
@useruid int ,
@whid varchar(8000),
@showbatch int ,
@key nvarchar(100),
@pagesize int , 
@pagecount int , 
@pageobjids varchar(8000)
)
as 
begin
set nocount on
declare @s1 varchar(8000) 
declare @s2 varchar(100) 
declare @i int 

declare @whids table(whid int) 

set @s1 = @whid  
while (@s1>'')  
begin 
  set @i = charindex(',', @s1) 
  if (@i>0) 
  begin 
    set @s2 = substring(@s1,1,@i-1) 
    set @s1 = substring(@s1,@i+1,len(@s1)-@i) 
  end else  
  begin 
    set @s2 = @s1 
    set @s1 = ''  
  end  
  insert into @whids(whid) values(convert(int,@s2)) 
end 

if (not exists (select 1 from @whids)) 
begin
insert into @whids(whid) 
select wh_id from mps_warehouse where wh_cmpid=@cmpid 
end 

declare @amount int 
select @amount = @pagecount*@pagesize 
declare @u table(uid int identity(1, 1),whid int, code varchar(50),tag int,prdid int , unit nvarchar(50) , batch varchar(100), amount decimal(18,4), price decimal(18,4),fee decimal(18,4)) 

declare @whcount int 
select @whcount=COUNT(1) from @whids 
 
if (@showbatch=1)
begin
if (@key='') 
insert into @u(whid,code,tag,prdid,unit,amount,price,fee,batch ) 
select case when @whcount=1 then whg_whid else 0 end whg_whid,prd_code,whg_tag,whg_prdid , whg_unit , SUM(whg_amount) , SUM(whg_fee)/SUM(whg_amount) ,SUM(whg_fee),''
from mps_warehouse_data ,mps_prds , @whids 
where whg_whid=whid and whg_prdid=prd_id and whg_tag=prd_tag  
group by case when @whcount=1 then whg_whid else 0 end,prd_code,whg_tag,whg_prdid , whg_unit
having SUM(whg_amount)>0 
else 
insert into @u(whid,code,tag,prdid,unit,amount,price,fee,batch) 
select  case when @whcount=1 then whg_whid else 0 end whg_whid,prd_code,whg_tag,whg_prdid , whg_unit , SUM(whg_amount) , SUM(whg_fee)/SUM(whg_amount) ,SUM(whg_fee),'' 
from mps_warehouse_data ,mps_prds, @whids 
where whg_whid=whid and whg_prdid=prd_id and whg_tag=prd_tag and 
(prd_code like @key+'%' or prd_title like '%'+@key+'%' or prd_model like '%'+@key+'%' or
prd_spec like '%'+@key+'%')  
group by case when @whcount=1 then whg_whid else 0 end,prd_code,whg_tag,whg_prdid , whg_unit
having SUM(whg_amount)>0 
end else
begin
if (@key='') 
insert into @u(whid,code,tag,prdid,unit,amount,price,fee,batch) 
select case when @whcount=1 then whg_whid else 0 end whg_whid,prd_code,whg_tag,whg_prdid , whg_unit , SUM(whg_amount) , SUM(whg_fee)/SUM(whg_amount) ,SUM(whg_fee),whg_batch
from mps_warehouse_data ,mps_prds, @whids 
where whg_whid=whid and whg_prdid=prd_id and whg_tag=prd_tag 
group by case when @whcount=1 then whg_whid else 0 end,prd_code,whg_tag,whg_prdid , whg_unit,whg_batch
having SUM(whg_amount)>0 
else 
insert into @u(whid,code,tag,prdid,unit,amount,price,fee,batch) 
select  case when @whcount=1 then whg_whid else 0 end whg_whid,prd_code,whg_tag,whg_prdid , whg_unit , SUM(whg_amount) , SUM(whg_fee)/SUM(whg_amount) ,SUM(whg_fee),whg_batch
from mps_warehouse_data ,mps_prds, @whids 
where whg_whid=whid and whg_prdid=prd_id and whg_tag=prd_tag and 
(prd_code like  @key+'%' or prd_title like '%'+@key+'%' or prd_model like '%'+@key+'%' or
prd_spec like '%'+@key+'%')  
group by case when @whcount=1 then whg_whid else 0 end  ,prd_code,whg_tag,whg_prdid , whg_unit,whg_batch
having SUM(whg_amount)>0 
end 

declare   @pobjs table(nid int identity(1, 1), ordid bigint) 
set @s1 = @pageobjids  
while (@s1>'')  
begin 
  set @i = charindex(',', @s1) 
  if (@i>0) 
  begin 
    set @s2 = substring(@s1,1,@i-1) 
    set @s1 = substring(@s1,@i+1,len(@s1)-@i) 
  end else  
  begin 
    set @s2 = @s1 
    set @s1 = ''  
  end  
  insert into @pobjs(ordid) values(convert(bigint,@s2)) 
end 
if (exists (select top 1 * from @pobjs)) 
begin 
select a.*,b.*, case when whid=0 then '' else '查看明细' end whg_details from mps_prds b , @pobjs , @u a 
where  uid=ordid and   tag=prd_tag and prdid=prd_id order by nid  
return 
end 
-- 返回总量
select count(1) cnt from @u 


-- 返回所有ID


select uid from @u order by uid  

-- 返回第一页
select a.*,b.*, case when whid=0 then '' else '查看明细' end whg_details from @u a,mps_prds b  
where uid<=@pagesize and tag=prd_tag and prdid=prd_id 
order by uid 

set nocount off 

end