# Chart of Accounts (COA) API

## Get Chart of Accounts

Retrieve the chart of accounts for an organization.

**Endpoint:** `POST https://api.quick.myquickapp.com/accounting/coa`

### Request Headers

```
Authorization: Bearer {api_key}
Content-Type: application/json
language: en
```

### Request Body - List Accounts

```json
{
  "org_id": "12345",
  "userid": "123"
}
```

### Response

```json
{
  "statusCode": 200,
  "headers": {
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Headers": "*",
    "Access-Control-Allow-Methods": "GET,POST,OPTIONS"
  },
  "body": {
    "chart_of_accounts": [
      {
        "id": "acc_101",
        "code": "1000",
        "name": "Bank Account",
        "type": "Asset",
        "subtype": "Current Asset",
        "category": "Bank",
        "currency": "ZAR",
        "balance": 50000.00,
        "is_active": true,
        "parent_id": null
      },
      {
        "id": "acc_201",
        "code": "2000",
        "name": "Accounts Payable",
        "type": "Liability",
        "subtype": "Current Liability",
        "category": "Payable",
        "currency": "ZAR",
        "balance": -10000.00,
        "is_active": true,
        "parent_id": null
      },
      {
        "id": "acc_301",
        "code": "3000",
        "name": "Sales Revenue",
        "type": "Income",
        "subtype": "Operating Income",
        "category": "Sales",
        "currency": "ZAR",
        "balance": 100000.00,
        "is_active": true,
        "parent_id": null
      },
      {
        "id": "acc_401",
        "code": "4000",
        "name": "Cost of Goods Sold",
        "type": "Expense",
        "subtype": "Operating Expense",
        "category": "COGS",
        "currency": "ZAR",
        "balance": 40000.00,
        "is_active": true,
        "parent_id": null
      }
    ],
    "tree": {
      "root": [
        {
          "id": "acc_101",
          "code": "1000",
          "name": "Bank Account",
          "children": []
        }
      ]
    }
  }
}
```

---

## Get Account Details

Get details for a specific account.

### Request Body

```json
{
  "org_id": "12345",
  "account_id": "acc_101"
}
```

### Response

```json
{
  "statusCode": 200,
  "headers": {...},
  "body": {
    "account": {
      "id": "acc_101",
      "code": "1000",
      "name": "Bank Account",
      "type": "Asset",
      "subtype": "Current Asset",
      "category": "Bank",
      "currency": "ZAR",
      "balance": 50000.00,
      "opening_balance": 45000.00,
      "is_active": true,
      "description": "Main bank account",
      "tax_number": "",
      "vat_type": "Zero-rated",
      "created_at": "2024-01-15T10:30:00",
      "updated_at": "2024-01-15T10:30:00"
    },
    "transactions": [
      {
        "id": 70001,
        "date": "2024-03-10",
        "description": "Invoice INV-2024-0001",
        "debit": 0,
        "credit": 1150.00,
        "balance": 48850.00
      },
      {
        "id": 70002,
        "date": "2024-03-15",
        "description": "Payment from customer",
        "debit": 500.00,
        "credit": 0,
        "balance": 49350.00
      }
    ]
  }
}
```

---

## Create Account

Create a new account in the chart of accounts.

**Endpoint:** `POST https://api.quick.myquickapp.com/accounting/coa`

### Request Body

```json
{
  "org_id": "12345",
  "userid": "123",
  "action": "create",
  "code": "1010",
  "name": "Savings Account",
  "type": "Asset",
  "subtype": "Current Asset",
  "category": "Bank",
  "currency": "ZAR",
  "parent_id": null,
  "description": "Savings bank account",
  "is_active": true
}
```

### Response

```json
{
  "statusCode": 200,
  "headers": {...},
  "body": {
    "message": "Account created successfully",
    "account": {
      "id": "acc_102",
      "code": "1010",
      "name": "Savings Account",
      "type": "Asset",
      "subtype": "Current Asset",
      "category": "Bank",
      "currency": "ZAR",
      "balance": 0.00,
      "opening_balance": 0.00,
      "description": "Savings bank account",
      "is_active": true,
      "created_at": "2024-03-10T12:00:00"
    }
  }
}
```

---

## Edit Account

Update an existing account.

### Request Body

```json
{
  "org_id": "12345",
  "userid": "123",
  "action": "edit",
  "account_id": "acc_102",
  "code": "1010",
  "name": "Savings Account - USD",
  "type": "Asset",
  "subtype": "Current Asset",
  "category": "Bank",
  "currency": "USD",
  "description": "USD savings account",
  "is_active": true
}
```

### Response

```json
{
  "statusCode": 200,
  "headers": {...},
  "body": {
    "message": "Account updated successfully",
    "account": {
      "id": "acc_102",
      "code": "1010",
      "name": "Savings Account - USD",
      "type": "Asset",
      "subtype": "Current Asset",
      "category": "Bank",
      "currency": "USD",
      "balance": 0.00,
      "description": "USD savings account",
      "is_active": true,
      "updated_at": "2024-03-10T12:30:00"
    }
  }
}
```

---

## Delete Account

Delete or deactivate an account.

### Request Body - Soft Delete

```json
{
  "org_id": "12345",
  "userid": "123",
  "action": "delete",
  "account_id": "acc_102",
  "deactivate": true
}
```

### Response

```json
{
  "statusCode": 200,
  "headers": {...},
  "body": {
    "message": "Account deactivated successfully"
  }
}
```

---

## Chart of Accounts Types

| Type | Description | Subtypes |
|------|-------------|----------|
| `Asset` | Assets (what you own) | Current Asset, Fixed Asset, Non-current Asset |
| `Liability` | Liabilities (what you owe) | Current Liability, Non-current Liability |
| `Income` | Income/Revenue | Operating Income, Other Income |
| `Expense` | Expenses | Operating Expense, Non-operating Expense |

---

## Account Categories

| Category | Accounts |
|----------|----------|
| `Bank` | Bank accounts, savings accounts |
| `Receivable` | Accounts receivable, customer accounts |
| `Payable` | Accounts payable, vendor accounts |
| `Sales` | Sales revenue, returns |
| `COGS` | Cost of goods sold, direct costs |
| `Operating Expense` | Rent, utilities, salaries |
| `Non-operating Expense` | Interest, losses |
| `Equity` | Owner's equity, retained earnings |

---

## Search Accounts

### Search by Code

```json
{
  "org_id": "12345",
  "search": "1000"
}
```

### Search by Name

```json
{
  "org_id": "12345",
  "search": "Bank"
}
```

---

## Account Balances

### Get Account Balance History

```json
{
  "org_id": "12345",
  "account_id": "acc_101",
  "date_from": "2024-01-01",
  "date_to": "2024-12-31"
}
```

### Response

```json
{
  "statusCode": 200,
  "headers": {...},
  "body": {
    "account": {
      "id": "acc_101",
      "code": "1000",
      "name": "Bank Account"
    },
    "opening_balance": 45000.00,
    "closing_balance": 50000.00,
    "total_debits": 15000.00,
    "total_credits": 10000.00,
    "monthly_balances": [
      {
        "month": "2024-01",
        "opening": 45000.00,
        "closing": 46000.00
      },
      {
        "month": "2024-02",
        "opening": 46000.00,
        "closing": 47000.00
      }
    ]
  }
}
```

---

## Trial Balance

### Get Trial Balance

```json
{
  "org_id": "12345",
  "date": "2024-03-31"
}
```

### Response

```json
{
  "statusCode": 200,
  "headers": {...},
  "body": {
    "trial_balance": [
      {
        "account_id": "acc_101",
        "code": "1000",
        "name": "Bank Account",
        "debit": 50000.00,
        "credit": 0
      },
      {
        "account_id": "acc_201",
        "code": "2000",
        "name": "Accounts Payable",
        "debit": 0,
        "credit": 10000.00
      },
      {
        "account_id": "acc_301",
        "code": "3000",
        "name": "Sales Revenue",
        "debit": 0,
        "credit": 100000.00
      },
      {
        "account_id": "acc_401",
        "code": "4000",
        "name": "Cost of Goods Sold",
        "debit": 40000.00,
        "credit": 0
      }
    ],
    "total_debits": 90000.00,
    "total_credits": 110000.00
  }
}
```
