Example #1
0
    def _activity_by_account(self, account_name=None):
        nb_activity_by_account = []
        for real_account in realization.iter_children(self.root_account):
            if not isinstance(real_account, RealAccount):
                continue
            if account_name and real_account.account != account_name:
                continue

            last_posting = realization.find_last_active_posting(
                real_account.txn_postings)

            if last_posting is None or isinstance(last_posting, Close):
                continue

            entry = get_entry(last_posting)

            nb_activity_by_account.append({
                'account':
                real_account.account,
                'last_posting_date':
                entry.date,
                'last_posting_filename':
                entry.meta['filename'],
                'last_posting_lineno':
                entry.meta['lineno']
            })

        return nb_activity_by_account
Example #2
0
    def render_real_text(self, real_root, price_map, price_date, options_map,
                         file):
        rows = []
        account_types = options.get_account_types(options_map)
        for root in (account_types.assets, account_types.liabilities):
            for unused_first_line, unused_cont_line, real_account in realization.dump(
                    realization.get(real_root, root)):

                last_posting = realization.find_last_active_posting(
                    real_account.txn_postings)

                # Don't render updates to accounts that have been closed.
                # Note: this is O(N), maybe we can store this at realization.
                if last_posting is None or isinstance(last_posting,
                                                      data.Close):
                    continue

                last_date = data.get_entry(last_posting).date
                # Skip this posting if a cutoff date has been specified and the
                # account has been updated to at least the cutoff date.
                if self.args.cutoff and self.args.cutoff <= last_date:
                    continue

                rows.append((real_account.account, last_date))

        table_ = table.create_table(rows, [(0, 'Account'),
                                           (1, 'Last Date', '{}'.format)])
        table.render_table(table_, file, 'text')
Example #3
0
 def get_users(self):
     users = []
     for user, account in self.accounts["Assets"]["Receivable"].items():
         last_posting = realization.find_last_active_posting(account.txn_postings)
         if not isinstance(last_posting, Close):
             users.append(user)
     return users
Example #4
0
    def render_real_htmldiv(self, real_root, options_map, file):
        account_types = options.get_account_types(options_map)
        for root in (account_types.assets, account_types.liabilities):
            oss = io.StringIO()
            table = tree_table.tree_table(oss,
                                          realization.get(real_root, root),
                                          None, ['Account', 'Last Entry'])
            num_cells = 0
            for real_account, cells, row_classes in table:
                if not isinstance(real_account, realization.RealAccount):
                    continue
                last_posting = realization.find_last_active_posting(
                    real_account.txn_postings)

                # Don't render updates to accounts that have been closed.
                # Note: this is O(N), maybe we can store this at realization.
                if last_posting is None or isinstance(last_posting,
                                                      data.Close):
                    continue

                last_date = data.get_entry(last_posting).date

                # Skip this posting if a cutoff date has been specified and the
                # account has been updated to at least the cutoff date.
                if self.args.cutoff and self.args.cutoff <= last_date:
                    continue

                # Okay, we need to render this. Append.
                cells.append(
                    data.get_entry(last_posting).date if real_account.
                    txn_postings else '-')
                num_cells += 1

            if num_cells:
                file.write(oss.getvalue())
Example #5
0
    def last_entry(self, account_name):
        """The last entry of the account if it is not a Close entry.
        """
        account = realization.get_or_create(self.all_root_account, account_name)

        last = realization.find_last_active_posting(account.txn_postings)

        if last is None or isinstance(last, Close):
            return

        return get_entry(last)
Example #6
0
    def last_entry(self, account_name):
        """The last entry of the account if it is not a Close entry.
        """
        account = realization.get_or_create(self.all_root_account,
                                            account_name)

        last = realization.find_last_active_posting(account.txn_postings)

        if last is None or isinstance(last, Close):
            return

        return get_entry(last)
Example #7
0
    def last_account_activity_in_days(self, account_name):
        real_account = realization.get_or_create(self.all_root_account,
                                                 account_name)

        last_posting = realization.find_last_active_posting(
            real_account.txn_postings)

        if last_posting is None or isinstance(last_posting, Close):
            return 0

        entry = get_entry(last_posting)

        return (datetime.date.today() - entry.date).days
Example #8
0
    def last_account_activity_in_days(self, account_name):
        real_account = realization.get_or_create(self.all_root_account,
                                                 account_name)

        last_posting = realization.find_last_active_posting(
            real_account.txn_postings)

        if last_posting is None or isinstance(last_posting, Close):
            return 0

        entry = get_entry(last_posting)

        return (datetime.date.today() - entry.date).days
Example #9
0
def serialize_real_account(ra):
    return {
        'account': ra.account,
        'balance_children':
            serialize_inventory(realization.compute_balance(ra),
                                at_cost=True),
        'balance': serialize_inventory(ra.balance, at_cost=True),
        'is_leaf': len(ra) == 0 or bool(ra.txn_postings),
        'is_closed': isinstance(realization.find_last_active_posting(
            ra.txn_postings), Close),
        'has_transactions': any(isinstance(t, TxnPosting)
                                for t in ra.txn_postings),
        'children': [serialize_real_account(a) for n, a in sorted(ra.items())],
    }
Example #10
0
def should_show(account):
    show_this_account = False
    # check if it's a leaf account
    if len(account) == 0 or bool(account.txn_postings):
        show_this_account = True
        if not g.api.fava_options['show-closed-accounts'] and \
                isinstance(realization.find_last_active_posting(
                        account.txn_postings), Close):
            show_this_account = False
        if not g.api.fava_options['show-accounts-with-zero-balance'] and \
                account.balance.is_empty():
            show_this_account = False
        if not g.api.fava_options['show-accounts-with-zero-transactions'] and \
                not any(isinstance(t, TxnPosting)
                        for t in account.txn_postings):
            show_this_account = False
    return show_this_account or any(should_show(a) for a in account.values())
Example #11
0
def serialize_real_account(ra):
    return {
        'account':
        ra.account,
        'balance_children':
        serialize_inventory(realization.compute_balance(ra), at_cost=True),
        'balance':
        serialize_inventory(ra.balance, at_cost=True),
        'is_leaf':
        len(ra) == 0 or bool(ra.txn_postings),
        'is_closed':
        isinstance(realization.find_last_active_posting(ra.txn_postings),
                   Close),
        'has_transactions':
        any(isinstance(t, TxnPosting) for t in ra.txn_postings),
        'children': [serialize_real_account(a) for n, a in sorted(ra.items())],
    }
Example #12
0
    def last_entry(self, account_name: str) -> Directive | None:
        """Get last entry of an account.

        Args:
            account_name: An account name.

        Returns:
            The last entry of the account if it is not a Close entry.
        """
        account = realization.get_or_create(self.all_root_account,
                                            account_name)

        last = realization.find_last_active_posting(account.txn_postings)

        if last is None or isinstance(last, Close):
            return None

        return get_entry(last)
Example #13
0
    def _last_posting_for_account(self, account_name):
        """
        Returns the last posting for an account (ignores Close)
        """
        real_account = realization.get_or_create(self.all_root_account,
                                                 account_name)

        last_posting = realization.find_last_active_posting(
            real_account.txn_postings)

        if not isinstance(last_posting, Close):
            return last_posting

        postings = realization.get_postings(real_account)
        if len(postings) >= 2:
            return postings[-2]

        return None
Example #14
0
def should_show(account):
    """Determine whether the account should be shown."""
    show_this_account = False
    # check if it's a leaf account
    if not account or account.txn_postings:
        show_this_account = True
        if (not g.ledger.fava_options['show-closed-accounts'] and isinstance(
                realization.find_last_active_posting(account.txn_postings),
                data.Close)):
            show_this_account = False
        if (not g.ledger.fava_options['show-accounts-with-zero-balance']
                and account.balance.is_empty()):
            show_this_account = False
        if (not g.ledger.fava_options['show-accounts-with-zero-transactions']
                and not any(
                    isinstance(t, data.TxnPosting)
                    for t in account.txn_postings)):
            show_this_account = False
    return show_this_account or any(should_show(a) for a in account.values())
Example #15
0
def should_show(account):
    """Determine whether the account should be shown."""
    show_this_account = False
    # check if it's a leaf account
    if len(account) == 0 or bool(account.txn_postings):
        show_this_account = True
        if not g.api.fava_options['show-closed-accounts'] and \
                isinstance(realization.find_last_active_posting(
                        account.txn_postings), Close):
            show_this_account = False
        if not g.api.fava_options['show-accounts-with-zero-balance'] and \
                account.balance.is_empty():
            show_this_account = False
        if not g.api.fava_options['show-accounts-with-zero-transactions'] and \
                not any(isinstance(t, TxnPosting)
                        for t in account.txn_postings):
            show_this_account = False
    return show_this_account or any(
        should_show(a) for a in account.values())
Example #16
0
 def get_balances(self):
     if self.transaction is None:
         accounts = self.accounts
     else:
         entries, balance_errors = booking.book(
                 self.entries + [self.transaction],
                 options.OPTIONS_DEFAULTS.copy())
         assert(len(balance_errors) == 0)
         accounts = realization.realize(entries)
     balances = {}
     for user, account in accounts["Assets"]["Receivable"].items():
         last_posting = realization.find_last_active_posting(account.txn_postings)
         if not isinstance(last_posting, Close):
             positions = account.balance.reduce(convert.get_cost).get_positions()
             amounts = [position.units for position in positions]
             assert(len(amounts) < 2)
             if len(amounts) == 1:
                 balances[user] = str(-amounts[0])
             else:
                 balances[user] = "0.00 EUR"
     return balances
Example #17
0
    def test_find_last_active_posting(self, entries, _, __):
        """
        2012-01-01 open Assets:Target
        2012-01-01 open Equity:Other

        2014-02-01 *
          Assets:Target            123.45 CAD
          Equity:Other

        2014-03-01 U "This should get ignored because of the unrealized flag."
          Assets:Target            -23.45 CAD
          Equity:Other

        ;; This should get ignored because it's not one of the directives checked for
        ;; active.
        2014-03-02 event "location" "Somewhere, Somewhereland"
        """
        real_account = realization.realize(entries)
        txn_postings = realization.get(real_account, 'Assets:Target').txn_postings
        txn_posting = realization.find_last_active_posting(txn_postings)
        self.assertEqual(datetime.date(2014, 2, 1), txn_posting.txn.date)
Example #18
0
    def _activity_by_account(self, account_name=None):
        nb_activity_by_account = []
        for real_account in realization.iter_children(self.root_account):
            if not isinstance(real_account, RealAccount):
                continue
            if account_name and real_account.account != account_name:
                continue

            last_posting = realization.find_last_active_posting(
                real_account.txn_postings)

            if last_posting is None or isinstance(last_posting, Close):
                continue

            entry = get_entry(last_posting)

            nb_activity_by_account.append({
                'account': real_account.account,
                'last_posting_date': entry.date,
                'last_posting_filename': entry.meta['filename'],
                'last_posting_lineno': entry.meta['lineno']
            })

        return nb_activity_by_account