From 77bf2c96af30ae17ff43e8a08d2ae81b3960b3fa Mon Sep 17 00:00:00 2001 From: Nicolas Martinelli Date: Thu, 5 Apr 2018 15:24:36 +0200 Subject: [PATCH] [FIX] stock_landed_costs: defer AML creation In order to improve the speed of landed cost validation, we defer the creation of AML at the very end of the process. This way, we don't suffer from various performance issues caused by: - cache invalidation (use of `with_context`) - recomputations opw-1824661 --- .../models/stock_landed_cost.py | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/addons/stock_landed_costs/models/stock_landed_cost.py b/addons/stock_landed_costs/models/stock_landed_cost.py index a1223d896c8..5e5c88031c7 100644 --- a/addons/stock_landed_costs/models/stock_landed_cost.py +++ b/addons/stock_landed_costs/models/stock_landed_cost.py @@ -85,11 +85,13 @@ class LandedCost(models.Model): raise UserError(_('Cost and adjustments lines do not match. You should maybe recompute the landed costs.')) for cost in self: - move = self.env['account.move'].create({ + move = self.env['account.move'] + move_vals = { 'journal_id': cost.account_journal_id.id, 'date': cost.date, - 'ref': cost.name - }) + 'ref': cost.name, + 'line_ids': [], + } for line in cost.valuation_adjustment_lines.filtered(lambda line: line.move_id): per_unit = line.final_cost / line.quantity diff = per_unit - line.former_cost_per_unit @@ -134,8 +136,9 @@ class LandedCost(models.Model): for quant in line.move_id.quant_ids: if quant.location_id.usage != 'internal': qty_out += quant.qty - line._create_accounting_entries(move, qty_out) - move.assert_balanced() + move_vals['line_ids'] += line._create_accounting_entries(move, qty_out) + + move = move.create(move_vals) cost.write({'state': 'done', 'account_move_id': move.id}) move.post() return True @@ -336,11 +339,10 @@ class AdjustmentLines(models.Model): Generate the account.move.line values to track the landed cost. Afterwards, for the goods that are already out of stock, we should create the out moves """ - AccountMoveLine = self.env['account.move.line'].with_context(check_move_validity=False, recompute=False) + AccountMoveLine = [] base_line = { 'name': self.name, - 'move_id': move.id, 'product_id': self.product_id.id, 'quantity': self.quantity, } @@ -354,8 +356,8 @@ class AdjustmentLines(models.Model): # negative cost, reverse the entry debit_line['credit'] = -diff credit_line['debit'] = -diff - AccountMoveLine.create(debit_line) - AccountMoveLine.create(credit_line) + AccountMoveLine.append([0, 0, debit_line]) + AccountMoveLine.append([0, 0, credit_line]) # Create account move lines for quants already out of stock if qty_out > 0: @@ -375,8 +377,8 @@ class AdjustmentLines(models.Model): # negative cost, reverse the entry debit_line['credit'] = -diff credit_line['debit'] = -diff - AccountMoveLine.create(debit_line) - AccountMoveLine.create(credit_line) + AccountMoveLine.append([0, 0, debit_line]) + AccountMoveLine.append([0, 0, credit_line]) # TDE FIXME: oh dear if self.env.user.company_id.anglo_saxon_accounting: @@ -396,7 +398,7 @@ class AdjustmentLines(models.Model): # negative cost, reverse the entry debit_line['credit'] = -diff credit_line['debit'] = -diff - AccountMoveLine.create(debit_line) - AccountMoveLine.create(credit_line) + AccountMoveLine.append([0, 0, debit_line]) + AccountMoveLine.append([0, 0, credit_line]) - return True + return AccountMoveLine