def test_add_amount(): inv = CounterInventory() inv.add_amount(A("10 USD")) inv.add_amount(A("30 USD")) assert len(inv) == 1 inv.add_amount(A("-40 USD")) assert inv.is_empty() inv.add_amount(A("10 USD")) inv.add_amount(A("20 CAD")) inv.add_amount(A("10 USD")) assert len(inv) == 2 inv.add_amount(A("-20 CAD")) assert len(inv) == 1
def test_add_amount(): inv = CounterInventory() inv.add_amount(A('10 USD')) inv.add_amount(A('30 USD')) assert len(inv) == 1 inv.add_amount(A('-40 USD')) assert inv.is_empty() inv.add_amount(A('10 USD')) inv.add_amount(A('20 CAD')) inv.add_amount(A('10 USD')) assert len(inv) == 2 inv.add_amount(A('-20 CAD')) assert len(inv) == 1
def net_worth(self, interval): """Compute net worth. Args: interval: A string for the interval. Returns: A list of dicts for all ends of the given interval containing the net worth (Assets + Liabilities) separately converted to all operating currencies. """ transactions = (entry for entry in self.ledger.entries if (isinstance(entry, Transaction) and entry.flag != flags.FLAG_UNREALIZED)) types = ( self.ledger.options['name_assets'], self.ledger.options['name_liabilities'], ) txn = next(transactions, None) inventory = CounterInventory() for date in self.ledger.interval_ends(interval): while txn and txn.date < date: for posting in filter(lambda p: p.account.startswith(types), txn.postings): # Since we will be reducing the inventory to the operating # currencies, pre-aggregate the positions to reduce the # number of elements in the inventory. inventory.add_amount( posting.units, Cost(ZERO, posting.cost.currency, None, None) if posting.cost else None, ) txn = next(transactions, None) yield { 'date': date, 'balance': { currency: inventory.reduce( convert.convert_position, currency, self.ledger.price_map, date, ).get(currency) for currency in self.ledger.options['operating_currency'] }, }
def net_worth(self, interval): """Compute net worth. Args: interval: A string for the interval. Returns: A list of dicts for all ends of the given interval containing the net worth (Assets + Liabilities) separately converted to all operating currencies. """ transactions = (entry for entry in self.ledger.entries if (isinstance(entry, Transaction) and entry.flag != flags.FLAG_UNREALIZED)) types = (self.ledger.options['name_assets'], self.ledger.options['name_liabilities']) txn = next(transactions, None) inventory = CounterInventory() for date in self.ledger.interval_ends(interval): while txn and txn.date < date: for posting in filter(lambda p: p.account.startswith(types), txn.postings): # Since we will be reducing the inventory to the operating # currencies, pre-aggregate the positions to reduce the # number of elements in the inventory. inventory.add_amount( posting.units, Cost(ZERO, posting.cost.currency, None, None) if posting.cost else None) txn = next(transactions, None) yield { 'date': date, 'balance': { currency: inventory.reduce(convert.convert_position, currency, self.ledger.price_map, date).get(currency) for currency in self.ledger.options['operating_currency'] } }
def test_add_inventory(): inv = CounterInventory() inv2 = CounterInventory() inv3 = CounterInventory() inv.add_amount(A("10 USD")) inv2.add_amount(A("30 USD")) inv3.add_amount(A("-40 USD")) inv.add_inventory(inv2) assert len(inv) == 1 inv.add_inventory(inv3) assert inv.is_empty() inv = CounterInventory() inv.add_inventory(inv2) assert len(inv) == 1
def test_add_inventory(): inv = CounterInventory() inv2 = CounterInventory() inv3 = CounterInventory() inv.add_amount(A('10 USD')) inv2.add_amount(A('30 USD')) inv3.add_amount(A('-40 USD')) inv.add_inventory(inv2) assert len(inv) == 1 inv.add_inventory(inv3) assert inv.is_empty() inv = CounterInventory() inv.add_inventory(inv2) assert len(inv) == 1
def test_CounterInventory_add_inventory(): inv = CounterInventory() inv2 = CounterInventory() inv3 = CounterInventory() inv.add_amount(A('10 USD')) inv2.add_amount(A('30 USD')) inv3.add_amount(A('-40 USD')) inv.add_inventory(inv2) assert len(inv) == 1 inv.add_inventory(inv3) assert inv.is_empty() inv = CounterInventory() inv.add_inventory(inv2) assert len(inv) == 1