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

45 lines
1.1 KiB
Ruby
Raw Normal View History

# 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
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
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)
else
nil
end
total_amount = if schedule
schedule[:items][0].amount
else
total
end
{ schedule: schedule, total: total_amount }
end
def type
'subscription'
end
def valid?(_all_items)
2022-12-28 17:51:27 +01:00
return true unless requested && plan&.monthly_payment
2022-12-28 17:51:27 +01:00
if plan&.disabled
errors.add(:plan, I18n.t('cart_item_validation.plan'))
return false
end
true
end
end