use Invoice#latest to fetch the most recent invoice

This commit is contained in:
Mohamed ABDELLANI
2023-08-23 11:57:30 +01:00
parent aaf1d22ccc
commit 928494af23
2 changed files with 5 additions and 1 deletions

View File

@@ -9,6 +9,10 @@ class Invoice < ApplicationRecord
after_create :cancel_previous_invoices
default_scope { order(created_at: :desc) }
def self.latest
reorder(created_at: :desc).first
end
def presenter
@presenter ||= Invoice::DataPresenter.new(self)
end

View File

@@ -13,7 +13,7 @@ class OrderInvoiceGenerator
data: invoice_data
)
elsif comparator.can_update_latest_invoice?
order.invoices.first.update!(
order.invoices.latest.update!(
date: Time.zone.today,
data: invoice_data
)