diff --git a/app/controllers/spree/admin/adjustments_controller_decorator.rb b/app/controllers/spree/admin/adjustments_controller.rb similarity index 57% rename from app/controllers/spree/admin/adjustments_controller_decorator.rb rename to app/controllers/spree/admin/adjustments_controller.rb index a45e3b4859..07a9207c14 100644 --- a/app/controllers/spree/admin/adjustments_controller_decorator.rb +++ b/app/controllers/spree/admin/adjustments_controller.rb @@ -1,12 +1,23 @@ module Spree module Admin - AdjustmentsController.class_eval do + class AdjustmentsController < ResourceController + belongs_to 'spree/order', find_by: :number + destroy.after :reload_order + prepend_before_filter :set_included_tax, only: [:create, :update] before_filter :set_default_tax_rate, only: :edit before_filter :enable_updates, only: :update private + def reload_order + @order.reload + end + + def collection + parent.adjustments.eligible + end + # Choose a default tax rate to show on the edit form. The adjustment stores its included # tax in dollars, but doesn't store the source of the tax (ie. TaxRate that generated it). # We guess which tax rate here, choosing: @@ -15,28 +26,34 @@ module Spree # When we have to go with 2, we show an error message to ask the admin to check that the # correct tax is being applied. def set_default_tax_rate - if @adjustment.included_tax > 0 - trs = TaxRate.match(@order) - tr_yielding_matching_tax = trs.select { |tr| tr.compute_tax(@adjustment.amount) == @adjustment.included_tax }.first.andand.id - tr_valid_for_order = TaxRate.match(@order).first.andand.id + return if @adjustment.included_tax <= 0 - @tax_rate_id = tr_yielding_matching_tax || tr_valid_for_order + tax_rates = TaxRate.match(@order) + tax_rate_with_matching_tax = find_tax_rate_with_matching_tax(tax_rates) + tax_rate_valid_for_order = tax_rates.first.andand.id - if tr_yielding_matching_tax.nil? - @adjustment.errors.add :tax_rate_id, I18n.t(:adjustments_tax_rate_error) - end + @tax_rate_id = tax_rate_with_matching_tax || tax_rate_valid_for_order + + return unless tax_rate_with_matching_tax.nil? + + @adjustment.errors.add :tax_rate_id, I18n.t(:adjustments_tax_rate_error) + end + + def find_tax_rate_with_matching_tax(tax_rates) + tax_rates_yielding_matching_tax = tax_rates.select do |tr| + tr.compute_tax(@adjustment.amount) == @adjustment.included_tax end + tax_rates_yielding_matching_tax.first.andand.id end def set_included_tax + included_tax = 0 if params[:tax_rate_id].present? tax_rate = TaxRate.find params[:tax_rate_id] amount = params[:adjustment][:amount].to_f - params[:adjustment][:included_tax] = tax_rate.compute_tax amount - - else - params[:adjustment][:included_tax] = 0 + included_tax = tax_rate.compute_tax amount end + params[:adjustment][:included_tax] = included_tax end # Spree 2.0 keeps shipping fee adjustments open unless they are manually diff --git a/config/routes/spree.rb b/config/routes/spree.rb index 72d43d90ec..bf06735bb7 100644 --- a/config/routes/spree.rb +++ b/config/routes/spree.rb @@ -74,6 +74,8 @@ Spree::Core::Engine.routes.prepend do get :poll end end + + resources :adjustments end resources :users do