You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
sql-tools/李双双库存问题.sql

323 lines
10 KiB

/*
SELECT * FROM TUser
DROP TABLE #Diff
SELECT * INTO #Diff FROM
(
SELECT G.FNo,G.FName,C.FNo AS CompNo, A.FGoodsId,A.FCompId,A.FStoreId,A.FQuantity,B.FHistQuantity
FROM (
SELECT A.FGoodsId,A.FCompId,A.FStoreId,SUM(A.FQuantity) AS FQuantity
FROM ZhengZhou_LiShuangShuang..TInventory AS A
GROUP BY A.FGoodsId,A.FCompId,A.FStoreId
) AS A
,
(
SELECT A.FGoodsId,A.FCompId,A.FStoreId, SUM(A.FInStdQuantity) - SUM(A.FOutStdQuantity) AS FHistQuantity
FROM ZhengZhou_LiShuangShuang..TInventoryHist AS A
GROUP BY A.FGoodsId,A.FCompId,A.FStoreId
) AS B, TGoods AS G,TCompany AS C
WHERE G.FId = A.FGoodsId
AND G.FId = B.FGoodsId
AND A.FGoodsId = B.FGoodsId
AND A.FStoreId = B.FStoreId
AND A.FCompId = B.FCompId
AND A.FCompId = C.FId
) AS A
WHERE A.FQuantity !=A.FHistQuantity
DELETE #Diff WHERE FNo != '110018'
select * from #Diff WHERE FNo = '160006'
SELECT * FROM #Diff
*/
/*
SELECT * FROM TStockInDetail WHERE FId NOT IN(SELECT FTransDetailId FROM TInventoryHist)
AND FBillId IN (SELECT FBillId FROM TStockInBill WHERE FState = 1)
SELECT * FROM TInventoryHist WHERE FTransDetailId NOT IN (SELECT FId FROM TStockOutDetail) AND FBIllType = 'CK'
*/
DECLARE @GoodsId BIGINT
DECLARE @GoodsNo VARCHAR(30)
DECLARE @CompNo VARCHAR(30)
DECLARE @CompId BIGINT
DECLARE @StoreId BIGINT
DECLARE Cur Cursor FOR select FGoodsId,FNo,FCompId,CompNo,FStoreId from #Diff -- WHERE CompNo = '011'
OPEN Cur
FETCH Cur INTO @GoodsId,@GoodsNo,@CompId,@CompNo,@StoreId
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @BillId BIGINT
DECLARE @BillNo VARCHAR(30)
DECLARE @BillTime DATETIME
DECLARE @UnitId BIGINT
DECLARE @ConvDir INT
DECLARE @ConvRate DECIMAL(18,12)
DECLARE @PhyQty DECIMAL(18,12)
DECLARE @StdPhyQty DECIMAL(18,12)
DECLARE @StdDiffQty DECIMAL(18,12)
DECLARE @DiffQty DECIMAL(18,12)
DECLARE @BillDetailId BIGINT
DECLARE @StockInBillId BIGINT
DECLARE @StockOutBillId BIGINT
DECLARE @StdUnitId BIGINT
SELECT TOP 1 @BillId = A.FBillId,@BillNo = A.FBillNo,@BillTime = FBillTime,
@UnitId = FUnitId,
@PhyQty = FPhyInventory,
@BillDetailId = C.FId,
@StockInBillId = FStockInBillId,
@StockOutBillId = FStockOutBillId
FROM TTakeStockBill AS A,TTakeStockDetail AS B,TTakeStockDetailBatch AS C
WHERE A.FBillID = B.FBillId
AND B.FGoodsId = @GoodsId
AND A.FCompId = @CompId
AND C.FDetailId = B.FId
AND A.FStoreId = @StoreId
AND A.FState = 1
ORDER BY FBillTime DESC
SELECT @StdUnitId = FUnitId FROM TGoods WHERE FId = @GoodsId
PRINT @GoodsNo
PRINT @CompNo
PRINT @BillNo
PRINT @CompId
PRINT @GoodsId
PRINT @StoreId
PRINT @BillTime
--<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD>
--<EFBFBD><EFBFBD><EFBFBD>±<EFBFBD>׼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
--<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
SELECT @ConvDir = 0,
@ConvRate = 1
FROM TGoods AS A WITH(NOLOCK)
WHERE A.FUnitId = @UnitId
AND A.FId = @GoodsId
IF @ConvDir IS NULL
BEGIN
SELECT @ConvDir = B.FConvDir,
@ConvRate = B.FConvRate
FROM TGoodsMultiUnit AS B
WHERE @UnitId = B.FUnitId
AND @GoodsId = B.FGoodsId
END
UPDATE A
SET A.FInStdQuantity =
CASE WHEN B.FConvDir = 2 THEN FInQuantity * B.FConvRate ELSE FInQuantity / FConvRate END,
A.FOutStdQuantity =
CASE WHEN B.FConvDir = 2 THEN FOutQuantity * B.FConvRate ELSE FOutQuantity / FConvRate END
FROM TInventoryHist AS A,TGoodsMultiUnit AS B
WHERE A.FGoodsId = B.FGoodsId
AND A.FUnitId = B.FUnitId
AND A.FGoodsId = @GoodsId
AND A.FCompId = @CompId
EXEC PRecalcInventoryHistBalance @CompId,@GoodsId,@StoreId,NULL
DECLARE @Quantity DECIMAL(18,12)
SELECT @Quantity = ISNULL(SUM(FInStdQuantity) - SUM(FOutStdQuantity),0)
FROM TInventoryHist
WHERE FCompId = @CompId
AND FGoodsId = @GoodsId
AND FStoreId = @StoreId
AND FTime < @BillTime
/*
SELECT SUM(FInStdQuantity) - SUM(FOutStdQuantity)
FROM TInventoryHist
WHERE FCompId = 3472610663749124096
AND FGoodsId = 7952782927033131997
AND FStoreId = 5351328954985378166
AND FTime < '12 15 2015 3:16PM'
*/
--<EFBFBD><EFBFBD><EFBFBD>㵱ǰ<EFBFBD><EFBFBD>λ<EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
-- SELECT @DiffQty = CASE WHEN @ConvDir = 2 THEN @StdDiffQty / @ConvRate ELSE @StdDiffQty * @ConvRate END
--<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׼<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
SELECT @StdPhyQty = CASE WHEN @ConvDir = 2 THEN @PhyQty * @ConvRate ELSE @PhyQty / @ConvRate END
--<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
SELECT @StdDiffQty = @StdPhyQty - @Quantity
--<EFBFBD><EFBFBD><EFBFBD>㵱ǰ<EFBFBD><EFBFBD>λ<EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
SELECT @DiffQty = CASE WHEN @ConvDir = 2 THEN @StdDiffQty / @ConvRate ELSE @StdDiffQty * @ConvRate END
print @Quantity
print @StdPhyQty
print @StdDiffQty
print @DiffQty
UPDATE A
SET A.FBookInventory = @Quantity,
A.FPhyStdInventory = @StdPhyQty
FROM TTakeStockDetailBatch AS A
WHERE FId = @BillDetailId
IF @StdDiffQty > 0 OR @StdDiffQty = 0
BEGIN
UPDATE A
SET A.FQuantity = @StdDiffQty,
A.FStdQuantity = @StdDiffQty
FROM TStockInDetail AS A,TStockInBill AS T
WHERE T.FBillId = A.FBillId
AND A.FGoodsId = @GoodsId
AND A.FBillId = @StockInBillId
IF @@ROWCOUNT = 0
BEGIN
DECLARE @InDetailId BIGINT
DECLARE @Price DECIMAL(18,6)
SELECT @InDetailId = dbo.FNNewId()
SELECT @Price = FPurCostPrice FROM TGoods WHERE FId = @GoodsId
INSERT TStockInDetail(FId,FBillId,FSequence,FGoodsId,FUnitId,FPrice,FAmount,FQuantity,FFromQuantity,
FAssistUnitId,FAssistQuantity,FSupplierId,FStoreId,FBatchNo,FPeriodDate,FStdQuantity,FIsAutoGen)
SELECT @InDetailId,@StockInBillId,(SELECT MAX(FSequence) + 1 FROM TStockInDetail WHERE FBillId = @StockInBillId ),
@GoodsId,@StdUnitId,@Price,@Price * @StdDiffQty,@StdDiffQty,@StdDiffQty,
0,0,0,@StoreId,'',NULL,0,1
END
UPDATE A
SET A.FInQuantity = B.FQuantity,
A.FInStdQuantity = B.FStdQuantity
FROM TInventoryHist AS A,TStockInDetail AS B
WHERE FTransId = @StockInBillId
AND A.FGoodsId = B.FGoodsId
AND A.FTransId = B.FBillId
AND A.FGoodsId = @GoodsId
IF @@ROWCOUNT = 0
BEGIN
UPDATE A
SET A.FBillType = 'RK',
A.FBizType = 'Profit',
A.FOutQuantity = 0,
A.FOutStdQuantity = 0,
A.FInQuantity = @StdDiffQty,
A.FInStdQuantity = @StdDiffQty,
A.FTransId = @StockInBillId,
A.FTransDetailId = @InDetailId
FROM TInventoryHist AS A
WHERE FTransId = @StockOutBillId
AND A.FGoodsId = @GoodsId
END
END
IF @StdDiffQty < 0 OR @StdDiffQty = 0
BEGIN
UPDATE A
SET A.FQuantity = -@StdDiffQty,
A.FStdQuantity = -@StdDiffQty
FROM TStockOutDetailBatch AS A,TStockOutBill AS T,TStockOutDetail AS B
WHERE T.FBillId = A.FBillId
AND B.FGoodsId = @GoodsId
AND A.FBillId = @StockOutBillId
AND B.FId = A.FDetailId
IF @@ROWCOUNT = 0
BEGIN
print 'count -------------------------'
--<EFBFBD><EFBFBD>ϸ
DECLARE @OutDetailId BIGINT
SELECT @OutDetailId = dbo.FNNewId()
INSERT TStockOutDetail(FId,FBillId,FSequence,FGoodsId,FUnitId)
SELECT @OutDetailId,@StockOutBillId,(SELECT MAX(FSequence) + 1 FROM TStockOutDetail WHERE FBillId = @StockOutBillId ),@GoodsId,@StdUnitId
INSERT TStockOutDetailBatch(FId,FBillId,FDetailId,FBatchNo,FQuantity,FStdQuantity)
SELECT dbo.FNNewId(),@StockOutBillId,@OutDetailId,'',-@DiffQty,-@DiffQty
END
DELETE TStockInDetail WHERE FBillId = @StockInBillId AND FGoodsId = @GoodsId
UPDATE A
SET A.FOutQuantity = C.FQuantity,
A.FOutStdQuantity = C.FStdQuantity
FROM TInventoryHist AS A,TStockOutDetail AS B,TStockOutDetailBatch AS C
WHERE FTransId = @StockOutBillId
AND A.FGoodsId = B.FGoodsId
AND A.FTransId = B.FBillId
AND B.FId = C.FDetailId
AND A.FGoodsId = @GoodsId
IF @@ROWCOUNT = 0
BEGIN
UPDATE A
SET A.FBillType = 'CK',
A.FBizType = 'Iosses',
A.FInQuantity = 0,
A.FInStdQuantity = 0,
A.FOutQuantity = -@StdDiffQty,
A.FOutStdQuantity = -@StdDiffQty,
A.FTransId = @StockOutBillId,
A.FTransDetailId = @OutDetailId
FROM TInventoryHist AS A
WHERE FTransId = @StockInBillId
AND A.FGoodsId = @GoodsId
END
END
EXEC PRecalcInventoryHistBalance @CompId,@GoodsId,@StoreId,NULL
SELECT @Quantity = ISNULL(SUM(FInStdQuantity) - SUM(FOutStdQuantity),0)
FROM TInventoryHist
WHERE FCompId = @CompId
AND FGoodsId = @GoodsId
AND FStoreId = @StoreId
UPDATE TInventory SET FQuantity = @Quantity
WHERE FCompId = @CompID
AND FGoodsId = @GoodsId
AND FStoreId = @StoreId
--break
FETCH Cur INTO @GoodsId,@GoodsNo,@CompId,@CompNo,@StoreId
END
CLOSE Cur
deallocate Cur
go
--
/*
SELECT ISNULL(SUM(FInStdQuantity) - SUM(FOutStdQuantity),0)
FROM TInventoryHist
WHERE FCompId = 3472330288284041216
AND FGoodsId = 4837181226551091980
AND FTime < @BillTime
SELECT * FROM TInventoryHist WHERE FGoodsId = 4837181226551091980 AND FCompId = 3472330288284041216
SELECT * FROM TTakeStockDetailBatch
SELECT FStoreId,SUM(FQuantity) FROM TInventory
GROUP BY FStoreId
SELECT * FROM TTakeStockBill WHERE FStoreId = 5460289448040568312
SELECT * FROM TUser
*/