Skip to main content

Invoices

Invoices represent charges sent to customers. Each invoice contains line items detailing the products and amounts charged.

Invoice Statuses

draft → open → paid
↓ ↓
void uncollectible
StatusDescription
draftDraft. Can be edited, items can be added/removed
openFinalized. Chargeable, can receive payments and have credits applied
paidFully paid
voidVoided. Applied credits are restored, installments canceled
uncollectibleMarked as uncollectible (write-off)

Invoice Fields

Identification

FieldDescription
NumberUnique sequential number (format: INV-YYYY-NNNNNN)
Billing accountCustomer who will receive the invoice
SubscriptionAssociated subscription (optional)
CompanyIssuing company (for multi-company setups)

Amounts (in cents, ÷100 for BRL)

FieldDescription
SubtotalSum of line items before discounts and taxes
DiscountTotal discount applied
TaxesTotal taxes calculated
Total withheldWithheld taxes (PIS/COFINS/IRPJ)
Net amountNet amount after withholdings
TotalFinal amount (subtotal - discount + taxes)
Amount dueTotal amount payable
Amount paidAmount already received
Amount remainingOutstanding balance

Configuration

FieldDescription
Collection methodcharge_automatically, manual_charge, or manual_invoice
Billing reasonsubscription_cycle, subscription_create, subscription_update, or manual
NF-e policydisabled, on_finalization, on_full_payment, or per_installment
Due dateCalculated from the customer's payment term days

Line Items

Each invoice contains line items that detail the charges:

FieldDescription
Typesubscription or invoiceitem
DescriptionDescriptive text
QuantityQuantity charged
Unit amountPrice per unit (in cents)
Total amountQuantity × unit amount
ProrationWhether the item is a prorated charge
PeriodStart and end of the billed period

Operations

Create Invoice

When creating an invoice, provide:

  • Billing account (required)
  • Line items with description, quantity, and amount
  • Due date (or uses the customer's payment term days)
  • Installments (optional, minimum 2, sum must equal the total)
  • NF-e policy (optional)

The invoice number is automatically generated using PostgreSQL advisory locks to prevent duplication.

Finalize (draftopen)

When finalizing an invoice:

  1. Validates that at least 1 line item exists
  2. Validates that the sum of installments = total (if installment-based)
  3. Calculates taxes automatically
  4. Applies credits automatically (FIFO: earliest expiration first, then oldest)
  5. If collection method is charge_automatically: attempts to charge immediately

Mark as Paid

  • Supports partial payments
  • When the full amount is received, the status changes to paid
  • Creates a transaction record

Void

When voiding an invoice:

  • Applied credits are automatically restored
  • Pending installments are canceled
  • The void reason is stored

Undo Payment

Reverts an invoice from paid to open:

  • Resets the paid amount to zero
  • Creates an adjustment transaction (negative amount)
  • Stores the reason in metadata

Mark as Uncollectible

Marks the invoice as a write-off. Irreversible. No automatic payment attempts will be made.

Automatic Number Generation

Invoice numbers are generated sequentially per year using PostgreSQL advisory locks. Format: INV-{year}-{6-digit sequence}.

Example: INV-2026-000147