diff --git a/addons/website_sale/models/product.py b/addons/website_sale/models/product.py index 3705d7b3cb3..c3c641bedff 100644 --- a/addons/website_sale/models/product.py +++ b/addons/website_sale/models/product.py @@ -2,7 +2,7 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. from odoo import api, fields, models, tools, _ -from odoo.exceptions import ValidationError +from odoo.exceptions import ValidationError, UserError from odoo.addons import decimal_precision as dp from odoo.addons.website.models import ir_http from odoo.tools.translate import html_translate @@ -55,12 +55,15 @@ class ProductPricelist(models.Model): @api.multi def write(self, data): res = super(ProductPricelist, self).write(data) + if data.keys() & {'code', 'active', 'website_id', 'selectable'}: + self._check_website_pricelist() self.clear_cache() return res @api.multi def unlink(self): res = super(ProductPricelist, self).unlink() + self._check_website_pricelist() self.clear_cache() return res @@ -78,6 +81,11 @@ class ProductPricelist(models.Model): res = res.filtered(lambda pl: pl._is_available_on_website(website.id)) return res + def _check_website_pricelist(self): + for website in self.env['website'].search([]): + if not website.pricelist_ids: + raise UserError(_("With this action, '%s' website would not have any pricelist available.") % (website.name)) + @api.multi def _is_available_on_website(self, website_id): """ To be able to be used on a website, a pricelist should either: diff --git a/addons/website_sale/tests/test_website_sale_pricelist.py b/addons/website_sale/tests/test_website_sale_pricelist.py index eb3b9d5d1f6..52858e3d8a8 100644 --- a/addons/website_sale/tests/test_website_sale_pricelist.py +++ b/addons/website_sale/tests/test_website_sale_pricelist.py @@ -65,6 +65,7 @@ class TestWebsitePriceList(TransactionCase): self.addCleanup(patcher.stop) def get_pl(self, show, current_pl, country): + self.website.invalidate_cache(['pricelist_ids'], [self.website.id]) pl_ids = self.website._get_pl_partner_order( country, show, @@ -185,7 +186,7 @@ class TestWebsitePriceListAvailable(TransactionCase): self.website2 = Website.create({'name': 'Website 2'}) # Remove existing pricelists and create new ones - Pricelist.search([]).write({'active': False}) + existing_pricelists = Pricelist.search([]) self.backend_pl = Pricelist.create({ 'name': 'Backend Pricelist', 'website_id': False, @@ -230,6 +231,7 @@ class TestWebsitePriceListAvailable(TransactionCase): 'name': 'Website 2 Pricelist', 'website_id': self.website2.id, }) + existing_pricelists.write({'active': False}) simulate_frontend_context(self)