当前库存统计分析

由于出库时未限制出库数量不能大于当前库存,可能会导致库存为负,针对此种情况,在出库时添加校验!

当前仓库库存的计算方式

场景及其包含的操作

graph LR

调拨出库列表-->|出库|调拨;
调拨-->|调入当前仓库 库存+|当前仓库;
调拨-->|调出当前仓库 库存-|当前仓库;
组装单列表-->|其它|组装单;
组装单-->组装-组合件;
组装单-->组装-普通子件;
组装-组合件-->|库存+|当前仓库;
组装-普通子件-->|库存-|当前仓库;

拆卸单列表-->|其它|拆卸单;
拆卸单-->拆卸-组合件;
拆卸单-->拆卸-普通子件;
拆卸-组合件-->|库存-|当前仓库;
拆卸-普通子件-->|库存+|当前仓库;

其它入库列表-->|入库|入库-其它;
入库-其它-->|库存+|当前仓库;
其它出库列表-->|出库|出库-其它;
出库-其它-->|库存-|当前仓库;

采购退货列表-->|出库|采购退货;
采购退货-->|库存-|当前仓库;
采购入库列表-->|入库|采购;
采购-->|库存+|当前仓库;
采购订单列表-->|其它|采购订单;
采购订单-->|库存不变|当前仓库;
零售退货列表-->|入库|零售退货;
零售退货-->|库存+|当前仓库;
零售出库列表-->|出库|零售;
零售-->|库存-|当前仓库;
销售退货列表-->|入库|销售退货;
销售退货-->|库存+|当前仓库;
销售订单列表-->|其它|销售订单;
销售订单-->|库存不变|当前仓库;
销售出库列表-->|出库|销售;
销售-->|库存-|当前仓库;

数据对应

graph LR
dh.type=入库-->dh.SubType=其它;
dh.type=入库-->dh.SubType=采购;
dh.type=入库-->dh.SubType=销售退货;
dh.type=入库-->dh.SubType=零售退货;

graph LR
dh.type=其它-->dh.SubType=拆卸单;
dh.type=其它-->dh.SubType=组装单;
dh.type=其它-->dh.SubType=采购订单;
dh.type=其它-->dh.SubType=销售订单;

graph LR
dh.type=出库-->dh.SubType=调拨;
dh.type=出库-->dh.SubType=其它;
dh.type=出库-->dh.SubType=采购退货;
dh.type=出库-->dh.SubType=零售;
dh.type=出库-->dh.SubType=销售;

库存计算

graph LR
  dh.type=入库-->|di.DepotId=当前仓库 库存+|当前仓库;
      dh.SubType=调拨-->|di.AnotherDepotId=当前仓库 库存+|当前仓库;
	  dh.SubType=调拨-->|di.DepotId=当前仓库 库存-|当前仓库;
      dh.type=出库-->|dh.SubType!=调拨 di.DepotId=当前仓库 库存-|当前仓库;
      dh.SubType=组装单-->|di.MType=组合件 di.DepotId=当前仓库 库存+|当前仓库;
      dh.SubType=组装单-->|di.MType=普通子件di.DepotId=当前仓库 库存-|当前仓库;
      dh.SubType=拆卸单-->|di.MType=普通子件di.DepotId=当前仓库 库存+|当前仓库;
      dh.SubType=拆卸单-->|di.MType=组合件di.DepotId=当前仓库 库存-|当前仓库;

查询 sql(统计数据条数)

select  ((curep.inTotal+curep.transfInTotal+curep.assemInTotal+curep.disAssemInTotal)
-(curep.transfOutTotal+curep.outTotal+curep.assemOutTotal+curep.disAssemOutTotal)) as currentRepo
from 
(select sum(dh.type='入库' and di.DepotId=#{depotId}) as inTotal,
sum(dh.SubType='调拨' and di.AnotherDepotId=#{depotId}) as transfInTotal,
sum(dh.SubType='调拨' and di.DepotId=#{depotId}) as transfOutTotal,
sum(dh.type='出库' and dh.SubType!='调拨' and di.DepotId=#{depotId}) as outTotal, 
sum(dh.SubType='组装单' and di.MType='组合件' and di.DepotId=#{depotId}) as assemInTotal, 
sum(dh.SubType='组装单' and di.MType='普通子件' and di.DepotId=#{depotId}) as assemOutTotal, 
sum(dh.SubType='拆卸单' and di.MType='普通子件' and di.DepotId=#{depotId}) as disAssemInTotal, 
sum(dh.SubType='拆卸单' and di.MType='组合件' and di.DepotId=#{depotId}) as disAssemOutTotal
from 
jsh_depothead dh,jsh_depotitem di
where 1=1  
and dh.id=di.HeaderId
and di.MaterialId=#{mid}
and ifnull(dh.delete_Flag,'0') !='1'
and ifnull(di.delete_Flag,'0') !='1') curep

查询 sql(统计库存数量)

select  ((curep.inTotal+curep.transfInTotal+curep.assemInTotal+curep.disAssemInTotal)
-(curep.transfOutTotal+curep.outTotal+curep.assemOutTotal+curep.disAssemOutTotal)) as currentRepo
from 
(select sum(if(dh.type='入库' and di.DepotId=#{depotId},di.BasicNumber,0)) as inTotal,
sum(if(dh.SubType='调拨' and di.AnotherDepotId=#{depotId},di.BasicNumber,0)) as transfInTotal,
sum(if(dh.SubType='调拨' and di.DepotId=#{depotId},di.BasicNumber,0)) as transfOutTotal,
sum(if(dh.type='出库' and dh.SubType!='调拨' and di.DepotId=#{depotId},di.BasicNumber,0)) as outTotal, 
sum(if(dh.SubType='组装单' and di.MType='组合件' and di.DepotId=#{depotId},di.BasicNumber,0)) as assemInTotal, 
sum(if(dh.SubType='组装单' and di.MType='普通子件' and di.DepotId=#{depotId},di.BasicNumber,0)) as assemOutTotal, 
sum(if(dh.SubType='拆卸单' and di.MType='普通子件' and di.DepotId=#{depotId},di.BasicNumber,0)) as disAssemInTotal, 
sum(if(dh.SubType='拆卸单' and di.MType='组合件' and di.DepotId=#{depotId},di.BasicNumber,0)) as disAssemOutTotal
from 
jsh_depothead dh,jsh_depotitem di
where 1=1  
and dh.id=di.HeaderId
and di.MaterialId=#{mid}
and ifnull(dh.delete_Flag,'0') !='1'
and ifnull(di.delete_Flag,'0') !='1') curep