1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-04 15:24:23 +01:00
fab-manager/app/models/cart_item/prepaid_pack.rb

50 lines
994 B
Ruby
Raw Normal View History

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
def type
'pack'
end
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'))
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
true
end
2021-06-28 18:17:11 +02:00
end