2021-10-12 14:07:35 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# A subscription extended for free, added to the shopping cart
|
|
|
|
class CartItem::FreeExtension < CartItem::BaseItem
|
2022-12-28 17:51:27 +01:00
|
|
|
belongs_to :customer_profile, class_name: 'InvoicingProfile'
|
|
|
|
belongs_to :subscription
|
2021-10-12 14:07:35 +02:00
|
|
|
|
2022-12-28 17:51:27 +01:00
|
|
|
def customer
|
|
|
|
statistic_profile.user
|
2021-10-12 14:07:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def start_at
|
2022-12-28 17:51:27 +01:00
|
|
|
raise InvalidSubscriptionError if subscription.nil?
|
|
|
|
if new_expiration_date.nil? || new_expiration_date <= subscription.expired_at
|
2022-12-27 17:15:49 +01:00
|
|
|
raise InvalidSubscriptionError, I18n.t('cart_items.must_be_after_expiration')
|
|
|
|
end
|
2021-10-12 14:07:35 +02:00
|
|
|
|
2022-12-28 17:51:27 +01:00
|
|
|
subscription.expired_at
|
2021-10-12 14:07:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def price
|
|
|
|
elements = { OfferDay: 0 }
|
|
|
|
|
|
|
|
{ elements: elements, amount: 0 }
|
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
2022-12-28 17:51:27 +01:00
|
|
|
I18n.t('cart_items.free_extension', DATE: I18n.l(new_expiration_date))
|
2021-10-12 14:07:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_object
|
|
|
|
::OfferDay.new(
|
2022-12-28 17:51:27 +01:00
|
|
|
subscription_id: subscription.id,
|
2021-10-12 14:07:35 +02:00
|
|
|
start_at: start_at,
|
2022-12-28 17:51:27 +01:00
|
|
|
end_at: new_expiration_date
|
2021-10-12 14:07:35 +02:00
|
|
|
)
|
|
|
|
end
|
2022-03-18 19:44:30 +01:00
|
|
|
|
|
|
|
def type
|
|
|
|
'subscription'
|
|
|
|
end
|
2021-10-12 14:07:35 +02:00
|
|
|
end
|