-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
202 lines (162 loc) · 7.7 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Expense Parser</title>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script type="text/javascript" charset="utf8"
src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.js"></script>
<style>
body,
html {
font-family: Arial, sans-serif;
}
tr.done,
tr.notMine {
background-color: #f0f0f0;
color: #ccc;
text-decoration: line-through;
}
</style>
</head>
<body>
<h2>Expense Parser</h2>
<button onclick="window.localStorage.clear(); window.location.reload();">Clear Data</button>
<h4 style="margin-top: 2rem;">Paste in data from My Wallet</h4>
<label>Enter your initials:</label>
<input type="text" id="initials" style="width: 10em;">
<br><br>
<textarea id="inputText" style="width: 100%; height: 8em;" placeholder="Paste your expenses here..."></textarea>
<br>
<button onclick="parseAndDisplay()">Parse Expenses</button>
<br>
<h4 style="margin-top: 2em;">Text table for pasting into Excel</h4>
<textarea id="outputText" style="width: 100%; height: 8em;" readonly></textarea>
<h4 style="margin-top: 2em;">Interactive table</h4>
<div id="expensesTable">
<table id="dataTable" class="display" style="width:100%;"></table>
</div>
<script src="papaparse.min.js"></script>
<script>
const formatDate = (date) => {
// Extracting year, month, and day as individual components
const year = date.getFullYear();
const month = date.getMonth() + 1; // getMonth() returns 0-11
const day = date.getDate();
// Padding single digit month and day values with leading zero
const monthFormatted = month.toString().padStart(2, '0');
const dayFormatted = day.toString().padStart(2, '0');
// Concatenating components in 'YYYYMMDD' format
const formattedDate = `${year}${monthFormatted}${dayFormatted}`;
return formattedDate;
};
const parseTextToTable = (input) => {
let initials = document.getElementById('initials').value.toUpperCase();
initials = initials.length == 0 ? '' : initials + ' ';
const lines = input.split('\n').filter(line => line.trim() !== '');
const table = [];
let currentDate;
window.lines = lines;
for (let i = 0; i < lines.length; i++) {
if (lines[i].match(/^\w+day,/)) {
currentDate = new Date(lines[i]);
continue;
}
if (!lines[i + 1].match(/^[\-\d,]+\.\d{2} CAD/)) {
const category = lines[i];
const expenseName = lines[++i];
const amounts = lines[++i].match(/([\-\d,]+\.\d{2}) CAD([\-\d,]+\.\d{2})? ([A-Z]{3})?/);
let amountInCAD, amountInOriginalCurrency;
if (!amounts) {
console.error('Error parsing line ' + i, lines[i-1], lines[i], lines[i+1]);
} else {
amountInCAD = amounts[1].replace(/,/g, '');
amountInOriginalCurrency = (amounts[3] ? `${amounts[2]} ${amounts[3]}` : '').replace(/,/g, '');
}
const dateFormatted = formatDate(currentDate);
const filename = `${initials}${dateFormatted} ${expenseName.replace(/[^a-zA-Z0-9.\-_]/g, '')} (${amountInCAD}).pdf`;
table.push({
date: currentDate.toLocaleDateString('en-CA'),
category,
expenseName,
amountInCAD,
amountInOriginalCurrency,
filename
});
}
}
return table;
};
const parseAndDisplay = () => {
const inputText = document.getElementById('inputText').value;
const outputText = document.getElementById('outputText');
const tableData = parseTextToTable(inputText);
outputText.value = Papa.unparse(tableData, { delimiter: '\t' });
window.dt.clear().rows.add(tableData);
window.dt.columns.adjust().draw();
window.localStorage.setItem('inputText', inputText);
window.localStorage.setItem('name', document.getElementById('initials').value);
};
window.dt = $('#dataTable').DataTable({
columns: [
{ title: "Done", render: () => '<input type="checkbox" name="done">', orderable: false },
{ title: "Not Mine", render: () => '<input type="checkbox" name="notMine">', orderable: false },
{ title: "Expense Date", data: 'date' },
{ title: "Category", data: 'category' },
{ title: "Expense Name", data: 'expenseName' },
{ title: "Amount in CAD", data: 'amountInCAD' },
{ title: "Amount in Original Currency", data: 'amountInOriginalCurrency' },
{ title: "Filename", data: 'filename' }
],
columnDefs: [
{ targets: [0, 1], className: 'dt-body-center' }
],
order: [[2, 'asc']],
paging: false,
});
$('#dataTable').on('change', 'input[name="done"]', function () {
const tr = $(this).closest('tr');
if (this.checked) {
tr.addClass('done');
} else {
tr.removeClass('done');
}
// get all checkbox states and datatable row indexes
const allChecked = $('#dataTable input[name="done"]').toArray().map(cb => ({ i: window.dt.row($(cb).closest('tr')).index(), c: cb.checked }));
window.localStorage.setItem('done', JSON.stringify(allChecked));
});
$('#dataTable').on('change', 'input[name="notMine"]', function () {
const tr = $(this).closest('tr');
if (this.checked) {
tr.addClass('notMine');
} else {
tr.removeClass('notMine');
}
// get all checkbox states and datatable row indexes
const allChecked = $('#dataTable input[name="notMine"]').toArray().map(cb => ({ i: window.dt.row($(cb).closest('tr')).index(), c: cb.checked }));
window.localStorage.setItem('notMine', JSON.stringify(allChecked));
});
const inputText = window.localStorage.getItem('inputText');
if (inputText) {
document.getElementById('inputText').value = inputText;
document.getElementById('initials').value = window.localStorage.getItem('name');
parseAndDisplay();
// restore checkbox state
const done = JSON.parse(window.localStorage.getItem('done'));
if (done) {
done.forEach(element => {
window.dt.row(element.i).nodes().to$().find('input[name="done"]').prop('checked', element.c).trigger('change');
});
}
const notMine = JSON.parse(window.localStorage.getItem('notMine'));
if (notMine) {
notMine.forEach(element => {
window.dt.row(element.i).nodes().to$().find('input[name="notMine"]').prop('checked', element.c).trigger('change');
});
}
}
</script>
</body>
</html>