refactor: get default contact or address (#35248)

* refactor: get_party_shipping_address

* refactor: get_default_contact

* chore: adding docstrings

* fix: keep original order

* fix: use get_all instead of get_list

---------

Co-authored-by: ruthra kumar <ruthra@erpnext.com>
This commit is contained in:
Raffael Meyer
2023-06-08 16:19:09 +02:00
committed by GitHub
parent 9cd982aa54
commit b91bb17779

View File

@@ -2,6 +2,8 @@
# License: GNU General Public License v3. See license.txt # License: GNU General Public License v3. See license.txt
from typing import Optional
import frappe import frappe
from frappe import _, msgprint, scrub from frappe import _, msgprint, scrub
from frappe.contacts.doctype.address.address import ( from frappe.contacts.doctype.address.address import (
@@ -850,7 +852,7 @@ def get_dashboard_info(party_type, party, loyalty_program=None):
return company_wise_info return company_wise_info
def get_party_shipping_address(doctype, name): def get_party_shipping_address(doctype: str, name: str) -> Optional[str]:
""" """
Returns an Address name (best guess) for the given doctype and name for which `address_type == 'Shipping'` is true. Returns an Address name (best guess) for the given doctype and name for which `address_type == 'Shipping'` is true.
and/or `is_shipping_address = 1`. and/or `is_shipping_address = 1`.
@@ -861,22 +863,23 @@ def get_party_shipping_address(doctype, name):
:param name: Party name :param name: Party name
:return: String :return: String
""" """
out = frappe.db.sql( shipping_addresses = frappe.get_all(
"SELECT dl.parent " "Address",
"from `tabDynamic Link` dl join `tabAddress` ta on dl.parent=ta.name " filters=[
"where " ["Dynamic Link", "link_doctype", "=", doctype],
"dl.link_doctype=%s " ["Dynamic Link", "link_name", "=", name],
"and dl.link_name=%s " ["disabled", "=", 0],
"and dl.parenttype='Address' " ],
"and ifnull(ta.disabled, 0) = 0 and" or_filters=[
"(ta.address_type='Shipping' or ta.is_shipping_address=1) " ["is_shipping_address", "=", 1],
"order by ta.is_shipping_address desc, ta.address_type desc limit 1", ["address_type", "=", "Shipping"],
(doctype, name), ],
pluck="name",
limit=1,
order_by="is_shipping_address DESC",
) )
if out:
return out[0][0] return shipping_addresses[0] if shipping_addresses else None
else:
return ""
def get_partywise_advanced_payment_amount( def get_partywise_advanced_payment_amount(
@@ -910,31 +913,32 @@ def get_partywise_advanced_payment_amount(
return frappe._dict(data) return frappe._dict(data)
def get_default_contact(doctype, name): def get_default_contact(doctype: str, name: str) -> Optional[str]:
""" """
Returns default contact for the given doctype and name. Returns contact name only if there is a primary contact for given doctype and name.
Can be ordered by `contact_type` to either is_primary_contact or is_billing_contact.
Else returns None
:param doctype: Party Doctype
:param name: Party name
:return: String
""" """
out = frappe.db.sql( contacts = frappe.get_all(
""" "Contact",
SELECT dl.parent, c.is_primary_contact, c.is_billing_contact filters=[
FROM `tabDynamic Link` dl ["Dynamic Link", "link_doctype", "=", doctype],
INNER JOIN `tabContact` c ON c.name = dl.parent ["Dynamic Link", "link_name", "=", name],
WHERE ],
dl.link_doctype=%s AND or_filters=[
dl.link_name=%s AND ["is_primary_contact", "=", 1],
dl.parenttype = 'Contact' ["is_billing_contact", "=", 1],
ORDER BY is_primary_contact DESC, is_billing_contact DESC ],
""", pluck="name",
(doctype, name), limit=1,
order_by="is_primary_contact DESC, is_billing_contact DESC",
) )
if out:
try: return contacts[0] if contacts else None
return out[0][0]
except Exception:
return None
else:
return None
def add_party_account(party_type, party, company, account): def add_party_account(party_type, party, company, account):