From 8fccdbf92f1945ee9d5884f5a0261ff96929ba30 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Sun, 7 Feb 2021 00:05:11 +0000 Subject: [PATCH] Introduce TaxCategories to ShippingMethods --- .../spree/admin/shipping_methods_controller.rb | 1 + app/models/calculator/default_tax.rb | 12 ++++++++---- app/models/spree/shipment.rb | 4 ++++ app/models/spree/shipping_method.rb | 2 ++ .../spree/admin/shipping_methods/_form.html.haml | 9 +++++++-- 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/app/controllers/spree/admin/shipping_methods_controller.rb b/app/controllers/spree/admin/shipping_methods_controller.rb index 1ede5fe867..9ff1f93831 100644 --- a/app/controllers/spree/admin/shipping_methods_controller.rb +++ b/app/controllers/spree/admin/shipping_methods_controller.rb @@ -79,6 +79,7 @@ module Spree def load_data @available_zones = Zone.order(:name) + @tax_categories = Spree::TaxCategory.order(:name) @calculators = ShippingMethod.calculators.sort_by(&:name) end diff --git a/app/models/calculator/default_tax.rb b/app/models/calculator/default_tax.rb index 5293dfd1cb..fa5a254252 100644 --- a/app/models/calculator/default_tax.rb +++ b/app/models/calculator/default_tax.rb @@ -13,6 +13,8 @@ module Calculator case computable when Spree::Order compute_order(computable) + when Spree::Shipment + compute_shipment(computable) when Spree::LineItem compute_line_item(computable) end @@ -70,17 +72,19 @@ module Calculator .sum { |applicator| applicator.enterprise_fee.compute_amount(order) } end - def compute_line_item(line_item) - if line_item.tax_category == rate.tax_category + def compute_shipment_or_line_item(item) + if item.tax_category == rate.tax_category if rate.included_in_price - deduced_total_by_rate(line_item.total, rate) + deduced_total_by_rate(item.amount, rate) else - round_to_two_places(line_item.total * rate.amount) + round_to_two_places(item.amount * rate.amount) end else 0 end end + alias_method :compute_shipment, :compute_shipment_or_line_item + alias_method :compute_line_item, :compute_shipment_or_line_item def round_to_two_places(amount) BigDecimal(amount.to_s).round(2, BigDecimal::ROUND_HALF_UP) diff --git a/app/models/spree/shipment.rb b/app/models/spree/shipment.rb index afad7e4428..9bd02bd1a0 100644 --- a/app/models/spree/shipment.rb +++ b/app/models/spree/shipment.rb @@ -104,6 +104,10 @@ module Spree save! end + def tax_category + selected_shipping_rate.try(:shipping_method).try(:tax_category) + end + def refresh_rates return shipping_rates if shipped? diff --git a/app/models/spree/shipping_method.rb b/app/models/spree/shipping_method.rb index 426d658b43..a910bf55ef 100644 --- a/app/models/spree/shipping_method.rb +++ b/app/models/spree/shipping_method.rb @@ -23,6 +23,8 @@ module Spree class_name: 'Spree::Zone', foreign_key: 'shipping_method_id' + belongs_to :tax_category, class_name: 'Spree::TaxCategory' + validates :name, presence: true validate :distributor_validation validate :at_least_one_shipping_category diff --git a/app/views/spree/admin/shipping_methods/_form.html.haml b/app/views/spree/admin/shipping_methods/_form.html.haml index ba11c45ecd..934e808a7a 100644 --- a/app/views/spree/admin/shipping_methods/_form.html.haml +++ b/app/views/spree/admin/shipping_methods/_form.html.haml @@ -43,9 +43,14 @@ = f.hidden_field :tag_list, "ng-value" => "shippingMethod.tag_list" %tags-with-translation#something{ object: "shippingMethod", 'find-tags' => 'findTags(query)' } - + = render partial: 'spree/admin/shared/calculator_fields', locals: { f: f } + %fieldset.tax_categories.no-border-bottom + = f.field_container :tax_categories do + = f.select :tax_category_id, @tax_categories.map { |tc| [tc.name, tc.id] }, {}, :class => "select2 fullwidth" + = error_message_on :shipping_method, :tax_category_id + %fieldset.categories.no-border-bottom %legend{align: "center"}= t('.categories') = f.field_container :categories do @@ -56,7 +61,7 @@ %br/ = error_message_on :shipping_method, :shipping_category_id - + %fieldset.no-border-bottom %legend{align: "center"}= t('.zones') = f.field_container :zones do