IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_warehouse_operation_insert_1]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_warehouse_operation_insert_1]
GO
CREATE procedure [dbo].[mps_warehouse_operation_insert_1](
@whop_id bigint ,
@whop_cmpidint,
@whop_tagint,
@whop_prdidint,
@whop_whidint,
@whop_whid2int,
@whop_opdatedatetime,
@whop_operationint,
@whop_valuedecimal(18, 4),
@whop_valuesuspenddecimal(18, 4),
@whop_unitnvarchar(50),
@whop_batchnvarchar(100),
@whop_pricedecimal(18, 4),
@whop_expiredatetime,
@whop_notenvarchar(200),
@whop_locationnvarchar(200)
) as 
begin
    
    -- 这段程序只处理入库  要支持二种情况，正常入库和盘点后的入库 
    -- 正常入库：有价格，有BATCH
    -- 盘点入库：无价格，无BATCH

if (@whop_value<=0) return 
    
declare @whoutstyle int 
set @whoutstyle = null 
declare @mywhid int 
declare @whgid bigint  
declare @mindate datetime 
set @mindate = GETDATE() - 10000 
declare @baseratio int 
declare @newbatch nvarchar(100) 
declare @amount decimal(18,4) 
declare @outprice decimal(18,4) 

-- 这个程序要重新写，要支持所有的出库方法和价格计算方法 但是目前版本，不支持价格的实时计算。
-- 实际上就是要支持先进先出，先进后出 的出库方式，还要支持对batch 或 型号规格的自动匹配
-- 我建议whg_expire 与 whg_batch 统一起来用，如果有 whg_expite 那就一定要有batch 
-- 这样，系统就只会对batch进行处理了。
-- 在入库的时候，如果没有batch 只有expire 系统将自动将expire 转化为 batch 
-- 首先要对商品的最小单位进行计算 

select top 1 @baseratio = prdu_ratio from mps_prds_unit where prdu_tag=@whop_tag and prdu_prdid=@whop_prdid and prdu_unit=@whop_unit 
set @baseratio = isnull(@baseratio , 0) 
if (@baseratio<=0) set @baseratio=1 


  -- 先处理入库时，库存里没有数据的情况 
  set @mywhid = @whop_whid 
  
  -- 如果没有批次，并且有exprre 则自动自成一个批次
  set @newbatch = case when @whop_batch<>'' then @whop_batch 
    when @whop_expire>@mindate then convert(varchar(10),@whop_expire,120) 
    else '' 
   end
  -- 对于盘点入库，如果有批次管理，那么 @newtach有可能的值是：first/last/low/high/或批次号
  if (@newbatch='first') 
  begin
  select top 1 @newbatch=whg_batch  from mps_warehouse_data where whg_whid=@mywhid and whg_tag=@whop_tag and whg_prdid=@whop_prdid and whg_unit=@whop_unit 
  and whg_amount>0 and whg_batch>'' order by whg_batch 
  if (@newbatch='first' or @newbatch is null or @newbatch='')  
  select top 1 @newbatch=whg_batch  from mps_warehouse_data where whg_whid=@mywhid and whg_tag=@whop_tag and whg_prdid=@whop_prdid and whg_unit=@whop_unit and whg_batch>'' 
  order by whg_batch desc 
  end 
  if (@newbatch='last') 
  begin
  select top 1 @newbatch=whg_batch  from mps_warehouse_data where whg_whid=@mywhid and whg_tag=@whop_tag and whg_prdid=@whop_prdid and whg_unit=@whop_unit 
  and whg_amount>0 and whg_batch>'' order by whg_batch desc 
  if (@newbatch='last' or @newbatch is null or @newbatch='')  
  select top 1 @newbatch=whg_batch  from mps_warehouse_data where whg_whid=@mywhid and whg_tag=@whop_tag and whg_prdid=@whop_prdid and whg_unit=@whop_unit and whg_batch>'' 
  order by whg_batch desc 
  end
  if (@newbatch='low') 
  begin
  select top 1 @newbatch=whg_batch  from mps_warehouse_data where whg_whid=@mywhid and whg_tag=@whop_tag and whg_prdid=@whop_prdid and whg_unit=@whop_unit 
  and whg_amount>0 and whg_batch>'' and whg_outprice>0  order by whg_outprice  
  if (@newbatch='low' or @newbatch is null or @newbatch='')  
  select top 1 @newbatch=whg_batch  from mps_warehouse_data where whg_whid=@mywhid and whg_tag=@whop_tag and whg_prdid=@whop_prdid and whg_unit=@whop_unit and whg_batch>'' 
  order by whg_batch desc 
  end
  if (@newbatch='high') 
  begin
  select top 1 @newbatch=whg_batch  from mps_warehouse_data where whg_whid=@mywhid and whg_tag=@whop_tag and whg_prdid=@whop_prdid and whg_unit=@whop_unit 
  and whg_amount>0 and whg_batch>'' and whg_outprice>0  order by whg_outprice  desc 
  if (@newbatch='high' or @newbatch is null or @newbatch='')  
  select top 1 @newbatch=whg_batch  from mps_warehouse_data where whg_whid=@mywhid and whg_tag=@whop_tag and whg_prdid=@whop_prdid and whg_unit=@whop_unit and whg_batch>'' 
  order by whg_batch desc 
  end
  
  -- 如果仓库里有数据，就累加，如果没有，就增加一条
set @whgid = 0
select top 1 @whgid=whg_id,@outprice=case when whg_fee>0 and whg_amount>0 then whg_fee/whg_amount else whg_outprice end from mps_warehouse_data 
where whg_whid=@mywhid and whg_tag=@whop_tag and whg_prdid=@whop_prdid and whg_unit=@whop_unit and whg_batch=@newbatch  
set @whgid = ISNULL(@whgid,0)
if (@whgid=0) -- 在库里加一条数据
begin
insert into  mps_warehouse_data(
whg_whid,
whg_tag,
whg_prdid,
whg_unit,
whg_batch,
whg_amount,
whg_baseamount,
whg_fee,
whg_outprice,
whg_date,
whg_expire,
whg_note,
whg_ready,
whg_suspend,
whg_location) 
select   @mywhid , @whop_tag , @whop_prdid , @whop_unit , @newbatch, 0,0,0,0,GETDATE(),'1900-1-1','',0,0,'' 
-- 获取到新加的那条记录 
select top 1 @whgid=whg_id,@outprice=whg_outprice from mps_warehouse_data 
where whg_whid=@mywhid and whg_tag=@whop_tag and whg_prdid=@whop_prdid and whg_unit=@whop_unit and 
whg_batch=@newbatch 
set @whgid = ISNULL(@whgid,0)  
end 

-- 开始向仓库里写数据 
update mps_warehouse_data set 
   whg_amount = whg_amount + @whop_value ,
   whg_baseamount = whg_baseamount + @whop_value * @baseratio ,
   whg_fee = whg_fee + @whop_value * case when @whop_price>=0 then @whop_price else @outprice end , 
   -- whg_outprice = case when whg_outprice=0 then @whop_price else whg_outprice end , 
   -- 这个价格，最好不要动，如果为0，系统自动算价，如果不为0，说明是给定的价
   whg_inamount = whg_inamount + @whop_value ,
   whg_infee = whg_infee + @whop_value * case when @whop_price>=0 then @whop_price else @outprice end , 
   whg_date = @whop_opdate ,
   whg_expire = @whop_expire , 
   whg_note = @whop_note , 
   whg_location = @whop_location 
  where whg_id=@whgid 
-- 将操作数据登录起来，以便还原
insert into mps_warehouse_operation_detail(whopd_whopid,whopd_whgid,whopd_amount,whopd_fee,whopd_operation,whopd_baseamount,whopd_whid,whopd_prdid,whopd_tag,whopd_unit,whopd_batch,whopd_date)
values(@whop_id,@whgid,@whop_value,@whop_value * case when @whop_price>=0 then @whop_price else @outprice end,1,@whop_value * @baseratio,@whop_whid,@whop_prdid,@whop_tag,@whop_unit,@whop_batch,@whop_opdate) 

end