5050 Income:Net:Bonus 100.00 EUR
5151"""
5252
53+ from __future__ import annotations
54+
5355import ast
5456import collections
5557import copy
5658import re
59+ from decimal import Decimal
60+ from typing import Any
61+ from typing import TYPE_CHECKING
5762
5863from beancount .core import convert
5964from beancount .core import data
6065from beancount .core import getters
6166from beancount .core .inventory import Inventory
62- from beancount .core .number import Decimal
63- from beancount .core .number import ZERO
67+
68+ if TYPE_CHECKING :
69+ from beancount .core .data import Directive
6470
6571__plugins__ = ("split_income" ,)
6672
6773SplitIncomeError = collections .namedtuple (
68- "SplitIncomeError" , "source message entry"
74+ "SplitIncomeError" ,
75+ "source message entry" ,
6976)
7077
78+ ZERO = Decimal ()
79+
7180
72- def split_income (entries , options_map , config_str ):
81+ def split_income (
82+ entries : list [Directive ], options_map : Any , config_str : str
83+ ) -> tuple [list [Directive ], list [SplitIncomeError ]]:
7384 """Split income transactions."""
74- # pylint: disable=too-many-locals
7585
7686 errors = []
77- new_entries = []
87+ new_entries : list [ Directive ] = []
7888 new_accounts = set ()
7989 config = {
8090 "income" : options_map ["name_income" ],
@@ -93,7 +103,7 @@ def split_income(entries, options_map, config_str):
93103 data .new_metadata (options_map ["filename" ], 0 ),
94104 f"Syntax error in config: { config_str } " ,
95105 None ,
96- )
106+ ),
97107 )
98108 return entries , errors
99109
@@ -107,14 +117,15 @@ def split_income(entries, options_map, config_str):
107117 # The new entry containing the raw income and taxes.
108118 new_entry = copy .deepcopy (entry )
109119 new_entry = new_entry ._replace (
110- postings = [], tags = frozenset ({config ["tag" ]} | entry .tags )
120+ postings = [],
121+ tags = frozenset ({config ["tag" ]} | entry .tags ),
111122 )
112123 new_entries .append (new_entry )
113124
114- income = collections .defaultdict (Inventory )
115- taxes = collections .defaultdict (Decimal )
125+ income : dict [ str , Inventory ] = collections .defaultdict (Inventory )
126+ taxes : dict [ str , Decimal ] = collections .defaultdict (Decimal )
116127 for posting in list (entry .postings ):
117- weight = convert .get_weight (posting )
128+ weight = convert .get_weight (posting ) # type: ignore[no-untyped-call]
118129 if posting .account .startswith (config ["income" ]):
119130 new_entry .postings .append (posting )
120131 entry .postings .remove (posting )
@@ -126,7 +137,8 @@ def split_income(entries, options_map, config_str):
126137
127138 for account , inv in income .items ():
128139 net_account = account .replace (
129- config ["income" ], config ["net_income" ]
140+ config ["income" ],
141+ config ["net_income" ],
130142 )
131143 if net_account not in new_accounts :
132144 new_accounts .add (net_account )
@@ -135,19 +147,25 @@ def split_income(entries, options_map, config_str):
135147 data .new_metadata ("<split_income>" , 0 ),
136148 entry .date ,
137149 net_account ,
150+ [],
138151 None ,
139- None ,
140- )
152+ ),
141153 )
142154
143155 for pos in inv :
144156 amount = pos .units
145157 number = amount .number + taxes .pop (amount .currency , ZERO )
146158 data .create_simple_posting (
147- entry , net_account , number , amount .currency
159+ entry ,
160+ net_account ,
161+ number ,
162+ amount .currency ,
148163 )
149164 data .create_simple_posting (
150- new_entry , net_account , - number , amount .currency
165+ new_entry ,
166+ net_account ,
167+ - number ,
168+ amount .currency ,
151169 )
152170
153171 return entries + new_entries , errors
0 commit comments