summaryrefslogtreecommitdiff
path: root/addons/stock_landed_costs/models
diff options
context:
space:
mode:
authorSimon Lejeune <sle@openerp.com>2019-05-22 09:40:17 +0000
committerSimon Lejeune <sle@openerp.com>2019-06-10 20:43:00 +0000
commit68d8e49f2fd03bdd42c9b50fde64f706af9d3c8b (patch)
tree27a226f9205eaf09ab78db821c3688d0fef162bd /addons/stock_landed_costs/models
parent1e5624ba680fa98dc17d3edd888b70405334ddca (diff)
[REF] stock_landed_costs: adapt to valuation layers
We need to increment the value of another layer. We don't want to write on this layer in place as we used to do for multiple reasons: - valuation at date: before the application of an lc, the value should not change - if the layer was consumed, we would need to adapt the consumers and if one day we would have needed to understand what happened in the fifo stack it would be hell So we allow to create a valuation layer linked to another valuation layer through a simple M2O O2M. At the time it is only used on the landed costs to add value and no quantity so the code expects it would be the case and it is not enforced. We adapt `_run_fifo` to use the full value. We also add a stat button on the landed cost to access the valuation layers, like what is done for the picking and manufacturing orders. task-1875873
Diffstat (limited to 'addons/stock_landed_costs/models')
-rw-r--r--addons/stock_landed_costs/models/__init__.py2
-rw-r--r--addons/stock_landed_costs/models/stock_landed_cost.py41
-rw-r--r--addons/stock_landed_costs/models/stock_valuation_layer.py13
3 files changed, 40 insertions, 16 deletions
diff --git a/addons/stock_landed_costs/models/__init__.py b/addons/stock_landed_costs/models/__init__.py
index 15a94628e98..8b81c037995 100644
--- a/addons/stock_landed_costs/models/__init__.py
+++ b/addons/stock_landed_costs/models/__init__.py
@@ -3,3 +3,5 @@
from . import product
from . import stock_landed_cost
+from . import stock_valuation_layer
+
diff --git a/addons/stock_landed_costs/models/stock_landed_cost.py b/addons/stock_landed_costs/models/stock_landed_cost.py
index 1d513fdc746..fd524beaee0 100644
--- a/addons/stock_landed_costs/models/stock_landed_cost.py
+++ b/addons/stock_landed_costs/models/stock_landed_cost.py
@@ -9,12 +9,6 @@ from odoo.addons.stock_landed_costs.models import product
from odoo.exceptions import UserError
-class StockMove(models.Model):
- _inherit = 'stock.move'
-
- landed_cost_value = fields.Float('Landed Cost')
-
-
class LandedCost(models.Model):
_name = 'stock.landed.cost'
_description = 'Stock Landed Cost'
@@ -53,6 +47,7 @@ class LandedCost(models.Model):
required=True, states={'done': [('readonly', True)]})
company_id = fields.Many2one('res.company', string="Company",
related='account_journal_id.company_id', readonly=False)
+ stock_valuation_layer_ids = fields.One2many('stock.valuation.layer', 'stock_landed_cost_id')
@api.one
@api.depends('cost_lines.price_unit')
@@ -101,25 +96,33 @@ class LandedCost(models.Model):
'line_ids': [],
}
for line in cost.valuation_adjustment_lines.filtered(lambda line: line.move_id):
+ remaining_qty = sum(line.move_id.stock_valuation_layer_ids.mapped('remaining_qty'))
+ linked_layer = line.move_id.stock_valuation_layer_ids[-1] # Maybe the LC layer should be linked to multiple IN layer?
+
# Prorate the value at what's still in stock
- cost_to_add = (line.move_id.remaining_qty / line.move_id.product_qty) * line.additional_landed_cost
-
- new_landed_cost_value = line.move_id.landed_cost_value + line.additional_landed_cost
- line.move_id.write({
- 'landed_cost_value': new_landed_cost_value,
- 'value': line.move_id.value + line.additional_landed_cost,
- 'remaining_value': line.move_id.remaining_value + cost_to_add,
- 'price_unit': (line.move_id.value + line.additional_landed_cost) / line.move_id.product_qty,
+ cost_to_add = (remaining_qty / line.move_id.product_qty) * line.additional_landed_cost
+ valuation_layer = self.env['stock.valuation.layer'].create({
+ 'value': cost_to_add,
+ 'unit_cost': 0,
+ 'quantity': 0,
+ 'remaining_qty': 0,
+ 'stock_valuation_layer_id': linked_layer.id,
+ 'description': cost.name,
+ 'stock_move_id': line.move_id.id,
+ 'product_id': line.move_id.product_id.id,
+ 'stock_landed_cost_id': self.id,
+ 'company_id': self.company_id.id,
})
# `remaining_qty` is negative if the move is out and delivered proudcts that were not
# in stock.
qty_out = 0
if line.move_id._is_in():
- qty_out = line.move_id.product_qty - line.move_id.remaining_qty
+ qty_out = line.move_id.product_qty - remaining_qty
elif line.move_id._is_out():
qty_out = line.move_id.product_qty
move_vals['line_ids'] += line._create_accounting_entries(move, qty_out)
+ move_vals['stock_valuation_layer_ids'] = [(6, None, [valuation_layer.id])]
move = move.create(move_vals)
cost.write({'state': 'done', 'account_move_id': move.id})
move.post()
@@ -153,7 +156,7 @@ class LandedCost(models.Model):
'product_id': move.product_id.id,
'move_id': move.id,
'quantity': move.product_qty,
- 'former_cost': move.value,
+ 'former_cost': sum(move.stock_valuation_layer_ids.mapped('value')),
'weight': move.product_id.weight * move.product_qty,
'volume': move.product_id.volume * move.product_qty
}
@@ -227,6 +230,12 @@ class LandedCost(models.Model):
AdjustementLines.browse(key).write({'additional_landed_cost': value})
return True
+ def action_view_stock_valuation_layers(self):
+ self.ensure_one()
+ domain = [('id', 'in', self.stock_valuation_layer_ids.ids)]
+ action = self.env.ref('stock_account.stock_valuation_layer_action').read()[0]
+ return dict(action, domain=domain)
+
class LandedCostLine(models.Model):
_name = 'stock.landed.cost.lines'
diff --git a/addons/stock_landed_costs/models/stock_valuation_layer.py b/addons/stock_landed_costs/models/stock_valuation_layer.py
new file mode 100644
index 00000000000..56e2e82bcce
--- /dev/null
+++ b/addons/stock_landed_costs/models/stock_valuation_layer.py
@@ -0,0 +1,13 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import fields, models
+
+
+class StockValuationLayer(models.Model):
+ """Stock Valuation Layer"""
+
+ _inherit = 'stock.valuation.layer'
+
+ stock_landed_cost_id = fields.Many2one('stock.landed.cost', 'Landed Cost')
+