feat: Add validation to ensure lowest tier has min_spent of 0 in LoyaltyProgram

This commit is contained in:
David (aider)
2024-09-05 12:46:09 +02:00
committed by David
parent 4bd26b845e
commit efd8f1e978
2 changed files with 15 additions and 6 deletions

View File

@@ -36,7 +36,17 @@ class LoyaltyProgram(Document):
to_date: DF.Date | None to_date: DF.Date | None
# end: auto-generated types # 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( def get_loyalty_details(

View File

@@ -39,9 +39,7 @@ class TestLoyaltyProgram(unittest.TestCase):
) )
self.assertEqual(si_original.get("loyalty_program"), customer.loyalty_program) self.assertEqual(si_original.get("loyalty_program"), customer.loyalty_program)
self.assertEqual( self.assertEqual(lpe.get("loyalty_program_tier"), "Bronce") # is always in the first tier
lpe.get("loyalty_program_tier"), None
) # 10000 does not surpass the first tier (11000)
self.assertEqual(lpe.get("loyalty_program_tier"), customer.loyalty_program_tier) self.assertEqual(lpe.get("loyalty_program_tier"), customer.loyalty_program_tier)
self.assertEqual(lpe.loyalty_points, earned_points) self.assertEqual(lpe.loyalty_points, earned_points)
@@ -220,7 +218,7 @@ class TestLoyaltyProgram(unittest.TestCase):
"collection_rules": [ "collection_rules": [
{"tier_name": "Gold", "collection_factor": 1000, "min_spent": 20000}, {"tier_name": "Gold", "collection_factor": 1000, "min_spent": 20000},
{"tier_name": "Silver", "collection_factor": 1000, "min_spent": 10000}, {"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", "company": "_Test Company",
"cost_center": "Main - _TC", "cost_center": "Main - _TC",
"expense_account": "Loyalty - _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() ).insert()
@@ -385,6 +383,7 @@ def create_records():
"cost_center": "Main - _TC", "cost_center": "Main - _TC",
"expense_account": "Loyalty - _TC", "expense_account": "Loyalty - _TC",
"collection_rules": [ "collection_rules": [
{"tier_name": "Bronze", "collection_factor": 1000, "min_spent": 0},
{"tier_name": "Silver", "collection_factor": 1000, "min_spent": 10000}, {"tier_name": "Silver", "collection_factor": 1000, "min_spent": 10000},
{"tier_name": "Gold", "collection_factor": 1000, "min_spent": 19000}, {"tier_name": "Gold", "collection_factor": 1000, "min_spent": 19000},
], ],