2021-04-22 19:24:08 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# A subscription added to the shopping cart
|
|
|
|
class CartItem::Subscription < CartItem::BaseItem
|
2021-05-21 18:25:18 +02:00
|
|
|
def initialize(plan, customer)
|
2021-04-26 11:40:26 +02:00
|
|
|
raise TypeError unless plan.is_a? Plan
|
2021-04-22 19:24:08 +02:00
|
|
|
|
|
|
|
@plan = plan
|
2021-05-21 18:25:18 +02:00
|
|
|
@customer = customer
|
2021-05-31 15:39:56 +02:00
|
|
|
super
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|
|
|
|
|
2021-05-28 17:34:20 +02:00
|
|
|
def plan
|
|
|
|
raise InvalidGroupError if @plan.group_id != @customer.group_id
|
|
|
|
|
|
|
|
@plan
|
|
|
|
end
|
|
|
|
|
2021-04-22 19:24:08 +02:00
|
|
|
def price
|
2021-05-28 17:34:20 +02:00
|
|
|
amount = plan.amount
|
2021-04-22 19:24:08 +02:00
|
|
|
elements = { plan: amount }
|
|
|
|
|
|
|
|
{ elements: elements, amount: amount }
|
|
|
|
end
|
2021-04-23 12:52:06 +02:00
|
|
|
|
|
|
|
def name
|
|
|
|
@plan.name
|
|
|
|
end
|
2021-05-19 18:12:52 +02:00
|
|
|
|
2021-05-21 18:25:18 +02:00
|
|
|
def to_object
|
2021-05-19 18:12:52 +02:00
|
|
|
Subscription.new(
|
2021-05-21 18:25:18 +02:00
|
|
|
plan_id: @plan.id,
|
|
|
|
statistic_profile_id: StatisticProfile.find_by(user: @customer).id
|
2021-05-19 18:12:52 +02:00
|
|
|
)
|
|
|
|
end
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|