From efd8f1e978ddad5dd5011b43d5832ac4d041775b Mon Sep 17 00:00:00 2001 From: "David (aider)" Date: Thu, 5 Sep 2024 12:46:09 +0200 Subject: [PATCH] feat: Add validation to ensure lowest tier has min_spent of 0 in LoyaltyProgram --- .../doctype/loyalty_program/loyalty_program.py | 12 +++++++++++- .../doctype/loyalty_program/test_loyalty_program.py | 9 ++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/erpnext/accounts/doctype/loyalty_program/loyalty_program.py b/erpnext/accounts/doctype/loyalty_program/loyalty_program.py index 240866df8de..6bf08e04e52 100644 --- a/erpnext/accounts/doctype/loyalty_program/loyalty_program.py +++ b/erpnext/accounts/doctype/loyalty_program/loyalty_program.py @@ -36,7 +36,17 @@ class LoyaltyProgram(Document): to_date: DF.Date | None # end: auto-generated types - pass + def validate(self): + self.validate_lowest_tier() + + def validate_lowest_tier(self): + tiers = sorted(self.collection_rules, key=lambda x: x.min_spent) + if tiers and tiers[0].min_spent != 0: + frappe.throw( + _( + "The lowest tier must have a minimum spent amount of 0. Customers need to be part of a tier as soon as they are enrolled in the program." + ) + ) def get_loyalty_details( diff --git a/erpnext/accounts/doctype/loyalty_program/test_loyalty_program.py b/erpnext/accounts/doctype/loyalty_program/test_loyalty_program.py index af8b38cca91..322ece836f0 100644 --- a/erpnext/accounts/doctype/loyalty_program/test_loyalty_program.py +++ b/erpnext/accounts/doctype/loyalty_program/test_loyalty_program.py @@ -39,9 +39,7 @@ class TestLoyaltyProgram(unittest.TestCase): ) self.assertEqual(si_original.get("loyalty_program"), customer.loyalty_program) - self.assertEqual( - lpe.get("loyalty_program_tier"), None - ) # 10000 does not surpass the first tier (11000) + self.assertEqual(lpe.get("loyalty_program_tier"), "Bronce") # is always in the first tier self.assertEqual(lpe.get("loyalty_program_tier"), customer.loyalty_program_tier) self.assertEqual(lpe.loyalty_points, earned_points) @@ -220,7 +218,7 @@ class TestLoyaltyProgram(unittest.TestCase): "collection_rules": [ {"tier_name": "Gold", "collection_factor": 1000, "min_spent": 20000}, {"tier_name": "Silver", "collection_factor": 1000, "min_spent": 10000}, - {"tier_name": "Bronze", "collection_factor": 1000, "min_spent": 5000}, + {"tier_name": "Bronze", "collection_factor": 1000, "min_spent": 0}, ], } ) @@ -354,7 +352,7 @@ def create_records(): "company": "_Test Company", "cost_center": "Main - _TC", "expense_account": "Loyalty - _TC", - "collection_rules": [{"tier_name": "Silver", "collection_factor": 1000, "min_spent": 1000}], + "collection_rules": [{"tier_name": "Bronce", "collection_factor": 1000, "min_spent": 0}], } ).insert() @@ -385,6 +383,7 @@ def create_records(): "cost_center": "Main - _TC", "expense_account": "Loyalty - _TC", "collection_rules": [ + {"tier_name": "Bronze", "collection_factor": 1000, "min_spent": 0}, {"tier_name": "Silver", "collection_factor": 1000, "min_spent": 10000}, {"tier_name": "Gold", "collection_factor": 1000, "min_spent": 19000}, ],