2021-04-22 19:24:08 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# A payment schedule applied to plan in the shopping cart
|
2022-12-28 17:51:27 +01:00
|
|
|
class CartItem::PaymentSchedule < ApplicationRecord
|
|
|
|
belongs_to :customer_profile, class_name: 'InvoicingProfile'
|
|
|
|
belongs_to :coupon
|
|
|
|
belongs_to :plan
|
|
|
|
|
|
|
|
def customer
|
|
|
|
customer_profile.user
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def schedule(total, total_without_coupon)
|
2022-12-28 17:51:27 +01:00
|
|
|
schedule = if requested && plan&.monthly_payment
|
|
|
|
PaymentScheduleService.new.compute(plan, total_without_coupon, customer, coupon: coupon.coupon, start_at: start_at)
|
2021-04-22 19:24:08 +02:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
total_amount = if schedule
|
|
|
|
schedule[:items][0].amount
|
|
|
|
else
|
|
|
|
total
|
|
|
|
end
|
|
|
|
|
|
|
|
{ schedule: schedule, total: total_amount }
|
|
|
|
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
|
|
|
return true unless requested && plan&.monthly_payment
|
2022-07-20 17:46:09 +02:00
|
|
|
|
2022-12-28 17:51:27 +01:00
|
|
|
if plan&.disabled
|
|
|
|
errors.add(:plan, I18n.t('cart_item_validation.plan'))
|
2022-06-07 16:55:24 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|