Files
openfoodnetwork/app/models/voucher.rb
Gaetan Craig-Riou c17eddd69b Add Voucher#vine?
And small refactor
2024-11-28 13:35:01 +01:00

67 lines
1.9 KiB
Ruby

# frozen_string_literal: false
class Voucher < ApplicationRecord
VINE_TYPE = "VINE".freeze
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, uniqueness: { scope: :enterprise_id }
TYPES = ["Vouchers::FlatRate", "Vouchers::PercentageRate"].freeze
scope :vine, -> { where(voucher_type: VINE_TYPE) }
scope :not_vine, -> { where.not(voucher_type: VINE_TYPE).or(where(voucher_type: nil)) }
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
def vine?
voucher_type == VINE_TYPE
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