Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Return:
types: Tuples of column names and types as strings.
rows: Dictionaries of row data by column names.
"""
operating_currency = self.ledger.options["operating_currency"][0]
acct_type = ("account", str(str))
bal_type = ("balance", str(Decimal))
alloc_type = ("allocation", str(Decimal))
types = [acct_type, bal_type, alloc_type]
rows = []
portfolio_total = ZERO
for node in nodes:
row = {}
row["account"] = node.name
balance = cost_or_value(node.balance, date)
if operating_currency in balance:
balance_dec = balance[operating_currency]
portfolio_total += balance_dec
row["balance"] = balance_dec
rows.append(row)
for row in rows:
if "balance" in row:
row["allocation"] = round(
(row["balance"] / portfolio_total) * 100, 2
)
return types, rows
Args:
interval: An interval.
accounts: A single account (str) or a tuple of accounts.
"""
for begin, end in pairwise(self.ledger.interval_ends(interval)):
inventory = CounterInventory()
entries = iter_entry_dates(self.ledger.entries, begin, end)
for entry in filter_type(entries, Transaction):
for posting in entry.postings:
if posting.account.startswith(accounts):
inventory.add_position(posting)
yield {
"date": begin,
"balance": cost_or_value(inventory, end),
"budgets": self.ledger.budgets.calculate_children(
accounts, begin, end
),
def serialise(self, end):
"""Serialise the account.
Args:
end: A date to use for cost conversions.
"""
children = [child.serialise(end) for child in self.children]
return {
"account": self.name,
"balance_children": cost_or_value(self.balance_children, end),
"balance": cost_or_value(self.balance, end),
"children": children,
}
real_account = realization.get_or_create(
self.ledger.root_account, account_name
)
postings = realization.get_postings(real_account)
journal = realization.iterate_with_balance(postings)
# When the balance for a commodity just went to zero, it will be
# missing from the 'balance' so keep track of currencies that last had
# a balance.
last_currencies = None
for entry, _, change, balance in journal:
if change.is_empty():
continue
balance = inv_to_dict(cost_or_value(balance, entry.date))
currencies = set(balance.keys())
if last_currencies:
for currency in last_currencies - currencies:
balance[currency] = 0
last_currencies = currencies
yield {"date": entry.date, "balance": balance}
def serialise(self, end):
"""Serialise the account.
Args:
end: A date to use for cost conversions.
"""
children = [child.serialise(end) for child in self.children]
return {
"account": self.name,
"balance_children": cost_or_value(self.balance_children, end),
"balance": cost_or_value(self.balance, end),
"children": children,
}
txn = next(transactions, None)
inventory = CounterInventory()
for end_date_exclusive in self.ledger.interval_ends(interval):
end_date_inclusive = end_date_exclusive - datetime.timedelta(
days=1
)
while txn and txn.date < end_date_exclusive:
for posting in filter(
lambda p: p.account.startswith(types), txn.postings
):
inventory.add_position(posting)
txn = next(transactions, None)
yield {
"date": end_date_exclusive,
"balance": cost_or_value(inventory, end_date_inclusive),
}