Files
openfoodnetwork/app/models/voucher.rb
Gaetan Craig-Riou a2c4c44eea Move Vine voucher to Vouchers::Vine
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.
2024-11-28 13:35:01 +01:00

58 lines
1.6 KiB
Ruby

# frozen_string_literal: false
class Voucher < ApplicationRecord
self.belongs_to_required_by_default = false
acts_as_paranoid
belongs_to :enterprise, optional: false
# We want to keep the association with the adjustment when a voucher is "destroyed" as we use
# the soft delete functionality to activate/deactivate vouchers
has_many :adjustments,
as: :originator,
class_name: 'Spree::Adjustment',
dependent: nil
validates :code, presence: true
TYPES = ["Vouchers::FlatRate", "Vouchers::PercentageRate"].freeze
def code=(value)
super(value.to_s.strip)
end
# Ideally we would use `include CalculatedAdjustments` to be consistent with other adjustments,
# but vouchers have complicated calculation so we can't easily use Spree::Calculator. We keep
# the same method to stay as consistent as possible.
#
# Creates a new voucher adjustment for the given order with an amount of 0
# The amount will be calculated via VoucherAdjustmentsService#update
def create_adjustment(label, order)
adjustment_attributes = {
amount: 0,
originator: self,
order:,
label:,
mandatory: false,
state: "open",
tax_category: nil
}
order.adjustments.create(adjustment_attributes)
end
# The following method must be overriden in a concrete voucher.
def display_value
raise NotImplementedError, 'please use concrete voucher'
end
def compute_amount(_order)
raise NotImplementedError, 'please use concrete voucher'
end
def rate(_order)
raise NotImplementedError, 'please use concrete voucher'
end
end