# CreateFiscalYear

## Permission Scope

periodManagement

## Overview

createFiscalYear establishes a new fiscal year record for a company. The fiscal year carries a name, start date, end date, and is scoped to a single company via `companyId`. Fiscal years serve as the top-level temporal grouping under which accounting periods are created. The system validates that the fiscal year does not overlap with any existing fiscal year for the same company.

## Business Rules

- Fiscal year requires a valid `companyId` referencing an ACTIVE company from the organization module
- Fiscal year name is required and must be non-empty
- Start date and end date are required, with start date strictly before end date
- Fiscal year date range must not overlap with any existing fiscal year for the same company
- Supports non-calendar fiscal years (e.g., April-to-March, February-to-January)
- Supports 4-4-5 week structures and 13-period fiscal calendars
- Fiscal years are scoped to a company; records from different companies are isolated
- Emits an audit event via the audit module recording the acting user, timestamp, company reference, and fiscal year details

## Process Flow

```mermaid
flowchart TD
    A[Receive create fiscal year request] --> B{Company exists and is ACTIVE?}
    B -->|No| C[Return error: company not found or inactive]
    B -->|Yes| D{Name non-empty?}
    D -->|No| E[Return error: name required]
    D -->|Yes| F{Start date before end date?}
    F -->|No| G[Return error: invalid date range]
    F -->|Yes| H{Overlaps with existing fiscal year?}
    H -->|Yes| I[Return error: overlapping fiscal year]
    H -->|No| J[Create fiscal year record]
    J --> K[Emit audit event]
    K --> L[Return created fiscal year]
```

## External Dependencies

- [organization::getCompany](../../../organization/docs/queries/GetCompany.md) - Validates that the referenced company exists and is ACTIVE

## Error Scenarios

- **COMPANY_NOT_FOUND**: Referenced company does not exist or is not in ACTIVE status
- **NAME_REQUIRED**: Name is empty, whitespace-only, or not provided
- **INVALID_DATE_RANGE**: Start date is not strictly before end date
- **OVERLAPPING_FISCAL_YEAR**: The date range overlaps with an existing fiscal year for the same company

## Test Cases

- returns error when company does not exist
- returns error when company is not ACTIVE
- returns error when name is empty
- returns error when start date is not before end date
- returns error when fiscal year overlaps with an existing fiscal year for the same company
- creates fiscal year with valid name, start date, and end date
- creates non-calendar fiscal year (e.g., April-to-March) with correct date boundaries
- fiscal years from different companies are isolated
- emits audit event recording acting user, timestamp, company reference, and fiscal year details
