prevent generating invoices when order's distributor can't generate invoices

This commit is contained in:
Mohamed ABDELLANI
2024-02-29 18:32:18 +01:00
parent a9d586952d
commit ee1f60808e
2 changed files with 28 additions and 2 deletions

View File

@@ -19,8 +19,14 @@ module Spree
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

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