IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_warehouse_operation_insert]') AND type = 'TR')
DROP TRIGGER [dbo].[mps_warehouse_operation_insert]
GO
 

CREATE trigger [dbo].[mps_warehouse_operation_insert] on [dbo].[mps_warehouse_operation] for insert as
begin
    declare
       @maxcard  int,
       @numrows  int,
       @numnull  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 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 
-- 首先要对商品的最小单位进行计算 


declare @prdbase table(tag int , prdid int , unit nvarchar(20),ratio int) 
insert into @prdbase(tag , prdid,unit,ratio) 
select distinct whop_tag, whop_prdid , whop_unit, 1 from inserted 
update @prdbase set ratio = prdu_ratio from mps_prds_unit where prdu_tag=tag and prdu_prdid=prdid and prdu_unit=unit 
-- 虽然产品表会有可能是不同的表，但是mps_prds_unit 系统只有一个表 并且是一个视图

-- 先不要考虑性能，先考虑稳定   通过游标来解决
declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for
  select whop_id,whop_cmpid,whop_tag,whop_prdid,isnull(whop_whid,0),isnull(whop_whid2,0),whop_opdate,whop_operation,isnull(whop_value,0),isnull(whop_valuesuspend,0),
  whop_unit,whop_batch,isnull(whop_price,0),whop_expire,whop_note,whop_location   from inserted   
  open status1
  
  declare 
  @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)  

  FETCH NEXT FROM status1 INTO @whop_id,@whop_cmpid,@whop_tag,@whop_prdid,@whop_whid,@whop_whid2,@whop_opdate,@whop_operation,@whop_value,@whop_valuesuspend,@whop_unit,@whop_batch,@whop_price,@whop_expire,@whop_note,@whop_location 
  WHILE (@@FETCH_STATUS = 0)
  BEGIN
   -- 如果是出库，则需要计算出该库的出库方式
   if (@whop_operation<=0 and @whoutstyle is null)  -- 出库或转库    @whoutstyle=0 先进先出 1=则相反 
   begin
   select @whoutstyle=wh_outstyle from mps_warehouse where wh_id=@whop_whid  
   set @whoutstyle = ISNULL(@whoutstyle,0)  
   end 
   -- 先处理入库时，库存里没有数据的情况 
   set @mywhid = 0 
   if (@whop_operation=0) set @mywhid = @whop_whid2  else 
   if (@whop_operation=1) set @mywhid = @whop_whid  
   if (@mywhid>0) -- 是需要检验仓库里有没有数据
   begin
   set @outprice = 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='' and @whop_operation=0)  -- 没有批次，并且又是转库，那么可以在库存表里去看有没有合适的批次
 begin
 if (@whoutstyle=0) -- 先进先出
 begin
 select top 1 @newbatch=whg_batch,@outprice=whg_outprice from 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_amount>0 and whg_batch>'' 
 order by whg_batch 
 end else
 begin
 select top 1 @newbatch=whg_batch,@outprice=whg_outprice from 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_amount>0 and whg_batch>'' 
 order by whg_batch desc  
 end 
 set @newbatch = isnull(@newbatch,'') 
 set @outprice = isnull(@outprice,0) 
 end 
 
   set @whgid = 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)
   if (@whgid=0) -- 在库里加一条数据
   begin
   set @outprice = @whop_price 
   
   if (@newbatch>'' and @outprice<=0) 
   begin
   select top 1 @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='' 
   end 
   if (@outprice<=0) 
   begin
   select top 1 @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_outprice>0 
   order by whg_id desc 
   end 
   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,@outprice,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 
   end
   if (@outprice<=0) 
   begin
   set @outprice = @whop_price 
   
 if (@outprice<=0) 
 begin
 select top 1 @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 
 end 
 if (@outprice<=0 and @newbatch>'') 
 begin
 select top 1 @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='' 
 end 
 if (@outprice<=0 and @newbatch='') 
 begin
 select top 1 @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_outprice>0 
 order by whg_id desc 
 end 
 if (@outprice<=0 ) 
 begin
 select top 1 @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_outprice>0 
 order by whg_id desc 
 end 
   end 
   if (@whop_operation=1 or @whop_operation=0)  -- 处理入库  转库也是这样的
   begin
set   @baseratio = 1 
   select top 1 @baseratio = ratio from @prdbase where tag=@whop_tag and prdid=@whop_prdid and unit=@whop_unit 
   set @baseratio = ISNULL(@baseratio,1) 
   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 , 
   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)
   values(@whop_id,@whgid,@whop_value,@whop_value * case when @whop_price>=0 then @whop_price else @outprice end,1,@whop_value * @baseratio) 
   end  
  if (@whop_operation=-1 or @whop_operation=0)  -- 处理出库  转库也是这样的
  begin
  set @newbatch = case when @whop_batch<>'' then @whop_batch 
    when @whop_expire>@mindate then convert(varchar(10),@whop_expire,120) 
    else '' 
   end
   -- 如果有批次，就按批次，要是没批次，就从库里去按顺序搜索
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=whg_outpricefrom 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=whg_outpricefrom 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 (@whgid=0 and @newbatch>'') 
 begin
 if (@whoutstyle=0) -- 先进先出
select top 1 @whgid=whg_id,@amount=whg_amount,@outprice=whg_outpricefrom 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='') and whg_amount>0 
 order by whg_batch 
 else 
 select top 1 @whgid=whg_id,@amount=whg_amount,@outprice=whg_outpricefrom 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='') and whg_amount>0 
 order by whg_batch desc 
 end 
 if (@whgid=0 and @newbatch='') 
 begin
 if (@whoutstyle=0) -- 先进先出
select top 1 @whgid=whg_id,@amount=whg_amount,@outprice=whg_outpricefrom 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_amount>0 
 order by whg_batch 
 else 
 select top 1 @whgid=whg_id,@amount=whg_amount,@outprice=whg_outpricefrom 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_amount>0 
 order by whg_batch desc 
 end 
 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 whg_outprice else @whop_price end  , 
 whg_outamount = whg_outamount + @amount ,
 whg_outfee = whg_outfee + @amount * case when @whop_price<=0 then whg_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)
   values(@whop_id,@whgid,@amount,@amount *case when @whop_price<=0 then @outprice else @whop_price end,-1,@amount * @baseratio) 
 set @whop_value = @whop_value - @amount 
 end else 
 begin
 break 
 end 
 end 
  end 
   FETCH NEXT FROM status1 INTO @whop_id,@whop_cmpid,@whop_tag,@whop_prdid,@whop_whid,@whop_whid2,@whop_opdate,@whop_operation,@whop_value,@whop_valuesuspend,@whop_unit,@whop_batch,@whop_price,@whop_expire,@whop_note,@whop_location 
  END
  CLOSE status1
DEALLOCATE status1 

 
    return

/*  Errors handling  */
error:
    -- raiserror @errno @errmsg
    rollback  transaction
end