Skip to content

Commit

Permalink
[MIG] pricelist_brand: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
david-s73 committed Feb 5, 2024
1 parent 6288dd2 commit cde5090
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 52 deletions.
2 changes: 1 addition & 1 deletion pricelist_brand/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
65 changes: 27 additions & 38 deletions pricelist_brand/models/product_pricelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 17 in pricelist_brand/models/product_pricelist.py

View check run for this annotation

Codecov / codecov/patch

pricelist_brand/models/product_pricelist.py#L17

Added line #L17 was not covered by tests
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):

Expand All @@ -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):
Expand Down Expand Up @@ -88,8 +77,8 @@ def _get_pricelist_item_name_price(self):
item.name = _("Brand: %s") % (item.product_brand_id.display_name)

Check warning on line 77 in pricelist_brand/models/product_pricelist.py

View check run for this annotation

Codecov / codecov/patch

pricelist_brand/models/product_pricelist.py#L77

Added line #L77 was not covered by tests

@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()

Check warning on line 81 in pricelist_brand/models/product_pricelist.py

View check run for this annotation

Codecov / codecov/patch

pricelist_brand/models/product_pricelist.py#L81

Added line #L81 was not covered by tests

@api.model_create_multi
def create(self, vals_list):
Expand Down
27 changes: 14 additions & 13 deletions pricelist_brand/tests/test_product_pricelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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,
},
),
(
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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,
),
Expand Down

0 comments on commit cde5090

Please sign in to comment.