forked from BittyTax/BittyTax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.py
217 lines (196 loc) · 7.55 KB
/
record.py
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
# -*- coding: utf-8 -*-
# (c) Nano Nano Ltd 2019
import sys
from .config import config
class TransactionRecord(object):
TYPE_DEPOSIT = 'Deposit'
TYPE_MINING = 'Mining'
TYPE_STAKING = 'Staking'
TYPE_INTEREST = 'Interest'
TYPE_DIVIDEND = 'Dividend'
TYPE_INCOME = 'Income'
TYPE_GIFT_RECEIVED = 'Gift-Received'
TYPE_AIRDROP = 'Airdrop'
TYPE_WITHDRAWAL = 'Withdrawal'
TYPE_SPEND = 'Spend'
TYPE_GIFT_SENT = 'Gift-Sent'
TYPE_GIFT_SPOUSE = 'Gift-Spouse'
TYPE_CHARITY_SENT = 'Charity-Sent'
TYPE_LOST = 'Lost'
TYPE_TRADE = 'Trade'
ALL_TYPES = (TYPE_DEPOSIT,
TYPE_MINING,
TYPE_STAKING,
TYPE_INCOME,
TYPE_INTEREST,
TYPE_DIVIDEND,
TYPE_GIFT_RECEIVED,
TYPE_AIRDROP,
TYPE_WITHDRAWAL,
TYPE_SPEND,
TYPE_GIFT_SENT,
TYPE_GIFT_SPOUSE,
TYPE_CHARITY_SENT,
TYPE_LOST,
TYPE_TRADE)
cnt = 0
def __init__(self, t_type, buy, sell, fee, wallet, timestamp, note):
self.tid = None
self.t_type = t_type
self.buy = buy
self.sell = sell
self.fee = fee
self.wallet = wallet
self.timestamp = timestamp
self.note = note
if self.buy:
self.buy.t_record = self
self.buy.timestamp = self.timestamp.astimezone(config.TZ_LOCAL)
self.buy.wallet = self.wallet
self.buy.note = self.note
if self.sell:
self.sell.t_record = self
self.sell.timestamp = self.timestamp.astimezone(config.TZ_LOCAL)
self.sell.wallet = self.wallet
self.sell.note = self.note
if self.fee:
self.fee.t_record = self
self.fee.timestamp = self.timestamp.astimezone(config.TZ_LOCAL)
self.fee.wallet = self.wallet
self.fee.note = self.note
def set_tid(self):
if self.tid is None:
TransactionRecord.cnt += 1
self.tid = [TransactionRecord.cnt, 0]
else:
self.tid[1] += 1
return list(self.tid)
def _format_fee(self):
if self.fee:
return " + fee=%s %s%s" % (
self._format_quantity(self.fee.quantity),
self.fee.asset,
self._format_value(self.fee.proceeds))
return ''
@staticmethod
def _format_quantity(quantity):
if quantity is None:
return ''
return '{:0,f}'.format(quantity.normalize())
@staticmethod
def _format_value(value):
if value is not None:
return " (%s %s)" % (
config.sym() + '{:0,.2f}'.format(value),
config.ccy)
return ''
@staticmethod
def _format_note(note):
if note:
if sys.version_info[0] < 3:
return "'%s' " % note.decode('utf8')
return "'%s' " % note
return ''
@staticmethod
def _format_timestamp(timestamp):
if timestamp.microsecond:
return timestamp.strftime('%Y-%m-%dT%H:%M:%S.%f %Z')
return timestamp.strftime('%Y-%m-%dT%H:%M:%S %Z')
@staticmethod
def _format_decimal(decimal):
if decimal is None:
return ''
return '{:0f}'.format(decimal.normalize())
@staticmethod
def _format_str(string):
if sys.version_info[0] < 3:
return string.decode('utf8')
return string
def __eq__(self, other):
return self.timestamp == other.timestamp
def __ne__(self, other):
return not self == other
def __lt__(self, other):
return self.timestamp < other.timestamp
def __str__(self):
if self.buy and self.sell:
return "%s %s %s%s <- %s %s%s%s '%s' %s %s[TID:%s]" % (
self.t_type,
self._format_quantity(self.buy.quantity),
self._format_str(self.buy.asset),
self._format_value(self.buy.cost),
self._format_quantity(self.sell.quantity),
self._format_str(self.sell.asset),
self._format_value(self.sell.proceeds),
self._format_fee(),
self._format_str(self.wallet),
self._format_timestamp(self.timestamp),
self._format_note(self.note),
self.tid[0])
if self.buy:
return "%s %s %s%s%s '%s' %s %s[TID:%s]" % (
self.t_type,
self._format_quantity(self.buy.quantity),
self._format_str(self.buy.asset),
self._format_value(self.buy.cost),
self._format_fee(),
self._format_str(self.wallet),
self._format_timestamp(self.timestamp),
self._format_note(self.note),
self.tid[0])
if self.sell:
return "%s %s %s%s%s '%s' %s %s[TID:%s]" % (
self.t_type,
self._format_quantity(self.sell.quantity),
self._format_str(self.sell.asset),
self._format_value(self.sell.proceeds),
self._format_fee(),
self._format_str(self.wallet),
self._format_timestamp(self.timestamp),
self._format_note(self.note),
self.tid[0])
return []
def to_csv(self):
if self.buy and self.sell:
return [self.t_type,
self._format_decimal(self.buy.quantity),
self._format_str(self.buy.asset),
self._format_decimal(self.buy.cost),
self._format_decimal(self.sell.quantity),
self._format_str(self.sell.asset),
self._format_decimal(self.sell.proceeds),
self._format_decimal(self.fee.quantity) if self.fee else '',
self._format_str(self.fee.asset) if self.fee else '',
self._format_decimal(self.fee.proceeds) if self.fee else '',
self._format_str(self.wallet),
self._format_timestamp(self.timestamp),
self._format_str(self.note)]
if self.buy:
return [self.t_type,
self._format_decimal(self.buy.quantity),
self._format_str(self.buy.asset),
self._format_decimal(self.buy.cost),
'',
'',
'',
self._format_decimal(self.fee.quantity) if self.fee else '',
self._format_str(self.fee.asset) if self.fee else '',
self._format_decimal(self.fee.proceeds) if self.fee else '',
self._format_str(self.wallet),
self._format_timestamp(self.timestamp),
self._format_str(self.note)]
if self.sell:
return [self.t_type,
'',
'',
'',
self._format_decimal(self.sell.quantity),
self._format_str(self.sell.asset),
self._format_decimal(self.sell.proceeds),
self._format_decimal(self.fee.quantity) if self.fee else '',
self._format_str(self.fee.asset) if self.fee else '',
self._format_decimal(self.fee.proceeds) if self.fee else '',
self._format_str(self.wallet),
self._format_timestamp(self.timestamp),
self._format_str(self.note)]
return []