diff --git a/app/controllers/spree/admin/orders_controller.rb b/app/controllers/spree/admin/orders_controller.rb index 80c3707ca3..0e5cb4b825 100644 --- a/app/controllers/spree/admin/orders_controller.rb +++ b/app/controllers/spree/admin/orders_controller.rb @@ -8,9 +8,8 @@ module Spree include OpenFoodNetwork::SpreeApiKeyLoader helper CheckoutHelper - before_action :load_order, only: [:edit, :update, :fire, :resend, - :invoice, :print, :distribution] - before_action :load_distribution_choices, only: [:new, :edit, :update, :distribution] + before_action :load_order, only: [:edit, :update, :fire, :resend, :invoice, :print] + before_action :load_distribution_choices, only: [:new, :create, :edit, :update] before_action :require_distributor_abn, only: :invoice before_action :restore_saved_query!, only: :index @@ -24,22 +23,7 @@ module Spree end def new - @order = Order.create - @order.created_by = spree_current_user - @order.generate_order_number - @order.save - redirect_to spree.distribution_admin_order_path(@order) - end - - def distribution - return if order_params.blank? - - on_update - - @order.assign_attributes(order_params) - return unless @order.save(context: :set_distribution_step) - - redirect_to spree.admin_order_customer_path(@order) + @order = Spree::Order.new end def edit @@ -47,6 +31,16 @@ module Spree @order.errors.clear end + def create + @order = Spree::Order.new(order_params.merge(created_by: spree_current_user)) + + if @order.save(context: :require_distribution) + redirect_to spree.admin_order_customer_path(@order) + else + render :new + end + end + def update on_update diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index 74a4f332d9..aaab5875ff 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -104,8 +104,9 @@ module Spree validates :email, presence: true, format: /\A([\w.%+\-']+)@([\w\-]+\.)+(\w{2,})\z/i, if: :require_email - validates :order_cycle, presence: true, on: :set_distribution_step - validates :distributor, presence: true, on: :set_distribution_step + + validates :order_cycle, presence: true, on: :require_distribution + validates :distributor, presence: true, on: :require_distribution make_permalink field: :number diff --git a/app/views/spree/admin/orders/distribution.html.haml b/app/views/spree/admin/orders/distribution.html.haml deleted file mode 100644 index ac577f0dd4..0000000000 --- a/app/views/spree/admin/orders/distribution.html.haml +++ /dev/null @@ -1,27 +0,0 @@ -- content_for :page_actions do - %li= button_link_to t(:back_to_orders_list), spree.admin_orders_path, :icon => 'icon-arrow-left' - -= admin_inject_shops(module: 'admin.orders') -= admin_inject_order_cycles - -- content_for :page_title do - = t(:new_order) - \# - = @order.number - -= render 'spree/admin/shared/order_tabs', :current => 'Customer Details' - -= csrf_meta_tags - -%div - = render 'spree/shared/error_messages', :target => @order - -%div{"ng-app" => "admin.orders", "ng-controller" => "orderCtrl"} - = form_for @order, url: distribution_admin_order_path(@order), method: :put do |f| - = render 'spree/admin/orders/_form/distribution_fields' - -# This param passed to stop validation error in next page due to no line items in order yet: - = hidden_field_tag 'suppress_error_msg', "true" - = hidden_field_tag "set_distribution_step", "true" - = button_tag :class => 'secondary radius expand small', :id => 'update-button' do - %i.icon-arrow-right - = t(:next) diff --git a/app/views/spree/admin/orders/new.html.haml b/app/views/spree/admin/orders/new.html.haml index 556e464cd4..b43d50fc9e 100644 --- a/app/views/spree/admin/orders/new.html.haml +++ b/app/views/spree/admin/orders/new.html.haml @@ -1,7 +1,5 @@ - content_for :page_title do = t(:new_order) - \# - = @order.number - content_for :page_actions do %li= button_link_to t(:back_to_orders_list), spree.admin_orders_path, :icon => 'icon-arrow-left' @@ -9,17 +7,14 @@ = admin_inject_shops(module: 'admin.orders') = admin_inject_order_cycles -= render 'spree/admin/shared/order_tabs', :current => 'Order Details' - -= csrf_meta_tags += render 'spree/admin/shared/order_tabs', current: 'Order Details' %div - = render 'spree/shared/error_messages', :target => @order + = render 'spree/shared/error_messages', target: @order %div{"ng-app" => "admin.orders", "ng-controller" => "orderCtrl"} - %div{"ng-show" => "distributionChosen()"} - = render 'add_product' - - - unless @order.line_items.any? - %div - = render 'form' + = form_for @order, url: spree.admin_orders_path, method: :post do + = render 'spree/admin/orders/_form/distribution_fields' + = button_tag class: 'secondary radius expand small', id: 'update-button' do + %i.icon-arrow-right + = t(:next) diff --git a/app/views/spree/admin/shared/_routes.html.erb b/app/views/spree/admin/shared/_routes.html.erb index dc8fac6b9d..27adba0559 100644 --- a/app/views/spree/admin/shared/_routes.html.erb +++ b/app/views/spree/admin/shared/_routes.html.erb @@ -10,6 +10,6 @@ :taxons_search => main_app.api_v0_taxons_url(:format => 'json'), :orders_api => main_app.api_v0_orders_url, :states_search => main_app.api_v0_states_url(:format => 'json'), - :cancel_order => spree.fire_admin_order_url(id: @order ? @order.number : "", e: 'cancel') + :cancel_order => spree.fire_admin_order_url(id: @order&.number ? @order.number : "", e: 'cancel') }.to_json %>; diff --git a/config/routes/spree.rb b/config/routes/spree.rb index 28834db308..a6e4bab5ed 100644 --- a/config/routes/spree.rb +++ b/config/routes/spree.rb @@ -89,8 +89,6 @@ Spree::Core::Engine.routes.draw do get :resend get :invoice get :print - get :distribution - put :distribution end collection do diff --git a/spec/system/admin/order_spec.rb b/spec/system/admin/order_spec.rb index 366c2f2a9a..8f77ff4f9d 100644 --- a/spec/system/admin/order_spec.rb +++ b/spec/system/admin/order_spec.rb @@ -116,33 +116,6 @@ describe ' end end - context "when order has a distributor and order cycle" do - before do - order.distributor = distributor - order.order_cycle = order_cycle - order.save! - login_as_admin - visit spree.distribution_admin_order_path(order) - end - - it "can access the `/distribution` step" do - expect(current_path).to eq spree.distribution_admin_order_path(order) - expect(page).to have_content "DISTRIBUTION" - end - - it "shows the distributor and order cycle" do - expect(page).to have_content distributor.name - expect(page).to have_content order_cycle.name - end - - it "shows links to other steps" do - expect(page).to have_content "CUSTOMER DETAILS" - expect(page).to have_content "ORDER DETAILS" - expect(page).to have_content "PAYMENTS" - expect(page).to have_content "ADJUSTMENTS" - end - end - context "when creating an order with a customer-only" do let(:customer2) { create(:customer, enterprise: distributor) } let(:customer3) { create(:customer, enterprise: distributor) }