fix: Plaid transaction import order, transaction_id duplicate check, added transaction category tags

This commit is contained in:
racitup 2020-01-21 15:07:47 +00:00 committed by Deepesh Garg
parent 9a47c3dc8c
commit 3c5bb80b5a

View File

@ -10,6 +10,7 @@ from frappe.model.document import Document
from erpnext.accounts.doctype.journal_entry.journal_entry import get_default_bank_cash_account
from erpnext.erpnext_integrations.doctype.plaid_settings.plaid_connector import PlaidConnector
from frappe.utils import getdate, formatdate, today, add_months
from frappe.desk.doctype.tag.tag import add_tag
class PlaidSettings(Document):
pass
@ -135,11 +136,11 @@ def sync_transactions(bank, bank_account):
transactions = get_transactions(bank=bank, bank_account=bank_account, start_date=start_date, end_date=end_date)
result = []
if transactions:
frappe.logger().info("Plaid is adding {} Bank Transactions from '{}' between {} and {}".format(
len(transactions), bank_account, start_date, end_date))
for transaction in transactions:
result.append(new_bank_transaction(transaction))
for transaction in reversed(transactions):
result += new_bank_transaction(transaction)
frappe.logger().info("Plaid added {} new Bank Transactions from '{}' between {} and {}".format(
len(result), bank_account, start_date, end_date))
frappe.db.set_value("Bank Account", bank_account, "last_integration_date", getdate(end_date))
@ -178,6 +179,13 @@ def new_bank_transaction(transaction):
status = "Pending" if transaction["pending"] == "True" else "Settled"
try:
tags = []
tags += transaction["category"]
tags += ["Plaid Cat. {}".format(transaction["category_id"])]
except KeyError:
pass
if not frappe.db.exists("Bank Transaction", dict(transaction_id=transaction["transaction_id"])):
try:
new_transaction = frappe.get_doc({
@ -188,11 +196,16 @@ def new_bank_transaction(transaction):
"debit": debit,
"credit": credit,
"currency": transaction["iso_currency_code"],
"transaction_id": transaction["transaction_id"],
"reference_number": transaction["payment_meta"]["reference_number"],
"description": transaction["name"]
})
new_transaction.insert()
new_transaction.submit()
for tag in tags:
add_tag(tag, "Bank Transaction", new_transaction.name)
result.append(new_transaction.name)
except Exception: