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
| Attribute | Type | Description |
|---|---|---|
id | int | Primary key |
uuid | string | Unique identifier for external references |
order_number | string | Human-readable order number (e.g., ORD20241201ABCD) |
user_id | int | Foreign key to User |
member_id | int | Foreign key to Member |
carrier_id | int | Foreign key to Carrier |
status | string | Order status (pending, processing, shipped, delivered, cancelled, refunded) |
type | string | Order type (standard, prescription, etc.) |
shipping_address | string | Shipping street address |
shipping_city | string | Shipping city |
shipping_state | string | Shipping state/province |
shipping_zip_code | string | Shipping postal code |
shipping_country | string | Shipping country |
shipping_notes | string | Additional shipping instructions |
billing_address | string | Billing street address |
billing_city | string | Billing city |
billing_state | string | Billing state/province |
billing_zip_code | string | Billing postal code |
billing_country | string | Billing country |
subtotal | int | Subtotal in cents |
tax_amount | int | Tax amount in cents |
shipping_amount | int | Shipping cost in cents |
discount_amount | int | Discount amount in cents |
total_amount | int | Total amount in cents |
currency | string | Currency code (USD, etc.) |
payment_method | string | Payment method used |
payment_status | string | Payment status (pending, paid, failed, refunded) |
transaction_id | string | External payment transaction ID |
shipping_method | string | Shipping method used |
tracking_number | string | Shipping tracking number |
shipped_at | datetime | When order was shipped |
delivered_at | datetime | When order was delivered |
requires_prescription | boolean | Whether order requires prescription |
prescription_status | string | Prescription status (pending, approved, rejected) |
prescription_notes | string | Prescription-related notes |
ordered_at | datetime | When order was placed |
processed_at | datetime | When order was processed |
created_at | timestamp | Creation timestamp |
updated_at | timestamp | Last update timestamp |
deleted_at | timestamp | Soft delete timestamp |
Relationships
Belongs To
belongsTo(User)- User who placed the orderbelongsTo(Member)- Member associated with the orderbelongsTo(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->phoneStatus 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; // boolPrescription Status
php
$order->needs_prescription_approval; // bool
$order->is_prescription_approved; // bool
$order->is_prescription_rejected; // boolFormatted 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