# CreateValuationPolicy

## Permission Scope

inventoryMaster

## Overview

createValuationPolicy creates a new valuation policy that defines the costing method used to calculate inventory value and carries the owning company's posting accounts for costing journals. The supported methods are STANDARD, FIFO, and AVERAGE.

## Business Rules

- The referenced company must exist
- Costing method is STANDARD, FIFO, or AVERAGE
- At most one policy per company can be the default; items without an ItemValuation record are auto-assigned the company's default policy on their first valuation-affecting movement
- All eight posting accounts are required and must be existing accounts of the owning company

## Process Flow

```mermaid
flowchart TD
    A[Receive create request] --> B{Company exists?}
    B -->|No| C[Return error: COMPANY_NOT_FOUND]
    B -->|Yes| H{All posting accounts exist in the company?}
    H -->|No| I[Return error: ACCOUNT_NOT_FOUND]
    H -->|Yes| D{isDefault and another default exists in the company?}
    D -->|Yes| E[Return error: DEFAULT_VALUATION_POLICY_ALREADY_EXISTS]
    D -->|No| F[Create valuation policy]
    F --> G[Return created policy]
```

## External Dependencies

- [organization::Company](../../../organization/docs/model/Company.md) - Validates the owning company
- [coa-management::Account](../../../coa-management/docs/model/Account.md) - Validates the posting accounts against the company's chart of accounts

## Error Scenarios

- **COMPANY_NOT_FOUND**: Referenced company does not exist
- **ACCOUNT_NOT_FOUND**: Referenced posting account does not exist in the company
- **DEFAULT_VALUATION_POLICY_ALREADY_EXISTS**: Another valuation policy in the same company is already marked as the default

## Test Cases

- creates standard cost policy with posting accounts
- creates fifo policy
- creates average cost policy
- creates a default policy
- creates a default policy when another company already has one
- returns error when company does not exist
- returns error when a posting account does not exist in the company
- returns error when another default policy exists in the same company
