Order schema for PhoenixKit Billing system.
Manages orders with line items, amounts, and billing information. Orders serve as the primary document for tracking what users purchased.
Schema Fields
Identity & Relations
user_uuid: Foreign key to the user who placed the orderbilling_profile_uuid: Foreign key to the billing profile usedorder_number: Unique order identifier (e.g., "ORD-2024-0001")status: Order status workflow
Payment
payment_method: Payment method (Phase 1: "bank" only)currency: ISO 4217 currency code
Line Items
line_items: JSONB array of items purchased
Financial
subtotal: Sum of line items before tax/discounttax_amount: Calculated tax amounttax_rate: Applied tax rate (0.20 = 20%)discount_amount: Discount applieddiscount_code: Coupon/referral code usedtotal: Final amount to be paid
Snapshots & Notes
billing_snapshot: Copy of billing profile at order timenotes: Customer-visible notesinternal_notes: Admin-only notes
Status Workflow
draft → pending → confirmed → paid
↘ ↘
cancelled refundedLine Item Structure
[
{
"name": "Pro Plan - Monthly",
"description": "Professional subscription plan",
"quantity": 1,
"unit_price": "99.00",
"total": "99.00",
"sku": "PLAN-PRO-M"
}
]Usage Examples
# Create an order
{:ok, order} = Billing.create_order(user, %{
billing_profile_uuid: profile.uuid,
currency: "EUR",
line_items: [
%{name: "Pro Plan", quantity: 1, unit_price: "99.00", total: "99.00"}
],
subtotal: "99.00",
total: "99.00"
})
# Confirm order
{:ok, order} = Billing.confirm_order(order)
# Mark as paid
{:ok, order} = Billing.mark_order_paid(order)
Summary
Functions
Calculates totals from line items.
Calculates totals with automatic tax rate from country.
Checks if order can be cancelled.
Creates a changeset for order creation.
Checks if order can be edited (is in draft or pending status).
Gets the standard VAT rate for a country as a Decimal.
Checks if order can be marked as paid.
Changeset for status transitions.
Returns status badge color class.
Returns human-readable status label.
Functions
Calculates totals from line items.
Returns {subtotal, tax_amount, total} as Decimals.
Calculates totals with automatic tax rate from country.
Uses standard VAT rate from BeamLabCountries based on the billing country.
Returns {subtotal, tax_amount, total} as Decimals.
Examples
iex> items = [%{"total" => "100.00"}]
iex> {subtotal, tax, total} = Order.calculate_totals_for_country(items, "EE")
iex> Decimal.to_string(tax)
"20.00"
iex> Decimal.to_string(total)
"120.00"
Checks if order can be cancelled.
Creates a changeset for order creation.
Checks if order can be edited (is in draft or pending status).
Gets the standard VAT rate for a country as a Decimal.
Examples
iex> Order.get_country_tax_rate("EE")
#Decimal<0.20>
iex> Order.get_country_tax_rate("US")
#Decimal<0>
Checks if order can be marked as paid.
Changeset for status transitions.
Returns status badge color class.
Returns human-readable status label.