Skip to content

Commit

Permalink
[FIX] stock_landed_costs: create LC in multi-comp setup
Browse files Browse the repository at this point in the history
To reproduce the issue:
1. Create two companies C1, C2
2. Select C1 and enable C2
3. Confirm a PO with a FIFO product in C2
4. Process the receipt
5. Bill the PO with a landed cost
6. Generate and post the LC

Few errors will happen: on the LC, the journal used belongs to C1,
not C2. Another one: in C2, on the product's form view, the standard
price does not include the LC

OPW-3651022

closes odoo#150200

X-original-commit: 0b62337
Signed-off-by: William Henrotin (whe) <[email protected]>
Signed-off-by: Adrien Widart (awt) <[email protected]>
  • Loading branch information
adwid committed Jan 20, 2024
1 parent fa9815a commit 3689958
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion addons/stock_landed_costs/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def button_create_landed_costs(self):
self.ensure_one()
landed_costs_lines = self.line_ids.filtered(lambda line: line.is_landed_costs_line)

landed_costs = self.env['stock.landed.cost'].create({
landed_costs = self.env['stock.landed.cost'].with_company(self.company_id).create({
'vendor_bill_id': self.id,
'cost_lines': [(0, 0, {
'product_id': l.product_id.id,
Expand Down
5 changes: 3 additions & 2 deletions addons/stock_landed_costs/models/stock_landed_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ def button_validate(self):
move_vals['line_ids'] += line._create_accounting_entries(move, qty_out)

# batch standard price computation avoid recompute quantity_svl at each iteration
products = self.env['product.product'].browse(p.id for p in cost_to_add_byproduct.keys())
products = self.env['product.product'].browse(p.id for p in cost_to_add_byproduct.keys()).with_company(cost.company_id)
for product in products: # iterate on recordset to prefetch efficiently quantity_svl
if not float_is_zero(product.quantity_svl, precision_rounding=product.uom_id.rounding):
product.with_company(cost.company_id).sudo().with_context(disable_auto_svl=True).standard_price += cost_to_add_byproduct[product] / product.quantity_svl
product.sudo().with_context(disable_auto_svl=True).standard_price += cost_to_add_byproduct[product] / product.quantity_svl

move_vals['stock_valuation_layer_ids'] = [(6, None, valuation_layer_ids)]
# We will only create the accounting entry when there are defined lines (the lines will be those linked to products of real_time valuation category).
Expand Down Expand Up @@ -219,6 +219,7 @@ def compute_landed_cost(self):

towrite_dict = {}
for cost in self.filtered(lambda cost: cost._get_targeted_move_ids()):
cost = cost.with_company(cost.company_id)
rounding = cost.currency_id.rounding
total_qty = 0.0
total_cost = 0.0
Expand Down
46 changes: 46 additions & 0 deletions addons/stock_landed_costs/tests/test_stockvaluationlayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,52 @@ def test_alreadyout_2(self):
self.assertEqual(self.product1.value_svl, 375)
self.assertEqual(self.product1.quantity_svl, 19)

def test_lc_generated_from_bill_multi_comapnies(self):
"""
In a multi-company environment:
Confirm PO, receive products, post bill and generate LC
"""
company = self.env.company
self.env.user.company_id = self.env['res.company'].create({
'name': 'Another Company',
})

po_form = Form(self.env['purchase.order'])
po_form.company_id = company
po_form.partner_id = self.partner_a
with po_form.order_line.new() as po_line:
po_line.product_id = self.product1
po_line.product_qty = 1
po_line.price_unit = 10
po_line.taxes_id.clear()
po = po_form.save()
po.button_confirm()

receipt = po.picking_ids
receipt.move_line_ids.quantity = 1
receipt.button_validate()

action = po.action_create_invoice()
bill = self.env['account.move'].browse(action['res_id'])
bill_form = Form(bill)
bill_form.invoice_date = bill_form.date
with bill_form.invoice_line_ids.new() as inv_line:
inv_line.product_id = self.productlc1
inv_line.price_unit = 5
inv_line.is_landed_costs_line = True
bill = bill_form.save()
bill.action_post()

action = bill.button_create_landed_costs()
lc_form = Form(self.env[action['res_model']].browse(action['res_id']))
lc_form.picking_ids.add(receipt)
lc = lc_form.save()
lc.button_validate()

product = self.product1.with_company(company)
self.assertEqual(product.value_svl, 15)
self.assertEqual(product.quantity_svl, 1)
self.assertEqual(product.standard_price, 15)

@tagged('-at_install', 'post_install')
class TestStockValuationLCFIFOVB(TestStockValuationLCCommon):
Expand Down

0 comments on commit 3689958

Please sign in to comment.