Skip to content

[19.0][MIG] stock_account_valuation_report#456

Open
AaronHForgeFlow wants to merge 32 commits into
OCA:19.0from
ForgeFlow:19.0-mig-stock_account_valuation_report
Open

[19.0][MIG] stock_account_valuation_report#456
AaronHForgeFlow wants to merge 32 commits into
OCA:19.0from
ForgeFlow:19.0-mig-stock_account_valuation_report

Conversation

@AaronHForgeFlow

Copy link
Copy Markdown
Contributor

The stock value info is taken from the stock move instead of the layers

@AaronHForgeFlow AaronHForgeFlow force-pushed the 19.0-mig-stock_account_valuation_report branch from bbbf972 to 2281b5a Compare November 25, 2025 16:02

@gdgellatly gdgellatly left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Static review + Functional test on live migrated db with 80k products and 10s of millions of moves. Essentially functionally equivelant despite move from svl's It is still really slow but works as expected, maybe marginally better than before.

Update: OK really slow is understating this, before it was 2 or 3 minutes, now still loading after 15 mins when filter set to Valuation Discrepancy.

Alright effectively unusable on large db's although I can see it works.

Final update: It loaded 80 records using Valuation discrepancy after 1 hour. I scrolled to the next 80 in the hope it had them all. It did not. I feel like the idea of this module has value, but we need to rethink how it is done.

Comment thread stock_account_valuation_report/models/product_product.py Outdated

@gdgellatly gdgellatly left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I am trying to make this more performant, but honestly, so far not much luck, just what I've found so far.

Also I think we canhandle the zero cases better. If there are no aml's and/or no moves we can skip many computations.

class ProductProduct(models.Model):
_inherit = "product.product"

stock_value = fields.Float("Inventory Value", compute="_compute_inventory_value")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

How does this differ from total_value on product_product? Also why not fields.Monetary

Comment thread stock_account_valuation_report/models/product_product.py Outdated
Comment thread stock_account_valuation_report/models/product_product.py Outdated
Comment thread stock_account_valuation_report/models/product_product.py Outdated
Comment thread stock_account_valuation_report/models/product_product.py Outdated
Comment thread stock_account_valuation_report/models/product_product.py Outdated
@gdgellatly

gdgellatly commented Mar 3, 2026

Copy link
Copy Markdown

I have an alternative implementation on top of this PR that addresses significant performance issues and a bug we encountered:

Branch: gdgellatly:19.0-gdg-valuation-perf

Bug fix

The existing accounting quantity calculation uses a CASE WHEN aml.balance < 0 THEN -aml.quantity ELSE aml.quantity END pattern. This was correct in older Odoo versions where aml.quantity was always unsigned (positive), but in v19 aml.quantity is already signed (negative for credit/outgoing lines). The CASE double-negates outgoing quantities, making them positive — so outgoing goods are added to the accounting quantity instead of subtracted.

Fix: replaced with sum(aml.quantity).

Performance changes

  • Use core's total_value and qty_available instead of iterating stock.move records and summing remaining_qty/remaining_value — leverages Odoo's batch-optimised computation
  • Pre-filter products via SQL — only storable products with done stock moves are considered, skipping thousands of irrelevant products
  • Filter AML query by valuation accounts only — there are typically very few valuation accounts per company, so adding account_id IN (...) dramatically reduces the rows scanned
  • Approximate stock value in discrepancy search for AVCO/Standard — uses qty_available * standard_price for Standard and AVCO products instead of triggering the expensive per-move replay during the search filter. FIFO products still use the exact total_value. The accurate total_value from core is always displayed in the list view for the rendered page.
  • Removed pre-computed Many2many fields (stock_fifo_real_time_aml_ids, stock_move_valuated_ids) — button drill-downs now build their domains on click, avoiding bulk computation
  • fields.Monetary with company_currency_id for value fields, digits="Product Unit" for quantity fields
  • Aligned wizard context key from at_date to core's to_date
  • Updated view: replaced stock_value with core total_value, fixed is_storable domain, removed redundant widget="monetary" attributes

Performance

On a database with ~2000 storable products and several million stock moves, the valuation discrepancy filter went from over 60 minutes (original implementation timed out) to ~30 seconds. The remaining ~30s is dominated by core's _compute_value rendering total_value for the displayed page (FIFO/AVCO replay), which is outside the scope of this module.

Caveat

The valuation discrepancy search uses qty_available * standard_price as a proxy for stock value for Standard and AVCO cost methods only. This is:

  • Exact for Standard Cost products
  • Very close for AVCO (standard_price is updated to weighted average on each move)
  • FIFO products use the exact total_value — no approximation

This means the search filter results should be equivalent for Standard and FIFO, and near-equivalent for AVCO. The accurate value is always shown in the list view itself.

@gdgellatly

Copy link
Copy Markdown

OK I give up, this is almost impossible, the quantity field on aml is used differently in different operations, I just can't get it to make any sense. whether I use the current algorithm (definitely wrong), quantity, or -quantity. Anyway hopefully some of this helps

@AaronHForgeFlow

Copy link
Copy Markdown
Contributor Author

Hi @gdgellatly, thank you, I am afraid I have not had the time to review your branch.

What's the overall performance improvement for your large database? I'll review it and likely merge it to this branch.

@gdgellatly

Copy link
Copy Markdown

Hi @gdgellatly, thank you, I am afraid I have not had the time to review your branch.

What's the overall performance improvement for your large database? I'll review it and likely merge it to this branch.

@AaronHForgeFlow It is just a bunch of experiments, and not very well working ones at that. I would not merge it. I actually feel like we might need to go back to the old v7 days where we used to maintain historical aggregates to lower the search window. I will relook later this week to see if we can more easily integrate that approach because it won't be just this module, just this module because of it's non stored computes is super badly affected

JordiBForgeFlow and others added 22 commits March 23, 2026 16:53
Currently translated at 100.0% (13 of 13 strings)

Translation: stock-logistics-reporting-12.0/stock-logistics-reporting-12.0-stock_account_valuation_report
Translate-URL: https://translation.odoo-community.org/projects/stock-logistics-reporting-12-0/stock-logistics-reporting-12-0-stock_account_valuation_report/es_MX/
[IMP] stock_account_valuation_report: filter by discrepancies
Currently translated at 100.0% (13 of 13 strings)

Translation: stock-logistics-reporting-15.0/stock-logistics-reporting-15.0-stock_account_valuation_report
Translate-URL: https://translation.odoo-community.org/projects/stock-logistics-reporting-15-0/stock-logistics-reporting-15-0-stock_account_valuation_report/sl/
… filtering products with discrepancies

also add a filter to see all discrepancies at once
Currently translated at 100.0% (37 of 37 strings)

Translation: stock-logistics-reporting-16.0/stock-logistics-reporting-16.0-stock_account_valuation_report
Translate-URL: https://translation.odoo-community.org/projects/stock-logistics-reporting-16-0/stock-logistics-reporting-16-0-stock_account_valuation_report/it/
StefanRijnhart and others added 9 commits March 23, 2026 16:53
Currently translated at 100.0% (37 of 37 strings)

Translation: stock-logistics-reporting-18.0/stock-logistics-reporting-18.0-stock_account_valuation_report
Translate-URL: https://translation.odoo-community.org/projects/stock-logistics-reporting-18-0/stock-logistics-reporting-18-0-stock_account_valuation_report/it/
1. stock_value, account_value, valuation_discrepancy: fields.Monetary
2. qty_at_date, account_qty_at_date, qty_discrepancy:added digits
3. Removed precomputed stock_fifo_real_time_aml_ids / stock_move_valuated_ids; AMLs fetched on-demand
4. _search_valuation_discrepancy simplified to float_compare
5. Use aml.parent_state to avoid JOIN
6. Fixed CASE aml.quanitity condition
@AaronHForgeFlow AaronHForgeFlow force-pushed the 19.0-mig-stock_account_valuation_report branch from 4fc711b to 2120ec0 Compare March 23, 2026 15:53
@AaronHForgeFlow

Copy link
Copy Markdown
Contributor Author

Thank you @gdgellatly , I applied the code fixes suggestions. Also partial of the performance improvements suggestions. In a small size database (>10000 products, around (1000 stock moves and journal items) the performance improvement is significant (from 5 seconds to almost immediate). Likely not sufficient for large databases, but I think they are useful for small companies.

For large companies, we can store the report and run it overnight with an scheduled action, but I prefer to wait until I have a real use case in v19.

@AaronHForgeFlow AaronHForgeFlow force-pushed the 19.0-mig-stock_account_valuation_report branch from 2120ec0 to c65b732 Compare March 23, 2026 17:04

@rrebollo rrebollo left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I did some suggestions for your consideration

Comment thread stock_account_valuation_report/models/product_product.py
Comment thread stock_account_valuation_report/models/product_product.py Outdated
Comment thread stock_account_valuation_report/models/product_product.py Outdated
Comment thread stock_account_valuation_report/models/product_product.py Outdated
Comment thread stock_account_valuation_report/models/product_product.py Outdated
Comment thread stock_account_valuation_report/tests/test_stock_account_valuation_report.py Outdated
Comment thread stock_account_valuation_report/tests/test_stock_account_valuation_report.py Outdated
Comment thread stock_account_valuation_report/tests/test_stock_account_valuation_report.py Outdated
Comment thread stock_account_valuation_report/wizards/stock_valuation_history.py Outdated
Comment thread stock_account_valuation_report/wizards/stock_valuation_history.py Outdated
@rrebollo

Copy link
Copy Markdown

@AaronHForgeFlow would you be so kind to review my #479 in return?

@AaronHForgeFlow AaronHForgeFlow force-pushed the 19.0-mig-stock_account_valuation_report branch from c65b732 to 41baee6 Compare March 24, 2026 12:20
…ossible

[IMP] stock_account_valuation_report: Domain
@AaronHForgeFlow AaronHForgeFlow force-pushed the 19.0-mig-stock_account_valuation_report branch from 41baee6 to 5f0b736 Compare March 24, 2026 12:23
@AaronHForgeFlow

Copy link
Copy Markdown
Contributor Author

@rrebollo Thank you for the suggestions, I applied them all.

@rrebollo rrebollo left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review. LGTM!

@BhaveshHeliconia BhaveshHeliconia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Functional review LGTM!

@gdgellatly

Copy link
Copy Markdown

Thank you @gdgellatly , I applied the code fixes suggestions. Also partial of the performance improvements suggestions. In a small size database (>10000 products, around (1000 stock moves and journal items) the performance improvement is significant (from 5 seconds to almost immediate). Likely not sufficient for large databases, but I think they are useful for small companies.

For large companies, we can store the report and run it overnight with an scheduled action, but I prefer to wait until I have a real use case in v19.

Hi @AaronHForgeFlow I have not got back to this. But my current thinking is that we create markers to reduce the moves under consideration. You will notice that in certain operation such as changing standard price odoo creates a stock value record. When computing stock value it limits the moves search to the earliest of these records for AVCO/FIFO replay. I have not looked at how to implement yet.

We have also traced back most of the performance issues to lot valuated products, so the scope of issues may be limited and I am working with Odoo to see what we can do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.