diff --git a/app/models/credit_note.rb b/app/models/credit_note.rb index 8e57a9ee520..ce08d9edba7 100644 --- a/app/models/credit_note.rb +++ b/app/models/credit_note.rb @@ -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 diff --git a/app/models/credit_note/applied_tax.rb b/app/models/credit_note/applied_tax.rb index e98212becd1..97db147b9c1 100644 --- a/app/models/credit_note/applied_tax.rb +++ b/app/models/credit_note/applied_tax.rb @@ -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 @@ -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 # diff --git a/db/migrate/20240910093646_change_tax_id_null_on_credit_note_applied_taxes.rb b/db/migrate/20240910093646_change_tax_id_null_on_credit_note_applied_taxes.rb new file mode 100644 index 00000000000..cdd270d069c --- /dev/null +++ b/db/migrate/20240910093646_change_tax_id_null_on_credit_note_applied_taxes.rb @@ -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 diff --git a/db/migrate/20240910111203_update_indexes_for_credit_notes_taxes.rb b/db/migrate/20240910111203_update_indexes_for_credit_notes_taxes.rb new file mode 100644 index 00000000000..8066647aabb --- /dev/null +++ b/db/migrate/20240910111203_update_indexes_for_credit_notes_taxes.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index df5ce76e902..24ce2b9c1c2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_09_06_170048) do +ActiveRecord::Schema[7.1].define(version: 2024_09_10_111203) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -360,7 +360,7 @@ create_table "credit_notes_taxes", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.uuid "credit_note_id", null: false - t.uuid "tax_id", null: false + t.uuid "tax_id" t.string "tax_description" t.string "tax_code", null: false t.string "tax_name", null: false @@ -370,8 +370,9 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.bigint "base_amount_cents", default: 0, null: false - t.index ["credit_note_id", "tax_id"], name: "index_credit_notes_taxes_on_credit_note_id_and_tax_id", unique: true + t.index ["credit_note_id", "tax_code"], name: "index_credit_notes_taxes_on_credit_note_id_and_tax_code", unique: true t.index ["credit_note_id"], name: "index_credit_notes_taxes_on_credit_note_id" + t.index ["tax_code"], name: "index_credit_notes_taxes_on_tax_code" t.index ["tax_id"], name: "index_credit_notes_taxes_on_tax_id" end diff --git a/spec/models/credit_note/applied_tax_spec.rb b/spec/models/credit_note/applied_tax_spec.rb index 94e63400057..485304444a8 100644 --- a/spec/models/credit_note/applied_tax_spec.rb +++ b/spec/models/credit_note/applied_tax_spec.rb @@ -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 diff --git a/spec/models/credit_note_spec.rb b/spec/models/credit_note_spec.rb index bb8d638db2b..9ad7395c2e7 100644 --- a/spec/models/credit_note_spec.rb +++ b/spec/models/credit_note_spec.rb @@ -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) }