From cde5090ca179cf27a30a1ea2004cd0ab22307355 Mon Sep 17 00:00:00 2001 From: david-s73 Date: Mon, 5 Feb 2024 12:58:30 +0100 Subject: [PATCH] [MIG] pricelist_brand: Migration to 16.0 --- pricelist_brand/__manifest__.py | 2 +- pricelist_brand/models/product_pricelist.py | 65 ++++++++----------- .../tests/test_product_pricelist.py | 27 ++++---- 3 files changed, 42 insertions(+), 52 deletions(-) diff --git a/pricelist_brand/__manifest__.py b/pricelist_brand/__manifest__.py index 1645144c1..49fdd4771 100644 --- a/pricelist_brand/__manifest__.py +++ b/pricelist_brand/__manifest__.py @@ -4,7 +4,7 @@ { "name": "Pricelist Brand", "summary": """This module allows to apply pricelist items on brand""", - "version": "13.0.1.0.0", + "version": "16.0.1.0.0", "license": "AGPL-3", "author": "ACSONE SA/NV,Odoo Community Association (OCA)", "website": "https://github.com/OCA/brand", diff --git a/pricelist_brand/models/product_pricelist.py b/pricelist_brand/models/product_pricelist.py index 24f0106ed..61223f582 100644 --- a/pricelist_brand/models/product_pricelist.py +++ b/pricelist_brand/models/product_pricelist.py @@ -3,48 +3,35 @@ from odoo import _, api, fields, models from odoo.exceptions import ValidationError +from odoo.osv import expression class ProductPricelist(models.Model): _inherit = "product.pricelist" - def _compute_price_rule_get_items( - self, products_qty_partner, date, uom_id, prod_tmpl_ids, prod_ids, categ_ids - ): - self.ensure_one() - product_tmpls = self.env["product.template"].browse(prod_tmpl_ids) - brand_ids = product_tmpls.mapped("product_brand_id").ids - # Load all rules - self.env["product.pricelist.item"].flush(["price", "currency_id", "company_id"]) - self.env.cr.execute( - """ - SELECT - item.id - FROM - product_pricelist_item AS item - LEFT JOIN product_category AS categ ON item.categ_id = categ.id - LEFT JOIN product_brand AS brand ON item.product_brand_id = brand.id - WHERE - (item.product_tmpl_id IS NULL OR item.product_tmpl_id = any(%s)) - AND (item.product_id IS NULL OR item.product_id = any(%s)) - AND (item.categ_id IS NULL OR item.categ_id = any(%s)) - AND (item.product_brand_id IS NULL OR item.product_brand_id = any(%s)) - AND (item.pricelist_id = %s) - AND (item.date_start IS NULL OR item.date_start<=%s) - AND (item.date_end IS NULL OR item.date_end>=%s) - ORDER BY - item.applied_on, item.min_quantity desc, - categ.complete_name desc, item.id desc - """, - (prod_tmpl_ids, prod_ids, categ_ids, brand_ids, self.id, date, date), + def _get_applicable_rules_domain(self, products, date, **kwargs): + res = super()._get_applicable_rules_domain(products, date) + print(res) + if products._name == "product.template": + brand_domain = ("product_brand_id", "in", products.product_brand_id.ids) + else: + brand_domain = ( + "product_brand_id", + "in", + products.product_tmpl_id.product_brand_id.ids, + ) + res = expression.AND( + [ + res, + [ + ("|"), + ("product_brand_id", "=", False), + (brand_domain), + ], + ] ) - # NOTE: if you change `order by` on that query, make sure it matches - # _order from model to avoid inconstencies and undeterministic issues. - - item_ids = [x[0] for x in self.env.cr.fetchall()] - return self.env["product.pricelist.item"].browse(item_ids) - + return res class ProductPricelistItem(models.Model): @@ -57,7 +44,9 @@ class ProductPricelistItem(models.Model): help="Specify a brand if this rule only applies to products" "belonging to this brand. Keep empty otherwise.", ) - applied_on = fields.Selection(selection_add=[("25_brand", "Brand")]) + applied_on = fields.Selection( + selection_add=[("25_brand", "Brand")], ondelete={"25_brand": "set default"} + ) @api.constrains("product_id", "product_tmpl_id", "categ_id", "product_brand_id") def _check_product_consistency(self): @@ -88,8 +77,8 @@ def _get_pricelist_item_name_price(self): item.name = _("Brand: %s") % (item.product_brand_id.display_name) @api.onchange("product_id", "product_tmpl_id", "categ_id", "product_brand_id") - def _onchane_rule_content(self): - super(ProductPricelistItem, self)._onchane_rule_content() + def _onchange_rule_content(self): + super(ProductPricelistItem, self)._onchange_rule_content() @api.model_create_multi def create(self, vals_list): diff --git a/pricelist_brand/tests/test_product_pricelist.py b/pricelist_brand/tests/test_product_pricelist.py index 56bd87d90..03c39fa5b 100644 --- a/pricelist_brand/tests/test_product_pricelist.py +++ b/pricelist_brand/tests/test_product_pricelist.py @@ -7,18 +7,19 @@ class TestProductPricelist(TransactionCase): - def setUp(self): - super(TestProductPricelist, self).setUp() - self.product_brand_obj = self.env["product.brand"] - self.product_brand = self.env["product.brand"].create( + @classmethod + def setUpClass(cls): + super(TestProductPricelist, cls).setUpClass() + cls.product_brand_obj = cls.env["product.brand"] + cls.product_brand = cls.env["product.brand"].create( {"name": "Test Brand", "description": "Test brand description"} ) - self.product = self.env.ref("product.product_product_4") - self.product.write({"product_brand_id": self.product_brand.id}) - self.product_2 = self.env.ref("product.product_product_5") + cls.product = cls.env.ref("product.product_product_4") + cls.product.write({"product_brand_id": cls.product_brand.id}) + cls.product_2 = cls.env.ref("product.product_product_5") - self.list0 = self.ref("product.list0") - self.pricelist = self.env["product.pricelist"].create( + cls.list0 = cls.env.ref("product.list0") + cls.pricelist = cls.env["product.pricelist"].create( { "name": "Test Pricelist", "item_ids": [ @@ -29,7 +30,7 @@ def setUp(self): "name": "Default pricelist", "compute_price": "formula", "base": "pricelist", - "base_pricelist_id": self.list0, + "base_pricelist_id": cls.list0.id, }, ), ( @@ -38,7 +39,7 @@ def setUp(self): { "name": "10% Discount on Test Brand", "applied_on": "25_brand", - "product_brand_id": self.product_brand.id, + "product_brand_id": cls.product_brand.id, "compute_price": "formula", "base": "list_price", "price_discount": 10, @@ -115,7 +116,7 @@ def test_calculation_price_of_products_pricelist(self): product_with_context = self.product.with_context(context) self.assertEqual( float_compare( - product_with_context.price, + product_with_context._get_contextual_price(), ( product_with_context.lst_price - product_with_context.lst_price * (0.10) @@ -129,7 +130,7 @@ def test_calculation_price_of_products_pricelist(self): product_2_with_context = self.product_2.with_context(context) self.assertEqual( float_compare( - product_2_with_context.price, + product_2_with_context._get_contextual_price(), product_2_with_context.lst_price, precision_digits=2, ),