-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNextPaymentDueDate.sql
More file actions
77 lines (59 loc) · 2.5 KB
/
NextPaymentDueDate.sql
File metadata and controls
77 lines (59 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
-- NOTE: this could be just some of the many ways to reliably implement this particular requirement
-- https://community.nortridge.com/t/next-payment-due-date/1751
--
-- -----------------------------------------------------------------------------------------
-- from Alan
begin
declare
@countpaymentdue numeric(9,0),
@duedate datetime,
@loannumber varchar(25),
@termdays numeric(9,0)
set @loannumber = '<insert loan number>'
set @termdays = 30 --needs to come from the loan setup?
select @countpaymentdue = count(1) from loanacct_payments_due where acctrefno = (select acctrefno from loanacct where loan_number = @loannumber)
PRINT @countpaymentdue
if @countpaymentdue >0
begin
PRINT 'Greater than 0'
select @duedate=max(date_due) from loanacct_payments_due where acctrefno = (select acctrefno from loanacct where loan_number = @loannumber)
PRINT @duedate
end
if @countpaymentdue=0
begin
select @duedate = max(date_due) from loanacct_TRANS_history where transaction_code = 124 and acctrefno = (select acctrefno from loanacct where loan_number = @loannumber)
PRINT 'DUE DATE IS:'
PRINT DateAdd(day,@termdays,@duedate)
end
end
-- -----------------------------------------------------------------------------------------
-- from Jojo (version 1)
select
case when (select min(date_due) from loanacct_payments_due where acctrefno = 1) is null
then
(select next_principal_payment_date from loanacct_payment P where P.acctrefno = 1)
else
(select min(date_due) from loanacct_payments_due where acctrefno = 1)
end
-- from Jojo (version 2)
-- (based from Larry's version)
select L.loan_number, L.acctrefno,
isnull(
(select min(date_due) from loanacct_payments_due D1 where D1.acctrefno = L.acctrefno),
next_principal_payment_date
)
from loanacct L join loanacct_payment P on L.acctrefno = P.acctrefno
-- -----------------------------------------------------------------------------------------
-- from Larry
SELECT loan_number,
ISNULL((SELECT MIN(date_due)
FROM loanacct_payments_due (NOLOCK)
WHERE loanacct_payments_due.acctrefno = loanacct.acctrefno
AND payment_remaining > 0
AND payment_type LIKE 'Z%'),
CASE WHEN next_interest_payment_date < next_principal_payment_date THEN next_interest_payment_date
ELSE next_principal_payment_date
END) AS NextPaymentDueDate
FROM loanacct
INNER JOIN loanacct_payment ON loanacct_payment.acctrefno = loanacct.acctrefno
-- -----------------------------------------------------------------------------------------