New Page
1. System Overview
Smart360's reporting system consists of two core modules:
- RX Module: Handles data fetching, processing, and report logic.
- BX Module: Manages templates, PDF generation, and delivery.
2. Report Generation Architecture
2.1 RX Module Components
Component | Purpose |
---|---|
| Fetches data from multiple databases (e.g.,
,
) |
| Abstract class for common report methods (saving, serialization) |
| Converts processed data to JSON for templates |
Key Data Flow:
Query DB → Process Data → Save to Report Model → Pass to BX Module
2.2 BX Module Components
Component | Purpose |
---|---|
| Stores master HTML templates (e.g., RX-REPORT, BILL) |
| Contains modular sections (headers, tables, charts) |
| Renders HTML → PDF using
|
| Fallback API for complex templates using
|
3. Step-by-Step Report Creation
3.1 Data Fetching & Logic (RX Module)
Step 1: Create a Data Fetcher
# rx_module/multi_db_utils.py
class ComplaintDataFetcher:
QUERY = """
SELECT consumer_no, status, created_date
FROM consumer
WHERE remote_utility_id = %(utility_id)s
"""
def fetch(self, utility_id):
return fetch_data_from_table("cx_db", self.QUERY, {"utility_id": utility_id})
Step 2: Implement Report Class
# rx_module/reports/complaint_report.py
class ComplaintReport(BaseReport):
def generate_report(self):
raw_data = ComplaintDataFetcher().fetch(self.remote_utility_id)
processed_data = self._calculate_stats(raw_data) # Custom logic
self.save_report(processed_data) # Saves to Report model
3.2 Template Design (BX Module)
Step 1: Create Templates in Django Admin
- Add a ReceiptsTemplate:
- Template Alias:
complaint_summary
- HTML: Base structure with
{{ other_detail }}
and{{ column_detail }}
placeholders.
- Template Alias:
- Add Components:
other_detail
: Summary stats (KPI cards)column_detail
: Data tables
Sample HTML Template:
<div class="report-header">
<h1>{{ data.report_name }}</h1>
<div>{{ other_detail }}</div>
</div>
<table>{{ column_detail }}</table>
3.3 PDF Generation Flow
RX Module calls BX via:
context = {
"template_alias": "complaint_summary",
"data": {
"other_detail": {"open_cases": 42},
"column_detail": {"rows": [...]}
}
}
AuthWrapper(REPORT_TOKEN).generate_receipt(context)
BX Module processes this through:
GenerateReceiptView → Renders HTML → PDF
4. Scheduling & Distribution
Manual Scheduling (UI)
5. Key APIs Deep Dive
5.1 PDF Generation API (GenerateReceiptView)
Endpoint: POST /api/receipts/generate-receipt/
Flow:
- Fetch template from
ReceiptsTemplate
- Render components (
other_detail
,column_detail
) - Combine into final HTML
- Convert to PDF using
pdfkit
# BX Module
pdf = pdfkit.from_string(
final_html,
False,
options={"page-size": "A4", "encoding": "UTF-8"}
)
response = HttpResponse(pdf, content_type="application/pdf")
5.2 External PDF Service Fallback
If pdfkit
fails:
consumer_obj = AuthWrapper(REPORT_TOKEN).generate_receipt(context)
# Calls external service via:
# POST /api/receipts/generate-receipt/ with HTML payload
6. Troubleshooting Guide
Issue | Debugging Steps |
---|---|
Missing data in PDF | 1. Check ReportSerializer output |
2. Verify template placeholders | |
PDF rendering fails | 1. Validate HTML syntax |
2. Check
permissions | |
Scheduled reports not sent | 1. Inspect Celery logs |
2. Verify SMTP settings |
Best Practices:
- ✔ Test templates with
template_type="RX-REPORT"
- ✔ Log context data before PDF generation
- ✔ Use component-specific CSS (avoid global styles)
No Comments