> ## Documentation Index
> Fetch the complete documentation index at: https://docs.capedigital.co.ke/llms.txt
> Use this file to discover all available pages before exploring further.

# Get Invoice Stats

> Finance dashboard summary — accrual basis (what has been invoiced, not yet collected).

**Sections returned:**

- **pipeline** — count of invoice requests at each lifecycle stage:
  `awaiting_upload` (finance must act), `invoice_uploaded` (sent to client),
  `paid`, `partially_paid`, `unpaid`, `written_off`, `cancelled`, `new_today`.

- **collection** — AR health indicators:
  `collection_rate_pct` (% of requests fully paid),
  `ar_outstanding` (KES owed right now),
  `avg_dso_days` (average days from invoice issue to payment — benchmark 30–60 days),
  `overdue_count` / `overdue_amount` (past due_date and still open),
  `bad_debt_ratio_pct` (written-off / total invoiced — target < 1%).

- **invoiced** — accrual totals for the period and year-to-date.
  Use alongside `GET /payments/stats` (cash basis) to see the gap.

- **aging** — open AR split into four buckets by days outstanding:
  `0_30_days`, `31_60_days`, `61_90_days`, `90_plus_days`.
  Each has `count` (number of invoice requests) and `amount` (KES outstanding).
  Colour-code for the UI: 0–30 green, 31–60 amber, 61–90 red, 90+ critical.

- **by_category** — AR outstanding, total invoiced, and collection rate
  broken down by campaign category (Direct, Agency, Digital, Government).
  Use for a bar chart or table showing which segment is slowest to pay.



## OpenAPI

````yaml /openapi/payment.yaml get /api/v1/invoice-requests/stats
openapi: 3.1.0
info:
  title: Cape Media - Payment Service
  description: >-
    Invoice requests, invoice uploads, payment tracking, and commission
    management.
  version: 1.0.0
servers:
  - url: https://api.capedigital.co.ke/payment
    description: Production
  - url: http://localhost:8000
    description: Local
security: []
paths:
  /api/v1/invoice-requests/stats:
    get:
      tags:
        - Invoices
      summary: Get Invoice Stats
      description: >-
        Finance dashboard summary — accrual basis (what has been invoiced, not
        yet collected).


        **Sections returned:**


        - **pipeline** — count of invoice requests at each lifecycle stage:
          `awaiting_upload` (finance must act), `invoice_uploaded` (sent to client),
          `paid`, `partially_paid`, `unpaid`, `written_off`, `cancelled`, `new_today`.

        - **collection** — AR health indicators:
          `collection_rate_pct` (% of requests fully paid),
          `ar_outstanding` (KES owed right now),
          `avg_dso_days` (average days from invoice issue to payment — benchmark 30–60 days),
          `overdue_count` / `overdue_amount` (past due_date and still open),
          `bad_debt_ratio_pct` (written-off / total invoiced — target < 1%).

        - **invoiced** — accrual totals for the period and year-to-date.
          Use alongside `GET /payments/stats` (cash basis) to see the gap.

        - **aging** — open AR split into four buckets by days outstanding:
          `0_30_days`, `31_60_days`, `61_90_days`, `90_plus_days`.
          Each has `count` (number of invoice requests) and `amount` (KES outstanding).
          Colour-code for the UI: 0–30 green, 31–60 amber, 61–90 red, 90+ critical.

        - **by_category** — AR outstanding, total invoiced, and collection rate
          broken down by campaign category (Direct, Agency, Digital, Government).
          Use for a bar chart or table showing which segment is slowest to pay.
      operationId: get_invoice_stats_api_v1_invoice_requests_stats_get
      parameters:
        - name: period
          in: query
          required: false
          schema:
            type: string
            description: >-
              Reporting window. One of the preset windows below, or **custom**
              (requires `date_from` and `date_to`):

              - **today** — current day only

              - **week** — Monday of the current week to today

              - **mtd** — Month To Date: 1st of this month to today

              - **qtd** — Quarter To Date: 1st of this quarter to today (e.g.
              Apr 1 in Q2)

              - **ytd** — Year To Date: Jan 1 to today

              - **rolling_30** — last 30 calendar days, regardless of month
              boundaries

              - **rolling_60** — last 60 calendar days

              - **rolling_90** — last 90 calendar days (standard treasury
              lookback)

              - **custom** — supply `date_from=YYYY-MM-DD&date_to=YYYY-MM-DD`
            default: mtd
            title: Period
          description: >-
            Reporting window. One of the preset windows below, or **custom**
            (requires `date_from` and `date_to`):

            - **today** — current day only

            - **week** — Monday of the current week to today

            - **mtd** — Month To Date: 1st of this month to today

            - **qtd** — Quarter To Date: 1st of this quarter to today (e.g. Apr
            1 in Q2)

            - **ytd** — Year To Date: Jan 1 to today

            - **rolling_30** — last 30 calendar days, regardless of month
            boundaries

            - **rolling_60** — last 60 calendar days

            - **rolling_90** — last 90 calendar days (standard treasury
            lookback)

            - **custom** — supply `date_from=YYYY-MM-DD&date_to=YYYY-MM-DD`
        - name: date_from
          in: query
          required: false
          schema:
            anyOf:
              - type: string
                format: date
              - type: 'null'
            description: Start date for period='custom' (YYYY-MM-DD).
            title: Date From
          description: Start date for period='custom' (YYYY-MM-DD).
        - name: date_to
          in: query
          required: false
          schema:
            anyOf:
              - type: string
                format: date
              - type: 'null'
            description: End date for period='custom' (YYYY-MM-DD).
            title: Date To
          description: End date for period='custom' (YYYY-MM-DD).
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema: {}
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
      security:
        - HTTPBearer: []
components:
  schemas:
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError
  securitySchemes:
    HTTPBearer:
      type: http
      scheme: bearer

````