From 61f073f8b1a68b6fedcb049de460b1d35cb95c66 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 12 Jan 2024 12:43:54 +0530 Subject: [PATCH] refactor: prevent foreign currency subscription for a party (cherry picked from commit 6b5fa2c673accab13a174a2945b333bd1b991cdb) --- .../doctype/subscription/subscription.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/erpnext/accounts/doctype/subscription/subscription.py b/erpnext/accounts/doctype/subscription/subscription.py index 94f5e29f06..20c9167bc9 100644 --- a/erpnext/accounts/doctype/subscription/subscription.py +++ b/erpnext/accounts/doctype/subscription/subscription.py @@ -16,6 +16,7 @@ from frappe.utils.data import ( date_diff, flt, get_last_day, + get_link_to_form, getdate, nowdate, ) @@ -317,6 +318,37 @@ class Subscription(Document): if self.is_new(): self.set_subscription_status() + self.validate_party_billing_currency() + + def validate_party_billing_currency(self): + """ + Subscription should be of the same currency as the Party's default billing currency or company default. + """ + if self.party: + party_billing_currency = frappe.get_cached_value( + self.party_type, self.party, "default_currency" + ) or frappe.get_cached_value("Company", self.company, "default_currency") + + plans = [x.plan for x in self.plans] + subscription_plan_currencies = frappe.db.get_all( + "Subscription Plan", filters={"name": ("in", plans)}, fields=["name", "currency"] + ) + unsupported_plans = [] + for x in subscription_plan_currencies: + if x.currency != party_billing_currency: + unsupported_plans.append("{0}".format(get_link_to_form("Subscription Plan", x.name))) + + if unsupported_plans: + unsupported_plans = [ + _( + "Below Subscription Plans are of different currency to the party default billing currency/Company currency: {0}" + ).format(frappe.bold(party_billing_currency)) + ] + unsupported_plans + + frappe.throw( + unsupported_plans, frappe.ValidationError, "Unsupported Subscription Plans", as_list=True + ) + def validate_trial_period(self) -> None: """ Runs sanity checks on trial period dates for the `Subscription`