2021-06-28 18:17:11 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# A prepaid-pack added to the shopping cart
|
|
|
|
class CartItem::PrepaidPack < CartItem::BaseItem
|
2022-12-28 17:51:27 +01:00
|
|
|
belongs_to :prepaid_pack
|
2022-12-30 15:29:52 +01:00
|
|
|
belongs_to :customer_profile, class_name: 'InvoicingProfile'
|
2021-06-28 18:17:11 +02:00
|
|
|
|
2022-12-28 17:51:27 +01:00
|
|
|
def customer
|
|
|
|
customer_profile.user
|
2021-06-28 18:17:11 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def pack
|
2022-12-28 17:51:27 +01:00
|
|
|
prepaid_pack
|
2021-06-28 18:17:11 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def price
|
|
|
|
amount = pack.amount
|
|
|
|
elements = { pack: amount }
|
|
|
|
|
|
|
|
{ elements: elements, amount: amount }
|
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
2022-12-28 17:51:27 +01:00
|
|
|
"#{pack.minutes / 60} h"
|
2021-06-28 18:17:11 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_object
|
|
|
|
::StatisticProfilePrepaidPack.new(
|
2022-12-28 17:51:27 +01:00
|
|
|
prepaid_pack_id: pack.id,
|
|
|
|
statistic_profile_id: StatisticProfile.find_by(user: customer).id
|
2021-06-28 18:17:11 +02:00
|
|
|
)
|
|
|
|
end
|
2022-03-18 19:44:30 +01:00
|
|
|
|
|
|
|
def type
|
|
|
|
'pack'
|
|
|
|
end
|
2022-06-07 16:55:24 +02:00
|
|
|
|
|
|
|
def valid?(_all_items)
|
2022-12-28 17:51:27 +01:00
|
|
|
if pack.disabled
|
2022-12-30 15:29:52 +01:00
|
|
|
errors.add(:prepaid_pack, I18n.t('cart_item_validation.pack'))
|
2022-06-07 16:55:24 +02:00
|
|
|
return false
|
|
|
|
end
|
2022-12-28 17:51:27 +01:00
|
|
|
if pack.group_id != customer.group_id
|
2023-02-24 17:26:55 +01:00
|
|
|
errors.add(:group, I18n.t('cart_item_validation.pack_group', **{ GROUP: pack.group.name }))
|
2022-12-28 17:51:27 +01:00
|
|
|
return false
|
|
|
|
end
|
2022-06-07 16:55:24 +02:00
|
|
|
true
|
|
|
|
end
|
2021-06-28 18:17:11 +02:00
|
|
|
end
|