From e3ed65d296e18a0aac43477ff34a5bdb32d68cde Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Tue, 20 Dec 2016 01:05:09 +0100 Subject: [PATCH] Add methods for reports --- account_usability/account.py | 43 ++++++++++++++++++++++++++++++++++++ sale_usability/sale.py | 7 +++--- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/account_usability/account.py b/account_usability/account.py index a5faf07..82d539c 100644 --- a/account_usability/account.py +++ b/account_usability/account.py @@ -6,6 +6,7 @@ from odoo import models, fields, api, _ from odoo.tools import float_compare, float_is_zero from odoo.exceptions import UserError +from itertools import groupby class AccountInvoice(models.Model): @@ -56,6 +57,48 @@ class AccountInvoice(models.Model): # return res + # for report + @api.multi + def py3o_lines_layout(self): + self.ensure_one() + res1 = [] + # [ + # {'categ': categ(6), 'lines': [l1, l2], 'subtotal': 23.32}, + # {'categ': categ(1), 'lines': [l3, l4, l5], 'subtotal': 12.42}, + # ] + for categ, lines in\ + groupby(self.invoice_line_ids, lambda l: l.layout_category_id): + entry = {'lines': [], 'categ': categ} + if categ.subtotal: + entry['subtotal'] = 0.0 + for line in lines: + entry['lines'].append(line) + if 'subtotal' in entry: + entry['subtotal'] += line.price_subtotal + res1.append(entry) + res2 = [] + if len(res1) == 1 and not res1[0]['categ']: + # No category at all + for l in res1[0]['lines']: + res2.append({'line': l}) + else: + # TODO : gérer qd il n'y a pas de categ + for ldict in res1: + res2.append({'categ': ldict['categ']}) + for line in ldict['lines']: + res2.append({'line': line}) + if 'subtotal' in ldict: + res2.append({'subtotal': ldict['subtotal']}) + # res2: + # [ + # {'categ': categ(1)}, + # {'line': invoice_line(2)}, + # {'line': invoice_line(3)}, + # {'subtotal': 8932.23}, + # ] + return res2 + + class AccountInvoiceLine(models.Model): _inherit = 'account.invoice.line' diff --git a/sale_usability/sale.py b/sale_usability/sale.py index e2c5e02..65315aa 100644 --- a/sale_usability/sale.py +++ b/sale_usability/sale.py @@ -43,7 +43,8 @@ class SaleOrder(models.Model): # {'categ': categ(6), 'lines': [l1, l2], 'subtotal': 23.32}, # {'categ': categ(1), 'lines': [l3, l4, l5], 'subtotal': 12.42}, # ] - for categ, lines in groupby(self.order_line, lambda l: l.layout_category_id): + for categ, lines in\ + groupby(self.order_line, lambda l: l.layout_category_id): entry = {'lines': [], 'categ': categ} if categ.subtotal: entry['subtotal'] = 0.0 @@ -61,8 +62,8 @@ class SaleOrder(models.Model): # TODO : gérer qd il n'y a pas de categ for ldict in res1: res2.append({'categ': ldict['categ']}) - for soline in ldict['lines']: - res2.append({'line': soline}) + for line in ldict['lines']: + res2.append({'line': line}) if 'subtotal' in ldict: res2.append({'subtotal': ldict['subtotal']}) # res2: