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-10-14 18:20:10 +02:00
|
|
|
attr_reader :start_at
|
|
|
|
|
|
|
|
def initialize(plan, customer, start_at = nil)
|
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-10-14 18:20:10 +02:00
|
|
|
@start_at = start_at
|
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
|
2021-09-14 16:35:18 +02:00
|
|
|
@plan.base_name
|
2021-04-23 12:52:06 +02:00
|
|
|
end
|
2021-05-19 18:12:52 +02:00
|
|
|
|
2021-05-21 18:25:18 +02:00
|
|
|
def to_object
|
2021-06-01 12:20:02 +02:00
|
|
|
::Subscription.new(
|
2021-05-21 18:25:18 +02:00
|
|
|
plan_id: @plan.id,
|
2021-10-14 18:20:10 +02:00
|
|
|
statistic_profile_id: StatisticProfile.find_by(user: @customer).id,
|
|
|
|
start_at: @start_at
|
2021-05-19 18:12:52 +02:00
|
|
|
)
|
|
|
|
end
|
2022-03-18 19:44:30 +01:00
|
|
|
|
|
|
|
def type
|
|
|
|
'subscription'
|
|
|
|
end
|
2022-06-07 16:55:24 +02:00
|
|
|
|
|
|
|
def valid?(_all_items)
|
|
|
|
if @plan.disabled
|
|
|
|
@errors[:item] = 'plan is disabled'
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|