Skip to content

Commit ff569b5

Browse files
committed
[FIX] loyalty: prevent archiving pricelists linked to active loyalty programs
Step to Reproduce: 1. Install `sale_loyalty` and `sale` 2. Enable the `Pricelists` option in the settings. 3. Create a pricelist named demo. 4. Create a Discount & Loyalty named `test program` with type `Discount Code`, 5. Assign the demo pricelist to the loyalty program. 6. Copy the discount code from the program’s conditional rules. 7. Archive the demo pricelist. 8. create sale order for any product. 9. Try to apply the copied coupon code. Observation: - An error is shown: "This code is invalid". - check loyalty program `test program` pricelist field is empty, that suggesting it's valid for all pricelists, but the coupon still fails. Issue: - When a linked pricelist is archived, it's hidden in the UI, but the relation still exists in the relational table. - While filtering the domain for coupon, the condition is not satisfied due to the program's pricelist. solution: - Display a validation error to the user when trying to archive a pricelist that is linked to any active promotional programs. opw-4841678 closes odoo#220603 X-original-commit: fefe16d Signed-off-by: Dhwani Patel (dwa) <dwa@odoo.com>
1 parent 9166e39 commit ff569b5

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

addons/loyalty/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from . import loyalty_program
77
from . import loyalty_reward
88
from . import loyalty_rule
9+
from . import product_pricelist
910
from . import product_product
1011
from . import product_template
1112
from . import res_partner
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from odoo import _, models
2+
from odoo.exceptions import UserError
3+
4+
5+
class ProductPricelist(models.Model):
6+
_inherit = 'product.pricelist'
7+
8+
def action_archive(self):
9+
loyalty_programs = self.env['loyalty.program'].sudo().search([
10+
('active', '=', True),
11+
('pricelist_ids', 'in', self.ids)
12+
])
13+
if loyalty_programs:
14+
raise UserError(_(
15+
"This pricelist may not be archived. "
16+
"It is being used for active promotion programs: %s",
17+
', '.join(loyalty_programs.mapped('name'))
18+
))
19+
return super().action_archive()

addons/loyalty/tests/test_loyalty.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from psycopg2 import IntegrityError
66

7-
from odoo.exceptions import ValidationError
7+
from odoo.exceptions import UserError, ValidationError
88
from odoo.fields import Command
99
from odoo.tests import Form, TransactionCase, tagged
1010
from odoo.tools import mute_logger
@@ -169,6 +169,15 @@ def test_archiving_unarchiving(self):
169169
after_archived_reward_ids = self.program.reward_ids
170170
self.assertEqual(before_archived_reward_ids, after_archived_reward_ids)
171171

172+
def test_prevent_archive_pricelist_linked_to_program(self):
173+
self.program.pricelist_ids = demo_pricelist = self.env['product.pricelist'].create({
174+
'name': "Demo"
175+
})
176+
with self.assertRaises(UserError):
177+
demo_pricelist.action_archive()
178+
self.program.action_archive()
179+
demo_pricelist.action_archive()
180+
172181
def test_prevent_archiving_product_linked_to_active_loyalty_reward(self):
173182
self.program.program_type = 'promotion'
174183
self.program.flush_recordset()

0 commit comments

Comments
 (0)