Invoice computation
Subscription Invoice Computation
This document explains how UniBee generates subscription invoices, including data structure, key fields, and computation logic.
Invoice Overview
A subscription invoice is composed of the following components:
- Subtotal: The original subscription amount before discounts and tax.
- Discount: Calculated based on the applied discount code.
- VAT: Computed using the applicable tax percentage.
- Total: The final payable amount after discount and VAT.
Sample Summary
Data Structure
Each invoice includes a list of lines
, representing items billed for the subscription. A typical line
item contains:
Formula Reference
Item-Level Calculations
Each line
item in the invoice follows these computation rules:
⚠️ Note:
item.amount
does not necessarily equalamountExcludingTax + tax
becausediscountAmount
is computed externally and distributed back into the items (e.g., for proration or code-level discounting).
Invoice-Level Aggregations
Outside the items, the system computes total values using the following logic:
Fields Summary
Field | Description |
---|---|
subscriptionAmount | Original total including VAT |
discountAmount | Calculated based on discount code |
taxAmount | Total VAT applied |
totalAmount | Final amount user needs to pay |
lines[n].amount | Final amount of each item after discounts |
lines[n].originAmount | Amount before discount |
lines[n].taxPercentage | Represented in basis points (e.g., 500 = 5%) |
Notes
- VAT is computed at invoice level based on total taxable amount after discounts.
- Proration logic may adjust
discountAmount
and reflect it across individual items. - All amounts are stored in minor units (e.g., cents), and formatted into standard currencies when rendering the PDF.
Proration Explanation
Proration occurs when a customer upgrades, downgrades, starts, or cancels a subscription in the middle of a billing cycle. UniBee calculates prorated amounts to ensure fair billing based on the actual usage time.
When Proration Applies
- Plan changes within a billing cycle
- Early cancellations
- Mid-cycle sign-ups
How Proration Is Calculated
UniBee uses a time-based proration model:
planAmount
: the full amount of the plantotalPeriodDurationInSeconds
: full length of the billing cycle in secondsactualUsageDurationInSeconds
: the portion of time the plan was used
Example
If a €30/month plan is canceled halfway through the month (15/30 days), the prorated charge would be:
This prorated amount may also be subject to discounts and tax, following the standard formula logic.
💡 Prorated discounts (if any) are calculated externally and applied to the
discountAmount
field at the invoice or item level.
Line Item Display Format in Invoice PDF
Each invoice line item is rendered in the PDF with the following format:
This format is derived from the following data structure:
Where:
- Description comes from
pdfDescription
- Unit Price is
unitAmountExcludingTax
(converted from cents) - Quantity is
quantity
- VAT is
tax
- Total is
originAmount
(before discounts)
💡 Note: The discounted amount is shown in the invoice summary, not in this table.
Invoice Summary Format in PDF
The bottom section of the invoice PDF summarizes the overall charges:
This section is computed using the invoice-level fields:
subscriptionAmountExcludingTax
discountAmount
taxAmount
totalAmount