diff --git a/addons/loyalty/i18n/loyalty.pot b/addons/loyalty/i18n/loyalty.pot index 2dca1d68196..ea28342fda0 100644 --- a/addons/loyalty/i18n/loyalty.pot +++ b/addons/loyalty/i18n/loyalty.pot @@ -1770,6 +1770,16 @@ msgstr "" msgid "You can not delete a program in an active state" msgstr "" +#. module: loyalty +#. odoo-python +#: code:addons/loyalty/models/product_product.py:0 +#: code:addons/loyalty/models/product_template.py:0 +#, python-format +msgid "" +"You cannot delete %(name)s as it is used in 'Coupons & Loyalty'. Please " +"archive it instead." +msgstr "" + #. module: loyalty #: model:mail.template,subject:loyalty.mail_template_gift_card msgid "Your Gift Card at {{ object.company_id.name }}" diff --git a/addons/loyalty/models/__init__.py b/addons/loyalty/models/__init__.py index daf8fafc9d6..25fd40743bb 100644 --- a/addons/loyalty/models/__init__.py +++ b/addons/loyalty/models/__init__.py @@ -7,4 +7,5 @@ from . import loyalty_reward from . import loyalty_rule from . import loyalty_program from . import product_product +from . import product_template from . import res_partner diff --git a/addons/loyalty/models/product_product.py b/addons/loyalty/models/product_product.py index 9193f258283..80fbc4ffe69 100644 --- a/addons/loyalty/models/product_product.py +++ b/addons/loyalty/models/product_product.py @@ -1,8 +1,9 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import _, models -from odoo.exceptions import ValidationError +from odoo import _, api, models +from odoo.exceptions import UserError, ValidationError + class ProductProduct(models.Model): _inherit = 'product.product' @@ -14,3 +15,16 @@ class ProductProduct(models.Model): if rewards: raise ValidationError(_("This product may not be archived. It is being used for an active promotion program.")) return super().write(vals) + + @api.ondelete(at_uninstall=False) + def _unlink_except_loyalty_products(self): + product_data = [ + self.env.ref('loyalty.gift_card_product_50', False), + self.env.ref('loyalty.ewallet_product_50', False), + ] + for product in self.filtered(lambda p: p in product_data): + raise UserError(_( + "You cannot delete %(name)s as it is used in 'Coupons & Loyalty'." + " Please archive it instead.", + name=product.name + )) diff --git a/addons/loyalty/models/product_template.py b/addons/loyalty/models/product_template.py new file mode 100644 index 00000000000..4d7ec6acdbc --- /dev/null +++ b/addons/loyalty/models/product_template.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import _, api, models +from odoo.exceptions import UserError + + +class ProductTemplate(models.Model): + _inherit = 'product.template' + + @api.ondelete(at_uninstall=False) + def _unlink_except_loyalty_products(self): + product_data = [ + self.env.ref('loyalty.gift_card_product_50', False), + self.env.ref('loyalty.ewallet_product_50', False), + ] + for product in self.filtered(lambda p: p.product_variant_id in product_data): + raise UserError(_( + "You cannot delete %(name)s as it is used in 'Coupons & Loyalty'." + " Please archive it instead.", + name=product.name + ))