# Chart of Accounts - Deployment Guide

## ✅ Status: Ready for Deployment

A comprehensive General Ledger Chart of Accounts with USGAAP and IFRS compliance has been created and is ready for deployment.

---

## 📋 What Was Created

### 1. Service Implementation
- **File:** `src/core/accounting/chart-of-accounts.service.ts`
- **Features:**
  - Standard chart of accounts initialization
  - Account hierarchy management
  - USGAAP and IFRS classifications
  - Account balance calculations
  - CRUD operations

### 2. API Routes
- **File:** `src/core/accounting/chart-of-accounts.routes.ts`
- **Endpoints:** 9 RESTful endpoints for account management

### 3. Database Schema
- **Model:** `ChartOfAccount` (added to Prisma schema)
- **Migration:** `prisma/migrations/add_chart_of_accounts.sql`

### 4. Documentation
- **File:** `docs/accounting/CHART_OF_ACCOUNTS.md`

---

## 🚀 Deployment Steps

### Step 1: Update Prisma Schema

The `ChartOfAccount` model has been added to the schema. Verify it's included:

```prisma
model ChartOfAccount {
  id                  String   @id @default(uuid())
  accountCode         String   @unique
  accountName         String
  category            String
  parentAccountCode   String?
  level               Int
  normalBalance       String
  accountType         String?
  usgaapClassification String?
  ifrsClassification  String?
  description         String?  @db.Text
  isActive            Boolean  @default(true)
  isSystemAccount     Boolean  @default(false)
  metadata            Json?
  createdAt           DateTime @default(now())
  updatedAt           DateTime @updatedAt

  parentAccount       ChartOfAccount? @relation("AccountHierarchy", fields: [parentAccountCode], references: [accountCode])
  childAccounts       ChartOfAccount[] @relation("AccountHierarchy")

  @@index([accountCode])
  @@index([category])
  @@map("chart_of_accounts")
}
```

### Step 2: Generate Prisma Client

```bash
cd dbis_core
npx prisma generate
```

### Step 3: Run Migration

```bash
# Create and apply migration
npx prisma migrate dev --name add_chart_of_accounts

# Or apply existing migration
npx prisma migrate deploy
```

### Step 4: Register API Routes

Add to your main router:

```typescript
import chartOfAccountsRoutes from '@/core/accounting/chart-of-accounts.routes';

app.use('/api/accounting/chart-of-accounts', chartOfAccountsRoutes);
```

### Step 5: Initialize Chart of Accounts

```bash
# Via API
curl -X POST http://localhost:3000/api/accounting/chart-of-accounts/initialize

# Or programmatically
import { chartOfAccountsService } from '@/core/accounting/chart-of-accounts.service';
await chartOfAccountsService.initializeChartOfAccounts();
```

---

## 📊 Account Structure Summary

### Assets (1000-1999)
- **1100** Current Assets
  - **1110** Cash and Cash Equivalents
  - **1120** Accounts Receivable
  - **1130** Settlement Assets
  - **1140** CBDC Holdings
  - **1150** GRU Holdings
- **1200** Non-Current Assets
  - **1210** Property, Plant and Equipment
  - **1220** Intangible Assets
  - **1230** Long-term Investments
  - **1300** Commodity Reserves

### Liabilities (2000-2999)
- **2100** Current Liabilities
  - **2110** Accounts Payable
  - **2120** Short-term Debt
  - **2130** Vostro Accounts
  - **2140** CBDC Liabilities
  - **2150** GRU Liabilities
- **2200** Non-Current Liabilities
  - **2210** Long-term Debt
  - **2220** Bonds Payable

### Equity (3000-3999)
- **3100** Capital
- **3200** Retained Earnings
- **3300** Reserves

### Revenue (4000-4999)
- **4100** Operating Revenue
  - **4110** Interest Income
  - **4120** Fee Income
  - **4130** FX Trading Revenue
- **4200** Non-Operating Revenue

### Expenses (5000-6999)
- **5100** Operating Expenses
  - **5110** Interest Expense
  - **5120** Personnel Expenses
  - **5130** Technology and Infrastructure
  - **5140** Depreciation Expense
  - **5150** Amortization Expense
  - **5160** Provision for Loan Losses
- **5200** Non-Operating Expenses

---

## ✅ Compliance Status

### USGAAP Compliance
- ✅ Standard account classifications
- ✅ Normal balance rules
- ✅ Contra-accounts (e.g., Allowance for Doubtful Accounts)
- ✅ Depreciation and amortization
- ✅ Provision for credit losses

### IFRS Compliance
- ✅ IFRS account classifications
- ✅ Revaluation reserves
- ✅ Expected credit losses (IFRS 9)
- ✅ Financial instruments classification
- ✅ Share capital structure

---

## 🔗 Integration Points

### With Ledger System
```typescript
// Use chart of accounts codes in ledger entries
await ledgerService.postDoubleEntry(
  ledgerId,
  '1112', // Cash in Banks
  '4110', // Interest Income
  amount,
  currencyCode,
  assetType,
  transactionType,
  referenceId
);
```

### With Reporting Engine
```typescript
// Generate financial statements using chart of accounts
const balanceSheet = await generateBalanceSheet({
  assets: await getAccountsByCategory(AccountCategory.ASSET),
  liabilities: await getAccountsByCategory(AccountCategory.LIABILITY),
  equity: await getAccountsByCategory(AccountCategory.EQUITY),
});
```

---

## 📝 Verification

After deployment, verify:

```bash
# Get all accounts
curl http://localhost:3000/api/accounting/chart-of-accounts

# Get assets
curl http://localhost:3000/api/accounting/chart-of-accounts/category/ASSET

# Get account hierarchy
curl http://localhost:3000/api/accounting/chart-of-accounts/1000/hierarchy
```

---

## 🎯 Result

✅ **Chart of Accounts is fully implemented and deployable!**

- ✅ 50+ standard accounts defined
- ✅ USGAAP compliant
- ✅ IFRS compliant
- ✅ Hierarchical structure
- ✅ API endpoints ready
- ✅ Database schema ready
- ✅ Service implementation complete

---

**Status:** Ready for deployment and integration with the General Ledger system.
