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

# Invoice generation

> How invoices are automatically generated from subscriptions.

TRXN automatically generates invoices from active subscriptions through the `GenerateInvoicesJob`. This ensures that recurring charges are billed on time without manual intervention.

## How it works

<Steps>
  <Step title="Scheduled execution">
    The `GenerateInvoicesJob` runs on a regular schedule, typically daily. It evaluates all subscriptions to determine which ones are due for billing.
  </Step>

  <Step title="Subscription processing">
    The job iterates through all subscriptions and checks billing eligibility based on the subscription's next billing date and the current time.
  </Step>

  <Step title="Active phase check">
    Only subscriptions with an active phase containing recurring items are processed. If a subscription has no active phase or the active phase contains only one-time items, it is skipped.
  </Step>

  <Step title="Due date calculation">
    The job compares each subscription's `next_billing_date` with the current time. If the billing date has arrived or passed, the subscription is eligible for invoice generation.
  </Step>

  <Step title="Invoice creation">
    For each eligible subscription, the job generates an invoice with line items corresponding to the recurring phase items in the active phase.
  </Step>
</Steps>

## Invoice structure

When an invoice is generated, it contains:

* **Invoice level** -- customer, account, and subscription references
* **Line items** -- one line item per subscription phase item
* **Pricing** -- uses overridden prices when set, otherwise falls back to the base product price
* **Quantities** -- reflects the quantity specified in each phase item

## Example

```
Subscription: John's Enterprise Plan
Active Phase: "Regular Pricing" (contains 3 recurring items)
Next Billing Date: 2024-01-15
Current Date: 2024-01-15

Generated Invoice:
├── Line Item 1: Base Platform Access -- $199.00 (qty: 1)
├── Line Item 2: Additional User Seats -- $125.00 (qty: 5 x $25)
└── Line Item 3: Premium Support -- $50.00 (qty: 1)

Total: $374.00
```

## Duplicate prevention

The invoice generation system includes safeguards to prevent duplicate invoices. If an invoice has already been generated for a billing period, the job will not create another one for the same period.

## Billing cycle anchors

Invoice generation respects [billing cycle anchors](/guides/billing-cycles). When a subscription has an anchor set, the `next_billing_date` calculation aligns with the anchor date rather than the subscription creation date.

## Initial invoices

When a subscription is first created, the system may generate an initial invoice depending on the [proration behavior](/guides/billing-cycles):

| Proration behavior  | Initial invoice                                                                   |
| ------------------- | --------------------------------------------------------------------------------- |
| `create_prorations` | Prorated charge for the partial period between creation and the billing anchor    |
| `always_invoice`    | Immediate full-price invoice                                                      |
| `none`              | No initial invoice if an anchor is set; immediate full-price invoice if no anchor |

## Related pages

<CardGroup cols={2}>
  <Card title="Subscriptions" href="/guides/subscriptions">
    Multi-phase subscriptions with flexible pricing and automatic transitions.
  </Card>

  <Card title="Billing cycles" href="/guides/billing-cycles">
    Control when recurring charges occur with billing cycle anchors.
  </Card>
</CardGroup>
