Merge pull request #20659 from deepeshgarg007/plaid_fix_develop

fix: Plaid fixes and enhancements
diff --git a/erpnext/accounts/doctype/bank_reconciliation/bank_reconciliation.js b/erpnext/accounts/doctype/bank_reconciliation/bank_reconciliation.js
index 7c94455..19fadbf 100644
--- a/erpnext/accounts/doctype/bank_reconciliation/bank_reconciliation.js
+++ b/erpnext/accounts/doctype/bank_reconciliation/bank_reconciliation.js
@@ -3,16 +3,16 @@
 
 frappe.ui.form.on("Bank Reconciliation", {
 	setup: function(frm) {
-		frm.add_fetch("bank_account", "account_currency", "account_currency");
+		frm.add_fetch("account", "account_currency", "account_currency");
 	},
 
 	onload: function(frm) {
 
 		let default_bank_account =  frappe.defaults.get_user_default("Company")? 
 			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 {
 				"filters": {
 					"account_type": ["in",["Bank","Cash"]],
diff --git a/erpnext/accounts/doctype/bank_reconciliation/bank_reconciliation.json b/erpnext/accounts/doctype/bank_reconciliation/bank_reconciliation.json
index e2f967d..b85ef3e 100644
--- a/erpnext/accounts/doctype/bank_reconciliation/bank_reconciliation.json
+++ b/erpnext/accounts/doctype/bank_reconciliation/bank_reconciliation.json
@@ -19,10 +19,9 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
-   "description": "Select account head of the bank where cheque was deposited.", 
-   "fetch_from": "bank_account_no.account", 
+   "fetch_from": "bank_account.account", 
    "fetch_if_empty": 1, 
-   "fieldname": "bank_account", 
+   "fieldname": "account", 
    "fieldtype": "Link", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
@@ -31,7 +30,7 @@
    "in_global_search": 0, 
    "in_list_view": 1, 
    "in_standard_filter": 0, 
-   "label": "Bank Account", 
+   "label": "Account", 
    "length": 0, 
    "no_copy": 0, 
    "options": "Account", 
@@ -164,7 +163,6 @@
    "length": 0, 
    "no_copy": 0, 
    "permlevel": 0, 
-   "precision": "", 
    "print_hide": 0, 
    "print_hide_if_no_value": 0, 
    "read_only": 0, 
@@ -183,8 +181,9 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "description": "Select the Bank Account to reconcile.",
    "fetch_if_empty": 0, 
-   "fieldname": "bank_account_no", 
+   "fieldname": "bank_account", 
    "fieldtype": "Link", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
@@ -193,12 +192,11 @@
    "in_global_search": 0, 
    "in_list_view": 0, 
    "in_standard_filter": 0, 
-   "label": "Bank Account No", 
+   "label": "Bank Account", 
    "length": 0, 
    "no_copy": 0, 
    "options": "Bank Account", 
    "permlevel": 0, 
-   "precision": "", 
    "print_hide": 0, 
    "print_hide_if_no_value": 0, 
    "read_only": 0, 
@@ -450,7 +448,7 @@
  "istable": 0, 
  "max_attachments": 0, 
  "menu_index": 0, 
- "modified": "2019-04-09 18:41:06.110453", 
+ "modified": "2020-01-22 00:00:00.000000", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Bank Reconciliation", 
@@ -483,4 +481,4 @@
  "track_changes": 0, 
  "track_seen": 0, 
  "track_views": 0
-}
\ No newline at end of file
+}
diff --git a/erpnext/accounts/doctype/bank_reconciliation/bank_reconciliation.py b/erpnext/accounts/doctype/bank_reconciliation/bank_reconciliation.py
index 90cdf83..52bbe33 100644
--- a/erpnext/accounts/doctype/bank_reconciliation/bank_reconciliation.py
+++ b/erpnext/accounts/doctype/bank_reconciliation/bank_reconciliation.py
@@ -21,10 +21,6 @@
 		if not self.include_reconciled_entries:
 			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("""
 			select
 				"Journal Entry" as payment_document, t1.name as payment_entry,
@@ -34,15 +30,12 @@
 			from
 				`tabJournal Entry` t1, `tabJournal Entry Account` t2
 			where
-				t2.parent = t1.name and t2.account = %s and t1.docstatus=1
-				and t1.posting_date >= %s and t1.posting_date <= %s
-				and ifnull(t1.is_opening, 'No') = 'No' {0} {1}
+				t2.parent = t1.name and t2.account = %(account)s and t1.docstatus=1
+				and t1.posting_date >= %(from)s and t1.posting_date <= %(to)s
+				and ifnull(t1.is_opening, 'No') = 'No' %(condition)s
 			group by t2.account, t1.name
 			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)
-
-		if self.bank_account_no:
-			condition = " and bank_account = %(bank_account_no)s"
+		""", {"condition":condition, "account": self.account, "from": self.from_date, "to": self.to_date}, as_dict=1)
 
 		payment_entries = frappe.db.sql("""
 			select
@@ -55,12 +48,12 @@
 			from `tabPayment Entry`
 			where
 				(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
 				posting_date ASC, name DESC
-		""".format(condition),
-		        {"account":self.bank_account, "from":self.from_date,
-				"to":self.to_date, "bank_account_no": self.bank_account_no}, as_dict=1)
+		""", {"account": self.account, "from":self.from_date,
+				"to": self.to_date, "bank_account": self.bank_account}, as_dict=1)
 
 		pos_entries = []
 		if self.include_pos_transactions:
@@ -72,11 +65,10 @@
 				from `tabSales Invoice Payment` sip, `tabSales Invoice` si, `tabAccount` account
 				where
 					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
 					si.posting_date ASC, si.name DESC
-			""".format(condition),
-			        {"account":self.bank_account, "from":self.from_date, "to":self.to_date}, as_dict=1)
+			""", {"account":self.account, "from":self.from_date, "to":self.to_date}, as_dict=1)
 
 		entries = sorted(list(payment_entries)+list(journal_entries+list(pos_entries)),
 			key=lambda k: k['posting_date'] or getdate(nowdate()))
diff --git a/erpnext/accounts/doctype/bank_transaction_payments/bank_transaction_payments.json b/erpnext/accounts/doctype/bank_transaction_payments/bank_transaction_payments.json
index a75e866..ab3f60d 100644
--- a/erpnext/accounts/doctype/bank_transaction_payments/bank_transaction_payments.json
+++ b/erpnext/accounts/doctype/bank_transaction_payments/bank_transaction_payments.json
@@ -110,6 +110,15 @@
    "set_only_once": 0, 
    "translatable": 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, 
@@ -122,7 +131,7 @@
  "issingle": 0, 
  "istable": 1, 
  "max_attachments": 0, 
- "modified": "2018-12-06 10:57:02.635141", 
+ "modified": "2020-01-22 00:00:00.000000", 
  "modified_by": "Administrator", 
  "module": "Accounts", 
  "name": "Bank Transaction Payments", 
@@ -138,4 +147,4 @@
  "track_changes": 1, 
  "track_seen": 0, 
  "track_views": 0
-}
\ No newline at end of file
+}
diff --git a/erpnext/erpnext_integrations/doctype/plaid_settings/plaid_settings.json b/erpnext/erpnext_integrations/doctype/plaid_settings/plaid_settings.json
index df77ad8..d8203d7 100644
--- a/erpnext/erpnext_integrations/doctype/plaid_settings/plaid_settings.json
+++ b/erpnext/erpnext_integrations/doctype/plaid_settings/plaid_settings.json
@@ -49,9 +49,10 @@
   },
   {
    "fieldname": "plaid_env",
-   "fieldtype": "Data",
+   "fieldtype": "Select",
    "in_list_view": 1,
-   "label": "Plaid Environment"
+   "label": "Plaid Environment",
+   "options": "sandbox\ndevelopment\nproduction"
   },
   {
    "fieldname": "column_break_2",
@@ -69,7 +70,7 @@
  ],
  "issingle": 1,
  "links": [],
- "modified": "2020-01-05 10:00:22.137832",
+ "modified": "2020-02-07 15:21:11.616231",
  "modified_by": "Administrator",
  "module": "ERPNext Integrations",
  "name": "Plaid Settings",
diff --git a/erpnext/erpnext_integrations/doctype/plaid_settings/plaid_settings.py b/erpnext/erpnext_integrations/doctype/plaid_settings/plaid_settings.py
index 9211133..7083950 100644
--- a/erpnext/erpnext_integrations/doctype/plaid_settings/plaid_settings.py
+++ b/erpnext/erpnext_integrations/doctype/plaid_settings/plaid_settings.py
@@ -10,6 +10,7 @@
 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
@@ -133,10 +134,13 @@
 
 	try:
 		transactions = get_transactions(bank=bank, bank_account=bank_account, start_date=start_date, end_date=end_date)
+
 		result = []
-		if transactions:
-			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))
 
@@ -175,6 +179,13 @@
 
 	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({
@@ -185,11 +196,16 @@
 				"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:
@@ -201,7 +217,7 @@
 	settings = frappe.get_doc("Plaid Settings", "Plaid Settings")
 
 	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:
 			frappe.enqueue("erpnext.erpnext_integrations.doctype.plaid_settings.plaid_settings.sync_transactions", bank=plaid_account.bank, bank_account=plaid_account.name)
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 09890c5..42d537a 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -657,4 +657,5 @@
 erpnext.patches.v12_0.set_permission_einvoicing
 erpnext.patches.v12_0.set_published_in_hub_tracked_item
 erpnext.patches.v12_0.set_job_offer_applicant_email
-erpnext.patches.v12_0.create_irs_1099_field_united_states
\ No newline at end of file
+erpnext.patches.v12_0.create_irs_1099_field_united_states
+erpnext.patches.v12_0.rename_bank_reconciliation_fields # 2020-01-22
diff --git a/erpnext/patches/v12_0/rename_bank_reconciliation_fields.py b/erpnext/patches/v12_0/rename_bank_reconciliation_fields.py
new file mode 100644
index 0000000..caeda8a
--- /dev/null
+++ b/erpnext/patches/v12_0/rename_bank_reconciliation_fields.py
@@ -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")