/
Loan.cs
262 lines (226 loc) · 6.07 KB
/
Loan.cs
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace LoanBook
{
public abstract class Loan
{
public enum LoanType
{
AmortizedPayment = 1,
InterestOnlyPayment = 2
}
private long id = 0;
private Account account;
private LoanType type = LoanType.AmortizedPayment;
private decimal defaultProcessingFeePercentage = 2;
private decimal principal, interest, processingFee = 0, processingFeePercentage, reloanFeePercentage = 1;
private int term;
private bool isReloan;
private string remarks;
private DateTime startDate, takenDate;
protected Database database;
public long Id
{
get { return id; }
set { id = value; }
}
public Account Account
{
get { return account; }
set { account = value; }
}
public LoanType Type
{
get { return type; }
set { type = value; }
}
public decimal Principal
{
get { return principal; }
set { principal = value; }
}
public decimal InterestRate
{
get { return interest; }
set { if (value >= 0) { interest = value; } }
}
public decimal ProcessingFeePercentage
{
get
{
if (processingFeePercentage == 0 && processingFee != 0)
{
return LoanCalculator.ProcessingFeePercentage(Principal, processingFee);
}
return defaultProcessingFeePercentage;
}
set { processingFeePercentage = value; }
}
public decimal ReloanFeePercentage
{
get { return reloanFeePercentage; }
set { reloanFeePercentage = value; }
}
public bool IsReloan
{
get { return isReloan; }
set { isReloan = value; }
}
public abstract decimal MonthlyDue { get; }
public abstract decimal TotalPayable { get; }
public abstract decimal Balance { get; }
public abstract decimal TotalPayments { get; }
public abstract int PaymentCount { get; }
public int Term
{
get { return term; }
set { term = value; }
}
public DateTime TakenDate
{
get { return takenDate; }
set { takenDate = value; }
}
public DateTime StartDate
{
get { return startDate; }
set { startDate = value; }
}
public decimal ProcessingFee
{
get
{
if (processingFee == 0)
{
return LoanCalculator.ProcessingFee(Principal, ProcessingFeePercentage);
}
return processingFee;
}
set { processingFee = value; }
}
public decimal ReloanFee
{
get
{
if (IsReloan)
{
return LoanCalculator.ReloanFee(Principal, Term - MonthsPaid);
}
return 0;
}
}
public decimal ReleaseAmount
{
get
{
if (Principal > 0)
{
return LoanCalculator.ReleaseAmount(Principal, ProcessingFeePercentage);
}
return 0;
}
}
public decimal MonthsPaid
{
get { return monthsPaid(); }
}
public string Remarks
{
get { return remarks; }
set { remarks = value; }
}
public Loan(Database database)
{
this.database = database;
}
public void MarkClosed()
{
string sql = "UPDATE loans SET is_closed=1, remarks=(CASE remarks IS NULL WHEN 1 THEN 'Closed' ELSE 'Closed. ' || remarks END) WHERE id=:id";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters[":id"] = Id;
database.Execute(sql, parameters);
}
public void Save()
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
string sql;
if (id == 0)
{
sql = "INSERT INTO loans (account_id, type, principal, term, interest, monthly_due, start_date, taken_date, processing_fee, reloan_fee, total_payable, remarks) "
+ " VALUES (:accountId, :type, :principal, :term, :interest, :monthlyDue, :startDate, :takenDate, :processingFee, :reloanFee, :totalPayable, :remarks)";
parameters[":accountId"] = account.Id;
}
else
{
sql = "UPDATE loans SET type=:type, principal=:principal, term=:term, "
+ "interest=:interest, monthly_due=:monthlyDue, taken_date=:takenDate, "
+ "start_date=:startDate, processing_fee=:processingFee, reloan_fee=:reloanFee, "
+ "total_payable=:totalPayable, remarks=:remarks "
+ "WHERE id=:id";
parameters[":id"] = Id;
}
parameters[":type"] = Type;
parameters[":principal"] = Principal;
parameters[":term"] = Term;
parameters[":interest"] = InterestRate;
parameters[":monthlyDue"] = MonthlyDue;
parameters[":processingFee"] = ProcessingFee;
parameters[":reloanFee"] = ReloanFee;
parameters[":totalPayable"] = TotalPayable;
parameters[":remarks"] = Remarks;
parameters[":startDate"] = StartDate;
parameters[":takenDate"] = TakenDate;
database.Execute(sql, parameters);
}
public virtual void Load()
{
string sql = "SELECT * FROM loans WHERE id=:id";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters[":id"] = Id;
DataTable result = database.Query(sql, parameters);
if (result.Rows.Count == 1)
{
DataRow row = result.Rows[0];
Principal = (decimal)row["principal"];
Type = (Loan.LoanType)int.Parse(row["type"].ToString());
if (Type == LoanType.AmortizedPayment)
{
Term = int.Parse(row["term"].ToString());
}
InterestRate = (decimal)row["interest"];
TakenDate = (DateTime)row["taken_date"];
StartDate = (DateTime)row["start_date"];
processingFee = (decimal)row["processing_fee"];
//reloanFee = (decimal)row["reloan_fee"];
if (row["remarks"].GetType() != typeof(System.DBNull))
{
Remarks = (string)row["remarks"];
}
else
{
Remarks = "";
}
Account loanAccount = new Account();
loanAccount.Id = (long)row["account_id"];
loanAccount.Load(database);
Account = loanAccount;
return;
}
throw new ApplicationException("Unable to load loan");
}
public override string ToString()
{
return Principal + " At " + InterestRate + "%";
}
private decimal monthsPaid()
{
string sql = "SELECT TOTAL(balance_deductible) AS total_payments FROM payments WHERE loan_id=:id";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters[":id"] = Id;
decimal totalPayments = (decimal)(double)database.QueryScalar(sql, parameters);
return totalPayments / MonthlyDue;
}
}
}