From 50da77c6dbdf57a1b1df3b766e1d3c68e0c57e22 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 14 Apr 2021 16:20:30 +0100 Subject: [PATCH 1/2] Hide tax rate selection when editing non-admin adjustments --- app/models/spree/adjustment.rb | 4 ++++ .../admin/adjustments/_edit_form.html.haml | 23 ++++++++++--------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/app/models/spree/adjustment.rb b/app/models/spree/adjustment.rb index fbdb0a8697..f3ca55527d 100644 --- a/app/models/spree/adjustment.rb +++ b/app/models/spree/adjustment.rb @@ -118,6 +118,10 @@ module Spree Spree::Money.new(amount, currency: currency) end + def admin? + originator_type.nil? + end + def immutable? state != "open" end diff --git a/app/views/spree/admin/adjustments/_edit_form.html.haml b/app/views/spree/admin/adjustments/_edit_form.html.haml index 918a9b5b6f..58d2db990d 100644 --- a/app/views/spree/admin/adjustments/_edit_form.html.haml +++ b/app/views/spree/admin/adjustments/_edit_form.html.haml @@ -5,18 +5,19 @@ = text_field :adjustment, :amount, :class => 'fullwidth' = f.error_message_on :amount - .four.columns - = f.field_container :included_tax do - = f.label :included_tax, t(:included_tax) - = f.text_field :included_tax, disabled: true, class: 'fullwidth', - value: number_with_precision(f.object.included_tax, precision: 2) - = f.error_message_on :included_tax + - if @adjustment.admin? + .four.columns + = f.field_container :included_tax do + = f.label :included_tax, t(:included_tax) + = f.text_field :included_tax, disabled: true, class: 'fullwidth', + value: number_with_precision(f.object.included_tax, precision: 2) + = f.error_message_on :included_tax - .omega.four.columns - = f.field_container :tax_rate_id do - = f.label :tax_rate_id, t(:tax_rate) - = select_tag :tax_rate_id, options_from_collection_for_select(Spree::TaxRate.all, :id, :name, @tax_rate_id), prompt: t(:remove_tax), class: 'select2 fullwidth' - = f.error_message_on :tax_rate_id + .omega.four.columns + = f.field_container :tax_rate_id do + = f.label :tax_rate_id, t(:tax_rate) + = select_tag :tax_rate_id, options_from_collection_for_select(Spree::TaxRate.all, :id, :name, @tax_rate_id), prompt: t(:remove_tax), class: 'select2 fullwidth' + = f.error_message_on :tax_rate_id .row .alpha.omega.twelve.columns From 206d9e06ea29c7f52cdea640fd2e615bdfd7df8f Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 15 Apr 2021 13:05:01 +0100 Subject: [PATCH 2/2] Reinstate old logic for adjustments controller Certain types of adjustments (eg enterprise fees) cannot really be changed arbitrarily; when the order is saved and "recalculated" the values will be reset. The adjustments are still shown in the main order edit tab, but are not editable in the adjustments tab. --- .../spree/admin/adjustments_controller.rb | 7 ++++- .../admin/adjustments_controller_spec.rb | 29 +++++++++++++++---- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/app/controllers/spree/admin/adjustments_controller.rb b/app/controllers/spree/admin/adjustments_controller.rb index f4fccde7ca..d5729a3477 100644 --- a/app/controllers/spree/admin/adjustments_controller.rb +++ b/app/controllers/spree/admin/adjustments_controller.rb @@ -18,7 +18,12 @@ module Spree end def collection - parent.all_adjustments.eligible + order_adjustments = parent.adjustments.where.not(originator_type: 'EnterpriseFee') + admin_adjustments = parent.adjustments.admin + payment_fees = parent.all_adjustments.payment_fee.eligible + shipping_fees = parent.all_adjustments.shipping + + order_adjustments.or(admin_adjustments) | payment_fees.or(shipping_fees) end def find_resource diff --git a/spec/controllers/spree/admin/adjustments_controller_spec.rb b/spec/controllers/spree/admin/adjustments_controller_spec.rb index 8dafb83493..4b9a7eaddb 100644 --- a/spec/controllers/spree/admin/adjustments_controller_spec.rb +++ b/spec/controllers/spree/admin/adjustments_controller_spec.rb @@ -9,20 +9,37 @@ module Spree before { controller_login_as_admin } describe "index" do - let!(:order) { create(:order) } + let!(:order) { create(:completed_order_with_totals) } let!(:adjustment1) { - create(:adjustment, originator_type: "Spree::ShippingMethod", order: order) + create(:adjustment, originator_type: "Spree::ShippingMethod", order: order, + adjustable: order.shipment) } let!(:adjustment2) { + create(:adjustment, originator_type: "Spree::PaymentMethod", eligible: true, order: order) + } + let!(:adjustment3) { create(:adjustment, originator_type: "Spree::PaymentMethod", eligible: false, order: order) } - let!(:adjustment3) { create(:adjustment, originator_type: "EnterpriseFee", order: order) } + let!(:adjustment4) { create(:adjustment, originator_type: "EnterpriseFee", order: order) } + let!(:adjustment5) { create(:adjustment, originator: nil, adjustable: order, order: order) } - it "loads all eligible adjustments" do + it "displays eligible adjustments" do spree_get :index, order_id: order.number - expect(assigns(:collection)).to include adjustment1, adjustment3 - expect(assigns(:collection)).to_not include adjustment2 + expect(assigns(:collection)).to include adjustment1, adjustment2 + expect(assigns(:collection)).to_not include adjustment3 + end + + it "displays admin adjustments" do + spree_get :index, order_id: order.number + + expect(assigns(:collection)).to include adjustment5 + end + + it "does not display enterprise fee adjustments" do + spree_get :index, order_id: order.number + + expect(assigns(:collection)).to_not include adjustment4 end end