# GET /v2/redis-accounting/documents/account

# Get Documents by Account

این اندپوینت لیست **اسناد حسابداری مربوط به یک کد معین** را از Redis بازیابی می‌کند. خروجی شامل اسناد، مانده فعلی، تعداد اسناد، و در صورت فعال بودن، ماندهٔ تجمیعی از ابتدای سال است. عملیات بر اساس ایندکس Redis `accounting:docs:account:{code}` انجام می‌شود.

<div class="api-docs" id="bkmrk-"></div>## Request Overview

<div class="api-docs" id="bkmrk-url%3A-%2Fv2%2Fredis-accou"><div class="endpoint-info"><div>**URL:** `/v2/redis-accounting/documents/account`</div><div>**Method:** <span class="method-get">GET</span></div><div>**Auth:** Bearer Token (Required)</div><div>**Content-Type:** application/json</div></div></div>## Query Parameters Schema

<div class="api-docs" id="bkmrk-field-type-required-"><table class="schema-table"><thead><tr><th>Field</th><th>Type</th><th>Required</th><th>Description</th></tr></thead><tbody><tr><td>`code`</td><td>integer</td><td>yes</td><td>کد معین که اسناد براساس آن فیلتر می‌شوند</td></tr><tr><td>`from_year_start`</td><td>boolean</td><td>optional</td><td>اگر true باشد، مانده تجمیعی از ابتدای سال شمسی محاسبه می‌شود</td></tr><tr><td>`to_date`</td><td>string (YYYY-MM-DD)</td><td>optional</td><td>تاریخ پایانی برای محاسبه year\_start\_balance (پیش‌فرض: تاریخ امروز)</td></tr></tbody></table>

</div>## Request Example

```
GET /v2/redis-accounting/documents/account?code=1011&from_year_start=true
  
```

<div class="api-docs" id="bkmrk--1"></div>## Response (Success)

ساختار خروجی:

<div class="api-docs" id="bkmrk-field-type-descripti"><table class="schema-table"><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td>`success`</td><td>boolean</td><td>وضعیت نهایی</td></tr><tr><td>`data.documents`</td><td>array</td><td>خروجی **دقیقاً شامل \[diffProcess, docs\]** است diffProcess آرایه‌ای از timestamps docs آرایه سندها</td></tr><tr><td>`data.balance`</td><td>object</td><td>مانده فعلی Redis برای حساب معین</td></tr><tr><td>`data.total_documents`</td><td>integer</td><td>توجه: **در سیستم فعلی مقدار آن ۲ است** چون count روی آرایه دوبخشی \[diffProcess, docs\] اعمال شده (این یک نقص منطقی در پیاده‌سازی است)</td></tr><tr><td>`data.year_start_balance`</td><td>object</td><td>فقط اگر from\_year\_start=true ارسال می‌شود</td></tr></tbody></table>

</div>```
{
  "success": true,
  "data": {
    "documents": [
      [
        [1733301120.101, 1733301120.103, ...],
        [
          {
            "id": "2210",
            "date": "1403-05-01",
            "debit": "500000",
            "credit": "0",
            "group": "10",
            "general": "101",
            "account": "1011",
            "subsidiary": "1011001"
          }
        ]
      ],
      "balance": {
        "debit": 500000,
        "credit": 0,
        "remaining": 500000
      },
      "total_documents": 2,
      "year_start_balance": {
        "debit": 500000,
        "credit": 0,
        "remaining": 500000,
        "year_start": "1403-01-01",
        "to_date": "1403-09-15"
      }
    }
  }
}
  
```

<div class="api-docs" id="bkmrk--2"></div>## Response (Validation Error)

```
{
  "success": false,
  "message": "خطا در اعتبارسنجی",
  "errors": {
    "code": [
      "The code field is required."
    ]
  }
}
  
```

<div class="api-docs" id="bkmrk--3"></div>## Response (Server Error)

```
{
  "success": false,
  "message": "خطا در دریافت اسناد: Internal server error..."
}
  
```

<div class="api-docs" id="bkmrk--4"></div>## Internal Redis Architecture

<div class="api-docs" id="bkmrk-key-type-description"><table class="schema-table"><thead><tr><th>Key</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td>`accounting:docs:account:{code}`</td><td>Set</td><td>لیست شناسه اسناد مربوط به این معین</td></tr><tr><td>`accounting:doc:{id}`</td><td>Hash</td><td>اطلاعات کامل سند</td></tr><tr><td>`balance:account:{code}`</td><td>Hash</td><td>مانده حساب در Redis</td></tr><tr><td>`accounting:docs:by_date`</td><td>SortedSet</td><td>ایندکس سراسری تمام اسناد بر اساس تاریخ برای year\_start\_balance</td></tr></tbody></table>

</div>## Process Flow

<div class="api-docs" id="bkmrk-receive-request-%2B-va"><div class="flowchart"><div class="flow-item">Receive Request + Validate Inputs</div><div class="flow-arrow">↓</div><div class="flow-item">Load Document IDs (accounting:docs:account:{code})</div><div class="flow-arrow">↓</div><div class="flow-item">Fetch Every Document Hash (accounting:doc:{id})</div><div class="flow-arrow">↓</div><div class="flow-item">Sort Documents by Date</div><div class="flow-arrow">↓</div><div class="flow-item">Read Current Balance (balance:account:{code})</div><div class="flow-arrow">↓</div><div class="flow-item">If from_year_start=true → compute getBalanceFromYearStart()</div><div class="flow-arrow">↓</div><div class="flow-item">Return Final JSON Response</div></div></div>