aboutsummaryrefslogtreecommitdiff
path: root/src/journal_lib/dataclasses.py
blob: 9c9729f794f517bcda9a2356f0fd5f590598246b (plain) (blame)
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
import sys
from copy import deepcopy
from dataclasses import dataclass, field
from decimal import Decimal
from datetime import datetime

FORCE_NOCOLOR = False
DATE_FORMAT = "%Y-%m-%d"


def set_force_nocolor(value: bool):
    global FORCE_NOCOLOR
    FORCE_NOCOLOR = value


def anesc(code: str):
    return "" if FORCE_NOCOLOR or not sys.stdout.isatty() else f"\u001b[{code}"


def format_amount(amount: str, currency: str | None = None) -> str:
    if currency is None:
        return amount
    if currency in ["NOK"]:
        return f"{amount} {currency}"
    if currency in ["$"]:
        return f"{currency}{amount}"
    return f"{amount} {currency}"


@dataclass
class JournalEntryTransaction:
    account: str
    currency: str | None
    amount: str | None
    comment: str | None
    sign: int = field(init=False)
    amount_value: int = field(init=False)

    def __post_init__(self):
        if self.amount is not None:
            if self.amount.startswith("-"):
                self.sign = -1
            else:
                self.sign = 1

            self.amount_value = Decimal(self.amount.lstrip("-+"))
        else:
            self.sign = 1

    def key(self):
        return (self.account, abs(Decimal(self.amount)), self.currency)


@dataclass
class JournalEntry:
    date: str
    cleared: bool
    pending: bool
    title: str
    effective_date: str | None
    transactions: list[JournalEntryTransaction]
    comments: list[str]

    def __str__(self):
        s = f"{anesc('34m')}{self.date}{anesc('0m')}"
        if self.effective_date is not None:
            s += f"={anesc('36m')}{self.effective_date}{anesc('0m')}"
        if self.cleared:
            s += f" {anesc('31m')}*{anesc('0m')}"
        if self.pending:
            s += f" {anesc('31m')}!{anesc('0m')}"
        s += f"  {anesc('36m')}{self.title}{anesc('0m')}\n"

        max_account_len = max(len(x.account) for x in self.transactions)
        for transaction in self.transactions:
            t = f"  {anesc('33m')}{transaction.account:{max_account_len}}{anesc('0m')}"
            if transaction.amount is not None:
                amount_code = "32m"
                if transaction.amount[0] == "-":
                    amount_code = "31m"
                else:
                    t += " "
                t += f"  {anesc(amount_code)}{format_amount(transaction.amount, transaction.currency)}{anesc('0m')}"
            if transaction.comment is not None:
                t += f"  {anesc('38m')}{transaction.comment}{anesc('0m')}"
            s += t + f"{anesc('0m')}\n"

        for comment in self.comments:
            s += f"  ; {anesc('38m')}{comment}{anesc('0m')}\n"
        return s + "\n"

    def __lt__(self, other):
        return self.date < other.date

    def get_comment_by_label(self, label: str):
        return next(
            (
                comment[len(label) + 2 :]
                for comment in self.comments
                if comment.startswith(label)
            ),
            None,
        )

    @property
    def from_dump_account(self):
        # Extract the first comment that starts with "FROM_DUMP_ACCOUNT", if any
        return self.get_comment_by_label("FROM_DUMP_ACCOUNT")

    @property
    def class_info(self):
        # Extract the first comment that starts with "FROM_DUMP_ACCOUNT", if any
        return self.get_comment_by_label("CLASS_INFO")

    def potential_transfer(self, other, date_skew=3):
        date_diff = abs((self.date - other.date).days)
        if date_diff > date_skew:
            return False
        return True

    def likely_transfer(self, other, date_skew=3):
        self_date = datetime.strptime(self.date, DATE_FORMAT)
        other_date = datetime.strptime(other.date, DATE_FORMAT)
        date_diff = abs((self_date - other_date).days)
        if date_diff > date_skew:
            return False

        # Check for inverse matching transactions
        for self_trans in self.transactions:
            inverse_match_found = False
            for other_trans in other.transactions:
                if (
                    self_trans.account != other_trans.account
                    and self_trans.amount_value == other_trans.amount_value
                    and self_trans.currency == other_trans.currency
                    and self_trans.sign != other_trans.sign
                ):
                    inverse_match_found = True
                    break

            if not inverse_match_found:
                return False

        if self.from_dump_account == other.from_dump_account:
            return False

        return True


@dataclass
class JournalAccountDef:
    account: str
    comment: str | None

    def __str__(self):
        s = f"{anesc('38m')}account {anesc('33m')}{self.account}{anesc('0m')}"
        if self.comment is not None:
            s += f" {anesc('38m')}{self.comment}{anesc('0m')}"
        return s + "\n"


@dataclass
class JournalCommodityDef:
    commodity: str
    format: str | None = None
    note: str | None = None
    nomarket: bool = False
    default: bool = False

    def __str__(self):
        s = f"{anesc('38m')}commodity {anesc('33m')}{self.commodity}{anesc('0m')}\n"
        for attr in ["format", "note", "nomarket", "default"]:
            if hasattr(self, attr) and getattr(self, attr) is not None:
                if isinstance(getattr(self, attr), bool) and not getattr(self, attr):
                    continue
                s += f"    {anesc('38m')}{attr}{anesc('0m')}"
                if isinstance(getattr(self, attr), str):
                    s += f" {anesc('36m')}{getattr(self, attr)}{anesc('0m')}"
                s += "\n"
        return s


@dataclass
class Journal:
    entries: list[JournalEntry]
    accounts: list[JournalAccountDef]
    commodities: list[JournalCommodityDef]

    def __str__(self):
        s = ""

        for account in self.accounts:
            s += f"{account}"
        s += "\n"

        for commodity in self.commodities:
            s += f"{commodity}"
        s += "\n"

        for entry in self.entries:
            s += f"{entry}"

        return s

    @staticmethod
    def from_elements(elements: list[str | JournalEntry]):
        entries = []
        accounts = []
        commodities = []

        for element in elements:
            if isinstance(element, JournalEntry):
                entries.append(element)
            elif isinstance(element, JournalAccountDef):
                accounts.append(element)
            elif isinstance(element, JournalCommodityDef):
                commodities.append(element)
            else:
                print(f"INVALID ELEMENT {element}")

        return Journal(entries=entries, accounts=accounts, commodities=commodities)