Skip to Content Skip to Search
Methods
C
M
N
O
P
S
T
Included Modules

Attributes

[R] admin_view
[R] data
[R] member
[R] member_payment_gateway_id
[R] plan_payment_gateway_id
[R] proration_date
[R] subscription_payment_gateway_id

Class Public methods

new(member:, proration_date: 20.minutes.from_now.to_i, new_plan: nil, admin_view: false)

# File app/models/billing/upcoming_invoice.rb, line 8
def initialize(member:, proration_date: 20.minutes.from_now.to_i, new_plan: nil, admin_view: false)
  @member = member
  @proration_date = proration_date
  @plan_payment_gateway_id = new_plan

  @member_payment_gateway_id = member.payment_gateway_id
  @subscription_payment_gateway_id = member.active_subscription&.payment_gateway_id

  @data = stripe_invoice_data

  @admin_view = admin_view
end

Instance Public methods

changing?()

# File app/models/billing/upcoming_invoice.rb, line 21
def changing?
  !!new_plan&.subscription_plan_type? && !!member_payment_gateway_id && !!subscription_payment_gateway_id
end

coupon_message()

# File app/models/billing/upcoming_invoice.rb, line 29
def coupon_message
  coupon = data[:coupon]
  return unless coupon

  fetcher = Action::CouponFetcher.new(member.site, plan)
  coupon_data = fetcher.coupon_message(coupon)
  "#{translate("coupon_discount")}: #{coupon_data}"
end

message()

# File app/models/billing/upcoming_invoice.rb, line 25
def message
  @message ||= changing? ? "#{proration_message} #{next_bill_message}" : new_subscription_message
end

old_sub()

# File app/models/billing/upcoming_invoice.rb, line 55
def old_sub
  @old_sub ||= DB::Subscription.where(payment_gateway_id: subscription_payment_gateway_id).last
end

prorated_amount_due()

# File app/models/billing/upcoming_invoice.rb, line 38
def prorated_amount_due
  return nil unless changing?

  current_prorations = data[:items].select { |item| item[:timestamp_start] == proration_date }
  current_prorations.inject(0) { |cost, proration| cost + proration[:amount] }
end

sweep_proration?()

# File app/models/billing/upcoming_invoice.rb, line 45
def sweep_proration?
  return false unless changing?

  prorated_amount_due > 0 && !billing_date_today?
end

trial_duration()

# File app/models/billing/upcoming_invoice.rb, line 51
def trial_duration
  TrialDuration.for(new_plan:, current_subscription: old_sub)
end