Skip to content

Commit

Permalink
notify the user if any order cannot be invoiced on bulk invoice sending
Browse files Browse the repository at this point in the history
  • Loading branch information
abdellani committed May 13, 2024
1 parent 4a3f413 commit 3161b0f
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions app/reflexes/admin/orders_reflex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 3161b0f

Please sign in to comment.