Skip to content

Commit

Permalink
[MIG] website_sale_product_minimal_price: Migration to version 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-lopez-tecnativa committed Jan 17, 2025
1 parent 07a60f1 commit 46e68a6
Show file tree
Hide file tree
Showing 17 changed files with 260 additions and 347 deletions.
9 changes: 5 additions & 4 deletions website_sale_product_minimal_price/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ Contributors

- `Tecnativa <https://www.tecnativa.com>`__:

- Sergio Teruel
- Carlos Roca
- Pedro M. Baeza
- Pilar Vargas
- Sergio Teruel
- Carlos Roca
- Pedro M. Baeza
- Pilar Vargas
- Carlos Lopez

Maintainers
-----------
Expand Down
1 change: 0 additions & 1 deletion website_sale_product_minimal_price/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import controllers
from . import models
7 changes: 3 additions & 4 deletions website_sale_product_minimal_price/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Website Sale Product Minimal Price",
"summary": "Display minimal price for products that has variants",
"version": "16.0.1.0.0",
"version": "17.0.1.0.0",
"development_status": "Production/Stable",
"maintainers": ["sergio-teruel"],
"category": "Website",
Expand All @@ -16,12 +16,11 @@
"data": ["views/templates.xml"],
"assets": {
"web.assets_frontend": [
"/web/static/src/legacy/js/fields/field_utils.js",
"/website_sale_product_minimal_price/static/src/js/*.esm.js",
"/website_sale_product_minimal_price/static/src/xml/*.xml",
"/website_sale_product_minimal_price/static/src/js/*.js",
],
"web.assets_tests": [
"/website_sale_product_minimal_price/static/src/js/tours/*.js"
"/website_sale_product_minimal_price/static/src/tests/**/*.esm.js"
],
},
}
1 change: 0 additions & 1 deletion website_sale_product_minimal_price/controllers/__init__.py

This file was deleted.

94 changes: 0 additions & 94 deletions website_sale_product_minimal_price/controllers/main.py

This file was deleted.

145 changes: 116 additions & 29 deletions website_sale_product_minimal_price/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,41 @@
# Copyright 2020 Tecnativa - Pedro M. Baeza
# Copyright 2021 Tecnativa - Carlos Roca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import models
from odoo import fields, models
from odoo.osv import expression


class ProductTemplate(models.Model):
_inherit = "product.template"

def _get_product_subpricelists(self, pricelist_id):
return pricelist_id.item_ids.filtered(
lambda i: (
i.applied_on == "3_global"
or (
i.applied_on == "2_product_category" and i.categ_id == self.categ_id
)
or (i.applied_on == "1_product" and i.product_tmpl_id == self)
or (
i.applied_on == "0_product_variant"
and i.product_id in self.product_variant_ids
)
)
and i.compute_price == "formula"
and i.base == "pricelist"
).mapped("base_pricelist_id")
def _get_product_subpricelists(self, pricelist):
base_domain = pricelist._get_applicable_rules_domain(
self, fields.Datetime.now()
)
domain = expression.AND(
[
base_domain,
[("compute_price", "=", "formula"), ("base", "=", "pricelist")],
]
)
pricelist_data = self.env["product.pricelist.item"]._read_group(
domain,
groupby=["base_pricelist_id"],
aggregates=["base_pricelist_id:array_agg"],
)
pricelist_ids = [item for line in pricelist_data for item in line[1]]
return self.env["product.pricelist"].browse(pricelist_ids)

def _get_variants_from_pricelist(self, pricelist_ids):
return pricelist_ids.mapped("item_ids").filtered(
def _get_variants_from_pricelist(self, pricelist):
return pricelist.mapped("item_ids").filtered(
lambda i: i.product_id in self.product_variant_ids
)

def _get_pricelist_variant_items(self, pricelist_id):
res = self._get_variants_from_pricelist(pricelist_id)
next_pricelists = self._get_product_subpricelists(pricelist_id)
def _get_pricelist_variant_items(self, pricelist):
res = self._get_variants_from_pricelist(pricelist)
next_pricelists = self._get_product_subpricelists(pricelist)
res |= self._get_variants_from_pricelist(next_pricelists)
visited_pricelists = pricelist_id
visited_pricelists = pricelist
while next_pricelists:
pricelist = next_pricelists[0]
if pricelist not in visited_pricelists:
Expand All @@ -51,7 +53,7 @@ def _get_cheapest_info(self, pricelist):
# TODO: Cache this method for getting better performance
self.ensure_one()
min_price = 99999999
product_id = False
product_find = self.env["product.product"]
add_qty = 0
has_distinct_price = False
# Variants with extra price
Expand Down Expand Up @@ -81,8 +83,8 @@ def _get_cheapest_info(self, pricelist):
if product_price < min_price:
min_price = product_price
add_qty = qty
product_id = product.id
return product_id, add_qty, has_distinct_price
product_find = product
return product_find, add_qty, has_distinct_price

def _get_first_possible_combination(
self, parent_combination=None, necessary_values=None
Expand All @@ -100,15 +102,100 @@ def _get_first_possible_combination(
# It only makes sense to change the default one when there are
# more than one variants and we know the pricelist
pricelist = self.env["product.pricelist"].browse(context["pricelist"])
product_id = self._get_cheapest_info(pricelist)[0]
product = self.env["product.product"].browse(product_id)
product = self._get_cheapest_info(pricelist)[0]

Check warning on line 105 in website_sale_product_minimal_price/models/product_template.py

View check run for this annotation

Codecov / codecov/patch

website_sale_product_minimal_price/models/product_template.py#L104-L105

Added lines #L104 - L105 were not covered by tests
# Rebuild the combination in the expected order
res = self.env["product.template.attribute.value"]

Check warning on line 107 in website_sale_product_minimal_price/models/product_template.py

View check run for this annotation

Codecov / codecov/patch

website_sale_product_minimal_price/models/product_template.py#L107

Added line #L107 was not covered by tests
for line in product.valid_product_template_attribute_line_ids:
value = product.product_template_attribute_value_ids.filtered(

Check warning on line 109 in website_sale_product_minimal_price/models/product_template.py

View check run for this annotation

Codecov / codecov/patch

website_sale_product_minimal_price/models/product_template.py#L109

Added line #L109 was not covered by tests
lambda x: x in line.product_template_value_ids
lambda x, line=line: x in line.product_template_value_ids
)
if not value:
value = line.product_template_value_ids[:1]
res += value

Check warning on line 114 in website_sale_product_minimal_price/models/product_template.py

View check run for this annotation

Codecov / codecov/patch

website_sale_product_minimal_price/models/product_template.py#L113-L114

Added lines #L113 - L114 were not covered by tests
return res

def _get_combination_info(
self,
combination=False,
product_id=False,
add_qty=1,
parent_combination=False,
only_template=False,
):
combination_info = super()._get_combination_info(
combination=combination,
product_id=product_id,
add_qty=add_qty,
parent_combination=parent_combination,
only_template=only_template,
)
if only_template and not product_id:
return combination_info
combination = combination or self.env["product.template.attribute.value"]
if only_template:
product = self.env["product.product"]

Check warning on line 136 in website_sale_product_minimal_price/models/product_template.py

View check run for this annotation

Codecov / codecov/patch

website_sale_product_minimal_price/models/product_template.py#L136

Added line #L136 was not covered by tests
elif product_id:
product = self.env["product.product"].browse(product_id)
if combination - product.product_template_attribute_value_ids:
# If the combination is not fully represented in the given product
# make sure to fetch the right product for the given combination
product = self._get_variant_for_combination(combination)
else:
product = self._get_variant_for_combination(combination)
# Getting all min_quantity of the current product to compute the possible
# price scale.
qty_list = self.env["product.pricelist.item"].search(
[
"|",
("product_id", "=", product.id),
"|",
("product_tmpl_id", "=", product.product_tmpl_id.id),
(
"categ_id",
"in",
list(map(int, product.categ_id.parent_path.split("/")[0:-1])),
),
("min_quantity", ">", 0),
]
)
qty_list = sorted(set(qty_list.mapped("min_quantity")))
price_scale = []
last_price = product.with_context(quantity=0)._get_contextual_price()
for min_qty in qty_list:
new_price = product.with_context(quantity=min_qty)._get_contextual_price()

Check warning on line 165 in website_sale_product_minimal_price/models/product_template.py

View check run for this annotation

Codecov / codecov/patch

website_sale_product_minimal_price/models/product_template.py#L165

Added line #L165 was not covered by tests
if new_price != last_price:
price_scale.append(

Check warning on line 167 in website_sale_product_minimal_price/models/product_template.py

View check run for this annotation

Codecov / codecov/patch

website_sale_product_minimal_price/models/product_template.py#L167

Added line #L167 was not covered by tests
{
"min_qty": min_qty,
"price": new_price,
"currency_id": product.currency_id.id,
}
)
last_price = new_price

Check warning on line 174 in website_sale_product_minimal_price/models/product_template.py

View check run for this annotation

Codecov / codecov/patch

website_sale_product_minimal_price/models/product_template.py#L174

Added line #L174 was not covered by tests
combination_info.update(
uom_name=product.uom_id.name,
minimal_price_scale=price_scale,
)
return combination_info

def _get_sales_prices(self, pricelist, fiscal_position):
res = super()._get_sales_prices(pricelist, fiscal_position)
website = (
self.env["website"].get_current_website().with_context(**self.env.context)
)
for template in self.filtered("is_published"):
price_info = res[template.id]
product, add_qty, has_distinct_price = template._get_cheapest_info(
pricelist
)
product_price_info = template._get_additionnal_combination_info(
product,
quantity=add_qty,
date=fields.Date.context_today(self),
website=website,
)
price_info.update(
distinct_prices=has_distinct_price,
price=product_price_info["list_price"],
)
return res
9 changes: 5 additions & 4 deletions website_sale_product_minimal_price/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
- [Tecnativa](https://www.tecnativa.com):

> - Sergio Teruel
> - Carlos Roca
> - Pedro M. Baeza
> - Pilar Vargas
- Sergio Teruel
- Carlos Roca
- Pedro M. Baeza
- Pilar Vargas
- Carlos Lopez
Original file line number Diff line number Diff line change
Expand Up @@ -425,16 +425,14 @@ <h2><a class="toc-backref" href="#toc-entry-5">Authors</a></h2>
</div>
<div class="section" id="contributors">
<h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<ul>
<li><p class="first"><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:</p>
<blockquote>
<ul class="simple">
<li><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:<ul>
<li>Sergio Teruel</li>
<li>Carlos Roca</li>
<li>Pedro M. Baeza</li>
<li>Pilar Vargas</li>
<li>Carlos Lopez</li>
</ul>
</blockquote>
</li>
</ul>
</div>
Expand Down
Loading

0 comments on commit 46e68a6

Please sign in to comment.