mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-02 21:57:17 +00:00
A Vine voucher is really a specific type of FlatRate voucher but because a Vine voucher can be used by mutiple enterprise, it can be considered different enough to warrant it's own class. It still share a lot of the behaviour of a FlatRate voucher, so to avoid duplication, all the shared functionality have been moved to a Vouchers::FlatRatable concern.
32 lines
729 B
Ruby
32 lines
729 B
Ruby
# frozen_string_literal: true
|
|
|
|
require "active_support/concern"
|
|
|
|
module Vouchers
|
|
module FlatRatable
|
|
extend ActiveSupport::Concern
|
|
|
|
included do
|
|
validates :amount,
|
|
presence: true,
|
|
numericality: { greater_than: 0 }
|
|
end
|
|
|
|
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
|