uncloud/doc/README-billing.org

86 lines
3.3 KiB
Org Mode
Raw Normal View History

2020-06-20 20:15:15 +00:00
* How to handle billing in general
2020-08-01 12:05:12 +00:00
** Manual test flow / setting up bills
- Needs orders
-
2020-06-20 20:15:15 +00:00
** Orders
- Orders are the heart of uncloud billing
- Have a starting date
- Have an ending date
2020-06-20 21:47:26 +00:00
- Orders are immutable
- Can usually not be cancelled / cancellation is not a refund
- Customer/user commits on a certain period -> gets discount
based on it
- Can be upgraded
- Create a new order
- We link the new order to the old order and say this one
replaces it
- If the price of the new order is HIGHER than the OLD order,
then we charge the difference until the end of the order period
- In the next billing run we set the OLD order to not to bill anymore
- And only the NEW order will be billed afterwards
- Can be downgraded in the next period (but not for this period)
- We create a new order, same as for upgrade
- The new order starts directly after the OLD order
- As the amount is LOWER than the OLD order, no additional charge is done
during this order period
- We might need to have an activate datetime
- When to implement this
- Order periods can be
2020-06-20 20:15:15 +00:00
*** Statuses
- CREATING/PREPARING
- INACTIVE (?)
2020-06-20 21:47:26 +00:00
- TO_BILL
- NOT_TO_BILL: we use this to accelerate queries to the DB
2020-06-20 20:15:15 +00:00
*** Updating status of orders
- If has succeeding order and billing date is last month -> set inactive
** Bills
- Are always for a month
- Can be preliminary
*** Which orders to include
- Not the cancelled ones / not active ones
** Flows / Approach
*** Finding all orders for a bill
- Get all orders, state != NOT_TO_BILL; for each order do:
- is it a one time order?
- has it a bill assigned?
- yes: set to NOT_TO_BILL
- no:
- get_or_create_bill_for_this_month
- assign bill to this order
- set to NOT_TO_BILL
2020-06-20 21:47:26 +00:00
- is it a recurring order?
- if it has a REPLACING order:
-
2020-06-20 20:15:15 +00:00
- First of month
- Last of month
2020-06-20 21:47:26 +00:00
*** Handling replacement of orders
- The OLD order will appear in the month that it was cancelled on
the bill
- The OLD order needs to be set to NOT_TO_BILL after it was billed
the last time
- The NEW order will be added pro rata if the amount is higher in
the same month
- The NEW order will be used next month
**** Disabling the old order
- On billing run
- If order.replacement_order (naming!) is set
- if the order.replacement_order starts during THIS_MONTH
- add order to bill
- if NOT:
- the order was already replaced in a previous billing period
- set the order to NOT_TO_BILL
**** Billing the new order
- If order.previous_order
*** Handling multiple times a recurring order
- For each recurring order check the order.period
- Find out when it was billed last
- lookup latest bill
- Calculate how many times it has been used until 2359, last day
of month
- For preliminary bill: until datetime.now()
- Call the bill_end_datetime
- Getting duration: bill_end_datetime - order.last_billed
- Amount in seconds; duration_in_seconds
- Divide duration_in_seconds by order.period; amount_used:
- If >= 1: add amount_used * order.recurring_amount to bill