# TANRAIL ONE — Finance Core Remediation Plan

Based on the Finance Core Gap Analysis, the following architectural and implementation fixes are required to stabilize the financial foundation before Business Extensions are built.

## 1. Eliminate Hardcoded Account Lookups (CRITICAL)
**Reason**: Integration listeners (`RecordSalesPaymentListener`) are searching for accounts by hardcoded strings like `'CASH'`. This breaks if the Organization renames its account codes.
**Action**:
- **Migration**: Create `organization_financial_settings` table (or similar) with fields like `organization_id`, `default_cash_account_id`, `default_ar_account_id`, `default_ap_account_id`, `default_sales_revenue_account_id`.
- **Model**: Create `OrganizationFinancialSetting` model.
- **Listeners**: Update all financial listeners to resolve accounts through this settings model instead of querying by code.

## 2. Implement Account Hierarchy (MEDIUM)
**Reason**: Reporting requires rolled-up totals (e.g., summarizing all Bank accounts under Current Assets).
**Action**:
- **Migration**: Add `parent_id` (foreign key to `accounts`) to the `accounts` table.
- **Model**: Add `parent()` and `children()` relationships to the `Account` model.

## 3. Enable Tax Accounting (HIGH)
**Reason**: When calculating taxes on an invoice, the system must know which liability account to credit.
**Action**:
- **Migration**: Add `liability_account_id` (foreign key to `accounts`) to the `tax_categories` table.
- **Model**: Add `liabilityAccount()` relationship to `TaxCategory`.

## 4. Introduce Journal Types (MEDIUM)
**Reason**: Essential for recording "Opening Balances" and differentiating them from daily operational transactions.
**Action**:
- **Migration**: Add `type` (string/enum) to `journals` table.
- **Enum**: Create `JournalType` enum (`OPERATIONAL`, `MANUAL`, `OPENING_BALANCE`).
- **Model/Service**: Update `FinancialService::postJournal()` to accept a `JournalType`.

## 5. Backward Compatibility Impact
- Existing tests (`Phase33FinancialEngineTest`, `Phase34CoreIntegrationTest`) will fail because they currently rely on seeding `CASH`, `AR`, and `AP` accounts and implicitly passing the hardcoded string lookups. These tests must be updated to seed and utilize the new `OrganizationFinancialSetting` mapping.

## Approval Required
This plan requires explicit human approval before any code modifications, migrations, or tests are altered.
