Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # Code snippet that illustrates how usage associated a a given subscription would be recorded and invoiced
- # Relies on a catalog containing the following plan xml description
- =begin
- <plan name="server-monthly">
- <product>Server</product>
- <finalPhase type="EVERGREEN">
- <duration>
- <unit>UNLIMITED</unit>
- </duration>
- <recurring>
- <billingPeriod>MONTHLY</billingPeriod>
- <recurringPrice>
- <price>
- <currency>USD</currency>
- <value>0.00</value>
- </price>
- </recurringPrice>
- </recurring>
- <usages>
- <usage name="server-monthly-usage-type-1" billingMode="IN_ARREAR" usageType="CONSUMABLE">
- <billingPeriod>MONTHLY</billingPeriod>
- <tiers>
- <tier>
- <blocks>
- <tieredBlock>
- <unit>server-hourly-type-1</unit>
- <size>1</size>
- <prices>
- <price>
- <currency>USD</currency>
- <value>1.00</value>
- </price>
- </prices>
- <max>100000</max>
- </tieredBlock>
- </blocks>
- </tier>
- </tiers>
- </usage>
- <usage name="server-monthly-usage-type-2" billingMode="IN_ARREAR" usageType="CONSUMABLE">
- <billingPeriod>MONTHLY</billingPeriod>
- <tiers>
- <tier>
- <blocks>
- <tieredBlock>
- <unit>server-hourly-type-2</unit>
- <size>1</size>
- <prices>
- <price>
- <currency>USD</currency>
- <value>2.00</value>
- </price>
- </prices>
- <max>100000</max>
- </tieredBlock>
- </blocks>
- </tier>
- </tiers>
- </usage>
- <usage name="server-monthly-usage-type-3" billingMode="IN_ARREAR" usageType="CONSUMABLE">
- <billingPeriod>MONTHLY</billingPeriod>
- <tiers>
- <tier>
- <blocks>
- <tieredBlock>
- <unit>server-hourly-type-3</unit>
- <size>1</size>
- <prices>
- <price>
- <currency>USD</currency>
- <value>3.00</value>
- </price>
- </prices>
- <max>100000</max>
- </tieredBlock>
- </blocks>
- </tier>
- </tiers>
- </usage>
- </usages>
- </finalPhase>
- </plan>
- =end
- NB_USAGE_TYPES = 3
- MAX_SERVERS_PER_TYPE = 10
- USAGE_MIN = 0
- USAGE_MAX = 24 * MAX_SERVERS_PER_TYPE
- def test_demo_with_multiple_units
- #
- # Step 1. Create a subscription associated with the account `@account` (or Project in Packet's terminology)
- #
- bp = create_entitlement_base(@account.account_id, 'Server', 'MONTHLY', 'DEFAULT', @user, @options)
- # Test waits synchronously until the first invoice was generated ($0 invoice since no usage was recorded yet)
- wait_for_expected_clause(1, @account, @options, &@proc_account_invoices_nb)
- # Step 2. Record usage: For each usage type, we generate random usage value per day, each point between [USAGE_MIN, USAGE_MAX)
- usage_input = []
- expected_dollar_amount_total = 0
- expected_dollar_amount_per_unit = {}
- NB_USAGE_TYPES.times do |i|
- raw_usage = generate_random_usage_values(8, 2013, USAGE_MIN, USAGE_MAX)
- expected_usage_unit_amount = raw_usage.inject(0) { |sum, e| sum += e }
- # Price for unit 1 is $1, price for unit 2 is $2, ...
- expected_dollar_amount_per_unit[i] = expected_usage_unit_amount * (i + 1)
- expected_dollar_amount_total += expected_dollar_amount_per_unit[i]
- usage_input << {:unit_type => "server-hourly-type-#{i + 1}",
- :usage_records => generate_usage_for_each_day(8, 2013, raw_usage)
- }
- end
- #
- # Make the call to record the usage (POST /1.0/kb/usages).
- #
- # We make one bulk call to record all the points for all usage types in the month, but we could
- # also make one call per day, and/or one call per unit type per day...
- #
- record_usage(bp.subscription_id, usage_input, @user, @options)
- #
- # Step 3. Move the clock to the beginning of the next month '2013-09-01' to trigger the first invoice with usage items
- #
- kb_clock_add_days(31, nil, @options)
- wait_for_expected_clause(2, @account, @options, &@proc_account_invoices_nb)
- #
- # Verify we see an invoice with:
- # * 3 usage items (one for each type) for the previous month (in arrear usage billing)
- # * 1 recurring item of $0 for the next month
- #
- all_invoices = @account.invoices(true, @options)
- sort_invoices!(all_invoices)
- assert_equal(2, all_invoices.size)
- last_invoice = all_invoices[1]
- check_invoice_no_balance(last_invoice, expected_dollar_amount_total, 'USD', '2013-09-01')
- check_invoice_item(last_invoice.items[0], last_invoice.invoice_id, 0.0, 'USD', 'RECURRING', 'server-monthly', 'server-monthly-evergreen', '2013-09-01', '2013-10-01')
- check_usage_invoice_item(last_invoice.items[1], last_invoice.invoice_id, expected_dollar_amount_per_unit[2], 'USD', 'USAGE', 'server-monthly', 'server-monthly-evergreen', 'server-monthly-usage-type-3', '2013-08-01', '2013-09-01')
- check_usage_invoice_item(last_invoice.items[2], last_invoice.invoice_id, expected_dollar_amount_per_unit[1], 'USD', 'USAGE', 'server-monthly', 'server-monthly-evergreen', 'server-monthly-usage-type-2', '2013-08-01', '2013-09-01')
- check_usage_invoice_item(last_invoice.items[3], last_invoice.invoice_id, expected_dollar_amount_per_unit[0], 'USD', 'USAGE', 'server-monthly', 'server-monthly-evergreen', 'server-monthly-usage-type-1', '2013-08-01', '2013-09-01')
- end
- #
- # Helper methods to generate random usage items
- #
- private
- COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
- def days_in_month(month, year)
- return 29 if month == 2 && Date.gregorian_leap?(year)
- COMMON_YEAR_DAYS_IN_MONTH[month]
- end
- def generate_random_usage_values(month, year, min_included, max_excluded)
- res = []
- nb_values = days_in_month(month, year)
- nb_values.times { |i| res << rand(min_included...max_excluded) }
- res
- end
- def generate_usage_for_each_day(month, year, raw_usage)
- res= [];
- raw_usage.inject(1) do |day, e|
- new_value = {}
- new_value[:record_date] = day < 10 ? "#{year.to_s}-#{month.to_s}-0#{day}" : "#{year.to_s}-#{month.to_s}-#{day}"
- new_value[:amount] = e
- res << new_value
- day +=1
- end
- res
- end
- #
- # Details of the invoice_items entries:
- #
- =begin
- mysql> select * from invoice_items where invoice_id = '2d20b330-b0af-48af-825f-e60c5fe926c2' and type = 'USAGE' \G
- *************************** 1. row ***************************
- record_id: 24
- id: 8de8a211-7f12-48f7-9d19-b0cf23f7db5d
- type: USAGE
- invoice_id: 2d20b330-b0af-48af-825f-e60c5fe926c2
- account_id: e6699b04-deb6-4ed4-9b62-12d11baf6806
- bundle_id: c20f6d96-d5ec-424f-92cd-fd352e28760c
- subscription_id: 0684ad09-42c7-4799-b066-433d9c0d3695
- description: server-monthly-usage-type-3 (usage item)
- plan_name: server-monthly
- phase_name: server-monthly-evergreen
- usage_name: server-monthly-usage-type-3
- start_date: 2013-08-01
- end_date: 2013-09-01
- amount: 12414.000000000
- rate: NULL
- currency: USD
- linked_item_id: NULL
- created_by: Next Billing Date
- created_date: 2013-09-01 06:00:21
- account_record_id: 6
- tenant_record_id: 6
- *************************** 2. row ***************************
- record_id: 25
- id: e62aa51f-72dc-45f1-bc5b-a7e544b7e74c
- type: USAGE
- invoice_id: 2d20b330-b0af-48af-825f-e60c5fe926c2
- account_id: e6699b04-deb6-4ed4-9b62-12d11baf6806
- bundle_id: c20f6d96-d5ec-424f-92cd-fd352e28760c
- subscription_id: 0684ad09-42c7-4799-b066-433d9c0d3695
- description: server-monthly-usage-type-2 (usage item)
- plan_name: server-monthly
- phase_name: server-monthly-evergreen
- usage_name: server-monthly-usage-type-2
- start_date: 2013-08-01
- end_date: 2013-09-01
- amount: 7812.000000000
- rate: NULL
- currency: USD
- linked_item_id: NULL
- created_by: Next Billing Date
- created_date: 2013-09-01 06:00:21
- account_record_id: 6
- tenant_record_id: 6
- *************************** 3. row ***************************
- record_id: 26
- id: c7ebbaec-1708-4ba2-9358-bc6b186fb23a
- type: USAGE
- invoice_id: 2d20b330-b0af-48af-825f-e60c5fe926c2
- account_id: e6699b04-deb6-4ed4-9b62-12d11baf6806
- bundle_id: c20f6d96-d5ec-424f-92cd-fd352e28760c
- subscription_id: 0684ad09-42c7-4799-b066-433d9c0d3695
- description: server-monthly-usage-type-1 (usage item)
- plan_name: server-monthly
- phase_name: server-monthly-evergreen
- usage_name: server-monthly-usage-type-1
- start_date: 2013-08-01
- end_date: 2013-09-01
- amount: 3069.000000000
- rate: NULL
- currency: USD
- linked_item_id: NULL
- created_by: Next Billing Date
- created_date: 2013-09-01 06:00:21
- account_record_id: 6
- tenant_record_id: 6
- =end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement