|
|

楼主 |
发表于 2011/4/1 16:10:02
|
显示全部楼层
USE [AIS20110310170031]
GO
/****** Object: StoredProcedure [dbo].[GetOverFlowStorage] Script Date: 03/24/2011 16:16:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
/******************************************************************************
* FUNCTION NAME: GetOverFlowStorage *
* CREATED BY: Tipi *
* CREATION DATE: 2005-05-31 *
* DESCRIPTION: 存量管理检查存储过程 *
* PARAMETERS: *
******************************************************************************/
ALTER PROCEDURE [dbo].[GetOverFlowStorage]
@nFYear INT,
@nFPeriod INT,
@dtBegin SMALLDATETIME,
@dtEnd SMALLDATETIME,
@strFromItem VARCHAR (2048),
@StrWhereItem VARCHAR (2048),
@strFromStock VARCHAR (2048),
@StrWhereStock VARCHAR (2048)
AS
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
DECLARE @StrSql VARCHAR (8000)
DECLARE @StrItemNumber VARCHAR(50)
--根据设置表构造检查临时表
SELECT FItemID,FStockID,0 AS FQty Into #tmp_xia FROM ICstorageSET where FCheck=1
--更新及时库存数量
--实仓
Update t1 SET t1.FQty=isnull(t2.FQty,0) FROM #tmp_xia t1 inner join (SELECT FItemid,FStockID,sum(Fqty) AS fqty FROM icinventory GROUP BY fitemid,fstockid )t2 ON t1.FItemID=t2.FItemID and t1.FStockID=t2.FStockID
--虚仓
Update t1 SET t1.FQty=isnull(t2.FQty,0) FROM #tmp_xia t1 inner join ( SELECT FItemid,FStockID,sum(Fqty) AS fqty FROM POInventory GROUP BY fitemid,fstockid ) t2 ON t1.FItemID=t2.FItemID and t1.FStockID=t2.FStockID
--====/开始/======统计出非明细仓库中的的所有物料的即时库存(包括设置了检查点的物料和没有设置的物料)的数量==========
--声明@i保存最大的明细仓库的级数
declare @i AS int
SELECT @i=MAX(flevel) FROM t_item where fitemclassid=5
--如果存在非明细仓库
if @i>1
begin
--声明@j保存倒数第二级的明细仓库的级数
declare @j int
SET @j = @i-1
----/开始/--从#tmp_xia把所有物料的最下层的明细数量汇总到它的上一级(仅仅上一级)的仓库中
while @i>1
begin
update #tmp_xia SET #tmp_xia.FQty=t3.FQty FROM (
SELECT t2.fitemid,t1.FParentID,sum(t2.FQty) AS FQty,t2.FStockID
FROM #tmp_xia t2 ,t_item t1
where t1.FLevel = @j and t2.FStockID=t1.FItemID and t1.FItemClassID=5
GROUP BY t2.fitemid,t1.FParentID,t2.FStockID ) t3
where #tmp_xia.FStockId=t3.FStockID and #tmp_xia.FItemID=t3.FItemID
SET @i = @i-1
end
end
--====/结束/======统计出非明细仓库中的的所有物料的即时库存(包括设置了检查点的物料和没有设置的物料)的数量==========
--====/开始/======================取出所需的数据=======================--
--====/开始/==少于最低库存====--
SELECT @StrItemNumber = ISNULL(FValue, '0' ) FROM t_Systemprofile WHERE FKey = 'UseShortNumber' AND FCategory = 'IC'
IF @StrItemNumber = '1'
SET @StrItemNumber = 'FShortNumber'
ELSE
SET @StrItemNumber = 'FNumber'
SET @StrSql= 'SELECT t3.' + @StrItemNumber + ' AS FNumber ,t3.FName AS FName,t6.FModel AS FModel,t4.FFullName AS FStockName,t4.' + @StrItemNumber + ' AS FStockNumber,
t1.FQty AS FQty,t2.FmaxStorage AS FMaxStorage ,t2.FminStorage AS FMinStorage,t1.FQty-t2.FminStorage AS banlance,t5.FQtyDecimal,
t1.FStockID AS FStockID ,t1.FItemID AS FItemID , t5.FUnitID AS FUnitID ,t7.FName AS FUnitName,t7.' + @StrItemNumber + ' AS FUnitNumber,t5.FLeadTime AS FLeadTime,
t5.FAuxClassID ,t5.FSecUnitID AS FSecUnitID ,t8.FName AS FSecUnitName ,t8.' + @StrItemNumber + ' AS FSecUnitNumber,ISNULL(t5.FSecCoefficient,0) AS FSecCoefficient,
t5.FOrderUnitID AS FOrderUnitID ,t9.FName AS FOrderUnitName ,t9.' + @StrItemNumber + ' AS FOrderUnitNumber,ISNULL(t9.FCoefficient,0) AS FOrderCoefficient,
t5.FStoreUnitID AS FStoreUnitID ,t10.FName AS FStoreUnitName ,t10.' + @StrItemNumber + ' AS FStoreUnitNumber,ISNULL(t10.FCoefficient,0) AS FStoreCoefficient
FROM #tmp_xia t1 left join
(SELECT * FROM ICstorageSET where FCheck=1) t2 ON t1.Fitemid=t2.Fitemid and t1.FStockid=t2.FStockid inner join t_Item t3
ON t3.FItemid=t2.Fitemid LEFT JOIN t_Item t4 ON t4.FItemid=t1.FStockID LEFT JOIN t_ICitem t5 ON t5.FItemid=t2.Fitemid
LEFT JOIN t_MeasureUnit t7 ON t5.FUnitID = t7.FItemID
LEFT JOIN t_MeasureUnit t8 ON t5.FSecUnitID = t8.FItemID
LEFT JOIN t_MeasureUnit t9 ON t5.FOrderUnitID = t9.FItemID
LEFT JOIN t_MeasureUnit t10 ON t5.FStoreUnitID = t10.FItemID
LEFT JOIN (SELECT t_item.FItemID AS FItemID ,t_ICItem.FModel AS FModel FROM t_Item ,t_ICItem where t_Item.FItemID=t_IcItem.FItemID ) t6
ON t6.FItemID=t2.Fitemid ' + @StrFromItem +@StrFromStock + ' where t1.FQty < t2.FminStorage and t2.FminStorage>0 '
+ @StrWhereItem + @StrWhereStock + ' order by t3.' + @StrItemNumber + ',t3.FStockID '
--print @StrSql
exec(@StrSql)
--====/开始/==超过最高库存====--
-- 物料代号、 物料名称、所在仓位、 即时存量、最高库存、 差额、采购数量、
SET @StrSql= 'SELECT t3.' + @StrItemNumber + ' AS FNumber,t3.FName AS Fname,t6.FModel AS FModel,t4.FFullName AS FStockName,t4.' + @StrItemNumber + ' AS FStockNumber,
t1.FQty AS FQty,t2.FmaxStorage AS FMaxStorage,t2.FminStorage AS FMinStorage,t1.FQty-t2.FmaxStorage AS banlance,t1.FStockID AS FStockID ,
t7.FName AS FUnitName,t7.' + @StrItemNumber + ' AS FUnitNumber, t5.FQtyDecimal
FROM #tmp_xia t1 LEFT JOIN (SELECT * FROM ICstorageSET where FCheck=1) t2 ON t1.Fitemid=t2.Fitemid and t1.FStockid=t2.FStockid
LEFT JOIN t_Item t3 ON t3.FItemid=t2.Fitemid LEFT JOIN t_Item t4 ON t4.FItemid=t1.FStockID LEFT JOIN t_ICitem t5 ON t5.FItemid=t2.Fitemid
LEFT JOIN t_MeasureUnit t7 ON t5.FUnitID = t7.FItemID
LEFT JOIN (SELECT t1.FItemID AS FItemID ,t2.FModel AS FModel FROM t_Item t1 ,t_ICItem t2 where t1.FItemID=t2.FItemID ) t6
ON t6.FItemID=t2.Fitemid ' + @StrFromItem + @StrFromStock + ' where t1.FQty > t2.FmaxStorage and t2.FmaxStorage>0 '
+ @StrWhereItem + @StrWhereStock + ' order by t3.' + @StrItemNumber + ',t3.FStockID '
exec(@StrSql)
drop table #tmp_xia |
|