Move taxing of enterprise fees to TaxRate

This commit is contained in:
Matt-Yorkley
2021-03-07 14:10:38 +00:00
parent c2211c501d
commit 2de442f44d
5 changed files with 15 additions and 11 deletions

View File

@@ -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'

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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