# Task 06 — Chart of Accounts

## Goal
Build the Chart of Accounts module as the accounting foundation for ERPdash.
This module must support hierarchical accounts, translated names, account types, normal balance, and postable/non-postable accounts.

---

## Database

Table: `accounts`

Fields:
- id
- code (string, unique)
- name (json, translatable)
- parent_id (nullable, FK to accounts.id)
- type (string)
  - asset
  - liability
  - equity
  - income
  - expense
- normal_balance (string)
  - debit
  - credit
- level (unsignedTinyInteger, default 1)
- is_postable (boolean, default true)
- is_active (boolean, default true)
- created_by (nullable, FK to admins.id)
- updated_by (nullable, FK to admins.id)
- created_at
- updated_at

Indexes:
- unique code
- index parent_id
- index type
- index is_postable
- index is_active

---

## Relations

Account model relations:
- parent() -> belongsTo Account
- children() -> hasMany Account
- creator() -> belongsTo Admin
- updater() -> belongsTo Admin

Future relations:
- journalEntryLines()
- balances()

---

## Business Rules

1. Parent accounts are usually not postable.
2. Final child accounts are postable.
3. Level must be calculated automatically:
   - root = 1
   - child = parent.level + 1
4. Code must be unique.
5. Name must be stored as JSON using HasTranslations.
6. Account type and normal balance are required.

---

## Files

### Model
- app/Models/Finance/Account.php

### Service
- app/Services/Finance/AccountService.php

### Controller
- app/Http/Controllers/Admin/Finance/AccountController.php

### Requests
- app/Http/Requests/Admin/Finance/StoreAccountRequest.php
- app/Http/Requests/Admin/Finance/UpdateAccountRequest.php

### Views
- resources/views/dashboard/finance/accounts/index.blade.php
- resources/views/dashboard/finance/accounts/create.blade.php
- resources/views/dashboard/finance/accounts/edit.blade.php

### Lang files
- lang/ar/finance.php
- lang/en/finance.php

---

## Permissions

- finance.accounts.view
- finance.accounts.create
- finance.accounts.update
- finance.accounts.delete

---

## Validation Rules

- code: required, unique
- name.ar: required, string
- name.en: nullable, string
- parent_id: nullable, exists:accounts,id
- type: required, in:asset,liability,equity,income,expense
- normal_balance: required, in:debit,credit
- is_postable: boolean
- is_active: boolean

---

## UI

Views path:
`resources/views/dashboard/finance/accounts/`

Fields in form:
- code
- name_ar
- name_en
- parent account
- type
- normal_balance
- is_postable
- is_active

---

## Lang Keys

Suggested keys:
- accounts
- account
- create_account
- edit_account
- account_code
- account_name
- account_type
- normal_balance
- is_postable
- is_active
- parent_account

---

## Steps

### Step 1
Create migration for `accounts` table.

### Step 2
Create `Account` model with HasTranslations.

### Step 3
Create `AccountService`.

### Step 4
Create Form Requests.

### Step 5
Create `AccountController`.

### Step 6
Create routes.

### Step 7
Create views.

### Step 8
Create lang files.

### Step 9
Update RolePermissionSeeder with finance.accounts permissions.

---

## Notes for AI

- Follow docs/ai/AI_MASTER_PROJECT.md
- Follow docs/ai/ARCHITECTURE_RULES.md
- Follow docs/ai/DATABASE_SCHEMA.md
- Follow docs/ai/MODULE_STRUCTURE.md
- Execute one step at a time
- Do not modify unrelated files
- Do not generate tests in this task
- Wait for confirmation after each step

