# Stock Valuation Layer Category Groupby

## Description

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, 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 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 "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` 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

All fields are indexed for better performance and are automatically updated when product categories change.

## Bug Tracker

Bugs are tracked on [GitHub Issues](https://github.com/OCA/stock-logistics-warehouse/issues).

## Credits

### Contributors

* Criptomart

### Maintainers

This module is maintained by the OCA.

## Compatibility

- Odoo 16.0
- stock_account
