diff --git a/app/models/spree/adjustment.rb b/app/models/spree/adjustment.rb index 973f7facee..366ade4935 100644 --- a/app/models/spree/adjustment.rb +++ b/app/models/spree/adjustment.rb @@ -35,10 +35,12 @@ module Spree # So we don't need the option `dependent: :destroy` as long as # AdjustmentMetadata has no destroy logic itself. has_one :metadata, class_name: 'AdjustmentMetadata' + has_many :adjustments, as: :adjustable, dependent: :destroy belongs_to :adjustable, polymorphic: true belongs_to :originator, -> { with_deleted }, polymorphic: true belongs_to :order, class_name: "Spree::Order" + belongs_to :tax_category, class_name: 'Spree::TaxCategory' belongs_to :tax_rate, -> { where spree_adjustments: { originator_type: 'Spree::TaxRate' } }, foreign_key: 'originator_id' diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index 1ed3c80da0..ba0cdc2b80 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -295,6 +295,7 @@ module Spree def create_tax_charge! Spree::TaxRate.adjust(self, line_items) Spree::TaxRate.adjust(self, shipments) if shipments.any? + Spree::TaxRate.adjust(self, all_adjustments.enterprise_fee) end def name diff --git a/lib/open_food_network/enterprise_fee_applicator.rb b/lib/open_food_network/enterprise_fee_applicator.rb index fe4544c604..8b12e17e76 100644 --- a/lib/open_food_network/enterprise_fee_applicator.rb +++ b/lib/open_food_network/enterprise_fee_applicator.rb @@ -11,11 +11,11 @@ module OpenFoodNetwork private def create_adjustment(label, adjustable) - adjustment = enterprise_fee.create_adjustment(label, adjustable, true) + adjustment = enterprise_fee.create_adjustment( + label, adjustable, true, "closed", tax_category(adjustable) + ) AdjustmentMetadata.create! adjustment: adjustment, enterprise: enterprise_fee.enterprise, fee_name: enterprise_fee.name, fee_type: enterprise_fee.fee_type, enterprise_role: role - - adjustment.set_absolute_included_tax! adjustment_tax(adjustment) end def line_item_adjustment_label @@ -30,11 +30,11 @@ module OpenFoodNetwork I18n.t(:enterprise_fee_by, type: enterprise_fee.fee_type, role: role, enterprise_name: enterprise_fee.enterprise.name) end - def adjustment_tax(adjustment) - tax_rates = TaxRateFinder.tax_rates_of(adjustment) - - tax_rates.select(&:included_in_price).sum do |rate| - rate.compute_tax adjustment.amount + def tax_category(target) + if target.is_a?(Spree::LineItem) && enterprise_fee.inherits_tax_category? + target.product.tax_category + else + enterprise_fee.tax_category end end end diff --git a/lib/spree/core/calculated_adjustments.rb b/lib/spree/core/calculated_adjustments.rb index 5b45063c3d..3e06a95179 100644 --- a/lib/spree/core/calculated_adjustments.rb +++ b/lib/spree/core/calculated_adjustments.rb @@ -26,7 +26,7 @@ module Spree # (which is any class that has_many :adjustments) and sets amount based on the # calculator as applied to the given calculable (Order, LineItems[], Shipment, etc.) # By default the adjustment will not be considered mandatory - def create_adjustment(label, adjustable, mandatory = false, state = "closed") + def create_adjustment(label, adjustable, mandatory = false, state = "closed", tax_category = nil) amount = compute_amount(adjustable) return if amount.zero? && !mandatory @@ -36,7 +36,8 @@ module Spree order: order_object_for(adjustable), label: label, mandatory: mandatory, - state: state + state: state, + tax_category: tax_category } if adjustable.respond_to?(:adjustments) diff --git a/spec/models/spree/order/state_machine_spec.rb b/spec/models/spree/order/state_machine_spec.rb index 0b37ee41fa..e61b84a9f4 100644 --- a/spec/models/spree/order/state_machine_spec.rb +++ b/spec/models/spree/order/state_machine_spec.rb @@ -55,7 +55,7 @@ describe Spree::Order do end it "adjusts tax rates when transitioning to payment" do - expect(Spree::TaxRate).to receive(:adjust) + expect(Spree::TaxRate).to receive(:adjust).at_least(:once) order.next! end end