summaryrefslogtreecommitdiff
path: root/addons/product_matrix/models
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/product_matrix/models
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/product_matrix/models')
-rw-r--r--addons/product_matrix/models/__init__.py1
-rw-r--r--addons/product_matrix/models/product_template.py92
2 files changed, 93 insertions, 0 deletions
diff --git a/addons/product_matrix/models/__init__.py b/addons/product_matrix/models/__init__.py
new file mode 100644
index 00000000..e8fa8f6b
--- /dev/null
+++ b/addons/product_matrix/models/__init__.py
@@ -0,0 +1 @@
+from . import product_template
diff --git a/addons/product_matrix/models/product_template.py b/addons/product_matrix/models/product_template.py
new file mode 100644
index 00000000..6915833a
--- /dev/null
+++ b/addons/product_matrix/models/product_template.py
@@ -0,0 +1,92 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+import itertools
+
+from odoo import models
+
+
+class ProductTemplate(models.Model):
+ _inherit = 'product.template'
+
+ def _get_template_matrix(self, **kwargs):
+ self.ensure_one()
+ company_id = kwargs.get('company_id', None) or self.company_id or self.env.company
+ currency_id = kwargs.get('currency_id', None) or self.currency_id
+ display_extra = kwargs.get('display_extra_price', False)
+ attribute_lines = self.valid_product_template_attribute_line_ids
+
+ Attrib = self.env['product.template.attribute.value']
+ first_line_attributes = attribute_lines[0].product_template_value_ids._only_active()
+ attribute_ids_by_line = [line.product_template_value_ids._only_active().ids for line in attribute_lines]
+
+ header = [{"name": self.display_name}] + [
+ attr._grid_header_cell(
+ fro_currency=self.currency_id,
+ to_currency=currency_id,
+ company=company_id,
+ display_extra=display_extra
+ ) for attr in first_line_attributes]
+
+ result = [[]]
+ for pool in attribute_ids_by_line:
+ result = [x + [y] for y in pool for x in result]
+ args = [iter(result)] * len(first_line_attributes)
+ rows = itertools.zip_longest(*args)
+
+ matrix = []
+ for row in rows:
+ row_attributes = Attrib.browse(row[0][1:])
+ row_header_cell = row_attributes._grid_header_cell(
+ fro_currency=self.currency_id,
+ to_currency=currency_id,
+ company=company_id,
+ display_extra=display_extra)
+ result = [row_header_cell]
+
+ for cell in row:
+ combination = Attrib.browse(cell)
+ is_possible_combination = self._is_combination_possible(combination)
+ cell.sort()
+ result.append({
+ "ptav_ids": cell,
+ "qty": 0,
+ "is_possible_combination": is_possible_combination
+ })
+ matrix.append(result)
+
+ return {
+ "header": header,
+ "matrix": matrix,
+ }
+
+
+class ProductTemplateAttributeValue(models.Model):
+ _inherit = "product.template.attribute.value"
+
+ def _grid_header_cell(self, fro_currency, to_currency, company, display_extra=True):
+ """Generate a header matrix cell for 1 or multiple attributes.
+
+ :param res.currency fro_currency:
+ :param res.currency to_currency:
+ :param res.company company:
+ :param bool display_extra: whether extra prices should be displayed in the cell
+ True by default, used to avoid showing extra prices on purchases.
+ :returns: cell with name (and price if any price_extra is defined on self)
+ :rtype: dict
+ """
+ header_cell = {
+ 'name': ' • '.join([attr.name for attr in self]) if self else " "
+ } # The " " is to avoid having 'Not available' if the template has only one attribute line.
+ extra_price = sum(self.mapped('price_extra')) if display_extra else 0
+ if extra_price:
+ sign = '+ ' if extra_price > 0 else '- '
+ header_cell.update({
+ "price": sign + self.env['ir.qweb.field.monetary'].value_to_html(
+ extra_price, {
+ 'from_currency': fro_currency,
+ 'display_currency': to_currency,
+ 'company_id': company.id,
+ }
+ )
+ })
+ return header_cell