2021-04-22 19:24:08 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-01-11 17:32:16 +01:00
|
|
|
require_relative 'cart_item'
|
|
|
|
|
2021-04-22 19:24:08 +02:00
|
|
|
# A payment schedule applied to plan in the shopping cart
|
2022-12-28 17:51:27 +01:00
|
|
|
class CartItem::PaymentSchedule < ApplicationRecord
|
2023-02-03 12:49:17 +01:00
|
|
|
self.table_name = 'cart_item_payment_schedules'
|
|
|
|
|
2022-12-28 17:51:27 +01:00
|
|
|
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
|