# UpdateValuationPolicy

## Permission Scope

inventoryMaster

## Overview

updateValuationPolicy updates an existing valuation policy's name, default flag, or posting accounts (inventory, accrual, invoice price variance, COGS, PPV, adjustment, standard cost adjustment, consumed price variance). Standard cost rates are not managed here — they are published per item via publishStandardCost.

## Business Rules

- Valuation policy must exist
- The owning company and the costing method cannot be changed
- At most one policy per company can be the default; marking a policy as default fails while another default exists in the same company
- Posting accounts can be updated to a different account of the owning company; they are required, so none can be cleared

## Process Flow

```mermaid
flowchart TD
    A[Receive update request] --> B{Policy exists?}
    B -->|No| C[Return error: VALUATION_POLICY_NOT_FOUND]
    B -->|Yes| H{Updated 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[Update policy]
    F --> G[Return updated policy]
```

## External Dependencies

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

## Error Scenarios

- **VALUATION_POLICY_NOT_FOUND**: Referenced valuation policy 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

- returns error when not found
- updates policy name
- updates posting accounts
- marks a policy as default
- returns error when a posting account does not exist in the company
- returns error when another default policy exists in the same company
