From a10966b66b89470dbbae553bbaadb712d6a18049 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Wed, 27 Nov 2019 21:46:53 +0000 Subject: [PATCH 1/4] Add adjustments_controller from spree_backend so that we can now merge it with the OFN's decorator --- .../spree/admin/adjustments_controller.rb | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 app/controllers/spree/admin/adjustments_controller.rb diff --git a/app/controllers/spree/admin/adjustments_controller.rb b/app/controllers/spree/admin/adjustments_controller.rb new file mode 100644 index 0000000000..72e89c2983 --- /dev/null +++ b/app/controllers/spree/admin/adjustments_controller.rb @@ -0,0 +1,30 @@ +module Spree + module Admin + class AdjustmentsController < ResourceController + belongs_to 'spree/order', :find_by => :number + destroy.after :reload_order + + def toggle_state + redirect_to admin_order_adjustments_path(@order) if @adjustment.finalized? + + if @adjustment.immutable? + @adjustment.fire_state_event(:open) + flash[:success] = Spree.t(:adjustment_successfully_opened) + else + @adjustment.fire_state_event(:close) + flash[:success] = Spree.t(:adjustment_successfully_closed) + end + redirect_to admin_order_adjustments_path(@order) + end + + private + def reload_order + @order.reload + end + + def collection + parent.adjustments.eligible + end + end + end +end From 68bf599a1aa632c0e644014d1b6bb8c9e24aae42 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Mon, 11 Nov 2019 15:45:32 +0000 Subject: [PATCH 2/4] Merge spree/admin/adjustments_controller with decorator --- .../spree/admin/adjustments_controller.rb | 56 ++++++++++++++++++- .../admin/adjustments_controller_decorator.rb | 54 ------------------ 2 files changed, 55 insertions(+), 55 deletions(-) delete mode 100644 app/controllers/spree/admin/adjustments_controller_decorator.rb diff --git a/app/controllers/spree/admin/adjustments_controller.rb b/app/controllers/spree/admin/adjustments_controller.rb index 72e89c2983..96bf82e0b2 100644 --- a/app/controllers/spree/admin/adjustments_controller.rb +++ b/app/controllers/spree/admin/adjustments_controller.rb @@ -1,9 +1,13 @@ module Spree module Admin class AdjustmentsController < ResourceController - belongs_to 'spree/order', :find_by => :number + 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 + def toggle_state redirect_to admin_order_adjustments_path(@order) if @adjustment.finalized? @@ -18,6 +22,7 @@ module Spree end private + def reload_order @order.reload end @@ -25,6 +30,55 @@ module Spree 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: + # 1. A tax rate that will compute to the same amount as the existing tax + # 2. If that's not present, the first tax rate that's valid for the current order + # 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 + return if @adjustment.included_tax <= 0 + + 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 + + @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 + 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 + # closed. But open adjustments cannot be edited. + # To preserve updates, like changing the amount of the shipping fee, + # we close the adjustment first. + # + # The Spree admin interface allows to open and close adjustments manually + # but we removed that functionality as it had no purpose for us. + def enable_updates + @adjustment.close + end end end end diff --git a/app/controllers/spree/admin/adjustments_controller_decorator.rb b/app/controllers/spree/admin/adjustments_controller_decorator.rb deleted file mode 100644 index a45e3b4859..0000000000 --- a/app/controllers/spree/admin/adjustments_controller_decorator.rb +++ /dev/null @@ -1,54 +0,0 @@ -module Spree - module Admin - AdjustmentsController.class_eval do - prepend_before_filter :set_included_tax, only: [:create, :update] - before_filter :set_default_tax_rate, only: :edit - before_filter :enable_updates, only: :update - - private - - # 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: - # 1. A tax rate that will compute to the same amount as the existing tax - # 2. If that's not present, the first tax rate that's valid for the current order - # 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 - - @tax_rate_id = tr_yielding_matching_tax || tr_valid_for_order - - if tr_yielding_matching_tax.nil? - @adjustment.errors.add :tax_rate_id, I18n.t(:adjustments_tax_rate_error) - end - end - end - - def set_included_tax - 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 - end - end - - # Spree 2.0 keeps shipping fee adjustments open unless they are manually - # closed. But open adjustments cannot be edited. - # To preserve updates, like changing the amount of the shipping fee, - # we close the adjustment first. - # - # The Spree admin interface allows to open and close adjustments manually - # but we removed that functionality as it had no purpose for us. - def enable_updates - @adjustment.close - end - end - end -end From 842e191c5f91c9e8c46545dd59fdc6aed912e61e Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Thu, 28 Nov 2019 16:20:32 +0000 Subject: [PATCH 3/4] Remove toggle_state action that is not used in OFN --- .../spree/admin/adjustments_controller.rb | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/app/controllers/spree/admin/adjustments_controller.rb b/app/controllers/spree/admin/adjustments_controller.rb index 96bf82e0b2..07a9207c14 100644 --- a/app/controllers/spree/admin/adjustments_controller.rb +++ b/app/controllers/spree/admin/adjustments_controller.rb @@ -8,19 +8,6 @@ module Spree before_filter :set_default_tax_rate, only: :edit before_filter :enable_updates, only: :update - def toggle_state - redirect_to admin_order_adjustments_path(@order) if @adjustment.finalized? - - if @adjustment.immutable? - @adjustment.fire_state_event(:open) - flash[:success] = Spree.t(:adjustment_successfully_opened) - else - @adjustment.fire_state_event(:close) - flash[:success] = Spree.t(:adjustment_successfully_closed) - end - redirect_to admin_order_adjustments_path(@order) - end - private def reload_order From 4e7b397c5ac0bb6a8c2df466810647f95266a960 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Thu, 28 Nov 2019 16:26:22 +0000 Subject: [PATCH 4/4] Bring orders adjustments route from spree_backend --- config/routes/spree.rb | 2 ++ 1 file changed, 2 insertions(+) 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