Overview
Simple Invoice uses a MySQL/MariaDB database namedsimple_invoice with 8 main tables. The schema uses MyISAM and InnoDB storage engines.
Tables
users
Stores user accounts with hashed passwords.| Column | Type | Constraints | Description |
|---|---|---|---|
user_id | INT(11) | PRIMARY KEY, AUTO_INCREMENT | Unique user identifier |
firstname | VARCHAR(20) | NOT NULL | User’s first name |
lastname | VARCHAR(20) | NOT NULL | User’s last name |
user_name | VARCHAR(64) | NOT NULL, UNIQUE | Username for login |
user_password_hash | VARCHAR(255) | NOT NULL | Hashed password (using password_hash) |
user_email | VARCHAR(64) | NOT NULL, UNIQUE | User’s email address |
date_added | DATETIME | NOT NULL | Account creation timestamp |
- PRIMARY KEY on
user_id - UNIQUE KEY on
user_name - UNIQUE KEY on
user_email
clientes
Stores client/customer information.| Column | Type | Constraints | Description |
|---|---|---|---|
id_cliente | INT(11) | PRIMARY KEY, AUTO_INCREMENT | Unique client identifier |
nombre_cliente | VARCHAR(255) | NOT NULL, UNIQUE | Client name |
telefono_cliente | CHAR(30) | NOT NULL | Client phone number |
email_cliente | VARCHAR(64) | NOT NULL | Client email address |
direccion_cliente | VARCHAR(255) | NOT NULL | Client physical address |
status_cliente | TINYINT(4) | NOT NULL | Status: 1=Active, 0=Inactive |
date_added | DATETIME | NOT NULL | Record creation timestamp |
- PRIMARY KEY on
id_cliente - UNIQUE KEY on
nombre_cliente
- Referenced by
facturas.id_cliente(foreign key constraint in application logic)
products
Stores product catalog information.| Column | Type | Constraints | Description |
|---|---|---|---|
id_producto | INT(11) | PRIMARY KEY, AUTO_INCREMENT | Unique product identifier |
codigo_producto | CHAR(20) | NOT NULL, UNIQUE | Product code/SKU |
nombre_producto | CHAR(255) | NOT NULL | Product name/description |
status_producto | TINYINT(4) | NOT NULL | Status: 1=Active, 0=Inactive |
date_added | DATETIME | NOT NULL | Record creation timestamp |
precio_producto | DOUBLE | NOT NULL | Product sale price |
- PRIMARY KEY on
id_producto - UNIQUE KEY on
codigo_producto
- Referenced by
detalle_factura.id_producto - Referenced by
tmp.id_producto
facturas
Stores invoice header information.| Column | Type | Constraints | Description |
|---|---|---|---|
id_factura | INT(11) | PRIMARY KEY, AUTO_INCREMENT | Unique invoice identifier |
numero_factura | INT(11) | NOT NULL, UNIQUE | Invoice number (display) |
fecha_factura | DATETIME | NOT NULL | Invoice date |
id_cliente | INT(11) | NOT NULL | Client ID (references clientes) |
id_vendedor | INT(11) | NOT NULL | Vendor/salesperson ID (references users) |
condiciones | VARCHAR(30) | NOT NULL | Payment terms/conditions |
total_venta | VARCHAR(20) | NOT NULL | Total sale amount |
estado_factura | TINYINT(1) | NOT NULL | Status: 1=Paid, 0=Pending |
- PRIMARY KEY on
id_factura - UNIQUE KEY on
numero_factura
- Foreign key to
clientes.id_cliente(application enforced) - Foreign key to
users.user_idviaid_vendedor(application enforced) - Referenced by
detalle_factura.numero_factura
detalle_factura
Stores invoice line items (products on each invoice).| Column | Type | Constraints | Description |
|---|---|---|---|
id_detalle | INT(11) | PRIMARY KEY, AUTO_INCREMENT | Unique line item identifier |
numero_factura | INT(11) | NOT NULL | Invoice number (references facturas) |
id_producto | INT(11) | NOT NULL | Product ID (references products) |
cantidad | INT(11) | NOT NULL | Quantity ordered |
precio_venta | DOUBLE | NOT NULL | Unit price at time of sale |
- PRIMARY KEY on
id_detalle - KEY on (
numero_factura,id_producto)
- Foreign key to
facturas.numero_factura(application enforced) - Foreign key to
products.id_producto(application enforced)
- Line total =
cantidad×precio_venta
perfil
Stores company/business profile settings (singleton table with one row).| Column | Type | Constraints | Description |
|---|---|---|---|
id_perfil | INT(11) | PRIMARY KEY, AUTO_INCREMENT | Profile identifier (always 1) |
nombre_empresa | VARCHAR(150) | NOT NULL | Company name |
direccion | VARCHAR(255) | NOT NULL | Company address |
ciudad | VARCHAR(100) | NOT NULL | City |
codigo_postal | VARCHAR(100) | NOT NULL | Postal/ZIP code |
estado | VARCHAR(100) | NOT NULL | State/province |
telefono | VARCHAR(20) | NOT NULL | Company phone |
email | VARCHAR(64) | NOT NULL | Company email |
impuesto | INT(2) | NOT NULL | Tax/IVA percentage (e.g., 13) |
moneda | VARCHAR(6) | NOT NULL | Currency symbol |
logo_url | VARCHAR(255) | NOT NULL | Path to company logo image |
- PRIMARY KEY on
id_perfil
- Used in invoice PDF generation
- Tax percentage applied to all invoices
- Currency symbol displayed throughout application
tmp
Temporary storage for invoice line items during invoice creation (session-based).| Column | Type | Constraints | Description |
|---|---|---|---|
id_tmp | INT(11) | PRIMARY KEY, AUTO_INCREMENT | Unique temporary item identifier |
id_producto | INT(11) | NOT NULL | Product ID (references products) |
cantidad_tmp | INT(11) | NOT NULL | Quantity |
precio_tmp | DOUBLE(8,2) | NULL | Unit price |
session_id | VARCHAR(100) | NOT NULL | PHP session ID |
- PRIMARY KEY on
id_tmp
- Stores products added to invoice before finalization
- Each user session has its own set of temporary items
- Items are moved to
detalle_facturawhen invoice is finalized - Should be periodically cleaned of old sessions
currencies
Reference table for supported currencies (pre-populated).| Column | Type | Constraints | Description |
|---|---|---|---|
id | INT(10) UNSIGNED | PRIMARY KEY, AUTO_INCREMENT | Currency identifier |
name | VARCHAR(255) | NOT NULL | Currency name |
symbol | VARCHAR(255) | NOT NULL | Currency symbol (HTML encoded) |
precision | VARCHAR(255) | NOT NULL | Decimal precision (e.g., “2”) |
thousand_separator | VARCHAR(255) | NOT NULL | Thousands separator (e.g., ”,“) |
decimal_separator | VARCHAR(255) | NOT NULL | Decimal separator (e.g., ”.”) |
code | VARCHAR(255) | NOT NULL | ISO currency code |
- PRIMARY KEY on
id
Entity Relationship Diagram
SQL Schema Export
The complete schema can be found insimple_invoice.sql in the source code root directory.
Data Integrity Notes
Foreign Key Constraints: The schema uses MyISAM engine for most tables, which does not support foreign key constraints. Data integrity is enforced at the application level through PHP validation.
- Clients with invoices cannot be deleted
- Products with invoice line items cannot be deleted
- User ID 1 (admin) cannot be deleted
- Deleting an invoice also deletes its line items
