Merge pull request #20659 from deepeshgarg007/plaid_fix_develop

fix: Plaid fixes and enhancements
This commit is contained in:
Deepesh Garg 2020-02-18 18:09:01 +05:30 committed by GitHub
commit 0a6314ab92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 72 additions and 41 deletions

View File

@ -3,16 +3,16 @@
frappe.ui.form.on("Bank Reconciliation", { frappe.ui.form.on("Bank Reconciliation", {
setup: function(frm) { setup: function(frm) {
frm.add_fetch("bank_account", "account_currency", "account_currency"); frm.add_fetch("account", "account_currency", "account_currency");
}, },
onload: function(frm) { onload: function(frm) {
let default_bank_account = frappe.defaults.get_user_default("Company")? let default_bank_account = frappe.defaults.get_user_default("Company")?
locals[":Company"][frappe.defaults.get_user_default("Company")]["default_bank_account"]: ""; locals[":Company"][frappe.defaults.get_user_default("Company")]["default_bank_account"]: "";
frm.set_value("bank_account", default_bank_account); frm.set_value("account", default_bank_account);
frm.set_query("bank_account", function() { frm.set_query("account", function() {
return { return {
"filters": { "filters": {
"account_type": ["in",["Bank","Cash"]], "account_type": ["in",["Bank","Cash"]],

View File

@ -19,10 +19,9 @@
"bold": 0, "bold": 0,
"collapsible": 0, "collapsible": 0,
"columns": 0, "columns": 0,
"description": "Select account head of the bank where cheque was deposited.", "fetch_from": "bank_account.account",
"fetch_from": "bank_account_no.account",
"fetch_if_empty": 1, "fetch_if_empty": 1,
"fieldname": "bank_account", "fieldname": "account",
"fieldtype": "Link", "fieldtype": "Link",
"hidden": 0, "hidden": 0,
"ignore_user_permissions": 0, "ignore_user_permissions": 0,
@ -31,7 +30,7 @@
"in_global_search": 0, "in_global_search": 0,
"in_list_view": 1, "in_list_view": 1,
"in_standard_filter": 0, "in_standard_filter": 0,
"label": "Bank Account", "label": "Account",
"length": 0, "length": 0,
"no_copy": 0, "no_copy": 0,
"options": "Account", "options": "Account",
@ -164,7 +163,6 @@
"length": 0, "length": 0,
"no_copy": 0, "no_copy": 0,
"permlevel": 0, "permlevel": 0,
"precision": "",
"print_hide": 0, "print_hide": 0,
"print_hide_if_no_value": 0, "print_hide_if_no_value": 0,
"read_only": 0, "read_only": 0,
@ -183,8 +181,9 @@
"bold": 0, "bold": 0,
"collapsible": 0, "collapsible": 0,
"columns": 0, "columns": 0,
"description": "Select the Bank Account to reconcile.",
"fetch_if_empty": 0, "fetch_if_empty": 0,
"fieldname": "bank_account_no", "fieldname": "bank_account",
"fieldtype": "Link", "fieldtype": "Link",
"hidden": 0, "hidden": 0,
"ignore_user_permissions": 0, "ignore_user_permissions": 0,
@ -193,12 +192,11 @@
"in_global_search": 0, "in_global_search": 0,
"in_list_view": 0, "in_list_view": 0,
"in_standard_filter": 0, "in_standard_filter": 0,
"label": "Bank Account No", "label": "Bank Account",
"length": 0, "length": 0,
"no_copy": 0, "no_copy": 0,
"options": "Bank Account", "options": "Bank Account",
"permlevel": 0, "permlevel": 0,
"precision": "",
"print_hide": 0, "print_hide": 0,
"print_hide_if_no_value": 0, "print_hide_if_no_value": 0,
"read_only": 0, "read_only": 0,
@ -450,7 +448,7 @@
"istable": 0, "istable": 0,
"max_attachments": 0, "max_attachments": 0,
"menu_index": 0, "menu_index": 0,
"modified": "2019-04-09 18:41:06.110453", "modified": "2020-01-22 00:00:00.000000",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Accounts", "module": "Accounts",
"name": "Bank Reconciliation", "name": "Bank Reconciliation",

View File

@ -21,10 +21,6 @@ class BankReconciliation(Document):
if not self.include_reconciled_entries: if not self.include_reconciled_entries:
condition = " and (clearance_date is null or clearance_date='0000-00-00')" condition = " and (clearance_date is null or clearance_date='0000-00-00')"
account_cond = ""
if self.bank_account_no:
account_cond = " and t2.bank_account_no = {0}".format(frappe.db.escape(self.bank_account_no))
journal_entries = frappe.db.sql(""" journal_entries = frappe.db.sql("""
select select
"Journal Entry" as payment_document, t1.name as payment_entry, "Journal Entry" as payment_document, t1.name as payment_entry,
@ -34,15 +30,12 @@ class BankReconciliation(Document):
from from
`tabJournal Entry` t1, `tabJournal Entry Account` t2 `tabJournal Entry` t1, `tabJournal Entry Account` t2
where where
t2.parent = t1.name and t2.account = %s and t1.docstatus=1 t2.parent = t1.name and t2.account = %(account)s and t1.docstatus=1
and t1.posting_date >= %s and t1.posting_date <= %s and t1.posting_date >= %(from)s and t1.posting_date <= %(to)s
and ifnull(t1.is_opening, 'No') = 'No' {0} {1} and ifnull(t1.is_opening, 'No') = 'No' %(condition)s
group by t2.account, t1.name group by t2.account, t1.name
order by t1.posting_date ASC, t1.name DESC order by t1.posting_date ASC, t1.name DESC
""".format(condition, account_cond), (self.bank_account, self.from_date, self.to_date), as_dict=1) """, {"condition":condition, "account": self.account, "from": self.from_date, "to": self.to_date}, as_dict=1)
if self.bank_account_no:
condition = " and bank_account = %(bank_account_no)s"
payment_entries = frappe.db.sql(""" payment_entries = frappe.db.sql("""
select select
@ -55,12 +48,12 @@ class BankReconciliation(Document):
from `tabPayment Entry` from `tabPayment Entry`
where where
(paid_from=%(account)s or paid_to=%(account)s) and docstatus=1 (paid_from=%(account)s or paid_to=%(account)s) and docstatus=1
and posting_date >= %(from)s and posting_date <= %(to)s {0} and posting_date >= %(from)s and posting_date <= %(to)s
and bank_account = %(bank_account)s
order by order by
posting_date ASC, name DESC posting_date ASC, name DESC
""".format(condition), """, {"account": self.account, "from":self.from_date,
{"account":self.bank_account, "from":self.from_date, "to": self.to_date, "bank_account": self.bank_account}, as_dict=1)
"to":self.to_date, "bank_account_no": self.bank_account_no}, as_dict=1)
pos_entries = [] pos_entries = []
if self.include_pos_transactions: if self.include_pos_transactions:
@ -72,11 +65,10 @@ class BankReconciliation(Document):
from `tabSales Invoice Payment` sip, `tabSales Invoice` si, `tabAccount` account from `tabSales Invoice Payment` sip, `tabSales Invoice` si, `tabAccount` account
where where
sip.account=%(account)s and si.docstatus=1 and sip.parent = si.name sip.account=%(account)s and si.docstatus=1 and sip.parent = si.name
and account.name = sip.account and si.posting_date >= %(from)s and si.posting_date <= %(to)s {0} and account.name = sip.account and si.posting_date >= %(from)s and si.posting_date <= %(to)s
order by order by
si.posting_date ASC, si.name DESC si.posting_date ASC, si.name DESC
""".format(condition), """, {"account":self.account, "from":self.from_date, "to":self.to_date}, as_dict=1)
{"account":self.bank_account, "from":self.from_date, "to":self.to_date}, as_dict=1)
entries = sorted(list(payment_entries)+list(journal_entries+list(pos_entries)), entries = sorted(list(payment_entries)+list(journal_entries+list(pos_entries)),
key=lambda k: k['posting_date'] or getdate(nowdate())) key=lambda k: k['posting_date'] or getdate(nowdate()))

View File

@ -110,6 +110,15 @@
"set_only_once": 0, "set_only_once": 0,
"translatable": 0, "translatable": 0,
"unique": 0 "unique": 0
},
{
"depends_on": "eval:doc.docstatus==1",
"fieldname": "clearance_date",
"fieldtype": "Date",
"label": "Clearance Date",
"no_copy": 1,
"print_hide": 1,
"read_only": 1
} }
], ],
"has_web_view": 0, "has_web_view": 0,
@ -122,7 +131,7 @@
"issingle": 0, "issingle": 0,
"istable": 1, "istable": 1,
"max_attachments": 0, "max_attachments": 0,
"modified": "2018-12-06 10:57:02.635141", "modified": "2020-01-22 00:00:00.000000",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Accounts", "module": "Accounts",
"name": "Bank Transaction Payments", "name": "Bank Transaction Payments",

View File

@ -49,9 +49,10 @@
}, },
{ {
"fieldname": "plaid_env", "fieldname": "plaid_env",
"fieldtype": "Data", "fieldtype": "Select",
"in_list_view": 1, "in_list_view": 1,
"label": "Plaid Environment" "label": "Plaid Environment",
"options": "sandbox\ndevelopment\nproduction"
}, },
{ {
"fieldname": "column_break_2", "fieldname": "column_break_2",
@ -69,7 +70,7 @@
], ],
"issingle": 1, "issingle": 1,
"links": [], "links": [],
"modified": "2020-01-05 10:00:22.137832", "modified": "2020-02-07 15:21:11.616231",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "ERPNext Integrations", "module": "ERPNext Integrations",
"name": "Plaid Settings", "name": "Plaid Settings",

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.accounts.doctype.journal_entry.journal_entry import get_default_bank_cash_account
from erpnext.erpnext_integrations.doctype.plaid_settings.plaid_connector import PlaidConnector from erpnext.erpnext_integrations.doctype.plaid_settings.plaid_connector import PlaidConnector
from frappe.utils import getdate, formatdate, today, add_months from frappe.utils import getdate, formatdate, today, add_months
from frappe.desk.doctype.tag.tag import add_tag
class PlaidSettings(Document): class PlaidSettings(Document):
pass pass
@ -133,10 +134,13 @@ def sync_transactions(bank, bank_account):
try: try:
transactions = get_transactions(bank=bank, bank_account=bank_account, start_date=start_date, end_date=end_date) transactions = get_transactions(bank=bank, bank_account=bank_account, start_date=start_date, end_date=end_date)
result = [] result = []
if transactions: for transaction in reversed(transactions):
for transaction in transactions: result += new_bank_transaction(transaction)
result.append(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)) frappe.db.set_value("Bank Account", bank_account, "last_integration_date", getdate(end_date))
@ -175,6 +179,13 @@ def new_bank_transaction(transaction):
status = "Pending" if transaction["pending"] == "True" else "Settled" 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"])): if not frappe.db.exists("Bank Transaction", dict(transaction_id=transaction["transaction_id"])):
try: try:
new_transaction = frappe.get_doc({ new_transaction = frappe.get_doc({
@ -185,11 +196,16 @@ def new_bank_transaction(transaction):
"debit": debit, "debit": debit,
"credit": credit, "credit": credit,
"currency": transaction["iso_currency_code"], "currency": transaction["iso_currency_code"],
"transaction_id": transaction["transaction_id"],
"reference_number": transaction["payment_meta"]["reference_number"],
"description": transaction["name"] "description": transaction["name"]
}) })
new_transaction.insert() new_transaction.insert()
new_transaction.submit() new_transaction.submit()
for tag in tags:
add_tag(tag, "Bank Transaction", new_transaction.name)
result.append(new_transaction.name) result.append(new_transaction.name)
except Exception: except Exception:
@ -201,7 +217,7 @@ def automatic_synchronization():
settings = frappe.get_doc("Plaid Settings", "Plaid Settings") settings = frappe.get_doc("Plaid Settings", "Plaid Settings")
if settings.enabled == 1 and settings.automatic_sync == 1: if settings.enabled == 1 and settings.automatic_sync == 1:
plaid_accounts = frappe.get_all("Bank Account", filter={"integration_id": ["!=", ""]}, fields=["name", "bank"]) plaid_accounts = frappe.get_all("Bank Account", filters={"integration_id": ["!=", ""]}, fields=["name", "bank"])
for plaid_account in plaid_accounts: for plaid_account in plaid_accounts:
frappe.enqueue("erpnext.erpnext_integrations.doctype.plaid_settings.plaid_settings.sync_transactions", bank=plaid_account.bank, bank_account=plaid_account.name) frappe.enqueue("erpnext.erpnext_integrations.doctype.plaid_settings.plaid_settings.sync_transactions", bank=plaid_account.bank, bank_account=plaid_account.name)

View File

@ -658,3 +658,4 @@ erpnext.patches.v12_0.set_permission_einvoicing
erpnext.patches.v12_0.set_published_in_hub_tracked_item erpnext.patches.v12_0.set_published_in_hub_tracked_item
erpnext.patches.v12_0.set_job_offer_applicant_email erpnext.patches.v12_0.set_job_offer_applicant_email
erpnext.patches.v12_0.create_irs_1099_field_united_states erpnext.patches.v12_0.create_irs_1099_field_united_states
erpnext.patches.v12_0.rename_bank_reconciliation_fields # 2020-01-22

View File

@ -0,0 +1,14 @@
# Copyright (c) 2020, Frappe and Contributors
# License: GNU General Public License v3. See license.txt
import frappe
def _rename_single_field(**kwargs):
count = frappe.db.sql("SELECT COUNT(*) FROM tabSingles WHERE doctype='{doctype}' AND field='{new_name}';".format(**kwargs))[0][0] #nosec
if count == 0:
frappe.db.sql("UPDATE tabSingles SET field='{new_name}' WHERE doctype='{doctype}' AND field='{old_name}';".format(**kwargs)) #nosec
def execute():
_rename_single_field(doctype = "Bank Reconciliation", old_name = "bank_account" , new_name = "account")
_rename_single_field(doctype = "Bank Reconciliation", old_name = "bank_account_no", new_name = "bank_account")
frappe.reload_doc("Accounts", "doctype", "Bank Reconciliation")