fix: account payable currency and value (#36859)

* fix: account payable currency and value

* fix: added party_type and party in accounts payable report

* chore: code cleanup

* fix: customer group test case failure

* fix: added test case of the issue

* fix: filter toggle for party_type

* fix: filter toggle for party_type

* chore: fix typo

---------

Co-authored-by: ruthra kumar <ruthra@erpnext.com>
diff --git a/erpnext/accounts/report/accounts_payable/accounts_payable.js b/erpnext/accounts/report/accounts_payable/accounts_payable.js
index e1a30a4..27a8570 100644
--- a/erpnext/accounts/report/accounts_payable/accounts_payable.js
+++ b/erpnext/accounts/report/accounts_payable/accounts_payable.js
@@ -38,24 +38,6 @@
 			}
 		},
 		{
-			"fieldname": "supplier",
-			"label": __("Supplier"),
-			"fieldtype": "Link",
-			"options": "Supplier",
-			on_change: () => {
-				var supplier = frappe.query_report.get_filter_value('supplier');
-				if (supplier) {
-					frappe.db.get_value('Supplier', supplier, "tax_id", function(value) {
-						frappe.query_report.set_filter_value('tax_id', value["tax_id"]);
-					});
-				} else {
-					frappe.query_report.set_filter_value('tax_id', "");
-				}
-
-				frappe.query_report.refresh();
-			}
-		},
-		{
 			"fieldname": "party_account",
 			"label": __("Payable Account"),
 			"fieldtype": "Link",
@@ -113,10 +95,37 @@
 			"options": "Payment Terms Template"
 		},
 		{
+			"fieldname": "party_type",
+			"label": __("Party Type"),
+			"fieldtype": "Link",
+			"options": "Party Type",
+			get_query: () => {
+				return {
+					filters: {
+						'account_type': 'Payable'
+					}
+				};
+			},
+			on_change: () => {
+				frappe.query_report.set_filter_value('party', "");
+				let party_type = frappe.query_report.get_filter_value('party_type');
+				frappe.query_report.toggle_filter_display('supplier_group', frappe.query_report.get_filter_value('party_type') !== "Supplier");
+
+			}
+
+		},
+		{
+			"fieldname":"party",
+			"label": __("Party"),
+			"fieldtype": "Dynamic Link",
+			"options": "party_type",
+		},
+		{
 			"fieldname": "supplier_group",
 			"label": __("Supplier Group"),
 			"fieldtype": "Link",
-			"options": "Supplier Group"
+			"options": "Supplier Group",
+			"hidden": 1
 		},
 		{
 			"fieldname": "group_by_party",
@@ -134,12 +143,6 @@
 			"fieldtype": "Check",
 		},
 		{
-			"fieldname": "tax_id",
-			"label": __("Tax Id"),
-			"fieldtype": "Data",
-			"hidden": 1
-		},
-		{
 			"fieldname": "show_future_payments",
 			"label": __("Show Future Payments"),
 			"fieldtype": "Check",
diff --git a/erpnext/accounts/report/accounts_payable/test_accounts_payable.py b/erpnext/accounts/report/accounts_payable/test_accounts_payable.py
new file mode 100644
index 0000000..cb84cf4
--- /dev/null
+++ b/erpnext/accounts/report/accounts_payable/test_accounts_payable.py
@@ -0,0 +1,67 @@
+import unittest
+
+import frappe
+from frappe.tests.utils import FrappeTestCase, change_settings
+from frappe.utils import add_days, flt, getdate, today
+
+from erpnext import get_default_cost_center
+from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_entry
+from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import make_purchase_invoice
+from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice
+from erpnext.accounts.report.accounts_payable.accounts_payable import execute
+from erpnext.accounts.test.accounts_mixin import AccountsTestMixin
+from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order
+
+
+class TestAccountsReceivable(AccountsTestMixin, FrappeTestCase):
+	def setUp(self):
+		self.create_company()
+		self.create_customer()
+		self.create_item()
+		self.create_supplier(currency="USD", supplier_name="Test Supplier2")
+		self.create_usd_payable_account()
+
+	def tearDown(self):
+		frappe.db.rollback()
+
+	def test_accounts_receivable_with_supplier(self):
+		pi = self.create_purchase_invoice(do_not_submit=True)
+		pi.currency = "USD"
+		pi.conversion_rate = 80
+		pi.credit_to = self.creditors_usd
+		pi = pi.save().submit()
+
+		filters = {
+			"company": self.company,
+			"party_type": "Supplier",
+			"party": self.supplier,
+			"report_date": today(),
+			"range1": 30,
+			"range2": 60,
+			"range3": 90,
+			"range4": 120,
+		}
+
+		data = execute(filters)
+		self.assertEqual(data[1][0].get("outstanding"), 300)
+		self.assertEqual(data[1][0].get("currency"), "USD")
+
+	def create_purchase_invoice(self, do_not_submit=False):
+		frappe.set_user("Administrator")
+		pi = make_purchase_invoice(
+			item=self.item,
+			company=self.company,
+			supplier=self.supplier,
+			is_return=False,
+			update_stock=False,
+			posting_date=frappe.utils.datetime.date(2021, 5, 1),
+			do_not_save=1,
+			rate=300,
+			price_list_rate=300,
+			qty=1,
+		)
+
+		pi = pi.save()
+		if not do_not_submit:
+			pi = pi.submit()
+		return pi
diff --git a/erpnext/accounts/report/accounts_receivable/accounts_receivable.js b/erpnext/accounts/report/accounts_receivable/accounts_receivable.js
index 0b4e577..cb8ec87 100644
--- a/erpnext/accounts/report/accounts_receivable/accounts_receivable.js
+++ b/erpnext/accounts/report/accounts_receivable/accounts_receivable.js
@@ -46,8 +46,7 @@
 				var customer = frappe.query_report.get_filter_value('customer');
 				var company = frappe.query_report.get_filter_value('company');
 				if (customer) {
-					frappe.db.get_value('Customer', customer, ["tax_id", "customer_name", "payment_terms"], function(value) {
-						frappe.query_report.set_filter_value('tax_id', value["tax_id"]);
+					frappe.db.get_value('Customer', customer, ["customer_name", "payment_terms"], function(value) {
 						frappe.query_report.set_filter_value('customer_name', value["customer_name"]);
 						frappe.query_report.set_filter_value('payment_terms', value["payment_terms"]);
 					});
@@ -59,7 +58,6 @@
 						}
 					}, "Customer");
 				} else {
-					frappe.query_report.set_filter_value('tax_id', "");
 					frappe.query_report.set_filter_value('customer_name', "");
 					frappe.query_report.set_filter_value('credit_limit', "");
 					frappe.query_report.set_filter_value('payment_terms', "");
@@ -173,12 +171,6 @@
 			"fieldtype": "Check",
 		},
 		{
-			"fieldname": "tax_id",
-			"label": __("Tax Id"),
-			"fieldtype": "Data",
-			"hidden": 1
-		},
-		{
 			"fieldname": "show_remarks",
 			"label": __("Show Remarks"),
 			"fieldtype": "Check",
diff --git a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
index 751063a..3700f00 100755
--- a/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
+++ b/erpnext/accounts/report/accounts_receivable/accounts_receivable.py
@@ -211,11 +211,10 @@
 			return
 
 		# amount in "Party Currency", if its supplied. If not, amount in company currency
-		for party_type in self.party_type:
-			if self.filters.get(scrub(party_type)):
-				amount = ple.amount_in_account_currency
-			else:
-				amount = ple.amount
+		if self.filters.get("party_type") and self.filters.get("party"):
+			amount = ple.amount_in_account_currency
+		else:
+			amount = ple.amount
 		amount_in_account_currency = ple.amount_in_account_currency
 
 		# update voucher
@@ -426,10 +425,9 @@
 		# customer / supplier name
 		party_details = self.get_party_details(row.party) or {}
 		row.update(party_details)
-		for party_type in self.party_type:
-			if self.filters.get(scrub(party_type)):
-				row.currency = row.account_currency
-				break
+
+		if self.filters.get("party_type") and self.filters.get("party"):
+			row.currency = row.account_currency
 		else:
 			row.currency = self.company_currency
 
@@ -765,6 +763,7 @@
 	def prepare_conditions(self):
 		self.qb_selection_filter = []
 		self.or_filters = []
+
 		for party_type in self.party_type:
 			party_type_field = scrub(party_type)
 			self.or_filters.append(self.ple.party_type == party_type)
@@ -800,6 +799,12 @@
 		if self.filters.get(party_type_field):
 			self.qb_selection_filter.append(self.ple.party == self.filters.get(party_type_field))
 
+		if self.filters.get("party_type"):
+			self.qb_selection_filter.append(self.filters.party_type == self.ple.party_type)
+
+		if self.filters.get("party"):
+			self.qb_selection_filter.append(self.filters.party == self.ple.party)
+
 		if self.filters.party_account:
 			self.qb_selection_filter.append(self.ple.account == self.filters.party_account)
 		else:
diff --git a/erpnext/accounts/report/balance_sheet/balance_sheet.js b/erpnext/accounts/report/balance_sheet/balance_sheet.js
index c65b9e8..ecc13d7 100644
--- a/erpnext/accounts/report/balance_sheet/balance_sheet.js
+++ b/erpnext/accounts/report/balance_sheet/balance_sheet.js
@@ -15,7 +15,6 @@
 		fieldtype: "Check",
 		default: 1,
 	});
-	console.log(frappe.query_reports["Balance Sheet"]["filters"]);
 
 	frappe.query_reports["Balance Sheet"]["filters"].push({
 		fieldname: "include_default_book_entries",
diff --git a/erpnext/accounts/test/accounts_mixin.py b/erpnext/accounts/test/accounts_mixin.py
index bf01362..0868860 100644
--- a/erpnext/accounts/test/accounts_mixin.py
+++ b/erpnext/accounts/test/accounts_mixin.py
@@ -126,6 +126,28 @@
 			acc = frappe.get_doc("Account", name)
 		self.debtors_usd = acc.name
 
+	def create_usd_payable_account(self):
+		account_name = "Creditors USD"
+		if not frappe.db.get_value(
+			"Account", filters={"account_name": account_name, "company": self.company}
+		):
+			acc = frappe.new_doc("Account")
+			acc.account_name = account_name
+			acc.parent_account = "Accounts Payable - " + self.company_abbr
+			acc.company = self.company
+			acc.account_currency = "USD"
+			acc.account_type = "Payable"
+			acc.insert()
+		else:
+			name = frappe.db.get_value(
+				"Account",
+				filters={"account_name": account_name, "company": self.company},
+				fieldname="name",
+				pluck=True,
+			)
+			acc = frappe.get_doc("Account", name)
+		self.creditors_usd = acc.name
+
 	def clear_old_entries(self):
 		doctype_list = [
 			"GL Entry",