From fb257353fac4119dca3f84dc21d9fabce7bac5fb Mon Sep 17 00:00:00 2001 From: Nicolas Martinelli Date: Wed, 3 May 2017 12:30:21 +0200 Subject: [PATCH] [FIX] account, purchase: performance when adding PO - Create 5 PO with 40 lines each for Partner A - Create an invoice - Add each PO thanks to the field "Add Purchase Order" The process takes more and more time for each PO. Most of the time is spent in the recomputation of the secondary fields (in the `onchange` method of "models.py"). The root cause is the cache invalidation due to the relational fields recomputed as superuser by default. opw-740485 --- addons/account/models/account_invoice.py | 8 ++++---- addons/purchase/invoice.py | 7 ++----- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/addons/account/models/account_invoice.py b/addons/account/models/account_invoice.py index 4fe54d0b77b..d492485c681 100644 --- a/addons/account/models/account_invoice.py +++ b/addons/account/models/account_invoice.py @@ -1154,11 +1154,11 @@ class AccountInvoiceLine(models.Model): account_analytic_id = fields.Many2one('account.analytic.account', string='Analytic Account') company_id = fields.Many2one('res.company', string='Company', - related='invoice_id.company_id', store=True, readonly=True) + related='invoice_id.company_id', store=True, readonly=True, related_sudo=False) partner_id = fields.Many2one('res.partner', string='Partner', - related='invoice_id.partner_id', store=True, readonly=True) - currency_id = fields.Many2one('res.currency', related='invoice_id.currency_id', store=True) - company_currency_id = fields.Many2one('res.currency', related='invoice_id.company_currency_id', readonly=True) + related='invoice_id.partner_id', store=True, readonly=True, related_sudo=False) + currency_id = fields.Many2one('res.currency', related='invoice_id.currency_id', store=True, related_sudo=False) + company_currency_id = fields.Many2one('res.currency', related='invoice_id.company_currency_id', readonly=True, related_sudo=False) @api.model def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False): diff --git a/addons/purchase/invoice.py b/addons/purchase/invoice.py index 40705702e8b..ad3b1e0cec7 100644 --- a/addons/purchase/invoice.py +++ b/addons/purchase/invoice.py @@ -67,10 +67,7 @@ class AccountInvoice(models.Model): self.partner_id = self.purchase_id.partner_id.id new_lines = self.env['account.invoice.line'] - for line in self.purchase_id.order_line: - # Load a PO line only once - if line in self.invoice_line_ids.mapped('purchase_line_id'): - continue + for line in self.purchase_id.order_line - self.invoice_line_ids.mapped('purchase_line_id'): data = self._prepare_invoice_line_from_po_line(line) new_line = new_lines.new(data) new_line._set_additional_fields(self) @@ -198,5 +195,5 @@ class AccountInvoiceLine(models.Model): _inherit = 'account.invoice.line' purchase_line_id = fields.Many2one('purchase.order.line', 'Purchase Order Line', ondelete='set null', index=True, readonly=True) - purchase_id = fields.Many2one('purchase.order', related='purchase_line_id.order_id', string='Purchase Order', store=False, readonly=True, + purchase_id = fields.Many2one('purchase.order', related='purchase_line_id.order_id', string='Purchase Order', store=False, readonly=True, related_sudo=False, help='Associated Purchase Order. Filled in automatically when a PO is chosen on the vendor bill.')