mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-11 23:17:48 +00:00
Fix voucher adjustments association
Add missing, "inverse_of" and "dependent" options. Use :nullify as for "dependent" because we would want to keep the adjustments on order even if the voucher is deleted.
This commit is contained in:
committed by
Maikel Linke
parent
58469adfeb
commit
4b5d6d7eac
@@ -44,6 +44,8 @@ module Spree
|
||||
|
||||
belongs_to :tax_rate, -> { where spree_adjustments: { originator_type: 'Spree::TaxRate' } },
|
||||
foreign_key: 'originator_id'
|
||||
belongs_to :voucher, -> { where spree_adjustments: { originator_type: 'Spree::Voucher' } },
|
||||
foreign_key: 'originator_id', inverse_of: :adjustments
|
||||
|
||||
validates :label, presence: true
|
||||
validates :amount, numericality: true
|
||||
|
||||
@@ -7,7 +7,8 @@ class Voucher < ApplicationRecord
|
||||
|
||||
belongs_to :enterprise
|
||||
|
||||
has_many :adjustments, as: :originator, class_name: 'Spree::Adjustment'
|
||||
has_many :adjustments, as: :originator, class_name: 'Spree::Adjustment', inverse_of: :voucher,
|
||||
dependent: :nullify
|
||||
|
||||
validates :code, presence: true, uniqueness: { scope: :enterprise_id }
|
||||
|
||||
@@ -91,6 +92,8 @@ class Voucher < ApplicationRecord
|
||||
# override the one from CalculatedAdjustments
|
||||
# Create an "open" adjustment which will be updated later once tax and other fees have
|
||||
# been applied to the order
|
||||
#
|
||||
# rubocop:disable Style/OptionalBooleanParameter
|
||||
def create_adjustment(label, order, mandatory = false, _state = "open", tax_category = nil)
|
||||
amount = compute_amount(order)
|
||||
|
||||
@@ -106,11 +109,13 @@ class Voucher < ApplicationRecord
|
||||
|
||||
order.adjustments.create(adjustment_attributes)
|
||||
end
|
||||
# rubocop:enable Style/OptionalBooleanParameter
|
||||
|
||||
# override the one from CalculatedAdjustments so 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
|
||||
# Doesn't work with taxes for now
|
||||
# TODO move this to a calculator
|
||||
def compute_amount(order)
|
||||
amount = calculator.compute(order)
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ module Spree
|
||||
it { is_expected.to belong_to(:order) }
|
||||
it { is_expected.to belong_to(:tax_category) }
|
||||
it { is_expected.to belong_to(:tax_rate) }
|
||||
it { is_expected.to belong_to(:voucher) }
|
||||
end
|
||||
|
||||
describe "scopes" do
|
||||
|
||||
Reference in New Issue
Block a user