From 7b9ae9d56e9ef95e1a2bc296d4f612b62c7cea56 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sat, 2 Jan 2021 21:28:40 +1100 Subject: [PATCH] Minor API changes --- ledger_pyreport/ledger.py | 2 +- ledger_pyreport/model.py | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/ledger_pyreport/ledger.py b/ledger_pyreport/ledger.py index 4305442..e756ea0 100644 --- a/ledger_pyreport/ledger.py +++ b/ledger_pyreport/ledger.py @@ -51,7 +51,7 @@ def financial_year(date): # Ledger logic -csv.register_dialect('ledger', doublequote=False, escapechar='\\') +csv.register_dialect('ledger', doublequote=False, escapechar='\\', strict=True) def parse_amount(amount): if '{' in amount: diff --git a/ledger_pyreport/model.py b/ledger_pyreport/model.py index 286c783..990db9c 100644 --- a/ledger_pyreport/model.py +++ b/ledger_pyreport/model.py @@ -65,6 +65,17 @@ class Ledger: raise Exception('No price information for {} to {} at {:%Y-%m-%d}'.format(commodity_from, commodity_to, date)) return max(prices, key=lambda p: p[0])[2] + + def get_balance(self, account, date=None): + result = Balance() + + for transaction in self.transactions: + if date is None or transaction.date <= date: + for posting in transaction.postings: + if posting.account == account: + result += posting.amount + + return result class Transaction: def __init__(self, ledger, id, date, description, code=None, uuid=None, metadata=None): @@ -315,7 +326,16 @@ class Amount: def __rsub__(self, other): return Amount(other - self.amount, self.commodity) - def exchange(self, commodity, is_cost, price=None, date=None, ledger=None): + def __mul__(self, other): + return Amount(self.amount * other, self.commodity) + def __truediv__(self, other): + if isinstance(other, Amount): + if other.commodity != self.commodity: + raise TypeError('Cannot combine Amounts of commodity {} and {}'.format(self.commodity.name, other.commodity.name)) + return self.amount / other.amount + return Amount(self.amount / Decimal(other), self.commodity) + + def exchange(self, commodity, is_cost=True, price=None, date=None, ledger=None): if self.commodity.name == commodity.name: return Amount(self) @@ -360,7 +380,7 @@ class Balance: def clean(self): return Balance([a for a in self.amounts if a != 0]) - def exchange(self, commodity, is_cost, date=None, ledger=None): + def exchange(self, commodity, is_cost=True, date=None, ledger=None): result = Amount(0, commodity) for amount in self.amounts: if is_cost or amount.commodity.name == commodity.name: