Skip to content

Commit

Permalink
fix(Dunning): logic for fetching text (backport frappe#43160) (frappe…
Browse files Browse the repository at this point in the history
…#43490)

Co-authored-by: Raffael Meyer <14891507+barredterra@users.noreply.github.com>
fix(Dunning): logic for fetching text (frappe#43160)
  • Loading branch information
2 people authored and khalandarsihan committed Oct 6, 2024
1 parent 1b816aa commit 1a29ac6
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions erpnext/accounts/doctype/dunning/dunning.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,31 @@ def get_linked_dunnings_as_per_state(sales_invoice, state):


@frappe.whitelist()
def get_dunning_letter_text(dunning_type, doc, language=None):
def get_dunning_letter_text(dunning_type: str, doc: str | dict, language: str | None = None) -> dict:
DOCTYPE = "Dunning Letter Text"
FIELDS = ["body_text", "closing_text", "language"]

if isinstance(doc, str):
doc = json.loads(doc)

if not language:
language = doc.get("language")

if language:
filters = {"parent": dunning_type, "language": language}
else:
filters = {"parent": dunning_type, "is_default_language": 1}
letter_text = frappe.db.get_value(
"Dunning Letter Text", filters, ["body_text", "closing_text", "language"], as_dict=1
)
if letter_text:
return {
"body_text": frappe.render_template(letter_text.body_text, doc),
"closing_text": frappe.render_template(letter_text.closing_text, doc),
"language": letter_text.language,
}
letter_text = frappe.db.get_value(
DOCTYPE, {"parent": dunning_type, "language": language}, FIELDS, as_dict=1
)

if not letter_text:
letter_text = frappe.db.get_value(
DOCTYPE, {"parent": dunning_type, "is_default_language": 1}, FIELDS, as_dict=1
)

if not letter_text:
return {}

return {
"body_text": frappe.render_template(letter_text.body_text, doc),
"closing_text": frappe.render_template(letter_text.closing_text, doc),
"language": letter_text.language,
}

0 comments on commit 1a29ac6

Please sign in to comment.