-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactionList.js
184 lines (173 loc) · 8.03 KB
/
transactionList.js
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
var transactionsCategories;
var startDate = "beginning";
var endDate = "end";
var total = 0;
function loadLargeTransactionArray() {
$.getJSON('./backend/get_transactions.php', function(data) {
total = 0;
var rawTransactions = data._embedded.transactions;
var transactions = [];
for (var i = 0; i < rawTransactions.length; i++) {
total += rawTransactions[i].amount;
transactions.push(loadSingleTransaction(rawTransactions[i]));
// console.log("1: " + i);
}
document.getElementById("transactionHeader").innerHTML = "<table width='100%'><tbody><tr><td><h1 style='display: float;'>Transactions</h1><td>(" + startDate + " to " + endDate + ")</td></td><td><h2 class='textRight'>Net Change in Balance: £" + total + "</h2></td></tr></tbody></table>";
document.getElementById("transactionList").innerHTML = "";
for (var i = 0; i < transactions.length; i++) {
document.getElementById("transactionList").innerHTML += transactions[i];
// console.log("2: " + i);
}
loadMasterCardData();
});
}
function displayTransactionCategories() {
if (document.getElementById("transactionList").innerHTML.indexOf("section") == -1) {
console.log("Started read");
loadTransactionCategories();
document.getElementById("transactionList").innerHTML = transactionsCategories;
console.log("ended read");
}
}
function loadTransactionCategories() {
transactionsCategories = "";
var largeTransactionArray = [];
var input = document.getElementById("transactionList").innerHTML;
while(String(input).length > -1) {
console.log(input);
largeTransactionArray.push(input.substring(0, input.indexOf("</div")+9));
console.log(input.substring(0, input.indexOf("</div>")+10));
input = input.substring(input.indexOf("</div>")+10);
}
console.log("Starting loop");
for (var i = 0; i < largeTransactionArray; i++) {
console.log(largeTransactionArray[i]);
}
console.log("ending loop");
var sections = [];
for (var t = 0; t < largeTransactionArray.length; t++) {
var transaction = largeTransactionArray[t];
var added = false;
for (var s = 0; s < sections.length; s++) {
if (sections[s].category.indexOf(transaction.substring(transaction.indexOf("<strong>Category: </strong>")+27, transaction.indexOf("<br id='flag' />"))) > -1) {
added = true;
sections[s].amount += parseInt(transaction.substring(transaction.indexOf("£")+1,transaction.indexOf("</h3>")));
sections[s].transactionsList += transaction;
}
break;
}
if (!added) {
var newSection = {
"category": transaction.substring(transaction.indexOf("<strong>Category: </strong>")+27, transaction.indexOf("<br id='flag' />")),
"amount": parseInt(transaction.substring(transaction.indexOf("£")+1,transaction.indexOf("</h3>"))),
"transactionsList": transaction,
"header1": "<div class='section'><table id='" + transaction.substring(transaction.indexOf("<strong>Category: </strong>")+27, transaction.indexOf("<br id='flag' />")) + "' onclick='toggleDetails(this.id)' width='100%'><tbody><tr><td><h2>" + transaction.substring(transaction.indexOf("<strong>Category: </strong>")+27, transaction.indexOf("<br id='flag' />")) + "</h2></td><td><h3 class='textRight'>",
"header2": "</h3></td></tr></tbody></table><div id='details_" + transaction.substring(transaction.indexOf("<strong>Category: </strong>")+27, transaction.indexOf("<br id='flag' />")) + "' style='display: none;'>"
}
sections.push(newSection);
}
}
for (var s = 0; s < sections.length; s++) {
transactionsCategories += sections[s].header1 + sections[s].amount + sections[s].header2 + sections[s].transactionsList + "</div></div>";
}
}
function loadSingleTransaction(transaction) {
var wrapperClass;
var moneyString;
if (transaction.direction.indexOf("OUT") > -1) {
wrapperClass = "outwrapper";
moneyString = String(transaction.amount).substring(1);
} else {
wrapperClass = "inwrapper";
moneyString = String(transaction.amount);
}
var newTransaction = "<div class='" + wrapperClass + "' id='" + transaction.id + "' onclick='toggleDetails(this.id)'>";
newTransaction += "<table width='100%'><tbody><tr><td><h2>" + transaction.narrative + " (";
newTransaction += transaction.created.substring(0,10) + ")</h2></td><td>";
newTransaction += "<h3 class='textRight'>£" + moneyString + "</h3></td></tr></tbody></table>";
newTransaction += "<div id='details_" + transaction.id + "' style='display: none;'>";
newTransaction += "<strong>Source: </strong>" + dataParse(transaction.source) + "<br />";
if (transaction.source.indexOf("MASTER") == -1) {
newTransaction += "<strong>Category: </strong>" + dataParse(transaction.source) + "<br id='flag' />";
}
newTransaction += "</div></div>";
return newTransaction;
}
function loadMasterCardData() {
$.getJSON('./backend/get_mastercardInfo.php', function(data) {
// console.log(data._embedded.transactions);
for (var i = 0; i < data._embedded.transactions.length; i++) {
var transaction = data._embedded.transactions[i];
var newTransaction = "<strong>Payment Method: </strong>" + dataParse(transaction.mastercardTransactionMethod) + "<br />";
newTransaction += "<strong>Category: </strong>" + dataParse(transaction.spendingCategory) + "<br id='flag' />";
newTransaction += "<strong>Country: </strong>" + transaction.country + "<br />";
newTransaction += "<strong>Address of Transaction: </strong><span class='address' id='address_" + transaction.id + "'>" + transaction.narrative + "</span><br />";
switch(transaction.narrative) { // We're not trying to cheat, just our sandbox account doesnt include merchant access
case "Target":
newTransaction += '<iframe style="width: 100%; height: 400px;" frameborder=0 src="https://wego.here.com/search/32001 John R Rd, Madison Heights, MI 48071, USA"></iframe>';
break;
case "Amc":
newTransaction += '<iframe style="width: 100%; height: 400px;" frameborder=0 src="https://wego.here.com/search/4300 Baldwin Rd, Auburn Hills, MI 48326"></iframe>';
break;
case "Pizza Hut":
newTransaction += '<iframe style="width: 100%; height: 400px;" frameborder=0 src="https://wego.here.com/search/1360 Walton Blvd, Rochester, MI 48309"></iframe>';
break;
case "Starbucks":
newTransaction += '<iframe style="width: 100%; height: 400px;" frameborder=0 src="https://wego.here.com/search/260 Adams Rd #260, Rochester Hills, MI 48309"></iframe>';
break;
case 'Amazon':
break;
default:
newTransaction += '<iframe style="width: 100%; height: 400px;" frameborder=0 src="https://wego.here.com/search/Detroit"></iframe>';
}
document.getElementById('details_' + transaction.id).innerHTML = newTransaction;
}
});
}
function toggleDetails(id) {
var currentState = document.getElementById("details_" + id).style.display;
if (currentState.indexOf("none") > -1) {
document.getElementById("details_" + id).style.display = "block";
} else {
document.getElementById("details_" + id).style.display = "none";
}
}
function dataParse(rawData) {
var output = "";
var i = rawData.indexOf("_");
while (true) {
if (i == -1) {
output += rawData.substring(0, 1) + rawData.substring(1, rawData.length).toLowerCase();
break;
}
output += rawData.substring(0, 1) + rawData.substring(1, i).toLowerCase() + " ";
rawData = rawData.substring(i+1);
i = rawData.indexOf("_");
}
return output;
}
function updateDateFilter() {
var tempStart = document.getElementById("startdate").value;
var tempEnd = document.getElementById("enddate").value;
if (tempStart.length == 0 || tempEnd.length == 0 || startDate.indexOf(tempStart) < 0 || endDate.indexOf(tempEnd) < 0) {
transactionsListOutput = null;
transactionsCategoriesOutput = null;
startDate = tempStart;
endDate = tempEnd;
if (startDate.length != 10) {
startDate = "beginning";
}
if (endDate.length != 10) {
endDate = "end";
}
if (document.getElementById("transactionList").innerHTML.indexOf("section") > -1) {
displayTransactionCategories();
} else {
displayTransactionList();
}
var output = "";
}
document.getElementById("startdate").value = "";
document.getElementById("enddate").value = "";
}
loadLargeTransactionArray();