aboutsummaryrefslogtreecommitdiff
path: root/utils/balance.py
diff options
context:
space:
mode:
authorAdrián Oliva <adrian.oliva@cimat.mx>2023-05-07 17:49:43 -0600
committerAdrián Oliva <adrian.oliva@cimat.mx>2023-05-07 17:49:43 -0600
commit4b2d8bcd3491ca2bb51f85e7923e2f4c79333432 (patch)
treeb89174a0069c348fcd6b579e9098a4e08bb3b031 /utils/balance.py
parent2dcabff113476a18dadf1e281c013115902c2059 (diff)
downloadLedger.py-4b2d8bcd3491ca2bb51f85e7923e2f4c79333432.tar.gz
Ledger.py-4b2d8bcd3491ca2bb51f85e7923e2f4c79333432.zip
First attempt at balance!!
The output still needs colors, but it's functional. :D
Diffstat (limited to '')
-rw-r--r--utils/balance.py114
1 files changed, 114 insertions, 0 deletions
diff --git a/utils/balance.py b/utils/balance.py
new file mode 100644
index 0000000..64fa3e3
--- /dev/null
+++ b/utils/balance.py
@@ -0,0 +1,114 @@
+from typing import List
+
+from utils.read_file import entry
+from utils.register import currencies, complete_prices
+
+
+class tree:
+ def __init__(self, name: str, value: currencies = None) -> None:
+ self.name = name
+ self.value = value
+ self.children = []
+
+
+ def compute_value(self):
+ if self.value is not None:
+ return
+
+ self.value = currencies()
+ for child in reversed(self.children):
+ child.compute_value()
+ for price in child.value:
+ self.value.add_money(price)
+
+
+ def add_child(self, name: str, value: currencies = None):
+ self.children.append(tree(name, value))
+
+
+ def add_value(self, price: str):
+ if self.value is None:
+ self.value = currencies()
+
+ self.value.add_money(price)
+
+
+ def get_child_with_name(self, name: str):
+ for child in self.children:
+ if name == child.name:
+ return child
+
+ return None
+
+
+ def __contains__(self, name: str) -> bool:
+ for child in self.children:
+ if name == child.name:
+ return True
+
+ return False
+
+
+ def __str__(self, level=0, ignore_level = False) -> str:
+ result = ''
+ if not ignore_level:
+ for price in self.value:
+ result += f'{price:>20}\n'
+
+ result = result.rstrip()
+ result += ' ' + ' '*(2 * level)
+
+ if len(self.children) == 1:
+ result += self.name + ':'
+ for child in self.children:
+ result += child.__str__(level + 1, ignore_level = True)
+
+ else:
+ result += self.name + '\n'
+ for child in self.children:
+ result += child.__str__(level + 1)
+
+ return result
+
+
+class accounts_tree:
+ def __init__(self) -> None:
+ self.my_accounts = tree(name='root')
+
+ def add_transaction(self, name_acc: str, price: str):
+ search = self.my_accounts
+
+ for name in name_acc.split(':'):
+ if name not in search:
+ search.add_child(name)
+
+ search = search.get_child_with_name(name)
+
+ search.add_value(price)
+
+
+ def __str__(self) -> str:
+ self.my_accounts.compute_value()
+
+ result = ''
+ for child in self.my_accounts.children:
+ result += str(child)
+
+ result += '--------------------\n'
+ for price in self.my_accounts.value:
+ result += f'{price:>20}\n'
+
+ return result.rstrip()
+
+
+
+def print_balance(my_entries: List[entry]):
+ my_tree = accounts_tree()
+
+ for ent in my_entries:
+ complete_prices(ent)
+
+ for trans in ent.transactions:
+ my_tree.add_transaction(trans[0], str(trans[1]))
+
+ print(my_tree)