1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/app/models/shopping_cart.rb
2021-04-22 19:24:08 +02:00

45 lines
1.3 KiB
Ruby

# frozen_string_literal: true
# Stores data about a shopping data
class ShoppingCart
attr_accessor :customer, :payment_method, :items, :coupon, :payment_schedule
# @param items {Array<CartItem::BaseItem>}
# @param coupon {CartItem::Coupon}
# @param payment_schedule {CartItem::PaymentSchedule}
# @param customer {User}
def initialize(customer, coupon, payment_method = '', items: [], payment_schedule: nil)
raise TypeError unless customer.class == User
@customer = customer
@payment_method = payment_method
@items = items
@coupon = coupon
@payment_schedule = payment_schedule
end
def total
total_amount = 0
all_elements = { slots: [] }
@items.map(&:price).each do |price|
total_amount += price[:amount]
all_elements.merge(price[:elements]) do |_key, old_val, new_val|
old_val | new_val
end
end
coupon_info = @coupon.price(total_amount)
schedule_info = @payment_schedule.schedule(coupon_info[:total_with_coupon], coupon_info[:total_without_coupon])
# return result
{
elements: all_elements,
total: schedule_info[:total].to_i,
before_coupon: coupon_info[:total_without_coupon].to_i,
coupon: @coupon.coupon,
schedule: schedule_info[:schedule]
}
end
end