Merge pull request #12219 from abdellani/prevent-generate-invoices-when-distributor-cannot-invoice

prevent generating invoices when order's distributor can't generate invoices
This commit is contained in:
Rachel Arnould
2024-04-25 12:18:49 +02:00
committed by GitHub
2 changed files with 27 additions and 2 deletions

View File

@@ -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

View File

@@ -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