Skip to content

Commit

Permalink
[IMP] purchase_stock_price_unit_sync: Avoid errors with cancelled sto…
Browse files Browse the repository at this point in the history
…ck moves

When the price of a line is changed, and it does not have any associated
stock.move completed but does have stock.moves in a different state,
this will result in no SVLs being found. Consequently, attempting to
write to the empty recordset is causing issues.
  • Loading branch information
CarlosRoca13 committed Nov 22, 2024
1 parent fd59c26 commit 7427ade
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions purchase_stock_price_unit_sync/models/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,23 @@ def stock_price_unit_sync(self):
bom_type="phantom",
):
continue
line.move_ids.write({"price_unit": line._get_stock_move_price_unit()})
# Apply sudo() to avoid access errors with users without Inventory > Admin
# permissions.
svls = (
line.move_ids.sudo()
.mapped("stock_valuation_layer_ids")
.filtered(
# Filter children SVLs (like landed cost)
lambda x: not x.stock_valuation_layer_id
moves = line.move_ids.filtered(lambda m: m.state == "done")
if moves:
moves.write({"price_unit": line._get_stock_move_price_unit()})
# Apply sudo() to avoid access errors with users without Inventory > Admin
# permissions.
svls = (
moves.sudo()
.mapped("stock_valuation_layer_ids")
.filtered(
# Filter children SVLs (like landed cost)
lambda x: not x.stock_valuation_layer_id
)
)
svls.write(
{
"unit_cost": line.with_context(
skip_stock_price_unit_sync=True
)._get_stock_move_price_unit(),
}
)
)
svls.write(
{
"unit_cost": line.with_context(
skip_stock_price_unit_sync=True
)._get_stock_move_price_unit(),
}
)

0 comments on commit 7427ade

Please sign in to comment.