Billing & Invoices
1. Real-Time Billing Flow
When each call ends, Wholesale automatically:
Call Ends
│
▼
Calculate Charge (sell price)
│
├── Write to wholesale_cdrs
├── Deduct tenant balance (SQL: balance = balance - price)
├── Report to circuit breaker (success/failure)
├── Record sliding window event (ASR/ACD)
└── Report Prometheus metrics
1.1 CDR Fields
| Field | Description |
|---|---|
| call_id | SIP Call-ID |
| tenant_id | Tenant ID |
| trunk_id | Trunk used |
| caller / callee | Caller / Callee |
| call_start / call_end | Start/end time |
| ring_duration | Ring duration |
| talk_duration | Talk duration |
| billable_duration | Billable duration |
| sell_rate / sell_price | Sell rate / sell price |
| buy_rate / buy_price | Buy rate / buy price |
| profit | Profit |
| sip_code | Final SIP status code |
| answered | Whether answered |
2. CDR Query
Wholesale → CDR Management:
Supported filters:
- Time range
- Tenant
- Trunk
- Caller / Callee
- Answered / Unanswered
- SIP status code
2.1 CDR Export
| Method | Description |
|---|---|
| Sync Export | Instant CSV download (small datasets) |
| Async Export | Background task, generates gzip CSV (large datasets) |
Async export flow:
- Create an export task (
wholesale_export_taskstable) - Background worker streams gzip CSV generation
- Download from the Export Tasks list when complete
2.2 CDR Recalculation (Rebill)
CDR Management → Rebill: Recalculate historical CDRs after modifying rates.
- Filter by time range and tenant
- Recalculate using current rate decks
- Update CDRs and tenant balances
3. Billing Cycles
3.1 Configuration
Tenant Details → Billing Settings:
| Field | Description |
|---|---|
| Cycle Type | Weekly / Monthly |
| Settlement Date | Day of month / day of week |
| Auto Archive | Whether to auto-archive settled CDRs |
3.2 Invoice Generation
A background task checks hourly:
- Finds billing cycles due for settlement
- Aggregates CDRs within the cycle
- Generates
wholesale_billsrecords:- Total amount, call count, ASR, total duration
- Bill status: draft → confirmed → paid
3.3 Invoice Operations
| Operation | Description |
|---|---|
| View | Invoice details (aggregated by trunk/date) |
| Generate HTML invoice page | |
| Download | Export CSV |
| Confirm | Mark as confirmed |
| Delete | Delete draft invoices |
4. Top-Up & Balance Management
Tenant Details → Top Up:
- Amount
- Payment proof image (uploaded to local server or S3)
- System logs to
rustpbx_recharge_logs
Tenant Details → Generate Invoice:
- Manually trigger invoice generation for the current period
5. Database Requirements
Wholesale billing involves significant concurrent writes. Database requirements:
| Item | MySQL | PostgreSQL |
|---|---|---|
| Transaction Isolation | READ COMMITTED | READ COMMITTED |
| Connection Pool | 10-20 connections per node | Same |
| Indexes | Auto-created by migrations | Same |
| CDR Partitioning | Recommended monthly (manual) | Recommended monthly (manual) |
CDR Data Growth Estimate:
- 1 CDR per call ≈ 1KB
- 100K calls/day ≈ 100MB/day ≈ 3GB/month
- Recommend monthly partitioning or periodic archiving for the
wholesale_cdrstable