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

144 lines
4.0 KiB

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
--膠틔깊
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
--淃커連마츠玖
SELECT FDetailId,FPayTypeId,FPayAmt ,FCardId
INTO #ItemPayment
FROM TConsumeItemPayment WITH(NOLOCK)
WHERE FBillId = @Billid
--끓틔連마츠玖
SELECT FDetailId,FPayTypeId,FPayAmt,FCardId
INTO #GoodsPayment
FROM TConsumeGoodsPayment WITH(NOLOCK)
WHERE FBillId = @Billid
--逃묏
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, --데앴ID
@CompId = @CompId, --무鱇ID
@BillNo = @BillNo, --데뵀
@BillTime =@BillTime, --데앴珂쇌
@BillType ='XF', --데앴잚깎
@MemId = @MemId, --삔逃id
@CardId = @CardId, --엥ID
@BillMemo = @BillMemo --데앴구鬧
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