Skip to content

[FIX] stock_account: a zero unit_cost is not a unit cost - #5951

Open
cuongnmtm wants to merge 3 commits into
OCA:19.0from
komit-consulting:19.0-stock_account-product-value-zero-unit-cost
Open

[FIX] stock_account: a zero unit_cost is not a unit cost#5951
cuongnmtm wants to merge 3 commits into
OCA:19.0from
komit-consulting:19.0-stock_account-product-value-zero-unit-cost

Conversation

@cuongnmtm

Copy link
Copy Markdown

No description provided.

hbrunn and others added 3 commits August 25, 2026 15:22
17.0 writes `unit_cost = 0` -- not NULL -- on a valuation layer that adjusts
the total value of the stock on hand without a per-unit price: a bill
correction, a landed cost, a manual revaluation. The adjustment itself stays
in `value`. The zero means "no per-unit price applies", not "this costs
nothing".

`product_value()` tests `unit_cost IS NOT NULL` to decide whether a layer
already knows its unit cost, so those layers take the first branch and are
inserted with `value = 0`. The second branch, which would have derived the
cost, only looks at `unit_cost IS NULL` and never sees them.

Where such a layer is the most recent move-less one for a product,
`_get_last_product_value()` returns it and the product's cost becomes 0.
That is worse than an obviously wrong cost: a zero reads as a product that
has never been costed, it passes review, and every subsequent delivery is
priced at nothing.

The first branch now takes only a non-zero `unit_cost`, and the second
matches `COALESCE(unit_cost, 0) = 0` so it derives the rest. The
`quantity = 0` restriction goes with it, because a revaluation booked
together with a quantity change carries a total value just the same.

The derivation is also guarded to a positive result. Where the accumulated
history is itself inconsistent -- 17.0 databases can carry wildly negative
revaluation totals -- the division yields a negative "cost", and inserting
no row leaves `standard_price` untouched rather than negative.

The derived cost has been checked on an upgraded 17.0 database against both
the cost 17.0 recorded separately for the product and the COGS 17.0 posted
for its deliveries, which agree.

Adds a product whose layers reproduce the shape, asserting the cost comes
out at 30 (4 bought at 20, plus a 40 revaluation) rather than 0.
Comment on lines +102 to +103
# 17.0 writes unit_cost 0 -- not NULL -- for this kind of adjustment, and the ORM
# offers no way to ask for that shape, so the layer is inserted directly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there's no way to create this kind of record via Odoo functions, how are they relevant?

@MiquelRForgeFlow MiquelRForgeFlow added this to the 19.0 milestone Aug 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mod:openupgrade_scripts Module openupgrade_scripts series:19.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants