Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: account payable currency and value (backport #36859) #36912

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions erpnext/accounts/report/accounts_payable/accounts_payable.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,6 @@ frappe.query_reports["Accounts Payable"] = {
}
}
},
{
"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"),
Expand Down Expand Up @@ -112,11 +94,38 @@ frappe.query_reports["Accounts Payable"] = {
"fieldtype": "Link",
"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",
Expand All @@ -133,12 +142,6 @@ frappe.query_reports["Accounts Payable"] = {
"label": __("Show Remarks"),
"fieldtype": "Check",
},
{
"fieldname": "tax_id",
"label": __("Tax Id"),
"fieldtype": "Data",
"hidden": 1
},
{
"fieldname": "show_future_payments",
"label": __("Show Future Payments"),
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ frappe.query_reports["Accounts Receivable"] = {
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"]);
});
Expand All @@ -59,7 +58,6 @@ frappe.query_reports["Accounts Receivable"] = {
}
}, "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', "");
Expand Down Expand Up @@ -172,12 +170,6 @@ frappe.query_reports["Accounts Receivable"] = {
"label": __("Show Sales Person"),
"fieldtype": "Check",
},
{
"fieldname": "tax_id",
"label": __("Tax Id"),
"fieldtype": "Data",
"hidden": 1
},
{
"fieldname": "show_remarks",
"label": __("Show Remarks"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,10 @@ def update_voucher_balance(self, ple):
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
Expand Down Expand Up @@ -426,10 +425,9 @@ def set_party_details(self, row):
# 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

Expand Down Expand Up @@ -765,6 +763,7 @@ def get_sales_invoices_or_customers_based_on_sales_person(self):
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)
Expand Down Expand Up @@ -800,6 +799,12 @@ def add_common_filters(self, party_type_field):
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:
Expand Down
22 changes: 22 additions & 0 deletions erpnext/accounts/test/accounts_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,28 @@ def create_usd_receivable_account(self):
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",
Expand Down