Authentication
All report endpoints require:- Authentication with a valid session
- Admin role (
role === "admin")
Endpoints
Get Main Reports
Indicates if the request was successful
Monthly income summary
Income breakdown by service type
Patient distribution across branches
Get Debts Report
Indicates if the request was successful
Array of debt records
Report Types
Monthly Income Report
Tracks revenue over time to identify trends and seasonal patterns. Use Cases:- Financial planning and forecasting
- Identifying high and low revenue periods
- Year-over-year comparisons
- Budget vs. actual analysis
Income by Service Report
Breaks down revenue by treatment or service type. Use Cases:- Identify most profitable services
- Resource allocation decisions
- Marketing focus for high-value services
- Service mix optimization
Patients by Branch Report
Shows patient distribution across different clinic locations. Use Cases:- Capacity planning
- Staff allocation
- Marketing targeting by location
- Branch performance comparison
Debts Report
Lists all outstanding patient balances. Use Cases:- Accounts receivable management
- Collection follow-up
- Cash flow forecasting
- Aging analysis
- Credit risk assessment
Common Use Cases
Dashboard Overview
Fetch all main reports for an admin dashboard:Collections Management
Get list of patients with outstanding debts:High-Priority Collections
Identify debts over a certain threshold:Revenue Analysis
Analyze monthly income trends:Data Aggregation
All reports are generated using stored procedures that aggregate data from multiple tables:sp_get_main_reports()- Generates monthly income, service income, and patient distributionsp_get_debts_report()- Calculates outstanding balances and aging
- Performance - Optimized queries with proper indexing
- Consistency - Single source of truth for calculations
- Security - Business logic encapsulated at database level
Error Responses
Performance Considerations
Caching Recommendations
Reports can be expensive to generate. Consider:- Caching report data for 5-15 minutes
- Using a separate caching layer (Redis, Memcached)
- Implementing incremental updates for frequently accessed reports
Pagination
For large datasets (especially debts report), consider:- Client-side pagination for small datasets (less than 1000 records)
- Server-side pagination for larger datasets
- Filtering options to reduce payload size
Related Endpoints
- Patients API - Patient data used in reports
- Appointments API - Appointment data for scheduling reports
- Branches API - Branch data for distribution reports
Source Reference
API implementation can be found in:src/routes/api/reports/+server.js- Main reports endpointsrc/routes/api/reports/debts/+server.js- Debts report endpoint
sp_get_main_reports()- Generates main reportssp_get_debts_report()- Calculates outstanding debts
