SQL Server: Calculate monthly payment records based on start and end dates and payment methods

Businesses that are often encountered when doing projects: Calculate the payment records between the two dates according to the start and end dates and payment methods specified in the contract, so as to remind the payment.

 

illustrate:

1. The two dates and payment methods in the example are obtained from the contract.

2. The main payment methods are: monthly, quarterly, semi-annual, and annual, and are stored in the database in the form of Chinese characters.

 

Idea: Calculate the number of months between the two dates according to the termination date and the lease start date, and then superimpose the months according to the payment method, that is

Monthly payment*1, quarterly payment*3, semi-annual payment*6, annual payment*12, thus calculate the date of monthly payment and the date of payment of the next month, then calculate the number of days between these two dates, and finally multiply Based on the daily rent, you can get the total rent that should be paid each month.

The SQL is as follows:

CREATE PROCEDURE [dbo].[calcuteContractPay]
	@id int -- contract serial number
AS
BEGIN
	
	SET NOCOUNT ON;

	--1. Check whether the contract serial number exists
	if @id is null
	begin
		raiseerror('must tell the contract serial number',16,1);
		return ;
	end   

	--2. Obtain payment method
	declare @field12 varchar(20);--payment method
	declare @field39 datetime;--lease date
	declare @field40 datetime; -- due date
	declare @field66 numeric(18,2);--Total rent per day
	select @field12 = field12,@field39=field39,@field40=field40,@field66=isnull(field66,0) from table3 where field1=@id;
	if @field12 is null or len(@field12)<=0
	begin
		raiseerror('must inform the payment method',16,1);
		return ;
	end
	
	
	--3. Check whether the date is legal
	declare @day int; -- the number of days between
	set @day = datediff(day,@field39,@field40);
	if(@day<=0)
	begin
		raiseerror('The termination date must be greater than the start date',16,1);
		return ;
	end	
	
	--4. Determine how many payment records to enter in total according to the payment method
	set @field12 = rtrim(ltrim(@field12));--remove spaces
	declare @monthes int;
	declare @totalRent numeric(18,2);--total rent
	declare @type int;--payment type
	if (@ field12 ='with month') set @ type = 1;
	else if(@field12='季付') set @type=2;
	else if (@ field12 ='with half a year') set @ type = 3;
	else if(@field12='年付') set @type=4;
	
	--4, 1 Calculate the number of months between the start date and the end		
	set @monthes = datediff(month,@field39,@field40);--月份
	
	If @monthes=0 --indicates that the lease starts and ends in the current month/quarter/half a year/year, then you only need to directly calculate the number of days between the two
	begin
		set @totalRent = @day*@field66;--days*total price per day
		begin
			--Add payment record
			insert into table6  
			select field1, field2, field4, field5, field12, field39, @ totalRent, field42, field64, field11,0, NULL, 0, @ totalRent, @ totalRent, 0, field13, @ day, field40
			from table3 where field1=@id;
		end
	end
	else -- indicates at least one month
	begin
		declare @startTime datetime;--start date
		declare @endTime datetime;--end date
		declare @num int
		set @num=0;
		while(@num<=@monthes)	
		begin
			--print @num;
			set @startTime = dateadd(month,@num,@field39);--Calculate the date of payment for each month
			if(@type=1) set @num=@num+1;--monthly payment
			else if(@type=2) set @num=@num+3;--季付
			else if(@type=3) set @num=@num+6;--semi-annual payment
			else if(@type=4) set @num=@num+12;--年付

			set @endTime = dateadd(month,@num,@field39);--payment date of next month
			--The end date minus the date of the next month, if the end date is greater than the date of the next month, it means that another month needs to be paid, then the date of the next month is used, otherwise the end date is used
			set @day = datediff(day,@endTime,@field40);
			if(@day<0) set @endTime=@field40;
			-- Calculate the number of days between the start date and the end date
			set @day = datediff(day,@startTime,@endTime);
			set @totalRent = @day*@field66;--days*total price per day
			if(@totalRent>0)
			begin
				insert into table6  
				select field1, field2, field4, field5, field12, @ startTime, @ totalRent, field42, field64, field11,0, NULL, 0, @ totalRent, @ totalRent, 0, field13, @ day, @ endTime
				from table3 where field1=@id;
			end
		end		
	end

END
GO

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327058373&siteId=291194637