[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
This commit is contained in:
Nicolas Martinelli
2018-04-10 11:02:36 +02:00
committed by Nicolas Martinelli
parent eca99f1f12
commit 77bf2c96af
@@ -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