From 9e808cb9b460081d38a22a8df8f381c9cfa879ab Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 11 Dec 2023 09:42:55 +0100 Subject: [PATCH] check ABN before bulk printing --- app/reflexes/admin/orders_reflex.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/app/reflexes/admin/orders_reflex.rb b/app/reflexes/admin/orders_reflex.rb index 765438d7cc3f..a249f48233ce 100644 --- a/app/reflexes/admin/orders_reflex.rb +++ b/app/reflexes/admin/orders_reflex.rb @@ -27,13 +27,16 @@ def ship end def bulk_invoice(params) + visible_orders = editable_orders.where(id: params[:bulk_ids]) + return unless all_distributors_can_invoice?(visible_orders) + cable_ready.append( selector: "#orders-index", html: render(partial: "spree/admin/orders/bulk/invoice_modal") ).broadcast BulkInvoiceJob.perform_later( - params[:bulk_ids], + visible_orders.pluck(:id), "tmp/invoices/#{Time.zone.now.to_i}-#{SecureRandom.hex(2)}.pdf", channel: SessionChannel.for_request(request), current_user_id: current_user.id @@ -96,5 +99,16 @@ def success(i18n_key, count) def editable_orders Permissions::Order.new(current_user).editable_orders end + + def all_distributors_can_invoice?(orders) + distributors = orders.map(&:distributor).uniq.reject(&:can_invoice?) + + return true if distributors.empty? + + flash[:error] = I18n.t(:must_have_valid_business_number, + enterprise_name: distributors.map(&:name).join(", ")) + morph_admin_flashes + false + end end end