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

167 lines
4.3 KiB

use qingdao_yifan
use qingdao_jflying
use qingdao_dadashida
--BEGIN TRAN
--ROLLBACK TRAN
/*
SELECT * FROM TMemberCardAcctHist AS B
WHERE 1=1
AND B.FBillType = 'NF'
AND YEAR(B.FTime) = 2017
ORDER BY B.FTime DESC
SELECT * FROM TMemberCard
WHERE FCardId NOT IN(SELECT FCardId FROM TMemberCardAcctHist WHERE FBillType = 'NF' )
AND FCreateDate < '2016-06-28'
AND FCardid IN(SELECT FCardId FROM TMemberCardAccount WHERE FBalance > 0)
SELECT * FROM TMemberCardAccount WHERE FCardId = 5592214049577067334
UPDATE TMemberCardAccount SET FBalance = FBalance + 24
WHERE FId = 7150050901345958628
SE
SELECT * FROM TMemberSaleBill WHERE FMemId = 5580698840598624980
*/
CREATE PROC PTaskAnnualDues
AS
BEGIN
DECLARE @Today VARCHAR(10)
DECLARE @Now DATETIME
DECLARE @DateTime VARCHAR(20)
DECLARE @Year INT
DECLARE @Dues INT --年费金额
DECLARE @Enabled BIT
SELECT @Now = getdate()
--SELECT @Now = '2017-12-10'
SELECT @Today = CONVERT(VARCHAR,@Now,23)
SELECT @DateTime = CONVERT(VARCHAR,DATEADD(YEAR,-1,@Now),23) + ' 23:59:59'
SELECT @Year = YEAR(@Today)
PRINT @Year
--EXEC PGetBizParameter 1,
--获取今年年费
SELECT @Dues = 24
DECLARE @AcctId BIGINT
SELECT @AcctId = 3
CREATE TABLE #Company
(
FCompId BIGINT
)
IF db_name () = 'QingDao_DaDaShiDa'
BEGIN
INSERT #Company
SELECT FId FROM TCompany WHERE FId = 3472333586818924544 --大大士大 005百丽
END
ELSE IF(db_name() IN ('ShenZhouShi_YiQiKaTe'))
BEGIN
SELECT @Dues = 10
INSERT #Company
SELECT FId FROM TCompany WHERE FId = 8658042276226205461 --一七卡特 001店
END
ELSE IF(db_name() IN ('QingDao_Gong','QingDao_ChaoRenDangDao'))
BEGIN
SELECT @Dues = 19
INSERT #Company
SELECT FId FROM TCompany
END
ELSE IF(db_name() IN ('QingDao_JFlying'))
BEGIN
SELECT @Dues = 24
INSERT #Company
SELECT FId FROM TCompany
END
ELSE
BEGIN
INSERT #Company
SELECT FId FROM TCompany
END
SELECT A.*,C.FBalance,C.FAcctId,CAST(0 AS FLOAT) AS FDues
INTO #Card
FROM TMemberCard AS A
INNER JOIN TMemberCardAccount AS C ON C.FCardId = A.FCardId AND C.FAcctId = @AcctId
-- AND C.FBalance >= @Dues
WHERE A.FCreateDate <= @DateTime
AND RIGHT(CONVERT(varchar(100), A.FCreateDate, 23),5) <= RIGHT(CONVERT(varchar(100), @Now, 23),5)
AND A.FDeleted = 0
AND A.FState = 1
AND A.FCompId IN(SELECT FCompId FROM #Company)
AND NOT EXISTS(
SELECT 1 FROM TMemberCardAcctHist AS B
WHERE B.FCardId = A.FCardId
AND B.FAcctId = C.FAcctId
AND B.FBillType = 'NF'
AND YEAR(B.FTime) = @Year)
DECLARE @Rule INT
SELECT @Rule = 1
IF db_name() LIKE '%YiFan%'
BEGIN
SELECT @Rule = 2
END
IF @Rule = 2
BEGIN
UPDATE A
SET A.FDues = CASE WHEN FBalance >= @Dues THEN @Dues ELSE FBalance END
FROM #Card AS A
WHERE A.FBalance > 0
END
ELSE
BEGIN
UPDATE A
SET A.FDues = @Dues
FROM #Card AS A
WHERE A.FBalance >= @Dues
END
UPDATE A
SET A.FBalance = A.FBalance - @Dues
FROM TMemberCardAccount AS A,#Card AS B
WHERE A.FCardId = B.FCardId
AND A.FAcctId = @AcctId
AND A.FBalance >= @Dues
INSERT INTO TMemberCardAcctHist(FId,FMemId,FCardId,FAcctId,FTime,FCardTypeId,FCompId,FBillType,FBizType,FTransId,FTransNo,FInAmount,FOutAmount,FBalance,FArrear,FRecordTime,FMemo)
SELECT dbo.FNNewId(),A.FMemId,A.FCardId,@AcctId,@Now,A.FTypeId,A.FCompId,'NF','AnnualDues',0,'',0, FDues ,FBalance - FDues ,0,@Now,'自动扣年费'
FROM #Card AS A
drop table #card
END
GO
/*
COMMIT TRAN
SELECT * FROM TMemberCardAcctHist WHERE FBillTYPE = 'NF'
SELECT * FROM TMemberCardAcctHist AS B
WHERE B.FBillType = 'NF'
'AnnualDues'
*/
BEGIN TRAN
PTaskAnnualDues
COMMIT TRAN
SELECT * FROM TMemberCardAcctHist WHERE FBillType = 'NF'