diff --git a/app/controllers/spree/admin/invoices_controller.rb b/app/controllers/spree/admin/invoices_controller.rb index 8fa92475a3..701652e031 100644 --- a/app/controllers/spree/admin/invoices_controller.rb +++ b/app/controllers/spree/admin/invoices_controller.rb @@ -19,8 +19,13 @@ module Spree def generate @order = Order.find_by(number: params[:order_id]) - authorize! :invoice, @order - ::Orders::GenerateInvoiceService.new(@order).generate_or_update_latest_invoice + if @order.distributor.can_invoice? + authorize! :invoice, @order + ::Orders::GenerateInvoiceService.new(@order).generate_or_update_latest_invoice + else + flash[:error] = t(:must_have_valid_business_number, + enterprise_name: @order.distributor.name) + end redirect_back(fallback_location: spree.admin_dashboard_path) end diff --git a/spec/controllers/spree/admin/orders/invoices_spec.rb b/spec/controllers/spree/admin/orders/invoices_spec.rb index 2a7b3b00c1..4aea9fd065 100644 --- a/spec/controllers/spree/admin/orders/invoices_spec.rb +++ b/spec/controllers/spree/admin/orders/invoices_spec.rb @@ -158,6 +158,10 @@ describe Spree::Admin::InvoicesController, type: :controller do let(:distributor) { order.distributor } let(:params) { { order_id: order.number } } + before do + distributor.update_attribute(:abn, "123412341234") + end + context "as a normal user" do before { allow(controller).to receive(:spree_current_user) { user } } @@ -193,6 +197,22 @@ describe Spree::Admin::InvoicesController, type: :controller do expect(response).to redirect_to spree.admin_dashboard_path end + + context "distributor didn't set an ABN" do + before do + distributor.update_attribute(:abn, "") + end + + it "should not allow me to generate a new invoice for the order" do + expect do + spree_get :generate, params + end.to change{ Invoice.count }.by(0) + + expect(response).to redirect_to spree.admin_dashboard_path + expect(flash[:error]) + .to eq "#{distributor.name} must have a valid ABN before invoices can be used." + end + end end end end