Advertisement
Guest User

Untitled

a guest
May 29th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.27 KB | None | 0 0
  1. #
  2. # Code snippet that illustrates how usage associated a a given subscription would be recorded and invoiced
  3. # Relies on a catalog containing the following plan xml description
  4.  
  5. =begin
  6. <plan name="server-monthly">
  7. <product>Server</product>
  8. <finalPhase type="EVERGREEN">
  9. <duration>
  10. <unit>UNLIMITED</unit>
  11. </duration>
  12. <recurring>
  13. <billingPeriod>MONTHLY</billingPeriod>
  14. <recurringPrice>
  15. <price>
  16. <currency>USD</currency>
  17. <value>0.00</value>
  18. </price>
  19. </recurringPrice>
  20. </recurring>
  21. <usages>
  22. <usage name="server-monthly-usage-type-1" billingMode="IN_ARREAR" usageType="CONSUMABLE">
  23. <billingPeriod>MONTHLY</billingPeriod>
  24. <tiers>
  25. <tier>
  26. <blocks>
  27. <tieredBlock>
  28. <unit>server-hourly-type-1</unit>
  29. <size>1</size>
  30. <prices>
  31. <price>
  32. <currency>USD</currency>
  33. <value>1.00</value>
  34. </price>
  35. </prices>
  36. <max>100000</max>
  37. </tieredBlock>
  38. </blocks>
  39. </tier>
  40. </tiers>
  41. </usage>
  42.  
  43. <usage name="server-monthly-usage-type-2" billingMode="IN_ARREAR" usageType="CONSUMABLE">
  44. <billingPeriod>MONTHLY</billingPeriod>
  45. <tiers>
  46. <tier>
  47. <blocks>
  48. <tieredBlock>
  49. <unit>server-hourly-type-2</unit>
  50. <size>1</size>
  51. <prices>
  52. <price>
  53. <currency>USD</currency>
  54. <value>2.00</value>
  55. </price>
  56. </prices>
  57. <max>100000</max>
  58. </tieredBlock>
  59. </blocks>
  60. </tier>
  61. </tiers>
  62. </usage>
  63.  
  64. <usage name="server-monthly-usage-type-3" billingMode="IN_ARREAR" usageType="CONSUMABLE">
  65. <billingPeriod>MONTHLY</billingPeriod>
  66. <tiers>
  67. <tier>
  68. <blocks>
  69. <tieredBlock>
  70. <unit>server-hourly-type-3</unit>
  71. <size>1</size>
  72. <prices>
  73. <price>
  74. <currency>USD</currency>
  75. <value>3.00</value>
  76. </price>
  77. </prices>
  78. <max>100000</max>
  79. </tieredBlock>
  80. </blocks>
  81. </tier>
  82. </tiers>
  83. </usage>
  84. </usages>
  85. </finalPhase>
  86. </plan>
  87. =end
  88.  
  89. NB_USAGE_TYPES = 3
  90. MAX_SERVERS_PER_TYPE = 10
  91. USAGE_MIN = 0
  92. USAGE_MAX = 24 * MAX_SERVERS_PER_TYPE
  93.  
  94.  
  95. def test_demo_with_multiple_units
  96.  
  97.  
  98. #
  99. # Step 1. Create a subscription associated with the account `@account` (or Project in Packet's terminology)
  100. #
  101. bp = create_entitlement_base(@account.account_id, 'Server', 'MONTHLY', 'DEFAULT', @user, @options)
  102. # Test waits synchronously until the first invoice was generated ($0 invoice since no usage was recorded yet)
  103. wait_for_expected_clause(1, @account, @options, &@proc_account_invoices_nb)
  104.  
  105.  
  106. # Step 2. Record usage: For each usage type, we generate random usage value per day, each point between [USAGE_MIN, USAGE_MAX)
  107. usage_input = []
  108. expected_dollar_amount_total = 0
  109. expected_dollar_amount_per_unit = {}
  110. NB_USAGE_TYPES.times do |i|
  111.  
  112. raw_usage = generate_random_usage_values(8, 2013, USAGE_MIN, USAGE_MAX)
  113. expected_usage_unit_amount = raw_usage.inject(0) { |sum, e| sum += e }
  114. # Price for unit 1 is $1, price for unit 2 is $2, ...
  115. expected_dollar_amount_per_unit[i] = expected_usage_unit_amount * (i + 1)
  116.  
  117. expected_dollar_amount_total += expected_dollar_amount_per_unit[i]
  118.  
  119. usage_input << {:unit_type => "server-hourly-type-#{i + 1}",
  120. :usage_records => generate_usage_for_each_day(8, 2013, raw_usage)
  121. }
  122.  
  123. end
  124. #
  125. # Make the call to record the usage (POST /1.0/kb/usages).
  126. #
  127. # We make one bulk call to record all the points for all usage types in the month, but we could
  128. # also make one call per day, and/or one call per unit type per day...
  129. #
  130. record_usage(bp.subscription_id, usage_input, @user, @options)
  131.  
  132.  
  133. #
  134. # Step 3. Move the clock to the beginning of the next month '2013-09-01' to trigger the first invoice with usage items
  135. #
  136. kb_clock_add_days(31, nil, @options)
  137. wait_for_expected_clause(2, @account, @options, &@proc_account_invoices_nb)
  138.  
  139. #
  140. # Verify we see an invoice with:
  141. # * 3 usage items (one for each type) for the previous month (in arrear usage billing)
  142. # * 1 recurring item of $0 for the next month
  143. #
  144. all_invoices = @account.invoices(true, @options)
  145. sort_invoices!(all_invoices)
  146. assert_equal(2, all_invoices.size)
  147. last_invoice = all_invoices[1]
  148. check_invoice_no_balance(last_invoice, expected_dollar_amount_total, 'USD', '2013-09-01')
  149. 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')
  150.  
  151. 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')
  152. 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')
  153. 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')
  154.  
  155. end
  156.  
  157.  
  158. #
  159. # Helper methods to generate random usage items
  160. #
  161. private
  162.  
  163. COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  164.  
  165. def days_in_month(month, year)
  166. return 29 if month == 2 && Date.gregorian_leap?(year)
  167. COMMON_YEAR_DAYS_IN_MONTH[month]
  168. end
  169.  
  170. def generate_random_usage_values(month, year, min_included, max_excluded)
  171. res = []
  172. nb_values = days_in_month(month, year)
  173. nb_values.times { |i| res << rand(min_included...max_excluded) }
  174. res
  175. end
  176.  
  177. def generate_usage_for_each_day(month, year, raw_usage)
  178. res= [];
  179. raw_usage.inject(1) do |day, e|
  180. new_value = {}
  181. new_value[:record_date] = day < 10 ? "#{year.to_s}-#{month.to_s}-0#{day}" : "#{year.to_s}-#{month.to_s}-#{day}"
  182. new_value[:amount] = e
  183. res << new_value
  184. day +=1
  185. end
  186. res
  187. end
  188.  
  189.  
  190. #
  191. # Details of the invoice_items entries:
  192. #
  193. =begin
  194. mysql> select * from invoice_items where invoice_id = '2d20b330-b0af-48af-825f-e60c5fe926c2' and type = 'USAGE' \G
  195. *************************** 1. row ***************************
  196. record_id: 24
  197. id: 8de8a211-7f12-48f7-9d19-b0cf23f7db5d
  198. type: USAGE
  199. invoice_id: 2d20b330-b0af-48af-825f-e60c5fe926c2
  200. account_id: e6699b04-deb6-4ed4-9b62-12d11baf6806
  201. bundle_id: c20f6d96-d5ec-424f-92cd-fd352e28760c
  202. subscription_id: 0684ad09-42c7-4799-b066-433d9c0d3695
  203. description: server-monthly-usage-type-3 (usage item)
  204. plan_name: server-monthly
  205. phase_name: server-monthly-evergreen
  206. usage_name: server-monthly-usage-type-3
  207. start_date: 2013-08-01
  208. end_date: 2013-09-01
  209. amount: 12414.000000000
  210. rate: NULL
  211. currency: USD
  212. linked_item_id: NULL
  213. created_by: Next Billing Date
  214. created_date: 2013-09-01 06:00:21
  215. account_record_id: 6
  216. tenant_record_id: 6
  217. *************************** 2. row ***************************
  218. record_id: 25
  219. id: e62aa51f-72dc-45f1-bc5b-a7e544b7e74c
  220. type: USAGE
  221. invoice_id: 2d20b330-b0af-48af-825f-e60c5fe926c2
  222. account_id: e6699b04-deb6-4ed4-9b62-12d11baf6806
  223. bundle_id: c20f6d96-d5ec-424f-92cd-fd352e28760c
  224. subscription_id: 0684ad09-42c7-4799-b066-433d9c0d3695
  225. description: server-monthly-usage-type-2 (usage item)
  226. plan_name: server-monthly
  227. phase_name: server-monthly-evergreen
  228. usage_name: server-monthly-usage-type-2
  229. start_date: 2013-08-01
  230. end_date: 2013-09-01
  231. amount: 7812.000000000
  232. rate: NULL
  233. currency: USD
  234. linked_item_id: NULL
  235. created_by: Next Billing Date
  236. created_date: 2013-09-01 06:00:21
  237. account_record_id: 6
  238. tenant_record_id: 6
  239. *************************** 3. row ***************************
  240. record_id: 26
  241. id: c7ebbaec-1708-4ba2-9358-bc6b186fb23a
  242. type: USAGE
  243. invoice_id: 2d20b330-b0af-48af-825f-e60c5fe926c2
  244. account_id: e6699b04-deb6-4ed4-9b62-12d11baf6806
  245. bundle_id: c20f6d96-d5ec-424f-92cd-fd352e28760c
  246. subscription_id: 0684ad09-42c7-4799-b066-433d9c0d3695
  247. description: server-monthly-usage-type-1 (usage item)
  248. plan_name: server-monthly
  249. phase_name: server-monthly-evergreen
  250. usage_name: server-monthly-usage-type-1
  251. start_date: 2013-08-01
  252. end_date: 2013-09-01
  253. amount: 3069.000000000
  254. rate: NULL
  255. currency: USD
  256. linked_item_id: NULL
  257. created_by: Next Billing Date
  258. created_date: 2013-09-01 06:00:21
  259. account_record_id: 6
  260. tenant_record_id: 6
  261.  
  262. =end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement