From 9358a7f550c7403e6a051ca71cd83f9b61aae43a Mon Sep 17 00:00:00 2001 From: "Mahdi Cheikh Rouhou (macr)" Date: Tue, 9 May 2023 11:00:01 +0000 Subject: [PATCH] [FIX] loyalty : unarchive archived rules and rewards When archive a loyalty program which have somes rules and rewards and the unarchinving it will not have the rules and rewards that it had before. Steps to reproduce the error : 1- install sales 2- activate Discounts, Loyalty & Gift Card in sales settings 3- go to sales/products/Discount&loaylty 4- select one of the default programs and try to archive it and unarchive it after 5- you will not get the rules that it had before The problem was in the toggle_active function of the loyalty program we try to unarchive already unarchived items because we do ```program.rule_ids``` so it will get only active items. opw-3299295 closes odoo/odoo#121710 X-original-commit: 1d9fbda1c5ec69ba83968668ddecec6a7e1421bb Signed-off-by: Mahdi Cheikh Rouhou (macr) --- addons/loyalty/models/loyalty_program.py | 7 ++++--- addons/loyalty/tests/test_loyalty.py | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/addons/loyalty/models/loyalty_program.py b/addons/loyalty/models/loyalty_program.py index d17f9057854..5a936d7ef57 100644 --- a/addons/loyalty/models/loyalty_program.py +++ b/addons/loyalty/models/loyalty_program.py @@ -372,13 +372,14 @@ class LoyaltyProgram(models.Model): raise UserError(_('You can not delete a program in an active state')) def toggle_active(self): - super().toggle_active() + res = super().toggle_active() # Propagate active state to children - for program in self: + for program in self.with_context(active_test=False): program.rule_ids.active = program.active program.reward_ids.active = program.active program.communication_plan_ids.active = program.active - program.reward_ids.discount_line_product_id.active = program.active + program.reward_ids.with_context(active_test=True).discount_line_product_id.active = program.active + return res def write(self, vals): # There is an issue when we change the program type, since we clear the rewards and create new ones. diff --git a/addons/loyalty/tests/test_loyalty.py b/addons/loyalty/tests/test_loyalty.py index eb1b566aa76..e3b5d791c3a 100644 --- a/addons/loyalty/tests/test_loyalty.py +++ b/addons/loyalty/tests/test_loyalty.py @@ -111,3 +111,17 @@ class TestLoyalty(TransactionCase): ], }) self.assertTrue(all(r.reward_type == 'product' for r in self.program.reward_ids)) + + def test_archiving_unarchiving(self): + self.program.write({ + 'reward_ids': [ + Command.create({ + 'description': 'Test Product', + }), + ], + }) + before_archived_reward_ids = self.program.reward_ids + self.program.toggle_active() + self.program.toggle_active() + after_archived_reward_ids = self.program.reward_ids + self.assertEqual(before_archived_reward_ids, after_archived_reward_ids)