- 如題, 個稅信息須要採集, 單獨增長表完成, 包括生效日期. 採用累計遞進制 實現. 舉例: 子女教育附加系統中錄入須要2019-03月生效, 那麼再算扣除是1,2月不包括在累進中.前端
- 表結構(工具生成, 若是手寫, CONSTRAINT 部分可不包括)sql
CREATE TABLE [dbo].[W1TaxSpelAddDedInfo] ( [TaxSpelAddDedInfoID] [varchar] (36) COLLATE Chinese_PRC_CI_AS NOT NULL, [ArchiveID] [varchar] (36) COLLATE Chinese_PRC_CI_AS NOT NULL, [ChildrenEduDeduction] [decimal] (22, 2) NOT NULL CONSTRAINT [DF__W1TaxSpel__Child__79006A92] DEFAULT ((0)), [ChildrenEduEffecDate] [datetime] NULL, [ContineEduDeduction] [decimal] (22, 2) NOT NULL CONSTRAINT [DF__W1TaxSpel__Conti__79F48ECB] DEFAULT ((0)), [ContineEduEffecDate] [datetime] NULL, [HouseLoanDeduction] [decimal] (22, 2) NOT NULL CONSTRAINT [DF__W1TaxSpel__House__7AE8B304] DEFAULT ((0)), [HouseLoanEffecDate] [datetime] NULL, [HouseRentDeduction] [decimal] (22, 2) NOT NULL CONSTRAINT [DF__W1TaxSpel__House__7BDCD73D] DEFAULT ((0)), [HouseRentEffecDate] [datetime] NULL, [SupporElderlyDeduction] [decimal] (22, 2) NOT NULL CONSTRAINT [DF__W1TaxSpel__Suppo__7CD0FB76] DEFAULT ((0)), [SupporElderlyEffecDate] [datetime] NULL ) ON [PRIMARY] GO ALTER TABLE [dbo].[W1TaxSpelAddDedInfo] ADD CONSTRAINT [PK_W1TaxSpelAddDedInfo] PRIMARY KEY NONCLUSTERED ([TaxSpelAddDedInfoID]) ON [PRIMARY] GO CREATE NONCLUSTERED INDEX [W1A_W1TSADI_FK] ON [dbo].[W1TaxSpelAddDedInfo] ([ArchiveID]) ON [PRIMARY] GO
2. 累計遞進式彙總數據.工具
/*----------------------------------------------------------*/ /* [FW1SpeDeductMoneyTotalGet] */ /*----------------------------------------------------------*/ IF EXISTS ( SELECT 1 FROM sys.objects o WHERE object_id = object_id( N'[FW1SpeDeductMoneyTotalGet]' ) AND OBJECTPROPERTY( object_id, N'IsTableFunction') IS NOT NULL ) DROP FUNCTION [FW1SpeDeductMoneyTotalGet] GO CREATE FUNCTION dbo.[FW1SpeDeductMoneyTotalGet] ( @ArchiveID VARCHAR(36),--人員信息ID @WageTaxMonth INT, --當前月份 @Type INT = -1--費用類型(-1:所有,0子女教育 1繼續教育 2住房 3 租房 4 贍養) ) RETURNS DECIMAL(22,2) AS /* 功能:專項附加扣除月份開累應扣金額 參數:@ArchiveID VARCHAR(36),--人員信息ID @WageTaxMonth INT --當前月份 返回:專項附加扣除月份開累應扣金額 */ BEGIN DECLARE @return DECIMAL(22,2) =0 DECLARE @TaxDate DATE DECLARE @LoopRn INT = 1 WHILE @LoopRn < =@WageTaxMonth BEGIN SET @TaxDate = CONVERT (datetime,CONVERT (VARCHAR (10),CAST(YEAR(GETDATE()) AS VARCHAR(4))+ CASE WHEN @LoopRn <=9 THEN '0' ELSE '' END + CAST(@LoopRn AS VARCHAR(2))+'01',120)) SET @TaxDate = [dbo].[F0_GetMonthBeginDateTime](@TaxDate) IF(@Type=-1 OR @Type = 0) SELECT @return = @return +CASE WHEN [dbo].[F0_GetMonthBeginDateTime](A.ChildrenEduEffecDate)<=@TaxDate THEN ISNULL(A.ChildrenEduDeduction,0) ELSE 0 END --子女教育 FROM W1TaxSpelAddDedInfo A(NOLOCK) WHERE A.ArchiveID = @ArchiveID IF(@Type=-1 OR @Type = 1) SELECT @return = @return +CASE WHEN [dbo].[F0_GetMonthBeginDateTime](A.ContineEduEffecDate)<=@TaxDate THEN ISNULL(A.ContineEduDeduction,0) ELSE 0 END --繼續教育 FROM W1TaxSpelAddDedInfo A(NOLOCK) WHERE A.ArchiveID = @ArchiveID IF(@Type=-1 OR @Type = 2) SELECT @return = @return +CASE WHEN [dbo].[F0_GetMonthBeginDateTime](A.HouseLoanEffecDate)<=@TaxDate THEN ISNULL(A.HouseLoanDeduction,0) ELSE 0 END --住房 FROM W1TaxSpelAddDedInfo A(NOLOCK) WHERE A.ArchiveID = @ArchiveID IF(@Type=-1 OR @Type = 3) SELECT @return = @return +CASE WHEN [dbo].[F0_GetMonthBeginDateTime](A.HouseRentEffecDate)<=@TaxDate THEN ISNULL(A.HouseRentDeduction,0) ELSE 0 END --租房 FROM W1TaxSpelAddDedInfo A(NOLOCK) WHERE A.ArchiveID = @ArchiveID IF(@Type=-1 OR @Type = 4) SELECT @return = @return +CASE WHEN [dbo].[F0_GetMonthBeginDateTime](A.SupporElderlyEffecDate)<=@TaxDate THEN ISNULL(A.SupporElderlyDeduction,0) ELSE 0 END --贍養 FROM W1TaxSpelAddDedInfo A(NOLOCK) WHERE A.ArchiveID = @ArchiveID SET @LoopRn=@LoopRn+1 END return ISNULL(@return,0) END GO
調用結果: 某一人員對應子女教育附加生效日期是二月, 對應結果以下. 其中前端控件未對生效年月"清尾巴"(有效到月, 忽略日期時間尾巴)的操做, 因此須要在程序中"取整"操做: oop
-- F0_GetMonthBeginDateTime -- 獲取最後一天 DECLARE @date DATETIME ; SELECT @date = dbo.F0_GetMonthBeginDateTime( '2019-03-13 01:24:47'); SET @date = DATEADD(SECOND, -1, @date ) SELECT @date /*----------------------------------------------------------*/ /* [F0_GetMonthBeginDateTime] */ /*----------------------------------------------------------*/ IF EXISTS ( SELECT 1 FROM sys.objects o WHERE object_id = object_id( N'[F0_GetMonthBeginDateTime]' ) AND OBJECTPROPERTY( object_id, N'IsTableFunction') IS NOT NULL ) DROP FUNCTION [F0_GetMonthBeginDateTime] GO CREATE FUNCTION [dbo].[F0_GetMonthBeginDateTime] ( @dt DATETIME ) /* 功能:取得傳入日期的月初日期 如:2014-10-01 00:00:00.000 */ RETURNS DATETIME AS BEGIN --SET @dt=CAST(CONVERT(VARCHAR(10), @dt, 121) AS DATETIME) --RETURN @dt RETURN CAST(CAST(YEAR(@dt) AS VARCHAR)+'-'+RIGHT(CAST(100+MONTH(@dt) AS VARCHAR), 2)+'-01' AS DATETIME) END GO