diff --git a/addons/product_margin/product_margin.py b/addons/product_margin/product_margin.py
index c7f3219b1be..10025ef3620 100644
--- a/addons/product_margin/product_margin.py
+++ b/addons/product_margin/product_margin.py
@@ -27,6 +27,39 @@ from openerp.osv import fields, osv
class product_product(osv.osv):
_inherit = "product.product"
+ def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False, lazy=True):
+ """
+ Inherit read_group to calculate the sum of the non-stored fields, as it is not automatically done anymore through the XML.
+ """
+ res = super(product_product, self).read_group(cr, uid, domain, fields, groupby, offset=offset, limit=limit, context=context, orderby=orderby, lazy=lazy)
+ if context is None:
+ context = {}
+ fields_list = ['turnover', 'sale_avg_price', 'sale_purchase_price', 'sale_num_invoiced', 'purchase_num_invoiced',
+ 'sales_gap', 'purchase_gap', 'total_cost', 'sale_expected', 'normal_cost', 'total_margin',
+ 'expected_margin', 'total_margin_rate', 'expected_margin_rate']
+ if any(x in fields for x in fields_list):
+ # Calculate first for every product in which line it needs to be applied
+ re_ind = 0
+ prod_re = {}
+ tot_products = []
+ for re in res:
+ if re.get('__domain'):
+ products = self.search(cr, uid, re['__domain'], context=context)
+ tot_products += products
+ for prod in products:
+ prod_re[prod] = re_ind
+ re_ind += 1
+
+ res_val = self._product_margin(cr, uid, tot_products, [x for x in fields if fields in fields_list], '', context=context)
+ for key in res_val.keys():
+ for l in res_val[key].keys():
+ re = res[prod_re[key]]
+ if re.get(l):
+ re[l] += res_val[key][l]
+ else:
+ re[l] = res_val[key][l]
+ return res
+
def _product_margin(self, cr, uid, ids, field_names, arg, context=None):
res = {}
if context is None:
@@ -65,7 +98,7 @@ class product_product(osv.osv):
from account_invoice_line l
left join account_invoice i on (l.invoice_id = i.id)
left join product_product product on (product.id=l.product_id)
- left join product_template pt on (pt.id = l.product_id)
+ left join product_template pt on (pt.id = product.product_tmpl_id)
left join product_uom pu on (pt.uom_id = pu.id)
left join product_uom pu2 on (l.uos_id = pu2.id)
where l.product_id = %s and i.state in %s and i.type IN %s and (i.date_invoice IS NULL or (i.date_invoice>=%s and i.date_invoice<=%s and i.company_id=%s))
diff --git a/addons/product_margin/product_margin_view.xml b/addons/product_margin/product_margin_view.xml
index 2255b109d13..e79ef3bc093 100644
--- a/addons/product_margin/product_margin_view.xml
+++ b/addons/product_margin/product_margin_view.xml
@@ -7,7 +7,7 @@
-
+