From 1a29ac63ba76e0bb10989b7e9965579096214dcc Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 3 Oct 2024 20:28:53 +0100 Subject: [PATCH] fix(Dunning): logic for fetching text (backport #43160) (#43490) Co-authored-by: Raffael Meyer <14891507+barredterra@users.noreply.github.com> fix(Dunning): logic for fetching text (#43160) --- erpnext/accounts/doctype/dunning/dunning.py | 38 ++++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/erpnext/accounts/doctype/dunning/dunning.py b/erpnext/accounts/doctype/dunning/dunning.py index ad58761e1bfd..d63db3a09a11 100644 --- a/erpnext/accounts/doctype/dunning/dunning.py +++ b/erpnext/accounts/doctype/dunning/dunning.py @@ -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, + }