diff --git a/app/graphql/resolvers/customers/usage_resolver.rb b/app/graphql/resolvers/customers/usage_resolver.rb index ad67f67fe73..b912137127d 100644 --- a/app/graphql/resolvers/customers/usage_resolver.rb +++ b/app/graphql/resolvers/customers/usage_resolver.rb @@ -15,7 +15,12 @@ class UsageResolver < Resolvers::BaseResolver type Types::Customers::Usage::Current, null: false def resolve(customer_id:, subscription_id:) - result = Invoices::CustomerUsageService.with_ids(context[:current_user], customer_id:, subscription_id:).call + result = Invoices::CustomerUsageService.with_ids( + context[:current_user], + customer_id:, + subscription_id:, + apply_taxes: false + ).call result.success? ? result.usage : result_error(result) end diff --git a/app/services/invoices/customer_usage_service.rb b/app/services/invoices/customer_usage_service.rb index aedf78ba91e..473802f801b 100644 --- a/app/services/invoices/customer_usage_service.rb +++ b/app/services/invoices/customer_usage_service.rb @@ -2,25 +2,26 @@ module Invoices class CustomerUsageService < BaseService - def initialize(current_user, customer:, subscription:) + def initialize(current_user, customer:, subscription:, apply_taxes: true) super(current_user) + @apply_taxes = apply_taxes @customer = customer @subscription = subscription end - def self.with_external_ids(customer_external_id:, external_subscription_id:, organization_id:) + def self.with_external_ids(customer_external_id:, external_subscription_id:, organization_id:, apply_taxes: true) customer = Customer.find_by!(external_id: customer_external_id, organization_id:) subscription = customer&.active_subscriptions&.find_by(external_id: external_subscription_id) - new(nil, customer:, subscription:) + new(nil, customer:, subscription:, apply_taxes:) rescue ActiveRecord::RecordNotFound result.not_found_failure!(resource: 'customer') end - def self.with_ids(current_user, customer_id:, subscription_id:) + def self.with_ids(current_user, customer_id:, subscription_id:, apply_taxes: true) customer = Customer.find_by(id: customer_id, organization_id: current_user.organization_ids) subscription = customer&.active_subscriptions&.find_by(id: subscription_id) - new(current_user, customer:, subscription:) + new(current_user, customer:, subscription:, apply_taxes:) rescue ActiveRecord::RecordNotFound result.not_found_failure!(resource: 'customer') end @@ -36,7 +37,7 @@ def call private - attr_reader :invoice, :subscription + attr_reader :invoice, :subscription, :apply_taxes delegate :plan, to: :subscription delegate :organization, to: :subscription @@ -56,10 +57,12 @@ def compute_usage add_charge_fees - if customer_provider_taxation? + if apply_taxes && customer_provider_taxation? compute_amounts_with_provider_taxes - else + elsif apply_taxes compute_amounts + else + compute_amounts_without_tax end format_usage @@ -141,6 +144,13 @@ def compute_amounts invoice.total_amount_cents = invoice.fees_amount_cents + invoice.taxes_amount_cents end + def compute_amounts_without_tax + invoice.fees_amount_cents = invoice.fees.sum(&:amount_cents) + invoice.taxes_amount_cents = 0 + invoice.taxes_rate = 0 + invoice.total_amount_cents = invoice.fees_amount_cents + end + def compute_amounts_with_provider_taxes invoice.fees_amount_cents = invoice.fees.sum(&:amount_cents) diff --git a/app/services/lifetime_usages/calculate_service.rb b/app/services/lifetime_usages/calculate_service.rb index cc731395764..b30ff3bb61e 100644 --- a/app/services/lifetime_usages/calculate_service.rb +++ b/app/services/lifetime_usages/calculate_service.rb @@ -52,7 +52,8 @@ def calculate_current_usage_amount_cents result = Invoices::CustomerUsageService.call( nil, # current_user customer: subscription.customer, - subscription: subscription + subscription: subscription, + apply_taxes: false ) result.usage.amount_cents end diff --git a/spec/graphql/resolvers/customers/usage_resolver_spec.rb b/spec/graphql/resolvers/customers/usage_resolver_spec.rb index 239e2d95194..70073d36af2 100644 --- a/spec/graphql/resolvers/customers/usage_resolver_spec.rb +++ b/spec/graphql/resolvers/customers/usage_resolver_spec.rb @@ -145,8 +145,8 @@ expect(usage_response['currency']).to eq('EUR') expect(usage_response['issuingDate']).to eq(Time.zone.today.end_of_month.iso8601) expect(usage_response['amountCents']).to eq('405') - expect(usage_response['totalAmountCents']).to eq('486') - expect(usage_response['taxesAmountCents']).to eq('81') + expect(usage_response['totalAmountCents']).to eq('405') + expect(usage_response['taxesAmountCents']).to eq('0') charge_usage = usage_response['chargesUsage'].first expect(charge_usage['billableMetric']['name']).to eq(metric.name) diff --git a/spec/services/invoices/customer_usage_service_spec.rb b/spec/services/invoices/customer_usage_service_spec.rb index 5700ed9cd38..b5463fabf9e 100644 --- a/spec/services/invoices/customer_usage_service_spec.rb +++ b/spec/services/invoices/customer_usage_service_spec.rb @@ -4,7 +4,7 @@ RSpec.describe Invoices::CustomerUsageService, type: :service, cache: :memory do subject(:usage_service) do - described_class.with_ids(membership.user, customer_id:, subscription_id:) + described_class.with_ids(membership.user, customer_id:, subscription_id:, apply_taxes:) end let(:membership) { create(:membership) } @@ -15,6 +15,7 @@ let(:subscription_id) { subscription&.id } let(:plan) { create(:plan, interval: 'monthly') } let(:timestamp) { Time.current } + let(:apply_taxes) { true } let(:subscription) do create( @@ -93,6 +94,31 @@ end end + context 'when apply_taxes property is set to false' do + let(:apply_taxes) { false } + + it 'initializes an invoice' do + result = usage_service.call + + aggregate_failures do + expect(result).to be_success + expect(result.invoice).to be_a(Invoice) + + expect(result.usage).to have_attributes( + from_datetime: Time.current.beginning_of_month.iso8601, + to_datetime: Time.current.end_of_month.iso8601, + issuing_date: Time.zone.today.end_of_month.iso8601, + currency: 'EUR', + amount_cents: 2532, # 1266 * 2, + taxes_amount_cents: 0, + total_amount_cents: 2532 + ) + expect(result.usage.fees.size).to eq(1) + expect(result.usage.fees.first.charge.invoice_display_name).to eq(charge.invoice_display_name) + end + end + end + context 'when there is tax provider integration' do let(:integration) { create(:anrok_integration, organization:) } let(:integration_customer) { create(:anrok_customer, integration:, customer:) }