//EMI Month wise
CREATE PROCEDURE [dbo].[EMICALC]
@TotalAmount NUMERIC(18, 1),
@TotalMonth NUMERIC(18, 2),
@TotalEmi INT,
@DownPer NUMERIC(18, 2) ,
@TrnDate Date,
@Day int=0
AS
BEGIN
DECLARE @@TotalDays NUMERIC(18,0)
DECLARE @@EmiDays NUMERIC(18, 0)
SELECT @@TotalDays = Datediff(day,@TrnDate,DATEADD(DAY,Convert(int,RIGHT(@TotalMonth, LEN(@TotalMonth) - CHARINDEX('.', @TotalMonth)))*@TotalEmi ,DATEADD(MONTH, Convert(int,LEFT(@TotalMonth, CHARINDEX('.', @TotalMonth) -1)) ,@TrnDate)))
SELECT @@EmiDays = ( @@TotalDays / @TotalEmi )
print Convert(varchar,RIGHT(@TotalMonth, LEN(@TotalMonth) - CHARINDEX('.', @TotalMonth)))
DECLARE @@i INT = 1
DECLARE @@RemaintAmt NUMERIC(18, 2);
DECLARE @@Amt NUMERIC(18, 2);
SET @@RemaintAmt=@TotalAmount - ( @TotalAmount * @DownPer / 100 )
CREATE TABLE #temp
(
SrNo INT,
TrnDate DATETIME,
Per NUMERIC(18, 2),
Amount NUMERIC(18, 2)
)
--INSERT INTO #temp
if(@Day =0)
begin
INSERT INTO #temp
SELECT 1,
@TrnDate,
@DownPer,
@TotalAmount * @DownPer / 100
end
else
begin
INSERT INTO #temp
SELECT 1,
dateadd(day,@Day,@TrnDate),
@DownPer,
@TotalAmount * @DownPer / 100
end
IF( @TotalMonth%@TotalEmi = 0 )
BEGIN
WHILE @@i <= @TotalEmi
BEGIN
--SELECT Dateadd(month,(@TotalMonth/@TotalEmi)* @@i, @TrnDate) as [EMI]
INSERT INTO #temp
SELECT @@i + 1,
Dateadd(month, ( @TotalMonth / @TotalEmi ) * @@i,
dateadd(day,@Day,@TrnDate)
),
0,
0
SET @@i =@@i + 1
END
END
ELSE
BEGIN
WHILE @@i <= @TotalEmi
BEGIN
INSERT INTO #temp
SELECT @@i + 1,
Dateadd(day, (@@EmiDays-1) * @@i, @TrnDate),
0,
0
SET @@i =@@i + 1
END
END
UPDATE #temp
SET per = (100-@DownPer) / (SELECT Count(1)
FROM #temp t1
WHERE t1.srno <> 1)
WHERE srno <> 1
UPDATE #temp
SET
amount = @@RemaintAmt / (SELECT Count(1)
FROM #temp t1
WHERE t1.srno <> 1)
WHERE srno <> 1
select @@amt=sum(amount) from #temp where SrNo<=@TotalEmi
update #temp set Amount = @TotalAmount - @@amt where SrNo = @TotalEmi + 1
SELECT *
FROM #temp
END
=====================================================================
===> exec EMICALC 10000,30,30,25,'01/01/2016',0
=====================================================================
==> O/P
SrNo TrnDate Per Amount
1 2016-01-01 25.00 2500.00
2 2016-02-01 2.50 250.00
3 2016-03-01 2.50 250.00
4 2016-04-01 2.50 250.00
5 2016-05-01 2.50 250.00
6 2016-06-01 2.50 250.00
7 2016-07-01 2.50 250.00
8 2016-08-01 2.50 250.00
9 2016-09-01 2.50 250.00
10 2016-10-01 2.50 250.00
11 2016-11-01 2.50 250.00
12 2016-12-01 2.50 250.00
13 2017-01-01 2.50 250.00
14 2017-02-01 2.50 250.00
15 2017-03-01 2.50 250.00
16 2017-04-01 2.50 250.00
17 2017-05-01 2.50 250.00
18 2017-06-01 2.50 250.00
19 2017-07-01 2.50 250.00
20 2017-08-01 2.50 250.00
21 2017-09-01 2.50 250.00
22 2017-10-01 2.50 250.00
23 2017-11-01 2.50 250.00
24 2017-12-01 2.50 250.00
25 2018-01-01 2.50 250.00
26 2018-02-01 2.50 250.00
27 2018-03-01 2.50 250.00
28 2018-04-01 2.50 250.00
29 2018-05-01 2.50 250.00
30 2018-06-01 2.50 250.00
31 2018-07-01 2.50 250.00
====================================
31 100% 10000