PhoenixKit.Modules.Billing.Invoice (phoenix_kit v1.7.42)

Copy Markdown View Source

Invoice schema for PhoenixKit Billing system.

Invoices are generated from orders and sent to customers for payment. They include receipt functionality once payment is confirmed.

Schema Fields

Identity & Relations

  • user_id: Foreign key to the user
  • order_id: Foreign key to the source order (optional)
  • invoice_number: Unique invoice identifier (e.g., "INV-2024-0001")
  • status: Invoice status workflow

Financial

  • subtotal, tax_amount, tax_rate, total: Financial amounts
  • currency: ISO 4217 currency code
  • due_date: Payment due date

Billing Details

  • billing_details: Full snapshot of billing profile
  • line_items: Copy of order line items
  • payment_terms: Payment terms text
  • bank_details: Bank account for payment

Receipt

  • receipt_number: Receipt identifier (generated after payment)
  • receipt_generated_at: When receipt was generated
  • receipt_data: Additional receipt data (PDF URL, etc.)

Status Workflow

draft  sent  paid
            
           overdue  paid
            
            void

Usage Examples

# Generate invoice from order
{:ok, invoice} = Billing.create_invoice_from_order(order)

# Send invoice
{:ok, invoice} = Billing.send_invoice(invoice)

# Mark as paid (generates receipt)
{:ok, invoice} = Billing.mark_invoice_paid(invoice)

# Get receipt
receipt = Billing.get_receipt(invoice)

Summary

Functions

Returns the billing name from billing_details snapshot.

Creates a changeset for invoice creation.

Checks if invoice can be edited.

Creates an invoice from an order.

Checks if invoice is fully paid (paid_amount >= total).

Checks if invoice has any payments (paid_amount > 0).

Checks if invoice has a receipt.

Checks if invoice is overdue.

Changeset for updating paid_amount.

Changeset for marking invoice as paid and generating receipt.

Checks if invoice can be marked as paid.

Returns all unique payment methods used in transactions for this invoice. Requires transactions to be preloaded.

Returns the primary payment method (most used in positive transactions). Useful for display when there are multiple payment methods. Requires transactions to be preloaded.

Checks if invoice can receive a refund (has payments).

Returns the remaining amount to be paid.

Checks if invoice can be resent (already sent, paid, or overdue).

Checks if invoice can be sent (first time - changes status to sent).

Changeset for status transitions.

Returns status badge color class.

Returns human-readable status label.

Checks if invoice can be voided.

Functions

billing_name(arg1)

Returns the billing name from billing_details snapshot.

changeset(invoice, attrs)

Creates a changeset for invoice creation.

editable?(arg1)

Checks if invoice can be edited.

from_order(order, opts \\ [])

Creates an invoice from an order.

fully_paid?(invoice)

Checks if invoice is fully paid (paid_amount >= total).

has_payments?(invoice)

Checks if invoice has any payments (paid_amount > 0).

has_receipt?(invoice)

Checks if invoice has a receipt.

overdue?(invoice)

Checks if invoice is overdue.

payable?(arg1)

Checks if invoice can be marked as paid.

payment_methods(arg1)

Returns all unique payment methods used in transactions for this invoice. Requires transactions to be preloaded.

Examples

iex> Invoice.payment_methods(invoice_with_transactions)
["bank", "stripe"]

iex> Invoice.payment_methods(invoice_without_transactions)
[]

primary_payment_method(arg1)

Returns the primary payment method (most used in positive transactions). Useful for display when there are multiple payment methods. Requires transactions to be preloaded.

Examples

iex> Invoice.primary_payment_method(invoice)
"stripe"

iex> Invoice.primary_payment_method(invoice_without_transactions)
nil

refundable?(invoice)

Checks if invoice can receive a refund (has payments).

remaining_amount(invoice)

Returns the remaining amount to be paid.

resendable?(arg1)

Checks if invoice can be resent (already sent, paid, or overdue).

sendable?(arg1)

Checks if invoice can be sent (first time - changes status to sent).

status_changeset(invoice, new_status)

Changeset for status transitions.

status_color(arg1)

Returns status badge color class.

status_label(arg1)

Returns human-readable status label.

voidable?(arg1)

Checks if invoice can be voided.