- 如题, 个税信息需要采集, 单独增加表完成, 包括生效日期. 采用累计递进制 实现. 举例: 子女教育附加系统中录入需要2019-03月生效, 那么再算扣除是1,2月不包括在累进中.
- 表结构(工具生成, 如果手写, CONSTRAINT 部分可不包括)
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
调用结果: 某一人员对应子女教育附加生效日期是二月, 对应结果如下. 其中前端控件未对生效年月"清尾巴"(有效到月, 忽略日期时间尾巴)的操作, 所以需要在程序中"取整"操作:
-- 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