Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use empty posting while balancing imported entry #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions beancount_import/source/generic_importer_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,40 @@ def get_info(raw_entry: Directive) -> dict:
line=raw_entry.meta['lineno'],
)

def _none_units_posting_index(txn:Transaction):
"""Index of posting with None units"""
for i, posting in enumerate(txn.postings):
if posting.units is None:
return i

def balance_amounts(txn:Transaction)-> None:
"""Add FIXME account for the remaing amount to balance accounts"""
inventory = SimpleInventory()
i = _none_units_posting_index(txn)
for posting in txn.postings:
inventory += posting.units
for currency in inventory:
txn.postings.append(
Posting(
account=FIXME_ACCOUNT,
units=Amount(currency=currency, number=-inventory[currency]),
cost=None,
price=None,
flag=None,
meta={},
))
if not inventory[currency]: # if amount already balanced
continue
if i is not None:
# use empty posting to balance amount instead of FIXME
none_posting = txn.postings[i]
txn.postings.append(
none_posting._replace(
units=Amount(currency=currency, number=-inventory[currency])
))
else:
txn.postings.append(
Posting(
account=FIXME_ACCOUNT,
units=Amount(currency=currency, number=-inventory[currency]),
cost=None,
price=None,
flag=None,
meta={},
))
if i is not None:
txn.postings.pop(i) # remove the None posting after balancing


def load(spec, log_status):
Expand Down