Skip to content

Commit

Permalink
prevent generating invoices when order's distributor can't generate i…
Browse files Browse the repository at this point in the history
…nvoices
  • Loading branch information
abdellani committed Feb 29, 2024
1 parent a9d5869 commit ee1f608
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
10 changes: 8 additions & 2 deletions app/controllers/spree/admin/invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ def show

def generate
@order = Order.find_by(number: params[:order_id])
authorize! :invoice, @order
OrderInvoiceGenerator.new(@order).generate_or_update_latest_invoice
if @order.distributor.can_invoice?
authorize! :invoice, @order
OrderInvoiceGenerator.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

Expand Down
20 changes: 20 additions & 0 deletions spec/controllers/spree/admin/orders/invoices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@
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 } }

Expand Down Expand Up @@ -193,6 +197,22 @@

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
Expand Down

0 comments on commit ee1f608

Please sign in to comment.