PhoenixKit.Modules.Billing.Order (phoenix_kit v1.7.71)

Copy Markdown View Source

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 order
  • billing_profile_uuid: Foreign key to the billing profile used
  • order_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/discount
  • tax_amount: Calculated tax amount
  • tax_rate: Applied tax rate (0.20 = 20%)
  • discount_amount: Discount applied
  • discount_code: Coupon/referral code used
  • total: Final amount to be paid

Snapshots & Notes

  • billing_snapshot: Copy of billing profile at order time
  • notes: Customer-visible notes
  • internal_notes: Admin-only notes

Status Workflow

draft  pending  confirmed  paid
                        
             cancelled   refunded

Line 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 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

calculate_totals(line_items, tax_rate \\ Decimal.new("0"), discount \\ Decimal.new("0"))

Calculates totals from line items.

Returns {subtotal, tax_amount, total} as Decimals.

calculate_totals_for_country(line_items, country_code, discount \\ Decimal.new("0"))

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"

cancellable?(arg1)

Checks if order can be cancelled.

changeset(order, attrs)

Creates a changeset for order creation.

editable?(arg1)

Checks if order can be edited (is in draft or pending status).

get_country_tax_rate(country_code)

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>

payable?(arg1)

Checks if order can be marked as paid.

status_changeset(order, new_status)

Changeset for status transitions.

status_color(arg1)

Returns status badge color class.

status_label(arg1)

Returns human-readable status label.