[FIX] product_margin: product template should be the correct one and make group bys work

The fact that it uses the correct template id now, fixes #6860

The read_group calculates the sum for every group for the non-stored fields  as it is not possible
anymore to add it in the xml, fixes #6638

The read_group simply searches for all products in the group and sums the totals of the fields for these
products.
This commit is contained in:
Josse Colpaert 2015-07-24 16:32:44 +02:00
parent 5e6ec89659
commit 2fddf1729b
2 changed files with 35 additions and 2 deletions

View File

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

View File

@ -7,7 +7,7 @@
<field name="priority" eval="50"/>
<field name="arch" type="xml">
<graph string="Product Margins" type="bar">
<field name="name"/>
<field name="product_tmpl_id"/>
<field name="turnover" operator="+"/>
<field name="total_cost" operator="+"/>
</graph>