[REF] stock_landed_costs: domain on transfers

The previous domain was too naive: a receipt is not always valued
(inter-warehouse transfer) and even a receipt that should be valued
isn't always (receive goods you do not own, change your config from
manual to automated and then apply the lc on an old receipt, etc.)

Enforce a domain making sure the transfer is valued.

related opw-2165697
task-2169844

closes odoo/odoo#43410

X-original-commit: c6aa61b22a18c3f110baa7ed3c97e275b21e5329
Signed-off-by: Simon Lejeune (sle) <sle@openerp.com>
This commit is contained in:
Simon Lejeune
2020-01-16 12:07:42 +00:00
parent 218e1cad61
commit 50cbaef2dd
2 changed files with 15 additions and 1 deletions
@@ -45,6 +45,7 @@ class LandedCost(models.Model):
picking_ids = fields.Many2many(
'stock.picking', string='Transfers',
copy=False, states={'done': [('readonly', True)]})
allowed_picking_ids = fields.Many2many('stock.picking', compute='_compute_allowed_picking_ids')
cost_lines = fields.One2many(
'stock.landed.cost.lines', 'cost_id', 'Cost Lines',
copy=True, states={'done': [('readonly', True)]})
@@ -79,6 +80,18 @@ class LandedCost(models.Model):
for cost in self:
cost.amount_total = sum(line.price_unit for line in cost.cost_lines)
@api.depends('company_id')
def _compute_allowed_picking_ids(self):
self.env.cr.execute("""SELECT sm.picking_id, sm.company_id
FROM stock_move AS sm
INNER JOIN stock_valuation_layer AS svl ON svl.stock_move_id = sm.id
WHERE sm.picking_id IS NOT NULL""")
valued_picking_ids_per_company = defaultdict(list)
for res in self.env.cr.fetchall():
valued_picking_ids_per_company[res[1]].append(res[0])
for cost in self:
cost.allowed_picking_ids = valued_picking_ids_per_company[cost.company_id.id]
@api.model
def create(self, vals):
if vals.get('name', _('New')) == _('New'):
@@ -29,7 +29,8 @@
<group>
<group>
<field name="date"/>
<field name="picking_ids" widget="many2many_tags" options="{'no_create_edit': True}" domain="[('state', '=', 'done'), ('picking_type_code', '!=', 'internal')]"/>
<field name="allowed_picking_ids" invisible="1"/>
<field name="picking_ids" widget="many2many_tags" options="{'no_create_edit': True}" domain="[('id', 'in', allowed_picking_ids)]"/>
</group>
<group>
<label for="account_journal_id" string="Journal"/>