How to use the fava.core.inventory.CounterInventory function in fava

To help you get started, we’ve selected a few fava examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github beancount / fava / tests / test_core_inventory.py View on Github external
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
github beancount / fava / tests / test_core_inventory.py View on Github external
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
github beancount / fava / tests / test_core_inventory.py View on Github external
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
github beancount / fava / tests / test_template_filters.py View on Github external
def test_should_show(app):
    with app.test_request_context("/"):
        app.preprocess_request()
        assert should_show(g.ledger.root_tree.get("")) is True
        assert should_show(g.ledger.root_tree.get("Expenses")) is True

        account = TreeNode("name")
        assert should_show(account) is False
        account.balance_children = CounterInventory({("USD", None): 9})
        assert should_show(account) is True
    with app.test_request_context("/?time=2100"):
        app.preprocess_request()
        assert not g.ledger.fava_options["show-accounts-with-zero-balance"]
        assert should_show(g.ledger.root_tree.get("")) is True
        assert should_show(g.ledger.root_tree.get("Expenses")) is False
github beancount / fava / fava / core / tree.py View on Github external
def __init__(self, name):
        #: str: Account name.
        self.name = name
        #: A list of :class:`.TreeNode`, its children.
        self.children = []
        #: :class:`.CounterInventory`: The cumulative account balance.
        self.balance_children = CounterInventory()
        #: :class:`.CounterInventory`: The account balance.
        self.balance = CounterInventory()
        #: bool: True if the account has any transactions.
        self.has_txns = False
github beancount / fava / fava / core / charts.py View on Github external
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 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),
            }
github beancount / fava / fava / core / tree.py View on Github external
def cap(self, options, unrealized_account):
        """Transfer Income and Expenses, add conversions and unrealized gains.

        Args:
            options: The Beancount options.
            unrealized_account: The name of the account to post unrealized
                gains to (as a subaccount of Equity).
        """
        equity = options["name_equity"]
        conversions = CounterInventory(
            {
                (currency, None): -number
                for currency, number in self.get("")
                .balance_children.reduce(convert.get_cost)
                .items()
            }
        )

        # Add conversions
        self.insert(
            equity + ":" + options["account_current_conversions"], conversions
        )

        # Insert unrealized gains.
        self.insert(
            equity + ":" + unrealized_account, -self.get("").balance_children
github beancount / fava / fava / core / inventory.py View on Github external
def __add__(self, other):
        counter = CounterInventory(self)
        counter.add_inventory(other)
        return counter
github beancount / fava / fava / core / tree.py View on Github external
def __init__(self, name):
        #: str: Account name.
        self.name = name
        #: A list of :class:`.TreeNode`, its children.
        self.children = []
        #: :class:`.CounterInventory`: The cumulative account balance.
        self.balance_children = CounterInventory()
        #: :class:`.CounterInventory`: The account balance.
        self.balance = CounterInventory()
        #: bool: True if the account has any transactions.
        self.has_txns = False
github beancount / fava / fava / core / inventory.py View on Github external
def __neg__(self):
        return CounterInventory({key: -num for key, num in self.items()})