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

145 lines
4.0 KiB

1 year ago
IF OBJECT_ID('PCreateConsumeTransHist') IS NOT NULL
DROP PROC PCreateConsumeTransHist
GO
CREATE PROC PCreateConsumeTransHist
(
@BillId BIGINT
)
AS
BEGIN
DECLARE @CompId BIGINT
DECLARE @MemId BIGINT
DECLARE @CardId BIGINT
DECLARE @BillMemo VARCHAR(1000)
DECLARE @BillTime DATETIME
DECLARE @BillNo VARCHAR(30)
SELECT @CompId = FCompId,
@MemId = FMemId,
@CardId = FCardId,
@BillMemo = FBillMemo,
@BillTime = FBillTime,
@BillNo = FBillNo
FROM TConsumeBill
WHERE FBillId= @Billid
SELECT A.FId, A.FItemId ,A.FOrigPrice,FDiscount,FAmount,FDctPrice,A.FSequence, A.FQuantity,
A.FMemCourseId, A.FMemComboItemId, A.FUseFreeCourse, A.FDeptId,A.FPoint,
IDENTITY(int) FIndex
INTO #Item
FROM TConsumeItem AS A
WHERE A.FBillId = @Billid
--<EFBFBD><EFBFBD>Ʒ<EFBFBD><EFBFBD>
SELECT A.FId, A.FGoodsId ,A.FOrigPrice,FDiscount,FAmount,FDctPrice,A.FSequence, A.FQuantity, A.FUnitId,FStoreId,
A.FMemComboGoodsId, A.FDeptId,A.FPoint,
IDENTITY(int) FIndex
INTO #Goods
FROM TConsumeGoods AS A
WHERE A.FBillId = @Billid
--<EFBFBD><EFBFBD>Ŀ֧<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϸ
SELECT FDetailId,FPayTypeId,FPayAmt ,FCardId
INTO #ItemPayment
FROM TConsumeItemPayment WITH(NOLOCK)
WHERE FBillId = @Billid
--<EFBFBD><EFBFBD>Ʒ֧<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϸ
SELECT FDetailId,FPayTypeId,FPayAmt,FCardId
INTO #GoodsPayment
FROM TConsumeGoodsPayment WITH(NOLOCK)
WHERE FBillId = @Billid
--Ա<EFBFBD><EFBFBD>
SELECT 'Item' AS FBizType,0 AS FIndex,FDetailId,
E1.FNo + '-' + E1.FName + CASE WHEN A.FDispatchMode != 10002 THEN ('[' + ISNULL(S.FName,'') + ']') ELSE '' END AS FEmployees
INTO #HistEmployee_
FROM TConsumeItemEmployee AS A WITH(NOLOCK)
LEFT JOIN TBasicType AS S WITH(NOLOCK) ON S.FId = A.FDispatchMode AND S.FKey = 'ServeDispatchMode'
,TEmployee AS E1 WITH(NOLOCK)
-- LEFT JOIN TEmployeeSub AS E2 WITH(NOLOCK) ON E2.FCompId = @CompId AND E2.FEmpId = E1.FId
WHERE FBillId = @BillId
AND E1.FId = A.FEmpId
UNION ALL
SELECT 'Goods' AS FBizType,0 AS FIndex,FDetailId,
E1.FNo + '-' + E1.FName AS FEmployees
FROM TConsumeGoodsEmployee AS A WITH(NOLOCK),TEmployee AS E1 WITH(NOLOCK)
-- LEFT JOIN TEmployeeSub AS E2 WITH(NOLOCK) ON E2.FCompId = @CompId AND E2.FEmpId = E1.FId
WHERE FBillId = @BillId
AND E1.FId = A.FEmpId
DECLARE @DetailId BIGINT
DECLARE @Index INT
DECLARE @Employee VARCHAR(1000)
SET @DetailId = 1
SET @Index = 0
UPDATE #HistEmployee_
SET @Employee = CASE WHEN @DetailId = FDetailId THEN @Employee + ',' + FEmployees ELSE FEmployees END,
@Index = CASE WHEN @DetailId = FDetailId THEN @Index + 1 ELSE FIndex END,
FEmployees = @Employee,
FIndex = @Index,
@DetailId = FDetailId
SELECT FDetailId,FBizType,MAX(FEmployees) AS FEmployees
INTO #HistEmployee
FROM #HistEmployee_
GROUP BY FDetailId,FBizType
EXEC PAuditConsumeMemberTransHist
@BillId = @Billid, --<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID
@CompId = @CompId, --<EFBFBD><EFBFBD>˾ID
@BillNo = @BillNo, --<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@BillTime =@BillTime, --<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>
@BillType ='XF', --<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@MemId = @MemId, --<EFBFBD><EFBFBD>Աid
@CardId = @CardId, --<EFBFBD><EFBFBD>ID
@BillMemo = @BillMemo --<EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD>ע
DROP TABLE #Item
DROP TABLE #Goods
DROP TABLE #ItemPayment
DROP TABLE #GoodsPayment
END
GO
DROP TABLE #Consume
SELECT ' EXEC PCreateConsumeTransHist ' + CAST(FBillId AS VARCHAR) AS FSql
INTO #Consume
FROM TConsumeBill
WHERE FState = 1
AND FMemID > 0
DECLARE @I INT
SELECT @I = 1
DECLARE @Sql VARCHAR(8000)
DECLARE List CURSOR FOR SELECT FSql FROM #Consume
OPEN List
FETCH List INTO @Sql
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC(@SQL)
PRINT @I
SELECT @I = @I + 1
FETCH List INTO @Sql
END
CLOSE List
DEALLOCATE LIST