Skip to content

Commit

Permalink
Feat: anrok <> credit_notes model changes (#2558)
Browse files Browse the repository at this point in the history
We need to adjust credit_note and credit_note::applied_tax to reflect
the changes in processing taxes:
- applied_tax might not belong to a stored in lago tax, so tax_id
becomes optional,
- during reporting we might receive some errors, so credit_note can have
many :error_details
  • Loading branch information
annvelents committed Sep 11, 2024
1 parent 3bfa4f7 commit d2f5348
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/models/credit_note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class CreditNote < ApplicationRecord
has_many :applied_taxes, class_name: 'CreditNote::AppliedTax', dependent: :destroy
has_many :taxes, through: :applied_taxes
has_many :integration_resources, as: :syncable
has_many :error_details, as: :owner, dependent: :destroy

has_one_attached :file

Expand Down
11 changes: 6 additions & 5 deletions app/models/credit_note/applied_tax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class AppliedTax < ApplicationRecord
include PaperTrailTraceable

belongs_to :credit_note
belongs_to :tax
belongs_to :tax, optional: true

monetize :amount_cents
monetize :base_amount_cents, with_model_currency: :amount_currency
Expand All @@ -29,13 +29,14 @@ class AppliedTax < ApplicationRecord
# created_at :datetime not null
# updated_at :datetime not null
# credit_note_id :uuid not null
# tax_id :uuid not null
# tax_id :uuid
#
# Indexes
#
# index_credit_notes_taxes_on_credit_note_id (credit_note_id)
# index_credit_notes_taxes_on_credit_note_id_and_tax_id (credit_note_id,tax_id) UNIQUE
# index_credit_notes_taxes_on_tax_id (tax_id)
# index_credit_notes_taxes_on_credit_note_id (credit_note_id)
# index_credit_notes_taxes_on_credit_note_id_and_tax_code (credit_note_id,tax_code) UNIQUE
# index_credit_notes_taxes_on_tax_code (tax_code)
# index_credit_notes_taxes_on_tax_id (tax_id)
#
# Foreign Keys
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class ChangeTaxIdNullOnCreditNoteAppliedTaxes < ActiveRecord::Migration[7.1]
def change
change_column_null :credit_notes_taxes, :tax_id, true
end
end
12 changes: 12 additions & 0 deletions db/migrate/20240910111203_update_indexes_for_credit_notes_taxes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class UpdateIndexesForCreditNotesTaxes < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

def change
remove_index :credit_notes_taxes, %i[credit_note_id tax_id], unique: true, algorithm: :concurrently

add_index :credit_notes_taxes, :tax_code, algorithm: :concurrently
add_index :credit_notes_taxes, %i[credit_note_id tax_code], unique: true, algorithm: :concurrently
end
end
7 changes: 4 additions & 3 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions spec/models/credit_note/applied_tax_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
RSpec.describe CreditNote::AppliedTax, type: :model do
subject(:applied_tax) { create(:credit_note_applied_tax) }

describe 'associations' do
it { is_expected.to belong_to(:credit_note) }
it { is_expected.to belong_to(:tax).optional }
end

it_behaves_like 'paper_trail traceable'
end
1 change: 1 addition & 0 deletions spec/models/credit_note_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
it_behaves_like 'paper_trail traceable'

it { is_expected.to have_many(:integration_resources) }
it { is_expected.to have_many(:error_details) }

describe 'sequential_id' do
let(:invoice) { create(:invoice) }
Expand Down

0 comments on commit d2f5348

Please sign in to comment.