1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/models/cart_item/subscription.rb

47 lines
945 B
Ruby
Raw Normal View History

# 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
end
def price
amount = plan.amount
elements = { plan: amount }
{ elements: elements, amount: amount }
end
def name
2022-12-28 17:51:27 +01:00
plan.base_name
end
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
)
end
def type
'subscription'
end
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 }))
return false
end
true
end
end