Skip to content

Commit ec6cdb8

Browse files
authored
The invoice number is not available for use after an invoice is delete bug fix (#1965)
* The invoice number is not available for use after an invoice is deleted bug fix * Added rspec for discard number update * Fixed security warning GHSA-mrxw-mxhj-p664 * Add test case for invoice to reuse existing invoince number
1 parent 905110e commit ec6cdb8

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

Gemfile.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ GEM
351351
memoist (0.16.2)
352352
mini_magick (4.12.0)
353353
mini_mime (1.1.5)
354+
mini_portile2 (2.8.8)
354355
minitest (5.25.4)
355356
mission_control-jobs (0.2.1)
356357
importmap-rails
@@ -375,6 +376,9 @@ GEM
375376
net-protocol
376377
newrelic_rpm (9.8.0)
377378
nio4r (2.7.3)
379+
nokogiri (1.18.7)
380+
mini_portile2 (~> 2.8.2)
381+
racc (~> 1.4)
378382
nokogiri (1.18.7-arm64-darwin)
379383
racc (~> 1.4)
380384
nokogiri (1.18.7-x86_64-darwin)
@@ -669,6 +673,7 @@ PLATFORMS
669673
arm64-darwin-21
670674
arm64-darwin-22
671675
arm64-darwin-23
676+
ruby
672677
x86_64-darwin-21
673678
x86_64-darwin-22
674679
x86_64-darwin-23

app/models/invoice.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class Invoice < ApplicationRecord
7878
after_commit :refresh_invoice_index
7979
after_save :lock_timesheet_entries, if: :draft?
8080
after_discard :unlock_timesheet_entries, if: :draft?
81+
after_discard :update_invoice_number
8182

8283
validates :issue_date, :due_date, :invoice_number, presence: true
8384
validates :due_date, comparison: { greater_than_or_equal_to: :issue_date }, if: :not_waived
@@ -95,6 +96,7 @@ class Invoice < ApplicationRecord
9596
scope :during, -> (duration) {
9697
where(issue_date: duration) if duration.present?
9798
}
99+
scope :active, -> { where(discarded_at: nil) }
98100

99101
delegate :name, to: :client, prefix: :client
100102
delegate :email, to: :client, prefix: :client
@@ -103,6 +105,8 @@ class Invoice < ApplicationRecord
103105
searchkick filterable: [:issue_date, :created_at, :updated_at, :client_name, :status, :invoice_number ],
104106
word_middle: [:invoice_number, :client_name]
105107

108+
ARCHIVED_PREFIX = "ARC"
109+
106110
def search_data
107111
{
108112
id: id.to_i,
@@ -215,4 +219,8 @@ def unlock_timesheet_entries
215219
def same_currency?
216220
client&.currency == company&.base_currency
217221
end
222+
223+
def update_invoice_number
224+
self.update(invoice_number: "#{ARCHIVED_PREFIX}-#{id}-#{invoice_number}")
225+
end
218226
end

app/views/internal_api/v1/partial/_client_list.json.jbuilder

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ json.email client.email
66
json.phone client.phone
77
json.address client.current_address
88
json.currency client.currency
9-
json.previousInvoiceNumber client.invoices&.last&.invoice_number || 0
9+
json.previousInvoiceNumber client.invoices.active.last&.invoice_number || 0
1010
json.client_members client.send_invoice_emails(@virtual_verified_invitations_allowed)
1111
json.logo client[:logo] ? polymorphic_url(client[:logo]) : ""

spec/models/invoice_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@
7171
@current_invoice.discard!
7272
expect { @current_invoice.discard! }.to raise_error(Discard::RecordNotDiscarded)
7373
end
74+
75+
it "update invoice number" do
76+
@current_invoice.discard!
77+
expect(@current_invoice.invoice_number).to start_with(
78+
"#{Invoice::ARCHIVED_PREFIX}-#{@current_invoice.id}-"
79+
)
80+
end
81+
82+
it "reuses invoice number after discard for new invoice" do
83+
invoice_number = @current_invoice.invoice_number
84+
@current_invoice.discard!
85+
new_invoice = create(:invoice, client: @current_invoice.client, company:, invoice_number:)
86+
expect(new_invoice.invoice_number).to eq(invoice_number)
87+
end
7488
end
7589

7690
describe "Scopes" do

0 commit comments

Comments
 (0)