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: use max function to get default company address #34116

Merged
merged 3 commits into from
Mar 14, 2023
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
2 changes: 1 addition & 1 deletion erpnext/setup/doctype/company/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ def get_default_company_address(name, sort_key="is_primary_address", existing_ad
return existing_address

if out:
return min(out, key=lambda x: x[1])[0] # find min by sort_key
return max(out, key=lambda x: x[1])[0] # find max by sort_key
else:
return None

Expand Down
33 changes: 33 additions & 0 deletions erpnext/setup/doctype/company/test_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from erpnext.accounts.doctype.account.chart_of_accounts.chart_of_accounts import (
get_charts_for_country,
)
from erpnext.setup.doctype.company.company import get_default_company_address

test_ignore = ["Account", "Cost Center", "Payment Terms Template", "Salary Component", "Warehouse"]
test_dependencies = ["Fiscal Year"]
Expand Down Expand Up @@ -132,6 +133,38 @@ def test_basic_tree(self, records=None):
self.assertTrue(lft >= min_lft)
self.assertTrue(rgt <= max_rgt)

def test_primary_address(self):
company = "_Test Company"

secondary = frappe.get_doc(
{
"address_title": "Non Primary",
"doctype": "Address",
"address_type": "Billing",
"address_line1": "Something",
"city": "Mumbai",
"state": "Maharashtra",
"country": "India",
"is_primary_address": 1,
"pincode": "400098",
"links": [
{
"link_doctype": "Company",
"link_name": company,
}
],
}
)
secondary.insert()
self.addCleanup(secondary.delete)

primary = frappe.copy_doc(secondary)
primary.is_primary_address = 1
primary.insert()
self.addCleanup(primary.delete)

self.assertEqual(get_default_company_address(company), primary.name)

def get_no_of_children(self, company):
def get_no_of_children(companies, no_of_children):
children = []
Expand Down