Skip to content

Commit

Permalink
Merge 81bb9d7 into 708ae8a
Browse files Browse the repository at this point in the history
  • Loading branch information
Zburatorul committed Dec 9, 2020
2 parents 708ae8a + 81bb9d7 commit 08358e5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
41 changes: 39 additions & 2 deletions beancount_import/reconcile.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

from .matching import FIXME_ACCOUNT, is_unknown_account, CLEARED_KEY

UNCONFIRMED_ACCOUNT_KEY = 'unconfirmed_account'
NEW_ACCOUNT_KEY = 'new_account'
display_prediction_explanation = False

classifier_cache_version_number = 1
Expand Down Expand Up @@ -722,8 +724,39 @@ def _get_primary_transaction_amount_number(self, transaction: Transaction):
return -source_posting.units.number
return None


def _get_unknown_account_names(self, transaction: Transaction):
return [NEW_ACCOUNT_KEY in posting.meta for posting in transaction.postings]

def _group_predicted_accounts_by_name(self, transaction: Transaction):
'''
Takes a list of postings with candidate account names,
and groups them into groups that should share the same exact account.
Expects each predicted posting to have an UNCONFIRMED_ACCOUNT_KEY meta field.
'''
num_groups = 0
group_numbers = []
predicted_account_names = []
existing_groups = {} # type: Dict[str, int]
for posting in transaction.postings:
if UNCONFIRMED_ACCOUNT_KEY not in posting.meta:
continue
group_number = existing_groups.setdefault(posting.account,
num_groups)
predicted_account_names.append(posting.account)
if group_number == num_groups:
num_groups += 1
group_numbers.append(group_number)
return predicted_account_names, group_numbers

def _get_unknown_account_predictions(self,
transaction: Transaction) -> List[str]:
if any(posting.meta is not None and UNCONFIRMED_ACCOUNT_KEY in posting.meta for posting in transaction.postings):
# if any of the postings have the UNCONFIRMED_ACCOUNT_KEY, then prediction was handled by smart_importer
predicted_account_names, _ = self._group_predicted_accounts_by_name(transaction)
transaction.meta[UNCONFIRMED_ACCOUNT_KEY] = True
return predicted_account_names
# Default behavior
group_prediction_inputs = self._feature_extractor.extract_unknown_account_group_features(
transaction)
group_predictions = [
Expand Down Expand Up @@ -754,8 +787,12 @@ def _make_candidate_with_substitutions(self,
unique_id: account
for unique_id, account in zip(unique_ids, new_accounts)
}
group_numbers = training.get_unknown_account_group_numbers(transaction)
unknown_names = training.get_unknown_account_names(transaction)
if transaction.meta is not None and UNCONFIRMED_ACCOUNT_KEY in transaction.meta:
_, group_numbers = self._group_predicted_accounts_by_name(transaction)
unknown_names = self._get_unknown_account_names(transaction)
else:
group_numbers = training.get_unknown_account_group_numbers(transaction)
unknown_names = training.get_unknown_account_names(transaction)
substitutions = [
AccountSubstitution(
unique_name=unique_id,
Expand Down
2 changes: 1 addition & 1 deletion beancount_import/source/paypal.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def _make_import_result(self, txn_id: str, data: Dict[str, Any],
elif transaction_type_enum.endswith('_SENT') or transaction_type_enum.endswith('_PURCHASE'):
negate_funding_source_amounts = True

elif transaction_type_enum.endswith('_RECEIVED'):
elif transaction_type_enum.endswith('_RECEIVED') or transaction_type_enum.endswith("EBAY_SALE"):
negate_funding_source_amounts = False
elif transaction_type_enum == 'MONEY_TRANSFER':
counterparty_remainder_account = self.assets_account
Expand Down

0 comments on commit 08358e5

Please sign in to comment.