forked from rotki/rotki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounting_settings.ts
169 lines (148 loc) · 6.09 KB
/
accounting_settings.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
import {showError, showInfo} from './utils';
import {form_button, form_entry, form_radio, form_select, page_header, settings_panel} from './elements';
import {settings} from './settings';
import {service} from './rotkehlchen_service';
function populate_ignored_assets() {
service.get_ignored_assets().then(assets => {
$('#ignored_assets_selection').append($('<option>', {
value: '',
text: 'Click to see all ignored assets and select one for removal'
}));
$.each(assets, (_, asset) => {
$('#ignored_assets_selection').append($('<option>', {
value: asset,
text: asset
}));
});
}).catch(() => {
showError('Error getting ignored assets');
});
}
function ignored_asset_modify_callback(event: JQuery.TriggeredEvent) {
event.preventDefault();
const button_type = $('#modify_ignored_asset_button').html();
const asset = $('#ignored_asset_entry').val() as string;
let add = true;
if (button_type === 'Remove') {
add = false;
}
service.modify_asset(add, asset).then(() => {
if (add) {
$('#ignored_assets_selection').append($('<option>', {
value: asset,
text: asset,
selected: true
}));
} else {
$(`#ignored_assets_selection option[value='${asset}']`).remove();
$('#modify_ignored_asset_button').html('Add');
$('#ignored_asset_entry').val('');
}
}).catch((reason: Error) => {
showError(
'Ignored Asset Modification Error',
`Error at modifying ignored asset ${asset} (${reason.message})`
);
});
}
function ignored_asset_selection_callback(event: JQuery.TriggeredEvent) {
const asset = $(event.target).val() as string;
$('#ignored_asset_entry').val(asset);
if (asset !== '') {
$('#modify_ignored_asset_button').html('Remove');
} else {
$('#modify_ignored_asset_button').html('Add');
}
}
function crypto2crypto_callback(event: JQuery.TriggeredEvent) {
const element = $(event.target);
const name = element.val();
const is_checked = element.prop('checked');
// return if a deselect triggered the event (may be unnecessary)
if (!is_checked) {
return;
}
// change class of div-box according to checked radio-box
let value = false;
if (name === 'Yes') {
value = true;
}
service.set_settings({'include_crypto2crypto': value}).then(() => {
showInfo('Success', 'Succesfully set crypto to crypto consideration value');
}).catch(reason => {
showError('Error setting crypto to crypto', reason.message);
});
}
function include_gas_costs_callback(event: JQuery.TriggeredEvent) {
const element = $(event.target);
const name = element.val();
const is_checked = element.prop('checked');
// return if a deselect triggered the event (may be unnecessary)
if (!is_checked) {
return;
}
// change class of div-box according to checked radio-box
let value = false;
if (name === 'Yes') {
value = true;
}
service.set_settings({'include_gas_costs': value}).then(() => {
showInfo('Success', 'Succesfully set Ethereum gas costs value');
}).catch(reason => {
showError('Error setting Ethereum gas costs: ', reason.message);
});
}
function modify_trade_settings_callback() {
const element = $('input[name=taxfree_period_exists]');
const is_checked = element.prop('checked');
// return if a deselect triggered the event (may be unnecessary)
let value = null;
if (is_checked) {
value = parseInt($('#taxfree_period_entry').val() as string, 10);
}
service.set_settings({'taxfree_after_period': value}).then(() => {
showInfo('Success', 'Succesfully set trade settings');
}).catch((reason: Error) => {
showError('Error setting trade settings', reason.message);
});
}
export function add_accounting_settings_listeners() {
$('#modify_ignored_asset_button').click(ignored_asset_modify_callback);
$('#ignored_assets_selection').change(ignored_asset_selection_callback);
$('input[name=crypto2crypto]').change(crypto2crypto_callback);
$('input[name=include_gas_costs]').change(include_gas_costs_callback);
$('#modify_trade_settings').click(modify_trade_settings_callback);
}
export function create_accounting_settings() {
let str = page_header('Accounting Settings');
str += settings_panel('Trade Settings', 'trades');
str += settings_panel('Asset Settings', 'assets');
$('#page-wrapper').html(str);
let starting_c2c = 'Yes';
if (!settings.include_crypto2crypto) {
starting_c2c = 'No';
}
let starting_include_gas_costs = 'Yes';
if (!settings.include_gas_costs) {
starting_include_gas_costs = 'No';
}
let starting_taxfree_after_period_choice = 'Yes';
let starting_taxfree_after_period = '';
if (!settings.taxfree_after_period) {
starting_taxfree_after_period_choice = 'No';
} else {
// get number of days of the setting
starting_taxfree_after_period = (settings.taxfree_after_period / 86400).toString();
}
str = form_radio('Take into account crypto to crypto trades', 'crypto2crypto', ['Yes', 'No'], starting_c2c);
str += form_radio('Take into account Ethereum gas costs', 'include_gas_costs', ['Yes', 'No'], starting_include_gas_costs);
str += form_radio('Is there a tax free period?', 'taxfree_period_exists', ['Yes', 'No'], starting_taxfree_after_period_choice);
str += form_entry('Tax free after how many days', 'taxfree_period_entry', starting_taxfree_after_period, '');
str += form_button('Set', 'modify_trade_settings');
$(str).appendTo($('#trades_panel_body'));
str = form_entry('Asset To Ignore', 'ignored_asset_entry', '', 'Assets to ignore during all accounting calculations');
str += form_select('Ignored Assets', 'ignored_assets_selection', [], '');
str += form_button('Add', 'modify_ignored_asset_button');
$(str).appendTo($('#assets_panel_body'));
populate_ignored_assets();
}