发货通知单对库存进行控制
金蝶云社区-GOICE
GOICE
1人赞赏了该文章 1,073次浏览 未经作者许可,禁止转载编辑于2020年07月17日 09:16:44

发货通知单进行保存、审核时,如果即时库存数量不足,单据不可以保存、审核
IF OBJECT_ID(N'FHTZD', 'TR') IS NOT NULL
DROP TRIGGER FHTZD;--发货通知单
GO
CREATE TRIGGER FHTZD
ON dbo.SEOutStock
FOR UPDATE--INSERT:单据保存时触发;UPDATE:单据审核时触发
AS
DECLARE @Finterid INT;
DECLARE @FTranType INT;
SELECT DISTINCT @Finterid = FInterID
FROM Inserted;
SELECT DISTINCT @FTranType = FTranType
FROM Inserted;

IF NOT EXISTS ( SELECT 1
FROM dbo.SEOutStockEntry A
INNER JOIN dbo.SEOutStock B ON A.FInterID = B.FInterID
INNER JOIN ( SELECT FItemID ,
FStockID ,
FBatchNo ,
SUM(FQty) AS FQTY
FROM dbo.ICInventory
WHERE FQty > 0
GROUP BY FItemID ,
FStockID ,
FBatchNo ) C ON A.FItemID = C.FItemID
WHERE C.FQTY >= A.FQty
AND C.FStockID = A.FStockID
AND C.FBatchNo = A.FBatchNo
AND B.FTranType = @FTranType
AND B.FInterID = @Finterid )
BEGIN
SET NOCOUNT ON;
RAISERROR('出库数量超出对应仓库,对应批次数量,请核实!', 16, 1);
ROLLBACK;
END;