From 8a9c75790aeb9e157929c40b22624a604cf33e18 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Tue, 17 Sep 2019 18:53:47 +0200 Subject: [PATCH] sale_usability: Add sale_ids and sale_count on account.invoice Show sale_line_ids on invoice line form view --- sale_usability/__manifest__.py | 1 + sale_usability/account_invoice.py | 35 +++++++++++++++++++++- sale_usability/account_invoice_view.xml | 39 +++++++++++++++++++++++++ sale_usability/sale.py | 1 - 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 sale_usability/account_invoice_view.xml diff --git a/sale_usability/__manifest__.py b/sale_usability/__manifest__.py index 2804772..c6a1905 100644 --- a/sale_usability/__manifest__.py +++ b/sale_usability/__manifest__.py @@ -30,6 +30,7 @@ This module has been written by Alexis de Lattre from Akretion 'sale_view.xml', 'sale_report_view.xml', 'product_view.xml', + 'account_invoice_view.xml', ], 'installable': True, } diff --git a/sale_usability/account_invoice.py b/sale_usability/account_invoice.py index 0bb2254..a419fe5 100644 --- a/sale_usability/account_invoice.py +++ b/sale_usability/account_invoice.py @@ -2,13 +2,46 @@ # @author Alexis de Lattre # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import models +from odoo import api, fields, models from collections import OrderedDict class AccountInvoice(models.Model): _inherit = 'account.invoice' + # sale_ids is kind of the symetric field of invoice_ids on sale.order + sale_ids = fields.Many2many( + 'sale.order', string='Sale Orders', compute="_compute_sale_ids", + readonly=True, copy=False) + sale_count = fields.Integer( + string='Sale Order Count', compute='_compute_sale_ids', readonly=True) + + @api.depends('invoice_line_ids.sale_line_ids') + def _compute_sale_ids(self): + for invoice in self: + if invoice.type == 'out_invoice': + sales = invoice.invoice_line_ids.mapped('sale_line_ids').\ + mapped('order_id') + invoice.sale_ids = sales.ids + invoice.sale_count = len(sales.ids) + else: + invoice.sale_ids = [] + invoice.sale_count = 0 + + def show_sale_orders(self): + self.ensure_one() + action = self.env.ref('sale.action_orders').read()[0] + sales = self.sale_ids + if len(sales) > 1: + action['domain'] = [('id', 'in', sales.ids)] + else: + action.update({ + 'res_id': sales.id, + 'view_mode': 'form,tree,kanban,calendar,pivot,graph,activity', + 'views': False, + }) + return action + def py3o_lines_layout_groupby_order(self, subtotal=True): # This method is an alternative to the method py3o_lines_layout() # defined above: you just have to change the call in the invoice diff --git a/sale_usability/account_invoice_view.xml b/sale_usability/account_invoice_view.xml new file mode 100644 index 0000000..843d6e5 --- /dev/null +++ b/sale_usability/account_invoice_view.xml @@ -0,0 +1,39 @@ + + + + + + + + sale_usability.customer.invoice.form + account.invoice + + +
+ +
+
+
+ + + sale_usability.invoice.line.form + account.invoice.line + + + + + + + + +
diff --git a/sale_usability/sale.py b/sale_usability/sale.py index 5d22715..f94f8f2 100644 --- a/sale_usability/sale.py +++ b/sale_usability/sale.py @@ -4,7 +4,6 @@ from odoo import models, fields, api from odoo.tools import float_is_zero -from collections import OrderedDict class SaleOrder(models.Model):