diff --git a/app/services/order_invoice_generator.rb b/app/services/order_invoice_generator.rb index 4e02ccf487..c1fba01a28 100644 --- a/app/services/order_invoice_generator.rb +++ b/app/services/order_invoice_generator.rb @@ -13,7 +13,7 @@ class OrderInvoiceGenerator data: invoice_data ) elsif comparator.can_update_latest_invoice? - order.invoices.last.update!( + order.invoices.first.update!( date: Time.zone.today, data: invoice_data ) diff --git a/spec/services/order_invoice_generator_spec.rb b/spec/services/order_invoice_generator_spec.rb index 852de2028a..24b83143de 100644 --- a/spec/services/order_invoice_generator_spec.rb +++ b/spec/services/order_invoice_generator_spec.rb @@ -43,6 +43,19 @@ describe OrderInvoiceGenerator do expect{ subject }.to change{ latest_invoice.reload.data } .and change{ order.invoices.count }.by(0) end + + context "when there are more than one invoice" do + before do + latest_invoice.update!(number: 2, created_at: 1.day.ago) + create(:invoice, order:, number: 1, created_at: 2.days.ago) + end + it "should update the most recent invoice" do + expect{ subject }.to change{ latest_invoice.reload.data } + .and change{ latest_invoice.date }.to(Time.zone.today) + .and change{ latest_invoice.number }.by(0) + .and change{ order.invoices.count }.by(0) + end + end end context "when can't generate new invoice or update latest invoice" do