# Unit Conversion

## Overview

Unit Conversion enables automatic quantity transformation between any two units within the same category. The conversion uses the reference unit as an intermediary, ensuring consistent results regardless of which units are involved. This feature supports the core ERP operations where quantities must be expressed in different units for purchasing, inventory, and sales.

Conversions include configurable rounding precision to match business requirements for different unit types.

## Business Purpose

Different stakeholders work with different units:

- **Purchasing** buys in bulk units (cases, pallets, drums)
- **Warehouse** tracks in standardized units (pieces, kilograms, liters)
- **Sales** sells in customer-friendly units (packs, bottles, individual items)
- **Manufacturing** measures in precise units (grams, milliliters)

Unit conversion eliminates manual calculation errors and ensures inventory accuracy when the same product moves through these different contexts.

## Process Flow

```mermaid
flowchart TD
    A[Conversion Request] --> B{Same Category?}
    B -->|No| C[Return Error: Incompatible Units]
    B -->|Yes| D[Get Source Unit Factor]
    D --> E[Get Target Unit Factor]
    E --> F[Calculate: Qty × Source Factor ÷ Target Factor]
    F --> G[Apply Rounding Precision]
    G --> H[Return Converted Quantity]

    subgraph Validation
    B
    end

    subgraph Calculation
    D
    E
    F
    G
    end
```

## Scenario Patterns

- **Purchase to Inventory**: Vendor ships 10 cases (12 units/case), system records 120 pieces in inventory
- **Sales Order Fulfillment**: Customer orders 5 kg, warehouse picks 5000 grams from bin tracked in grams
- **Manufacturing Consumption**: Recipe calls for 500ml, inventory deducts 0.5 liters from stock
- **Cross-Unit Reporting**: Generate report showing total weight in both kilograms and pounds for different markets
- **Fractional Handling**: Order for 2.5 dozen converts to 30 pieces for picking

## Test Cases

- Converting between units in the same category should return correct result
- Converting between units in different categories should return an error
- Converting with the same source and target unit should return the original quantity
- Converting to/from the reference unit should use factor of 1.0
- Rounding should respect the target unit's precision setting
- Zero quantity conversion should return zero
- Negative quantity conversion should be handled consistently (error or allow based on business rules)
- Very large quantities should not cause overflow errors

## Reference Links

- [NIST Unit Conversion Guide](https://www.nist.gov/pml/owm/metric-si/unit-conversion)
- [Odoo UoM Conversion Documentation](https://www.odoo.com/documentation/19.0/applications/inventory_and_mrp/inventory/product_management/configure/uom.html)
