From 4b3a1d78c2f0c22fc6ce8605af7d61aad3b064d2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gast=C3=B3n=20Bertolani?= <gaston.bertolani@gmail.com>
Date: Fri, 25 Aug 2023 11:05:19 -0300
Subject: [PATCH] [FIX]Pricelist items in product

---
 models/product.py           | 17 ++++++++----
 models/product_pricelist.py | 53 +++++++++++++++----------------------
 views/product_views.xml     |  4 +--
 3 files changed, 35 insertions(+), 39 deletions(-)

diff --git a/models/product.py b/models/product.py
index 7469cfb..8511c7e 100644
--- a/models/product.py
+++ b/models/product.py
@@ -6,7 +6,7 @@
 
 import logging
 
-from odoo import fields, models
+from odoo import api, fields, models
 
 _logger = logging.getLogger(__name__)
 
@@ -14,8 +14,15 @@ _logger = logging.getLogger(__name__)
 class ProductProduct(models.Model):
     _inherit = 'product.product'
 
-    price_by_rate_ids = fields.One2many(
-        comodel_name='product.pricelist.item.view',
-        inverse_name='base_product_id',
-        string='Price By Rate',
+    pricelist_item_ids = fields.Many2many(
+        comodel_name='product.pricelist.item',
+        relation='product_pricelist_item_rel',
+        column1='product_id',
+        column2='item_id',
+        string='Items',
+        domain=lambda self: self._get_domain_pricelist_items(),
     )
+
+    @api.model
+    def _get_domain_pricelist_items(self):
+        return [('pricelist_id.is_product_charge', '=', True)]
diff --git a/models/product_pricelist.py b/models/product_pricelist.py
index 9ded79b..8505f05 100644
--- a/models/product_pricelist.py
+++ b/models/product_pricelist.py
@@ -7,7 +7,7 @@
 
 import logging
 
-from odoo import fields, models, tools
+from odoo import api, fields, models
 
 _logger = logging.getLogger(__name__)
 
@@ -20,39 +20,28 @@ class ProductPricelist(models.Model):
     )
 
 
-class ProductPricelistItemView(models.Model):
-    _name = 'product.pricelist.item.view'
+class ProductPricelistItem(models.Model):
     _inherit = 'product.pricelist.item'
-    _description = 'Product Price Rate'
-    _auto = False
 
-    base_product_id = fields.Many2one(
+    product_ids = fields.Many2many(
         comodel_name='product.product',
-        string='Product',
+        relation='product_pricelist_item_rel',
+        column1='item_id',
+        column2='product_id',
+        string='Products',
+        compute='_compute_related_products',
+        store=True,
     )
 
-    def init(self):
-        tools.drop_view_if_exists(self._cr, self._table)
-        self._cr.execute("""
-            CREATE OR REPLACE VIEW %s AS (
-                SELECT pi.*,
-                CASE
-                    WHEN pi.applied_on = '1_product' THEN tmpl_pp.id
-                    WHEN pi.applied_on = '0_product_variant' THEN pp.id
-                ELSE NULL
-                END AS base_product_id
-                FROM product_pricelist_item AS pi
-                LEFT JOIN product_product AS pp
-                    ON pi.product_id = pp.id AND
-                        pi.applied_on = '0_product_variant'
-                LEFT JOIN product_template AS tmpl
-                    ON pi.product_tmpl_id = tmpl.id AND
-                        pi.applied_on = '1_product'
-                LEFT JOIN product_product AS tmpl_pp
-                    ON tmpl_pp.product_tmpl_id = tmpl.id
-                LEFT JOIN product_pricelist AS pps
-                    ON pi.pricelist_id = pps.id
-                WHERE pi.applied_on in ('1_product', '0_product_variant')
-                    AND pps.is_product_charge = true
-            );
-        """ % self._table)
+    @api.depends('product_id', 'product_tmpl_id', 'applied_on')
+    def _compute_related_products(self):
+        for reg in self:
+            reg.product_ids = [(5, 0, 0)]
+            if reg.applied_on not in ('0_product_variant', '1_product'):
+                continue
+            if reg.applied_on == '0_product_variant':
+                reg.product_ids = [(6, 0, reg.product_id.ids)]
+            else:
+                reg.product_ids = [
+                    (6, 0, reg.product_tmpl_id.product_variant_ids.ids),
+                ]
diff --git a/views/product_views.xml b/views/product_views.xml
index 60b5c8c..0b47370 100644
--- a/views/product_views.xml
+++ b/views/product_views.xml
@@ -8,8 +8,8 @@
             <field name="inherit_id" ref="product.product_normal_form_view"/>
             <field name="arch" type="xml">
                 <xpath expr="//page[@name='sales']/group[@name='sale']" position="before">
-                    <group name="price_by_rate" string="Price By Rate">
-                        <field name="price_by_rate_ids" readonly="1" nolabel="1"
+                    <group name="pricelist_item_ids" string="Price By Rate">
+                        <field name="pricelist_item_ids" readonly="1" nolabel="1"
                             options="{'no_open': True}"
                             context="{'tree_view_ref': 'product.product_pricelist_item_tree_view_from_product'}"/>
                     </group>
-- 
GitLab