From 6b5fa2c673accab13a174a2945b333bd1b991cdb Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 12 Jan 2024 12:43:54 +0530 Subject: [PATCH 1/3] refactor: prevent foreign currency subscription for a party --- .../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 72e574c299..ce56a7b0a2 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` From 19975dcb7bb688f5b93b8e7757d6a906cb68a2bc Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Sun, 14 Jan 2024 17:59:43 +0530 Subject: [PATCH 2/3] refactor: making currency mandatory for subcscription plans --- .../doctype/subscription_plan/subscription_plan.json | 7 +++++-- .../doctype/subscription_plan/subscription_plan.py | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/erpnext/accounts/doctype/subscription_plan/subscription_plan.json b/erpnext/accounts/doctype/subscription_plan/subscription_plan.json index 563df79eec..bc1f579cf0 100644 --- a/erpnext/accounts/doctype/subscription_plan/subscription_plan.json +++ b/erpnext/accounts/doctype/subscription_plan/subscription_plan.json @@ -41,7 +41,8 @@ "fieldname": "currency", "fieldtype": "Link", "label": "Currency", - "options": "Currency" + "options": "Currency", + "reqd": 1 }, { "fieldname": "column_break_3", @@ -148,10 +149,11 @@ } ], "links": [], - "modified": "2021-12-10 15:24:15.794477", + "modified": "2024-01-14 17:59:34.687977", "modified_by": "Administrator", "module": "Accounts", "name": "Subscription Plan", + "naming_rule": "By fieldname", "owner": "Administrator", "permissions": [ { @@ -193,5 +195,6 @@ ], "sort_field": "modified", "sort_order": "DESC", + "states": [], "track_changes": 1 } \ No newline at end of file diff --git a/erpnext/accounts/doctype/subscription_plan/subscription_plan.py b/erpnext/accounts/doctype/subscription_plan/subscription_plan.py index 118d254780..cdfa3e56d9 100644 --- a/erpnext/accounts/doctype/subscription_plan/subscription_plan.py +++ b/erpnext/accounts/doctype/subscription_plan/subscription_plan.py @@ -24,7 +24,7 @@ class SubscriptionPlan(Document): billing_interval_count: DF.Int cost: DF.Currency cost_center: DF.Link | None - currency: DF.Link | None + currency: DF.Link item: DF.Link payment_gateway: DF.Link | None plan_name: DF.Data From 1387b0ba7f9edf71f838a270e5c8aa40b30b3a70 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Sun, 14 Jan 2024 18:00:45 +0530 Subject: [PATCH 3/3] refactor(test): supply default currency for subscription plans --- .../doctype/subscription/test_subscription.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/erpnext/accounts/doctype/subscription/test_subscription.py b/erpnext/accounts/doctype/subscription/test_subscription.py index 785fd04b82..37326fd035 100644 --- a/erpnext/accounts/doctype/subscription/test_subscription.py +++ b/erpnext/accounts/doctype/subscription/test_subscription.py @@ -463,7 +463,7 @@ class TestSubscription(FrappeTestCase): subscription = create_subscription( start_date="2018-01-01", generate_invoice_at="Beginning of the current subscription period", - plans=[{"plan": "_Test Plan Multicurrency", "qty": 1}], + plans=[{"plan": "_Test Plan Multicurrency", "qty": 1, "currency": "USD"}], party="_Test Subscription Customer", ) @@ -528,13 +528,21 @@ class TestSubscription(FrappeTestCase): def make_plans(): - create_plan(plan_name="_Test Plan Name", cost=900) - create_plan(plan_name="_Test Plan Name 2", cost=1999) + create_plan(plan_name="_Test Plan Name", cost=900, currency="INR") + create_plan(plan_name="_Test Plan Name 2", cost=1999, currency="INR") create_plan( - plan_name="_Test Plan Name 3", cost=1999, billing_interval="Day", billing_interval_count=14 + plan_name="_Test Plan Name 3", + cost=1999, + billing_interval="Day", + billing_interval_count=14, + currency="INR", ) create_plan( - plan_name="_Test Plan Name 4", cost=20000, billing_interval="Month", billing_interval_count=3 + plan_name="_Test Plan Name 4", + cost=20000, + billing_interval="Month", + billing_interval_count=3, + currency="INR", ) create_plan( plan_name="_Test Plan Multicurrency", cost=50, billing_interval="Month", currency="USD"