Criptomart/red-supermercados-coop#6 stock_valuation_layer_category_groupby: add group_by parent categories

This commit is contained in:
Luis 2025-12-10 12:57:05 +01:00
parent 51a53f54ba
commit 9d426ff176
5 changed files with 157 additions and 17 deletions

View file

@ -2,40 +2,60 @@
## Description
This module allows grouping by product category in the stock valuation layer reports.
This module allows grouping by product category at different hierarchy levels in the stock valuation layer reports.
### Problem
By default, the `categ_id` field in `stock.valuation.layer` is a related field without storage (`store=False`). This means it cannot be used for grouping in reports and pivot views, even though it can be used for filtering and searching.
Additionally, when dealing with hierarchical category structures (e.g., Food > Dairy > Cheese > Cheddar), it's often useful to group by different levels of the hierarchy.
### Solution
This module makes the `categ_id` field stored (`store=True`) and indexed, which enables:
- Grouping by product category in list views
- Grouping by product category in pivot views
- Grouping by product category in graph views
This module makes the `categ_id` field stored (`store=True`) and indexed, and adds computed fields for each category hierarchy level (up to 4 levels), enabling:
- Grouping by product category (full path) in list, pivot, and graph views
- Grouping by category level 1 (top-level category)
- Grouping by category level 2 (second-level category)
- Grouping by category level 3 (third-level category)
- Grouping by category level 4 (fourth-level category)
- Better performance when filtering by category
## Installation
1. Install the module from the Apps menu
2. The field will be automatically populated for existing records
2. The fields will be automatically populated for existing records
## Usage
After installation:
1. Go to Inventory > Reporting > Inventory Valuation
2. Switch to Pivot or Graph view
3. Click on "Measures" or group options
4. You will now see "Product Category" available for grouping
3. Click on "Group By" options
4. You will now see multiple category grouping options:
- **Product Category**: Groups by the full category (leaf node)
- **Category Level 1**: Groups by top-level category
- **Category Level 2**: Groups by second-level category
- **Category Level 3**: Groups by third-level category
- **Category Level 4**: Groups by fourth-level category
### Example
If you have a category structure like:
- Food (Level 1)
- Dairy (Level 2)
- Cheese (Level 3)
- Cheddar (Level 4)
You can now group your inventory valuation by "Food" (Level 1), "Dairy" (Level 2), "Cheese" (Level 3), or "Cheddar" (Level 4) as needed.
## Technical Details
The module extends `stock.valuation.layer` model and modifies the `categ_id` field to:
- `store=True`: Store the value in the database
- `index=True`: Add database index for better performance
The module extends `stock.valuation.layer` model and:
- Modifies the `categ_id` field to: `store=True` and `index=True`
- Adds computed stored fields: `categ_level_1_id`, `categ_level_2_id`, `categ_level_3_id`, `categ_level_4_id`
- Uses the `parent_path` field from `product.category` to determine hierarchy levels
The field remains a related field, so it will automatically update when the product category changes.
All fields are indexed for better performance and are automatically updated when product categories change.
## Bug Tracker