Money.Subscription (Money v5.23.0)
View SourceProvides functions to create, upgrade and downgrade subscriptions from one plan to another.
Since moving from one plan to another may require prorating the payment stream at the point of transition, this module is introduced to provide a single point of calculation of the proration in order to give clear focus to the issues of calculating the carry-over amount or the carry-over period at the point of plan change.
Defining a subscription
A subscription records this current state and history of all plans assigned over time to a subscriber. The definition is deliberately minimal to simplify integration into applications that have a specific implementation of a subscription.
A new subscription is created with Money.Subscription.new/3
which has the following attributes:
planwhich defines the initial plan for the subscription. This option is required.effective_datewhich determines the effective date of the inital plan. This option is required.optionswhich include:created_atand:idwith which a subscription may be annotated
Changing a subscription plan
Changing a subscription plan requires the following information be provided:
A Subscription or the definition of the current plan
The definition of the new plan
The strategy for changing the plan which is either:
to have the effective date of the new plan be after the current interval of the current plan
To change the plan immediately in which case there will be a credit on the current plan which needs to be applied to the new plan.
See Money.Subscription.change_plan/3
When the new plan is effective at the end of the current billing period
The first strategy simply finishes the current billing period before the new plan is introduced and therefore no proration is required. This is the default strategy.
When the new plan is effective immediately
If the new plan is to be effective immediately then any credit balance remaining on the old plan needs to be applied to the new plan. There are two options of applying the credit:
Reduce the billing amount of the first period of the new plan be the amount of the credit left on the old plan. This means that the billing amount for the first period of the new plan will be different (less) than the billing amount for subsequent periods on the new plan.
Extend the first period of the new plan by the interval amount that can be funded by the credit amount left on the old plan. In the situation where the credit amount does not fully fund an integral interval the additional interval can be truncated or rounded up to the next integral period.
Plan definition
This module, and Money in general, does not provide a full
billing or subscription solution - its focus is to support a reliable
means of calcuating the accounting outcome of a plan change only.
Therefore the plan definition required by Money.Subscription can be
any Map.t that includes the following fields:
intervalwhich defines the time interval for a plan. The value can be one ofday,week,monthoryear.interval_countwhich defines the number ofintervals for the current plan interval. This must be a positive integer.pricewhich is aMoney.trepresenting the price of the plan to be paid each interval count.
Billing in advance
This module calculates all subscription changes on the basis that billing is done in advance. This primarily affects the calculation of plan credit when a plan changes. The assumption is that the period from the start of the current interval to the point of change has been consumed and therefore the credit is based upon that period of the plan that has not yet been consumed.
If the calculation was based upon "payment in arrears" then the credit would actually be a debit since the part of the current period consumed has not yet been paid.
Summary
Functions
A struct defining a subscription
Cancel a subscription's pending plan.
Change plan from the current plan to a new plan.
Change plan from the current plan to a new plan.
Returns the first date of the current interval of a plan.
Retrieve the plan that is currently in affect.
Returns the start date of the current plan.
Returns number of days remaining in a plan interval.
Returns the latest plan for a subscription.
Creates a new subscription.
Creates a new subscription or raises an exception.
Returns the next interval start date for a plan.
Returns number of days in a plan interval.
Returns a boolean indicating if there is a pending plan.
Types
@type id() :: term()
An id that uniquely identifies a subscription
@type t() :: %Money.Subscription{ created_at: DateTime.t(), id: id(), plans: [{Money.Subscription.Change.t(), Money.Subscription.Plan.t()}] }
A Money.Subscription type
Functions
A struct defining a subscription
:idany term that uniquely identifies this subscription:plansis a list of{change, plan}tuples that record the history of plans assigned to this subscription:created_atrecords theDateTime.twhen the subscription was created
Cancel a subscription's pending plan.
A pending plan arise when a a Subscription.change_plan/3 has
been executed but the effective date is in the future. Only
one plan may be pending at any one time so that if
Subscription.change_plan/3 is attemtped a second time an
error tuple will be returned.
Subscription.cancel_pending_plan/2
can be used to roll back the pending plan change.
Arguments
:subscriptionis anyMoney.Subscription.t:optionsis aKeyword.t
Options
:todayis aDate.tthat represents today. The default isDate.utc_today
Returns
- An updated
Money.Subscription.twhich may or may not have had a pending plan. If it did have a pending plan that plan is deleted. If there was no pending plan then the subscription is returned unchanged.
@spec change_plan( subscription_or_plan :: t() | Money.Subscription.Plan.t(), new_plan :: Money.Subscription.Plan.t(), options :: Keyword.t() ) :: {:ok, Money.Subscription.Change.t() | t()} | {:error, {module(), String.t()}}
Change plan from the current plan to a new plan.
Arguments
subscription_or_planis either aMoney.Subscription.torMoney.Subscription.Plan.tor a map with the same fieldsnew_planis aMoney.Subscription.Plan.tor a map with at least the fieldsinterval,interval_countandpricecurrent_interval_startedis aDate.tor other map with the fieldsyear,month,dayandcalendaroptionsis a keyword list of options the define how the change is to be made
Options
:effectivedefines when the new plan comes into effect. The values are:immediately, aDate.tor:next_period. The default is:next_period. Note that the date applied in the case of:immediatelyis the date returned byDate.utc_today.:proratewhich determines how to prorate the current plan into the new plan. The options are:pricewhich will reduce the price of the first period of the new plan by the credit amount left on the old plan (this is the default). Or:periodin which case the first period of the new plan is extended by theintervalamount of the new plan that the credit on the old plan will fund.:rounddetermines whether when prorating the:periodit is truncated or rounded up to the next nearest fullinterval_count. Valid values are:down,:half_up,:half_even,:ceiling,:floor,:half_down,:up. The default is:up.:first_interval_starteddetermines the anchor day for monthly billing. For example if a monthly plan starts on January 31st then the next period will start on February 28th (or 29th). The period following that should, however, be March 31st. Ifsubscription_or_planis aMoney.Subscription.tthen the:first_interval_startedis automatically populated from the subscription. If:first_interval_startedisnilthen the date defined by:effectiveis used.
Returns
A Money.Subscription.Change.t with the following elements:
:first_interval_startswhich is the start date of the first interval for the new plan:first_billing_amountis the amount to be billed, net of any credit, at the:first_interval_starts:next_interval_startsis the start date of the next interval after thefirst intervalincluding anycredit_days_applied:credit_amountis the amount of unconsumed credit of the current plan:credit_amount_appliedis the amount of credit applied to the new plan. If the:prorateoption is:price(the default) then:first_billing_amountis the plan:pricereduced by the:credit_amount_applied. If the:prorateoption is:periodthen the:first_billing_amountis the planpriceand the:next_interval_dateis extended by the:credit_days_appliedinstead.:credit_days_appliedis the number of days credit applied to the first interval by adding days to the:first_interval_startsdate.:credit_period_endsis the date on which any applied credit is consumed ornil:carry_forwardis any amount of credit carried forward to a subsequent period. If non-zero, this amount is a negativeMoney.t. It is non-zero when the credit amount for the current plan is greater than the:priceof the new plan. In this case the:first_billing_amountis zero.
Returns
{:ok, updated_subscription}or{:error, {exception, message}}
Examples
# Change at end of the current period so no proration
iex> current = Money.Subscription.Plan.new!(Money.new(:USD, 10), :month, 1)
iex> new = Money.Subscription.Plan.new!(Money.new(:USD, 10), :month, 3)
iex> Money.Subscription.change_plan current, new, current_interval_started: ~D[2018-01-01]
{:ok, %Money.Subscription.Change{
carry_forward: Money.zero(:USD),
credit_amount: Money.zero(:USD),
credit_amount_applied: Money.zero(:USD),
credit_days_applied: 0,
credit_period_ends: nil,
next_interval_starts: ~D[2018-05-01],
first_billing_amount: Money.new(:USD, 10),
first_interval_starts: ~D[2018-02-01]
}}
# Change during the current plan generates a credit amount
iex> current = Money.Subscription.Plan.new!(Money.new(:USD, 10), :month, 1)
iex> new = Money.Subscription.Plan.new!(Money.new(:USD, 10), :month, 3)
iex> Money.Subscription.change_plan current, new, current_interval_started: ~D[2018-01-01], effective: ~D[2018-01-15]
{:ok, %Money.Subscription.Change{
carry_forward: Money.zero(:USD),
credit_amount: Money.new(:USD, "5.49"),
credit_amount_applied: Money.new(:USD, "5.49"),
credit_days_applied: 0,
credit_period_ends: nil,
next_interval_starts: ~D[2018-04-15],
first_billing_amount: Money.new(:USD, "4.51"),
first_interval_starts: ~D[2018-01-15]
}}
# Change during the current plan generates a credit period
iex> current = Money.Subscription.Plan.new!(Money.new(:USD, 10), :month, 1)
iex> new = Money.Subscription.Plan.new!(Money.new(:USD, 10), :month, 3)
iex> Money.Subscription.change_plan current, new, current_interval_started: ~D[2018-01-01], effective: ~D[2018-01-15], prorate: :period
{:ok, %Money.Subscription.Change{
carry_forward: Money.zero(:USD),
credit_amount: Money.new(:USD, "5.49"),
credit_amount_applied: Money.zero(:USD),
credit_days_applied: 50,
credit_period_ends: ~D[2018-03-05],
next_interval_starts: ~D[2018-06-04],
first_billing_amount: Money.new(:USD, 10),
first_interval_starts: ~D[2018-01-15]
}}
@spec change_plan!( subscription_or_plan :: t() | Money.Subscription.Plan.t(), new_plan :: Money.Subscription.Plan.t(), options :: Keyword.t() ) :: Money.Subscription.Change.t() | no_return()
Change plan from the current plan to a new plan.
Retuns the plan or raises an exception on error.
See Money.Subscription.change_plan/3 for the description
of arguments, options and return.
@spec current_interval_start_date( t() | {Money.Subscription.Change.t(), Money.Subscription.Plan.t()} | map(), Keyword.t() ) :: Date.t()
Returns the first date of the current interval of a plan.
Arguments
:subscription_or_changesetis anyMoney.Subscription.tor a{Change.t, Plan.t}tuple:optionsis a keyword list of options
Options
:todayis aDate.tthat represents today. The default isDate.utc_today
Returns
- The
Date.tthat is the first date of the current interval
@spec current_plan(t() | map(), Keyword.t()) :: Money.Subscription.Plan.t() | {Money.Subscription.Change.t(), Money.Subscription.Plan.t()} | nil
Retrieve the plan that is currently in affect.
The plan in affect is not necessarily the first plan in the list. We may have upgraded plans to be in affect at some later time.
Arguments
subscriptionis aMoney.Subscription.tor any map that provides the field:plans
Returns
- The
Money.Subscription.Plan.tthat is the plan currently in affect ornil
Returns the start date of the current plan.
Arguments
subscriptionis aMoney.Subscription.tor any map that provides the field:plans
Returns
- The start
Date.tof the current plan
@spec days_remaining(Money.Subscription.Plan.t(), Date.t(), Date.t(), Keyword.t()) :: integer()
Returns number of days remaining in a plan interval.
Arguments
planis anyMoney.Subscription.Plan.tcurrent_interval_startedis aDate.teffective_dateis aDate.tafter thecurrent_interval_startedand before the end of theplan_days
Returns
The number of days remaining in a plan interval
Examples
iex> plan = Money.Subscription.Plan.new! Money.new!(:USD, 100), :month, 1
iex> Money.Subscription.days_remaining plan, ~D[2018-01-01], ~D[2018-01-02]
30
iex> Money.Subscription.days_remaining plan, ~D[2018-02-01], ~D[2018-02-02]
27
@spec latest_plan(t() | map()) :: {Money.Subscription.Change.t(), Money.Subscription.Plan.t()}
Returns the latest plan for a subscription.
The latest plan may not be in affect since its start date may be in the future.
Arguments
subscriptionis aMoney.Subscription.tor any map that provides the field:plans
Returns
- The
Money.Subscription.Plan.tthat is the most recent plan - whether or not it is the currently active plan.
@spec new( plan :: Money.Subscription.Plan.t(), effective_date :: Date.t(), Keyword.t() ) :: {:ok, t()} | {:error, {module(), String.t()}}
Creates a new subscription.
Arguments
planis anyMoney.Subscription.Plan.tthe defines the initial planeffective_dateis aDate.tthat represents the effective date of the initial plan. This defines the start of the first intervaloptionsis a keyword list of options
Options
:idis any term that an application can use to uniquely identify this subscription. It is not used in any function in this module.:created_atis aDateTime.tthat records the timestamp when the subscription was created. The default isDateTime.utc_now/0
Returns
{:ok, Money.Subscription.t}or{:error, {exception, message}}
@spec new!( plan :: Money.Subscription.Plan.t(), effective_date :: Date.t(), Keyword.t() ) :: t() | no_return()
Creates a new subscription or raises an exception.
Arguments
planis anyMoney.Subscription.Plan.tthe defines the initial planeffective_dateis aDate.tthat represents the effective date of the initial plan. This defines the start of the first interval:optionsis a keyword list of options
Options
:idis any term that an application can use to uniquely identify this subscription. It is not used in any function in this module.:created_atis aDateTime.tthat records the timestamp when the subscription was created. The default isDateTime.utc_now/0
Returns
A
Money.Subscription.torraises an exception
@spec next_interval_starts(Money.Subscription.Plan.t(), Date.t(), Keyword.t()) :: Date.t()
Returns the next interval start date for a plan.
Arguments
planis anyMoney.Subscription.Plan.t:current_interval_startedis theDate.tthat represents the start of the current interval
Returns
The next interval start date as a Date.t.
Example
iex> plan = Money.Subscription.Plan.new!(Money.new!(:USD, 100), :month)
iex> Money.Subscription.next_interval_starts(plan, ~D[2018-03-01])
~D[2018-04-01]
iex> plan = Money.Subscription.Plan.new!(Money.new!(:USD, 100), :day, 30)
iex> Money.Subscription.next_interval_starts(plan, ~D[2018-02-01])
~D[2018-03-03]
@spec plan_days(Money.Subscription.Plan.t(), Date.t(), Keyword.t()) :: integer()
Returns number of days in a plan interval.
Arguments
planis anyMoney.Subscription.Plan.tcurrent_interval_startedis anyDate.t
Returns
The number of days in a plan interval.
Examples
iex> plan = Money.Subscription.Plan.new! Money.new!(:USD, 100), :month, 1
iex> Money.Subscription.plan_days plan, ~D[2018-01-01]
31
iex> Money.Subscription.plan_days plan, ~D[2018-02-01]
28
iex> Money.Subscription.plan_days plan, ~D[2018-04-01]
30
Returns a boolean indicating if there is a pending plan.
A pending plan is one where the subscription has changed plans but the plan is not yet in effect. There can only be one pending plan.
Arguments
:subscriptionis anyMoney.Subscription.t:optionsis a keyword list of options
Options
:todayis aDate.tthat represents the effective date used to determine is there is a pending plan. The default isDate.utc_today/1.
Returns
- Either
trueorfalse