mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-18 00:17:25 +00:00
We are taking advantage of having a FlatRate and a PercentageRate model to simplify the code a little
26 lines
637 B
Ruby
26 lines
637 B
Ruby
# frozen_string_literal: false
|
|
|
|
module Vouchers
|
|
class FlatRate < Voucher
|
|
validates :amount,
|
|
presence: true,
|
|
numericality: { greater_than: 0 }
|
|
|
|
def display_value
|
|
Spree::Money.new(amount)
|
|
end
|
|
|
|
# We limit adjustment to the maximum amount needed to cover the order, ie if the voucher
|
|
# covers more than the order.total we only need to create an adjustment covering the order.total
|
|
def compute_amount(order)
|
|
-amount.clamp(0, order.pre_discount_total)
|
|
end
|
|
|
|
def rate(order)
|
|
amount = compute_amount(order)
|
|
|
|
amount / order.pre_discount_total
|
|
end
|
|
end
|
|
end
|