From f207ef5fae779f641b3e262d98d8935931dc18e0 Mon Sep 17 00:00:00 2001
From: Nicolas Seinlet <nse@odoo.com>
Date: Thu, 12 May 2016 11:41:13 +0200
Subject: [PATCH] [IMP] account: improve reconciliation speed

 - Only invalidate cache for fields and records we modify
 - Rewrite query to be more efficient
 - Avoid o2m commands to be more efficient; write directly on reverse m2o
---
 addons/account/account.py           | 2 +-
 addons/account/account_invoice.py   | 4 ++--
 addons/account/account_move_line.py | 8 +++-----
 3 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/addons/account/account.py b/addons/account/account.py
index da19fcf7197e..0c32aea78f50 100644
--- a/addons/account/account.py
+++ b/addons/account/account.py
@@ -1339,7 +1339,7 @@ class account_move(osv.osv):
                    'SET state=%s '\
                    'WHERE id IN %s',
                    ('posted', tuple(valid_moves),))
-        self.invalidate_cache(cr, uid, context=context)
+        self.invalidate_cache(cr, uid, ['state', ], valid_moves, context=context)
         return True
 
     def button_validate(self, cursor, user, ids, context=None):
diff --git a/addons/account/account_invoice.py b/addons/account/account_invoice.py
index 7a0d737d847b..17ccfb3bd866 100644
--- a/addons/account/account_invoice.py
+++ b/addons/account/account_invoice.py
@@ -629,9 +629,9 @@ class account_invoice(models.Model):
         line_ids = self.move_line_id_payment_get()
         if not line_ids:
             return False
-        query = "SELECT reconcile_id FROM account_move_line WHERE id IN %s"
+        query = "SELECT count(*) FROM account_move_line WHERE reconcile_id IS NULL AND id IN %s"
         self._cr.execute(query, (tuple(line_ids),))
-        return all(row[0] for row in self._cr.fetchall())
+        return self._cr.fetchone()[0] == 0
 
     @api.multi
     def button_reset_taxes(self):
diff --git a/addons/account/account_move_line.py b/addons/account/account_move_line.py
index 2ace8acc7ee9..e7509edecdc4 100644
--- a/addons/account/account_move_line.py
+++ b/addons/account/account_move_line.py
@@ -1074,11 +1074,9 @@ class account_move_line(osv.osv):
         # marking the lines as reconciled does not change their validity, so there is no need
         # to revalidate their moves completely.
         reconcile_context = dict(context, novalidate=True)
-        r_id = move_rec_obj.create(cr, uid, {
-            'type': type,
-            'line_id': map(lambda x: (4, x, False), ids),
-            'line_partial_ids': map(lambda x: (3, x, False), ids)
-        }, context=reconcile_context)
+        r_id = move_rec_obj.create(cr, uid, {'type': type}, context=reconcile_context)
+        self.write(cr, uid, ids, {'reconcile_id': r_id, 'reconcile_partial_id': False}, context=reconcile_context)
+
         # the id of the move.reconcile is written in the move.line (self) by the create method above
         # because of the way the line_id are defined: (4, x, False)
         for id in ids:
-- 
GitLab