Skip to content

Order Model

Comprehensive order management with multi-tenant support, prescription handling, and full audit trail.

Overview

The Order model manages the complete order lifecycle from creation to delivery, including prescription requirements, payment processing, and shipping tracking. It supports multi-tenant operations with carrier-specific data isolation.

Attributes

AttributeTypeDescription
idintPrimary key
uuidstringUnique identifier for external references
order_numberstringHuman-readable order number (e.g., ORD20241201ABCD)
user_idintForeign key to User
member_idintForeign key to Member
carrier_idintForeign key to Carrier
statusstringOrder status (pending, processing, shipped, delivered, cancelled, refunded)
typestringOrder type (standard, prescription, etc.)
shipping_addressstringShipping street address
shipping_citystringShipping city
shipping_statestringShipping state/province
shipping_zip_codestringShipping postal code
shipping_countrystringShipping country
shipping_notesstringAdditional shipping instructions
billing_addressstringBilling street address
billing_citystringBilling city
billing_statestringBilling state/province
billing_zip_codestringBilling postal code
billing_countrystringBilling country
subtotalintSubtotal in cents
tax_amountintTax amount in cents
shipping_amountintShipping cost in cents
discount_amountintDiscount amount in cents
total_amountintTotal amount in cents
currencystringCurrency code (USD, etc.)
payment_methodstringPayment method used
payment_statusstringPayment status (pending, paid, failed, refunded)
transaction_idstringExternal payment transaction ID
shipping_methodstringShipping method used
tracking_numberstringShipping tracking number
shipped_atdatetimeWhen order was shipped
delivered_atdatetimeWhen order was delivered
requires_prescriptionbooleanWhether order requires prescription
prescription_statusstringPrescription status (pending, approved, rejected)
prescription_notesstringPrescription-related notes
ordered_atdatetimeWhen order was placed
processed_atdatetimeWhen order was processed
created_attimestampCreation timestamp
updated_attimestampLast update timestamp
deleted_attimestampSoft delete timestamp

Relationships

Belongs To

  • belongsTo(User) - User who placed the order
  • belongsTo(Member) - Member associated with the order
  • belongsTo(Carrier) - Carrier for multi-tenant isolation

Has Many

  • hasMany(OrderItem) - Items in the order

Key Features

Multi-Tenancy

  • Belongs to a specific carrier
  • Carrier-specific data isolation
  • Multi-tenant order management

Order Lifecycle Management

  • Comprehensive status tracking
  • Prescription approval workflow
  • Payment processing integration
  • Shipping and delivery tracking

Financial Management

  • Detailed pricing breakdown
  • Tax calculation support
  • Discount handling
  • Currency support

Audit Trail

  • Full activity logging
  • Change tracking for critical fields
  • Compliance and reporting support

Prescription Support

  • Prescription requirement tracking
  • Approval workflow management
  • Prescription status monitoring

Usage Examples

Creating an Order

php
$order = Order::create([
    'user_id' => $user->id,
    'code' => $member->id,
    'carrier_id' => $carrier->id,
    'status' => 'pending',
    'type' => 'standard',
    'shipping_address' => '123 Main St',
    'shipping_city' => 'Anytown',
    'shipping_state' => 'CA',
    'shipping_zip_code' => '12345',
    'shipping_country' => 'US',
    'billing_address' => '123 Main St',
    'billing_city' => 'Anytown',
    'billing_state' => 'CA',
    'billing_zip_code' => '12345',
    'billing_country' => 'US',
    'subtotal' => 5000, // $50.00
    'tax_amount' => 500, // $5.00
    'shipping_amount' => 1000, // $10.00
    'total_amount' => 6500, // $65.00
    'currency' => 'USD',
    'payment_method' => 'credit_card',
    'payment_status' => 'pending',
    'requires_prescription' => false,
    'ordered_at' => now(),
]);

Getting Associated Data

php
// Get user who placed the order
$user = $order->user;

// Get member details
$member = $order->member;

// Get carrier information
$carrier = $order->carrier;

// Get order items
$items = $order->items;

// Get total number of items
$totalItems = $order->total_items;

Order Status Management

php
// Check order status
if ($order->is_pending) {
    // Handle pending order
}

if ($order->is_paid) {
    // Process paid order
}

if ($order->is_shipped) {
    // Handle shipped order
}

// Update order status
$order->markAsShipped('TRK123456789');
$order->markAsDelivered();
$order->markAsPaid('TXN123456789');
$order->markAsCancelled();

Prescription Management

php
// Check prescription requirements
if ($order->needs_prescription_approval) {
    // Handle prescription approval
}

// Update prescription status
$order->markPrescriptionApproved();
$order->markPrescriptionRejected('Invalid prescription');

Financial Operations

php
// Get formatted amounts
$subtotal = $order->subtotal_formatted; // "$50.00"
$tax = $order->tax_amount_formatted; // "$5.00"
$shipping = $order->shipping_amount_formatted; // "$10.00"
$total = $order->total_amount_formatted; // "$65.00"

// Update totals
$order->updateTotals();

Scopes

Status Scopes

php
// Get orders by status
Order::pending()->get();
Order::processing()->get();
Order::shipped()->get();
Order::delivered()->get();
Order::cancelled()->get();
Order::paid()->get();

Prescription Scopes

php
// Get orders requiring prescription
Order::requiresPrescription()->get();
Order::prescriptionApproved()->get();
Order::prescriptionPending()->get();

User/Carrier Scopes

php
// Get orders for specific user
Order::forUser($userId)->get();

// Get orders for specific member
Order::forMember($memberId)->get();

// Get orders for specific carrier
Order::forCarrier($carrierId)->get();

Accessors

Customer Information

php
$customerEmail = $order->customer_email; // From member->user->email
$customerName = $order->customer_name; // From member->user->name
$customerPhone = $order->customer_phone; // From member->phone

Status Checks

php
$order->is_pending; // bool
$order->is_processing; // bool
$order->is_shipped; // bool
$order->is_delivered; // bool
$order->is_cancelled; // bool
$order->is_refunded; // bool
$order->is_paid; // bool

Prescription Status

php
$order->needs_prescription_approval; // bool
$order->is_prescription_approved; // bool
$order->is_prescription_rejected; // bool

Formatted Amounts

php
$order->subtotal_formatted; // "$50.00"
$order->tax_amount_formatted; // "$5.00"
$order->shipping_amount_formatted; // "$10.00"
$order->discount_amount_formatted; // "$0.00"
$order->total_amount_formatted; // "$65.00"

Order Number Generation

Orders automatically generate unique order numbers in the format: ORD{YYYYMMDD}{RANDOM}

php
// Example order numbers
// ORD20241201ABCD
// ORD20241201EFGH
// ORD20241201IJKL
  • User - User who placed the order
  • Member - Member associated with the order
  • Carrier - Multi-tenant organization
  • OrderItem - Items in the order