diff --git a/app/services/order_invoice_generator.rb b/app/services/order_invoice_generator.rb index a6d30b33d0cd..1d91fec8da93 100644 --- a/app/services/order_invoice_generator.rb +++ b/app/services/order_invoice_generator.rb @@ -9,7 +9,7 @@ def generate_or_update_latest_invoice if comparator.can_generate_new_invoice? order.invoices.create!( date: Time.zone.today, - number: order.invoices.count + 1, + number: total_invoices_created_by_distributor + 1, data: invoice_data ) elsif comparator.can_update_latest_invoice? @@ -31,4 +31,8 @@ def comparator def invoice_data @invoice_data ||= InvoiceDataGenerator.new(order).generate end + + def total_invoices_created_by_distributor + Invoice.joins(:order).where(order: { distributor: order.distributor }).count + end end