forked from rotki/rotki
/
taxreport.ts
230 lines (214 loc) · 8.32 KB
/
taxreport.ts
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
import {form_button, form_entry, invisible_anchor, loading_placeholder, page_header, table_html} from './elements';
import {date_text_to_utc_ts, prompt_directory_select_async, showError, showInfo, showWarning, timestamp_to_date, utc_now} from './utils';
import {create_task, monitor_add_callback} from './monitor';
import {pages, settings} from './settings';
import {ActionResult} from './model/action-result';
import {TradeHistoryOverview, TradeHistoryResult} from './model/trade-history-result';
import {EventEntry} from './model/event-entry';
import {service} from './rotkehlchen_service';
let REPORT_END_TS = -1;
export function create_taxreport_ui() {
let str = page_header('Tax Report');
str += form_entry('Start Date', 'analysis_start_date');
str += form_entry('End Date', 'analysis_end_date');
str += form_button('Generate Report', 'generate_report');
str += invisible_anchor('tax_report_anchor');
str += table_html(2, 'report_overview');
str += table_html(11, 'report_details');
$('#page-wrapper').html(str);
}
export function add_taxreport_listeners() {
$('#analysis_start_date').datetimepicker({format: settings.datetime_format});
$('#analysis_end_date').datetimepicker({format: settings.datetime_format});
$('#generate_report').click(generate_report_callback);
$('#export_csv').click(export_csv_callback);
}
function clean_taxreport_ui() {
$('#export_csv').remove();
$('#report_overview_table_wrapper').parent().remove();
$('#report_details_table_wrapper').parent().remove();
let str = table_html(2, 'report_overview');
str += table_html(11, 'report_details');
$(str).insertAfter('#tax_report_anchor');
}
function export_csv_callback(event: JQuery.Event) {
event.preventDefault();
prompt_directory_select_async((directories: string[]) => {
if (directories === undefined) {
return;
}
const dir = directories[0];
service.export_processed_history_csv(dir).then(() => {
showInfo('Success', 'History exported to CVS successfully');
}).catch((reason: Error) => {
showError('Exporting History to CSV error', reason.message);
});
});
}
function generate_report_callback(event: JQuery.Event) {
event.preventDefault();
const start_ts_text = $('#analysis_start_date').val() as string;
const end_ts_text = $('#analysis_end_date').val() as string;
const start_ts = date_text_to_utc_ts(start_ts_text);
const end_ts = date_text_to_utc_ts(end_ts_text);
REPORT_END_TS = end_ts;
const now_ts = utc_now();
if (end_ts <= start_ts) {
showError('Input Error', 'The end time should be after the start time.');
return;
}
if (end_ts > now_ts) {
showError('Input Error', 'The end time should not be in the future.');
return;
}
if (($('#tax_report_loading').length)) {
showError(
'In Progress Error',
'A tax report is already being generated. Please wait until the ' +
'current report is finished until you can generate a new one'
);
return;
}
const str = loading_placeholder('tax_report_loading', '0%');
$(str).insertAfter('#tax_report_anchor');
clean_taxreport_ui();
service.process_trade_history_async(start_ts, end_ts).then(result => {
create_task(
result.task_id,
'process_trade_history',
'Create tax report',
false,
true
);
}).catch((reason: Error) => {
showError('Trade History Processing Error', reason.message);
});
}
export function update_tax_report_progress(start_ts: number, current_ts: number) {
if (!($('#tax_report_loading').length)) {
return;
}
const percentage = (current_ts - start_ts) * (1 / (REPORT_END_TS - start_ts));
const percentage_str = (percentage * 100).toFixed(2);
$('#tax_report_loading_extra_text').text(percentage_str + '%');
}
function show_float_or_empty(data: string) {
if (data === '') {
return '';
}
return parseFloat(data).toFixed(settings.floating_precision);
}
function create_taxreport_overview(results: TradeHistoryOverview) {
const data = [];
for (const result in results) {
if (results.hasOwnProperty(result)) {
const row = {'result': result, 'value': results[result]};
data.push(row);
}
}
const init_obj = {
'data': data,
'columns': [
{'data': 'result', 'title': 'Result'},
{
'data': 'value',
'title': settings.main_currency.ticker_symbol + ' value',
'render': (dat: string) => {
// no need for conversion here as it's already in main_currency
return parseFloat(dat).toFixed(settings.floating_precision);
}
}
],
'order': [[1, 'desc']]
};
$('#report_overview_table').DataTable(init_obj);
}
function create_taxreport_details(all_events: EventEntry[]) {
const init_obj = {
'data': all_events,
'columns': [
{'data': 'type', 'title': 'Type'},
{
'data': 'paid_in_profit_currency',
'title': 'Paid in ' + settings.main_currency.ticker_symbol,
'render': (data: string) => {
// it's already in main currency
return show_float_or_empty(data);
}
},
{'data': 'paid_asset', 'title': 'Paid Asset'},
{
'data': 'paid_in_asset',
'title': 'Paid In Asset',
'render': (data: string) => show_float_or_empty(data)
},
{
'data': 'taxable_amount',
'title': 'Taxable Amount',
'render': (data: string) => show_float_or_empty(data)
},
{
'data': 'taxable_bought_cost_in_profit_currency',
'title': 'Taxable Bought Cost in ' + settings.main_currency.ticker_symbol,
'render': (data: string) => show_float_or_empty(data)
},
{'data': 'received_asset', 'title': 'Received Asset'},
{
'data': 'received_in_asset',
'title': 'Received In Asset',
'render': (data: string) => {
return show_float_or_empty(data);
}
},
{
'data': 'taxable_received_in_profit_currency',
'title': 'Taxable Received in ' + settings.main_currency.ticker_symbol,
'render': (data: string) => {
// it's already in main currency
return show_float_or_empty(data);
}
},
{
'data': 'time',
'title': 'Time',
'render': (data: number, type: string) => {
if (type === 'sort') {
return data;
}
return timestamp_to_date(data);
}
},
{'data': 'is_virtual', 'title': 'Virtual ?'}
],
'pageLength': 25,
'order': [[9, 'asc']]
};
$('#report_details_table').DataTable(init_obj);
}
export function init_taxreport() {
monitor_add_callback('process_trade_history', (result: ActionResult<TradeHistoryResult>) => {
$('#tax_report_loading').remove();
if (result.error) {
showError(
'Trade History Query Error',
`Querying trade history died because of: ${result.error}. Check the logs for more details`
);
return;
}
if (result.message !== '') {
showWarning(
'Trade History Query Warning',
'During trade history query we got:' + result.message + '. History report is probably not complete.'
);
}
if ($('#elementId').length === 0) {
const str = form_button('Export CSV', 'export_csv');
$(str).insertAfter('#generate_report');
$('#export_csv').click(export_csv_callback);
}
create_taxreport_overview(result.result.overview);
create_taxreport_details(result.result.all_events);
// also save the page
pages.page_taxreport = $('#page-wrapper').html();
});
}