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

33 lines
904 B
Ruby
Raw Normal View History

# frozen_string_literal: true
# A payment schedule applied to plan in the shopping cart
class CartItem::PaymentSchedule
attr_reader :requested
2021-10-14 18:20:10 +02:00
def initialize(plan, coupon, requested, customer, start_at = nil)
2021-04-26 11:40:26 +02:00
raise TypeError unless coupon.is_a? CartItem::Coupon
@plan = plan
@coupon = coupon
@requested = requested
2021-10-14 18:20:10 +02:00
@customer = customer
@start_at = start_at
end
def schedule(total, total_without_coupon)
schedule = if @requested && @plan&.monthly_payment
2021-10-14 18:20:10 +02:00
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
end