---
title: "e-conomic Ledger"
description: "Every posted entry and the legs of every draft voucher, VAT split out, with account name, type and chart section."
url: https://www.gatlas.ai/docs/datasets/catalog/economic/ledger
markdown_url: https://www.gatlas.ai/docs/datasets/catalog/economic/ledger.md
---

# e-conomic Ledger

Every posted entry and the legs of every draft voucher, VAT split out, with account name, type and chart section.

Template id: economic/ledger. Added for a company when it connects e-conomic. The dataset is named "{Company} Ledger".

## Questions it answers
- What was posted to an account in a period, and from which voucher?
- Which entries are still drafts?

## Columns
- posting_status: posted (booked) or draft (in a journal, not booked yet).
- leg: account, contra for the opposite side of a draft voucher line, or vat for the VAT split out of a draft leg. Posted entries are always account.
- connection_label: The e-conomic agreement the row comes from; a company can hold several.
- accounting_year: e-conomic accounting year of a posted entry; null on drafts.
- date: Posting date.
- voucher_number: Voucher number.
- entry_number: Entry number of a posted entry; null on drafts.
- entry_type: e-conomic entry type, e.g. financeVoucher, customerInvoice, supplierInvoice, openingEntry.
- is_opening: True on openingEntry and transferredOpeningEntry rows, which restate a balance account at the start of a year.
- account_number: Account the amount posts to. On the account leg of a supplier or customer draft, the group's payable or receivable account.
- account_name: Account name from the chart.
- account_type: Account type from the chart: profitAndLoss or status for posting accounts.
- chart_section: Nearest heading at or above the account in the company's chart.
- counter_account_number: On a draft account or contra leg, the account on the line's other side; on a vat leg, the account the VAT was split from.
- narrative: Entry text.
- amount: Amount in the entry's currency, signed: positive is debit.
- amount_base: Amount in the company's base currency, signed: positive is debit.
- currency: Currency of amount.
- vat_code: VAT code on this leg, if any.
- department_number: Department, if the entry carries one.
- journal_number: Journal a draft sits in; null on posted entries.
- journal_name: Name of that journal.
- supplier_number: Supplier on a draft voucher line, on its account leg only.
- customer_number: Customer on a draft voucher line, on its account leg only.
- supplier_invoice_number: Supplier invoice number on a draft, on its account leg only.
- data_as_of: When this row was last synced.

## AI context

__ENTITY__'s general ledger from e-conomic: one row per posted entry, and one row per leg of each draft voucher still in a journal.
posting_status = 'posted' is the booked ledger; 'draft' rows are not booked yet. For booked figures filter posting_status = 'posted'.
A draft voucher line is a double entry, shown as the legs e-conomic will book: leg 'account' carries amount on account_number, leg 'contra' carries the opposite amount on the contra account, and leg 'vat' carries VAT split out of either. A side with a VAT code keeps its net amount and its VAT moves to the code's VAT account; a reverse charge code adds equal and opposite VAT on the code's two VAT accounts. So drafts sum correctly per account, cash and P&L included, and each voucher nets to zero. A draft that is already posted is left out.
On a supplier or customer draft the account leg sits on the group's payable or receivable account. supplier_number, customer_number and supplier_invoice_number are on that leg only, so what is owed on a draft bill is the sum of its account legs. A draft line entered with only one side (often just the bank) has one leg, so its voucher does not balance until it is completed in e-conomic.
amount_base is in the company's base currency; amount is in the entry's currency. Sum amount_base, not amount, across currencies.
NEVER sum a balance-sheet (account_type 'status') account across accounting years: is_opening rows restate the cumulative balance at the start of every year, so a closing balance is the sum within ONE accounting_year (drafts carry no accounting_year; place them by date). P&L accounts carry no opening rows.
Monthly balances per account are ready-made in the Monthly Trial Balance dataset.

## SQL

__SCHEMA__ stands for the company's schema (schema_name in list_companies), and __ENTITY__ for its name.

```sql
WITH accts AS (
  SELECT
    COALESCE(a."_gatlas_connection_label", '') AS label_key,
    a."accountNumber" AS account_number,
    a."name" AS account_name,
    a."accountType" AS account_type,
    (SELECT h."name"
       FROM __SCHEMA__.economic_accounts h
      WHERE COALESCE(h."_gatlas_connection_label", '') = COALESCE(a."_gatlas_connection_label", '')
        AND h."accountType" IN ('heading', 'headingStart')
        AND h."accountNumber" <= a."accountNumber"
      ORDER BY h."accountNumber" DESC
      LIMIT 1) AS chart_section
  FROM __SCHEMA__.economic_accounts a
),
posted AS (
  SELECT
    'posted'::text AS posting_status,
    'account'::text AS leg,
    e."_gatlas_connection_label" AS connection_label,
    e."accountingYear" AS accounting_year,
    e."date"::date AS date,
    e."voucherNumber" AS voucher_number,
    e."entryNumber" AS entry_number,
    e."entryType" AS entry_type,
    e."accountNumber" AS account_number,
    e."accountName" AS source_account_name,
    NULL::bigint AS counter_account_number,
    e."text" AS narrative,
    e."amount" AS amount,
    e."amountInBaseCurrency" AS amount_base,
    e."currency" AS currency,
    NULLIF(btrim(e."vatCode"), '') AS vat_code,
    e."departmentNumber" AS department_number,
    NULL::bigint AS journal_number,
    NULL::text AS journal_name,
    NULL::bigint AS supplier_number,
    NULL::bigint AS customer_number,
    NULL::text AS supplier_invoice_number,
    e."_gatlas_synced_at" AS data_as_of
  FROM __SCHEMA__.economic_accounting_year_entries e
),
-- VAT codes, for splitting a draft leg's VAT out the way e-conomic books it.
vat_codes AS (
  SELECT
    COALESCE(va."_gatlas_connection_label", '') AS label_key,
    btrim(va."vatCode") AS vat_code,
    va."ratePercentage"::numeric AS rate,
    COALESCE(va."reverseCharge", false) AS reverse_charge,
    va."accountNumber" AS vat_account_number,
    va."contraAccountNumber" AS contra_vat_account_number
  FROM __SCHEMA__.economic_vat_accounts va
  WHERE va."ratePercentage" IS NOT NULL AND va."ratePercentage" <> 0
),
vouchers AS (
  SELECT
    v."_gatlas_connection_label" AS connection_label,
    COALESCE(v."_gatlas_connection_label", '') AS label_key,
    v."date"::date AS date,
    v."voucherNumber" AS voucher_number,
    v."entryType" AS entry_type,
    v."accountNumber" AS account_number,
    -- The account side of a supplier or customer line is the supplier's or
    -- customer's balance, which e-conomic books to its group's control account
    -- (the payable or receivable).
    COALESCE(v."accountNumber",
             (NULLIF(spg."account"::text, '')::jsonb ->> 'accountNumber')::bigint,
             (NULLIF(cug."account"::text, '')::jsonb ->> 'accountNumber')::bigint) AS side_account_number,
    v."accountName" AS account_name,
    v."contraAccountNumber" AS contra_account_number,
    v."text" AS narrative,
    v."amount" AS amount,
    v."amountInBaseCurrency" AS amount_base,
    v."currency" AS currency,
    NULLIF(btrim(v."vatCode"), '') AS vat_code,
    NULLIF(btrim(v."contraVatCode"), '') AS contra_vat_code,
    v."departmentNumber" AS department_number,
    v."journalNumber" AS journal_number,
    v."journalName" AS journal_name,
    v."supplierNumber" AS supplier_number,
    v."customerNumber" AS customer_number,
    v."supplierInvoiceNumber" AS supplier_invoice_number,
    v."_gatlas_synced_at" AS data_as_of
  FROM __SCHEMA__.economic_journal_vouchers v
  LEFT JOIN __SCHEMA__.economic_suppliers sp
    ON v."accountNumber" IS NULL
   AND COALESCE(sp."_gatlas_connection_label", '') = COALESCE(v."_gatlas_connection_label", '')
   AND sp."supplierNumber" = v."supplierNumber"
  LEFT JOIN __SCHEMA__.economic_supplier_groups spg
    ON COALESCE(spg."_gatlas_connection_label", '') = COALESCE(sp."_gatlas_connection_label", '')
   AND spg."supplierGroupNumber" = (NULLIF(sp."supplierGroup"::text, '')::jsonb ->> 'supplierGroupNumber')::bigint
  LEFT JOIN __SCHEMA__.economic_customers cu
    ON v."accountNumber" IS NULL
   AND COALESCE(cu."_gatlas_connection_label", '') = COALESCE(v."_gatlas_connection_label", '')
   AND cu."customerNumber" = v."customerNumber"
  LEFT JOIN __SCHEMA__.economic_customer_groups cug
    ON COALESCE(cug."_gatlas_connection_label", '') = COALESCE(cu."_gatlas_connection_label", '')
   AND cug."customerGroupNumber" = (NULLIF(cu."customerGroup"::text, '')::jsonb ->> 'customerGroupNumber')::bigint
),
-- Posted rows that share a voucher number with a draft, built once so the
-- exclusions below are hash anti-joins rather than a scan per draft.
posted_keys AS (
  SELECT DISTINCT COALESCE(p.connection_label, '') AS label_key, p.voucher_number,
         p.date, p.account_number, p.amount
  FROM posted p
  WHERE p.voucher_number IN (SELECT d.voucher_number FROM vouchers d)
),
-- A draft that is already posted stays in the journal until the next sync.
-- The whole voucher goes when either of its legs is found posted.
open_vouchers AS (
  SELECT d.*
  FROM vouchers d
  WHERE NOT EXISTS (
          SELECT 1 FROM posted_keys k
          WHERE k.label_key = d.label_key AND k.voucher_number = d.voucher_number
            AND k.date = d.date AND k.account_number = d.account_number
            AND k.amount = d.amount)
    AND NOT EXISTS (
          SELECT 1 FROM posted_keys k
          WHERE k.label_key = d.label_key AND k.voucher_number = d.voucher_number
            AND k.date = d.date AND k.account_number = d.contra_account_number
            AND k.amount = -d.amount)
),
-- Each draft line's two sides at gross: the account side as booked, and the
-- contra side with the amount negated. The supplier, customer, invoice number
-- and source account name belong to the account side only, so a payable or
-- receivable is read from that leg alone.
draft_sides AS (
  SELECT 'account'::text AS leg, o.label_key, o.connection_label, o.date, o.voucher_number,
    o.entry_type, o.side_account_number AS account_number, o.account_name AS source_account_name,
    o.contra_account_number AS counter_account_number, o.narrative,
    o.amount::numeric AS gross, o.amount_base::numeric AS gross_base, o.currency,
    o.vat_code, o.department_number, o.journal_number, o.journal_name,
    o.supplier_number, o.customer_number, o.supplier_invoice_number, o.data_as_of
  FROM open_vouchers o
  -- A line's account side is a ledger account, or a supplier or customer. A
  -- line with neither has only its contra side: its counterpart is another
  -- line of the same voucher.
  WHERE o.account_number IS NOT NULL
     OR o.supplier_number IS NOT NULL
     OR o.customer_number IS NOT NULL
  UNION ALL
  SELECT 'contra'::text, o.label_key, o.connection_label, o.date, o.voucher_number,
    o.entry_type, o.contra_account_number, NULL::text,
    o.side_account_number, o.narrative,
    -o.amount::numeric, -o.amount_base::numeric, o.currency,
    o.contra_vat_code, o.department_number, o.journal_number, o.journal_name,
    NULL::bigint, NULL::bigint, NULL::text, o.data_as_of
  FROM open_vouchers o
  WHERE o.contra_account_number IS NOT NULL
),
draft_vat AS (
  SELECT s.*, c.rate, c.reverse_charge, c.vat_account_number, c.contra_vat_account_number,
    ROUND(s.gross * c.rate / (100 + c.rate), 2) AS vat,
    ROUND(s.gross_base * c.rate / (100 + c.rate), 2) AS vat_base,
    ROUND(s.gross * c.rate / 100, 2) AS reverse_vat,
    ROUND(s.gross_base * c.rate / 100, 2) AS reverse_vat_base
  FROM draft_sides s
  LEFT JOIN vat_codes c
    ON c.label_key = s.label_key AND c.vat_code = s.vat_code
),
-- A draft side with a VAT code is gross, as e-conomic holds it until it books.
-- Here it is booked the way e-conomic will: an ordinary code keeps the net on
-- the side's account and moves the VAT share to the code's VAT account (leg
-- 'vat'); a reverse charge code leaves the side whole and adds equal and
-- opposite VAT on the code's two VAT accounts. Every voucher's legs still net
-- to zero.
draft_legs AS (
  SELECT 'draft'::text AS posting_status, d.leg,
    d.connection_label, NULL::text AS accounting_year, d.date, d.voucher_number,
    NULL::bigint AS entry_number, d.entry_type, d.account_number,
    d.source_account_name, d.counter_account_number, d.narrative,
    (CASE WHEN d.rate IS NOT NULL AND NOT d.reverse_charge THEN d.gross - d.vat ELSE d.gross END)::double precision AS amount,
    (CASE WHEN d.rate IS NOT NULL AND NOT d.reverse_charge THEN d.gross_base - d.vat_base ELSE d.gross_base END)::double precision AS amount_base,
    d.currency, d.vat_code, d.department_number,
    d.journal_number, d.journal_name, d.supplier_number, d.customer_number,
    d.supplier_invoice_number, d.data_as_of
  FROM draft_vat d
  UNION ALL
  SELECT 'draft'::text, 'vat'::text,
    d.connection_label, NULL::text, d.date, d.voucher_number,
    NULL::bigint, d.entry_type, d.vat_account_number,
    NULL::text, d.account_number, d.narrative,
    d.vat::double precision, d.vat_base::double precision,
    d.currency, d.vat_code, d.department_number,
    d.journal_number, d.journal_name, NULL::bigint, NULL::bigint,
    NULL::text, d.data_as_of
  FROM draft_vat d
  WHERE d.rate IS NOT NULL AND NOT d.reverse_charge
  UNION ALL
  SELECT 'draft'::text, 'vat'::text,
    d.connection_label, NULL::text, d.date, d.voucher_number,
    NULL::bigint, d.entry_type, x.vat_account_number,
    NULL::text, d.account_number, d.narrative,
    (x.sign * d.reverse_vat)::double precision, (x.sign * d.reverse_vat_base)::double precision,
    d.currency, d.vat_code, d.department_number,
    d.journal_number, d.journal_name, NULL::bigint, NULL::bigint,
    NULL::text, d.data_as_of
  FROM draft_vat d
  CROSS JOIN LATERAL (VALUES (d.vat_account_number, 1), (d.contra_vat_account_number, -1)) AS x(vat_account_number, sign)
  WHERE d.rate IS NOT NULL AND d.reverse_charge
),
ledger AS (
  SELECT * FROM posted
  UNION ALL
  SELECT * FROM draft_legs
)
SELECT
  l.posting_status,
  l.leg,
  l.connection_label,
  l.accounting_year,
  l.date,
  l.voucher_number,
  l.entry_number,
  l.entry_type,
  l.entry_type IN ('openingEntry', 'transferredOpeningEntry') AS is_opening,
  l.account_number,
  COALESCE(ac.account_name, l.source_account_name) AS account_name,
  ac.account_type,
  ac.chart_section,
  l.counter_account_number,
  l.narrative,
  l.amount,
  l.amount_base,
  l.currency,
  l.vat_code,
  l.department_number,
  l.journal_number,
  l.journal_name,
  l.supplier_number,
  l.customer_number,
  l.supplier_invoice_number,
  l.data_as_of
FROM ledger l
LEFT JOIN accts ac
  ON ac.label_key = COALESCE(l.connection_label, '')
 AND ac.account_number = l.account_number
```

Link: https://www.gatlas.ai/docs/datasets/catalog/economic/ledger.md
