Invoice
extends Model
in package
Model for managing billing invoices.
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.
- countByStatus() : int
- Count invoices by status.
- countForBusiness() : int
- Count invoices for a business.
- 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.
- generateInvoiceNumber() : string
- Generate a unique invoice number (INV-YYYYMM-XXXX).
- getAllWithBusiness() : array<string|int, mixed>
- Get all invoices with business info for admin view.
- getDb() : PDO
- Get the underlying PDO connection.
- getForBusiness() : array<string|int, mixed>
- Get invoices for a business.
- getForSubscription() : array<string|int, mixed>
- Get invoices for a subscription.
- getOverdue() : array<string|int, mixed>
- Get overdue invoices.
- getPlatformRevenue() : float
- Get total platform revenue from paid invoices.
- markPaid() : bool
- Mark an invoice as paid.
- paginate() : array<string|int, mixed>
- Paginate records matching conditions.
- query() : PDOStatement
- Execute a raw SQL query with parameter binding.
- update() : bool
- Update a record by primary key.
- 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', 'subscription_id', 'invoice_number', 'amount', 'tax_amount', 'total', 'status', 'stripe_invoice_id', 'due_date', 'paid_at', 'period_start', 'period_end', 'description']
Mass-assignable fields
$primaryKey
protected
string
$primaryKey
= 'id'
Primary key column
$table
protected
string
$table
= 'invoices'
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
intcountByStatus()
Count invoices by status.
public
countByStatus(string $status) : int
Parameters
- $status : string
-
Invoice status
Return values
intcountForBusiness()
Count invoices for a business.
public
countForBusiness(int $businessId) : int
Parameters
- $businessId : int
-
Business ID
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>|nullgenerateInvoiceNumber()
Generate a unique invoice number (INV-YYYYMM-XXXX).
public
generateInvoiceNumber() : string
Return values
stringgetAllWithBusiness()
Get all invoices with business info for admin view.
public
getAllWithBusiness([int $limit = 100 ]) : array<string|int, mixed>
Parameters
- $limit : int = 100
-
Max results
Return values
array<string|int, mixed>getDb()
Get the underlying PDO connection.
public
getDb() : PDO
Return values
PDOgetForBusiness()
Get invoices for a business.
public
getForBusiness(int $businessId[, int $limit = 50 ][, int $offset = 0 ]) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
- $limit : int = 50
-
Max results
- $offset : int = 0
Return values
array<string|int, mixed>getForSubscription()
Get invoices for a subscription.
public
getForSubscription(int $subscriptionId) : array<string|int, mixed>
Parameters
- $subscriptionId : int
-
Subscription ID
Return values
array<string|int, mixed>getOverdue()
Get overdue invoices.
public
getOverdue() : array<string|int, mixed>
Return values
array<string|int, mixed>getPlatformRevenue()
Get total platform revenue from paid invoices.
public
getPlatformRevenue([string|null $startDate = null ][, string|null $endDate = null ]) : float
Parameters
- $startDate : string|null = null
-
Start date (Y-m-d)
- $endDate : string|null = null
-
End date (Y-m-d)
Return values
floatmarkPaid()
Mark an invoice as paid.
public
markPaid(int $id) : bool
Parameters
- $id : int
-
Invoice ID
Return values
boolpaginate()
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
PDOStatementupdate()
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
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