-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool.02.report.html
338 lines (291 loc) · 7.79 KB
/
tool.02.report.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<html>
<body>
<script src="js/csv.js"></script>
year: <input id="inputYear" type="text"><br>
income.csv : <input type="file" onchange="incomeFileSelected(this.files)"><br>
input.csv : <input type="file" onchange="inputFileSelected(this.files)"><br>
output.csv : <input type="file" onchange="outputFileSelected(this.files)"><br>
<button id="generateButton" onclick="generate()" disabled>Generate</button><br>
<a id="taxesCSVLink"></a><br>
<a id="taxesTXFLink"></a><br>
<a id="taxesTXTLink"></a><br>
<pre id="incomeText"></div>
<script>
var inputYear = document.getElementById('inputYear');
var taxesCSVLink = document.getElementById('taxesCSVLink');
var taxesTXFLink = document.getElementById('taxesTXFLink');
var taxesTXTLink = document.getElementById('taxesTXTLink');
var incomeText = document.getElementById('incomeText');
inputYear.value = new Date().getFullYear()-1;
var inputCSVFile = null;
var outputCSVFile = null;
var incomeCSVFile = null;
function inputFileSelected(files)
{
if (files.length != 1)
{
inputCSVFile = null;
generateButton.disabled = true;
}
inputCSVFile = files[0];
if (outputCSVFile)
{
generateButton.disabled = false;
}
}
function outputFileSelected(files)
{
if (files.length != 1)
{
outputCSVFile = null;
generateButton.disabled = true;
}
outputCSVFile = files[0];
if (outputCSVFile)
{
generateButton.disabled = false;
}
}
function incomeFileSelected(files)
{
if (files.length != 1)
{
incomeCSVFile = null;
generateButton.disabled = true;
}
incomeCSVFile = files[0];
if (incomeCSVFile)
{
generateButton.disabled = false;
}
}
var inputCSV = null;
var outputCSV = null;
var incomeCSV = null;
function filesLoaded(year)
{
var tsStart = (new Date(year + '-01-01')).getTime() / 1000;
var tsEnd = (new Date((year + 1) + '-01-01')).getTime() / 1000;
var taxes = generateTaxes(inputCSV, outputCSV, tsStart, tsEnd);
var income = accumulateIncome(incomeCSV, tsStart, tsEnd);
var fileBaseName = 'taxes_' + year;
taxesCSVLink.href = 'data:text/csv;charset=UTF-8,' + encodeURIComponent(CSV.stringify(taxes.taxes));
taxesCSVLink.download = fileBaseName + '.csv'
taxesCSVLink.textContent = fileBaseName + '.csv'
taxesTXFLink.href = 'data:text/txf;charset=UTF-8,' + encodeURIComponent(taxes.txf);
taxesTXFLink.download = fileBaseName + '.txf'
taxesTXFLink.textContent = fileBaseName + '.txf'
var taxText =
'Income (USD): ' + income.USD
+ '\r\nIncome (BTC): ' + income.BTC
+ '\r\n\r\nShort Term Gains:'
+ '\r\nTop Line: ' + taxes.shortterm.price
+ '\r\nCost (basis): ' + taxes.shortterm.cost
+ '\r\nBottom Line (gains): ' + taxes.shortterm.gain
+ '\r\n\r\nLong Term Gains:'
+ '\r\nTop Line: ' + taxes.longterm.price
+ '\r\nCost (basis): ' + taxes.longterm.cost
+ '\r\nBottom Line (gains): ' + taxes.longterm.gain
taxesTXTLink.href = 'data:text/txt;charset=UTF-8,' + encodeURIComponent(taxText);
taxesTXTLink.download = fileBaseName + '.txt'
taxesTXTLink.textContent = fileBaseName + '.txt'
incomeText.innerHTML = taxText;
}
function generate()
{
var year = parseInt(inputYear.value);
if (isNaN(year))
{
alert('year is not a year');
return;
}
var reader = new FileReader();
reader.onload = function ()
{
inputCSV = CSV.parse(this.result);
var reader = new FileReader();
reader.onload = function ()
{
outputCSV = CSV.parse(this.result);
var reader = new FileReader();
reader.onload = function ()
{
incomeCSV = CSV.parse(this.result);
filesLoaded(year);
}
reader.readAsText(incomeCSVFile);
}
reader.readAsText(outputCSVFile);
}
reader.readAsText(inputCSVFile);
}
// gets transactions for btc range, used for getting rates of those btc.
// input - is a list of input transactions
// start - is the first btc
// end - is the last btc
function getBTC(input, start, end)
{
//console.log('getBTC ' + start + ' ' + end);
var btc = 0;
// find start
var sbtc = 0;
var si;
for (si = 0; si < input.length; si++)
{
var txbtc = parseFloat(input[si][1]);
if (btc + txbtc > start)
{
sbtc = start - btc;
break;
}
btc += txbtc;
}
// find end
var ebtc = 0;
var ei;
for (ei = si; ei < input.length; ei++)
{
var txbtc = parseFloat(input[ei][1]);
if (btc + txbtc > end)
{
ebtc = end - btc;
//console.log(' ' + ei + ' ' + ebtc);
break;
}
btc += txbtc;
//console.log(' ' + ei + ' ' + btc);
}
//console.log(' ' + ei + ' ' + ebtc + ' , ' + si + ' ' + sbtc);
if (ei == si)
{
return [[input[si][0], end - start, input[si][2], input[si][3], input[si][4]]];
}
var tx = [];
tx.push([input[si][0], parseFloat(input[si][1]) - sbtc, input[si][2], input[si][3], input[si][4]]);
for (var i = si+1; i < ei; i++)
{
tx.push([input[i][0], parseFloat(input[i][1]), input[i][2], input[i][3], input[i][4]]);
}
tx.push([input[ei][0], ebtc, input[ei][2], input[si][3], input[ei][4]]);
return tx;
}
// round to cent.
function r2f(v) { return (Math.round(v*100)/100).toFixed(2); }
// format a date for TXF files
function txfDate(d)
{
d = d.toISOString().split('T')[0].split('-');
return d[1] + '/' + d[2] + '/' + d[0];
}
function generateTaxes(input, output, tsStart, tsEnd)
{
var taxes = [['description','code','date acquired', 'date sold', 'sales price', 'cost', 'adjustment', 'gain']];
var txf = 'V042\r\nABitcoinTaxes\r\nD' + txfDate(new Date()) + '\r\n^\r\n';
var longterm = {
price : 0,
cost : 0,
gain : 0
}
var shortterm = {
price : 0,
cost : 0,
gain : 0
}
var btc = 0;
for (var i = 0; i < output.length; i++)
{
var obtc = -parseFloat(output[i][1]);
var btcStart = btc;
btc += obtc;
var tsSold = parseInt(output[i][0]);
if (tsSold < tsStart || tsSold >= tsEnd)
{
continue;
}
var tx = getBTC(input, btcStart, btcStart + obtc);
for (var j = 0; j < tx.length; j++)
{
var price = r2f(tx[j][1] * output[i][2]);
var cost = r2f(tx[j][1] * tx[j][2]);
var gain = r2f(price - cost);
var tsAcquired = parseInt(tx[j][0]);
var dateAcquired = new Date(tsAcquired * 1000);
var dateSold = new Date(tsSold * 1000);
var date1 = txfDate(dateAcquired);
var date2 = txfDate(dateSold);
taxes.push([tx[j][1].toFixed(8) + ' BTC', '', date1, date2, price, cost, '', gain]);
if (tsSold > tsYear)
{
longterm.price += parseFloat(price);
longterm.cost += parseFloat(cost);
longterm.gain += parseFloat(gain);
}
else
{
shortterm.price += parseFloat(price);
shortterm.cost += parseFloat(cost);
shortterm.gain += parseFloat(gain);
}
// separate ISO string to [year, month, date and time zone]
var yymmdd = dateAcquired.toISOString().split('-');
// add a year
yymmdd[0] = parseInt(yymmdd[0]) + 1;
// recombine the ISO string, get the timestamp, and add a day.
var tsYear = (new Date(yymmdd.join('-'))).getTime()/1000 + 24*60*60;
txf += 'TD\r\n';
if (tsSold > tsYear)
{
txf += 'N714\r\n';
}
else
{
txf += 'N712\r\n';
}
txf += 'C1\r\n';
txf += 'L1\r\n';
txf += 'P' + tx[j][1].toFixed(8) + ' BTC\r\n';
txf += 'D' + date1 + '\r\n';
txf += 'D' + date2 + '\r\n';
txf += '$' + cost +'\r\n';
txf += '$' + price + '\r\n';
txf += '^\r\n';
}
}
longterm.price = r2f(longterm.price);
longterm.cost = r2f(longterm.cost);
longterm.gain = r2f(longterm.gain);
shortterm.price = r2f(shortterm.price);
shortterm.cost = r2f(shortterm.cost);
shortterm.gain = r2f(shortterm.gain);
return {
taxes : taxes,
txf : txf,
longterm : longterm,
shortterm : shortterm,
}
}
function accumulateIncome(income, tsStart, tsEnd)
{
var incomeBTC = 0;
var incomeUSD = 0;
for (var i = 0; i < income.length; i++)
{
var tx = income[i];
var tsSold = parseInt(tx[0]);
if (tsSold < tsStart || tsSold >= tsEnd)
{
continue;
}
var btc = parseFloat(tx[1]);
var usd = parseFloat(tx[2]);
incomeBTC += btc
incomeUSD += btc * usd;
}
return {
BTC : incomeBTC,
USD : r2f(incomeUSD),
}
}
</script>
</body>
</html>