-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtransaction.py
250 lines (229 loc) · 9.32 KB
/
transaction.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
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
# SPDX-FileCopyrightText: 2019–2023 Felix Gruber <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import annotations
from abc import ABCMeta, abstractmethod
from collections.abc import Callable, Iterable
from copy import copy
from collections import defaultdict
from datetime import date
from decimal import Decimal
from typing import Any, NamedTuple, Optional, TypeVar, Union
class BaseTransaction(metaclass=ABCMeta):
description: str
transaction_date: date
metadata: dict[str, Any]
@abstractmethod
def change_property(self,
prop: Union[str, Iterable[str]],
f: Callable[[BaseTransaction], Any],
) -> BaseTransaction: pass
@abstractmethod
def format_as_ledger_transaction(self) -> str: pass
@abstractmethod
def __repr__(self) -> str: pass
@property
def type(self) -> str:
try:
return self.metadata['type']
except KeyError:
raise AttributeError("'{}' object has no attribute 'type'"
.format(self.__class__.__name__))
class Transaction(BaseTransaction):
def __init__(self, account: str, description: str,
transaction_date: date, value_date: Optional[date],
amount: Decimal, currency: str = '€',
external_account: Optional[str] = None,
external_value_date: Optional[date] = None,
metadata: Optional[dict[str, Any]] = None):
self.account = account
self.description = description
self.transaction_date = transaction_date
self.value_date = value_date
self.external_value_date = external_value_date
self.amount = amount
self.currency = currency
self.external_account = external_account
if metadata is None:
metadata = {}
self.metadata = metadata
# TODO: Overload to handle different types of f
def change_property(self,
prop: Union[str, Iterable[str]],
f: Callable[[BaseTransaction], Any],
) -> Transaction:
res = copy(self)
const_properties = ('amount', 'currency', 'sub_total')
if isinstance(prop, str):
if prop in const_properties:
raise RuntimeError(f'Cannot change {prop} of a transaction')
setattr(res, prop, f(self))
else:
new_vals = f(self)
for p, v in zip(prop, new_vals):
if prop in const_properties:
raise RuntimeError(f'Cannot change {prop} of a transaction')
setattr(res, p, v)
return res
def to_multi_transaction(self) -> MultiTransaction:
mt = MultiTransaction(description=self.description,
transaction_date=self.transaction_date,
metadata=self.metadata)
mt.add_posting(Posting(self.account, self.amount, self.currency,
self.value_date))
mt.add_posting(Posting(self.external_account, -self.amount,
self.currency, self.external_value_date))
return mt
def format_as_ledger_transaction(self) -> str:
t = self
if '\n' in t.description:
raise RuntimeError(
'Transaction description contains unallowed'
f' character "\\n": {t.description!r}')
comment = t.metadata.get('comment', '')
if comment:
comment = ' ; ' + comment
result = f'{t.transaction_date} {t.description}{comment}\n'
block_comment = t.metadata.get('block_comment')
if block_comment is not None:
block_comment = '\n ; '.join(block_comment.split('\n'))
result += ' ; ' + block_comment + '\n'
if t.value_date is not None and t.value_date != t.transaction_date:
value_date = f' ; date:{t.value_date}'
else:
value_date = ''
result += f' {t.account} {t.amount} {t.currency}{value_date}\n'
ext_acc = t.external_account or 'TODO:assign_account'
if t.external_value_date is None:
ext_date = ''
else:
ext_date = f' ; date:{t.external_value_date}'
result += f' {ext_acc}{ext_date}\n'
return result
def __repr__(self) -> str:
s = self
if s.external_account is not None:
ext_account = f', external_account={s.external_account!r}'
else:
ext_account = ''
if s.external_value_date is not None:
ext_date = f', external_value_date={s.external_value_date!r}'
else:
ext_date = ''
if s.metadata:
meta = f', metadata={s.metadata!r}'
else:
meta = ''
return (f'Transaction(account={s.account!r}, '
f'description={s.description!r}, '
f'transaction_date={s.transaction_date!r}, '
f'value_date={s.value_date!r}, amount={s.amount!r}, '
f'currency={s.currency!r}'
f'{ext_account}{ext_date}{meta})')
class MultiTransaction(BaseTransaction):
def __init__(self, description: str, transaction_date: date,
postings: Optional[list[Posting]] = None,
metadata: Optional[dict[str, Any]] = None):
self.description = description
self.transaction_date = transaction_date
if postings is None:
self.postings = []
else:
self.postings = postings
if metadata is None:
metadata = {}
self.metadata = metadata
def add_posting(self, posting: Posting) -> None:
self.postings.append(posting)
# TODO: Overload to handle different types of f
def change_property(self,
prop: Union[str, Iterable[str]],
f: Callable[[MultiTransaction], Any],
) -> MultiTransaction:
res = copy(self)
if isinstance(prop, str):
setattr(res, prop, f(self))
else:
new_vals = f(self)
for p, v in zip(prop, new_vals):
setattr(res, p, v)
return res
def format_as_ledger_transaction(self) -> str:
t = self
assert('\n' not in t.description)
comment = t.metadata.get('comment', '')
if comment:
comment = ' ; ' + comment
result = f'{t.transaction_date} {t.description}{comment}\n'
block_comment = t.metadata.get('block_comment')
if block_comment is not None:
block_comment = '\n ; '.join(block_comment.split('\n'))
result += ' ; ' + block_comment + '\n'
result += ''.join(p.format_as_ledger_transaction(t.transaction_date)
for p in t.postings)
return result
def is_balanced(self) -> bool:
without_amount = 0
amounts: defaultdict[str, Decimal] = defaultdict(lambda: Decimal(0))
for p in self.postings:
if p.amount is None:
without_amount += 1
continue
amounts[p.currency] += p.amount
unbalanced_currencies = sum(1 for a in amounts.values() if a != 0)
return (unbalanced_currencies == 0 and without_amount == 0) \
or (unbalanced_currencies <= 1 and without_amount == 1)
def __repr__(self) -> str:
s = self
if s.metadata:
meta = f', metadata={s.metadata!r}'
else:
meta = ''
return (f'MultiTransaction({s.description!r}, {s.transaction_date!r},'
f' {s.postings!r}{meta})')
class Posting:
def __init__(self, account: Optional[str], amount: Decimal,
currency: str = '€', posting_date: Optional[date] = None,
comment: Optional[str] = None, *,
conversion_price: Optional[tuple[Decimal, str]] = None):
self.account = account
self.amount = amount
self.currency = currency
self.date = posting_date
self.comment = comment
self.conversion_price = conversion_price
def format_as_ledger_transaction(self, transaction_date: date) -> str:
t = self
account = t.account or 'TODO:assign_account'
if t.amount is None:
amount = ''
else:
amount = f'{t.amount} {t.currency}'
if t.conversion_price is not None:
amount += f' @@ {t.conversion_price[0]}' \
f' {t.conversion_price[1]}'
comments = []
if t.date is not None and t.date != transaction_date:
comments.append(f'date:{t.date}')
if t.comment is not None:
comments.append(t.comment)
if comments:
comments_str = ' ; ' + ', '.join(comments)
else:
comments_str = ''
return f' {account} {amount}{comments_str}\n'
def __repr__(self) -> str:
s = self
if s.date is not None:
date = f', posting_date={s.date!r}'
else:
date = ''
if s.comment is not None:
comment = f', comment={s.comment!r}'
else:
comment = ''
return (f'Posting({s.account!r}, {s.amount!r}, {s.currency!r}'
f'{date}{comment})')
class Balance(NamedTuple):
balance: Decimal
date: date