IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_warehouse_operation_insert_0]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_warehouse_operation_insert_0]
GO
CREATE procedure [dbo].[mps_warehouse_operation_insert_0](
@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
    
    -- 这段程序只处理转库 
    -- 转库要很复杂的，主要是要涉及到成本，涉及到批次
    -- 要考虑以下几种情况：
    -- 1 转库时，商品是有批次的 这时 转入库中也要有批次
    -- 2 转库时，商品没有批定批次，这时候，系统要自动去匹配批次，在转入时，也要自动按批次来操作
    -- 3 转库时，商品没有批定批次，这时候，系统要自动去匹配批次
    --   但是在商品批次里，有以下几个固定值 ， 则在转入时，是需要合并批次的
    --   first/last 按最小批次合并/按最大批次合并

if (@whop_value<=0) return 

-- 建立一个临时表，来保存出库信息
declare @tmp table(whgid bigint , amount decimal(18,4) , fee decimal(18,4) , tag int , prdid int , unit nvarchar(50), batch nvarchar(100), price decimal(18,4) , flag bigint)  
    
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 
  
  select @whoutstyle=wh_outstyle from mps_warehouse where wh_id=@whop_whid  
  set @whoutstyle = ISNULL(@whoutstyle,0)  
  
  set @newbatch = case when @whop_batch<>'' then @whop_batch 
    when @whop_expire>@mindate then convert(varchar(10),@whop_expire,120) 
    else '' 
   end
  
  if (@newbatch='first' or @newbatch='last') set @newbatch='' 
  
  -- 如果有批次，就按批次，要是没批次，就从库里去按顺序搜索
while (@whop_value>0) 
begin
set @amount = 0 
set @whgid = 0
set @outprice = 0
if (@whoutstyle=0) -- 先进先出
select top 1 @whgid=whg_id,@amount=whg_amount,@outprice=case when whg_fee>0 and whg_amount>0 then whg_fee/whg_amount else whg_outprice endfrom mps_warehouse_data 
where whg_whid=@whop_whid and whg_tag=@whop_tag and whg_prdid=@whop_prdid and whg_unit=@whop_unit and 
(whg_batch=@newbatch or @newbatch='') and whg_amount>0 
order by whg_batch 
else 
select top 1 @whgid=whg_id,@amount=whg_amount,@outprice=case when whg_fee>0 and whg_amount>0 then whg_fee/whg_amount else whg_outprice endfrom mps_warehouse_data 
where whg_whid=@whop_whid and whg_tag=@whop_tag and whg_prdid=@whop_prdid and whg_unit=@whop_unit and 
(whg_batch=@newbatch or @newbatch='') and whg_amount>0 
order by whg_batch desc 
if (@amount>0) 
begin
-- 开始减库存
if (@amount>=@whop_value) set @amount=@whop_value 
update mps_warehouse_data set 
whg_amount = whg_amount - @amount ,
whg_baseamount = whg_baseamount - @amount * @baseratio ,
whg_fee = whg_fee - @amount * case when @whop_price<=0 then @outprice else @whop_price end  , 
whg_outamount = whg_outamount + @amount ,
whg_outfee = whg_outfee + @amount * case when @whop_price<=0 then @outprice else @whop_price end ,
whg_date = @whop_opdate 
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,@amount,@amount *case when @whop_price<=0 then @outprice else @whop_price end,-1,@amount * @baseratio,@whop_whid,@whop_prdid,@whop_tag,@whop_unit,@whop_batch,@whop_opdate) 
set @whop_value = @whop_value - @amount 
-- 将出库的数据登记起来
insert into @tmp(whgid , amount,fee , flag , tag , prdid,unit,batch,price) 
select @whgid , @amount , @amount * case when @whop_price<=0 then @outprice else @whop_price end  , 0 , 
whg_tag , whg_prdid , whg_unit , whg_batch , whg_outprice 
from mps_warehouse_data where whg_id=@whgid 
end else 
begin
break 
end 
end 
-- 如果转为成功 
if (not exists (select 1 from @tmp)) return 

if (@whop_batch='first' or @whop_batch='last') 
begin
insert into @tmp(whgid , amount,fee , flag , tag , prdid,unit,batch,price) 
select whgid , sum(amount),sum(fee) , 2 , tag , prdid,unit,'',0 
from @tmp where flag=0  group by whgid , tag ,prdid,unit 
end 
if (@whop_batch='first') 
begin
set @newbatch = '' 
select top 1 @newbatch=batch from @tmp where flag=0 order by batch 
end else 
if (@whop_batch='last') 
begin
set @newbatch = '' 
select top 1 @newbatch=batch from @tmp where flag=0 order by batch desc 
end
if (@whop_batch='first' or @whop_batch='last') 
begin
delete @tmp where flag=0 
update @tmp set batch=@newbatch , flag=0  
end 

-- 看一下目标仓库里，有没有与之对应的商品及批次
update @tmp set flag=1 
where exists (select 1 from mps_warehouse_data where whg_whid=@whop_whid2 and whg_tag=tag and 
whg_prdid=prdid and whg_unit=unit and whg_batch=batch) 

-- 将不存在的那些数据，插入到新仓库中
insert into mps_warehouse_data(whg_whid,whg_tag,whg_prdid,whg_unit,whg_batch) 
select @whop_whid2,tag,prdid,unit,batch  
from @tmp where flag=0 

-- 将数据转到新仓库中
update @tmp set flag=whg_id from mps_warehouse_data where whg_whid=@whop_whid2 and whg_tag=tag and whg_prdid=prdid and whg_unit=unit and whg_batch=batch  

update mps_warehouse_data set 
   whg_amount = whg_amount + amount ,
   whg_baseamount = whg_baseamount + amount * @baseratio ,
   whg_fee = whg_fee + fee , 
   whg_inamount = whg_inamount + amount ,
   whg_infee = whg_infee + fee  
  from @tmp  
  where whg_id=flag 
-- 将操作数据登录起来，以便还原
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)
select @whop_id,flag,amount,fee,1,amount * @baseratio ,@whop_whid2,prdid,tag,unit,batch,@whop_opdate  from @tmp 

end