-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccountLoans.cs
134 lines (116 loc) · 3.29 KB
/
AccountLoans.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace LoanBook
{
public class AccountLoans : IEnumerable<Loan>
{
private Database database;
private Dictionary<long, Loan> loanList;
private Account account;
private bool includeClosedLoans = true;
private DateTime startDate, endDate;
public AccountLoans(Database database, Account account)
{
this.database = database;
this.account = account;
}
public DateTime StartDate
{
get { return startDate; }
set { startDate = value; }
}
public DateTime EndDate
{
get { return endDate; }
set { endDate = value; }
}
public bool IncludeClosedLoans
{
get { return includeClosedLoans; }
set { includeClosedLoans = value; }
}
public Account Account
{
get { return account; }
set { account = value; }
}
public DataTable Summary
{
get { return summary(); }
}
public Loan this[long id]
{
get { initializeListFromDatabase(); return loanList[id]; }
set { loanList[id] = value; }
}
IEnumerator<Loan> IEnumerable<Loan>.GetEnumerator()
{
initializeListFromDatabase();
return loanList.Values.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
initializeListFromDatabase();
return loanList.GetEnumerator();
}
private DataTable summary()
{
DatabaseView view = new DatabaseView(database);
view.StartDate = StartDate;
view.EndDate = EndDate;
return view.AccountLoans(account, IncludeClosedLoans);
}
private void initializeListFromDatabase()
{
if (loanList != null)
{
return;
}
loanList = new Dictionary<long, Loan>();
string condition;
Dictionary<string, object> parameters = new Dictionary<string, object>();
constructCondition(out condition, parameters);
string sql = string.Format("SELECT * FROM loans WHERE {0} ORDER BY start_date", condition);
DataTable results = this.database.Query(sql, parameters);
foreach (DataRow row in results.Rows)
{
Loan.LoanType type = (Loan.LoanType)int.Parse(row["type"].ToString());
Loan loan;
if (type == Loan.LoanType.AmortizedPayment)
{
loan = new AmortizedLoan(DatabaseFactory.Default);
loan.Term = int.Parse(row["term"].ToString());
}
else if (type == Loan.LoanType.InterestOnlyPayment)
{
loan = new PerpetualInterestLoan(DatabaseFactory.Default);
}
else
{
throw new ApplicationException("Invalid loan type found in database");
}
loan.Account = account;
loan.Id = long.Parse(row["id"].ToString());
loan.InterestRate = decimal.Parse(row["interest"].ToString());
loan.Principal = decimal.Parse(row["principal"].ToString());
loan.Remarks = row["remarks"].ToString();
loan.StartDate = DateTime.Parse(row["start_date"].ToString());
loan.TakenDate = DateTime.Parse(row["taken_date"].ToString());
loanList.Add(loan.Id, loan);
}
}
private void constructCondition(out string condition, Dictionary<string, object> parameters)
{
List<string> conditionList = new List<string>();
conditionList.Add("account_id = :accountId");
parameters[":accountId"] = account.Id;
if (!IncludeClosedLoans)
{
conditionList.Add("is_closed != 1");
}
condition = string.Join(" AND ", conditionList.ToArray());
}
}
}