# E-Invoices

One row per e-invoice document issued by your teams and operators (the seller side), for example the invoices, credit notes and self-billed invoices Monta sends to the buyer over the e-invoicing network. The line items of each document are in [E-Invoice Lines](/docs/e_invoice_lines).

Credit notes carry negative amounts, as sent to the buyer. Net, VAT and gross amounts are calculated from the document lines. Purged drafts stay in the table with `deleted_at` set (`document_status = 'deleted'`). Drafts and failed documents are included too; read `document_status` to tell the states apart, and filter on `document_status = 'delivered'` for the documents the buyer received.

**SQL:** `e_invoices`

## Schema

| Column Name                     | Data Type     | Description                                                                                                                                                                                                                                                                                                                                                                                            |
|:--------------------------------|:--------------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **Primary ID**                  |               |                                                                                                                                                                                                                                                                                                                                                                                                        |
| id                              | INTEGER       | Primary key, unique identifier of the e-invoice document                                                                                                                                                                                                                                                                                                                                               |
| **Other IDs**                   |               |                                                                                                                                                                                                                                                                                                                                                                                                        |
| invoice_number                  | VARCHAR       | Serial number carried on the transmitted document                                                                                                                                                                                                                                                                                                                                                      |
| corrects_document_id            | INTEGER       | For a credit note, the `id` of the e-invoice document it gives money back against. Null on every other document type. Not available in the Partner API                                                                                                                                                                                                                                                 |
| seller_owner_id                 | INTEGER       | ID of the team or operator that owns the legal entity which issued the document                                                                                                                                                                                                                                                                                                                        |
| seller_wallet_id                | INTEGER       | Wallet the document was issued from, as recorded on the latest submission. Null when the document has no submission                                                                                                                                                                                                                                                                                    |
| buyer_owner_id                  | INTEGER       | ID of the team or operator the document was issued to                                                                                                                                                                                                                                                                                                                                                  |
| buyer_wallet_id                 | INTEGER       | Wallet of the buyer, as recorded on the latest submission. Null when the document has no submission                                                                                                                                                                                                                                                                                                    |
| **Timestamps**                  |               |                                                                                                                                                                                                                                                                                                                                                                                                        |
| created_at                      | TIMESTAMP_NTZ | Timestamp when the document was created                                                                                                                                                                                                                                                                                                                                                                |
| updated_at                      | TIMESTAMP_NTZ | Timestamp when the document was last updated                                                                                                                                                                                                                                                                                                                                                           |
| deleted_at                      | TIMESTAMP_NTZ | Timestamp when the document was soft deleted (a purged draft or failed document, not a cancelled invoice), null if not deleted. The Partner API never returns deleted documents                                                                                                                                                                                                                        |
| period_from                     | TIMESTAMP_NTZ | Start of the period the document covers                                                                                                                                                                                                                                                                                                                                                                |
| period_to                       | TIMESTAMP_NTZ | End of the period the document covers                                                                                                                                                                                                                                                                                                                                                                  |
| submitted_at                    | TIMESTAMP_NTZ | When the document was handed to the e-invoicing network                                                                                                                                                                                                                                                                                                                                                |
| delivered_at                    | TIMESTAMP_NTZ | When the network confirmed delivery to the buyer                                                                                                                                                                                                                                                                                                                                                       |
| payment_lifecycle_status_at     | TIMESTAMP_NTZ | When the buyer reported `payment_lifecycle_status`                                                                                                                                                                                                                                                                                                                                                     |
| **Dates**                       |               |                                                                                                                                                                                                                                                                                                                                                                                                        |
| invoice_date                    | DATE          | Date the document was issued                                                                                                                                                                                                                                                                                                                                                                           |
| due_date                        | DATE          | When payment is due: the end of the invoicing period plus the agreed days to pay. Null when the document asks for nothing. Not available in the Partner API                                                                                                                                                                                                                                            |
| **Seller**                      |               |                                                                                                                                                                                                                                                                                                                                                                                                        |
| seller_owner_type               | VARCHAR       | Type of the seller owner: `TEAM` or `OPERATOR`                                                                                                                                                                                                                                                                                                                                                         |
| seller_company_name             | VARCHAR       | Registered company name of the seller. Null when unknown or the seller is a deleted personal account                                                                                                                                                                                                                                                                                                   |
| seller_country_code             | VARCHAR       | ISO 3166-1 alpha-2 country of the seller. Null when unknown or the seller is a deleted personal account                                                                                                                                                                                                                                                                                                |
| seller_identifiers              | ARRAY         | Tax and registration identifiers of the seller, as an array of objects with `scheme` (Monta scheme name such as `NL_KVK` or `FO_VAT`) and `id` (the value)                                                                                                                                                                                                                                             |
| **Buyer**                       |               |                                                                                                                                                                                                                                                                                                                                                                                                        |
| buyer_owner_type                | VARCHAR       | Type of the buyer owner: `TEAM` or `OPERATOR`                                                                                                                                                                                                                                                                                                                                                          |
| buyer_company_name              | VARCHAR       | Registered company name of the buyer. Null when unknown or the buyer is a deleted personal account                                                                                                                                                                                                                                                                                                     |
| buyer_country_code              | VARCHAR       | ISO 3166-1 alpha-2 country of the buyer. Null when unknown or the buyer is a deleted personal account                                                                                                                                                                                                                                                                                                  |
| buyer_identifiers               | ARRAY         | Tax and registration identifiers of the buyer, same shape as `seller_identifiers`                                                                                                                                                                                                                                                                                                                      |
| **Attributes**                  |               |                                                                                                                                                                                                                                                                                                                                                                                                        |
| document_type                   | VARCHAR       | Type of document: `INVOICE`, `SELF_BILLED_INVOICE` or `CREDIT_NOTE`                                                                                                                                                                                                                                                                                                                                    |
| currency_code                   | VARCHAR       | ISO 4217 currency code of every amount on the document                                                                                                                                                                                                                                                                                                                                                 |
| net_amount                      | NUMBER        | Total excluding VAT, in major units. Sum of the line net amounts; negative on credit notes                                                                                                                                                                                                                                                                                                             |
| vat_amount                      | NUMBER        | Total VAT, in major units. Sum of the line VAT amounts; negative on credit notes                                                                                                                                                                                                                                                                                                                       |
| gross_amount                    | NUMBER        | Total including VAT, in major units. Negative on credit notes                                                                                                                                                                                                                                                                                                                                          |
| payable_amount                  | NUMBER        | Part of the total the receiver still owes against this document. 0 means the whole total was settled through the Monta wallets. Not available in the Partner API                                                                                                                                                                                                                                       |
| note                            | VARCHAR       | Free-text note carried on the document                                                                                                                                                                                                                                                                                                                                                                 |
| **Status**                      |               |                                                                                                                                                                                                                                                                                                                                                                                                        |
| document_status                 | VARCHAR       | One reading of where the document stands: `deleted` when `deleted_at` is set; otherwise the lowercase latest submission status: `delivered` (the only value meaning the buyer received it), `failed` (see `submission_error_details`), `draft`, `processing` or `pending`, or `no_submission` when the document has no live submission. Draft, failed and no_submission documents were never delivered |
| submission_status               | VARCHAR       | Latest non-deleted submission status on the e-invoicing network: `PENDING`, `DRAFT`, `PROCESSING`, `DELIVERED` or `FAILED`. Null when there is none                                                                                                                                                                                                                                                    |
| submission_error_details        | VARCHAR       | Why the submission failed, when `submission_status` is `FAILED`                                                                                                                                                                                                                                                                                                                                        |
| payment_lifecycle_status        | VARCHAR       | Latest payment state the buyer reported back over the e-invoicing network                                                                                                                                                                                                                                                                                                                              |

## Inclusion Criteria

A document is included if its seller is a team or operator within your scope. Documents issued *to* your teams and operators by others (the buyer side) are not included, which matches the Partner API.

Soft-deleted documents are included with `deleted_at` set, and so are drafts and failed documents. Submission fields (`submission_status`, `submitted_at`, `delivered_at`, `submission_error_details`) come from the latest non-deleted submission. The seller and buyer company name, country and identifiers are blank for a deleted personal team, the same rule as in `teams`.

## Data flow

| Upstream tables  | Downstream tables |
|:-----------------|:------------------|
| `teams`          | `e_invoice_lines` |
| `operators`      | ---               |

## Alignment
### Monta API
- Aligns with the Monta API e-invoices endpoint, which returns only the documents the owner issued (seller side)
- The Monta DW table includes additional fields not in the API response: `payable_amount`, `due_date`, `corrects_document_id`, and the soft-deleted documents (`deleted_at`, `document_status = 'deleted'`)
- Payment instructions, bank details and the raw document payload are not exposed

## Joining e-invoices to their lines

```sql
select
    i.invoice_number
    , i.document_type
    , i.invoice_date
    , i.currency_code
    , l.item_name
    , l.quantity
    , l.net_amount
    , l.vat_amount
from e_invoices i
inner join e_invoice_lines l
    on l.e_invoice_document_id = i.id
where i.document_status = 'delivered'
```
