From 3f4d1e1fbe2da53112d2791c71c6cfb4033c29a4 Mon Sep 17 00:00:00 2001
From: Nicolas Seinlet <nse@openerp.com>
Date: Wed, 16 Jan 2019 14:08:18 +0100
Subject: [PATCH] [IMP] mrp,stock_account: faster computation

Use read_group is faster than the python equivalent,
and then don't push ids in ORM cache, which makes
the ORM faster later in the process.
Also unlink in batch.
---
 addons/mrp/models/stock_move.py      | 22 ++++++++++++++++++----
 addons/stock_account/models/stock.py |  2 +-
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/addons/mrp/models/stock_move.py b/addons/mrp/models/stock_move.py
index 868bd08d1219..2e0b4ce374c7 100644
--- a/addons/mrp/models/stock_move.py
+++ b/addons/mrp/models/stock_move.py
@@ -116,9 +116,24 @@ class StockMove(models.Model):
     @api.multi
     @api.depends('move_lot_ids', 'move_lot_ids.quantity_done', 'quantity_done_store')
     def _qty_done_compute(self):
+        move_lot_ids = self.env['stock.move.lots'].sudo().read_group(
+            [('move_id', 'in', self.ids), ('lot_id', '!=', False)],
+            ['move_id'],
+            ['move_id']
+        )
+        move_lot_count = {x['move_id'][0]: x['move_id_count'] for x in move_lot_ids}
+        move_qty_ids = self.env['stock.move.lots'].sudo().read_group(
+            [
+                ('move_id', 'in', self.ids),
+                ('done_wo', '!=', False)
+            ],
+            ['move_id', 'quantity_done'],
+            ['move_id']
+        )
+        move_qty = {x['move_id'][0]: x['quantity_done'] for x in move_qty_ids}
         for move in self:
-            if move.has_tracking != 'none' or move.sudo().move_lot_ids.mapped('lot_id'):
-                move.quantity_done = sum(move.move_lot_ids.filtered(lambda x: x.done_wo).mapped('quantity_done')) #TODO: change with active_move_lot_ids?
+            if move.has_tracking != 'none' or move_lot_count.get(move.id, 0) > 0:
+                move.quantity_done = move_qty.get(move.id, 0)
             else:
                 move.quantity_done = move.quantity_done_store
 
@@ -162,9 +177,8 @@ class StockMove(models.Model):
     @api.multi
     def create_lots(self):
         lots = self.env['stock.move.lots']
+        lots.search([('move_id', 'in', self.ids), ('done_wo', '=', True), ('quantity_done', '=', 0)]).sudo().unlink()
         for move in self:
-            unlink_move_lots = move.move_lot_ids.filtered(lambda x : (x.quantity_done == 0) and x.done_wo)
-            unlink_move_lots.sudo().unlink()
             group_new_quant = {}
             old_move_lot = {}
             for movelot in move.move_lot_ids:
diff --git a/addons/stock_account/models/stock.py b/addons/stock_account/models/stock.py
index 0ba1f76102a6..d2a853f2253f 100644
--- a/addons/stock_account/models/stock.py
+++ b/addons/stock_account/models/stock.py
@@ -150,7 +150,7 @@ class StockQuant(models.Model):
     def _quant_create_from_move(self, qty, move, lot_id=False, owner_id=False, src_package_id=False, dest_package_id=False, force_location_from=False, force_location_to=False):
         quant = super(StockQuant, self)._quant_create_from_move(qty, move, lot_id=lot_id, owner_id=owner_id, src_package_id=src_package_id, dest_package_id=dest_package_id, force_location_from=force_location_from, force_location_to=force_location_to)
         quant._account_entry_move(move)
-        if move.product_id.valuation == 'real_time':
+        if move.product_id.product_tmpl_id.valuation == 'real_time':
             # If the precision required for the variable quant cost is larger than the accounting
             # precision, inconsistencies between the stock valuation and the accounting entries
             # may arise.
-- 
GitLab