Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: closed invoices with generated number #2646

Merged
merged 7 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Invoice < ApplicationRecord
VISIBLE_STATUS = {draft: 0, finalized: 1, voided: 2, failed: 4}.freeze
INVISIBLE_STATUS = {generating: 3, open: 5, closed: 6}.freeze
STATUS = VISIBLE_STATUS.merge(INVISIBLE_STATUS).freeze
COMPLETE_INVOICE_STATUSES = %w[finalized closed].freeze

enum invoice_type: INVOICE_TYPES
enum payment_status: PAYMENT_STATUS, _prefix: :payment
Expand Down
2 changes: 1 addition & 1 deletion app/services/invoices/calculate_fees_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def fetch_taxes_for_invoice
end

def finalizing_invoice?
context == :finalize || invoice.status == 'finalized'
context == :finalize || Invoice::COMPLETE_INVOICE_STATUSES.include?(invoice.status)
end
end
end
16 changes: 9 additions & 7 deletions app/services/invoices/subscription_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ def call
result.invoice = invoice

fee_result = ActiveRecord::Base.transaction do
invoice.status = invoice_status
context = grace_period? ? :draft : :finalize
fee_result = Invoices::CalculateFeesService.call(
annvelents marked this conversation as resolved.
Show resolved Hide resolved
invoice:,
recurring:
recurring:,
context:
)
if invoice.status == "finalized"
Invoices::TransitionToFinalStatusService.call(invoice:)
end

set_invoice_generated_status unless invoice.failed?
invoice.save!

# NOTE: We don't want to raise error and corrupt DB commit if there is tax error.
Expand Down Expand Up @@ -123,8 +123,10 @@ def grace_period?
@grace_period ||= customer.applicable_invoice_grace_period.positive?
end

def invoice_status
grace_period? ? :draft : :finalized
def set_invoice_generated_status
return invoice.status = :draft if grace_period?

Invoices::TransitionToFinalStatusService.call(invoice:)
end

def should_deliver_finalized_email?
Expand Down
2 changes: 2 additions & 0 deletions spec/services/invoices/subscription_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@
it 'creates an invoice in :finalized status' do
result = invoice_service.call
expect(result.invoice.status).to eq('finalized')
expect(result.invoice.number).not_to include('DRAFT')
end
end

Expand All @@ -353,6 +354,7 @@
it 'creates an invoice in :closed status' do
result = invoice_service.call
expect(result.invoice.status).to eq('closed')
expect(result.invoice.number).to include('DRAFT')
end

context 'when organization gas grace period' do
Expand Down
Loading