# 🧾 Accounting Module - Complete Setup

## ✅ What's Been Created

### 📊 Database Structure

**1. Natures (Account Types)**
- Assets (A) - Debit
- Liabilities (L) - Credit
- Capital/Equity (C) - Credit
- Income (I) - Credit
- Expenses (E) - Debit

**2. Sub-Natures**
- Current Assets (CA)
- Fixed Assets (FA)
- Current Liabilities (CL)
- Long Term Liabilities (LTL)
- Owners Equity (OE)
- Operating Income (OI)
- Non-Operating Income (NOI)
- Operating Expenses (OPEX)
- Non-Operating Expenses (NOE)

**3. Ledger Groups**
- Cash
- Bank Accounts
- Debtors/Receivables
- Creditors/Payables
- Capital
- Sales/Service Revenue

**4. Ledgers**
- Cash in Hand
- Main Bank Account
- Owner's Capital
- (Custom ledgers can be added)

**5. Vouchers**
- Journal Voucher (JV)
- Receipt Voucher (RV)
- Payment Voucher (PV)
- Contra Voucher (CV)

### 📁 Files Created

**Models:**
- `app/Models/Nature.php`
- `app/Models/SubNature.php`
- `app/Models/LedgerGroup.php`
- `app/Models/Ledger.php`
- `app/Models/Voucher.php`
- `app/Models/VoucherTransaction.php`

**Controllers:**
- `app/Http/Controllers/Admin/Accounting/AccountingController.php`
- `app/Http/Controllers/Admin/Accounting/NatureController.php`
- `app/Http/Controllers/Admin/Accounting/SubNatureController.php`
- `app/Http/Controllers/Admin/Accounting/LedgerGroupController.php`
- `app/Http/Controllers/Admin/Accounting/LedgerController.php`
- `app/Http/Controllers/Admin/Accounting/VoucherController.php`

**Migrations:**
- `2025_11_30_104318_add_natures_table.php`
- `2025_11_30_104409_add_sub_natures_table.php`
- `2025_11_30_081219_create_ledger_groups_table.php` (updated)
- `2025_11_30_081219_create_ledgers_table.php` (updated)
- `2025_11_30_081219_create_vouchers_table.php` (updated)
- `2025_11_30_081219_create_voucher_transactions_table.php` (updated)

**Seeders:**
- `database/seeders/AccountingSeeder.php`

## 🗂️ Table Structure

### `natures`
```sql
- id
- name (Assets, Liabilities, etc.)
- code (A, L, C, I, E)
- type (debit/credit)
- description
- is_active
- sort_order
- timestamps
```

### `sub_natures`
```sql
- id
- nature_id (FK)
- name (Current Assets, Fixed Assets, etc.)
- code
- description
- is_active
- sort_order
- timestamps
```

### `ledger_groups`
```sql
- id
- sub_nature_id (FK)
- name (Cash, Bank, Debtors, etc.)
- code
- description
- is_active
- sort_order
- timestamps
```

### `ledgers`
```sql
- id
- ledger_group_id (FK)
- garage_id (FK, nullable)
- name (specific ledger name)
- code
- description
- opening_balance
- opening_balance_type (debit/credit)
- contact_person
- phone
- email
- address
- tax_number
- credit_limit
- is_active
- timestamps
```

### `vouchers`
```sql
- id
- garage_id (FK, nullable)
- voucher_type (journal/receipt/payment/contra)
- voucher_no (unique)
- voucher_date
- reference_no
- narration
- total_amount
- created_by (FK to super_admins)
- approved_by (FK to super_admins)
- approved_at
- status (draft/posted/cancelled)
- cheque_no
- cheque_date
- cheque_bank
- timestamps
- deleted_at (soft deletes)
```

### `voucher_transactions`
```sql
- id
- voucher_id (FK)
- ledger_id (FK)
- transaction_type (debit/credit)
- amount
- narration
- sort_order
- timestamps
```

## 🔗 Relationships

**Nature** → hasMany → SubNature  
**SubNature** → hasMany → LedgerGroup  
**LedgerGroup** → hasMany → Ledger  
**Ledger** → hasMany → VoucherTransaction  
**Voucher** → hasMany → VoucherTransaction  

## 💡 Features

### Account Management
- ✅ Hierarchical account structure (Nature → Sub-Nature → Group → Ledger)
- ✅ Opening balance support
- ✅ Contact information for party ledgers
- ✅ Credit limit management
- ✅ Current balance calculation

### Voucher Types

**1. Journal Voucher (JV)**
- General journal entries
- Adjustments and corrections
- Any debit/credit combination

**2. Receipt Voucher (RV)**
- Cash/Bank receipts
- Customer payments
- Income entries

**3. Payment Voucher (PV)**
- Cash/Bank payments
- Vendor payments
- Expense entries

**4. Contra Voucher (CV)**
- Cash to Bank transfers
- Bank to Cash transfers
- Inter-bank transfers

### Voucher Features
- ✅ Multi-line entries (Dr/Cr)
- ✅ Narration support
- ✅ Cheque management
- ✅ Approval workflow
- ✅ Draft/Posted/Cancelled status
- ✅ Soft deletes
- ✅ Audit trail (created_by, approved_by)

## 🎯 Next Steps

1. **Create Views** - Build Blade templates for:
   - Accounting dashboard
   - Chart of Accounts
   - Ledger management
   - Voucher entry forms
   - Reports (Trial Balance, Ledger Books, etc.)

2. **Add Routes** - Set up routing in `routes/admin.php`

3. **Implement Controllers** - Add logic for CRUD operations

4. **Create Reports:**
   - Trial Balance
   - Ledger Books
   - Day Book
   - Cash Book
   - Bank Book
   - Profit & Loss
   - Balance Sheet

## 📝 Model Methods

### Ledger::getCurrentBalance()
Returns the current balance of a ledger based on opening balance and transactions.

### Voucher Scopes
- `journal()` - Filter journal vouchers
- `receipt()` - Filter receipt vouchers
- `payment()` - Filter payment vouchers
- `contra()` - Filter contra vouchers
- `posted()` - Filter posted vouchers
- `draft()` - Filter draft vouchers

## 🚀 Usage Examples

### Create a Receipt Voucher
```php
$voucher = Voucher::create([
    'voucher_type' => 'receipt',
    'voucher_no' => 'RV-001',
    'voucher_date' => now(),
    'narration' => 'Cash received from customer',
    'total_amount' => 5000,
    'created_by' => auth()->id(),
    'status' => 'posted',
]);

// Debit: Cash
$voucher->transactions()->create([
    'ledger_id' => $cashLedger->id,
    'transaction_type' => 'debit',
    'amount' => 5000,
]);

// Credit: Customer
$voucher->transactions()->create([
    'ledger_id' => $customerLedger->id,
    'transaction_type' => 'credit',
    'amount' => 5000,
]);
```

### Get Ledger Balance
```php
$ledger = Ledger::find(1);
$balance = $ledger->getCurrentBalance();
```

### Query Vouchers
```php
// All posted receipts
$receipts = Voucher::receipt()->posted()->get();

// All draft payments
$payments = Voucher::payment()->draft()->get();
```

## 📊 Default Data Seeded

- 5 Natures (Assets, Liabilities, Capital, Income, Expenses)
- 9 Sub-Natures
- 6 Ledger Groups
- 3 Default Ledgers (Cash, Bank, Capital)

## ✨ System Ready!

Your accounting module is now set up with a professional double-entry bookkeeping system ready to handle all financial transactions!

Next: Create the views and complete the UI! 🎨












