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

misc: Remove Fee#{charge|subscription|commitment}_kind scope #2948

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion app/graphql/types/invoice_subscription/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def should_use_in_advance_charges_interval
return @should_use_in_advance_charges_interval if defined? @should_use_in_advance_charges_interval

@should_use_in_advance_charges_interval =
object.fees.charge_kind.any? &&
object.fees.charge.any? &&
rsempe marked this conversation as resolved.
Show resolved Hide resolved
object.subscription.plan.charges.where(pay_in_advance: true).any? &&
!object.subscription.plan.pay_in_advance?
end
Expand Down
4 changes: 0 additions & 4 deletions app/models/fee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ class Fee < ApplicationRecord
validates :true_up_fee_id, presence: false, unless: :charge?
validates :total_aggregated_units, presence: true, if: :charge?

scope :subscription_kind, -> { where(fee_type: :subscription) }
scope :charge_kind, -> { where(fee_type: :charge) }
scope :commitment_kind, -> { where(fee_type: :commitment) }

scope :positive_units, -> { where('units > ?', 0) }

# NOTE: pay_in_advance fees are not be linked to any invoice, but add_on fees does not have any subscriptions
Expand Down
6 changes: 3 additions & 3 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@ def fee_total_amount_cents
end

def charge_amount_cents
fees.charge_kind.sum(:amount_cents)
fees.charge.sum(:amount_cents)
end

def subscription_amount_cents
fees.subscription_kind.sum(:amount_cents)
fees.subscription.sum(:amount_cents)
end

def invoice_subscription(subscription_id)
Expand All @@ -185,7 +185,7 @@ def progressice_billing_credits(subscription)

def existing_fees_in_interval?(subscription_id:, charge_in_advance: false)
subscription_fees(subscription_id)
.charge_kind
.charge
.positive_units
.where(true_up_parent_fee: nil)
.joins(:charge)
Expand Down
6 changes: 3 additions & 3 deletions app/models/invoice_subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ def previous_invoice_subscription
end

def charge_amount_cents
fees.charge_kind.sum(:amount_cents)
fees.charge.sum(:amount_cents)
end

def subscription_amount_cents
subscription_fee&.amount_cents || 0
end

def subscription_fee
fees.subscription_kind.first
fees.subscription.first
end

def commitment_fee
fees.commitment_kind.first
fees.commitment.first
end

def total_amount_cents
Expand Down
2 changes: 1 addition & 1 deletion app/models/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def next_subscription
end

def already_billed?
fees.subscription_kind.any?
fees.subscription.any?
end

def starting_in_the_future?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def subscription_fees
).call

Fee
.subscription_kind
.subscription
.joins(subscription: :plan)
.where(
"(fees.properties->>'from_datetime') >= ?",
Expand All @@ -45,7 +45,7 @@ def charge_fees
invoices_result = FetchInvoicesService.call(commitment: minimum_commitment, invoice_subscription:)

Fee
.charge_kind
.charge
.joins(:charge)
.where(
subscription_id: subscription.id,
Expand All @@ -61,7 +61,7 @@ def charge_in_advance_fees
).call

Fee
.charge_kind
.charge
.joins(:charge)
.where(
subscription_id: subscription.id,
Expand All @@ -87,7 +87,7 @@ def charge_in_advance_recurring_fees
).call

Fee
.charge_kind
.charge
.joins(:charge)
.joins(charge: :billable_metric)
.where(billable_metric: {recurring: true})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def subscription_fees
invoices_result = FetchInvoicesService.call(commitment: minimum_commitment, invoice_subscription:)

Fee
.subscription_kind
.subscription
.joins(subscription: :plan)
.where(
subscription_id: subscription.id,
Expand All @@ -26,7 +26,7 @@ def charge_fees
).call

Fee
.charge_kind
.charge
.joins(:charge)
.where(
subscription_id: subscription.id,
Expand All @@ -49,7 +49,7 @@ def charge_in_advance_fees
).call

Fee
.charge_kind
.charge
.joins(:charge)
.where(
subscription_id: subscription.id,
Expand Down Expand Up @@ -84,7 +84,7 @@ def charge_in_advance_recurring_fees
).call

scope = Fee
.charge_kind
.charge
.joins(:charge)
.joins(charge: :billable_metric)
.where(billable_metric: {recurring: true})
Expand Down
2 changes: 1 addition & 1 deletion app/services/fees/commitments/minimum/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def call
delegate :organization, to: :invoice

def invoice_has_minimum_commitment_fee?
invoice.fees.commitment_kind.where(subscription:).any? { |fee| fee.invoiceable.minimum_commitment? }
invoice.fees.commitment.where(subscription:).any? { |fee| fee.invoiceable.minimum_commitment? }
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/fees/subscription_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def adjusted_fee
end

def already_billed?
existing_fee = invoice.fees.subscription_kind.find_by(subscription_id: subscription.id)
existing_fee = invoice.fees.subscription.find_by(subscription_id: subscription.id)
return false unless existing_fee

result.fee = existing_fee
Expand Down Expand Up @@ -138,7 +138,7 @@ def should_use_full_amount?
# However, we should not bill full amount if subscription is downgraded since in that case, first invoice
# should be prorated (this part is covered with first_subscription_amount method).
return true if plan.pay_in_advance? && subscription.anniversary? && !subscription.previous_subscription_id
return true if subscription.fees.subscription_kind.where('created_at < ?', invoice.created_at).exists?
return true if subscription.fees.subscription.where('created_at < ?', invoice.created_at).exists?
return true if subscription.started_in_past? && plan.pay_in_advance?

if subscription.started_in_past? &&
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 @@ -226,7 +226,7 @@ def should_create_subscription_fee?(subscription, boundaries)
# NOTE: When plan is pay in advance we generate an invoice upon subscription creation
# We want to prevent creating subscription fee if subscription creation already happened on billing day
fee_exists = subscription.fees
.subscription_kind
.subscription
.where(created_at: issuing_date.beginning_of_day..issuing_date.end_of_day)
.where.not(invoice_id: invoice.id)
.any?
Expand Down
2 changes: 1 addition & 1 deletion app/services/subscriptions/free_trial_billing_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def call
# during the customer trial period
# Unfortunately, this introduces an N+1 query
def already_billed_on_day_one?(subscription)
Fee.subscription_kind.where(
Fee.subscription.where(
invoice_id: subscription.invoice_subscriptions.select('invoices.id').joins(:invoice).where(
'invoices.invoice_type' => :subscription,
'invoices.status' => %i[draft finalized],
Expand Down
8 changes: 4 additions & 4 deletions app/views/templates/invoices/v1.slim
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ html
- elsif subscription?
td.body-1 style="text-align: right;" width="30%"
= subscription_amount.format(format: I18n.t('money.format'), decimal_mark: I18n.t('money.decimal_mark'), thousands_separator: I18n.t('money.thousands_separator'))
- if fees.charge_kind.any?
- if fees.charge.any?
tr
td width="70%"
.body-1 = I18n.t('invoice.all_usage_based_fees')
Expand Down Expand Up @@ -499,19 +499,19 @@ html
.body-1
= I18n.t('invoice.subscription_interval', plan_interval: I18n.t("invoice.#{subscription.plan.interval}"), plan_name: subscription.plan.invoice_name)
td.body-1 style="text-align: right;" width="30%"
= subscription_fees(subscription.id).subscription_kind.first&.amount&.format(format: I18n.t('money.format'), decimal_mark: I18n.t('money.decimal_mark'), thousands_separator: I18n.t('money.thousands_separator'))
= subscription_fees(subscription.id).subscription.first&.amount&.format(format: I18n.t('money.format'), decimal_mark: I18n.t('money.decimal_mark'), thousands_separator: I18n.t('money.thousands_separator'))
table.total-table width="100%"
tr
td.body-2 width="70%" = I18n.t('invoice.sub_total')
td.body-1 width="30%" = invoice_subscription(subscription.id).subscription_amount.format(format: I18n.t('money.format'), decimal_mark: I18n.t('money.decimal_mark'), thousands_separator: I18n.t('money.thousands_separator'))

- if subscription? && subscription_fees(subscription.id).charge_kind.any?
- if subscription? && subscription_fees(subscription.id).charge.any?
.body-1 = I18n.t('invoice.usage_based_fees')
.mb-24.body-3
= I18n.t('invoice.list_of_charges', from: I18n.l(invoice_subscription(subscription.id).charges_from_datetime_in_customer_timezone&.to_date, format: :default), to: I18n.l(invoice_subscription(subscription.id).charges_to_datetime_in_customer_timezone&.to_date, format: :default))
.charge-details.mb-24
table.charge-details-table width="100%"
- subscription_fees(subscription.id).charge_kind.group_by(&:charge_id).each do |_charge_id, fees|
- subscription_fees(subscription.id).charge.group_by(&:charge_id).each do |_charge_id, fees|
- fee = fees.first
- if fees.all? { |f| f.group_id? } && fees.sum(&:units) > 0
tr
Expand Down
8 changes: 4 additions & 4 deletions app/views/templates/invoices/v2.slim
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ html
- elsif subscription?
td.body-1 style="text-align: right;" width="30%"
= subscription_amount.format(format: I18n.t('money.format'), decimal_mark: I18n.t('money.decimal_mark'), thousands_separator: I18n.t('money.thousands_separator'))
- if fees.charge_kind.any?
- if fees.charge.any?
tr
td width="70%"
.body-1 = I18n.t('invoice.all_usage_based_fees')
Expand Down Expand Up @@ -508,19 +508,19 @@ html
.body-1
= I18n.t('invoice.subscription_interval', plan_interval: I18n.t("invoice.#{subscription.plan.interval}"), plan_name: subscription.plan.invoice_name)
td.body-1 style="text-align: right;" width="30%"
= subscription_fees(subscription.id).subscription_kind.first&.amount&.format(format: I18n.t('money.format'), decimal_mark: I18n.t('money.decimal_mark'), thousands_separator: I18n.t('money.thousands_separator'))
= subscription_fees(subscription.id).subscription.first&.amount&.format(format: I18n.t('money.format'), decimal_mark: I18n.t('money.decimal_mark'), thousands_separator: I18n.t('money.thousands_separator'))
table.total-table width="100%"
tr
td.body-2 width="70%" = I18n.t('invoice.sub_total')
td.body-1 width="30%" = invoice_subscription(subscription.id).subscription_amount.format(format: I18n.t('money.format'), decimal_mark: I18n.t('money.decimal_mark'), thousands_separator: I18n.t('money.thousands_separator'))

- if subscription? && subscription_fees(subscription.id).charge_kind.any?
- if subscription? && subscription_fees(subscription.id).charge.any?
.body-1 = I18n.t('invoice.usage_based_fees')
.mb-24.body-3
= I18n.t('invoice.list_of_charges', from: I18n.l(invoice_subscription(subscription.id).charges_from_datetime_in_customer_timezone&.to_date, format: :default), to: I18n.l(invoice_subscription(subscription.id).charges_to_datetime_in_customer_timezone&.to_date, format: :default))
.charge-details.mb-24
table.charge-details-table width="100%"
- subscription_fees(subscription.id).charge_kind.where(true_up_parent_fee: nil).group_by(&:charge_id).each do |_charge_id, fees|
- subscription_fees(subscription.id).charge.where(true_up_parent_fee: nil).group_by(&:charge_id).each do |_charge_id, fees|
- fee = fees.first
- if fees.all? { |f| f.group_id? } && fees.sum(&:units) > 0
tr
Expand Down
10 changes: 5 additions & 5 deletions app/views/templates/invoices/v3/_subscription_details.slim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
h2.title-2.mb-24 class="#{'invoice-details-title' if subscriptions.count > 1}" = I18n.t('invoice.details', resource: subscription.invoice_name)

/ Subscription fee section
.invoice-resume.overflow-auto class="#{'mb-24' if subscription_fees(subscription.id).charge_kind.any?}"
.invoice-resume.overflow-auto class="#{'mb-24' if subscription_fees(subscription.id).charge.any?}"
table.invoice-resume-table width="100%"
tr
td.body-2 = I18n.t('invoice.fees_from_to_date', from_date: I18n.l(invoice_subscription(subscription.id).from_datetime_in_customer_timezone&.to_date, format: :default), to_date: I18n.l(invoice_subscription(subscription.id).to_datetime_in_customer_timezone&.to_date, format: :default))
Expand All @@ -18,7 +18,7 @@
td.body-2 = MoneyHelper.format(invoice_subscription(subscription.id).subscription_amount)

/ Charge fees section for subsctiption invoice
- if subscription? && subscription_fees(subscription.id).charge_kind.any?
- if subscription? && subscription_fees(subscription.id).charge.any?

/ Charges payed in arrears OR charges and plan payed in advance
- if (subscription.plan.charges.where(pay_in_advance: false).any? || (subscription.plan.charges.where(pay_in_advance: true).any? && subscription.plan.pay_in_advance?))
Expand All @@ -31,7 +31,7 @@
td.body-2 = I18n.t('invoice.amount_without_tax')

/ Loop over all top level fees
- subscription_fees(subscription.id).charge_kind.where(true_up_parent_fee: nil).group_by(&:charge_id).each do |_charge_id, fees|
- subscription_fees(subscription.id).charge.where(true_up_parent_fee: nil).group_by(&:charge_id).each do |_charge_id, fees|
- fee = fees.first
- next if fee.charge.pay_in_advance? && !fee.charge.plan.pay_in_advance?

Expand Down Expand Up @@ -93,7 +93,7 @@
td.body-2 = I18n.t('invoice.amount_without_tax')

/ Loop over all top level fees
- subscription_fees(subscription.id).charge_kind.where(true_up_parent_fee: nil).group_by(&:charge_id).each do |_charge_id, fees|
- subscription_fees(subscription.id).charge.where(true_up_parent_fee: nil).group_by(&:charge_id).each do |_charge_id, fees|
- fee = fees.first
- next unless fee.charge.pay_in_advance?

Expand Down Expand Up @@ -197,7 +197,7 @@
= MoneyHelper.format(invoice_subscription(subscription.id).total_amount)

/ Recuring fees breakdown
- if subscription? && subscription_fees(subscription.id).charge_kind.any?
- if subscription? && subscription_fees(subscription.id).charge.any?
.invoice-resume.mb-24.overflow-auto
- recurring_fees(subscription.id).group_by(&:charge_id).each do |_charge_id, fees|
- next unless fees.sum(&:units) > 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
td.body-2 = I18n.t('invoice.amount')

/ Charge fees section for subscription invoice
- if subscription_fees(subscription.id).charge_kind.any?
- if subscription_fees(subscription.id).charge.any?
/ Charges payed in arrears OR charges and plan payed in advance
- if subscription.plan.charges.any?
.invoice-resume.overflow-auto
table.invoice-resume-table width="100%"

/ Loop over all top level fees
- subscription_fees(subscription.id).charge_kind.positive_units.where(true_up_parent_fee: nil).joins(charge: :billable_metric).sort_by { |f| f.invoice_sorting_clause }.group_by(&:charge_id).each do |_charge_id, fees|
- subscription_fees(subscription.id).charge.positive_units.where(true_up_parent_fee: nil).joins(charge: :billable_metric).sort_by { |f| f.invoice_sorting_clause }.group_by(&:charge_id).each do |_charge_id, fees|
- fee = fees.first
- next if fee.charge.pay_in_advance?

Expand Down
14 changes: 7 additions & 7 deletions app/views/templates/invoices/v4/_subscription_details.slim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
h2.title-2.mb-24 class="#{'invoice-details-title' if subscriptions.count > 1}" = I18n.t('invoice.details', resource: subscription.invoice_name)

/ Subscription fee section
.invoice-resume.overflow-auto class="#{'mb-24' if subscription_fees(subscription.id).charge_kind.any? && different_boundaries_for_subscription_and_charges(subscription)}"
.invoice-resume.overflow-auto class="#{'mb-24' if subscription_fees(subscription.id).charge.any? && different_boundaries_for_subscription_and_charges(subscription)}"
table.invoice-resume-table width="100%"
tr.first_child
td.body-2 = I18n.t('invoice.fees_from_to_date', from_date: I18n.l(invoice_subscription.from_datetime_in_customer_timezone&.to_date, format: :default), to_date: I18n.l(invoice_subscription.to_datetime_in_customer_timezone&.to_date, format: :default))
Expand All @@ -27,11 +27,11 @@
td.body-2 == TaxHelper.applied_taxes(subscription_fee)
td.body-2 = MoneyHelper.format(invoice_subscription.subscription_amount)

- if subscription? && subscription_fees(subscription.id).charge_kind.any?
- if subscription? && subscription_fees(subscription.id).charge.any?
/ Charges payed in advance on payed in advance plan
- if subscription.plan.charges.where(pay_in_advance: true).any? && subscription.plan.pay_in_advance?
/ Loop over all top level fees
- subscription_fees(subscription.id).charge_kind.positive_units.where(true_up_parent_fee: nil).joins(charge: :billable_metric).sort_by { |f| f.invoice_sorting_clause }.group_by(&:charge_id).each do |_charge_id, fees|
- subscription_fees(subscription.id).charge.positive_units.where(true_up_parent_fee: nil).joins(charge: :billable_metric).sort_by { |f| f.invoice_sorting_clause }.group_by(&:charge_id).each do |_charge_id, fees|
- fee = fees.first
- next unless fee.charge.pay_in_advance?

Expand All @@ -53,7 +53,7 @@
== SlimHelper.render('templates/invoices/v4/_fees_without_filters', fee)

/ Charge fees section for subscription invoice
- if subscription? && subscription_fees(subscription.id).charge_kind.any?
- if subscription? && subscription_fees(subscription.id).charge.any?
/ Charges payed in arrears OR charges and plan payed in advance
- if subscription.plan.charges.where(pay_in_advance: false).any? && existing_fees_in_interval?(subscription_id: subscription.id)
.invoice-resume.overflow-auto
Expand All @@ -67,7 +67,7 @@
td.body-2 = I18n.t('invoice.amount')

/ Loop over all top level fees
- subscription_fees(subscription.id).charge_kind.positive_units.where(true_up_parent_fee: nil).joins(charge: :billable_metric).sort_by { |f| f.invoice_sorting_clause }.group_by(&:charge_id).each do |_charge_id, fees|
- subscription_fees(subscription.id).charge.positive_units.where(true_up_parent_fee: nil).joins(charge: :billable_metric).sort_by { |f| f.invoice_sorting_clause }.group_by(&:charge_id).each do |_charge_id, fees|
- fee = fees.first
- next if fee.charge.pay_in_advance?

Expand Down Expand Up @@ -110,7 +110,7 @@
td.body-2 = I18n.t('invoice.amount')

/ Loop over all top level fees
- subscription_fees(subscription.id).charge_kind.positive_units.where(true_up_parent_fee: nil).joins(charge: :billable_metric).sort_by { |f| f.invoice_sorting_clause }.group_by(&:charge_id).each do |_charge_id, fees|
- subscription_fees(subscription.id).charge.positive_units.where(true_up_parent_fee: nil).joins(charge: :billable_metric).sort_by { |f| f.invoice_sorting_clause }.group_by(&:charge_id).each do |_charge_id, fees|
- fee = fees.first
- next unless fee.charge.pay_in_advance?

Expand Down Expand Up @@ -201,7 +201,7 @@
= MoneyHelper.format(invoice_subscription.total_amount)

/ Recuring fees breakdown
- if subscription? && subscription_fees(subscription.id).charge_kind.any?
- if subscription? && subscription_fees(subscription.id).charge.any?
.invoice-resume.mb-24.overflow-auto
- recurring_fees(subscription.id).group_by(&:charge_id).each do |_charge_id, fees|
- next unless fees.sum(&:units) > 0
Expand Down
Loading
Loading