diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 33c556caf01..0bd0abdeb82 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -295,13 +295,13 @@ def creditable_amount_cents def refundable_amount_cents return 0 if version_number < CREDIT_NOTES_MIN_VERSION || draft? || !payment_succeeded? - base_amount = if credit? - available_to_credit_amount_cents - else - total_paid_amount_cents - credit_notes.sum("refund_amount_cents + credit_amount_cents") - end + # base_amount = if credit? + # available_to_credit_amount_cents + # else + # total_paid_amount_cents - credit_notes.sum("refund_amount_cents + credit_amount_cents") + # end - amount = base_amount - + amount = available_to_credit_amount_cents - credits.where(before_taxes: false).sum(:amount_cents) - prepaid_credit_amount_cents amount = amount.negative? ? 0 : amount diff --git a/app/models/payment.rb b/app/models/payment.rb index 4aef045a1c9..b7b38cc26e0 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -90,7 +90,7 @@ def payment_request_succeeded # Indexes # # index_payments_on_invoice_id (invoice_id) -# index_payments_on_payable_id_and_payable_type (payable_id,payable_type) UNIQUE WHERE (payable_payment_status = ANY (ARRAY['pending'::payment_payable_payment_status, 'processing'::payment_payable_payment_status])) +# index_payments_on_payable_id_and_payable_type (payable_id,payable_type) UNIQUE WHERE ((payable_payment_status = ANY (ARRAY['pending'::payment_payable_payment_status, 'processing'::payment_payable_payment_status])) AND (payment_type = 'provider'::payment_type)) # index_payments_on_payable_type_and_payable_id (payable_type,payable_id) # index_payments_on_payment_provider_customer_id (payment_provider_customer_id) # index_payments_on_payment_provider_id (payment_provider_id) diff --git a/app/services/invoices/advance_charges_service.rb b/app/services/invoices/advance_charges_service.rb index 72e8aa5f429..a9c978e2230 100644 --- a/app/services/invoices/advance_charges_service.rb +++ b/app/services/invoices/advance_charges_service.rb @@ -57,9 +57,9 @@ def create_manual_payment(invoice) reference = I18n.t("invoice.charges_paid_in_advance") created_at = invoice.created_at - params = {invoice_id: invoice.id, amount_cents:, reference:, created_at:, skip_checks: true} + params = {invoice_id: invoice.id, amount_cents:, reference:, created_at:} - ManualPayments::CreateJob.perform_later(organization:, params:) + ManualPayments::CreateJob.perform_later(organization:, params:, skip_checks: true) end def create_group_invoice diff --git a/config/locales/en/invoice.yml b/config/locales/en/invoice.yml index c9aa12c34f9..0b8a3ad32f4 100644 --- a/config/locales/en/invoice.yml +++ b/config/locales/en/invoice.yml @@ -71,7 +71,7 @@ en: subscription: Subscription subscription_interval: "%{plan_interval} subscription - %{plan_name}" tax: Tax - tax_identification_number: "Tax ID: %{tax_identification_number}" + tax_identification_number: 'Tax ID: %{tax_identification_number}' tax_name: "%{name} (%{rate}% on %{amount})" tax_name_only: customer_exempt: Customer is tax exempt @@ -89,11 +89,11 @@ en: tax_exempt: Customer is tax exempt. total: Total total_credits: Total credits - total_credits_with_value: "Total credits: %{credit_amount} credits" + total_credits_with_value: 'Total credits: %{credit_amount} credits' total_due: Total due - total_events: "Total events: %{count}" - total_unit: "Total unit: %{units}" - total_unit_interval: "Total unit: %{events_count} events for %{units}" + total_events: 'Total events: %{count}' + total_unit: 'Total unit: %{units}' + total_unit_interval: 'Total unit: %{events_count} events for %{units}' true_up_details: Minimum spend of %{min_amount} prorated on days of usage true_up_metric: "%{metric} - True-up" unit: Unit diff --git a/db/migrate/20250122130735_change_unique_index_in_payments.rb b/db/migrate/20250122130735_change_unique_index_in_payments.rb new file mode 100644 index 00000000000..22b29869584 --- /dev/null +++ b/db/migrate/20250122130735_change_unique_index_in_payments.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +class ChangeUniqueIndexInPayments < ActiveRecord::Migration[7.1] + disable_ddl_transaction! + + def up + remove_index :payments, %i[payable_id payable_type] + + add_index :payments, + %i[payable_id payable_type], + where: "payable_payment_status in ('pending', 'processing') and payment_type = 'provider'", + unique: true, + algorithm: :concurrently + end + + def down + remove_index :payments, %i[payable_id payable_type] + + add_index :payments, + %i[payable_id payable_type], + where: "payable_payment_status in ('pending', 'processing')", + unique: true, + algorithm: :concurrently + end +end diff --git a/db/schema.rb b/db/schema.rb index dde3de5ef3b..e54ca90d145 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: 2025_01_20_151959) do +ActiveRecord::Schema[7.1].define(version: 2025_01_22_130735) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -1126,7 +1126,7 @@ t.enum "payment_type", default: "provider", null: false, enum_type: "payment_type" t.string "reference" t.index ["invoice_id"], name: "index_payments_on_invoice_id" - t.index ["payable_id", "payable_type"], name: "index_payments_on_payable_id_and_payable_type", unique: true, where: "(payable_payment_status = ANY (ARRAY['pending'::payment_payable_payment_status, 'processing'::payment_payable_payment_status]))" + t.index ["payable_id", "payable_type"], name: "index_payments_on_payable_id_and_payable_type", unique: true, where: "((payable_payment_status = ANY (ARRAY['pending'::payment_payable_payment_status, 'processing'::payment_payable_payment_status])) AND (payment_type = 'provider'::payment_type))" t.index ["payable_type", "payable_id"], name: "index_payments_on_payable_type_and_payable_id" t.index ["payment_provider_customer_id"], name: "index_payments_on_payment_provider_customer_id" t.index ["payment_provider_id"], name: "index_payments_on_payment_provider_id" diff --git a/spec/models/invoice_spec.rb b/spec/models/invoice_spec.rb index 7569a689b4a..bf58f95fe38 100644 --- a/spec/models/invoice_spec.rb +++ b/spec/models/invoice_spec.rb @@ -1470,7 +1470,7 @@ let(:payment_status) { :succeeded } it 'returns the correct refundable amount' do - expect(invoice.refundable_amount_cents).to eq(700) + expect(invoice.refundable_amount_cents).to eq(800) end end diff --git a/spec/queries/payments_query_spec.rb b/spec/queries/payments_query_spec.rb index 2b71d72d39c..be00de888cb 100644 --- a/spec/queries/payments_query_spec.rb +++ b/spec/queries/payments_query_spec.rb @@ -13,9 +13,10 @@ let(:membership) { create(:membership) } let(:organization) { membership.organization } let(:invoice) { create(:invoice, organization:) } + let(:invoice2) { create(:invoice, organization:) } let(:payment_request) { create(:payment_request, organization:) } let(:payment_one) { create(:payment, payable: invoice) } - let(:payment_two) { create(:payment, payable: invoice) } + let(:payment_two) { create(:payment, payable: invoice2) } let(:payment_three) { create(:payment, payable: payment_request) } before do @@ -51,9 +52,9 @@ it "returns only payments for the specified invoice" do expect(result).to be_success - expect(returned_ids.count).to eq(2) + expect(returned_ids.count).to eq(1) expect(returned_ids).to include(payment_one.id) - expect(returned_ids).to include(payment_two.id) + expect(returned_ids).not_to include(payment_two.id) expect(returned_ids).not_to include(payment_three.id) end end diff --git a/spec/requests/api/v1/payments_controller_spec.rb b/spec/requests/api/v1/payments_controller_spec.rb index f32d50e01b7..21be029293d 100644 --- a/spec/requests/api/v1/payments_controller_spec.rb +++ b/spec/requests/api/v1/payments_controller_spec.rb @@ -54,9 +54,10 @@ it "returns organization's payments", :aggregate_failures do invoice = create(:invoice, organization:) + invoice2 = create(:invoice, organization:) payment_request = create(:payment_request, organization:) first_payment = create(:payment, payable: invoice) - second_payment = create(:payment, payable: invoice) + second_payment = create(:payment, payable: invoice2) third_payment = create(:payment, payable: payment_request) subject diff --git a/spec/services/invoices/advance_charges_service_spec.rb b/spec/services/invoices/advance_charges_service_spec.rb index 2f469fd2e3a..93ef0376a2a 100644 --- a/spec/services/invoices/advance_charges_service_spec.rb +++ b/spec/services/invoices/advance_charges_service_spec.rb @@ -118,9 +118,9 @@ def fee_boundaries invoice_id: result.invoice.id, amount_cents: result.invoice.total_amount_cents, reference:, - created_at: result.invoice.created_at, - skip_checks: true - } + created_at: result.invoice.created_at + }, + skip_checks: true ) end end