# Opening Balance Import Validation Rules

**Task:** AST-1.8.1
**Column contract:** [Opening Balance Import Column Dictionary](./OPENING_BALANCE_IMPORT_COLUMN_DICTIONARY.md)
**Schema source:** [Asset Management database schema](./ASSET_MANAGEMENT_DATABASE_SCHEMA.md)

The rules below define the validation contract for AST-1.8.2 through AST-1.8.4. This spike does not implement them.

## File and sheet checks

- The submitted workbook must use version 1.0.0 and contain exactly the sheets `Serialized Assets`, `Quantity Assets`, and `Optional Warranty`.
- Each sheet must have the exact header sequence in the column dictionary. Unknown, reordered, missing, or duplicated headers are invalid.
- Every non-empty row is validated in the importing company scope. Empty rows are ignored.
- Row errors prevent that row from being confirmed. The later validation card owns the error report.

## Serialized-row checks

- Asset Code and Item Code are required.
- Asset Code is unique in the serialized sheet and must not already exist in `serialized_assets` for the company.
- Item Code resolves to an active `asset_items` record with `tracking_mode = SERIALIZED`.
- A supplied Serial Number is unique for the resolved item in the company.
- Status and Condition use only the allowed values in the column dictionary.
- Supplied Location Code and Cost Center Code resolve in the importing company.
- Acquisition Cost is non-negative; Currency is required when Acquisition Cost is supplied.

## Quantity-row checks

- Item Code, Location Code, Quantity, and Occurred On are required.
- Item Code resolves to an active `asset_items` record with `tracking_mode = QUANTITY`.
- Location Code resolves in the importing company.
- Quantity is strictly greater than zero and has no more than four decimal places.
- Unit Cost, when supplied, is non-negative; Currency is required when Unit Cost is supplied.
- Cost Center Code, when supplied, resolves in the importing company.
- The confirmed transaction type is always `OPENING_BALANCE`; users cannot choose a ledger type from the template.

## Warranty-row checks

- Asset Code, Provider Code, Starts On, and Ends On are required when a warranty row is present.
- Asset Code references a serialized asset created by the same import and must not resolve to a quantity item.
- Provider Code resolves to an active `asset_service_providers` record in the importing company.
- Ends On is on or after Starts On.
- File paths, attachment names, identifiers, company IDs, and audit columns are not accepted from the workbook.

## Confirmation guard

No row may create an asset, warranty, inventory transaction, balance, finance event, or employee-asset record merely by upload or validation. Only the explicit, authorized confirmation flow may apply validated rows. Existing Asset Codes are errors and never updates.
