Merge pull request #39332 from ruthra-kumar/validate_subscription_billing_currency
refactor: prevent foreign currency subscription for a party
diff --git a/erpnext/accounts/doctype/subscription/subscription.py b/erpnext/accounts/doctype/subscription/subscription.py
index 72e574c..ce56a7b 100644
--- a/erpnext/accounts/doctype/subscription/subscription.py
+++ b/erpnext/accounts/doctype/subscription/subscription.py
@@ -16,6 +16,7 @@
date_diff,
flt,
get_last_day,
+ get_link_to_form,
getdate,
nowdate,
)
@@ -317,6 +318,37 @@
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`
diff --git a/erpnext/accounts/doctype/subscription/test_subscription.py b/erpnext/accounts/doctype/subscription/test_subscription.py
index 785fd04..37326fd 100644
--- a/erpnext/accounts/doctype/subscription/test_subscription.py
+++ b/erpnext/accounts/doctype/subscription/test_subscription.py
@@ -463,7 +463,7 @@
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 @@
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"
diff --git a/erpnext/accounts/doctype/subscription_plan/subscription_plan.json b/erpnext/accounts/doctype/subscription_plan/subscription_plan.json
index 563df79..bc1f579 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 118d254..cdfa3e5 100644
--- a/erpnext/accounts/doctype/subscription_plan/subscription_plan.py
+++ b/erpnext/accounts/doctype/subscription_plan/subscription_plan.py
@@ -24,7 +24,7 @@
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