Overview
The database includes numerous stored procedures (functions) and triggers to encapsulate business logic, maintain data integrity, and provide reusable query interfaces.Naming Conventions
- Functions: Prefixed with
fn_(e.g.,fn_get_customers) - Triggers: Prefixed with
trg_(e.g.,trg_product_price_change) - Parameters: Prefixed with
p_(e.g.,p_customer_id) - Variables: Prefixed with
v_(e.g.,v_total)
Customer Management Functions
fn_get_customers
fn_get_customers
Retrieves customer list with filtering, pagination, and related data.Features:
- Automatically resolves best email, address, and contact
- Includes default company name (razón social)
- Supports search across multiple fields
- Returns total count for pagination
fn_get_customer_by_id
fn_get_customer_by_id
Retrieves complete customer information in JSONB format.Returns:
fn_list_customers
fn_list_customers
Lists customers with optional filters and pagination.Usage:
fn_upsert_company_name
fn_upsert_company_name
Creates or updates a company name (razón social) for a customer.Features:
- Inserts if
p_company_name_idis NULL - Updates if
p_company_name_idis provided - Automatically sets as default if
p_is_defaultis TRUE
Product Management Functions
fn_generate_product_sku
fn_generate_product_sku
Trigger function that automatically generates SKU codes for new products.Example Output:
- Category “Químicos” with prefix “QUIM” →
QUIM-0001,QUIM-0002, etc. - Category “Accesorios” with prefix “ACC” →
ACC-0001,ACC-0002, etc.
fn_log_product_price_change
fn_log_product_price_change
Trigger function that logs price changes to history table.
fn_get_request_products_total
fn_get_request_products_total
Calculates the total cost of products for a service request.Usage:
Catalog Functions
fn_get_service_types
fn_get_service_types
Returns all service types available in the system.
fn_get_roles
fn_get_roles
Returns all roles in JSONB format.
fn_form_catalogs_editUser
fn_form_catalogs_editUser
Returns all catalog data needed for user edit forms.Returns:
fn_get_edit_client_catalog
fn_get_edit_client_catalog
Returns catalog data for client edit forms including sellers and phones.Returns:
- Departments
- Active sellers (users with role_id = 1)
- Customer types and statuses
- Existing phones and emails for the client (if
p_client_idprovided)
Audit & Logging Functions
fn_get_all_logs
fn_get_all_logs
Retrieves comprehensive system logs in JSONB format.Returns logs for:
service_request_logs- Request data changesservice_request_status_log- Status changesservice_request_state_timeline- SLA timelineservice_order_status_log- Order status changesuser_access_log- Login/logout eventsuser_api_usage- API endpoint usage
log_payment_status_change
log_payment_status_change
Trigger function that automatically logs payment status changes.
Utility Functions
update_payment_updated_at
update_payment_updated_at
Trigger function to automatically update Applied to:
updated_at timestamp.paymentstable- Similar pattern used across other tables
fn_sync_telefono_boleta
fn_sync_telefono_boleta
Legacy trigger for synchronizing phone numbers between related tables.
Trigger Assignments
Product Triggers
Payment Triggers
Best Practices
Security
Some functions use
SECURITY DEFINER to run with elevated privileges. Use this carefully and validate all inputs.Performance
Functions that return large datasets support pagination parameters (
p_limit, p_offset) to prevent memory issues.Error Handling
Functions validate inputs and return NULL or empty results rather than throwing errors for invalid IDs.
JSONB Returns
Functions returning JSONB are convenient for API endpoints but may need parsing in application code.
Next Steps
Table Schemas
View detailed table structures
Migrations
Learn about database migrations