diff --git a/app/reflexes/admin/orders_reflex.rb b/app/reflexes/admin/orders_reflex.rb index b52136391e05..66c0eebd31d2 100644 --- a/app/reflexes/admin/orders_reflex.rb +++ b/app/reflexes/admin/orders_reflex.rb @@ -93,15 +93,15 @@ def resend_confirmation_emails(params) end def send_invoices(params) - count = 0 - editable_orders.invoiceable.where(id: params[:bulk_ids]).find_each do |o| - next unless o.distributor.can_invoice? + orders = editable_orders.invoiceable.where(id: params[:bulk_ids]) + return if any_order_cannot_be_invoiced(orders) + + orders.each do |o| Spree::OrderMailer.invoice_email(o.id, current_user_id: current_user.id).deliver_later - count += 1 end - success("admin.send_invoice_feedback", count) + success("admin.send_invoice_feedback", orders.size) end private @@ -133,5 +133,21 @@ def render_business_number_required_error(distributors) enterprise_name: distributor_names.join(", ")) morph_admin_flashes end + + def any_order_cannot_be_invoiced(orders) + distributors = distributors_who_cannot_invoice(orders) + return false if distributors.empty? + + distributor_names = distributors.map(&:name).join(",") + flash[:error] = I18n.t(:must_have_valid_business_number, enterprise_name: distributor_names) + true + end + + def distributors_who_cannot_invoice(orders) + orders.map do |order| + order.distributor + end.reject(&:can_invoice?) + .uniq + end end end