Order
extends Model
in package
Order model for customer takeout orders.
Tags
Table of Contents
Properties
- $db : PDO
- $fillable : array<string|int, mixed>
- $primaryKey : string
- $table : string
Methods
- __construct() : mixed
- Initialize model with database connection.
- all() : array<string|int, mixed>
- Get all records from the table.
- count() : int
- Count records matching optional conditions.
- countForBusiness() : int
- Count orders for a business, optionally by status.
- create() : int
- Create a new record.
- delete() : bool
- Delete a record by primary key.
- find() : array<string|int, mixed>|null
- Find a record by primary key.
- findBy() : array<string|int, mixed>|null
- Find a record by a specific field value.
- generateOrderNumber() : string
- Generate a unique order number (BEO-XXXXXX).
- getAverageOrderValue() : float
- Get average order value for a business.
- getByOrderNumber() : array<string|int, mixed>|null
- Find an order by its order number.
- getByStatus() : array<string|int, mixed>
- Get orders by status for a business.
- getCustomersForBusiness() : array<string|int, mixed>
- Get unique customers who have ordered from a business.
- getDb() : PDO
- Get the underlying PDO connection.
- getForBusiness() : array<string|int, mixed>
- Get orders for a business, optionally filtered by status.
- getForBusinessByDateRange() : array<string|int, mixed>
- Get orders for a business within a date range, with optional status filter.
- getForCustomer() : array<string|int, mixed>
- Get orders for a customer.
- getOrderCountByHour() : array<string|int, mixed>
- Get order counts grouped by hour of day.
- getPaymentMethodBreakdown() : array<string|int, mixed>
- Get payment method breakdown for a business.
- getPopularItems() : array<string|int, mixed>
- Get popular items for a business ordered by quantity sold.
- getRecentForBusiness() : array<string|int, mixed>
- Get recent orders for a business (last 5).
- getRevenueByDateRange() : float
- Get revenue for a business within a date range.
- getRevenueByMonth() : array<string|int, mixed>
- Get revenue grouped by month for a business.
- getRevenueByWeek() : array<string|int, mixed>
- Get revenue grouped by week for a business.
- getTodayRevenue() : float
- Get today's revenue for a business.
- getTopCustomers() : array<string|int, mixed>
- Get top customers by spend for a business.
- getWithItems() : array<string|int, mixed>|null
- Get an order with its items.
- getWithItemsByDateRange() : array<string|int, mixed>
- Get orders with their line items for a business within a date range.
- paginate() : array<string|int, mixed>
- Paginate records matching conditions.
- query() : PDOStatement
- Execute a raw SQL query with parameter binding.
- search() : array<string|int, mixed>
- Search orders by order number or customer name.
- update() : bool
- Update a record by primary key.
- updateStatus() : bool
- Update the status of an order.
- where() : array<string|int, mixed>
- Find records matching conditions.
- filterFillable() : array<string|int, mixed>
- Filter data to only include fillable fields.
Properties
$db
protected
PDO
$db
Database connection
$fillable
protected
array<string|int, mixed>
$fillable
= ['business_id', 'customer_id', 'table_id', 'is_dine_in', 'order_number', 'status', 'subtotal', 'tax_amount', 'tip_amount', 'total', 'payment_method', 'payment_status', 'payment_transaction_id', 'pickup_time', 'pickup_type', 'special_instructions', 'utensil_selections', 'coupon_id', 'coupon_code', 'discount_amount', 'customer_name', 'customer_phone', 'customer_email']
Mass-assignable fields
$primaryKey
protected
string
$primaryKey
= 'id'
Primary key column
$table
protected
string
$table
= 'orders'
Database table name
Methods
__construct()
Initialize model with database connection.
public
__construct() : mixed
all()
Get all records from the table.
public
all([string $orderBy = 'id' ][, string $direction = 'ASC' ]) : array<string|int, mixed>
Parameters
- $orderBy : string = 'id'
-
Column to order by
- $direction : string = 'ASC'
-
Sort direction (ASC or DESC)
Return values
array<string|int, mixed>count()
Count records matching optional conditions.
public
count([array<string|int, mixed> $conditions = [] ]) : int
Parameters
- $conditions : array<string|int, mixed> = []
-
Associative array of field => value pairs
Return values
intcountForBusiness()
Count orders for a business, optionally by status.
public
countForBusiness(int $businessId[, string|null $status = null ]) : int
Parameters
- $businessId : int
-
Business ID
- $status : string|null = null
-
Optional status filter
Return values
intcreate()
Create a new record.
public
create(array<string|int, mixed> $data) : int
Parameters
- $data : array<string|int, mixed>
-
Associative array of field => value pairs
Return values
int —The ID of the newly created record
delete()
Delete a record by primary key.
public
delete(int $id) : bool
Parameters
- $id : int
-
Primary key value
Return values
boolfind()
Find a record by primary key.
public
find(int $id) : array<string|int, mixed>|null
Parameters
- $id : int
-
Primary key value
Return values
array<string|int, mixed>|nullfindBy()
Find a record by a specific field value.
public
findBy(string $field, mixed $value) : array<string|int, mixed>|null
Parameters
- $field : string
-
Column name
- $value : mixed
-
Value to match
Return values
array<string|int, mixed>|nullgenerateOrderNumber()
Generate a unique order number (BEO-XXXXXX).
public
generateOrderNumber() : string
Return values
stringgetAverageOrderValue()
Get average order value for a business.
public
getAverageOrderValue(int $businessId[, string $startDate = null ][, string $endDate = null ]) : float
Parameters
- $businessId : int
-
Business ID
- $startDate : string = null
-
Optional start date
- $endDate : string = null
-
Optional end date
Return values
floatgetByOrderNumber()
Find an order by its order number.
public
getByOrderNumber(string $orderNumber) : array<string|int, mixed>|null
Parameters
- $orderNumber : string
-
Order number (e.g., BEO-ABC123)
Return values
array<string|int, mixed>|nullgetByStatus()
Get orders by status for a business.
public
getByStatus(int $businessId, string $status) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
- $status : string
-
Order status
Return values
array<string|int, mixed>getCustomersForBusiness()
Get unique customers who have ordered from a business.
public
getCustomersForBusiness(int $businessId[, int $limit = 100 ]) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
- $limit : int = 100
-
Max results
Return values
array<string|int, mixed>getDb()
Get the underlying PDO connection.
public
getDb() : PDO
Return values
PDOgetForBusiness()
Get orders for a business, optionally filtered by status.
public
getForBusiness(int $businessId[, string|null $status = null ][, int $limit = 50 ][, int $offset = 0 ]) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
- $status : string|null = null
-
Optional status filter
- $limit : int = 50
-
Max results
- $offset : int = 0
Return values
array<string|int, mixed>getForBusinessByDateRange()
Get orders for a business within a date range, with optional status filter.
public
getForBusinessByDateRange(int $businessId, string $startDate, string $endDate[, string|null $status = null ][, bool $includeCancelled = false ][, int $limit = 50000 ][, int $offset = 0 ]) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
- $startDate : string
-
Start date (Y-m-d)
- $endDate : string
-
End date (Y-m-d)
- $status : string|null = null
-
Optional status filter
- $includeCancelled : bool = false
-
Whether to include cancelled orders
- $limit : int = 50000
-
Max results
- $offset : int = 0
-
Offset for pagination
Return values
array<string|int, mixed>getForCustomer()
Get orders for a customer.
public
getForCustomer(int $customerId[, int $limit = 50 ][, int|null $businessId = null ]) : array<string|int, mixed>
Parameters
- $customerId : int
-
Customer ID
- $limit : int = 50
-
Max results
- $businessId : int|null = null
Return values
array<string|int, mixed>getOrderCountByHour()
Get order counts grouped by hour of day.
public
getOrderCountByHour(int $businessId, string $startDate, string $endDate) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
- $startDate : string
-
Start date
- $endDate : string
-
End date
Return values
array<string|int, mixed>getPaymentMethodBreakdown()
Get payment method breakdown for a business.
public
getPaymentMethodBreakdown(int $businessId[, string $startDate = null ][, string $endDate = null ]) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
- $startDate : string = null
-
Optional start date
- $endDate : string = null
-
Optional end date
Return values
array<string|int, mixed>getPopularItems()
Get popular items for a business ordered by quantity sold.
public
getPopularItems(int $businessId[, int $limit = 10 ][, string $startDate = null ][, string $endDate = null ]) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
- $limit : int = 10
-
Max items
- $startDate : string = null
-
Optional start date
- $endDate : string = null
-
Optional end date
Return values
array<string|int, mixed>getRecentForBusiness()
Get recent orders for a business (last 5).
public
getRecentForBusiness(int $businessId[, int $limit = 5 ]) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
- $limit : int = 5
-
Number of orders
Return values
array<string|int, mixed>getRevenueByDateRange()
Get revenue for a business within a date range.
public
getRevenueByDateRange(int $businessId, string $startDate, string $endDate) : float
Parameters
- $businessId : int
-
Business ID
- $startDate : string
-
Start date (Y-m-d)
- $endDate : string
-
End date (Y-m-d)
Return values
floatgetRevenueByMonth()
Get revenue grouped by month for a business.
public
getRevenueByMonth(int $businessId[, int $months = 12 ]) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
- $months : int = 12
-
Number of months back
Return values
array<string|int, mixed>getRevenueByWeek()
Get revenue grouped by week for a business.
public
getRevenueByWeek(int $businessId[, int $weeks = 12 ]) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
- $weeks : int = 12
-
Number of weeks back
Return values
array<string|int, mixed>getTodayRevenue()
Get today's revenue for a business.
public
getTodayRevenue(int $businessId) : float
Parameters
- $businessId : int
-
Business ID
Return values
floatgetTopCustomers()
Get top customers by spend for a business.
public
getTopCustomers(int $businessId[, int $limit = 10 ]) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
- $limit : int = 10
-
Max results
Return values
array<string|int, mixed>getWithItems()
Get an order with its items.
public
getWithItems(int $id) : array<string|int, mixed>|null
Parameters
- $id : int
-
Order ID
Return values
array<string|int, mixed>|nullgetWithItemsByDateRange()
Get orders with their line items for a business within a date range.
public
getWithItemsByDateRange(int $businessId, string $startDate, string $endDate[, bool $includeCancelled = false ]) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
- $startDate : string
-
Start date (Y-m-d)
- $endDate : string
-
End date (Y-m-d)
- $includeCancelled : bool = false
-
Whether to include cancelled orders
Return values
array<string|int, mixed> —Orders with 'items' key containing line items
paginate()
Paginate records matching conditions.
public
paginate(array<string|int, mixed> $conditions, int $page, int $perPage[, string $orderBy = 'id' ][, string $direction = 'ASC' ]) : array<string|int, mixed>
Parameters
- $conditions : array<string|int, mixed>
-
Associative array of field => value pairs
- $page : int
-
Current page number (1-based)
- $perPage : int
-
Records per page
- $orderBy : string = 'id'
-
Column to order by
- $direction : string = 'ASC'
-
Sort direction (ASC or DESC)
Return values
array<string|int, mixed> —Pagination result with data, total, page, per_page, total_pages
query()
Execute a raw SQL query with parameter binding.
public
query(string $sql[, array<string|int, mixed> $params = [] ]) : PDOStatement
Parameters
- $sql : string
-
SQL query string
- $params : array<string|int, mixed> = []
-
Bound parameters
Return values
PDOStatementsearch()
Search orders by order number or customer name.
public
search(int $businessId, string $query) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
- $query : string
-
Search query
Return values
array<string|int, mixed>update()
Update a record by primary key.
public
update(int $id, array<string|int, mixed> $data) : bool
Parameters
- $id : int
-
Primary key value
- $data : array<string|int, mixed>
-
Associative array of field => value pairs
Return values
boolupdateStatus()
Update the status of an order.
public
updateStatus(int $id, string $status) : bool
Parameters
- $id : int
-
Order ID
- $status : string
-
New status
Return values
boolwhere()
Find records matching conditions.
public
where(array<string|int, mixed> $conditions[, string $orderBy = 'id' ][, string $direction = 'ASC' ]) : array<string|int, mixed>
Parameters
- $conditions : array<string|int, mixed>
-
Associative array of field => value pairs
- $orderBy : string = 'id'
-
Column to order by
- $direction : string = 'ASC'
-
Sort direction
Return values
array<string|int, mixed>filterFillable()
Filter data to only include fillable fields.
protected
filterFillable(array<string|int, mixed> $data) : array<string|int, mixed>
Parameters
- $data : array<string|int, mixed>
-
Input data
Return values
array<string|int, mixed> —Filtered data