diff --git a/app/models/applied_usage_threshold.rb b/app/models/applied_usage_threshold.rb index 0bdcd5e9e220..fecd2e1b8277 100644 --- a/app/models/applied_usage_threshold.rb +++ b/app/models/applied_usage_threshold.rb @@ -1,21 +1,25 @@ # frozen_string_literal: true class AppliedUsageThreshold < ApplicationRecord - belongs_to :usage_threshold + belongs_to :usage_threshold, -> { with_discarded } belongs_to :invoice validates :usage_threshold_id, uniqueness: {scope: :invoice_id} + + monetize :lifetime_usage_amount_cents, + with_currency: ->(applied_usage_threshold) { applied_usage_threshold.invoice.currency } end # == Schema Information # # Table name: applied_usage_thresholds # -# id :uuid not null, primary key -# created_at :datetime not null -# updated_at :datetime not null -# invoice_id :uuid not null -# usage_threshold_id :uuid not null +# id :uuid not null, primary key +# lifetime_usage_amount_cents :bigint default(0), not null +# created_at :datetime not null +# updated_at :datetime not null +# invoice_id :uuid not null +# usage_threshold_id :uuid not null # # Indexes # diff --git a/app/models/lifetime_usage.rb b/app/models/lifetime_usage.rb index bacb1b270d90..44dc38d3f052 100644 --- a/app/models/lifetime_usage.rb +++ b/app/models/lifetime_usage.rb @@ -14,11 +14,16 @@ class LifetimeUsage < ApplicationRecord monetize :current_usage_amount_cents, :invoiced_usage_amount_cents, + :historical_usage_amount_cents, with_currency: ->(lifetime_usage) { lifetime_usage.subscription.plan.amount_currency } default_scope -> { kept } scope :needs_recalculation, -> { where(recalculate_current_usage: true).or(where(recalculate_invoiced_usage: true)) } + + def total_amount_cents + historical_usage_amount_cents + invoiced_usage_amount_cents + current_usage_amount_cents + end end # == Schema Information diff --git a/app/services/invoices/progressive_billing_service.rb b/app/services/invoices/progressive_billing_service.rb index 9295b129e3df..c146f6e34756 100644 --- a/app/services/invoices/progressive_billing_service.rb +++ b/app/services/invoices/progressive_billing_service.rb @@ -23,7 +23,6 @@ def call Credits::AppliedCouponsService.call(invoice:) Invoices::ComputeAmountsFromFees.call(invoice:) - create_credit_note_credit create_applied_prepaid_credit invoice.payment_status = invoice.total_amount_cents.positive? ? :pending : :succeeded @@ -109,19 +108,19 @@ def boundaries end def create_applied_usage_thresholds - usage_thresholds.each { AppliedUsageThreshold.create!(invoice:, usage_threshold: _1) } + usage_thresholds.each do + AppliedUsageThreshold.create!( + invoice:, + usage_threshold: _1, + lifetime_usage_amount_cents: lifetime_usage.total_amount_cents + ) + end end def should_deliver_email? License.premium? && subscription.organization.email_settings.include?('invoice.finalized') end - def create_credit_note_credit - credit_result = Credits::CreditNoteService.call(invoice:).raise_if_error! - - invoice.total_amount_cents -= credit_result.credits.sum(&:amount_cents) if credit_result.credits - end - def create_applied_prepaid_credit wallet = subscription.customer.wallets.active.first return unless wallet&.active? diff --git a/db/migrate/20240823092643_create_applied_usage_thresholds.rb b/db/migrate/20240823092643_create_applied_usage_thresholds.rb index e3f45667fb20..e10c85e7abaa 100644 --- a/db/migrate/20240823092643_create_applied_usage_thresholds.rb +++ b/db/migrate/20240823092643_create_applied_usage_thresholds.rb @@ -5,6 +5,7 @@ def change create_table :applied_usage_thresholds, id: :uuid do |t| t.references :usage_threshold, null: false, foreign_key: true, type: :uuid t.references :invoice, null: false, foreign_key: true, type: :uuid + t.bigint :lifetime_usage_amount_cents, null: false, default: 0 t.timestamps diff --git a/db/schema.rb b/db/schema.rb index 42336df9a8ed..eeefd1d56cc7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -132,6 +132,7 @@ create_table "applied_usage_thresholds", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.uuid "usage_threshold_id", null: false t.uuid "invoice_id", null: false + t.bigint "lifetime_usage_amount_cents", default: 0, null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["invoice_id"], name: "index_applied_usage_thresholds_on_invoice_id" diff --git a/spec/models/lifetime_usage_spec.rb b/spec/models/lifetime_usage_spec.rb index 73a15a3f9c39..97eae27700c2 100644 --- a/spec/models/lifetime_usage_spec.rb +++ b/spec/models/lifetime_usage_spec.rb @@ -60,4 +60,14 @@ expect(described_class.needs_recalculation).to match_array([lifetime_usage1, lifetime_usage2]) end end + + describe '#total_amount_cents' do + it 'returns the sum of the historical, invoiced, and current usage' do + lifetime_usage.historical_usage_amount_cents = 100 + lifetime_usage.invoiced_usage_amount_cents = 200 + lifetime_usage.current_usage_amount_cents = 300 + + expect(lifetime_usage.total_amount_cents).to eq(600) + end + end end diff --git a/spec/services/invoices/progressive_billing_service_spec.rb b/spec/services/invoices/progressive_billing_service_spec.rb index 2404b2f9a8ef..f3b4f724ee1e 100644 --- a/spec/services/invoices/progressive_billing_service_spec.rb +++ b/spec/services/invoices/progressive_billing_service_spec.rb @@ -63,6 +63,9 @@ expect(invoice.invoice_subscriptions.count).to eq(1) expect(invoice.fees.count).to eq(1) expect(invoice.applied_usage_thresholds.count).to eq(1) + + expect(invoice.applied_usage_thresholds.first.lifetime_usage_amount_cents) + .to eq(lifetime_usage.total_amount_cents) end context 'with multiple thresholds' do