2021-04-22 19:24:08 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# A subscription added to the shopping cart
|
|
|
|
class CartItem::Subscription < CartItem::BaseItem
|
2022-12-28 17:51:27 +01:00
|
|
|
belongs_to :plan
|
|
|
|
belongs_to :customer_profile, class_name: 'InvoicingProfile'
|
2021-10-14 18:20:10 +02:00
|
|
|
|
2022-12-28 17:51:27 +01:00
|
|
|
def customer
|
|
|
|
customer_profile.user
|
2021-05-28 17:34:20 +02:00
|
|
|
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
|
2022-12-28 17:51:27 +01: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(
|
2022-12-28 17:51:27 +01:00
|
|
|
plan_id: plan.id,
|
|
|
|
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)
|
2022-12-28 17:51:27 +01:00
|
|
|
if plan.disabled
|
|
|
|
errors.add(:plan, I18n.t('cart_item_validation.plan'))
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
if plan.group_id != customer.group_id
|
2023-02-24 17:26:55 +01:00
|
|
|
errors.add(:group, I18n.t('cart_item_validation.plan_group', **{ GROUP: plan.group.name }))
|
2022-06-07 16:55:24 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|